]> git.jsancho.org Git - dungeon-master.git/blob - src/mods.cpp
dea09b633b0e40261e5d25c060b849e95bc3339b
[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 add_to_load_path (std::string path)
47 {
48   // Add path to %load-path variable, needed for modules created in mods
49   std::string exp = "(add-to-load-path \"" + path + "\")";
50   scm_c_eval_string (exp.c_str ());
51 }
52
53 void load_dmaster_mods ()
54 {
55   scm_init_guile ();
56   scm_init_dungeon_master_module ();
57
58   std::set<std::string> paths = get_dmaster_paths ();
59   DIR* mods_dir;
60   struct dirent* mod;
61   std::string
62     mods_path,
63     mod_main;
64
65   for (const std::string &path : paths)
66     {
67       mods_path = path + PATH_DELIM + "mods";
68       add_to_load_path (mods_path);
69       mods_dir = opendir (mods_path.c_str ());
70       if (mods_dir != NULL)
71         {
72           while (mod = readdir (mods_dir))
73             {
74               if (strstr (mod->d_name, ".scm") != NULL) {
75                 mod_main = mods_path + PATH_DELIM + mod->d_name;
76                 scm_primitive_load (scm_from_locale_string (mod_main.c_str ()));
77               }
78             }
79         }
80       closedir (mods_dir);
81     }
82 }