Skip to content

Commit 588de59

Browse files
committed
Remove state setter, add plugin system to app
1 parent 9ecaeb8 commit 588de59

File tree

9 files changed

+30
-243
lines changed

9 files changed

+30
-243
lines changed

app/android/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pluginManagement {
1818

1919
plugins {
2020
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21-
id "com.android.application" version '8.8.1' apply false
21+
id "com.android.application" version '8.8.2' apply false
2222
id "org.jetbrains.kotlin.android" version "2.1.0" apply false
2323
}
2424

app/lib/bloc/world/bloc.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:setonix/helpers/asset.dart';
99
import 'package:setonix/services/file_system.dart';
1010
import 'package:setonix/bloc/multiplayer.dart';
1111
import 'package:setonix_api/setonix_api.dart';
12+
import 'package:setonix_plugin/setonix_plugin.dart';
1213

1314
ServerProcessed _compute(
1415
(ServerWorldEvent, WorldState, List<SignatureMetadata>) m) =>
@@ -17,6 +18,7 @@ ServerProcessed _compute(
1718
SetonixData _saveState(WorldState state) => state.save();
1819

1920
class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
21+
late final PluginSystem pluginSystem;
2022
bool _remoteEvent = false;
2123
WorldBloc({
2224
required MultiplayerCubit multiplayer,
@@ -39,6 +41,16 @@ class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
3941
info: data?.getInfo() ?? const GameInfo(),
4042
),
4143
)) {
44+
pluginSystem = PluginSystem(
45+
onProcess: (p0, p1, [force = false]) {
46+
process(p1);
47+
},
48+
onSendEvent: (p0, p1) {
49+
_processEvent(p1);
50+
},
51+
playersGetter: () => state.multiplayer.clients.toList(),
52+
stateGetter: () => state.world,
53+
);
4254
state.multiplayer
4355
..events.listen((event) {
4456
_remoteEvent = true;
@@ -113,6 +125,9 @@ class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
113125
emit(
114126
state.copyWith(showDuplicates: event.value ?? !state.showDuplicates));
115127
});
128+
if (!state.multiplayer.isClient) {
129+
_loadScript(state.world.info.script);
130+
}
116131
}
117132

118133
Future<void> save() async {
@@ -162,4 +177,12 @@ class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
162177
add(e);
163178
}
164179
}
180+
181+
Future<void> _loadScript(String? script) async {
182+
try {
183+
if (script == null) return;
184+
pluginSystem.loadLuaPlugin(state.assetManager, script);
185+
// ignore: empty_catches
186+
} catch (e) {}
187+
}
165188
}

plugin/lib/src/rust/api/plugin.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ abstract class PluginCallback implements RustOpaqueInterface {
2424
void changeStateFieldAccess(
2525
{required FutureOr<String> Function(StateFieldAccess) stateFieldAccess});
2626

27-
void changeStateFieldChange(
28-
{required FutureOr<void> Function(StateFieldAccess, String)
29-
stateFieldChange});
30-
3127
static PluginCallback default_() =>
3228
RustLib.instance.api.crateApiPluginPluginCallbackDefault();
3329
}

plugin/lib/src/rust/frb_generated.dart

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
6868
String get codegenVersion => '2.7.1';
6969

7070
@override
71-
int get rustContentHash => 1166095122;
71+
int get rustContentHash => 2139481266;
7272

