Initial flame_lua_runtime package
This commit is contained in:
43
lib/runtime/models/runtime_command.dart
Normal file
43
lib/runtime/models/runtime_command.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import '../protocol/runtime_protocol.dart';
|
||||
|
||||
class RuntimeCommand {
|
||||
const RuntimeCommand({
|
||||
required this.type,
|
||||
this.target,
|
||||
this.payload = const {},
|
||||
});
|
||||
|
||||
final String type;
|
||||
final String? target;
|
||||
final Map<String, Object?> payload;
|
||||
|
||||
static RuntimeCommand fromMap(Map<String, Object?> map) {
|
||||
final type = map[RuntimeProtocolField.type];
|
||||
if (type is! String || type.isEmpty) {
|
||||
throw const FormatException('RuntimeCommand.type must be a string');
|
||||
}
|
||||
|
||||
if (!RuntimeCommandType.isSupported(type)) {
|
||||
throw FormatException('RuntimeCommand.type is unsupported: $type');
|
||||
}
|
||||
RuntimeProtocolSchema.ensureKnownKeys(
|
||||
map,
|
||||
allowed: RuntimeProtocolSchema.allowedCommandFields(type),
|
||||
context: 'RuntimeCommand.$type',
|
||||
);
|
||||
|
||||
final targetValue = map[RuntimeProtocolField.target];
|
||||
if (targetValue != null && targetValue is! String) {
|
||||
throw const FormatException('RuntimeCommand.target must be a string');
|
||||
}
|
||||
|
||||
final payload = Map<String, Object?>.from(map)
|
||||
..remove(RuntimeProtocolField.type)
|
||||
..remove(RuntimeProtocolField.target);
|
||||
return RuntimeCommand(
|
||||
type: type,
|
||||
target: targetValue as String?,
|
||||
payload: payload,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user