Decompiled source of LocalTransportFix v1.0.1

LocalTransportFix.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("LocalTransportFix")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LocalTransportFix")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("753def7a-7dce-4f66-a048-68dac5c425b3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace nord.LocalTransportFix;

public abstract class ConfigManager
{
	public enum Step
	{
		AWAKE,
		GAME_MAIN_BEGIN,
		UNIVERSE_GEN_CREATE_GALAXY,
		STATE
	}

	private static ConfigManager instance;

	private static Dictionary<ConfigDefinition, string> orphanedEntries;

	public static ConfigFile Config { get; private set; }

	protected ConfigManager(ConfigFile config)
	{
		instance = this;
		Config = config;
		Config.SaveOnConfigSet = false;
	}

	public static void CheckConfig(Step step)
	{
		instance.CheckConfigImplements(step);
	}

	protected abstract void CheckConfigImplements(Step step);

	public static ConfigEntry<T> Bind<T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
	{
		return Config.Bind<T>(configDefinition, defaultValue, configDescription);
	}

	public static ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, ConfigDescription configDescription = null)
	{
		return Config.Bind<T>(section, key, defaultValue, configDescription);
	}

	public static ConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description)
	{
		return Config.Bind<T>(section, key, defaultValue, description);
	}

	public static ConfigEntry<T> GetEntry<T>(ConfigDefinition configDefinition)
	{
		try
		{
			return (ConfigEntry<T>)(object)Config[configDefinition];
		}
		catch (KeyNotFoundException ex)
		{
			LogManager.LogError($"{ex.GetType()}: configDefinition={configDefinition}");
			throw;
		}
	}

	public static ConfigEntry<T> GetEntry<T>(string section, string key)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		return GetEntry<T>(new ConfigDefinition(section, key));
	}

	public static T GetValue<T>(ConfigDefinition configDefinition)
	{
		return GetEntry<T>(configDefinition).Value;
	}

	public static T GetValue<T>(string section, string key)
	{
		return GetEntry<T>(section, key).Value;
	}

	public static bool ContainsKey(ConfigDefinition configDefinition)
	{
		return Config.ContainsKey(configDefinition);
	}

	public static bool ContainsKey(string section, string key)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		return Config.ContainsKey(new ConfigDefinition(section, key));
	}

	public static bool UpdateEntry<T>(string section, string key, T value) where T : IComparable
	{
		ConfigEntry<T> entry = GetEntry<T>(section, key);
		if (entry.Value.CompareTo(value) == 0)
		{
			return false;
		}
		entry.Value = value;
		return true;
	}

	public static bool RemoveEntry(ConfigDefinition key)
	{
		return Config.Remove(key);
	}

	public static Dictionary<ConfigDefinition, string> GetOrphanedEntries()
	{
		if (orphanedEntries == null)
		{
			orphanedEntries = Traverse.Create((object)Config).Property<Dictionary<ConfigDefinition, string>>("OrphanedEntries", (object[])null).Value;
		}
		return orphanedEntries;
	}

	public static void Migration<T>(string newSection, string newKey, T defaultValue, string oldSection, string oldKey)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		GetOrphanedEntries();
		ConfigDefinition key = new ConfigDefinition(oldSection, oldKey);
		if (orphanedEntries.TryGetValue(key, out var value))
		{
			((ConfigEntryBase)Bind(newSection, newKey, defaultValue)).SetSerializedValue(value);
			orphanedEntries.Remove(key);
			LogManager.LogInfo("migration " + oldSection + "." + oldKey + "(" + value + ") => " + newSection + "." + newKey);
		}
	}

	public static void Save(bool clearOrphanedEntries = false)
	{
		if (clearOrphanedEntries)
		{
			GetOrphanedEntries().Clear();
		}
		Config.Save();
		LogManager.LogInfo("save config.");
	}
}
public class LocalTransportFixConfig : ConfigManager
{
	public LocalTransportFixConfig(ConfigFile Config)
		: base(Config)
	{
	}

	protected override void CheckConfigImplements(Step step)
	{
		bool flag = false;
		if (step == Step.AWAKE)
		{
			ConfigManager.Bind("Base", "ModVersion", "1.0.0", "Don't change.").Value = "1.0.0";
			LocalTransportFixPlugin.TicksBetweenUpdate = ConfigManager.Bind("Setting", "TicksBetweenUpdate", LocalTransportFixPlugin.TicksBetweenUpdate, "The trigger frequency of the Logistics Drone in game ticks (vanilla setting is 10 ticks). Minimum allowed value is 1 tick.").Value;
			flag = true;
		}
		if (flag)
		{
			ConfigManager.Save();
		}
	}
}
[BepInPlugin("nord.LocalTransportFix", "LocalTransportFix", "1.0.0")]
public class LocalTransportFixPlugin : BaseUnityPlugin
{
	public const string ModGuid = "nord.LocalTransportFix";

	public const string ModName = "LocalTransportFix";

	public const string ModVersion = "1.0.0";

	public static int TicksBetweenUpdate = 3;

	private Harmony harmony;

	public void Awake()
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Expected O, but got Unknown
		LogManager.Logger = ((BaseUnityPlugin)this).Logger;
		new LocalTransportFixConfig(((BaseUnityPlugin)this).Config);
		ConfigManager.CheckConfig(ConfigManager.Step.AWAKE);
		harmony = new Harmony("nord.LocalTransportFix.Patch");
		harmony.PatchAll(typeof(LocalTransportFix));
	}

	public void OnDestroy()
	{
		harmony.UnpatchSelf();
	}
}
public class LocalTransportFix
{
	[HarmonyTranspiler]
	[HarmonyPatch(typeof(StationComponent), "InternalTickLocal")]
	public static IEnumerable<CodeInstruction> InternalTickLocal_Patch(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Expected O, but got Unknown
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Expected O, but got Unknown
		CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
		val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
		{
			new CodeMatch((OpCode?)OpCodes.Ldc_I4_S, (object)null, (string)null)
		});
		val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[2]
		{
			new CodeMatch((OpCode?)OpCodes.Ldarg_2, (object)null, (string)null),
			new CodeMatch((OpCode?)OpCodes.Ldc_I4_S, (object)null, (string)null)
		});
		val.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Ldc_I4_S, (object)LocalTransportFixPlugin.TicksBetweenUpdate));
		val.Advance(1).MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
		{
			new CodeMatch((OpCode?)OpCodes.Ldc_I4_S, (object)null, (string)null)
		});
		val.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Ldc_I4_S, (object)LocalTransportFixPlugin.TicksBetweenUpdate));
		LogManager.LogInfo($"Update tick set to {LocalTransportFixPlugin.TicksBetweenUpdate}.");
		return val.InstructionEnumeration();
	}
}
public static class LogManager
{
	public static ManualLogSource Logger { private get; set; }

	public static void LogInfo(object data)
	{
		Logger.LogInfo(data);
	}

	public static void LogInfo(MethodBase method)
	{
		Logger.LogInfo((object)(method.DeclaringType.Name + "." + method.Name));
	}

	public static void LogError(object data)
	{
		Logger.LogError(data);
	}

	public static void LogError(MethodBase method, object data)
	{
		Logger.LogError((object)(method.DeclaringType.Name + "." + method.Name + ": " + data));
	}
}