feat: imported input-box pets, with dsh-Go's sprite packs
Nothing is built in: Settings → 宠物 shows "尚未导入宠物" until a zip is picked. The zip holds a dsh-Go pack.json and PNG sheets; it is validated (pack.json, every named sheet) and unpacked under the app's private files directory, with enable, size (40/52/64) and remove kept on the device. A pack's canvas, anchor, per-animation box and frame list drive rendering: PetSlot reserves the area above the baseline and stands the sprite on the composer's top edge (or on the activity cap while a turn runs). Idle, thinking, working and error follow the chat's state, and a tap plays the pack's react-left/react-right. Only one sheet is decoded at a time. path_provider's JNI binding does not load on this engine, so MainActivity exposes the files directory over a tiny method channel instead. dist/deepy-whale-pet.zip is the Deepy whale for testing. Tests cover the real pack manifest, frame/state mapping and zip import. 0.4.0 (400).
This commit is contained in:
1 parent
ff0dbf1b37
commit
d72af036a1
16 files changed
+1410
-12
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:archive/archive.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:openchamber/pets/pet_library.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
test('a zip with a nested pack is unpacked and activated', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final root = await Directory.systemTemp.createTemp('pet-import-');
|
||||
addTearDown(() async {
|
||||
await PetLibrary.instance.remove();
|
||||
if (await root.exists()) await root.delete(recursive: true);
|
||||
});
|
||||
|
||||
final manifest = jsonEncode({
|
||||
'id': 'test-pet',
|
||||
'name': '测试宠物',
|
||||
'canvas': {'w': 8, 'h': 8},
|
||||
'anchor': {'x': 4, 'baseline': 7},
|
||||
'animations': {
|
||||
'idle': {
|
||||
'sheet': 'idle.png',
|
||||
'frameW': 40,
|
||||
'frameH': 40,
|
||||
'cols': 1,
|
||||
'loop': true,
|
||||
'ms': 100,
|
||||
'box': {'x': 0, 'y': 0, 'w': 8, 'h': 8},
|
||||
'frames': [
|
||||
{'i': 0, 'ms': 100},
|
||||
],
|
||||
},
|
||||
},
|
||||
'states': {
|
||||
'idle': ['idle'],
|
||||
},
|
||||
});
|
||||
|
||||
final archive = Archive();
|
||||
void add(String name, List<int> bytes) =>
|
||||
archive.addFile(ArchiveFile(name, bytes.length, bytes));
|
||||
add('pet/pack.json', utf8.encode(manifest));
|
||||
add('pet/idle.png', [1, 2, 3, 4]);
|
||||
add('pet/README.md', utf8.encode('ignored? no, kept'));
|
||||
|
||||
final bytes = ZipEncoder().encode(archive);
|
||||
expect(bytes, isNotNull);
|
||||
|
||||
final pack = await PetLibrary.instance.importZip(bytes!, root: root);
|
||||
expect(pack.id, 'test-pet');
|
||||
expect(pack.name, '测试宠物');
|
||||
expect(PetLibrary.instance.hasPet, isTrue);
|
||||
expect(PetLibrary.instance.enabled, isTrue);
|
||||
|
||||
final unpacked = Directory('${root.path}/test-pet');
|
||||
expect(await File('${unpacked.path}/pack.json').exists(), isTrue);
|
||||
expect(await File('${unpacked.path}/idle.png').exists(), isTrue);
|
||||
// The nested folder is stripped, not kept as an extra level.
|
||||
expect(await Directory('${unpacked.path}/pet').exists(), isFalse);
|
||||
|
||||
await PetLibrary.instance.remove();
|
||||
expect(PetLibrary.instance.pack, isNull);
|
||||
expect(PetLibrary.instance.enabled, isFalse);
|
||||
expect(await unpacked.exists(), isFalse);
|
||||
});
|
||||
|
||||
test('a zip without pack.json is refused', () async {
|
||||
final root = await Directory.systemTemp.createTemp('pet-import-bad-');
|
||||
addTearDown(() async {
|
||||
if (await root.exists()) await root.delete(recursive: true);
|
||||
});
|
||||
final archive = Archive();
|
||||
final bytes = ZipEncoder().encode(archive)!;
|
||||
expect(
|
||||
() => PetLibrary.instance.importZip(bytes, root: root),
|
||||
throwsA(isA<PetImportException>()),
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user