Skip to content

Commit 2dcae38

Browse files
authored
Add support for loading Mod JS includes (#3)
* Added JS loading * Pass Zip to next promises * Corrected typo * Correct Hello World Mod * Correct ModManager definition
1 parent cdb802e commit 2dcae38

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

API/GDMod.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
* An interface descibing a Mod.
33
* @interface
44
*/
5-
GDAPI.Mod = function() {};
5+
GDAPI.Mod = function() {
6+
/**
7+
* The mod's name
8+
* @type {string}
9+
*/
10+
this.name = "";
11+
12+
/**
13+
* The mod's uid
14+
* @type {string}
15+
*/
16+
this.uid = "";
17+
};
618

719
/**
820
* Function called while the mod is loading.
@@ -28,6 +40,27 @@ GDAPI.Mod.prototype.postEvent = function(runtimeScene) {};
2840
*/
2941
GDAPI.Mod.prototype.onGameStart = function(runtimeScene) {}
3042

43+
/**
44+
* A Manager for mods.
45+
* @namespace
46+
*/
47+
GDAPI.ModManager = {
48+
mods: {}
49+
};
50+
51+
/**
52+
* Adds a mod to the manager.
53+
* @param {GDAPI.Mod} mod - The mod to add to the manager.
54+
* @returns {boolean} - Was the adding successful?
55+
*/
56+
GDAPI.ModManager.add = function(mod) {
57+
if(mod.uid in this.mods) {
58+
return false;
59+
}
60+
this.mods[mod.uid] = mod;
61+
return true;
62+
}
63+
3164
/**
3265
* Loads a mod from a zip.
3366
* This is what is used to actually load a modfile.
@@ -71,12 +104,18 @@ GDAPI.loadZipMod = function(modAsZip) {
71104
.catch(() => {
72105
reject("The manifest resources.json cannot be parsed! Is it valid JSON?")
73106
})
74-
.then(resolver);
107+
.then(() => resolver(zip));
108+
})
109+
.then((zip) => {
110+
let promises = [];
111+
for (let include of manifests.includes) {
112+
promises.push(
113+
zip.file("code/"+include).async("string")
114+
.then(jsFile => eval(jsFile))
115+
)
116+
}
117+
return Promise.all(promises);
75118
});
76-
77-
}).then(() => {
78-
console.log(manifests);
79-
resolve();
80119
});
81120
}).catch((error) => {
82121
console.error("Error while loading mod file: " + error.toString());
@@ -89,6 +128,7 @@ GDAPI.loadZipMod = function(modAsZip) {
89128
* @param {GDAPI.Mod} mod - The {@link GDAPI.Mod} instance.
90129
*/
91130
GDAPI.loadMod = function(mod) {
131+
if(!GDAPI.ModManager.add(mod)) return;
92132
GDAPI.registerCallback(GDAPI.CALLBACKS.PRE_EVENTS, mod.preEvent);
93133
GDAPI.registerCallback(GDAPI.CALLBACKS.POST_EVENTS, mod.postEvent);
94134
GDAPI.registerCallback(GDAPI.CALLBACKS.FIRST_SCENE_LOADED, mod.onGameStart);
139 Bytes
Binary file not shown.

Mod Templates/Hello World/code/main.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
* My mods shared data
55
* @namespace
66
*/
7-
MyAwesomeMod = {};
7+
window.MyAwesomeMod = {};
88

99
/**
1010
* My Hello World Mod :)
1111
* @extends {GDAPI.Mod}
1212
*/
13-
class MyAwesomeMod.HelloWorldMod extends GDAPI.Mod {
13+
class HelloWorldMod extends GDAPI.Mod {
1414
initialize() {
1515
MyAwesomeMod.message = "Hello World!";
16-
},
16+
};
1717
preEvent(runtimeScene) {
1818
console.log(MyAwesomeMod.message);
19-
}
19+
};
2020
}
2121

22+
MyAwesomeMod.HelloWorldMod = HelloWorldMod;
23+
2224
GDAPI.loadMod(new MyAwesomeMod.HelloWorldMod());

0 commit comments

Comments
 (0)