Decompiled source of ThermalAlignFix v1.1.0

ThermalAlignFix.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.Json;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using GTFO.API.Utilities;
using GameData;
using Gear;
using HarmonyLib;
using MTFO.API;
using Microsoft.CodeAnalysis;
using ThermalAlignFix.CustomData;
using ThermalAlignFix.Dependencies;
using ThermalAlignFix.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ThermalAlignFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+03674ad1fae71b4554e3d48d7c22895d1393cd8d")]
[assembly: AssemblyProduct("ThermalAlignFix")]
[assembly: AssemblyTitle("ThermalAlignFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ThermalAlignFix
{
	internal static class DinoLogger
	{
		private static ManualLogSource logger = Logger.CreateLogSource("ThermalAlignFix");

		public static void Log(string format, params object[] args)
		{
			Log(string.Format(format, args));
		}

		public static void Log(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)8, (object)str);
			}
		}

		public static void Warning(string format, params object[] args)
		{
			Warning(string.Format(format, args));
		}

		public static void Warning(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)4, (object)str);
			}
		}

		public static void Error(string format, params object[] args)
		{
			Error(string.Format(format, args));
		}

		public static void Error(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)2, (object)str);
			}
		}

		public static void Debug(string format, params object[] args)
		{
			Debug(string.Format(format, args));
		}

		public static void Debug(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)32, (object)str);
			}
		}
	}
	[BepInPlugin("Dinorush.ThermalAlignFix", "ThermalAlignFix", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal sealed class EntryPoint : BasePlugin
	{
		public const string MODNAME = "ThermalAlignFix";

		public override void Load()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			ThermalShiftManager.Init();
			new Harmony("ThermalAlignFix").PatchAll();
			((BasePlugin)this).Log.LogMessage((object)"Loaded ThermalAlignFix");
		}
	}
	[HarmonyPatch(typeof(GearPartSpawner))]
	internal static class GearBuilderPatch
	{
		[HarmonyPatch("OnPartIsLoaded")]
		[HarmonyPostfix]
		private static void OnPartLoaded(eGearComponent type, GameObject part, GearPartGeneralData general)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			if ((int)type == 21 && ThermalShiftManager.TryGetData(general.Model, out ThermalShiftData data))
			{
				Transform val = part.transform.FindChild(data.ThermalChild);
				if ((Object)(object)val == (Object)null)
				{
					DinoLogger.Warning("Unable to find thermal child " + data.ThermalChild + " under model " + data.Model);
				}
				else
				{
					val.localPosition = data.LocalPosition;
				}
			}
		}
	}
}
namespace ThermalAlignFix.Json
{
	public static class JSON
	{
		private static readonly JsonSerializerOptions _setting = new JsonSerializerOptions
		{
			ReadCommentHandling = JsonCommentHandling.Skip,
			IncludeFields = true,
			PropertyNameCaseInsensitive = true,
			WriteIndented = true,
			IgnoreReadOnlyProperties = true
		};

		public static T? Deserialize<T>(string json)
		{
			return JsonSerializer.Deserialize<T>(json, _setting);
		}

		public static T? Deserialize<T>(ref Utf8JsonReader reader)
		{
			return JsonSerializer.Deserialize<T>(ref reader, _setting);
		}

		public static object? Deserialize(Type type, string json)
		{
			return JsonSerializer.Deserialize(json, type, _setting);
		}

		public static string Serialize<T>(T value)
		{
			return JsonSerializer.Serialize(value, _setting);
		}

		public static void Serialize<T>(Utf8JsonWriter writer, T value)
		{
			JsonSerializer.Serialize(writer, value, _setting);
		}

		public static void Serialize<T>(Utf8JsonWriter writer, string name, T value)
		{
			writer.WritePropertyName(name);
			JsonSerializer.Serialize(writer, value, _setting);
		}
	}
}
namespace ThermalAlignFix.Dependencies
{
	internal static class MTFOWrapper
	{
		public const string PLUGIN_GUID = "com.dak.MTFO";

		public static readonly bool HasMTFO;

		public static string GameDataPath
		{
			get
			{
				if (!HasMTFO)
				{
					return "";
				}
				return GameDataPath_Unsafe();
			}
		}

		public static string CustomPath
		{
			get
			{
				if (!HasMTFO)
				{
					return "";
				}
				return CustomPath_Unsafe();
			}
		}

		public static bool HasCustomContent
		{
			get
			{
				if (!HasMTFO)
				{
					return false;
				}
				return HasCustomContent_Unsafe();
			}
		}

