Decompiled source of Spawn Remapper v1.1.1

SpawnRemap.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Bootstrap;
using ChatCommands;
using ChatCommands.Attributes;
using ComputerysModdingUtilities;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MyceliumNetworking;
using Newtonsoft.Json;
using SpawnRemap;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: StraftatMod(false)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Koki")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+32a84bb9f23238789d5c6522a326f35230b78285")]
[assembly: AssemblyProduct("SpawnRemap")]
[assembly: AssemblyTitle("SpawnRemap")]
[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;
		}
	}
}
[CommandCategory("SpawnRemap")]
internal class SpawnRemapCommands
{
	private const CommandFlags _spawnEditFlags = 7;

	[Command(/*Could not decode attribute arguments.*/)]
	[CommandAliases(new string[] { "srps" })]
	private static string PlaceSpawn(int spawnPointIndex, bool twovtwo = false)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: 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_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		if (!twovtwo && spawnPointIndex != 0 && spawnPointIndex != 1)
		{
			throw new CommandException("Input a spawn # from 0-1!");
		}
		if (twovtwo && (spawnPointIndex < 0 || spawnPointIndex > 3))
		{
			throw new CommandException("Input a spawn # from 0-3!");
		}
		FirstPersonController localPlayer = Settings.Instance.localPlayer;
		Scene activeScene = SceneManager.GetActiveScene();
		string name = ((Scene)(ref activeScene)).name;
		if (!SaveData.spawnRemaps.TryGetValue(name, out var value))
		{
			value = new List<SpawnData>(6);
			value.AddRange(new <>z__ReadOnlyArray<SpawnData>(new SpawnData[6]));
		}
		if (twovtwo)
		{
			spawnPointIndex += 2;
		}
		List<SpawnData> list = value;
		int index = spawnPointIndex;
		SpawnData value2 = new SpawnData
		{
			position = ((Component)localPlayer).transform.position
		};
		float rotationX = localPlayer.rotationX;
		Quaternion rotation = ((Component)localPlayer).transform.rotation;
		value2.rotation = (SerializableVector3)new Vector3(rotationX, ((Quaternion)(ref rotation)).eulerAngles.y, localPlayer.rotationZ);
		list[index] = value2;
		SaveData.spawnRemaps[name] = value;
		SaveData.Save();
		RemapSpawnLocal.SendSpawnPoints(value);
		if (twovtwo)
		{
			return $"Set spawnpoint {spawnPointIndex - 2} for 2v2";
		}
		return $"Set spawnpoint {spawnPointIndex} for 1v1";
	}

	[Command(/*Could not decode attribute arguments.*/)]
	[CommandAliases(new string[] { "srrs" })]
	private static string ResetSpawn(string spawnPointIndex, bool twovtwo = false)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		Scene activeScene = SceneManager.GetActiveScene();
		string name = ((Scene)(ref activeScene)).name;
		if (!SaveData.spawnRemaps.TryGetValue(name, out var value))
		{
			throw new CommandException("No spawn remaps have been set for this map.");
		}
		if (spawnPointIndex == "all")
		{
			if (twovtwo)
			{
				value.RemoveRange(2, 4);
				value.AddRange(new <>z__ReadOnlyArray<SpawnData>(new SpawnData[4]));
			}
			else
			{
				value[0] = default(SpawnData);
				value[1] = default(SpawnData);
			}
		}
		else
		{
			if (!int.TryParse(spawnPointIndex, out var result))
			{
				throw new CommandException("Input a spawn # from 0-3, or the word \"all\"!");
			}
			if (!twovtwo && result != 0 && result != 1)
			{
				throw new CommandException("Input a spawn # from 0-1!");
			}
			if (twovtwo && (result < 0 || result > 3))
			{
				throw new CommandException("Input a spawn # from 0-3!");
			}
			int num = (twovtwo ? 2 : 0);
			if ((Vector3)value[result + num].rotation == default(Vector3))
			{
				throw new CommandException($"Spawn #{result} has no remap!");
			}
			value[result + num] = default(SpawnData);
		}
		SaveData.spawnRemaps[name] = value;
		SaveData.Save();
		RemapSpawnLocal.SendSpawnPoints(value);
		return "Removed spawnpoint " + spawnPointIndex;
	}

	[Command(/*Could not decode attribute arguments.*/)]
	[CommandAliases(new string[] { "srexp" })]
	private static string ExportSpawns()
	{
		GUIUtility.systemCopyBuffer = SaveData.Compress(JsonConvert.SerializeObject((object)SaveData.spawnRemaps));
		return "Exported spawn data to clipboard!";
	}

	[Command(/*Could not decode attribute arguments.*/)]
	[CommandAliases(new string[] { "srimp" })]
	private static string ImportSpawns()
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			SaveData.spawnRemaps = JsonConvert.DeserializeObject<Dictionary<string, List<SpawnData>>>(SaveData.Decompress(GUIUtility.systemCopyBuffer));
			SaveData.Save();
		}
		catch (Exception)
		{
			throw new CommandException("Invalid code!");
		}
		return "Imported spawn data!";
	}
}
internal class RemapSpawnLocal : MonoBehaviour
{
	private const int MyceliumID = 239487836;