7373
static const kDefaultExternalLibraryLoaderConfig =
7474
ExternalLibraryLoaderConfig(
@@ -108,11 +108,6 @@ abstract class RustLibApi extends BaseApi {
108108
{required PluginCallback that,
109109
required FutureOr<String> Function(StateFieldAccess) stateFieldAccess});
110110

111-
void crateApiPluginPluginCallbackChangeStateFieldChange(
112-
{required PluginCallback that,
113-
required FutureOr<void> Function(StateFieldAccess, String)
114-
stateFieldChange});
115-
116111
PluginCallback crateApiPluginPluginCallbackDefault();
117112

118113
Future<int> crateApiSimpleSimpleAdderTwinNormal(
@@ -357,43 +352,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
357352
argNames: ["that", "stateFieldAccess"],
358353
);
359354

360-
@override
361-
void crateApiPluginPluginCallbackChangeStateFieldChange(
362-
{required PluginCallback that,
363-
required FutureOr<void> Function(StateFieldAccess, String)
364-
stateFieldChange}) {
365-
return handler.executeSync(SyncTask(
366-
callFfi: () {
367-
final serializer = SseSerializer(generalizedFrbRustBinding);
368-
sse_encode_Auto_RefMut_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerPluginCallback(
369-
that, serializer);
370-
sse_encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
371-
stateFieldChange, serializer);
372-
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 8)!;
373-
},
374-
codec: SseCodec(
375-
decodeSuccessData: sse_decode_unit,
376-
decodeErrorData: null,
377-
),
378-
constMeta: kCrateApiPluginPluginCallbackChangeStateFieldChangeConstMeta,
379-
argValues: [that, stateFieldChange],
380-
apiImpl: this,
381-
));
382-
}
383-
384-
TaskConstMeta
385-
get kCrateApiPluginPluginCallbackChangeStateFieldChangeConstMeta =>
386-
const TaskConstMeta(
387-
debugName: "PluginCallback_change_state_field_change",
388-
argNames: ["that", "stateFieldChange"],
389-
);
390-
391355
@override
392356
PluginCallback crateApiPluginPluginCallbackDefault() {
393357
return handler.executeSync(SyncTask(
394358
callFfi: () {
395359
final serializer = SseSerializer(generalizedFrbRustBinding);
396-
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 9)!;
360+
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 8)!;
397361
},
398362
codec: SseCodec(
399363
decodeSuccessData:
@@ -421,7 +385,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
421385
sse_encode_i_32(a, serializer);
422386
sse_encode_i_32(b, serializer);
423387
pdeCallFfi(generalizedFrbRustBinding, serializer,
424-
funcId: 12, port: port_);
388+
funcId: 11, port: port_);
425389
},
426390
codec: SseCodec(
427391
decodeSuccessData: sse_decode_i_32,
@@ -573,40 +537,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
573537
};
574538
}
575539

576-
Future<void> Function(int, dynamic, dynamic)
577-
encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
578-
FutureOr<void> Function(StateFieldAccess, String) raw) {
579-
return (callId, rawArg0, rawArg1) async {
580-
final arg0 = dco_decode_state_field_access(rawArg0);
581-
final arg1 = dco_decode_String(rawArg1);
582-
583-
Box<void>? rawOutput;
584-
Box<AnyhowException>? rawError;
585-
try {
586-
rawOutput = Box(await raw(arg0, arg1));
587-
} catch (e, s) {
588-
rawError = Box(AnyhowException("$e\n\n$s"));
589-
}
590-
591-
final serializer = SseSerializer(generalizedFrbRustBinding);
592-
assert((rawOutput != null) ^ (rawError != null));
593-
if (rawOutput != null) {
594-
serializer.buffer.putUint8(0);
595-
sse_encode_unit(rawOutput.value, serializer);
596-
} else {
597-
serializer.buffer.putUint8(1);
598-
sse_encode_AnyhowException(rawError!.value, serializer);
599-
}
600-
final output = serializer.intoRaw();
601-
602-
generalizedFrbRustBinding.dartFnDeliverOutput(
603-
callId: callId,
604-
ptr: output.ptr,
605-
rustVecLen: output.rustVecLen,
606-
dataLen: output.dataLen);
607-
};
608-
}
609-
610540
RustArcIncrementStrongCountFnType
611541
get rust_arc_increment_strong_count_LuauPlugin => wire
612542
.rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerLuauPlugin;
@@ -692,14 +622,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
692622
throw UnimplementedError('');
693623
}
694624

695-
@protected
696-
FutureOr<void> Function(StateFieldAccess, String)
697-
dco_decode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
698-
dynamic raw) {
699-
// Codec=Dco (DartCObject based), see doc to use other codecs
700-
throw UnimplementedError('');
701-
}
702-
703625
@protected
704626
Object dco_decode_DartOpaque(dynamic raw) {
705627
// Codec=Dco (DartCObject based), see doc to use other codecs
@@ -1151,18 +1073,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
11511073
serializer);
11521074
}
11531075

1154-
@protected
1155-
void
1156-
sse_encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
1157-
FutureOr<void> Function(StateFieldAccess, String) self,
1158-
SseSerializer serializer) {
1159-
// Codec=Sse (Serialization based), see doc to use other codecs
1160-
sse_encode_DartOpaque(
1161-
encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
1162-
self),
1163-
serializer);
1164-
}
1165-
11661076
@protected
11671077
void sse_encode_DartOpaque(Object self, SseSerializer serializer) {
11681078
// Codec=Sse (Serialization based), see doc to use other codecs
@@ -1405,10 +1315,4 @@ class PluginCallbackImpl extends RustOpaque implements PluginCallback {
14051315
stateFieldAccess}) =>
14061316
RustLib.instance.api.crateApiPluginPluginCallbackChangeStateFieldAccess(
14071317
that: this, stateFieldAccess: stateFieldAccess);
1408-
1409-
void changeStateFieldChange(
1410-
{required FutureOr<void> Function(StateFieldAccess, String)
1411-
stateFieldChange}) =>
1412-
RustLib.instance.api.crateApiPluginPluginCallbackChangeStateFieldChange(
1413-
that: this, stateFieldChange: stateFieldChange);
14141318
}

