Skip to content

Commit

Permalink
Initialization Fixes
Browse files Browse the repository at this point in the history
Reimplemented Game Name and Version in Console Window Name
Fixed an issue with Console and Logger methods throwing an error when Console hasn't initialized yet
Fixed an issue with AsmResolver update breaking several usages in ML
Improved Unity Version parsing
  • Loading branch information
HerpDerpinstine committed Jul 12, 2024
1 parent ad7f963 commit 6a6cd39
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
7 changes: 4 additions & 3 deletions MelonLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ internal static int Initialize()
Fixes.DotnetLoadFromManagedFolderFix.Install();
Fixes.UnhandledException.Install(AppDomain.CurrentDomain);
Fixes.ServerCertificateValidation.Install();

MelonUtils.Setup(AppDomain.CurrentDomain);

Assertions.LemonAssertMapping.Setup();

MelonUtils.Setup(AppDomain.CurrentDomain);
BootstrapInterop.SetDefaultConsoleTitleWithGameName(UnityInformationHandler.GameName,
UnityInformationHandler.GameVersion);

try
{
if (!MonoLibrary.Setup()
Expand Down
2 changes: 1 addition & 1 deletion MelonLoader/InternalUtils/MelonStartScreen.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using AssetRipper.VersionUtilities;

Check failure on line 2 in MelonLoader/InternalUtils/MelonStartScreen.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'VersionUtilities' does not exist in the namespace 'AssetRipper' (are you missing an assembly reference?)

Check failure on line 2 in MelonLoader/InternalUtils/MelonStartScreen.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'VersionUtilities' does not exist in the namespace 'AssetRipper' (are you missing an assembly reference?)
using MelonLoader.Modules;
using MelonLoader.Utils;
using UnityVersion = AssetRipper.Primitives.UnityVersion;

namespace MelonLoader.InternalUtils
{
Expand Down
52 changes: 25 additions & 27 deletions MelonLoader/InternalUtils/UnityInformationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using System.Text.RegularExpressions;
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using UnityVersion = AssetRipper.Primitives.UnityVersion;
using System.Drawing;
using MelonLoader.Utils;
using UnityVersion = AssetRipper.VersionUtilities.UnityVersion;

Check failure on line 12 in MelonLoader/InternalUtils/UnityInformationHandler.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'VersionUtilities' does not exist in the namespace 'AssetRipper' (are you missing an assembly reference?)

Check failure on line 12 in MelonLoader/InternalUtils/UnityInformationHandler.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'VersionUtilities' does not exist in the namespace 'AssetRipper' (are you missing an assembly reference?)

namespace MelonLoader.InternalUtils
{
Expand All @@ -19,22 +19,31 @@ public static class UnityInformationHandler

public static string GameName { get; private set; }
public static string GameDeveloper { get; private set; }
public static UnityVersion EngineVersion { get; private set; } = UnityVersion.MinVersion;
public static UnityVersion EngineVersion { get; private set; }
public static string GameVersion { get; private set; }

private static UnityVersion TryParse(string version)
{
UnityVersion returnval = UnityVersion.MinVersion;
try
{
returnval = UnityVersion.Parse(version);
}
catch (Exception ex)
{
if (MelonDebug.IsEnabled())
MelonLogger.Error(ex);
returnval = UnityVersion.MinVersion;
}
return returnval;
}

internal static void Setup()
{
string gameDataPath = MelonEnvironment.UnityGameDataDirectory;

if (!string.IsNullOrEmpty(MelonLaunchOptions.Core.UnityVersion))
{
try { EngineVersion = UnityVersion.Parse(MelonLaunchOptions.Core.UnityVersion); }
catch (Exception ex)
{
if (MelonDebug.IsEnabled())
MelonLogger.Error(ex);
}
}
EngineVersion = TryParse(MelonLaunchOptions.Core.UnityVersion);

AssetsManager assetsManager = new AssetsManager();
ReadGameInfo(assetsManager, gameDataPath);
Expand All @@ -45,22 +54,12 @@ internal static void Setup()
ReadGameInfoFallback();

if (EngineVersion == UnityVersion.MinVersion)
{
try { EngineVersion = ReadVersionFallback(gameDataPath); }
catch (Exception ex)
{
if (MelonDebug.IsEnabled())
MelonLogger.Error(ex);
}
}
EngineVersion = ReadVersionFallback(gameDataPath);

if (string.IsNullOrEmpty(GameDeveloper))
GameDeveloper = DefaultInfo;
if (string.IsNullOrEmpty(GameName))
GameName = DefaultInfo;

//BootstrapInterop.SetDefaultConsoleTitleWithGameName(GameName, GameVersion);

if (string.IsNullOrEmpty(GameVersion))
GameVersion = DefaultInfo;

Expand Down Expand Up @@ -101,7 +100,7 @@ private static void ReadGameInfo(AssetsManager assetsManager, string gameDataPat
assetsManager.LoadClassDatabaseFromPackage(instance.file.Metadata.UnityVersion);

if (EngineVersion == UnityVersion.MinVersion)
EngineVersion = UnityVersion.Parse(instance.file.Metadata.UnityVersion);
EngineVersion = TryParse(instance.file.Metadata.UnityVersion);

List<AssetFileInfo> assetFiles = instance.file.GetAssetsOfType(AssetClassID.PlayerSettings);
if (assetFiles.Count > 0)
Expand Down Expand Up @@ -129,7 +128,6 @@ private static void ReadGameInfo(AssetsManager assetsManager, string gameDataPat
{
if (MelonDebug.IsEnabled())
MelonLogger.Error(ex);
//MelonLogger.Error("Failed to Initialize Assets Manager!");
}
if (instance != null)
instance.file.Close();
Expand Down Expand Up @@ -170,7 +168,7 @@ private static UnityVersion ReadVersionFallback(string gameDataPath)
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var unityVer = FileVersionInfo.GetVersionInfo(unityPlayerPath);
return new UnityVersion((ushort)unityVer.FileMajorPart, (ushort)unityVer.FileMinorPart, (ushort)unityVer.FileBuildPart);
return TryParse(unityVer.FileVersion);
}

try
Expand All @@ -197,7 +195,7 @@ private static UnityVersion ReadVersionFallback(string gameDataPath)
MelonLogger.Error(ex);
}

return default;
return UnityVersion.MinVersion;
}

private static UnityVersion GetVersionFromGlobalGameManagers(byte[] ggmBytes)
Expand Down Expand Up @@ -225,7 +223,7 @@ private static UnityVersion GetVersionFromGlobalGameManagers(byte[] ggmBytes)
unityVer = verString.ToString().Trim();
}

return UnityVersion.Parse(unityVer);
return TryParse(unityVer);
}

private static UnityVersion GetVersionFromDataUnity3D(Stream fileStream)
Expand All @@ -248,7 +246,7 @@ private static UnityVersion GetVersionFromDataUnity3D(Stream fileStream)
verString.Append(Convert.ToChar(read));
}

return UnityVersion.Parse(verString.ToString().Trim());
return TryParse(verString.ToString().Trim());
}
}
}
4 changes: 3 additions & 1 deletion MelonLoader/Utils/AssemblyVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if NET6_0
using AsmResolver.DotNet;
using AsmResolver.PE.DotNet.Metadata;
using AsmResolver.PE.DotNet.Metadata.Strings;