	internal static List<SpawnData> originalSpawnLocations;

	private void Awake()
	{
		MyceliumNetwork.RegisterNetworkObject((object)this, 239487836u, 0);
		originalSpawnLocations = new List<SpawnData>(6)
		{
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData)
		};
	}

	internal static void SendSpawnPoints(List<SpawnData> spawns)
	{
		MyceliumNetwork.RPC(239487836u, "MoveSpawnPoints", (ReliableType)1, new object[1] { JsonConvert.SerializeObject((object)spawns) });
	}

	[CustomRPC]
	private void MoveSpawnPoints(string spawnJSON)
	{
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: 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_0177: Unknown result type (might be due to invalid IL or missing references)
		//IL_017e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_0230: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: 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_01ba: 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_01cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01db: 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_01e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		List<SpawnData> list;
		try
		{
			list = JsonConvert.DeserializeObject<List<SpawnData>>(spawnJSON);
		}
		catch (Exception arg)
		{
			Debug.LogError((object)$"SpawnRemapper: Failed to decode JSON from host: {arg}");
			return;
		}
		List<Transform> source = (from s in Object.FindObjectsByType<SpawnPoint>((FindObjectsSortMode)0)
			select ((Component)s).transform into t
			orderby ((Object)t).name
			select t).ToList();
		List<Transform> list2 = new List<Transform>();
		list2.AddRange(source.Where((Transform t) => ((Component)t.parent).tag != "Spawnpoints4Player"));
		list2.AddRange(source.Where((Transform t) => ((Component)t.parent).tag == "Spawnpoints4Player"));
		source = list2;
		for (int i = 0; i < list.Count; i++)
		{
			Transform val = source[i];
			if ((Vector3)list[i].position == default(Vector3))
			{
				if ((Vector3)originalSpawnLocations[i].position != default(Vector3))
				{
					val.position = originalSpawnLocations[i].position;
					val.eulerAngles = originalSpawnLocations[i].rotation;
				}
				continue;
			}
			if ((Vector3)originalSpawnLocations[i].position == default(Vector3))
			{
				SpawnData value = originalSpawnLocations[i];
				value.position = val.position;
				Quaternion rotation = val.rotation;
				float x = ((Quaternion)(ref rotation)).eulerAngles.x;
				rotation = val.rotation;
				float y = ((Quaternion)(ref rotation)).eulerAngles.y;
				rotation = val.rotation;
				value.rotation = (SerializableVector3)new Vector3(x, y, ((Quaternion)(ref rotation)).eulerAngles.z);
				originalSpawnLocations[i] = value;
			}
			val.position = list[i].position;
			val.eulerAngles = list[i].rotation;
		}
	}
}
internal static class ApplyRemap
{
	internal static void Init()
	{
		SceneManager.sceneLoaded += DetermineRemapping;
	}

	private static void DetermineRemapping(Scene s, LoadSceneMode mode)
	{
		RemapSpawnLocal.originalSpawnLocations = new List<SpawnData>(6)
		{
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData),
			default(SpawnData)
		};
		if (SaveData.spawnRemaps.ContainsKey(((Scene)(ref s)).name))
		{
			RemapSpawnLocal.SendSpawnPoints(SaveData.spawnRemaps[((Scene)(ref s)).name]);
		}
	}
}
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("com.koki.spawnremap", "Spawn Remapper", "1.1.1")]
public class SRPlugin : BaseUnityPlugin
{
	private Harmony _harmony = new Harmony("com.koki.spawnremap");

