Skip to content

Commit

Permalink
Implemented Unity.Shared
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Dec 8, 2023
1 parent 8a61934 commit 0893734
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 40 deletions.
8 changes: 7 additions & 1 deletion MelonLoader/MelonLoader.Bootstrap/Entrypoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MelonLoader.Utils;
using MelonLoader.Fixes;
using MelonLoader.Utils;
using System.IO;

namespace MelonLoader.Bootstrap
{
Expand All @@ -24,6 +26,10 @@ public static unsafe void Entry()

MelonDebug.Msg("Found Bootstrap Module: " + module.GetType().FullName);
MelonLogger.Msg($"Running Engine: {module.EngineName}");

// Fix Resolving Issue
UnhandledAssemblyResolve.AddSearchDirectoryToFront(Path.Combine(MelonEnvironment.ModulesDirectory, module.EngineName, "net6"));

module.Startup(); // TO-DO: Implement Fallback Handling for when a Module Fails to Startup
}
}
Expand Down
35 changes: 32 additions & 3 deletions MelonLoader/MelonLoader.Shared/Fixes/UnhandledAssemblyResolve.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MelonLoader.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

Expand All @@ -15,7 +16,7 @@ public static class UnhandledAssemblyResolve
private static AssemblyLoadContext _alc;
#endif

private static string[] SearchableDirectories = new string[]
private static List<string> SearchableDirectories = new List<string>
{
#if NET6_0
Path.Combine(MelonEnvironment.ModulesDirectory, "Mono", "net6"),
Expand All @@ -38,7 +39,7 @@ public static class UnhandledAssemblyResolve
MelonEnvironment.GameRootDirectory
};

public static void Install()
internal static void Install()
{
#if NET6_0
AssemblyLoadContext.Default.Resolving += OnResolve;
Expand All @@ -48,18 +49,46 @@ public static void Install()
#endif
}

public static void AddSearchDirectoryToFront(string path)
{
if (SearchableDirectories.Contains(path))
return;

string[] dirArr = SearchableDirectories.ToArray();
SearchableDirectories.Clear();
SearchableDirectories.Add(path);
SearchableDirectories.AddRange(dirArr);
}

public static void AddSearchDirectory(string path)
{
if (SearchableDirectories.Contains(path))
return;
SearchableDirectories.Add(path);
}

public static void RemoveSearchDirectory(string path)
{
if (!SearchableDirectories.Contains(path))
return;
SearchableDirectories.Remove(path);
}

