Decompiled source of PersistentShipObjects v0.2.1

PersistentShipObjects.dll

Decompiled 10 months 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 BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Newtonsoft.Json;
using PersistentShipObjects.Patches;
using Unity.Netcode;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PersistentShipObjects")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PersistentShipObjects")]
[assembly: AssemblyTitle("PersistentShipObjects")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PersistentShipObjects
{
	[BepInPlugin("VivianGreen.PersistentShipObjects", "PersistentShipObjects", "0.2.0")]
	public class PersistentShipObjects : BaseUnityPlugin
	{
		private const string GUID = "VivianGreen.PersistentShipObjects";

		private const string NAME = "PersistentShipObjects";

		private const string VERSION = "0.2.0";

		private readonly Harmony harmony = new Harmony("VivianGreen.PersistentShipObjects");

		public static PersistentShipObjects instance;

		public static string ObjTransformsPath = Application.persistentDataPath + "/PersistentShipObjects/Objects.json";

		public static string ObjTransformsFolderPath = Application.persistentDataPath + "/PersistentShipObjects";

		public static ConfigEntry<bool> doDebugPrints;

		public static List<TransformObject> TransformObjectsManager;

		private const int A_CONCERNING_AMOUNT_OF_NESTING = 30;

		private const string TAB = "|   ";

		public static ManualLogSource MLS { get; private set; }

		public void Awake()
		{
			Debug.Log((object)ObjTransformsPath);
			doDebugPrints = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "doDebugPrints", false, "should PersistentShipObjects paint the console yellow?");
			if (!File.Exists(ObjTransformsFolderPath))
			{
				Directory.CreateDirectory(ObjTransformsFolderPath);
			}
			if (!File.Exists(ObjTransformsPath))
			{
				File.Create(ObjTransformsPath).Dispose();
			}
			TransformObjectsManager = ReadJSON();
			Harmony.CreateAndPatchAll(typeof(ShipBuildModeManagerPatch), (string)null);
		}

		public static void SaveObjTransforms()
		{
			string text = JsonConvert.SerializeObject((object)TransformObjectsManager);
			Debug.Log((object)"JSON: ");
			Debug.Log((object)text);
			File.WriteAllText(ObjTransformsPath, text);
		}

		public static TransformObject FindObjectIfExists(int unlockableID)
		{
			return TransformObjectsManager.FirstOrDefault((TransformObject obj) => obj.unlockableID == unlockableID);
		}

		public static void UpdateObjectManager(TransformObject newObj)
		{
			if (newObj != null)
			{
				TransformObject transformObject = TransformObjectsManager.FirstOrDefault((TransformObject thisObj) => thisObj.unlockableID == newObj.unlockableID);
				if (transformObject != null)
				{
					TransformObjectsManager.Remove(transformObject);
				}
				TransformObjectsManager.Add(newObj);
				SaveObjTransforms();
			}
		}

		public static List<TransformObject> ReadJSON()
		{
			Console.WriteLine("entering ReadJSON()");
			try
			{
				string text = File.ReadAllText(ObjTransformsPath) ?? null;
				if (text != null)
				{
					Console.WriteLine("    json:\n" + text);
					List<TransformObject> list = JsonConvert.DeserializeObject<List<TransformObject>>(text);
					Console.WriteLine("    length of objs: " + list.Count);
					foreach (TransformObject item in list)
					{
						Console.WriteLine("        object found!");
						Console.WriteLine("        named: " + item.unlockableName);
					}
					return list;
				}
			}
			catch (Exception ex)
			{
				Debug.Log((object)ex);
			}
			return new List<TransformObject>();
		}

		public static void DebugLog(object logMessage, char logType = 'i')
		{
			ConfigEntry<bool> obj = doDebugPrints;
			if (obj != null && !obj.Value)
			{
				return;
			}
			if (logMessage == null)
			{
				Console.WriteLine("Error: logMessage is null");
				return;
			}
			string text = logMessage.ToString();
			if (text == null)
			{
				Console.WriteLine("Error: LogString is null");
				return;
			}
			Console.BackgroundColor = ConsoleColor.DarkGray;
			try
			{
				string value = "PersistentShipObjects: " + text;
				switch (logType)
				{
				case 'w':
					Console.ForegroundColor = ConsoleColor.Yellow;
					Console.WriteLine(value);
					break;
				case 'm':
					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine(value);
					break;
				case 'e':
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine(value);
					break;
				case 'i':
					Console.WriteLine(value);
					break;
				default:
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine("DebugLogger received an invalid log type, but here's whatever this is:");
					DebugLog("    " + text, 'w');
					break;
				}
				Console.ResetColor();
			}
			catch (Exception value2)
			{
				Console.ForegroundColor = ConsoleColor.Red;
				Console.WriteLine(value2);
				Console.ResetColor();
			}
		}

		public static void DebugPrintDescendantsWrapper(Transform parent)
		{
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (doDebugPrints.Value && Object.op_Implicit((Object)(object)((Component)parent).gameObject))
			{
				if (Object.op_Implicit((Object)(object)parent.parent))
				{
					string name = ((Object)((Component)parent.parent).gameObject).name;
					Vector3 position = parent.parent.position;
					DebugLog("My parent: " + name + " at pos " + ((object)(Vector3)(ref position)).ToString(), 'w');
				}
				DebugPrintDescendants(parent, "");
			}
		}

		private static void DebugPrintDescendants(Transform parent, string indentMinusOne)
		{
			//IL_0091: 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_00fa: 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)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Expected O, but got Unknown
			indentMinusOne += "|   ";
			if (indentMinusOne.Length > 30 * "|   ".Length)
			{
				Console.WriteLine("DebugPrintDescendants: depth is " + indentMinusOne.Length / 4 + ", which is probably very wrong. If this is a mod conflict, got dang they are nesting too hard. Have probably a comical amount of error:");
				StackTrace stackTrace = new StackTrace();
				Debug.LogError((object)stackTrace);
				return;
			}
			DebugLog(((Component)parent).gameObject, 'w');
			DebugLog(((object)((Component)parent).gameObject).GetType(), 'w');
			string name = ((Object)((Component)parent).gameObject).name;
			Vector3 position = parent.position;
			DebugLog(name + " at pos " + ((object)(Vector3)(ref position)).ToString(), 'w');
			string[] obj = new string[9]
			{
				indentMinusOne,
				"P ",
				((object)((Component)parent).gameObject).GetType()?.ToString(),
				" named ",
				((Object)parent).name,
				" at pos ",
				null,
				null,
				null
			};
			position = parent.position;
			obj[6] = ((object)(Vector3)(ref position)).ToString();
			obj[7] = "-------------------- P of ";
			obj[8] = parent.childCount.ToString();
			DebugLog(string.Concat(obj), 'w');
			foreach (Transform item in parent)
			{
				Transform val = item;
				DebugLog(indentMinusOne + "|   " + ((object)val).GetType()?.ToString() + " named " + ((Object)val).name);
				if (val.childCount > 0)
				{
					DebugPrintDescendants(val, indentMinusOne);
				}
			}
		}
	}
	public class TransformObject
	{
		public AltVector3 pos;

		public AltVector3 rot;

		public int unlockableID;

		public string unlockableName;

		public TransformObject(Vector3 newPosition, Vector3 newRotation, int newUnlockableID, string newUnlockableName)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			pos = new AltVector3(newPosition);
			rot = new AltVector3(newRotation);
			unlockableID = newUnlockableID;
			unlockableName = newUnlockableName;
		}

		public Vector3 Get(string val)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: 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_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0049: Unknown result type (might be due to invalid IL or missing references)
			string text = val.ToLower();
			string text2 = text;
			if (!(text2 == "pos"))
			{
				if (text2 == "rot")
				{
					return rot.GetVector3();
				}
				return Vector3.zero;
			}
			return pos.GetVector3();
		}
	}
	public class AltVector3
	{
		public float x;

		public float y;

		public float z;

		public AltVector3(Vector3 value)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			x = value.x;
			y = value.y;
			z = value.z;
		}

		public Vector3 GetVector3()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(x, y, z);
		}
	}
}
namespace PersistentShipObjects.Patches
{
	internal class ShipBuildModeManagerPatch
	{
		[HarmonyPatch(typeof(ShipBuildModeManager), "PlaceShipObjectClientRpc")]
		[HarmonyPrefix]
		public static void PlaceShipObjectClientRpc(Vector3 newPosition, Vector3 newRotation, NetworkObjectReference objectRef, int playerWhoMoved)
		{
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			NetworkObject val = default(NetworkObject);
			((NetworkObjectReference)(ref objectRef)).TryGet(ref val, (NetworkManager)null);
			PlaceableShipObject componentInChildren = ((Component)val).GetComponentInChildren<PlaceableShipObject>();
			if ((Object)(object)componentInChildren == (Object)null)
			{
				Debug.LogError((object)"ShipBuildModeManagerPatch: placeableObject is null");
			}
			else if ((Object)(object)((Component)componentInChildren).transform == (Object)null)
			{
				Debug.LogError((object)"ShipBuildModeManagerPatch: placeableObject.transform is null");
			}
			else
			{
				Transform parent = ((Component)componentInChildren).transform.parent;
				GameObject val2 = ((parent != null) ? ((Component)parent).gameObject : null);
				if ((Object)(object)val2 == (Object)null)
				{
					Debug.LogError((object)"ShipBuildModeManagerPatch: actualObject is null");
				}
				else
				{
					string name = ((Object)val2).name;
					string[] obj = new string[6]
					{
						"ShipBuildModeManagerPatch: Saving trans of ",
						((object)componentInChildren).GetType()?.ToString(),
						" named ",
						name,
						" at pos ",
						null
					};
					Vector3 val3 = newPosition;
					obj[5] = ((object)(Vector3)(ref val3)).ToString();
					Debug.Log((object)string.Concat(obj));
					Transform transform = ((Component)((Component)componentInChildren).transform.parent).transform;
					val3 = ((Component)componentInChildren).transform.parent.position;
					Debug.Log((object)("old parent position: " + ((object)(Vector3)(ref val3)).ToString()));
					string unlockableName = StartOfRound.Instance.unlockablesList.unlockables[componentInChildren.unlockableID].unlockableName;
					TransformObject transformObject = new TransformObject(newPosition, newRotation, componentInChildren.unlockableID, unlockableName);
					Debug.Log((object)(transformObject.unlockableName + " ID " + transformObject.unlockableID));
					PersistentShipObjects.UpdateObjectManager(transformObject);
				}
			}
			Console.WriteLine("early return");
		}

