]> git.jsancho.org Git - dungeon-master.git/blob - src/mods.cpp
license
[dungeon-master.git] / src / mods.cpp
1 /* Dungeon Master --- RPG Adventure Generator
2    Copyright © 2019 Javier Sancho <jsf@jsancho.org>
3
4    Dungeon Master is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    Dungeon Master is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with Dungeon Master. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <dirent.h>
19 #include <iostream>
20 #include <libguile.h>
21 #include "generators.h"
22 #include "mods.h"
23 #include "paths.h"
24
25
26 SCM register_scene_generator(SCM name, SCM type, SCM proc)
27 {
28   SceneGenerator generator {scm_to_locale_string(name), scm_to_locale_string(type), proc };
29   register_generator(generator);
30   printf ("Register: %s (%s)\n", generator.name.c_str(), generator.type.c_str());
31   scm_call_1(proc, scm_from_int(-1));
32   return SCM_UNSPECIFIED;
33 }
34
35 void init_dungeon_master_module(void *unused)
36 {
37   scm_c_define_gsubr("register-scene-generator", 3, 0, 0, (scm_t_subr)register_scene_generator);
38   scm_c_export("register-scene-generator", NULL);
39 }
40
41 void scm_init_dungeon_master_module()
42 {
43   scm_c_define_module("dungeon-master", init_dungeon_master_module, NULL);
44 }
45
46 void load_mods()
47 {
48   scm_init_guile();
49   scm_init_dungeon_master_module();
50
51   std::set<std::string> paths = get_dm_paths();
52   DIR* mods_dir;
53   struct dirent* mod;
54   std::string
55     mods_path,
56     mod_main;
57
58   for (const std::string &path : paths) {
59     mods_path = path + DIR_DELIM + "mods";
60     mods_dir = opendir(mods_path.c_str());
61     if (mods_dir != NULL) {
62       while (mod = readdir(mods_dir)) {
63         if (strcmp(mod->d_name, ".") != 0 && strcmp(mod->d_name, "..") != 0) {
64           mod_main = mods_path + DIR_DELIM + mod->d_name + DIR_DELIM + "main.scm";
65           scm_primitive_load(scm_from_locale_string(mod_main.c_str()));
66         }
67       }
68     }
69     closedir(mods_dir);
70   }
71 }