Skip to content

Commit

Permalink
Added toggleable compression (#59)
Browse files Browse the repository at this point in the history
* Added toggleable compression

* Set default compression to false
  • Loading branch information
KiritoDv authored Jun 1, 2024
1 parent 5f296ac commit c1be413
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 17 deletions.
20 changes: 12 additions & 8 deletions lib/arc/arc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,21 @@ class Arc {
return files;
}

void _addMPQFile(String path, Uint8List data) {
void _addMPQFile(String path, Uint8List data, bool compress) {
final mpqArchive = handle as MPQArchive;
final timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
mpqArchive.createFile(path, timestamp, data.length, 0, 0)
..write(data, data.length, MPQ_COMPRESSION_ZLIB)
mpqArchive.createFile(path, timestamp, data.length, 0, compress ? MPQ_FILE_COMPRESS : 0)
..write(data, data.length, compress ? MPQ_COMPRESSION_ZLIB : 0)
..finish();
}

void _addZipFile(String path, Uint8List data) {
void _addZipFile(String path, Uint8List data, bool compress) {
final archive = handle as Archive;
archive.addFile(ArchiveFile(path, data.length, data));
if(compress) {
archive.addFile(ArchiveFile(path, data.length, data));
} else {
archive.addFile(ArchiveFile.noCompress(path, data.length, data));
}
}

void _closeMPQ() {
Expand All @@ -147,11 +151,11 @@ class Arc {
}
}

void addFile(String path, Uint8List data) {
void addFile(String path, Uint8List data, { bool compress = false }) {
if(handle is MPQArchive) {
return _addMPQFile(path, data);
return _addMPQFile(path, data, compress);
} else {
return _addZipFile(path, data);
return _addZipFile(path, data, compress);
}
}

Expand Down
21 changes: 14 additions & 7 deletions lib/features/create/create_finish/create_finish_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CreateFinishViewModel with ChangeNotifier {
bool isEphemeralBarExpanded = false;
bool isGenerating = false;
bool prependAlt = false;
bool compressFiles = false;
int totalFiles = 0;
int filesProcessed = 0;

Expand All @@ -46,6 +47,11 @@ class CreateFinishViewModel with ChangeNotifier {
notifyListeners();
}

Future<void> onToggleCompressFiles(bool newCompressFilesValue) async {
compressFiles = newCompressFilesValue;
notifyListeners();
}

void reset() {
currentState = AppState.none;
totalFiles = 0;
Expand Down Expand Up @@ -166,7 +172,7 @@ class CreateFinishViewModel with ChangeNotifier {

isGenerating = true;
notifyListeners();
await createGenerationIsolate(entries, outputFile, prependAlt);
await createGenerationIsolate(entries, outputFile, prependAlt, compressFiles);
// await compute(generateOTR, Tuple2(entries, outputFile));
isGenerating = false;
notifyListeners();
Expand All @@ -176,11 +182,11 @@ class CreateFinishViewModel with ChangeNotifier {
}

Future createGenerationIsolate(HashMap<String, StageEntry> entries,
String outputFile, bool shouldPrependAlt) async {
String outputFile, bool shouldPrependAlt, bool shouldCompress) async {
final receivePort = ReceivePort();
await Isolate.spawn(
generateOTR,
Tuple4(entries, outputFile, receivePort.sendPort, shouldPrependAlt),
Tuple5(entries, outputFile, receivePort.sendPort, shouldPrependAlt, shouldCompress),
onExit: receivePort.sendPort,
onError: receivePort.sendPort,
);
Expand Down Expand Up @@ -212,8 +218,9 @@ class CreateFinishViewModel with ChangeNotifier {
}
}

Future<void> generateOTR(Tuple4<HashMap<String, StageEntry>, String, SendPort, bool> params) async {
Future<void> generateOTR(Tuple5<HashMap<String, StageEntry>, String, SendPort, bool, bool> params) async {
try {
final compress = params.item5;
final arcFile = Arc(params.item2);

for (final entry in params.item1.entries) {
Expand All @@ -222,15 +229,15 @@ Future<void> generateOTR(Tuple4<HashMap<String, StageEntry>, String, SendPort, b
final fileData = await file.readAsBytes();
final fileName =
"${entry.key}/${p.normalize(file.path).split("/").last}";
arcFile.addFile(fileName, fileData);
arcFile.addFile(fileName, fileData, compress: compress);
params.item3.send(1);
}
} else if (entry.value is CustomSequencesEntry) {
for (final pair in (entry.value as CustomSequencesEntry).pairs) {
final sequence = await compute(Sequence.fromSeqFile, pair);
final fileName = '${entry.key}/${sequence.path}';
final data = sequence.build();
arcFile.addFile(fileName, data);
arcFile.addFile(fileName, data, compress: compress);
params.item3.send(1);
}
} else if (entry.value is CustomTexturesEntry) {
Expand All @@ -248,7 +255,7 @@ Future<void> generateOTR(Tuple4<HashMap<String, StageEntry>, String, SendPort, b
continue;
}

arcFile.addFile(texture.item1, texture.item2!);
arcFile.addFile(texture.item1, texture.item2!, compress: compress);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Widget FolderContent(
style: ElevatedButton.styleFrom(minimumSize: const Size(100, 50)),
child: Text(i18n.folderContentView_selectButton),)
],),
const SizedBox(height: 10),
Row(children: [
Switch(
activeColor: Colors.blue,
Expand All @@ -43,6 +44,16 @@ Widget FolderContent(
),
Text(i18n.folderContentView_prependAltToggle),
],),
Row(children: [
Switch(
activeColor: Colors.blue,
value: finishViewModel.compressFiles,
onChanged: (value) {
finishViewModel.onToggleCompressFiles(value);
},
),
Text(i18n.folderContentView_compressToggle),
],),
if (viewModel.processedFiles.isEmpty && viewModel.isProcessing == false)
const Spacer(),
if (viewModel.processedFiles.isNotEmpty || viewModel.isProcessing)
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"otrContentView_processing": "Verarbeite...",
"folderContentView_customTexturePath": "Benutzeridentifizierter Austausch-Texturen Ordner",
"folderContentView_prependAltToggle": "Füge einen `alt/` Dies ermöglicht es den Spielern, Assets im Spiel zu aktivieren oder zu deaktivieren.",
"folderContentView_compressToggle": "Dateien komprimieren. Dadurch wird die Größe der OTR / O2R reduziert.",
"folderContentView_selectButton": "Wähle",
"folderContentView_stageTextures": "Texturen indexieren",
"createCustomSequences_addCustomSequences": "Füge benutzeridentifizierte Sequenzen hinzu",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"otrContentView_processing": "Processing...",
"folderContentView_customTexturePath": "Custom Texture Replacements Folder",
"folderContentView_prependAltToggle": "Prepend `alt/`. This will allow players to toggle these assets on-the-fly in game.",
"folderContentView_compressToggle": "Compress Files. This will reduce the size of the OTR / O2R.",
"folderContentView_selectButton": "Select",
"folderContentView_stageTextures": "Stage Textures",
"createCustomSequences_addCustomSequences": "Add Custom Sequences",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"otrContentView_processing": "Procesando...",
"folderContentView_customTexturePath": "Carpeta de reemplazos de texturas personalizadas",
"folderContentView_prependAltToggle": "Agregar en `alt/`. Esto permitirá a los jugadores cambiar los recursos durante el juego.",
"folderContentView_compressToggle": "Comprimir archivos, esto puede reducir el tamaño del OTR / O2R",
"folderContentView_selectButton": "Seleccionar",
"folderContentView_stageTextures": "Agregar texturas",
"createCustomSequences_addCustomSequences": "Agregar secuencias personalizadas",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"otrContentView_processing": "Traitement en cours...",
"folderContentView_customTexturePath": "Dossier de remplacements de textures personnalisées",
"folderContentView_prependAltToggle": "Ajouter un dossier `alt/` à votre OTR / O2R. Permet aux joueurs d'activer ou désactiver les assets en jeu.",
"folderContentView_compressToggle": "Compresser les fichiers. Cela réduira la taille de l'OTR / O2R.",
"folderContentView_selectButton": "Sélectionner",
"folderContentView_stageTextures": "Indexer les textures",
"createCustomSequences_addCustomSequences": "Ajouter des séquences personnalisées",
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_nl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"otrContentView_step5": "5. We genereren een OTR / O2R met de gewijzigde textures! 🚀",
"otrContentView_processing": "Verwerken...",
"folderContentView_customTexturePath": "Vervangende Texture Map",
"folderContentView_prependAltToggle": "Prepend `alt/`. This will allow players to toggle these assets on-the-fly in game.",
"folderContentView_prependAltToggle": "Voeg 'alt/' toe aan het begin. Dit stelt spelers in staat om deze assets tijdens het spel direct aan en uit te zetten.",
"folderContentView_compressToggle": "Bestanden comprimeren. Dit zal de grootte van de OTR / O2R verkleinen.",
"folderContentView_selectButton": "Selecteer",
"folderContentView_stageTextures": "Stage-Textures",
"createCustomSequences_addCustomSequences": "Voeg aangepaste composities toe",
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/components/ephemeral_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class _EphemeralBarState extends State<EphemeralBar>
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text('Retro: 0.1.0', style: textTheme.bodyMedium!.copyWith(
Text('Retro: 0.1.1', style: textTheme.bodyMedium!.copyWith(
color: colorScheme.onSurface,),
),
if (!isExpanded)
Expand Down

0 comments on commit c1be413

Please sign in to comment.