Check failure on line 4 in MelonLoader/Utils/AssemblyVerifier.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'Strings' does not exist in the namespace 'AsmResolver.PE.DotNet.Metadata' (are you missing an assembly reference?)
using AsmResolver.PE.DotNet.Metadata.Tables;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;

Check failure on line 6 in MelonLoader/Utils/AssemblyVerifier.cs

View workflow job for this annotation

GitHub Actions / build_nuget_package

The type or namespace name 'Rows' does not exist in the namespace 'AsmResolver.PE.DotNet.Metadata.Tables' (are you missing an assembly reference?)
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -41,7 +43,7 @@ internal static void EnsureInitialized()
var dummyListToEnsureThisCodeDoesntGetNuked = new List<object>();

//Force load AsmResolver
dummyListToEnsureThisCodeDoesntGetNuked.Add(new Constant(AsmResolver.PE.DotNet.Metadata.Tables.ElementType.Class, null));
dummyListToEnsureThisCodeDoesntGetNuked.Add(new Constant(ElementType.Class, null));
dummyListToEnsureThisCodeDoesntGetNuked.Add(typeof(AsmResolver.PE.File.PEFile));
}

Expand Down
12 changes: 9 additions & 3 deletions MelonLoader/Utils/MelonConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ internal static void Init()
};
}

private static bool ShouldNotUseWriter()
=> (MelonUtils.IsUnderWineOrSteamProton()
|| !MelonUtils.IsWindows
|| MelonLaunchOptions.Console.ShouldHide
|| (ConsoleOutWriter == null));

internal static void WriteLine(string txt)
{
if (MelonUtils.IsUnderWineOrSteamProton() || !MelonUtils.IsWindows || MelonLaunchOptions.Console.ShouldHide)
if (ShouldNotUseWriter())
{
Console.WriteLine(txt);
return;
Expand All @@ -48,7 +54,7 @@ internal static void WriteLine(string txt)

internal static void WriteLine(object txt)
{
if (MelonUtils.IsUnderWineOrSteamProton() || !MelonUtils.IsWindows || MelonLaunchOptions.Console.ShouldHide)
if (ShouldNotUseWriter())
{
Console.WriteLine(txt.ToString());
return;
Expand All @@ -58,7 +64,7 @@ internal static void WriteLine(object txt)

internal static void WriteLine()
{
if (MelonUtils.IsUnderWineOrSteamProton() || !MelonUtils.IsWindows || MelonLaunchOptions.Console.ShouldHide)
if (ShouldNotUseWriter())
{
Console.WriteLine();
return;
Expand Down

0 comments on commit 6a6cd39

Please sign in to comment.