Skip to content

Commit 31042a9

Browse files
committed
Allow addons in mods folder
This allows people to add addons using `mods/<mymod>/addons/<myaddon>/`
1 parent e9e36ed commit 31042a9

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

source/funkin/backend/system/MainState.hx

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import funkin.menus.TitleState;
88
import funkin.menus.BetaWarningState;
99
import funkin.backend.chart.EventsData;
1010
import flixel.FlxState;
11+
import haxe.io.Path;
12+
13+
@dox(hide)
14+
typedef AddonInfo = {
15+
var name:String;
16+
var path:String;
17+
}
1118

1219
/**
1320
* Simple state used for loading the game
@@ -32,25 +39,64 @@ class MainState extends FlxState {
3239
Paths.assetsTree.reset();
3340

3441
#if MOD_SUPPORT
35-
var _lowPriorityAddons:Array<String> = [];
36-
var _highPriorityAddons:Array<String> = [];
37-
var _noPriorityAddons:Array<String> = [];
38-
if (FileSystem.exists(ModsFolder.addonsPath) && FileSystem.isDirectory(ModsFolder.addonsPath)) {
39-
for(i=>addon in [for(dir in FileSystem.readDirectory(ModsFolder.addonsPath)) if (FileSystem.isDirectory('${ModsFolder.addonsPath}$dir')) dir]) {
40-
if (addon.startsWith("[LOW]")) _lowPriorityAddons.insert(0, addon);
41-
else if (addon.startsWith("[HIGH]")) _highPriorityAddons.insert(0, addon);
42-
else _noPriorityAddons.insert(0, addon);
42+
inline function isDirectory(path:String):Bool
43+
return FileSystem.exists(path) && FileSystem.isDirectory(path);
44+
45+
inline function ltrim(str:String, prefix:String):String
46+
return str.substr(prefix.length).ltrim();
47+
48+
inline function loadLib(path:String, name:String)
49+
Paths.assetsTree.addLibrary(ModsFolder.loadModLib(path, name));
50+
51+
var _lowPriorityAddons:Array<AddonInfo> = [];
52+
var _highPriorityAddons:Array<AddonInfo> = [];
53+
var _noPriorityAddons:Array<AddonInfo> = [];
54+
55+
var addonPaths = [
56+
ModsFolder.addonsPath,
57+
(
58+
ModsFolder.currentModFolder != null ?
59+
ModsFolder.modsPath + ModsFolder.currentModFolder + "/addons/" :
60+
null
61+
)
62+
];
63+
64+
for(path in addonPaths) {
65+
if (path == null) continue;
66+
if (!isDirectory(path)) continue;
67+
68+
for(addon in FileSystem.readDirectory(path)) {
69+
if(!FileSystem.isDirectory(path + addon)) {
70+
switch(Path.extension(addon).toLowerCase()) {
71+
case 'zip':
72+
addon = Path.withoutExtension(addon);
73+
default:
74+
continue;
75+
}
76+
}
77+
78+
var data:AddonInfo = {
79+
name: addon,
80+
path: path + addon
81+
};
82+
83+
if (addon.startsWith("[LOW]")) _lowPriorityAddons.insert(0, data);
84+
else if (addon.startsWith("[HIGH]")) _highPriorityAddons.insert(0, data);
85+
else _noPriorityAddons.insert(0, data);
4386
}
44-
for (addon in _lowPriorityAddons)
45-
Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', StringTools.ltrim(addon.substr("[LOW]".length))));
4687
}
88+
89+
for (addon in _lowPriorityAddons)
90+
loadLib(addon.path, ltrim(addon.name, "[LOW]"));
91+
4792
if (ModsFolder.currentModFolder != null)
48-
Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.modsPath}${ModsFolder.currentModFolder}', ModsFolder.currentModFolder));
93+
loadLib(ModsFolder.modsPath + ModsFolder.currentModFolder, ModsFolder.currentModFolder);
4994

50-
if (FileSystem.exists(ModsFolder.addonsPath) && FileSystem.isDirectory(ModsFolder.addonsPath)){
51-
for (addon in _noPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', addon));
52-
for (addon in _highPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', StringTools.ltrim(addon.substr("[HIGH]".length))));
53-
}
95+
for (addon in _noPriorityAddons)
96+
loadLib(addon.path, addon.name);
97+
98+
for (addon in _highPriorityAddons)
99+
loadLib(addon.path, ltrim(addon.name, "[HIGH]"));
54100
#end
55101

56102
MusicBeatTransition.script = "";

0 commit comments

Comments
 (0)