From 506ceaa1db2a58c1177a6e54b1b5bff9253179bc Mon Sep 17 00:00:00 2001 From: Bitl Date: Wed, 2 Nov 2022 13:22:43 -0700 Subject: [PATCH] add BZip2 support to Asset Fixer. --- .../StorageAndFunctions/ClientManagement.cs | 32 +------- .../NovetusCore/StorageAndFunctions/Util.cs | 79 +++++++++++++++++++ .../Forms/LauncherForm/LauncherFormShared.cs | 2 +- .../NovetusLauncher/Forms/SDK/AssetFixer.cs | 42 ++++++---- 4 files changed, 112 insertions(+), 43 deletions(-) diff --git a/Novetus/NovetusCore/StorageAndFunctions/ClientManagement.cs b/Novetus/NovetusCore/StorageAndFunctions/ClientManagement.cs index d774ce0..1be268f 100644 --- a/Novetus/NovetusCore/StorageAndFunctions/ClientManagement.cs +++ b/Novetus/NovetusCore/StorageAndFunctions/ClientManagement.cs @@ -982,40 +982,16 @@ public class ClientManagement #endif #if LAUNCHER - //https://stackoverflow.com/questions/30687987/unable-to-decompress-bz2-file-has-orginal-file-using-dotnetzip-library - private static string Decompress(bool forceOverwrite) + public static void DecompressMap() { - var outFname = GlobalVars.UserConfiguration.MapPath.Replace(".bz2", ""); - if (File.Exists(outFname)) - { - if (forceOverwrite) - File.Delete(outFname); - else - return null; - } - - using (Stream fs = File.OpenRead(GlobalVars.UserConfiguration.MapPath), - output = File.Create(outFname), - decompressor = new Ionic.BZip2.BZip2InputStream(fs)) - Pump(decompressor, output); - - return outFname; + DecompressMap(ScriptType.None, false); } - private static void Pump(Stream src, Stream dest) - { - byte[] buffer = new byte[2048]; - int n; - while ((n = src.Read(buffer, 0, buffer.Length)) > 0) - dest.Write(buffer, 0, n); - - } - - private static void DecompressMap(ScriptType type, bool nomap) + public static void DecompressMap(ScriptType type, bool nomap) { if ((type != ScriptType.Client || type != ScriptType.EasterEgg) && !nomap && GlobalVars.UserConfiguration.Map.Contains(".bz2")) { - Decompress(true); + Util.Decompress(GlobalVars.UserConfiguration.MapPath, true); GlobalVars.UserConfiguration.MapPath = GlobalVars.UserConfiguration.MapPath.Replace(".bz2", ""); GlobalVars.UserConfiguration.Map = GlobalVars.UserConfiguration.Map.Replace(".bz2", ""); diff --git a/Novetus/NovetusCore/StorageAndFunctions/Util.cs b/Novetus/NovetusCore/StorageAndFunctions/Util.cs index 693bb1d..42911a7 100644 --- a/Novetus/NovetusCore/StorageAndFunctions/Util.cs +++ b/Novetus/NovetusCore/StorageAndFunctions/Util.cs @@ -791,6 +791,85 @@ public static class Util } } } + +#if LAUNCHER + //https://stackoverflow.com/questions/30687987/unable-to-decompress-bz2-file-has-orginal-file-using-dotnetzip-library + public static string Compress(string sourceFile, bool forceOverwrite) + { + var outFname = sourceFile + ".bz2"; + + if (File.Exists(outFname)) + { + if (forceOverwrite) + File.Delete(outFname); + else + return null; + } + long rowCount = 0; + var output = File.Create(outFname); + + try + { + using (StreamReader reader = new StreamReader(sourceFile)) + { + using (var compressor = new Ionic.BZip2.ParallelBZip2OutputStream(output)) + { + StreamWriter writer = new StreamWriter(compressor, Encoding.UTF8); + string line = ""; + while ((line = reader.ReadLine()) != null) + { + writer.WriteLine(line); + rowCount++; + } + + writer.Close(); + compressor.Close(); + } + } + } + catch (Exception) + { + + throw; + } + finally + { + if (output != null) + output = null; + } + + // Pump(fs, compressor); + + return outFname; + } + + public static string Decompress(string sourceFile, bool forceOverwrite) + { + var outFname = sourceFile.Replace(".bz2", ""); + if (File.Exists(outFname)) + { + if (forceOverwrite) + File.Delete(outFname); + else + return null; + } + + using (Stream fs = File.OpenRead(sourceFile), + output = File.Create(outFname), + decompressor = new Ionic.BZip2.BZip2InputStream(fs)) + Pump(decompressor, output); + + return outFname; + } + + private static void Pump(Stream src, Stream dest) + { + byte[] buffer = new byte[2048]; + int n; + while ((n = src.Read(buffer, 0, buffer.Length)) > 0) + dest.Write(buffer, 0, n); + } +#endif #endregion #if !BASICLAUNCHER diff --git a/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs b/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs index f014e4c..82fc948 100644 --- a/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs +++ b/Novetus/NovetusLauncher/Forms/LauncherForm/LauncherFormShared.cs @@ -1095,7 +1095,7 @@ namespace NovetusLauncher { using (var ofd = new OpenFileDialog()) { - ofd.Filter = "Roblox Level (*.rbxl)|*.rbxl|Roblox Level (*.rbxlx)|*.rbxlx|bzip2 compressed Roblox Level (*.bz2)|*.bz2"; + ofd.Filter = "Roblox Level (*.rbxl)|*.rbxl|Roblox Level (*.rbxlx)|*.rbxlx|BZip2 compressed Roblox Level (*.bz2)|*.bz2"; ofd.FilterIndex = 1; ofd.Title = "Load Roblox map"; if (ofd.ShowDialog() == DialogResult.OK) diff --git a/Novetus/NovetusLauncher/Forms/SDK/AssetFixer.cs b/Novetus/NovetusLauncher/Forms/SDK/AssetFixer.cs index d6689ac..a6b2a3d 100644 --- a/Novetus/NovetusLauncher/Forms/SDK/AssetFixer.cs +++ b/Novetus/NovetusLauncher/Forms/SDK/AssetFixer.cs @@ -23,6 +23,7 @@ public partial class AssetFixer : Form private string customFolder; private int errors = 0; private bool hasOverrideWarningOpenedOnce = false; + private bool compressedMap = true; #endregion #region Constructor @@ -132,7 +133,7 @@ public partial class AssetFixer : Form switch (type) { case RobloxFileType.RBXL: - typeFilter = "Roblox Level (*.rbxl)|*.rbxl|Roblox Level (*.rbxlx)|*.rbxlx"; + typeFilter = "Roblox Level (*.rbxl)|*.rbxl|Roblox Level (*.rbxlx)|*.rbxlx|BZip2 compressed Roblox Level (*.bz2)|*.bz2"; break; case RobloxFileType.Script: typeFilter = "Lua Script (*.lua)|*.lua"; @@ -283,6 +284,25 @@ public partial class AssetFixer : Form public void LocalizeAsset(RobloxFileType type, BackgroundWorker worker, string path, bool useURLs = false, string remoteurl = "") { + if (GlobalVars.UserConfiguration.AssetSDKFixerSaveBackups) + { + try + { + Util.FixedFileCopy(path, path + ".bak", false); + } + catch (Exception ex) + { + Util.LogExceptions(ex); + return; + } + } + + if (path.Contains(".bz2")) + { + Util.Decompress(path, true); + compressedMap = true; + } + LocalizePermanentlyIfNeeded(); AssetFixer_ProgressLabel.Text = "Loading..."; @@ -298,19 +318,6 @@ public partial class AssetFixer : Form } } - if (!error && GlobalVars.UserConfiguration.AssetSDKFixerSaveBackups) - { - try - { - Util.FixedFileCopy(path, path.Replace(".", " - BAK."), false); - } - catch (Exception ex) - { - Util.LogExceptions(ex); - return; - } - } - //assume we're a script try { @@ -321,6 +328,13 @@ public partial class AssetFixer : Form else { FixURLSOrDownloadFromScript(path, GlobalPaths.AssetCacheDirAssets, GlobalPaths.AssetCacheAssetsGameDir, useURLs, url); + + if (compressedMap) + { + //compress adds bz2 to our file though? this shouldn't be necessary. + Util.Compress(path.Replace(".rbxlx.bz2", ".rbxlx").Replace(".rbxl.bz2", ".rbxl"), true); + Util.FixedFileDelete(path.Replace(".rbxlx.bz2", ".rbxlx").Replace(".rbxl.bz2", ".rbxl")); + } } } catch (Exception ex)