From ed6c16b0a29009a853e12d29e6f92be4ab5cb7e5 Mon Sep 17 00:00:00 2001 From: Bayu Satiyo Date: Wed, 18 Oct 2023 07:07:02 +0700 Subject: [PATCH] Refactor: Fixes for handling globalgamemanagers Signed-off-by: Bayu Satiyo --- .vscode/launch.json | 2 +- Program.cs | 91 +++++++++++++++++++++++++-------------------- Utility.cs | 6 +-- 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3d542fe..f9c9dc3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,7 @@ "args": [], "cwd": "${workspaceFolder}", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "integratedTerminal", + "console": "externalTerminal", "stopAtEntry": false, "requireExactSource": false }, diff --git a/Program.cs b/Program.cs index cd24ac8..205f360 100644 --- a/Program.cs +++ b/Program.cs @@ -96,7 +96,7 @@ static void Main(string[] args) string? ussrExec = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); ChooseAction: - string[] actionList = { "Remove Unity Splash Screen", "Remove Watermark", "Cancel" }; + string[] actionList = { "Remove Unity Splash Screen", "Remove Watermark", "Exit" }; string actionPrompt = AnsiConsole.Prompt( new SelectionPrompt() .Title("What would you like to do?") @@ -119,7 +119,7 @@ static void Main(string[] args) goto ChooseAction; else if (filePicker.IsError) { - AnsiConsole.MarkupLine("[red]Something is wrong.[/]"); + AnsiConsole.MarkupLine("[red]( ERROR )[/]Unable to open File Picker dialog!"); goto ChooseAction; } @@ -135,22 +135,22 @@ static void Main(string[] args) if (Utility.ValidateFile(selectedFile, ggmMagic)) { - AnsiConsole.MarkupLine("globalgamemanagers file selected."); + AnsiConsole.MarkupLine("( INFO ) [green]globalgamemanagers[/] file selected."); assetType = AssetTypes.Asset; } else if (Utility.ValidateFile(selectedFile, unity3dMagic)) { - AnsiConsole.MarkupLine("unity3d file selected."); + AnsiConsole.MarkupLine("( INFO ) [green]unity3d[/] file selected."); assetType = AssetTypes.Bundle; } else if (Utility.ValidateFile(selectedFile, unityWebDataMagic)) { - AnsiConsole.MarkupLine("UnityWebData file selected."); + AnsiConsole.MarkupLine("( INFO ) [green]UnityWebData[/] file selected."); assetType = AssetTypes.WebData; } else if (Utility.ValidateFile(selectedFile, unityBrotliMagic)) { - AnsiConsole.MarkupLine("Brotli file selected."); + AnsiConsole.MarkupLine("( INFO ) [green]Unity Brotli[/] file selected."); assetType = AssetTypes.WebData; AnsiConsole.MarkupLineInterpolated($"Decompress [green]{selectedFile}[/]..."); @@ -158,7 +158,7 @@ static void Main(string[] args) } else if (Utility.ValidateFile(selectedFile, gzipMagic)) { - AnsiConsole.MarkupLine("GZip file selected."); + AnsiConsole.MarkupLine("( INFO ) [green]GZip[/] file selected."); assetType = AssetTypes.WebData; AnsiConsole.MarkupLineInterpolated($"Decompress [green]{selectedFile}[/]..."); @@ -166,7 +166,7 @@ static void Main(string[] args) } else { - AnsiConsole.MarkupLine("[red]Unknown/Unsupported file type![/]"); + AnsiConsole.MarkupLine("[red]( ERROR )[/] Unknown/Unsupported file type!"); goto ChooseAction; } @@ -176,12 +176,10 @@ static void Main(string[] args) // List of files to be deleted later List temporaryFiles = new(); + string inspectedFile = Utility.BackupOnlyOnce(selectedFile); if (assetType == AssetTypes.WebData) { - // Backup original file - Utility.BackupOnlyOnce(selectedFile); - // unpackedWebDataDirectory = UnityWebDataHelper.UnpackWebDataToFile(webDataFile); temporaryFiles.Add(unpackedWebDataDirectory = UnityWebDataHelper.UnpackWebDataToFile(webDataFile)); @@ -193,17 +191,14 @@ static void Main(string[] args) BundleFileInstance? bundleFileInstance = null; FileStream? bundleStream = null; - string tempFile = Utility.CloneFile(selectedFile, $"{selectedFile}.temp"); - temporaryFiles.Add(tempFile); - switch (assetType) { case AssetTypes.Asset: - assetFileInstance = LoadAssetFileInstance(tempFile); + assetFileInstance = LoadAssetFileInstance(inspectedFile); break; case AssetTypes.Bundle: - bundleFileInstance = LoadBundleFileInstance(tempFile, bundleStream); - assetFileInstance = LoadAssetFileInstance(tempFile, bundleFileInstance); + bundleFileInstance = LoadBundleFileInstance(inspectedFile, bundleStream); + assetFileInstance = LoadAssetFileInstance(inspectedFile, bundleFileInstance); break; } @@ -263,7 +258,7 @@ static void Main(string[] args) // AssetBundleFile? bundleFile = bundleFileInstance?.file; // AssetsFile? assetFile = assetFileInstance?.file; - AnsiConsole.MarkupLine("Loading asset class types database..."); + AnsiConsole.MarkupLine("( INFO ) Loading asset class types database..."); assetsManager.LoadClassDatabaseFromPackage(assetFileInstance?.file.Metadata.UnityVersion); // List? buildSettingsInfo = assetFile?.GetAssetsOfType( @@ -400,7 +395,7 @@ static void Main(string[] args) // ) // }; - List assetsReplacer = new(); + List? assetsReplacer = new(); switch (choiceIndex) { @@ -413,8 +408,8 @@ static void Main(string[] args) } WriteChanges(selectedFile, assetFileInstance, bundleFileInstance, assetsReplacer); - - // Utility.Cleanup(temporaryFiles); + assetsManager.UnloadAll(true); + Utility.CleanUp(temporaryFiles); // try // { @@ -517,7 +512,8 @@ static void Main(string[] args) // File.Delete(rawWebGLFile); // } - Console.ReadLine(); + Console.WriteLine(); + goto ChooseAction; } static void PrintHelp() @@ -540,7 +536,7 @@ static void PrintHelp() Console.WriteLine(); AnsiConsole.MarkupLine("[bold green]How to Use:[/]"); AnsiConsole.MarkupLine( - "Choose the action then choose either [green]globalgamemanagers[/] | [green]data.unity3d[/] | [green]WebGL.data[/] | [green]WebGL.data.br[/] | [green]WebGL.data.gz[/] file." + "Select the Action and choose one of this files: [green]globalgamemanagers[/] | [green]data.unity3d[/] | [green]WebGL.data[/] | [green]WebGL.data.br[/] | [green]WebGL.data.gz[/]" ); } @@ -548,13 +544,13 @@ static void LoadClassPackage(AssetsManager assetsManager, string tpkFile) { try { - AnsiConsole.MarkupLine("Loading class type package..."); + AnsiConsole.MarkupLineInterpolated($"( INFO ) Loading class types package: [green]{tpkFile}[/]..."); assetsManager.LoadClassPackage(path: tpkFile); } catch (Exception ex) { AnsiConsole.MarkupLine( - $"[red]Asset class types package not found![/] {ex.Message}" + $"[red]( ERROR )[/] Class types package not found! {ex.Message}" ); Console.ReadLine(); return; @@ -742,12 +738,11 @@ static List RemoveSplashScreen( } AnsiConsole.MarkupLine( - "[yellow]Sometimes USSR can\'t automatically detect Unity splash screen logo and it\'s leading to accidentally removing your own logo.[/] To tackle this, USSR need information about \"Made With Unity\" logo duration." + "( INFO ) Sometimes USSR [yellow]can\'t automatically detect Unity splash screen logo[/] and it\'s leading to accidentally [red]removing your own logo[/]. To tackle this, USSR [green]need information about \"Made With Unity\" logo duration[/]." ); AnsiConsole.MarkupLine( - "[red]Please make a difference with the logo duration when you build your game! If your logo and Unity logo have same duration, USSR will remove both of them.[/] If no value provided, USSR will use it\'s own way to detect it and [red]may removing your own logo[/]." + "( INFO ) Please [red]make a difference with the logo duration[/] when you build your game! [red]If your logo and Unity logo have same duration, USSR will remove both of them[/]. If no value provided, USSR will use it\'s own way to detect it and [red]may removing your own logo[/]." ); - Console.ReadLine(); AnsiConsole.Markup("[green](Optional)[/] Enter Unity splash screen logo duration: "); int unityLogoDuration = 0; @@ -765,7 +760,9 @@ out unityLogoDuration AnsiConsole.WriteException(ex); } - AnsiConsole.MarkupLine("Removing Unity splash screen..."); + AnsiConsole.MarkupLine("( INFO ) Removing Unity splash screen..."); + + AnsiConsole.MarkupLineInterpolated($"( INFO ) hasProVersion = [green]{!hasProVersion}[/] | m_ShowUnitySplashLogo = [green]{!showUnityLogo}[/]"); // Remove Unity splash screen by flipping these boolean fields buildSettingsBase["hasPROVersion"].AsBool = !hasProVersion; // true @@ -816,10 +813,10 @@ out unityLogoDuration if (unityLogo == null && unityLogoDuration <= 0) { AnsiConsole.MarkupLine( - "[red][ERROR][/] Failed to remove Unity splash screen logo!" + "[red]( ERROR )[/] Failed to remove Unity splash screen logo!" ); AnsiConsole.MarkupLine( - "Looks like USSR can'\t detect the Unity splash screen and at the same time you didn'\t provide any value to the input to help USSR find the splash screen. [yellow]Try again and fill in the input.[/]" + "Looks like USSR [red]can'\t detect the Unity splash screen[/] and at the same time [red]you didn'\t provide any value to the input[/] to help USSR find the splash screen. [yellow]Try again and fill in the input.[/]" ); goto Dispose; } @@ -828,9 +825,10 @@ out unityLogoDuration * Remove "UnitySplash-cube" to completely remove * Unity splash screen logo. */ + AnsiConsole.MarkupLineInterpolated($"( INFO ) Removing [red]UnitySplash-cube[/]..."); splashScreenLogos?.Children.Remove(unityLogo); - AnsiConsole.MarkupLine("[green]Successfully removed the Unity splash screen.[/]"); + AnsiConsole.MarkupLine("( INFO ) [green]Successfully removed the Unity splash screen.[/]"); return new() { @@ -843,20 +841,30 @@ out unityLogoDuration return new(); } - static List RemoveWatermark(AssetsManager assetsManager, AssetsFileInstance? assetFileInstance) + static List? RemoveWatermark(AssetsManager assetsManager, AssetsFileInstance? assetFileInstance) { - AssetsFile? assetFile = assetFileInstance?.file; - List? buildSettingsInfo = assetFile?.GetAssetsOfType(AssetClassID.BuildSettings); - AssetTypeValueField buildSettingsBase = assetsManager.GetBaseField(assetFileInstance, buildSettingsInfo?[0]); + try + { + AnsiConsole.MarkupLine("( INFO ) Removing watermark..."); - buildSettingsBase["isNoWatermarkBuild"].AsBool = true; + AssetsFile? assetFile = assetFileInstance?.file; + List? buildSettingsInfo = assetFile?.GetAssetsOfType(AssetClassID.BuildSettings); + AssetTypeValueField buildSettingsBase = assetsManager.GetBaseField(assetFileInstance, buildSettingsInfo?[0]); - AnsiConsole.MarkupLine("[green]Watermark successfully removed.[/]"); + AnsiConsole.MarkupLineInterpolated($"( INFO ) isNoWatermarkBuild = [green]true[/]"); + buildSettingsBase["isNoWatermarkBuild"].AsBool = true; - return new() + AnsiConsole.MarkupLine("( INFO ) [green]Watermark successfully removed.[/]"); + return new() + { + new AssetsReplacerFromMemory(assetFile, buildSettingsInfo?[0], buildSettingsBase) + }; + } + catch (Exception ex) { - new AssetsReplacerFromMemory(assetFile, buildSettingsInfo?[0], buildSettingsBase) - }; + AnsiConsole.MarkupLineInterpolated($"( ERROR ) Unable to remove watermark! {ex.Message}"); + return null; + } } static void WriteChanges(string modifiedFile, AssetsFileInstance? assetFileInstance, BundleFileInstance? bundleFileInstance, List assetsReplacer) @@ -869,6 +877,7 @@ static void WriteChanges(string modifiedFile, AssetsFileInstance? assetFileInsta { case AssetTypes.Asset: { + AnsiConsole.MarkupLineInterpolated($"( INFO ) Writing changes to [green]{modifiedFile}[/]..."); using AssetsFileWriter writer = new(modifiedFile); assetFileInstance?.file.Write(writer, 0, assetsReplacer); break; diff --git a/Utility.cs b/Utility.cs index 9276d94..ffdfef1 100644 --- a/Utility.cs +++ b/Utility.cs @@ -65,7 +65,7 @@ internal static string CloneFile(string sourceFile, string outputFile) } /// - /// Backup a file. If it's already exist, skip. + /// Backup a fileas ".bak". If it's already exist, skip. /// /// /// @@ -75,10 +75,10 @@ internal static string BackupOnlyOnce(string sourceFile) if (!File.Exists(backupFile)) { - AnsiConsole.MarkupLine("Backup original file..."); - + AnsiConsole.MarkupLineInterpolated($"( INFO ) Backup [green]{Path.GetFileNameWithoutExtension(sourceFile)}[/] as [green]{sourceFile}[/]..."); CloneFile(sourceFile, backupFile); } + else backupFile = string.Empty; return backupFile; }