From c1be413bc7f57fa472c797caaff90ee11122b0c2 Mon Sep 17 00:00:00 2001 From: Lywx Date: Fri, 31 May 2024 19:00:00 -0600 Subject: [PATCH] Added toggleable compression (#59) * Added toggleable compression * Set default compression to false --- lib/arc/arc.dart | 20 +++++++++++------- .../create_finish_viewmodel.dart | 21 ++++++++++++------- .../components/folder_content_view.dart | 11 ++++++++++ lib/l10n/app_de.arb | 1 + lib/l10n/app_en.arb | 1 + lib/l10n/app_es.arb | 1 + lib/l10n/app_fr.arb | 1 + lib/l10n/app_nl.arb | 3 ++- lib/ui/components/ephemeral_bar.dart | 2 +- 9 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/arc/arc.dart b/lib/arc/arc.dart index e31e8b6..72dab16 100644 --- a/lib/arc/arc.dart +++ b/lib/arc/arc.dart @@ -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() { @@ -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); } } diff --git a/lib/features/create/create_finish/create_finish_viewmodel.dart b/lib/features/create/create_finish/create_finish_viewmodel.dart index 50075d9..7d7ac5c 100644 --- a/lib/features/create/create_finish/create_finish_viewmodel.dart +++ b/lib/features/create/create_finish/create_finish_viewmodel.dart @@ -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; @@ -46,6 +47,11 @@ class CreateFinishViewModel with ChangeNotifier { notifyListeners(); } + Future onToggleCompressFiles(bool newCompressFilesValue) async { + compressFiles = newCompressFilesValue; + notifyListeners(); + } + void reset() { currentState = AppState.none; totalFiles = 0; @@ -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(); @@ -176,11 +182,11 @@ class CreateFinishViewModel with ChangeNotifier { } Future createGenerationIsolate(HashMap 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, ); @@ -212,8 +218,9 @@ class CreateFinishViewModel with ChangeNotifier { } } -Future generateOTR(Tuple4, String, SendPort, bool> params) async { +Future generateOTR(Tuple5, String, SendPort, bool, bool> params) async { try { + final compress = params.item5; final arcFile = Arc(params.item2); for (final entry in params.item1.entries) { @@ -222,7 +229,7 @@ Future generateOTR(Tuple4, 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) { @@ -230,7 +237,7 @@ Future generateOTR(Tuple4, String, SendPort, b 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) { @@ -248,7 +255,7 @@ Future generateOTR(Tuple4, String, SendPort, b continue; } - arcFile.addFile(texture.item1, texture.item2!); + arcFile.addFile(texture.item1, texture.item2!, compress: compress); } } } diff --git a/lib/features/create/create_replace_textures/components/folder_content_view.dart b/lib/features/create/create_replace_textures/components/folder_content_view.dart index 8bb91c9..95387d8 100644 --- a/lib/features/create/create_replace_textures/components/folder_content_view.dart +++ b/lib/features/create/create_replace_textures/components/folder_content_view.dart @@ -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, @@ -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) diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index a559875..cb42d88 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -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", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 04141c7..64ebd5b 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -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", diff --git a/lib/l10n/app_es.arb b/lib/l10n/app_es.arb index cc66f5a..dba29a1 100644 --- a/lib/l10n/app_es.arb +++ b/lib/l10n/app_es.arb @@ -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", diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index 54d52e1..bb02277 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -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", diff --git a/lib/l10n/app_nl.arb b/lib/l10n/app_nl.arb index e404ba8..709d5f5 100644 --- a/lib/l10n/app_nl.arb +++ b/lib/l10n/app_nl.arb @@ -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", diff --git a/lib/ui/components/ephemeral_bar.dart b/lib/ui/components/ephemeral_bar.dart index ddd677b..134c42f 100644 --- a/lib/ui/components/ephemeral_bar.dart +++ b/lib/ui/components/ephemeral_bar.dart @@ -95,7 +95,7 @@ class _EphemeralBarState extends State 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)