plugin/lib/src/rust/frb_generated.io.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
6969
dco_decode_DartFn_Inputs_state_field_access_Output_String_AnyhowException(
7070
dynamic raw);
7171

72-
@protected
73-
FutureOr<void> Function(StateFieldAccess, String)
74-
dco_decode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
75-
dynamic raw);
76-
7772
@protected
7873
Object dco_decode_DartOpaque(dynamic raw);
7974

@@ -285,12 +280,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
285280
FutureOr<String> Function(StateFieldAccess) self,
286281
SseSerializer serializer);
287282

288-
@protected
289-
void
290-
sse_encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
291-
FutureOr<void> Function(StateFieldAccess, String) self,
292-
SseSerializer serializer);
293-
294283
@protected
295284
void sse_encode_DartOpaque(Object self, SseSerializer serializer);
296285

plugin/lib/src/rust/frb_generated.web.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
7171
dco_decode_DartFn_Inputs_state_field_access_Output_String_AnyhowException(
7272
dynamic raw);
7373

74-
@protected
75-
FutureOr<void> Function(StateFieldAccess, String)
76-
dco_decode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
77-
dynamic raw);
78-
7974
@protected
8075
Object dco_decode_DartOpaque(dynamic raw);
8176

@@ -287,12 +282,6 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
287282
FutureOr<String> Function(StateFieldAccess) self,
288283
SseSerializer serializer);
289284

290-
@protected
291-
void
292-
sse_encode_DartFn_Inputs_state_field_access_String_Output_unit_AnyhowException(
293-
FutureOr<void> Function(StateFieldAccess, String) self,
294-
SseSerializer serializer);
295-
296285
@protected
297286
void sse_encode_DartOpaque(Object self, SseSerializer serializer);
298287

plugin/rust/src/api/luau/state.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ impl LuaUserData for LuauStateUserData {
1818
let serialized = lua.to_value(&result).unwrap();
1919
Ok(serialized)
2020
});
21-
fields.add_field_method_set(name, move |_, this: &mut LuauStateUserData, value: LuaValue| {
22-
let callback = this.0.state_field_change.clone();
23-
let value = serde_json::to_string(&value).unwrap();
24-
block_on(callback(field.clone(), value));
25-
Ok(())
26-
});
2721
}
2822
}
2923
}

plugin/rust/src/api/plugin.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub struct PluginCallback {
2626
pub(crate) process_event: DartCallback2<String, Option<bool>>,
2727
pub(crate) send_event: DartCallback2<String, Option<Channel>>,
2828
pub(crate) state_field_access: Arc<dyn Fn(StateFieldAccess) -> DartFnFuture<String> + Send + Sync>,
29-
pub(crate) state_field_change: Arc<dyn Fn(StateFieldAccess, String) -> DartFnFuture<()> + Send + Sync>,
3029
}
3130

3231
impl Default for PluginCallback {
@@ -41,7 +40,6 @@ impl Default for PluginCallback {
4140
process_event: Arc::new(|_, _| Box::pin(async {})),
4241
send_event: Arc::new(|_, _| Box::pin(async {})),
4342
state_field_access: Arc::new(|_| Box::pin(async { "".to_string() })),
44-
state_field_change: Arc::new(|_, _| Box::pin(async {})),
4543
}
4644
}
4745
}
@@ -65,11 +63,6 @@ impl PluginCallback {
6563
pub fn change_state_field_access(&mut self, state_field_access: impl Fn(StateFieldAccess) -> DartFnFuture<String> + 'static + Send + Sync) {
6664
self.state_field_access = Arc::new(Box::new(state_field_access)); // or sth like that
6765
}
68-
69-
#[frb(sync)]
70-
pub fn change_state_field_change(&mut self, state_field_change: impl Fn(StateFieldAccess, String) -> DartFnFuture<()> + 'static + Send + Sync) {
71-
self.state_field_change = Arc::new(Box::new(state_field_change)); // or sth like that
72-
}
7366
}
7467

7568
pub type Channel = i16;

0 commit comments

Comments
 (0)