private static Assembly FindAssembly(string name, Func<string, Assembly> tryLoad)
{
var filename = name + ".dll";

Assembly ret = null;
foreach (string folder in SearchableDirectories)
{
if (!Directory.Exists(folder))
continue;

ret = tryLoad(Path.Combine(folder, filename));
if (ret != null)
return ret;

foreach (var childFolder in Directory.GetDirectories(folder))
foreach (var childFolder in Directory.GetDirectories(folder, "*", SearchOption.AllDirectories))
{
ret = tryLoad(Path.Combine(childFolder, filename));
if (ret != null)
Expand Down
4 changes: 2 additions & 2 deletions MelonLoader/MelonLoader.Shared/Fixes/UnhandledException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace MelonLoader.Fixes
{
public static class UnhandledException
internal static class UnhandledException
{
public static void Install(AppDomain domain) =>
internal static void Install(AppDomain domain) =>
domain.UnhandledException +=
(sender, args) =>
MelonLogger.Error((args.ExceptionObject as Exception).ToString());
Expand Down
2 changes: 1 addition & 1 deletion MelonLoader/MelonLoader.Shared/MelonLoader.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageProjectUrl>https://github.com/LavaGang/MelonLoader</PackageProjectUrl>
<Authors>Lava Gang</Authors>
<Company>Lava Gang</Company>
<Copyright>Copyright (c) 2023 Lava Gang</Copyright>
<Copyright>Copyright (c) 2020 - 2024 Lava Gang</Copyright>
<Description>The World's First Universal Mod Loader, expandable to work on any game engine.</Description>
</PropertyGroup>
<PropertyGroup>
Expand Down
78 changes: 52 additions & 26 deletions MelonLoader/MelonLoader.Shared/Utils/MelonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,60 @@ public static IntPtr ToAnsiPointer(this string str)

#endregion

#region Enum

/// <summary>
/// From: http://www.sambeauvois.be/blog/2011/08/enum-hasflag-method-extension-for-4-0-framework/
/// A FX 3.5 way to mimic the FX4 "HasFlag" method.
/// </summary>
/// <param name="variable">The tested enum.</param>
/// <param name="value">The value to test.</param>
/// <returns>True if the flag is set. Otherwise false.</returns>
public static bool HasFlag(this Enum variable, Enum value)
{
// check if from the same type.
if (variable.GetType() != value.GetType())
throw new ArgumentException("The checked flag is not from the same type as the checked variable.");

ulong num = Convert.ToUInt64(value);
ulong num2 = Convert.ToUInt64(variable);
return (num2 & num) == num;
}

#endregion

#region Color

private static Dictionary<Color, ConsoleColor> DrawingColorDict = new Dictionary<Color, ConsoleColor>
{
{ Color.Black, ConsoleColor.Black },
{ Color.DarkBlue, ConsoleColor.DarkBlue },
{ Color.DarkGreen, ConsoleColor.DarkGreen },
{ Color.DarkCyan, ConsoleColor.DarkCyan },
{ Color.DarkRed, ConsoleColor.DarkRed },
{ Color.DarkMagenta, ConsoleColor.DarkMagenta },
{ Color.Yellow, ConsoleColor.Yellow },
{ Color.LightGray, ConsoleColor.Gray },
{ Color.DarkGray, ConsoleColor.DarkGray },
{ Color.CornflowerBlue, ConsoleColor.Blue } ,
{ Color.LimeGreen, ConsoleColor.Green },
{ Color.Cyan, ConsoleColor.Cyan },
{ Color.IndianRed, ConsoleColor.Red },
{ Color.Magenta, ConsoleColor.Magenta },
{ Color.White, ConsoleColor.White },
};

public static ConsoleColor ToConsoleColor(this Color color)
{
if (!DrawingColorDict.ContainsKey(color))
return MelonUtils.DefaultTextConsoleColor;
return DrawingColorDict[color];
}

#endregion

#region ConsoleColor

private static Dictionary<ConsoleColor, Color> ConsoleColorDict = new Dictionary<ConsoleColor, Color>
{
{ ConsoleColor.Black, Color.Black },
Expand All @@ -85,39 +137,13 @@ public static IntPtr ToAnsiPointer(this string str)
{ ConsoleColor.White, Color.White },
};

private static Dictionary<Color, ConsoleColor> DrawingColorDict = new Dictionary<Color, ConsoleColor>
{
{ Color.Black, ConsoleColor.Black },
{ Color.DarkBlue, ConsoleColor.DarkBlue },
{ Color.DarkGreen, ConsoleColor.DarkGreen },
{ Color.DarkCyan, ConsoleColor.DarkCyan },
{ Color.DarkRed, ConsoleColor.DarkRed },
{ Color.DarkMagenta, ConsoleColor.DarkMagenta },
{ Color.Yellow, ConsoleColor.Yellow },
{ Color.LightGray, ConsoleColor.Gray },
{ Color.DarkGray, ConsoleColor.DarkGray },
{ Color.CornflowerBlue, ConsoleColor.Blue } ,
{ Color.LimeGreen, ConsoleColor.Green },
{ Color.Cyan, ConsoleColor.Cyan },
{ Color.IndianRed, ConsoleColor.Red },
{ Color.Magenta, ConsoleColor.Magenta },
{ Color.White, ConsoleColor.White },
};

public static Color ToDrawingColor(this ConsoleColor color)
{
if (!ConsoleColorDict.ContainsKey(color))
return MelonUtils.DefaultTextColor;
return ConsoleColorDict[color];
}

public static ConsoleColor ToConsoleColor(this Color color)
{
if (!DrawingColorDict.ContainsKey(color))
return MelonUtils.DefaultTextConsoleColor;
return DrawingColorDict[color];
}

#endregion
}
}
2 changes: 1 addition & 1 deletion MelonLoader/MelonLoader.Shared/Utils/MelonLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal static void Internal_Warning(string namesection, string txt)

internal static void Internal_Error(string namesection, string txt) => Internal_Msg(Color.IndianRed, Color.IndianRed, namesection, txt);

internal static void WriteSpacer()
public static void WriteSpacer()
{
BootstrapInterop.NativeWriteLogToFile("");
Console.WriteLine();
Expand Down
7 changes: 7 additions & 0 deletions MelonLoader/MelonLoader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CoreCLR", "CoreCLR", "{40E3
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreCLR.Bootstrap", "Modules\CoreCLR\CoreCLR.Bootstrap\CoreCLR.Bootstrap.csproj", "{0950C2EC-9116-4A7A-B5CF-A6C24CB90689}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Shared", "Modules\Unity\Unity.Shared\Unity.Shared.csproj", "{AAB39E01-B118-4481-9796-036E92B857A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,6 +69,10 @@ Global
{0950C2EC-9116-4A7A-B5CF-A6C24CB90689}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0950C2EC-9116-4A7A-B5CF-A6C24CB90689}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0950C2EC-9116-4A7A-B5CF-A6C24CB90689}.Release|Any CPU.Build.0 = Release|Any CPU
{AAB39E01-B118-4481-9796-036E92B857A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAB39E01-B118-4481-9796-036E92B857A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAB39E01-B118-4481-9796-036E92B857A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAB39E01-B118-4481-9796-036E92B857A0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -81,6 +87,7 @@ Global
{5C34E23A-F841-460C-BD45-3EE1ED4E15F8} = {879B734F-9F94-4811-BA4A-28160BD525AA}
{40E3E955-2FEE-4C38-BE89-76F61594DB4A} = {716CE800-E309-4104-ACA3-867BB8BF4FB0}
{0950C2EC-9116-4A7A-B5CF-A6C24CB90689} = {40E3E955-2FEE-4C38-BE89-76F61594DB4A}
{AAB39E01-B118-4481-9796-036E92B857A0} = {3E7F5C09-221B-4492-9BA0-2D1ED9BEC58C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4490CB48-16CD-4D0A-858D-1484CB9DED02}
Expand Down
23 changes: 17 additions & 6 deletions MelonLoader/Modules/Unity/Unity.Bootstrap/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
using MelonLoader.Interfaces;
using MelonLoader.Utils;
using MelonLoader.Mono.Bootstrap;
using System;
using System.Collections.Generic;
using MelonLoader.Unity.Utils;
using System.Drawing;
using MelonLoader.Fixes;

namespace MelonLoader.Unity
{
public class Bootstrap : IBootstrapModule
{
private static readonly string GameDataPath = $"{Path.Combine(MelonEnvironment.GameRootDirectory, MelonEnvironment.GameExecutableName)}_Data";

public string EngineName => "Unity";

/// <summary>
Expand All @@ -27,10 +31,18 @@ public bool IsMyEngine
}
}

internal static string GameDataPath { get; private set; } = $"{Path.Combine(MelonEnvironment.GameRootDirectory, MelonEnvironment.GameExecutableName)}_Data";

public void Startup()
{
// Read Game Info
UnityEnvironment.Initialize(GameDataPath);

// Log the Information
//BootstrapInterop.SetDefaultConsoleTitleWithGameName(GameName, GameVersion);
MelonLogger.Msg($"Engine Version: {UnityEnvironment.EngineVersionString}");
MelonLogger.Msg($"Game Name: {UnityEnvironment.GameName}");
MelonLogger.Msg($"Game Developer: {UnityEnvironment.GameDeveloper}");
MelonLogger.Msg($"Game Version: {UnityEnvironment.GameVersion}");

// Get GameAssembly Name
string gameAssemblyName = "GameAssembly";
if (MelonUtils.IsUnix)
Expand All @@ -45,7 +57,7 @@ public void Startup()
if (File.Exists(gameAssemblyPath))
{
// Start Il2Cpp Support
MelonLogger.Msg("Engine Variant: Il2Cpp");
MelonLogger.Msg("Runtime Variant: Il2Cpp");
//Il2CppLoader.Startup(gameAssemblyPath);
}
else
Expand All @@ -56,7 +68,7 @@ public void Startup()
MelonAssertion.ThrowInternalFailure("Failed to get Mono Runtime Info!");
else
{
MelonLogger.Msg($"Engine Variant: {runtimeInfo.VariantName}");
MelonLogger.Msg($"Runtime Variant: {runtimeInfo.VariantName}");
MonoLoader.Startup(runtimeInfo);
}
}
Expand Down Expand Up @@ -158,7 +170,6 @@ internal static MonoRuntimeInfo GetMonoRuntimeInfo()
}
}


// Return Nothing
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
<ExcludeAssets>all</ExcludeAssets>
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
<ProjectReference Include="..\Unity.Shared\Unity.Shared.csproj">
<Private>False</Private>
<ExcludeAssets>all</ExcludeAssets>
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
</ItemGroup>
</Project>
Binary file not shown.
26 changes: 26 additions & 0 deletions MelonLoader/Modules/Unity/Unity.Shared/Unity.Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>MelonLoader.Unity</RootNamespace>
<TargetFrameworks>net35;netstandard2.1;net6</TargetFrameworks>
<LangVersion>Latest</LangVersion>
<OutputPath>$(SolutionDir)Output\$(Configuration)\MelonLoader\Modules\Unity\</OutputPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>embedded</DebugType>
<AssemblyName>MelonLoader.Unity.Shared</AssemblyName>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\classdata.tpk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\classdata.tpk" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AssetRipper.Primitives" Version="2.0.1" />
<PackageReference Include="AssetsTools.NET" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\MelonLoader.Shared\MelonLoader.Shared.csproj" Private="false" ExcludeAssets="runtime">
<CopyLocalSatelliteAssemblies>False</CopyLocalSatelliteAssemblies>
</ProjectReference>
</ItemGroup>
</Project>
Loading

0 comments on commit 0893734

Please sign in to comment.