]> git.jsancho.org Git - dungeon-master.git/blob - src/mods.cpp
7b9115e6320618ce749112f79ec9a15203e2c8a5
[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 <libguile.h>
20 #include "generators.h"
21 #include "mods.h"
22 #include "paths.h"
23
24 SCM
25 register_scene_generator (SCM name,
26                           SCM type,
27                           SCM proc)
28 {
29   SceneGenerator* generator =
30     register_generator (scm_to_utf8_string (name),
31                         scm_to_utf8_string (scm_symbol_to_string (type)),
32                         proc);
33   printf ("Register: %s (%s)\n", generator->name, generator->type);
34   scm_call_1 (generator->proc, scm_from_int (-1));
35   return SCM_UNSPECIFIED;
36 }
37
38 void
39 init_dungeon_master_module (void *unused)
40 {
41   scm_c_define_gsubr ("register-scene-generator", 3, 0, 0, (scm_t_subr) register_scene_generator);
42   scm_c_export ("register-scene-generator", NULL);
43 }
44
45 void
46 scm_init_dungeon_master_module ()
47 {
48   scm_c_define_module ("dungeon-master", init_dungeon_master_module, NULL);
49 }
50
51 void
52 add_to_load_path (std::string path)
53 {
54   // Add path to %load-path variable, needed for mods structured like modules
55   std::string exp = "(add-to-load-path \"" + path + "\")";
56   scm_c_eval_string (exp.c_str ());
57 }
58
59 void
60 load_dmaster_mods ()
61 {
62   scm_init_guile ();
63   scm_init_dungeon_master_module ();
64
65   std::set<std::string> paths = get_dmaster_paths ();
66   DIR* mods_dir;
67   struct dirent* mod;
68   std::string
69     mods_path,
70     mod_main;
71
72   for (const std::string &path : paths)
73     {
74       mods_path = path + PATH_DELIM + "mods";
75       add_to_load_path (mods_path);
76       mods_dir = opendir (mods_path.c_str ());
77       if (mods_dir != NULL)
78         {
79           while (mod = readdir (mods_dir))
80             {
81               if (strstr (mod->d_name, ".scm") != NULL) {
82                 mod_main = mods_path + PATH_DELIM + mod->d_name;
83                 scm_primitive_load (scm_from_locale_string (mod_main.c_str ()));
84               }
85             }
86         }
87       closedir (mods_dir);
88     }
89 }