Skip to content

Commit

Permalink
Using Transpiler
Browse files Browse the repository at this point in the history
- Using transpiler is GigaChad
- Remove all Prefix and Postfix
- No change for user (if you find a change with this commit it's a bug)
  • Loading branch information
louis1706 committed Sep 8, 2022
1 parent 2b39bed commit e6b1b8d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 25 deletions.
65 changes: 47 additions & 18 deletions Patches/CommandIntercomTextFix.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
using CommandSystem;
using CommandSystem.Commands.RemoteAdmin.MutingAndIntercom;
using Exiled.API.Features;
using HarmonyLib;
using NorthwoodLib.Pools;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

using static HarmonyLib.AccessTools;

namespace FacilityManagement.Patches
{
[HarmonyPatch(typeof(IntercomTextCommand), nameof(IntercomTextCommand.Execute))]
public static class IntercomTextCommandFix
{
public static bool Prefix(ref bool __result, ArraySegment<string> arguments, ICommandSender sender, out string response)
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
if (!sender.CheckPermission(PlayerPermissions.Broadcasting, out response))
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Shared.Rent(instructions);

// Fix useless check from NW skillissue
const int offset = 2;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ret) + offset;

newInstructions.RemoveRange(index, 16);

newInstructions.Insert(index, new(OpCodes.Pop));

// Facility Management Fix

index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldloc_0);

// remove component.CustomContent = null;
newInstructions.RemoveRange(index, 4);

// Add FacilityManagement.Singleton.CustomText = null;
newInstructions.InsertRange(index, new CodeInstruction[]
{
__result = false;
return false;
}
new(OpCodes.Ldsfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton))),
new(OpCodes.Ldnull),
new(OpCodes.Stfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton.CustomText))),
});



string text = string.Join(" ", arguments);
if (string.IsNullOrEmpty(text.Trim()))
index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldloc_0);

// remove component.CustomContent = text;
newInstructions.RemoveRange(index, 4);

// Add FacilityManagement.Singleton.CustomText = text;
newInstructions.InsertRange(index, new CodeInstruction[]
{
ServerLogs.AddLog(ServerLogs.Modules.Administrative, sender.LogName + " cleared the intercom text.", ServerLogs.ServerLogType.RemoteAdminActivity_GameChanging, false);
response = "Reset intercom text.";
FacilityManagement.Singleton.CustomText = null;
__result = true;
return false;
}
ServerLogs.AddLog(ServerLogs.Modules.Administrative, sender.LogName + " set the intercom text to \"" + text + "\".", ServerLogs.ServerLogType.RemoteAdminActivity_GameChanging, false);
response = "Set intercom text to: " + text;
FacilityManagement.Singleton.CustomText = text;
__result = true;
return false;
new(OpCodes.Ldsfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton))),
new(OpCodes.Ldloc_0),
new(OpCodes.Stfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton.CustomText))),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Shared.Return(newInstructions);
}
}
}
46 changes: 43 additions & 3 deletions Patches/IntercomDisplayTextFix.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
using HarmonyLib;
using Exiled.Events.EventArgs;
using HarmonyLib;
using LightContainmentZoneDecontamination;
using NorthwoodLib.Pools;
using System.Collections.Generic;
using System.Reflection.Emit;
using static HarmonyLib.AccessTools;

namespace FacilityManagement.Patches
{
#pragma warning disable IDE0060 // Supprimer le paramètre inutilisé

[HarmonyPatch(typeof(Exiled.API.Features.Intercom), nameof(Exiled.API.Features.Intercom.DisplayText), MethodType.Setter)]
public static class CommandIntercomTextSetterFix
{
public static void Prefix(ref string value) => FacilityManagement.Singleton.CustomText = value;
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Shared.Rent();

newInstructions.AddRange(new CodeInstruction[]
{
new(OpCodes.Ldsfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton))),
new(OpCodes.Ldarg_0),
new(OpCodes.Ldind_Ref),
new(OpCodes.Stfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton.CustomText))),
new(OpCodes.Ret),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Shared.Return(newInstructions);
}
}

[HarmonyPatch(typeof(Exiled.API.Features.Intercom), nameof(Exiled.API.Features.Intercom.DisplayText), MethodType.Getter)]
public static class CommandIntercomTextGetterFix
{
public static void Prefix(ref string __result) => __result = FacilityManagement.Singleton.CustomText;
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Shared.Rent(instructions);

newInstructions.AddRange(new CodeInstruction[]
{
new(OpCodes.Ldsfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton))),
new(OpCodes.Ldfld, Field(typeof(FacilityManagement),nameof(FacilityManagement.Singleton.CustomText))),
new(OpCodes.Ret),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Shared.Return(newInstructions);
}
}
}
32 changes: 30 additions & 2 deletions Patches/IntercomPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
using System;
using Exiled.API.Features;
using UnityEngine;
using NorthwoodLib.Pools;
using System.Collections.Generic;
using System.Reflection.Emit;
using static HarmonyLib.AccessTools;
using Mono.Cecil.Cil;

namespace FacilityManagement.Patches
{
Expand All @@ -10,10 +15,33 @@ public class IntercomUpdateTextPatch
[HarmonyPatch(typeof(Intercom), nameof(Intercom.IntercomState), MethodType.Setter)]
public static class CommandIntercomTextSetterFix
{
public static bool Prefix(Intercom __instance, ref Intercom.State value)
public static void Postfix(Intercom __instance, ref Intercom.State value)
{
__instance.Network_state = SetContent(__instance, value);
return false;
return;
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
Label returnLabel = generator.DefineLabel();

List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Shared.Rent();

newInstructions.AddRange(new CodeInstruction[]
{
new(OpCodes.Nop),
new(OpCodes.Ldarg_0),
new(OpCodes.Ldarg_0),
new(OpCodes.Ldarg_1),
new(OpCodes.Ldind_U1),
new(OpCodes.Call, Method(typeof(IntercomUpdateTextPatch), nameof(SetContent))),
new(OpCodes.Callvirt, PropertySetter(typeof(Intercom), nameof(Intercom.Network_state))), // Set string
new CodeInstruction(OpCodes.Ret),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Shared.Return(newInstructions);
}
}
internal static Intercom.State SetContent(Intercom intercom, Intercom.State state)
Expand Down
25 changes: 23 additions & 2 deletions Patches/NameFormaterPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@
using Exiled.API.Features;
using System.Collections.Generic;
using UnityEngine;
using Exiled.Events.EventArgs;
using LightContainmentZoneDecontamination;
using NorthwoodLib.Pools;
using System.Reflection.Emit;
using static HarmonyLib.AccessTools;

namespace FacilityManagement.Patches
{
[HarmonyPatch(typeof(Intercom), nameof(Intercom.Start))]
public class NameFormaterPatch
{
public static void Postfix(Intercom __instance)
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Shared.Rent(instructions);

newInstructions.InsertRange(0, new CodeInstruction[]
{
new(OpCodes.Nop),
new(OpCodes.Call, Method(typeof(NameFormaterPatch),nameof(AddCustomInterpolatedCommand))),
new(OpCodes.Nop),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Shared.Return(newInstructions);
}
public static void AddCustomInterpolatedCommand()
{
try
{
Expand Down Expand Up @@ -43,7 +64,7 @@ public static void Postfix(Intercom __instance)
}
catch (Exception ex)
{
Log.Error($"Intercom::Start Postfix : {ex}\n {ex.StackTrace}");
Log.Error($"AddCustomInterpolatedCommand : {ex}\n {ex.StackTrace}");
}
}
}
Expand Down

0 comments on commit e6b1b8d

Please sign in to comment.