using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ModFixerOnePreloader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ModFixerOnePreloader")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("dfae1e99-3b26-43c4-bd5c-40f86cb3a6d1")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.1.0")]
[module: UnverifiableCode]
namespace ModFixerOne;
public static class Injection
{
public static void AddFied(this TypeDefinition typeDefinition, string fieldName, TypeReference fieldType)
{
//IL_0003: Unknown result type (might be due to invalid IL or missing references)
//IL_0009: Expected O, but got Unknown
FieldDefinition val = new FieldDefinition(fieldName, (FieldAttributes)6, fieldType);
typeDefinition.Fields.Add(val);
}
public static MethodDefinition AddMethod(this TypeDefinition typeDefinition, string methodName, TypeReference returnType, TypeReference[] parmeterTypes)
{
//IL_0003: Unknown result type (might be due to invalid IL or missing references)
//IL_0009: Expected O, but got Unknown
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Expected O, but got Unknown
MethodDefinition val = new MethodDefinition(methodName, (MethodAttributes)6, returnType);
foreach (TypeReference val2 in parmeterTypes)
{
((MethodReference)val).Parameters.Add(new ParameterDefinition(val2));
}
typeDefinition.Methods.Add(val);
return val;
}
internal static void RefreshTraffic(AssemblyDefinition assembly)
{
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
ILProcessor iLProcessor = assembly.MainModule.GetType("PlanetTransport").AddMethod("RefreshTraffic", assembly.MainModule.TypeSystem.Void, (TypeReference[])(object)new TypeReference[1] { assembly.MainModule.TypeSystem.Int32 }).Body.GetILProcessor();
iLProcessor.Emit(OpCodes.Ldarg_0);
iLProcessor.Emit(OpCodes.Ldarg_1);
MethodDefinition val = ((IEnumerable<MethodDefinition>)((IEnumerable<TypeDefinition>)assembly.MainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "PlanetTransport").Methods).First((MethodDefinition m) => ((MemberReference)m).Name == "RefreshStationTraffic");
iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val);
iLProcessor.Emit(OpCodes.Ret);
}
internal static TypeDefinition Language(AssemblyDefinition assembly)
{
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Expected O, but got Unknown
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Expected O, but got Unknown
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_007d: Expected O, but got Unknown
//IL_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_008a: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a0: Expected O, but got Unknown
//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Expected O, but got Unknown
//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
//IL_00e6: Expected O, but got Unknown
//IL_012e: Unknown result type (might be due to invalid IL or missing references)
//IL_0134: Expected O, but got Unknown
//IL_0148: Unknown result type (might be due to invalid IL or missing references)
//IL_014e: Expected O, but got Unknown
//IL_015a: Unknown result type (might be due to invalid IL or missing references)
//IL_0165: Unknown result type (might be due to invalid IL or missing references)
TypeDefinition val = new TypeDefinition("", "Language", (TypeAttributes)257, assembly.MainModule.ImportReference(typeof(Enum)));
FieldAttributes val2 = (FieldAttributes)32854;
val.Fields.Add(new FieldDefinition("value__", (FieldAttributes)1542, assembly.MainModule.TypeSystem.Int32));
val.Fields.Add(new FieldDefinition("zhCN", val2, (TypeReference)(object)val)
{
Constant = 0
});
val.Fields.Add(new FieldDefinition("enUS", val2, (TypeReference)(object)val)
{
Constant = 1
});
val.Fields.Add(new FieldDefinition("frFR", val2, (TypeReference)(object)val)
{
Constant = 2
});
val.Fields.Add(new FieldDefinition("Max", val2, (TypeReference)(object)val)
{
Constant = 3
});
assembly.MainModule.Types.Add(val);
TypeDefinition obj = ((IEnumerable<TypeDefinition>)assembly.MainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "Localization");
FieldDefinition val3 = new FieldDefinition("lang", (FieldAttributes)22, (TypeReference)(object)val);
obj.Fields.Add(val3);
MethodDefinition val4 = new MethodDefinition("get_language", (MethodAttributes)22, (TypeReference)(object)val);
ILProcessor iLProcessor = val4.Body.GetILProcessor();
iLProcessor.Emit(OpCodes.Ldsfld, (FieldReference)(object)val3);
iLProcessor.Emit(OpCodes.Ret);
obj.Methods.Add(val4);
return val;
}
internal static void StringTranslate(AssemblyDefinition assembly, TypeDefinition enumType)
{
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Expected O, but got Unknown
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
//IL_004f: Expected O, but got Unknown
//IL_006b: Unknown result type (might be due to invalid IL or missing references)
//IL_0075: Expected O, but got Unknown
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
//IL_011d: Unknown result type (might be due to invalid IL or missing references)
//IL_0123: Expected O, but got Unknown
//IL_013f: Unknown result type (might be due to invalid IL or missing references)
//IL_0149: Expected O, but got Unknown
//IL_0156: Unknown result type (might be due to invalid IL or missing references)
//IL_0160: Expected O, but got Unknown
//IL_0178: Unknown result type (might be due to invalid IL or missing references)
//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
TypeDefinition val = new TypeDefinition("", "StringTranslate", (TypeAttributes)1, assembly.MainModule.TypeSystem.Object);
assembly.MainModule.Types.Add(val);
MethodDefinition val2 = new MethodDefinition("Translate", (MethodAttributes)22, assembly.MainModule.TypeSystem.String);
((MethodReference)val2).Parameters.Add(new ParameterDefinition("s", (ParameterAttributes)0, assembly.MainModule.TypeSystem.String));
val.Methods.Add(val2);
ILProcessor iLProcessor = val2.Body.GetILProcessor();
iLProcessor.Emit(OpCodes.Ldarg_0);
MethodDefinition val3 = ((IEnumerable<MethodDefinition>)((IEnumerable<TypeDefinition>)assembly.MainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "Localization").Methods).First((MethodDefinition m) => ((MemberReference)m).Name == "Translate");
iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val3);
iLProcessor.Emit(OpCodes.Ret);
val2 = new MethodDefinition("Translate", (MethodAttributes)22, assembly.MainModule.TypeSystem.String);
((MethodReference)val2).Parameters.Add(new ParameterDefinition("s", (ParameterAttributes)0, assembly.MainModule.TypeSystem.String));
((MethodReference)val2).Parameters.Add(new ParameterDefinition("_", (ParameterAttributes)0, (TypeReference)(object)enumType));
val.Methods.Add(val2);
ILProcessor iLProcessor2 = val2.Body.GetILProcessor();
iLProcessor2.Emit(OpCodes.Ldarg_0);
val3 = ((IEnumerable<MethodDefinition>)((IEnumerable<TypeDefinition>)assembly.MainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "Localization").Methods).First((MethodDefinition m) => ((MemberReference)m).Name == "Translate");
iLProcessor2.Emit(OpCodes.Call, (MethodReference)(object)val3);
iLProcessor2.Emit(OpCodes.Ret);
}
internal static void StringProto(AssemblyDefinition assembly)
{
//IL_004b: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Expected O, but got Unknown
//IL_0077: Unknown result type (might be due to invalid IL or missing references)
//IL_007d: Expected O, but got Unknown
//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
//IL_00a9: Expected O, but got Unknown
//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
TypeDefinition val = ((IEnumerable<TypeDefinition>)assembly.MainModule.Types).First((TypeDefinition t) => ((MemberReference)t).FullName == "Proto");
TypeDefinition val2 = new TypeDefinition("", "StringProto", (TypeAttributes)1, assembly.MainModule.TypeSystem.Object)
{
BaseType = (TypeReference)(object)val
};
CustomAttribute val3 = new CustomAttribute(assembly.MainModule.ImportReference((MethodBase)typeof(SerializableAttribute).GetConstructor(Type.EmptyTypes)));
val2.CustomAttributes.Add(val3);
MethodDefinition val4 = new MethodDefinition(".ctor", (MethodAttributes)6150, assembly.MainModule.TypeSystem.Void);
ILProcessor iLProcessor = val4.Body.GetILProcessor();
iLProcessor.Emit(OpCodes.Ldarg_0);
iLProcessor.Emit(OpCodes.Call, assembly.MainModule.ImportReference((MethodReference)(object)((IEnumerable<MethodDefinition>)val.Methods).First((MethodDefinition m) => ((MemberReference)m).Name == ".ctor")));
iLProcessor.Emit(OpCodes.Ret);
val2.Methods.Add(val4);
assembly.MainModule.Types.Add(val2);
val2.AddFied("ZHCN", assembly.MainModule.TypeSystem.String);
val2.AddFied("ENUS", assembly.MainModule.TypeSystem.String);
val2.AddFied("FRFR", assembly.MainModule.TypeSystem.String);
}
}
public static class Preloader
{
public static ManualLogSource logSource = Logger.CreateLogSource("ModFixerOne Preloader");
public static IEnumerable<string> Guids;
public static IEnumerable<string> TargetDLLs { get; } = new string[1] { "Assembly-CSharp.dll" };
public static void Patch(AssemblyDefinition assembly)
{
ModifyMainGame(assembly);
}
public static void Finish()
{
RemoveProcessFiler();
}
private static void ModifyMainGame(AssemblyDefinition assembly)
{
try
{
assembly.MainModule.GetType("UIGame").AddFied("inventory", (TypeReference)(object)assembly.MainModule.GetType("UIStorageGrid"));
logSource.LogDebug((object)"UIStorageGrid UIGame.inventory");
assembly.MainModule.GetType("StationComponent").AddFied("name", assembly.MainModule.TypeSystem.String);
logSource.LogDebug((object)"string StationComponent.name");
Injection.RefreshTraffic(assembly);
logSource.LogDebug((object)"void PlanetTransport.RefreshTraffic(int)");
TypeDefinition enumType = Injection.Language(assembly);
logSource.LogDebug((object)"enum Language { zhCN, enUS, frFR, Max }");
logSource.LogDebug((object)"public static Language Localization.get_language()");
Injection.StringTranslate(assembly, enumType);
logSource.LogDebug((object)"public static string StringTranslate.Translate(this string s)");
Injection.StringProto(assembly);
logSource.LogDebug((object)"public StringProto");
}
catch (Exception ex)
{
logSource.LogError((object)"Error when patching!");
logSource.LogError((object)ex);
}
}
private static void RemoveProcessFiler()
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Expected O, but got Unknown
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_005e: Expected O, but got Unknown
//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
//IL_00b2: Expected O, but got Unknown
try
{
Harmony val = new Harmony("ModFixerOnePreloader");
val.Patch((MethodBase)AccessTools.Method(typeof(TypeLoader), "FindPluginTypes", (Type[])null, (Type[])null).MakeGenericMethod(typeof(PluginInfo)), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Preloader), "PostFindPluginTypes", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
val.Patch((MethodBase)AccessTools.Method(typeof(Utility), "TopologicalSort", (Type[])null, (Type[])null).MakeGenericMethod(typeof(string)), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Preloader), "PostTopologicalSort", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
catch (Exception ex)
{
logSource.LogError((object)"Remove process filter & change load order fail!");
logSource.LogError((object)ex);
}
}
private static void PostFindPluginTypes(Dictionary<string, List<PluginInfo>> __result)
{
try
{
int num = 0;
foreach (List<PluginInfo> value in __result.Values)
{
foreach (PluginInfo item in value)
{
if (item.Processes.Any())
{
AccessTools.Property(typeof(PluginInfo), "Processes").SetValue(item, new List<BepInProcess>());
num++;
}
}
}
logSource.LogDebug((object)$"Remove process filter from {num} plugins.");
}
catch (Exception ex)
{
logSource.LogWarning((object)"Can't remove process filter!");
logSource.LogWarning((object)ex);
}
}
private static void PostTopologicalSort(ref IEnumerable<string> __result)
{
IEnumerable<string> enumerable = __result.Where((string item) => item != "starfi5h.plugin.ModFixerOne");
if (enumerable.Count() != __result.Count())
{
__result = new string[1] { "starfi5h.plugin.ModFixerOne" }.Concat(enumerable);
logSource.LogDebug((object)"Move ModFixerOne to first plugin to load.");
}
Guids = __result;
}
}