	private void Awake()
	{
		((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
		SaveData.Init();
		ApplyRemap.Init();
		((Component)this).gameObject.AddComponent<RemapSpawnLocal>();
		if (Chainloader.PluginInfos.ContainsKey("kestrel.straftat.chatcommands"))
		{
			RegisterCommands();
		}
	}

	private void OnDestroy()
	{
		_harmony.UnpatchSelf();
	}

	private void RegisterCommands()
	{
		CommandRegistry.RegisterCommandsFromAssembly();
	}
}
namespace SpawnRemap
{
	[Serializable]
	internal struct SpawnData
	{
		public SerializableVector3 position;

		public SerializableVector3 rotation;
	}
	internal static class SaveData
	{
		private const string _saveFilename = "com.koki.spawnremap_spawnpoint_data.json";

		private static string _savePath;

		internal static Dictionary<string, List<SpawnData>> spawnRemaps;

		internal static void Init()
		{
			_savePath = Path.Combine(Paths.ConfigPath, "com.koki.spawnremap_spawnpoint_data.json");
			try
			{
				if (File.Exists(_savePath))
				{
					spawnRemaps = JsonConvert.DeserializeObject<Dictionary<string, List<SpawnData>>>(File.ReadAllText(_savePath));
				}
				if (spawnRemaps == null)
				{
					spawnRemaps = new Dictionary<string, List<SpawnData>>();
					Save();
				}
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"SpawnRemapper: Failed to read save data: {arg}");
			}
		}

		internal static void Save()
		{
			try
			{
				File.WriteAllText(_savePath, JsonConvert.SerializeObject((object)spawnRemaps, (Formatting)1));
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"SpawnRemapper: Failed to save data {arg}");
			}
		}

		internal static string Compress(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return text;
			}
			byte[] bytes = Encoding.UTF8.GetBytes(text);
			using MemoryStream memoryStream = new MemoryStream();
			using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, leaveOpen: true))
			{
				deflateStream.Write(bytes, 0, bytes.Length);
			}
			memoryStream.Position = 0L;
			return Convert.ToBase64String(memoryStream.ToArray());
		}

		internal static string Decompress(string compressedText)
		{
			if (string.IsNullOrEmpty(compressedText))
			{
				return compressedText;
			}
			using MemoryStream stream = new MemoryStream(Convert.FromBase64String(compressedText));
			using DeflateStream deflateStream = new DeflateStream(stream, CompressionMode.Decompress);
			using MemoryStream memoryStream = new MemoryStream();
			deflateStream.CopyTo(memoryStream);
			return Encoding.UTF8.GetString(memoryStream.ToArray());
		}
	}
	[Serializable]
	internal struct SerializableVector3
	{
		public float x;

		public float y;

		public float z;

		public SerializableVector3(float x, float y, float z)
		{
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public static implicit operator Vector3(SerializableVector3 sVec)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(sVec.x, sVec.y, sVec.z);
		}

		public static implicit operator SerializableVector3(Vector3 uVec)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return new SerializableVector3(uVec.x, uVec.y, uVec.z);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.koki.spawnremap";

		public const string PLUGIN_NAME = "Spawn Remapper";

		public const string PLUGIN_VERSION = "1.1.1";
	}
}
[CompilerGenerated]
internal sealed class <>z__ReadOnlyArray<T> : IEnumerable, ICollection, IList, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>
{
	int ICollection.Count => _items.Length;

	bool ICollection.IsSynchronized => false;

	object ICollection.SyncRoot => this;

	object IList.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	bool IList.IsFixedSize => true;

	bool IList.IsReadOnly => true;

	int IReadOnlyCollection<T>.Count => _items.Length;

	T IReadOnlyList<T>.this[int index] => _items[index];

	int ICollection<T>.Count => _items.Length;

	bool ICollection<T>.IsReadOnly => true;

	T IList<T>.this[int index]
	{
		get
		{
			return _items[index];
		}
		set
		{
			throw new NotSupportedException();
		}
	}

	public <>z__ReadOnlyArray(T[] items)
	{
		_items = items;
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return ((IEnumerable)_items).GetEnumerator();
	}

	void ICollection.CopyTo(Array array, int index)
	{
		((ICollection)_items).CopyTo(array, index);
	}

	int IList.Add(object value)
	{
		throw new NotSupportedException();
	}

	void IList.Clear()
	{
		throw new NotSupportedException();
	}

	bool IList.Contains(object value)
	{
		return ((IList)_items).Contains(value);
	}

	int IList.IndexOf(object value)
	{
		return ((IList)_items).IndexOf(value);
	}

	void IList.Insert(int index, object value)
	{
		throw new NotSupportedException();
	}

	void IList.Remove(object value)
	{
		throw new NotSupportedException();
	}

	void IList.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}

	IEnumerator<T> IEnumerable<T>.GetEnumerator()
	{
		return ((IEnumerable<T>)_items).GetEnumerator();
	}

	void ICollection<T>.Add(T item)
	{
		throw new NotSupportedException();
	}

	void ICollection<T>.Clear()
	{
		throw new NotSupportedException();
	}

	bool ICollection<T>.Contains(T item)
	{
		return ((ICollection<T>)_items).Contains(item);
	}

	void ICollection<T>.CopyTo(T[] array, int arrayIndex)
	{
		((ICollection<T>)_items).CopyTo(array, arrayIndex);
	}

	bool ICollection<T>.Remove(T item)
	{
		throw new NotSupportedException();
	}

	int IList<T>.IndexOf(T item)
	{
		return ((IList<T>)_items).IndexOf(item);
	}

	void IList<T>.Insert(int index, T item)
	{
		throw new NotSupportedException();
	}

	void IList<T>.RemoveAt(int index)
	{
		throw new NotSupportedException();
	}
}