Skip to content

Commit

Permalink
Fixed several MelonLaunchOptions related issues
Browse files Browse the repository at this point in the history
Implemented `Peek` Method for LemonEnumerator
Fixed an issue with MelonLaunchOptions failing to parse arguments if an `=` sign is used instead of a space
Fixed an issue with MelonLaunchOptions failing to parse options if a `-` prefix is used instead of `--`
Fixed an issue with Command Line Arguments not being logged
  • Loading branch information
HerpDerpinstine committed Sep 4, 2024
1 parent 95a8f70 commit 498e1eb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 52 deletions.
35 changes: 24 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,30 @@
4. Updated AsmResolver to 6.0.0-beta.1
5. Updated AssetRipper.VersionUtilities to 1.5.0
6. Updated AssetsTools.NET to 3.0.0
7. Implemented `--cpp2il.callanalyzer` launch option to enable Cpp2IL's CallAnalyzer processor
8. Implemented `--cpp2il.nativemethoddetector` launch option to enable Cpp2IL's NativeMethodDetector processor
9. Implemented several fixes for Il2CppInterop related issues
10. Implemented Cpp2IL's StrippedCodeRegSupport plugin
11. Fixed an accidental regression with LemonSHA256
12. Fixed an issue with Native logs using the wrong Colors
13. Fixed an issue with Preload module not replacing Mono libraries on Older Mono Games
14. Fixed an issue with MonoMod DetourContext Disposal not working properly
15. Fixed an issue with Debugger Launch Option causing crashes
16. Fixed an issue with Console not having the Game Name and Version in the title
17. Fixed an issue with Sharing Violation during Log Initialization
7. Updated UnityEngine.Il2CppAssetBundleManager for latest compatibility
8. Updated UnityEngine.Il2CppImageConversionManager for latest compatibility
9. Implemented `--cpp2il.callanalyzer` launch option to enable Cpp2IL's CallAnalyzer processor
10. Implemented `--cpp2il.nativemethoddetector` launch option to enable Cpp2IL's NativeMethodDetector processor
11. Implemented several fixes for Il2CppInterop related issues
12. Implemented Cpp2IL's StrippedCodeRegSupport plugin
13. Implemented `ExternalArguments` Dictionary for MelonLaunchOptions (Credits to [HAHOOS](https://github.com/HAHOOS) :P)
14. Implemented `Peek` Method for LemonEnumerator
15. Fixed an accidental regression with LemonSHA256
16. Fixed an issue with Native logs using the wrong Colors
17. Fixed an issue with Preload module not replacing Mono libraries on Older Mono Games
18. Fixed an issue with MonoMod DetourContext Disposal not working properly
19. Fixed an issue with Debugger Launch Option causing crashes
20. Fixed an issue with Console not having the Game Name and Version in the title
21. Fixed an issue with Sharing Violation during Log Initialization
22. Fixed an issue with `--melonloader.basedir` launch option always expecting an `=` sign before the path
23. Fixed an issue with Il2CppInteropFixes not being properly error handled
24. Fixed an issue with Il2CppInteropFixes using `il2cpp_type_get_class_or_element_class` instead of `il2cpp_class_from_type`
25. Fixed an issue with EOS Support Module not being properly error handled
26. Fixed an issue with NativeStackWalk not unregistering addresses
27. Fixed an issue with Errors being Spammed if `UnityEngine.Transform::SetAsLastSibling` fails to resolve
28. Fixed an issue with MelonLaunchOptions failing to parse arguments if an `=` sign is used instead of a space
29. Fixed an issue with MelonLaunchOptions failing to parse options if a `-` prefix is used instead of `--`
30. Fixed an issue with Command Line Arguments not being logged

---

Expand Down
4 changes: 4 additions & 0 deletions MelonLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using bHapticsLib;
using System.Threading;
using System.Linq;
#pragma warning disable IDE0051 // Prevent the IDE from complaining about private unreferenced methods

namespace MelonLoader
Expand Down Expand Up @@ -172,6 +173,9 @@ internal static void WelcomeMessage()
var archString = MelonUtils.IsGame32Bit() ? "x86" : "x64";
MelonLogger.MsgDirect($"Game Arch: {archString}");
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect($"CommandLine: {string.Join(" ", MelonLaunchOptions.CommandLineArgs)}");
MelonLogger.MsgDirect("------------------------------");


MelonEnvironment.PrintEnvironment();
}
Expand Down
14 changes: 14 additions & 0 deletions MelonLoader/Lemons/LemonEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public LemonEnumerator(IList<T> lemons)
object IEnumerator.Current => Current;
public T Current { get; private set; }

public bool Peek(out T next)
{
if ((LemonPatch == null)
|| (LemonPatch.Length <= 0)
|| (NextLemon >= LemonPatch.Length))
{
next = Current;
return false;
}

next = LemonPatch[NextLemon];
return true;
}

bool IEnumerator.MoveNext() => MoveNext();
public bool MoveNext()
{
Expand Down
95 changes: 54 additions & 41 deletions MelonLoader/MelonLaunchOptions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace MelonLoader
{
public static class MelonLaunchOptions
{
private static Dictionary<string, Action> WithoutArg = new Dictionary<string, Action>();
private static Dictionary<string, Action<string>> WithArg = new Dictionary<string, Action<string>>();
private static string[] _cmd;

/// <summary>
/// Dictionary of all Arguments with value (if found) that were not used by MelonLoader
Expand All @@ -17,6 +17,19 @@ public static class MelonLaunchOptions
/// </summary>
public static Dictionary<string, string> ExternalArguments { get; private set; } = new Dictionary<string, string>();

/// <summary>
/// Array of All Command Line Arguments
/// </summary>
public static string[] CommandLineArgs
{
get
{
if (_cmd == null)
_cmd = Environment.GetCommandLineArgs();
return _cmd;
}
}

static MelonLaunchOptions()
{
Core.Setup();
Expand All @@ -28,61 +41,61 @@ static MelonLaunchOptions()

internal static void Load()
{
List<string> foundOptions = new List<string>();
LemonEnumerator<string> argEnumerator = new LemonEnumerator<string>(Environment.GetCommandLineArgs());
LemonEnumerator<string> argEnumerator = new LemonEnumerator<string>(CommandLineArgs);
while (argEnumerator.MoveNext())
{
string fullcmd = argEnumerator.Current;
if (string.IsNullOrEmpty(fullcmd))
continue;

if (!fullcmd.StartsWith("--"))
// Parse Prefix
string noPrefixCmd = fullcmd;
if (noPrefixCmd.StartsWith("--"))
noPrefixCmd = noPrefixCmd.Remove(0, 2);
else if (noPrefixCmd.StartsWith("-"))
noPrefixCmd = noPrefixCmd.Remove(0, 1);
else
{
// Unknown Command, Add it to Dictionary
ExternalArguments.Add(noPrefixCmd, null);
continue;
}

string cmd = fullcmd.Remove(0, 2);

if (WithoutArg.TryGetValue(cmd, out Action withoutArgFunc))
// Parse Argumentless Commands
if (WithoutArg.TryGetValue(noPrefixCmd, out Action withoutArgFunc))
{
foundOptions.Add(fullcmd);
withoutArgFunc();
continue;
}
else if (WithArg.TryGetValue(cmd, out Action<string> withArgFunc))
{
if (!argEnumerator.MoveNext())
continue;

string cmdArg = argEnumerator.Current;
if (string.IsNullOrEmpty(cmdArg))
continue;

if (cmdArg.StartsWith("--"))
continue;

foundOptions.Add($"{fullcmd} = {cmdArg}");
withArgFunc(cmdArg);
// Parse Argument
string cmdArg = null;
if (noPrefixCmd.Contains("="))
{
string[] split = noPrefixCmd.Split('=');
noPrefixCmd = split[0];
cmdArg = split[1];
}
if ((string.IsNullOrEmpty(cmdArg)
&& !argEnumerator.Peek(out cmdArg))
|| string.IsNullOrEmpty(cmdArg)
|| !cmdArg.StartsWith("--")
|| !cmdArg.StartsWith("-"))
{
// Unknown Command, Add it to Dictionary
ExternalArguments.Add(noPrefixCmd, null);
continue;
}
if (foundOptions.Where(x => x.StartsWith(fullcmd)).Count() <= 0)

// Parse Argument Commands
if (WithArg.TryGetValue(noPrefixCmd, out Action<string> withArgFunc))
{
if (!argEnumerator.MoveNext())
{
ExternalArguments.Add(cmd, null);
continue;
}

string cmdArg = argEnumerator.Current;
if (string.IsNullOrEmpty(cmdArg))
{
ExternalArguments.Add(cmd, null);
continue;
}

if (cmdArg.StartsWith("--"))
{
ExternalArguments.Add(cmd, null);
continue;
}
ExternalArguments.Add(cmd, cmdArg);
withArgFunc(cmdArg);
continue;
}

// Unknown Command with Argument, Add it to Dictionary
ExternalArguments.Add(noPrefixCmd, cmdArg);
}
}

Expand Down

0 comments on commit 498e1eb

Please sign in to comment.