		public static void makeAllPSOsFuckOff(string strxjksdf)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			Object[] array = Object.FindObjectsOfType(typeof(PlaceableShipObject));
			for (int i = 0; i < array.Length; i++)
			{
				PlaceableShipObject obj = (PlaceableShipObject)array[i];
				makePSOFuckOff(obj, strxjksdf);
			}
		}

		public static void makePSOFuckOff(PlaceableShipObject obj, string caller)
		{
			//IL_0064: 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)
			Debug.Log((object)("sending PSO " + ((Object)obj).name + " to fuck off land from " + caller + "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"));
			Vector3 position = default(Vector3);
			((Vector3)(ref position))..ctor(1000f, 10000f, 1000f);
			if ((Object)(object)obj.parentObjectSecondary != (Object)null)
			{
				obj.parentObjectSecondary.position = position;
			}
			else if ((Object)(object)obj.parentObject != (Object)null)
			{
				((Component)obj).transform.parent.position = position;
			}
		}

		[HarmonyPatch(typeof(StartOfRound), "OnEnable")]
		[HarmonyPostfix]
		public static void Start(ref StartOfRound __instance)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(1000f, 1000f, 1000f);
			Debug.Log((object)"MOVING SHIT AROUND!!!");
			Object[] array = Object.FindObjectsOfType(typeof(PlaceableShipObject));
			for (int i = 0; i < array.Length; i++)
			{
				PlaceableShipObject val2 = (PlaceableShipObject)array[i];
				PersistentShipObjects.DebugPrintDescendantsWrapper((val2 != null) ? ((Component)val2).transform : null);
				TransformObject transformObject = PersistentShipObjects.FindObjectIfExists(val2.unlockableID);
				if (transformObject != null)
				{
					string unlockableName = transformObject.unlockableName;
					Vector3 position = ((Component)((Component)val2).transform.parent).transform.position;
					Debug.Log((object)("    Item Found: " + unlockableName + " at pos " + ((object)(Vector3)(ref position)).ToString()));
					Debug.Log((object)("        type: " + ((object)val2).GetType()));
					Debug.Log((object)("        name: " + ((Object)val2).name));
					Transform parent = ((Component)val2).transform.parent;
					Debug.Log((object)("        parent type: " + ((parent != null) ? ((object)((Component)parent).gameObject).GetType() : null)));
					Transform parent2 = ((Component)val2).transform.parent;
					Debug.Log((object)("        parent name: " + ((parent2 != null) ? ((Object)parent2).name : null)));
					((Component)val2.parentObject).transform.position = transformObject.pos.GetVector3();
					((Component)val2.parentObject).transform.rotation = Quaternion.Euler(transformObject.rot.GetVector3());
					position = ((Component)val2).transform.parent.position;
					Debug.Log((object)("        new pos: " + ((object)(Vector3)(ref position)).ToString()));
				}
				else
				{
					Transform parent3 = ((Component)val2).transform.parent;
					Debug.Log((object)("    couldn't find " + ((parent3 != null) ? ((Object)parent3).name : null)));
				}
			}
		}
	}
}