Skip to content

Commit

Permalink
Refactor: Fixes for handling globalgamemanagers
Browse files Browse the repository at this point in the history
Signed-off-by: Bayu Satiyo <[email protected]>
  • Loading branch information
kiraio-moe committed Oct 18, 2023
1 parent 21e881b commit ed6c16b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
91 changes: 50 additions & 41 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
.Title("What would you like to do?")
Expand All @@ -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;
}

Expand All @@ -135,38 +135,38 @@ 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}[/]...");
BrotliUtils.DecompressFile(selectedFile, webDataFile);
}
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}[/]...");
GZipUtils.DecompressFile(selectedFile, webDataFile);
}
else
{
AnsiConsole.MarkupLine("[red]Unknown/Unsupported file type![/]");
AnsiConsole.MarkupLine("[red]( ERROR )[/] Unknown/Unsupported file type!");
goto ChooseAction;
}

Expand All @@ -176,12 +176,10 @@ static void Main(string[] args)

// List of files to be deleted later
List<string> 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));

Expand All @@ -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;
}

Expand Down Expand Up @@ -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<AssetFileInfo>? buildSettingsInfo = assetFile?.GetAssetsOfType(
Expand Down Expand Up @@ -400,7 +395,7 @@ static void Main(string[] args)
// )
// };

List<AssetsReplacer> assetsReplacer = new();
List<AssetsReplacer>? assetsReplacer = new();

switch (choiceIndex)
{
Expand All @@ -413,8 +408,8 @@ static void Main(string[] args)
}

WriteChanges(selectedFile, assetFileInstance, bundleFileInstance, assetsReplacer);

// Utility.Cleanup(temporaryFiles);
assetsManager.UnloadAll(true);
Utility.CleanUp(temporaryFiles);

// try
// {
Expand Down Expand Up @@ -517,7 +512,8 @@ static void Main(string[] args)
// File.Delete(rawWebGLFile);
// }

Console.ReadLine();
Console.WriteLine();
goto ChooseAction;
}

static void PrintHelp()
Expand All @@ -540,21 +536,21 @@ 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[/]"
);
}

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;
Expand Down Expand Up @@ -742,12 +738,11 @@ static List<AssetsReplacer> 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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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()
{
Expand All @@ -843,20 +841,30 @@ out unityLogoDuration
return new();
}

static List<AssetsReplacer> RemoveWatermark(AssetsManager assetsManager, AssetsFileInstance? assetFileInstance)
static List<AssetsReplacer>? RemoveWatermark(AssetsManager assetsManager, AssetsFileInstance? assetFileInstance)
{
AssetsFile? assetFile = assetFileInstance?.file;
List<AssetFileInfo>? 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<AssetFileInfo>? 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> assetsReplacer)
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal static string CloneFile(string sourceFile, string outputFile)
}

/// <summary>
/// Backup a file. If it's already exist, skip.
/// Backup a fileas ".bak". If it's already exist, skip.
/// </summary>
/// <param name="sourceFile"></param>
/// <returns></returns>
Expand All @@ -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;
}
Expand Down

0 comments on commit ed6c16b

Please sign in to comment.