@@ -16,6 +16,8 @@ Future<ServerProcessed> _computeEvent(
1616 );
1717}
1818
19+ const scriptSuffix = '.lua' ;
20+
1921class WorldBloc extends Bloc <PlayableWorldEvent , WorldState >
2022 with ServerInterface {
2123 final SetonixServer server;
@@ -52,14 +54,14 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
5254 processed.responses.forEach (process);
5355 if (event is WorldInitialized ) {
5456 server.log (
55- "World initialized${(event .info ?.script != null ) ? " with script ${event .info ?.script }" : "" }" ,
57+ "World initialized${(event .info ?.gameMode != null ) ? " with script ${event .info ?.gameMode }" : "" }" ,
5658 level: LogLevel .info,
5759 );
5860 _serverPlugin = await _pluginSystem.registerPlugin (
5961 '' ,
6062 SetonixPlugin .new ,
6163 );
62- await _loadScripts ((newState ?? state).info.script );
64+ await _loadScripts ((newState ?? state).info.gameMode );
6365 }
6466 if (newState == null ) return ;
6567 emit (newState);
@@ -79,11 +81,19 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
7981 }
8082 }
8183
82- Future <void > _loadScripts (ItemLocation ? location) async {
84+ Future <void > _loadGameMode (ItemLocation location) async {
85+ final mode = assetManager.getPack (location.namespace)? .getMode (location.id);
86+ if (mode == null ) return ;
87+ final script = mode.script;
88+ if (script == null ) return ;
89+ final scriptLocation = ItemLocation .fromString (script, location.namespace);
90+ pluginSystem.loadLuaPluginFromLocation (assetManager, scriptLocation);
91+ }
92+
93+ Future <void > _loadScripts (ItemLocation ? mode) async {
94+ pluginSystem.unregisterAll ();
8395 try {
84- if (location != null ) {
85- pluginSystem.loadLuaPluginFromLocation (assetManager, location);
86- }
96+ if (mode != null ) await _loadGameMode (mode);
8797 } catch (e) {
8898 server.log ('Error loading script: $e ' , level: LogLevel .error);
8999 }
@@ -94,16 +104,19 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
94104 }
95105 final scriptFiles = (await scriptsFolder.list ().toList ())
96106 .whereType <File >()
97- .where ((file) => file.path.endsWith ('.lua' ));
107+ .where ((file) => file.path.endsWith (scriptSuffix ));
98108 server.log (
99109 "Found ${scriptFiles .length } script file(s)" ,
100110 level: LogLevel .info,
101111 );
102112 for (final file in scriptFiles) {
103113 try {
104114 final code = await file.readAsString ();
105- final relativePath = file.path.substring (scriptsFolder.path.length + 1 );
106- pluginSystem.registerLuauPlugin (relativePath, code);
115+ final relativePath = file.path.substring (
116+ scriptsFolder.path.length + 1 ,
117+ file.path.length - scriptSuffix.length,
118+ );
119+ await pluginSystem.registerLuauPlugin (relativePath, code);
107120 } catch (e) {
108121 server.log (
109122 'Error loading script from ${file .path }: $e ' ,
@@ -114,7 +127,7 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
114127 }
115128
116129 Future <void > init () async {
117- await _loadScripts (state.info.script );
130+ await _loadScripts (state.info.gameMode );
118131 }
119132
120133 Future <void > resetWorld ([ItemLocation ? mode]) async {
@@ -124,7 +137,7 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
124137
125138 Future <void > save ({bool force = false }) async {
126139 var file = File (
127- worldName == defaultWorldName ? 'world.stnx' : 'worlds /$worldName .stnx ' ,
140+ '${ worldName == defaultWorldName ? SetonixServer . defaultWorldName : '${ SetonixServer . worldDirectory } /$worldName ' }${ SetonixServer . worldSuffix } ' ,
128141 );
129142 if (! await file.exists ()) {
130143 await file.create (recursive: true );
0 commit comments