Decompiled source of ConfigSync v2.0.0

BepInEx/plugins/ConfigSync/Notest.ConfigSync.dll

Decompiled 6 months 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 System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MyceliumNetworking;
using Steamworks;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using Zorro.Core.Serizalization;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Notest.ConfigSync")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0+755e00fd4bc35a31db91356510e9633290d33d23")]
[assembly: AssemblyProduct("ConfigSync")]
[assembly: AssemblyTitle("Notest.ConfigSync")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.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.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;
		}
	}
	[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 ConfigSync
{
	[ContentWarningPlugin("Notest.ConfigSync", "2.0.0", true)]
	[BepInPlugin("Notest.ConfigSync", "ConfigSync", "2.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class ConfigStartup : BaseUnityPlugin
	{
		public delegate void SerializerDelegate(TypeSerializer serializer, object value);

		public delegate object DeserializerDelegate(TypeDeserializer serializer, object? value);

		internal static uint modID;

		public static Dictionary<Type, SerializerDelegate> SerializerDictionary;

		public static Dictionary<Type, DeserializerDelegate> DeserializerDictionary;

		public static ConfigStartup Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			MyceliumNetwork.RegisterNetworkObject((object)this, modID, 0);
			MyceliumNetwork.LobbyLeft += Synchronizer.OnLobbyLeft;
			MyceliumNetwork.PlayerEntered += Synchronizer.OnPlayerEntered;
			Patch();
			Logger.LogInfo((object)"Notest.ConfigSync v2.0.0 has loaded!");
		}

		public static void RPCTargetRelay(string methodName, CSteamID steamId, string configGUID, Type type, object value)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			if (SerializerDictionary.TryGetValue(type, out SerializerDelegate value2))
			{
				TypeSerializer typeSerializer = new TypeSerializer();
				value2(typeSerializer, value);
				byte[] array = Array.Empty<byte>();
				NativeArray<byte> buffer = ((BinarySerializer)typeSerializer).buffer;
				ByteArrayConvertion.MoveToByteArray<byte>(ref buffer, ref array);
				MyceliumNetwork.RPCTarget(modID, methodName, steamId, (ReliableType)1, new object[2] { configGUID, array });
				((BinarySerializer)typeSerializer).Dispose();
			}
			else
			{
				Logger.LogError((object)$"Type {type} is not currently serializable!");
			}
		}

		public static void RPCTargetRelay(string methodName, CSteamID steamId, string configGUID)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			MyceliumNetwork.RPCTarget(modID, methodName, steamId, (ReliableType)1, new object[1] { configGUID });
		}

		[CustomRPC]
		public void ReceiveSync(string configGUID, byte[] byteArray)
		{
			if (MyceliumNetwork.IsHost)
			{
				Logger.LogWarning((object)"ReceiveSync was called to the host! This is not supposed to happen!");
				return;
			}
			Configuration configOfGUID = Synchronizer.GetConfigOfGUID(configGUID);
			if (configOfGUID == null)
			{
				Logger.LogError((object)("Config of GUID '" + configGUID + "' does not exist!"));
				return;
			}
			Type configType = configOfGUID.ConfigType;
			if (DeserializerDictionary.TryGetValue(configType, out DeserializerDelegate value))
			{
				TypeDeserializer typeDeserializer = new TypeDeserializer(byteArray, (Allocator)2);
				object value2 = value(typeDeserializer, null);
				configOfGUID.UpdateValue(value2);
				((BinaryDeserializer)typeDeserializer).Dispose();
			}
			else
			{
				Logger.LogError((object)$"Type '{configType}' is not currently deserializable!");
			}
		}

		[CustomRPC]
		public void RequestSync(string configGUID, RPCInfo info)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			Configuration configOfGUID = Synchronizer.GetConfigOfGUID(configGUID);
			if (configOfGUID == null)
			{
				Logger.LogError((object)("Config of GUID '" + configGUID + "' does not exist!"));
			}
			else
			{
				RPCTargetRelay("ReceiveSync", info.SenderSteamID, configGUID, configOfGUID.ConfigType, configOfGUID.CurrentValue);
			}
		}

		internal static void Patch()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Notest.ConfigSync");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}

		static ConfigStartup()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			Hash128 val = Hash128.Compute("Notest.ConfigSync");
			modID = (uint)((object)(Hash128)(ref val)).GetHashCode();
			SerializerDictionary = new Dictionary<Type, SerializerDelegate>
			{
				{
					typeof(bool),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteBool((bool)value);
					}
				},
				{
					typeof(byte),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteByte((byte)value);
					}
				},
				{
					typeof(short),
					delegate(TypeSerializer serializer, object value)
					{
						serializer.WriteShort((short)value);
					}
				},
				{
					typeof(int),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteInt((int)value);
					}
				},
				{
					typeof(long),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteLong((long)value);
					}
				},
				{
					typeof(ushort),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteUshort((ushort)value);
					}
				},
				{
					typeof(uint),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteUInt((uint)value);
					}
				},
				{
					typeof(ulong),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteUlong((ulong)value);
					}
				},
				{
					typeof(float),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteFloat((float)value);
					}
				},
				{
					typeof(Vector3),
					delegate(TypeSerializer serializer, object value)
					{
						//IL_0002: Unknown result type (might be due to invalid IL or missing references)
						serializer.WriteVector3((Vector3)value);
					}
				},
				{
					typeof(Quaternion),
					delegate(TypeSerializer serializer, object value)
					{
						//IL_0002: Unknown result type (might be due to invalid IL or missing references)
						serializer.WriteQuaternion((Quaternion)value);
					}
				},
				{
					typeof(string),
					delegate(TypeSerializer serializer, object value)
					{
						((BinarySerializer)serializer).WriteString((string)value, Encoding.UTF8);
					}
				}
			};
			DeserializerDictionary = new Dictionary<Type, DeserializerDelegate>
			{
				{
					typeof(bool),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadBool()
				},
				{
					typeof(byte),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadByte()
				},
				{
					typeof(short),
					(TypeDeserializer serializer, object? value) => serializer.ReadShort()
				},
				{
					typeof(int),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadInt()
				},
				{
					typeof(long),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadLong()
				},
				{
					typeof(ushort),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadUShort()
				},
				{
					typeof(uint),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadUInt()
				},
				{
					typeof(ulong),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadUlong()
				},
				{
					typeof(float),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadFloat()
				},
				{
					typeof(Vector3),
					(TypeDeserializer serializer, object? value) => serializer.ReadVector3()
				},
				{
					typeof(Quaternion),
					(TypeDeserializer serializer, object? value) => serializer.ReadQuaternion()
				},
				{
					typeof(string),
					(TypeDeserializer serializer, object? value) => ((BinaryDeserializer)serializer).ReadString(Encoding.UTF8)
				}
			};
		}
	}
	public class Configuration
	{
		public string ConfigName;

		public string ConfigGUID;

		public object CurrentValue;

		public object InitialValue;

		public Type ConfigType;

		public event Action<object>? ConfigChanged;

		public Configuration(string name, string GUID, object initialValue)
		{
			ConfigName = name;
			ConfigGUID = GUID;
			InitialValue = initialValue;
			CurrentValue = initialValue;
			ConfigType = initialValue.GetType();
			ConfigStartup.Logger.LogDebug((object)$"Config created by end user, initial value: {initialValue}");
			Synchronizer.ConfigList.Add(this);
			if (MyceliumNetwork.InLobby)
			{
				Synchronizer.SyncConfig(ConfigGUID, CurrentValue);
			}
		}

		internal void UpdateValue(object value)
		{
			ConfigStartup.Logger.LogDebug((object)$"Current value updated internally, new value: {value}, old value: {CurrentValue}");
			CurrentValue = value;
			this.ConfigChanged?.Invoke(CurrentValue);
		}

		public void SetValue(object value)
		{
			InitialValue = value;
			if (MyceliumNetwork.InLobby && !MyceliumNetwork.IsHost)
			{
				ConfigStartup.Logger.LogDebug((object)$"Current value tried to be set by end user, but is in lobby without being a host, value: {value}");
				return;
			}
			ConfigStartup.Logger.LogDebug((object)$"Current value set by end user, value: {value}");
			CurrentValue = value;
			if (MyceliumNetwork.InLobby && MyceliumNetwork.IsHost)
			{
				Synchronizer.SyncConfig(ConfigGUID, CurrentValue);
			}
		}
	}
	public static class Synchronizer
	{
		public static List<Configuration> ConfigList = new List<Configuration>();

		internal static void OnPlayerEntered(CSteamID cSteamID)
		{
			//IL_0007: 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)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (!MyceliumNetwork.IsHost || MyceliumNetwork.LobbyHost == cSteamID)
			{
				return;
			}
			foreach (Configuration config in ConfigList)
			{
				ConfigStartup.RPCTargetRelay("ReceiveSync", cSteamID, config.ConfigGUID, config.ConfigType, config.CurrentValue);
			}
		}

		internal static void OnLobbyLeft()
		{
			for (int i = 0; i < ConfigList.Count; i++)
			{
				Configuration configuration = ConfigList[i];
				configuration.UpdateValue(configuration.InitialValue);
			}
		}

		internal static void SyncConfig(string configGUID, object? value = null)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: 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_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			if (!MyceliumNetwork.InLobby)
			{
				ConfigStartup.Logger.LogWarning((object)"SyncConfig called but was not in lobby!");
				return;
			}
			Configuration configOfGUID = GetConfigOfGUID(configGUID);
			if (configOfGUID == null)
			{
				return;
			}
			if (MyceliumNetwork.IsHost)
			{
				CSteamID[] players = MyceliumNetwork.Players;
				foreach (CSteamID val in players)
				{
					if (!(MyceliumNetwork.LobbyHost == val))
					{
						ConfigStartup.RPCTargetRelay("ReceiveSync", val, configGUID, configOfGUID.ConfigType, value);
					}
				}
			}
			else
			{
				ConfigStartup.RPCTargetRelay("RequestSync", MyceliumNetwork.LobbyHost, configGUID);
			}
		}

		public static Configuration? GetConfigOfName(string name)
		{
			string name2 = name;
			return ConfigList.Find((Configuration match) => match.ConfigName == name2);
		}

		public static Configuration? GetConfigOfGUID(string GUID)
		{
			string GUID2 = GUID;
			return ConfigList.Find((Configuration match) => match.ConfigGUID == GUID2);
		}
	}
	public class TypeDeserializer : BinaryDeserializer
	{
		public TypeDeserializer(byte[] buffer, Allocator allocator)
			: base(buffer, allocator)
		{
		}//IL_0002: Unknown result type (might be due to invalid IL or missing references)


		public short ReadShort()
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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)
			NativeSlice<byte> val = default(NativeSlice<byte>);
			val..ctor(base.buffer, base.position, 2);
			short result = val.SliceConvert<short>()[0];
			base.position += 2;
			return result;
		}

		public Vector3 ReadVector3()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(((BinaryDeserializer)this).ReadFloat(), ((BinaryDeserializer)this).ReadFloat(), ((BinaryDeserializer)this).ReadFloat());
		}

		public Quaternion ReadQuaternion()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			return new Quaternion(((BinaryDeserializer)this).ReadFloat(), ((BinaryDeserializer)this).ReadFloat(), ((BinaryDeserializer)this).ReadFloat(), ((BinaryDeserializer)this).ReadFloat());
		}
	}
	public class TypeSerializer : BinarySerializer
	{
		public void WriteShort(short value)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			NativeArray<short> val = default(NativeArray<short>);
			val..ctor(1, (Allocator)2, (NativeArrayOptions)1);
			val[0] = value;
			((BinarySerializer)this).WriteBytes(val.Reinterpret<byte>(UnsafeUtility.SizeOf<short>()));
			val.Dispose();
		}

		public void WriteVector3(Vector3 value)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			((BinarySerializer)this).WriteFloat(value.x);
			((BinarySerializer)this).WriteFloat(value.y);
			((BinarySerializer)this).WriteFloat(value.z);
		}

		public void WriteQuaternion(Quaternion value)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: 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)
			((BinarySerializer)this).WriteFloat(value.x);
			((BinarySerializer)this).WriteFloat(value.y);
			((BinarySerializer)this).WriteFloat(value.z);
			((BinarySerializer)this).WriteFloat(value.w);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Notest.ConfigSync";

		public const string PLUGIN_NAME = "ConfigSync";

		public const string PLUGIN_VERSION = "2.0.0";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}