Skip to content

Commit

Permalink
option to revert settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Sep 21, 2023
1 parent b9fe2a1 commit 2602209
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Dependencies/Il2CppAssemblyGenerator/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static int Run()
Logger.Msg("Assembly Generation Needed!");

//if (!LaunchOptions.Il2CppAssemblyGenerator.OfflineMode)
RemoteAPI.Contact();
//RemoteAPI.Contact();

dumper = new Cpp2IL();
il2cppinterop = new Packages.Il2CppInterop();
Expand Down
7 changes: 0 additions & 7 deletions RedLoader/Attributes/HarmonyDontPatchAllAttribute.cs

This file was deleted.

2 changes: 1 addition & 1 deletion RedLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal static int Initialize()
bHapticsManager.Connect(BuildInfo.Name, UnityInformationHandler.GameName);

StatusWindow.StatusText = "Loading Plugins...";
MelonHandler.LoadMelonsFromDirectory<MelonPlugin>(LoaderEnvironment.PluginsDirectory);
MelonHandler.LoadMelonsFromDirectory<LoaderPlugin>(LoaderEnvironment.PluginsDirectory);
GlobalEvents.MelonHarmonyEarlyInit.Invoke();
GlobalEvents.OnPreInitialization.Invoke();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using RedLoader;

namespace Harmony;

Expand All @@ -28,6 +29,20 @@ public ConfiguredPatcher(HarmonyLib.Harmony harmony)
{
_harmony = harmony;
}

public void Patch(string methodName, bool shouldPatch = true)
{
if (!shouldPatch)
return;

var targetMethod = AccessTools.Method(typeof(T), methodName);
if (targetMethod == null)
throw new Exception($"Could not find method {methodName} in type {typeof(T).FullName}");

var isPrefix = targetMethod.Name.EndsWith("Prefix");
var sourceMethod = GetMethodFromAttribute(targetMethod);
_harmony.Patch(sourceMethod, isPrefix ? targetMethod.ToNewHarmonyMethod() : null, isPrefix ? null : targetMethod.ToNewHarmonyMethod());
}

public void Prefix<T2>(string sourceMethodName, string targetMethodName, bool shouldPatch = true, params Type[] parameters)
{
Expand Down Expand Up @@ -89,4 +104,30 @@ private HarmonyLib.HarmonyMethod GetTargetMethod(string methodName)
_harmonyMethods.Add(methodName, harmonyMethod);
return harmonyMethod;
}

