From c9bde9737aecd6e8c65c6014569305996d96b698 Mon Sep 17 00:00:00 2001 From: ArthurHeitmann <37270165+ArthurHeitmann@users.noreply.github.com> Date: Fri, 22 Dec 2023 15:45:16 +0100 Subject: [PATCH] fixed xml whitespace replacement --- lib/fileTypeUtils/audio/wavToWemConverter.dart | 4 +++- lib/fileTypeUtils/bxm/bxmReader.dart | 3 ++- lib/fileTypeUtils/xml/xmlExtension.dart | 13 +++++++++++++ lib/fileTypeUtils/yax/yaxToXml.dart | 3 ++- lib/fileTypeUtils/z/zlibDecompress.dart | 4 +++- 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 lib/fileTypeUtils/xml/xmlExtension.dart diff --git a/lib/fileTypeUtils/audio/wavToWemConverter.dart b/lib/fileTypeUtils/audio/wavToWemConverter.dart index 7de4c96..6b27c1d 100644 --- a/lib/fileTypeUtils/audio/wavToWemConverter.dart +++ b/lib/fileTypeUtils/audio/wavToWemConverter.dart @@ -5,6 +5,8 @@ import 'package:archive/archive_io.dart'; import 'package:path/path.dart'; import 'package:xml/xml.dart'; +import '../xml/xmlExtension.dart'; + const _defaultTemplateFile = "wavToWemTemplate_default.zip"; const _bgmTemplateFile = "wavToWemTemplate_BGM.zip"; @@ -50,7 +52,7 @@ Future wavToWem(String wavPath, String wemSavePath, bool isBgm, bool enabl String wavSrcDir = join(projectPath, "wavSrc"); XmlDocument wSourcesXml = _getWwiseSourcesXml(wavPath, enableVolumeNormalization); String wSourcesXmlPath = join(wavSrcDir, "ExtSourceList.wsources"); - var xmlStr = wSourcesXml.toXmlString(pretty: true, indent: "\t"); + var xmlStr = wSourcesXml.toPrettyString(); await File(wSourcesXmlPath).writeAsString(xmlStr); print("Converting WAV to WEM"); diff --git a/lib/fileTypeUtils/bxm/bxmReader.dart b/lib/fileTypeUtils/bxm/bxmReader.dart index fe3da88..7cac0b6 100644 --- a/lib/fileTypeUtils/bxm/bxmReader.dart +++ b/lib/fileTypeUtils/bxm/bxmReader.dart @@ -5,6 +5,7 @@ import 'dart:typed_data'; import 'package:xml/xml.dart'; import '../utils/ByteDataWrapper.dart'; +import '../xml/xmlExtension.dart'; import 'bxmIO.dart'; XmlElement parseBxm(ByteDataWrapper bytes) { @@ -95,6 +96,6 @@ Future convertBxmFileToXml(String bxmPath, String xmlPath) async { var doc = XmlDocument(); doc.children.add(XmlDeclaration([XmlAttribute(XmlName("version"), "1.0"), XmlAttribute(XmlName("encoding"), "utf-8")])); doc.children.add(root); - var xmlStr = "${doc.toXmlString(pretty: true, indent: "\t")}\n"; + var xmlStr = doc.toPrettyString(); await File(xmlPath).writeAsString(xmlStr); } diff --git a/lib/fileTypeUtils/xml/xmlExtension.dart b/lib/fileTypeUtils/xml/xmlExtension.dart new file mode 100644 index 0000000..9623d52 --- /dev/null +++ b/lib/fileTypeUtils/xml/xmlExtension.dart @@ -0,0 +1,13 @@ + +import 'package:xml/xml.dart'; + +extension XmlExtension on XmlNode { + String toPrettyString({ int? level }) { + return "${toXmlString( + pretty: true, + indent: "\t", + level: level, + preserveWhitespace: (node) => node.children.whereType().isNotEmpty + )}\n"; + } +} \ No newline at end of file diff --git a/lib/fileTypeUtils/yax/yaxToXml.dart b/lib/fileTypeUtils/yax/yaxToXml.dart index e7aa0f5..267e17d 100644 --- a/lib/fileTypeUtils/yax/yaxToXml.dart +++ b/lib/fileTypeUtils/yax/yaxToXml.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:xml/xml.dart'; import '../../utils.dart'; +import '../xml/xmlExtension.dart'; import 'hashToStringMap.dart'; import 'japToEng.dart'; import '../utils/ByteDataWrapper.dart'; @@ -84,7 +85,7 @@ XmlElement yaxToXml(ByteDataWrapper bytes, { includeAnnotations = true }) { Future yaxFileToXmlFile(String yaxFilePath, String xmlFilePath) async { var bytes = await ByteDataWrapper.fromFile(yaxFilePath); var xml = yaxToXml(bytes); - var xmlString = xml.toXmlString(pretty: true, indent: "\t"); + var xmlString = xml.toPrettyString(); var xmlFile = File(xmlFilePath); await xmlFile.writeAsString('\n'); await xmlFile.writeAsString(xmlString, mode: FileMode.append); diff --git a/lib/fileTypeUtils/z/zlibDecompress.dart b/lib/fileTypeUtils/z/zlibDecompress.dart index f05886c..be1a750 100644 --- a/lib/fileTypeUtils/z/zlibDecompress.dart +++ b/lib/fileTypeUtils/z/zlibDecompress.dart @@ -4,11 +4,13 @@ import 'dart:io'; import 'package:euc/jis.dart'; import 'package:xml/xml.dart'; +import '../xml/xmlExtension.dart'; + Future decompressZlibXml(String input, String output) async { var bytes = await File(input).readAsBytes(); var decompressed = ZLibDecoder().convert(bytes); var str = ShiftJIS().decode(decompressed); var xml = XmlDocument.parse(str); - var xmlStr = xml.toXmlString(pretty: true, indent: "\t"); + var xmlStr = xml.toPrettyString(); await File(output).writeAsString(xmlStr); }