From 93b9c9b1b03c24bd02df2dac68afa88490e4f0ec Mon Sep 17 00:00:00 2001 From: Vhati Date: Mon, 11 Dec 2017 13:34:11 -0500 Subject: [PATCH] Conditionally set encoding to UTF-8 for ftl.dat, vs windows-1252 for data.dat+resource.dat --- .../vhati/modmanager/core/ModPatchThread.java | 16 +++++++++----- .../vhati/modmanager/core/ModUtilities.java | 21 +++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/vhati/modmanager/core/ModPatchThread.java b/src/main/java/net/vhati/modmanager/core/ModPatchThread.java index 88bef7c..5cd2ce4 100644 --- a/src/main/java/net/vhati/modmanager/core/ModPatchThread.java +++ b/src/main/java/net/vhati/modmanager/core/ModPatchThread.java @@ -176,6 +176,8 @@ public class ModPatchThread extends Thread { return true; } + String ultimateEncoding = null; + packContainer = new PackContainer(); if ( ftlDatFile.exists() ) { // FTL 1.6.1. AbstractPack ftlPack = new PkgPack( ftlDatFile, "r+" ); @@ -186,6 +188,8 @@ public class ModPatchThread extends Thread { packContainer.setPackFor( "img/", ftlPack ); packContainer.setPackFor( null, ftlPack ); // Supposedly "exe_icon.png" has been observed at top-level? + + ultimateEncoding = "UTF-8"; } else if ( dataDatFile.exists() && resourceDatFile.exists() ) { // FTL 1.01-1.5.13. AbstractPack dataPack = new FTLPack( dataDatFile, "r+" ); @@ -195,6 +199,8 @@ public class ModPatchThread extends Thread { packContainer.setPackFor( "data/", dataPack ); packContainer.setPackFor( "fonts/", resourcePack ); packContainer.setPackFor( "img/", resourcePack ); + + ultimateEncoding = "windows-1252"; } else { throw new IOException( String.format( "Could not find either \"%s\" or both \"%s\" and \"%s\"", ftlDatFile.getName(), dataDatFile.getName(), resourceDatFile.getName() ) ); @@ -277,7 +283,7 @@ public class ModPatchThread extends Thread { InputStream mainStream = null; try { mainStream = pack.getInputStream( innerPath ); - InputStream mergedStream = ModUtilities.patchXMLFile( mainStream, zis, "windows-1252", globalPanic, pack.getName()+":"+innerPath, modFile.getName()+":"+parentPath+fileName ); + InputStream mergedStream = ModUtilities.patchXMLFile( mainStream, zis, ultimateEncoding, globalPanic, pack.getName()+":"+innerPath, modFile.getName()+":"+parentPath+fileName ); mainStream.close(); pack.remove( innerPath ); pack.add( innerPath, mergedStream ); @@ -304,7 +310,7 @@ public class ModPatchThread extends Thread { InputStream mainStream = null; try { mainStream = pack.getInputStream( innerPath ); - InputStream mergedStream = ModUtilities.appendXMLFile( mainStream, zis, "windows-1252", pack.getName()+":"+innerPath, modFile.getName()+":"+parentPath+fileName ); + InputStream mergedStream = ModUtilities.appendXMLFile( mainStream, zis, ultimateEncoding, pack.getName()+":"+innerPath, modFile.getName()+":"+parentPath+fileName ); mainStream.close(); pack.remove( innerPath ); pack.add( innerPath, mergedStream ); @@ -330,7 +336,7 @@ public class ModPatchThread extends Thread { String fixedText = ModUtilities.decodeText( zis, modFile.getName()+":"+parentPath+fileName ).text; fixedText = Pattern.compile("\n").matcher( fixedText ).replaceAll( "\r\n" ); - InputStream fixedStream = ModUtilities.encodeText( fixedText, "windows-1252", modFile.getName()+":"+parentPath+fileName+" (with new EOL)" ); + InputStream fixedStream = ModUtilities.encodeText( fixedText, ultimateEncoding, modFile.getName()+":"+parentPath+fileName+" (with new EOL)" ); if ( !moddedItems.contains( innerPath ) ) { moddedItems.add( innerPath ); @@ -345,7 +351,7 @@ public class ModPatchThread extends Thread { else if ( fileName.endsWith( ".xml" ) ) { innerPath = checkCase( innerPath, knownPaths, knownPathsLower ); - InputStream fixedStream = ModUtilities.rebuildXMLFile( zis, "windows-1252", modFile.getName()+":"+parentPath+fileName ); + InputStream fixedStream = ModUtilities.rebuildXMLFile( zis, ultimateEncoding, modFile.getName()+":"+parentPath+fileName ); if ( !moddedItems.contains( innerPath ) ) { moddedItems.add( innerPath ); @@ -365,7 +371,7 @@ public class ModPatchThread extends Thread { String fixedText = ModUtilities.decodeText( zis, modFile.getName()+":"+parentPath+fileName ).text; fixedText = Pattern.compile("\n").matcher( fixedText ).replaceAll( "\r\n" ); - InputStream fixedStream = ModUtilities.encodeText( fixedText, "windows-1252", modFile.getName()+":"+parentPath+fileName+" (with new EOL)" ); + InputStream fixedStream = ModUtilities.encodeText( fixedText, ultimateEncoding, modFile.getName()+":"+parentPath+fileName+" (with new EOL)" ); if ( !moddedItems.contains( innerPath ) ) { moddedItems.add( innerPath ); diff --git a/src/main/java/net/vhati/modmanager/core/ModUtilities.java b/src/main/java/net/vhati/modmanager/core/ModUtilities.java index efcd165..43212c4 100644 --- a/src/main/java/net/vhati/modmanager/core/ModUtilities.java +++ b/src/main/java/net/vhati/modmanager/core/ModUtilities.java @@ -179,8 +179,11 @@ public class ModUtilities { * closing. * * The result will have CR-LF line endings and the desired encoding. - * Note: FTL stubbornly assumes all XML is in windows-1252 encoding, even - * on Linux. + * + * FTL 1.01-1.5.13 assumes all XML is in windows-1252 encoding, even on + * Linux. + * + * FTL 1.6.1 assumes all XML is in UTF-8 encoding. * * The description arguments identify the streams for log messages. * @@ -244,8 +247,11 @@ public class ModUtilities { * which doesn't need closing. * * The result will have CR-LF line endings and the desired encoding. - * Note: FTL stubbornly assumes all XML is in windows-1252 encoding, - * even on Linux. + * + * FTL 1.01-1.5.13 assumes all XML is in windows-1252 encoding, even on + * Linux. + * + * FTL 1.6.1 assumes all XML is in UTF-8 encoding. * * The description arguments identify the streams for log messages. * @@ -334,8 +340,11 @@ public class ModUtilities { * which doesn't need closing. * * The result will have CR-LF line endings and the desired encoding. - * Note: FTL stubbornly assumes all XML is in windows-1252 encoding, - * even on Linux. + * + * FTL 1.01-1.5.13 assumes all XML is in windows-1252 encoding, even on + * Linux. + * + * FTL 1.6.1 assumes all XML is in UTF-8 encoding. * * The description argument identifies the stream for log messages. *