Skip to content

Commit c8cc517

Browse files
scrlysAWSWCommunity
authored andcommitted
Now dynamically imports mods!
All mods except for testing mod is removed
1 parent d72fbe2 commit c8cc517

File tree

14 files changed

+66
-344
lines changed

14 files changed

+66
-344
lines changed

modloader/__init__.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
1+
import os
2+
import sys
3+
import importlib
4+
5+
import renpy
6+
17
import modinfo
28

3-
import test
4-
print(modinfo.modlist)
9+
def get_mod_path():
10+
"""Get the mod path
11+
12+
Returns:
13+
The full path to the mods folder
14+
"""
15+
#TODO: Use a path combining function
16+
return renpy.config.gamedir + "/mods/"
17+
18+
# By setting the import path to the mod folder, we can do something like `import mod`
19+
# NOTE: To import files in the modloader/ folder, do `from modloader import ...`
20+
# If we add the modloader to the path, the modlist would get reloaded again
21+
sys.path.append(get_mod_path())
22+
23+
for mod in os.listdir(get_mod_path()):
24+
# Some mods require resources to be recognized by renpy. If a folder exist, force renpy to load it
25+
resource_dir = get_mod_path() + mod + "/resource"
26+
if os.path.isdir(resource_dir):
27+
renpy.config.searchpath.append(resource_dir)
28+
29+
# Try importing the mod. If all goes well, the mod is imported through the Mod class
30+
try:
31+
importlib.import_module(mod)
32+
except Exception, e:
33+
print("Oh no in {}".format(mod))
34+
print(e)
35+
raise e # Raise it again even though the stacktrace provides nothing useful
36+
37+
# After all mods are loaded, call their respective mod_complete functions
38+
for mod_name, mod in modinfo.get_mods().iteritems():
39+
print("Completing mod {}".format(mod_name))
40+
mod.mod_complete()

modloader/modclass.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class Mod():
44
"""The Mod class
55
66
This is supposed to act like a superclass for mods.
7+
Execution order is as follows:
8+
mod_load -> mod_complete
79
"""
810
def mod_info(self):
911
"""Get the mod info
@@ -13,11 +15,31 @@ def mod_info(self):
1315
"""
1416
raise Exception("Mod info isn't overriden")
1517

18+
def mod_load(self):
19+
"""Executes when a mod is loaded
20+
21+
This is where you put special renpy code
22+
Other mods may not be fully loaded yet. If you want this functionality, see mod_complete
23+
"""
24+
pass
25+
26+
def mod_complete(self):
27+
"""Executes when all mods are loaded"""
28+
pass
29+
1630
def loadable_mod(clazz):
1731
"""Annotation to add a Mod subclass to the mod list
1832
1933
Args:
2034
clazz (Mod): The Mod class
35+
36+
Raises:
37+
Exception: If the given class is not a subclass of Mod
2138
"""
39+
if not issubclass(clazz, Mod):
40+
raise Exception("Class must be a subclass of Mod")
41+
2242
mod = clazz() # Create a new instance of the class
23-
modinfo.add_mod(mod.mod_info()[0], mod)
43+
mod_name, _, _ = mod.mod_info() # Get just the mod name
44+
mod.mod_load() # Load the mod
45+
modinfo.add_mod(mod_name, mod)

modloader/modinfo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
def add_mod(mod_name, mod):
44
print("Adding mod {}".format(mod_name))
55
modlist[mod_name] = mod
6+
7+
def get_mods():
8+
return modlist

mods/anna_post_true/anna_rpy_scene.rpy

Lines changed: 0 additions & 171 deletions
This file was deleted.

mods/anna_post_true/mod.py

Lines changed: 0 additions & 32 deletions
This file was deleted.
-24.9 KB
Binary file not shown.
-24.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)