@@ -16,6 +16,8 @@ Future<ServerProcessed> _computeEvent(
16
16
);
17
17
}
18
18
19
+ const scriptSuffix = '.lua' ;
20
+
19
21
class WorldBloc extends Bloc <PlayableWorldEvent , WorldState >
20
22
with ServerInterface {
21
23
final SetonixServer server;
@@ -52,14 +54,14 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
52
54
processed.responses.forEach (process);
53
55
if (event is WorldInitialized ) {
54
56
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 }" : "" }" ,
56
58
level: LogLevel .info,
57
59
);
58
60
_serverPlugin = await _pluginSystem.registerPlugin (
59
61
'' ,
60
62
SetonixPlugin .new ,
61
63
);
62
- await _loadScripts ((newState ?? state).info.script );
64
+ await _loadScripts ((newState ?? state).info.gameMode );
63
65
}
64
66
if (newState == null ) return ;
65
67
emit (newState);
@@ -79,11 +81,19 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
79
81
}
80
82
}
81
83
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 ();
83
95
try {
84
- if (location != null ) {
85
- pluginSystem.loadLuaPluginFromLocation (assetManager, location);
86
- }
96
+ if (mode != null ) await _loadGameMode (mode);
87
97
} catch (e) {
88
98
server.log ('Error loading script: $e ' , level: LogLevel .error);
89
99
}
@@ -94,16 +104,19 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
94
104
}
95
105
final scriptFiles = (await scriptsFolder.list ().toList ())
96
106
.whereType <File >()
97
- .where ((file) => file.path.endsWith ('.lua' ));
107
+ .where ((file) => file.path.endsWith (scriptSuffix ));
98
108
server.log (
99
109
"Found ${scriptFiles .length } script file(s)" ,
100
110
level: LogLevel .info,
101
111
);
102
112
for (final file in scriptFiles) {
103
113
try {
104
114
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);
107
120
} catch (e) {
108
121
server.log (
109
122
'Error loading script from ${file .path }: $e ' ,
@@ -114,7 +127,7 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
114
127
}
115
128
116
129
Future <void > init () async {
117
- await _loadScripts (state.info.script );
130
+ await _loadScripts (state.info.gameMode );
118
131
}
119
132
120
133
Future <void > resetWorld ([ItemLocation ? mode]) async {
@@ -124,7 +137,7 @@ class WorldBloc extends Bloc<PlayableWorldEvent, WorldState>
124
137
125
138
Future <void > save ({bool force = false }) async {
126
139
var file = File (
127
- worldName == defaultWorldName ? 'world.stnx' : 'worlds /$worldName .stnx ' ,
140
+ '${ worldName == defaultWorldName ? SetonixServer . defaultWorldName : '${ SetonixServer . worldDirectory } /$worldName ' }${ SetonixServer . worldSuffix } ' ,
128
141
);
129
142
if (! await file.exists ()) {
130
143
await file.create (recursive: true );
0 commit comments