private MethodBase GetMethodFromAttribute(MethodBase method)
{
var attr = method.GetCustomAttribute<HarmonyPatch>();
if (attr == null)
throw new Exception($"Method {method.Name} in type {typeof(T).FullName} is not a Harmony patch");
var info = attr.info;

MethodBase sourceMethod = null;

if (!info.methodType.HasValue)
sourceMethod = AccessTools.Method(info.declaringType, info.methodName, info.argumentTypes);
else if(info.methodType.Value == MethodType.Setter)
sourceMethod = AccessTools.PropertySetter(info.declaringType, info.methodName);
else if(info.methodType.Value == MethodType.Getter)
sourceMethod = AccessTools.PropertyGetter(info.declaringType, info.methodName);
else if(info.methodType.Value == MethodType.Constructor)
sourceMethod = AccessTools.Constructor(info.declaringType, info.argumentTypes);
else if(info.methodType.Value == MethodType.StaticConstructor)
sourceMethod = AccessTools.Constructor(info.declaringType, info.argumentTypes);

if (sourceMethod == null)
throw new MissingMethodException(info.declaringType.FullName, info.methodName);

return sourceMethod;
}
}
2 changes: 1 addition & 1 deletion RedLoader/LoaderUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static ModBase GetMelonFromAssembly(Assembly asm)
=>
asm == null
? null
: MelonPlugin.RegisteredMods.Cast<ModBase>()
: LoaderPlugin.RegisteredMods.Cast<ModBase>()
.FirstOrDefault(x => x.ModAssembly.Assembly == asm) ??
ModBase.RegisteredMelons.FirstOrDefault(x => x.ModAssembly.Assembly == asm);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace RedLoader
{
public abstract class MelonPlugin : ModTypeBase<MelonPlugin>
public abstract class LoaderPlugin : ModTypeBase<LoaderPlugin>
{
static MelonPlugin()
static LoaderPlugin()
{
TypeName = "Plugin";
}
Expand Down Expand Up @@ -35,7 +35,7 @@ protected private override bool RegisterInternal()
}
private void HarmonyInit()
{
if (!ModAssembly.HarmonyDontPatchAll)
if (HarmonyPatchAll)
HarmonyInstance.PatchAll(ModAssembly.Assembly);
}

Expand Down
6 changes: 1 addition & 5 deletions RedLoader/Melons/MelonAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ public static MelonAssembly LoadMelonAssembly(string path, Assembly assembly, bo

public readonly MelonEvent OnUnregister = new();

public bool HarmonyDontPatchAll { get; internal set; } = true;

/// <summary>
/// A SHA256 Hash of the Assembly.
/// </summary>
Expand Down Expand Up @@ -241,7 +239,7 @@ public void LoadMelons()
var info = LoaderUtils.PullAttributeFromAssembly<MelonInfoAttribute>(Assembly);
if (info != null && info.SystemType != null && info.SystemType.IsSubclassOf(typeof(ModBase)))
{
if (info.SystemType.IsSubclassOf(typeof(MelonPlugin)))
if (info.SystemType.IsSubclassOf(typeof(LoaderPlugin)))
{
ModBase mod;
try
Expand Down Expand Up @@ -269,7 +267,6 @@ public void LoadMelons()
var domainAttr = LoaderUtils.PullAttributeFromAssembly<MelonPlatformDomainAttribute>(Assembly);
var mlVersionAttr = LoaderUtils.PullAttributeFromAssembly<VerifyLoaderVersionAttribute>(Assembly);
var mlBuildAttr = LoaderUtils.PullAttributeFromAssembly<VerifyLoaderBuildAttribute>(Assembly);
var harmonyDPAAttr = LoaderUtils.PullAttributeFromAssembly<HarmonyDontPatchAllAttribute>(Assembly);

mod.Info = info;
mod.AdditionalCredits = additionalCreditsAttr;
Expand All @@ -287,7 +284,6 @@ public void LoadMelons()
//melon.OptionalDependencies = optionalDependenciesAttr.AssemblyNames;
//melon.OptionalDependencies = Array.Empty<string>();
mod.ID = idAttr?.ID;
HarmonyDontPatchAll = harmonyDPAAttr != null;

loadedMelons.Add(mod);

Expand Down
6 changes: 3 additions & 3 deletions RedLoader/Melons/MelonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static void LoadMelonsFromDirectory<T>(string path) where T : ModTypeBase

var count = ModTypeBase<T>._registeredMelons.Count;
RLog.Msg($"{count} {ModTypeBase<T>.TypeName.MakePlural(count)} loaded.");
if (firstSpacer || (typeof(T) == typeof(MelonMod)))
RLog.WriteSpacer();
// if (firstSpacer || (typeof(T) == typeof(MelonMod)))
// RLog.WriteSpacer();
firstSpacer = true;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public static void LoadModsFromDirectory(string path, string name)
asm.LoadMelons();
foreach (var m in asm.LoadedMelons)
{
if (m is not MelonPlugin && melons.All(x => x.ID != m.ID))
if (m is not LoaderPlugin && melons.All(x => x.ID != m.ID))
{
melons.Add(m);
}
Expand Down
72 changes: 0 additions & 72 deletions RedLoader/Melons/MelonMod.cs

This file was deleted.

7 changes: 6 additions & 1 deletion RedLoader/Melons/ModBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ private static void SortMelons<T>(ref List<T> melons) where T : ModBase
/// </summary>
protected bool NoUpdate;

/// <summary>
/// If true the loader will automatically apply all harmony patches in the assembly.
/// </summary>
protected bool HarmonyPatchAll;

/// <summary>
/// Name of the current Melon Type.
/// </summary>
Expand Down Expand Up @@ -370,7 +375,7 @@ public bool Register()

private void HarmonyInit()
{
if (!ModAssembly.HarmonyDontPatchAll)
if (HarmonyPatchAll)
HarmonyInstance.PatchAll(ModAssembly.Assembly);
}

Expand Down
14 changes: 2 additions & 12 deletions SonsGameManager/DebugCommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AdvancedTerrainGrass;
using Sons.Construction.GRABS;
using Sons.Lodding;
using Sons.PostProcessing;
using SonsSdk;
Expand Down Expand Up @@ -112,18 +113,7 @@ private void NoForestCommand(string args)
PathologicalGames.PoolManager.Pools["Bushes"].gameObject.SetActive(!isActive);
PathologicalGames.PoolManager.Pools["SmallTree"].gameObject.SetActive(!isActive);
}

/// <summary>
/// Equip the laser pointer that let's you use the procedural building system
/// </summary>
/// <param name="args"></param>
/// <command>laserpointer</command>
[DebugCommand("laserpointer")]
private void LaserPointerCommand(string args)
{
DebugConsole.Instance._equipitem("505");
}


/// <summary>
/// Toggles the shadow rendering (Shadows, Contact Shadows, Micro Shadowing)
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion SonsGameManager/GamePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void Init()
{
_patcher = new(Core.HarmonyInst);

_patcher.Prefix<SonsLaunch>("Start", nameof(LaunchStartPatch));
_patcher.Prefix<SonsLaunch>(nameof(SonsLaunch.Start), nameof(LaunchStartPatch));
_patcher.Prefix<SonsFMODEventEmitter>(nameof(SonsFMODEventEmitter.Play), nameof(SonsEmitterPlayPatch));
_patcher.Prefix<FMOD_StudioEventEmitter>(nameof(FMOD_StudioEventEmitter.StartEvent), nameof(FModEmitterPlayPatch));

Expand Down
Loading

0 comments on commit 2602209

Please sign in to comment.