Decompiled source of AtlantumReactor Updated v4.0.11

AtlantumReactor.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
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 TechtonicaFramework.TechTree;
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+28c8d0d984ccd0015b323de318ee4ba46bfa72bf")]
[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.11")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[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.11";

		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_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_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: 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_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Expected O, but got Unknown
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: 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_01e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"PluginName: AtlantumReactor, VersionString: 4.0.11 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 = ModdedTabModule.ModdedCategory,
				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)262144,
				treePosition = 100
			}, 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 = "Modded",
				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.11 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 OnTechTreeStateLoaded()
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			Sprite val = LoadEmbeddedSprite("radiation.png");
			Unlock unlockByName = Unlocks.GetUnlockByName("Atlantum Reactor", false);
			if ((Object)(object)unlockByName != (Object)null)
			{
				if ((Object)(object)val != (Object)null)
				{
					unlockByName.sprite = val;
					Log.LogInfo((object)"Set Atlantum Reactor unlock sprite to nuclear/radiation icon");
				}
				else
				{
					ResourceInfo resourceInfoByName = Resources.GetResourceInfoByName("Crank Generator MKII", false);
					if ((Object)(object)resourceInfoByName != (Object)null && (Object)(object)resourceInfoByName.sprite != (Object)null && (Object)(object)unlockByName.sprite == (Object)null)
					{
						unlockByName.sprite = resourceInfoByName.sprite;
						Log.LogInfo((object)"Set Atlantum Reactor unlock sprite from Crank Generator MKII (fallback)");
					}
				}
				unlockByName.requiredTier = (ResearchTier)262144;
				unlockByName.treePosition = 100f;
				Log.LogInfo((object)"Configured Atlantum Reactor: Tier18, position=100");
			}
			else
			{
				Log.LogWarning((object)"Could not find unlock: Atlantum Reactor");
			}
			ResourceInfo resourceInfoByName2 = Resources.GetResourceInfoByName("Atlantum Reactor", false);
			if ((Object)(object)resourceInfoByName2 != (Object)null && (Object)(object)val != (Object)null)
			{
				FieldInfo field = typeof(ResourceInfo).GetField("rawSprite", BindingFlags.Instance | BindingFlags.Public);
				if (field != null)
				{
					field.SetValue(resourceInfoByName2, val);
					Log.LogInfo((object)"Applied nuclear icon to Atlantum Reactor resource");
				}
			}
		}

		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");
			}
		}

		public static void LogDebug(string message)
		{
			if (DebugMode != null && DebugMode.Value)
			{
				Log.LogInfo((object)("[DEBUG] " + message));
			}
		}

		public static Sprite LoadEmbeddedSprite(string resourceName, int width = 256, int height = 256)
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				string text = executingAssembly.GetManifestResourceNames().FirstOrDefault((string n) => n.EndsWith(resourceName));
				if (string.IsNullOrEmpty(text))
				{
					LogDebug("Embedded resource not found: " + resourceName);
					return null;
				}
				using Stream stream = executingAssembly.GetManifestResourceStream(text);
				if (stream == null)
				{
					return null;
				}
				byte[] array = new byte[stream.Length];
				stream.Read(array, 0, array.Length);
				Texture2D val = new Texture2D(width, height, (TextureFormat)4, false);
				((Texture)val).filterMode = (FilterMode)1;
				if (ImageConversion.LoadImage(val, array))
				{
					TintTexture(val, new Color(0.2f, 1f, 0.3f));
					val.Apply();
					Sprite result = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
					Log.LogInfo((object)$"Loaded embedded sprite: {resourceName} ({((Texture)val).width}x{((Texture)val).height}) - tinted green");
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Failed to load embedded sprite " + resourceName + ": " + ex.Message));
			}
			return null;
		}

		private static void TintTexture(Texture2D tex, Color tint)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			Color[] pixels = tex.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				float num = (pixels[i].r + pixels[i].g + pixels[i].b) / 3f;
				pixels[i] = new Color(tint.r * num, tint.g * num, tint.b * num, pixels[i].a);
			}
			tex.SetPixels(pixels);
		}
	}
	[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;
			}
		}
	}
}