Decompiled source of AtlantumReactor Updated v4.0.9

AtlantumReactor.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EquinoxsModUtils;
using EquinoxsModUtils.Additions;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("AtlantumReactor")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AtlantumReactor")]
[assembly: AssemblyTitle("AtlantumReactor")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AtlantumReactor
{
	[BepInPlugin("com.certifired.AtlantumReactor", "AtlantumReactor", "4.0.9")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class AtlantumReactorPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.certifired.AtlantumReactor";

		private const string PluginName = "AtlantumReactor";

		private const string VersionString = "4.0.9";

		private static readonly Harmony Harmony = new Harmony("com.certifired.AtlantumReactor");

		public static ManualLogSource Log;

		public const string ReactorName = "Atlantum Reactor";

		public const string AtlantumBrickName = "Atlantum Mixture Brick";

		public const string CoolantName = "Shiverthorn Coolant";

		public static ConfigEntry<int> PowerOutputKW;

		public static ConfigEntry<bool> DebugMode;

		public static int reactorDefinitionId = -1;

		private void Awake()
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Expected O, but got Unknown
			//IL_0118: 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_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: 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)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Expected O, but got Unknown
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0248: Unknown result type (might be due to invalid IL or missing references)
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_0269: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"PluginName: AtlantumReactor, VersionString: 4.0.9 is loading...");
			PowerOutputKW = ((BaseUnityPlugin)this).Config.Bind<int>("Power", "Power Output (kW)", 20000, new ConfigDescription("Power output in kW (20000 = 20MW)", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1000, 100000), Array.Empty<object>()));
			DebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug Mode", false, "Enable verbose debug logging");
			Harmony.PatchAll();
			EMUAdditions.AddNewUnlock(new NewUnlockDetails
			{
				category = (TechCategory)4,
				coreTypeNeeded = (CoreType)2,
				coreCountNeeded = 1000,
				description = string.Format("Consumes {0} and {1} to produce {2}MW of power.", "Atlantum Mixture Brick", "Shiverthorn Coolant", PowerOutputKW.Value / 1000),
				displayName = "Atlantum Reactor",
				requiredTier = (ResearchTier)2,
				treePosition = 0
			}, false);
			PowerGeneratorDefinition val = ScriptableObject.CreateInstance<PowerGeneratorDefinition>();
			val.usesFuel = true;
			val.isCrankDriven = false;
			val.powerDuration = 0f;
			val.torqueDemand = 0;
			EMUAdditions.AddNewMachine<PowerGeneratorInstance, PowerGeneratorDefinition>((MachineDefinition<PowerGeneratorInstance, PowerGeneratorDefinition>)(object)val, new NewResourceDetails
			{
				name = "Atlantum Reactor",
				description = string.Format("Advanced nuclear reactor that consumes {0} and {1} to produce {2}MW of power. High-output endgame power solution.", "Atlantum Mixture Brick", "Shiverthorn Coolant", PowerOutputKW.Value / 1000),
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				headerTitle = "Production",
				maxStackCount = 1,
				sortPriority = 999,
				unlockName = "Atlantum Reactor",
				parentName = "Crank Generator MKII"
			}, false);
			EMUAdditions.AddNewRecipe(new NewRecipeDetails
			{
				GUID = "com.certifired.AtlantumReactor",
				craftingMethod = (CraftingMethod)0,
				craftTierRequired = 0,
				duration = 30f,
				unlockName = "Atlantum Reactor",
				ingredients = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Crank Generator MKII", 5),
					new RecipeResourceInfo("Steel Frame", 20),
					new RecipeResourceInfo("Processor Unit", 10),
					new RecipeResourceInfo("Cooling System", 10),
					new RecipeResourceInfo("Atlantum Mixture Brick", 5)
				},
				outputs = new List<RecipeResourceInfo>
				{
					new RecipeResourceInfo("Atlantum Reactor", 1)
				},
				sortPriority = 999
			}, false);
			Events.GameDefinesLoaded += OnGameDefinesLoaded;
			Events.TechTreeStateLoaded += OnTechTreeStateLoaded;
			Log.LogInfo((object)"PluginName: AtlantumReactor, VersionString: 4.0.9 is loaded.");
		}

		private void OnGameDefinesLoaded()
		{
			ResourceInfo resourceInfoByName = Resources.GetResourceInfoByName("Atlantum Reactor", false);
			if ((Object)(object)resourceInfoByName != (Object)null)
			{
				resourceInfoByName.unlock = Unlocks.GetUnlockByName("Atlantum Reactor", false);
				reactorDefinitionId = ((UniqueIdScriptableObject)resourceInfoByName).uniqueId;
				LogDebug($"Reactor registered with ID: {reactorDefinitionId}");
			}
			RegisterFuelResources();
		}

		private void RegisterFuelResources()
		{
			ResourceInfo resourceInfoByName = Resources.GetResourceInfoByName("Atlantum Mixture Brick", false);
			if ((Object)(object)resourceInfoByName != (Object)null)
			{
				if (resourceInfoByName.fuelAmount <= 0f)
				{
					resourceInfoByName.fuelAmount = 200f;
				}
				LogDebug(string.Format("Registered {0} as fuel with amount {1}", "Atlantum Mixture Brick", resourceInfoByName.fuelAmount));
			}
			else
			{
				Log.LogWarning((object)"Could not find Atlantum Mixture Brick to register as fuel");
			}
			ResourceInfo resourceInfoByName2 = Resources.GetResourceInfoByName("Shiverthorn Coolant", false);
			if ((Object)(object)resourceInfoByName2 != (Object)null)
			{
				if (resourceInfoByName2.fuelAmount <= 0f)
				{
					resourceInfoByName2.fuelAmount = 100f;
				}
				LogDebug(string.Format("Registered {0} as fuel with amount {1}", "Shiverthorn Coolant", resourceInfoByName2.fuelAmount));
			}
			else
			{
				Log.LogWarning((object)"Could not find Shiverthorn Coolant to register as fuel");
			}
		}

		private void OnTechTreeStateLoaded()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			Unlock unlockByName = Unlocks.GetUnlockByName("HVC Reach IV", false);
			Unlock unlockByName2 = Unlocks.GetUnlockByName("Atlantum Reactor", false);
			if ((Object)(object)unlockByName2 != (Object)null && (Object)(object)unlockByName != (Object)null)
			{
				unlockByName2.requiredTier = unlockByName.requiredTier;
				unlockByName2.treePosition = unlockByName.treePosition;
				LogDebug("Positioned reactor unlock after HVC Reach IV");
			}
		}

		public static void LogDebug(string message)
		{
			if (DebugMode != null && DebugMode.Value)
			{
				Log.LogInfo((object)("[DEBUG] " + message));
			}
		}
	}
	[HarmonyPatch]
	internal static class ReactorPowerPatches
	{
		[HarmonyPatch(typeof(PowerGeneratorInstance), "SimUpdate")]
		[HarmonyPostfix]
		private static void EnsureReactorPowerGeneration(ref PowerGeneratorInstance __instance)
		{
			try
			{
				if (!((Object)(object)((PowerGeneratorInstance)(ref __instance)).myDef == (Object)null) && !(((ResourceInfo)((PowerGeneratorInstance)(ref __instance)).myDef).displayName != "Atlantum Reactor"))
				{
					ref PowerInfo powerInfo = ref __instance.powerInfo;
					powerInfo.curPowerConsumption = -AtlantumReactorPlugin.PowerOutputKW.Value;
					powerInfo.isGenerator = true;
					AtlantumReactorPlugin.LogDebug($"Reactor generating {AtlantumReactorPlugin.PowerOutputKW.Value}kW");
				}
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch]
	internal static class ReactorVisualPatches
	{
		private static readonly Color ReactorGreen = new Color(0.2f, 1f, 0.3f);

		private static readonly Color EmissionGreen = new Color(0.1f, 0.8f, 0.2f);

		private static readonly HashSet<uint> tintedReactors = new HashSet<uint>();

		[HarmonyPatch(typeof(PowerGeneratorInstance), "SimUpdate")]
		[HarmonyPostfix]
		private static void ApplyGreenTintOnUpdate(ref PowerGeneratorInstance __instance)
		{
			try
			{
				if ((Object)(object)((PowerGeneratorInstance)(ref __instance)).myDef == (Object)null || ((ResourceInfo)((PowerGeneratorInstance)(ref __instance)).myDef).displayName != "Atlantum Reactor")
				{
					return;
				}
				uint instanceId = __instance.commonInfo.instanceId;
				if (!tintedReactors.Contains(instanceId))
				{
					GameObject refGameObj = __instance.commonInfo.refGameObj;
					if (!((Object)(object)refGameObj == (Object)null))
					{
						ApplyGreenMaterial(refGameObj);
						tintedReactors.Add(instanceId);
						AtlantumReactorPlugin.LogDebug($"Applied green tint to reactor {instanceId}");
					}
				}
			}
			catch
			{
			}
		}

		[HarmonyPatch(typeof(GridManager), "RemoveObj", new Type[] { typeof(GenericMachineInstanceRef) })]
		[HarmonyPrefix]
		private static void ClearTintedReactor(GenericMachineInstanceRef machineRef)
		{
			if (((GenericMachineInstanceRef)(ref machineRef)).IsValid())
			{
				tintedReactors.Remove(((GenericMachineInstanceRef)(ref machineRef)).instanceId);
			}
		}

		private static void ApplyGreenMaterial(GameObject visual)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: 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: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			MeshRenderer[] componentsInChildren = visual.GetComponentsInChildren<MeshRenderer>(true);
			MeshRenderer[] array = componentsInChildren;
			foreach (MeshRenderer val in array)
			{
				MaterialPropertyBlock val2 = new MaterialPropertyBlock();
				((Renderer)val).GetPropertyBlock(val2);
				val2.SetColor("_Color", ReactorGreen);
				val2.SetColor("_BaseColor", ReactorGreen);
				val2.SetColor("_EmissionColor", EmissionGreen * 3f);
				((Renderer)val).SetPropertyBlock(val2);
			}
			MeshRenderer[] array2 = componentsInChildren;
			foreach (MeshRenderer val3 in array2)
			{
				Material[] materials = ((Renderer)val3).materials;
				foreach (Material val4 in materials)
				{
					if (val4.HasProperty("_Color"))
					{
						val4.color = ReactorGreen;
					}
					if (val4.HasProperty("_BaseColor"))
					{
						val4.SetColor("_BaseColor", ReactorGreen);
					}
					if (val4.HasProperty("_EmissionColor"))
					{
						val4.EnableKeyword("_EMISSION");
						val4.SetColor("_EmissionColor", EmissionGreen * 3f);
					}
				}
				((Renderer)val3).materials = materials;
			}
		}
	}
}