		static MTFOWrapper()
		{
			HasMTFO = ((BaseChainloader<BasePlugin>)(object)IL2CPPChainloader.Instance).Plugins.ContainsKey("com.dak.MTFO");
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private static string GameDataPath_Unsafe()
		{
			return MTFOPathAPI.RundownPath;
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private static string CustomPath_Unsafe()
		{
			return MTFOPathAPI.CustomPath;
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private static bool HasCustomContent_Unsafe()
		{
			return MTFOPathAPI.HasRundownPath;
		}
	}
}
namespace ThermalAlignFix.CustomData
{
	public sealed class ThermalShiftData
	{
		public static readonly List<ThermalShiftData> VanillaData = new List<ThermalShiftData>
		{
			new ThermalShiftData
			{
				Model = "Assets/AssetPrefabs/Items/Gear/Parts/Sights/Sight_10_t.prefab",
				ThermalChild = "Sight_10_Thermal",
				LocalPosition = new Vector3(0f, 0.0397f, -0.03585f)
			},
			new ThermalShiftData
			{
				Model = "Assets/AssetPrefabs/Items/Gear/Parts/Sights/Sight_19_t.prefab",
				ThermalChild = "Sight_19_Thermal",
				LocalPosition = new Vector3(0f, -0.001f, -0.01387f)
			}
		};

		public string Model { get; set; } = string.Empty;


		public string ThermalChild { get; set; } = string.Empty;


		public Vector3 LocalPosition { get; set; } = Vector3.zero;

	}
	public sealed class ThermalShiftManager
	{
		public static readonly ThermalShiftManager Current;

		private readonly Dictionary<string, List<ThermalShiftData>> _fileToData = new Dictionary<string, List<ThermalShiftData>>();

		private readonly Dictionary<string, ThermalShiftData> _modelToData = new Dictionary<string, ThermalShiftData>();

		private void FileChanged(LiveEditEventArgs e)
		{
			LiveEditEventArgs e2 = e;
			DinoLogger.Warning("LiveEdit File Changed: " + e2.FileName);
			LiveEdit.TryReadFileContent(e2.FullPath, (Action<string>)delegate(string content)
			{
				ReadFileContent(e2.FullPath, content);
			});
		}

		private void FileDeleted(LiveEditEventArgs e)
		{
			DinoLogger.Warning("LiveEdit File Removed: " + e.FileName);
			RemoveFile(e.FullPath);
		}

		private void FileCreated(LiveEditEventArgs e)
		{
			LiveEditEventArgs e2 = e;
			DinoLogger.Warning("LiveEdit File Created: " + e2.FileName);
			LiveEdit.TryReadFileContent(e2.FullPath, (Action<string>)delegate(string content)
			{
				ReadFileContent(e2.FullPath, content);
			});
		}

		private void ReadFileContent(string file, string content)
		{
			RemoveFile(file);
			List<ThermalShiftData> list = null;
			try
			{
				list = JSON.Deserialize<List<ThermalShiftData>>(content);
			}
			catch (JsonException ex)
			{
				DinoLogger.Error("Error parsing settings json " + file);
				DinoLogger.Error(ex.Message);
			}
			if (list != null)
			{
				AddFile(file, list);
			}
		}

		private void RemoveFile(string file)
		{
			if (!_fileToData.Remove(file, out List<ThermalShiftData> value))
			{
				return;
			}
			foreach (ThermalShiftData item in value)
			{
				_modelToData.Remove(item.Model);
			}
		}

		public void AddFile(string file, List<ThermalShiftData> list)
		{
			if (!_fileToData.TryAdd(file, list))
			{
				return;
			}
			foreach (ThermalShiftData item in list)
			{
				_modelToData.Add(item.Model, item);
			}
		}

		private ThermalShiftManager()
		{
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Expected O, but got Unknown
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Expected O, but got Unknown
			if (!MTFOWrapper.HasCustomContent)
			{
				DinoLogger.Log("No custom path detected! Using vanilla data only.");
				{
					foreach (ThermalShiftData vanillaDatum in ThermalShiftData.VanillaData)
					{
						_modelToData.Add(vanillaDatum.Model, vanillaDatum);
					}
					return;
				}
			}
			string text = Path.Combine(MTFOWrapper.CustomPath, "ThermalAlignFix");
			if (!Directory.Exists(text))
			{
				DinoLogger.Log("No directory detected. Creating vanilla data.");
				Directory.CreateDirectory(text);
				StreamWriter streamWriter = File.CreateText(Path.Combine(text, "Vanilla.json"));
				streamWriter.WriteLine(JSON.Serialize(ThermalShiftData.VanillaData));
				streamWriter.Flush();
				streamWriter.Close();
			}
			else
			{
				DinoLogger.Log("Directory detected.");
			}
			foreach (string item in Directory.EnumerateFiles(text, "*.json", SearchOption.AllDirectories))
			{
				string content = File.ReadAllText(item);
				ReadFileContent(item, content);
			}
			LiveEditListener obj = LiveEdit.CreateListener(text, "*.json", true);
			obj.FileCreated += new LiveEditEventHandler(FileCreated);
			obj.FileChanged += new LiveEditEventHandler(FileChanged);
			obj.FileDeleted += new LiveEditEventHandler(FileDeleted);
		}

		static ThermalShiftManager()
		{
			Current = new ThermalShiftManager();
		}

		internal static void Init()
		{
		}

		public static bool TryGetData(string model, [MaybeNullWhen(false)] out ThermalShiftData data)
		{
			return Current._modelToData.TryGetValue(model, out data);
		}
	}
}