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 */
2941GDAPI . 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 */
91130GDAPI . 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 ) ;
0 commit comments