Decompiled source of R2API Prefab v1.0.4

plugins/R2API.Prefab/R2API.Prefab.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using On.RoR2;
using On.RoR2.Networking;
using R2API.AutoVersionGen;
using R2API.ContentManagement;
using R2API.ScriptableObjects;
using R2API.Utils;
using RoR2.Networking;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("R2API.Prefab")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+4ab123e8fb22d95bcc11c94c9804307ec130fa2c")]
[assembly: AssemblyProduct("R2API.Prefab")]
[assembly: AssemblyTitle("R2API.Prefab")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.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 System.Diagnostics.CodeAnalysis
{
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullAttribute : Attribute
	{
		public string[] Members { get; }

		public MemberNotNullAttribute(string member)
		{
			Members = new string[1] { member };
		}

		public MemberNotNullAttribute(params string[] members)
		{
			Members = members;
		}
	}
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullWhenAttribute : Attribute
	{
		public bool ReturnValue { get; }

		public string[] Members { get; }

		public MemberNotNullWhenAttribute(bool returnValue, string member)
		{
			ReturnValue = returnValue;
			Members = new string[1] { member };
		}

		public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
		{
			ReturnValue = returnValue;
			Members = members;
		}
	}
}
namespace R2API
{
	[AutoVersion]
	public static class PrefabAPI
	{
		private struct HashStruct
		{
			public GameObject Prefab;

			public string GoName;

			public string TypeName;

			public string MethodName;

			public Assembly Assembly;
		}

		[CompilerGenerated]
		private static class <>O
		{
			public static hook_OnStartClient <0>__SetProperAssetIdForDelayedPrefabs;
		}

		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static Func<HashStruct, GameObject> <>9__6_0;

			public static hook_IsPrefab <>9__14_0;

			internal GameObject <IsPrefabHashed>b__6_0(HashStruct hash)
			{
				return hash.Prefab;
			}

			internal bool <GetParent>b__14_0(orig_IsPrefab orig, GameObject obj)
			{
				if (Object.op_Implicit((Object)(object)obj.transform.parent) && ((Object)((Component)obj.transform.parent).gameObject).name == "ModdedPrefabs")
				{
					return true;
				}
				return orig.Invoke(obj);
			}
		}

		public const string PluginGUID = "com.bepis.r2api.prefab";

		public const string PluginName = "R2API.Prefab";

		private static GameObject _parent;

		private static readonly List<HashStruct> _networkedPrefabs = new List<HashStruct>();

		private static bool _delayedPrefabsHookEnabled = false;

		private static List<HashStruct> _prefabsWithDelayedAddedNetworkIdentities = new List<HashStruct>();

		public const string PluginVersion = "1.0.4";

		[Obsolete("All submodules are automatically loaded and this property is now unused")]
		public static bool Loaded => true;

		public static bool IsPrefabHashed(GameObject prefabToCheck)
		{
			return _networkedPrefabs.Select((HashStruct hash) => hash.Prefab).Contains(prefabToCheck);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		public static GameObject InstantiateClone(this GameObject g, string nameToSet)
		{
			return g.InstantiateCloneInternal(nameToSet, registerNetwork: true);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		public static GameObject InstantiateClone(this GameObject g, string nameToSet, bool registerNetwork)
		{
			return g.InstantiateCloneInternal(nameToSet, registerNetwork);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		[Obsolete("Left over to not break old mods")]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static GameObject InstantiateClone(this GameObject? g, string? nameToSet, bool registerNetwork = true, [CallerFilePath] string? file = "", [CallerMemberName] string? member = "", [CallerLineNumber] int line = 0)
		{
			return g.InstantiateCloneInternal(nameToSet, registerNetwork);
		}

		private static GameObject InstantiateCloneInternal(this GameObject g, string nameToSet, bool registerNetwork)
		{
			GameObject val = Object.Instantiate<GameObject>(g, GetParent().transform);
			((Object)val).name = nameToSet;
			if (registerNetwork)
			{
				RegisterPrefabInternal(val, new StackFrame(2));
			}
			return val;
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		public static void RegisterNetworkPrefab(this GameObject g)
		{
			RegisterNetworkPrefabInternal(g);
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		[Obsolete("Left over to not break old mods.")]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static void RegisterNetworkPrefab(this GameObject? g, [CallerFilePath] string? file = "", [CallerMemberName] string? member = "", [CallerLineNumber] int line = 0)
		{
			RegisterNetworkPrefabInternal(g);
		}

		private static void RegisterNetworkPrefabInternal(GameObject g)
		{
			RegisterPrefabInternal(g, new StackFrame(2));
		}

		private static GameObject GetParent()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			if (!Object.op_Implicit((Object)(object)_parent))
			{
				_parent = new GameObject("ModdedPrefabs");
				Object.DontDestroyOnLoad((Object)(object)_parent);
				((Object)_parent).hideFlags = (HideFlags)61;
				_parent.SetActive(false);
				object obj2 = <>c.<>9__14_0;
				if (obj2 == null)
				{
					hook_IsPrefab val = (orig_IsPrefab orig, GameObject obj) => (Object.op_Implicit((Object)(object)obj.transform.parent) && ((Object)((Component)obj.transform.parent).gameObject).name == "ModdedPrefabs") || orig.Invoke(obj);
					<>c.<>9__14_0 = val;
					obj2 = (object)val;
				}
				Util.IsPrefab += (hook_IsPrefab)obj2;
			}
			return _parent;
		}

		private static void RegisterPrefabInternal(GameObject prefab, StackFrame frame)
		{
			//IL_009e: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Expected O, but got Unknown
			MethodBase method = frame.GetMethod();
			HashStruct hashStruct = default(HashStruct);
			hashStruct.Prefab = prefab;
			hashStruct.GoName = ((Object)prefab).name;
			hashStruct.TypeName = method.DeclaringType.AssemblyQualifiedName;
			hashStruct.MethodName = method.Name;
			hashStruct.Assembly = method.DeclaringType.Assembly;
			HashStruct hashStruct2 = hashStruct;
			_networkedPrefabs.Add(hashStruct2);
			AddToNetworkedObjectPrefabs(hashStruct2);
			NetworkIdentity component = hashStruct2.Prefab.GetComponent<NetworkIdentity>();
			if (Object.op_Implicit((Object)(object)component))
			{
				Reflection.SetFieldValue<NetworkHash128>((object)component, "m_AssetId", NetworkHash128.Parse(MakeHash(hashStruct2.GoName + hashStruct2.TypeName + hashStruct2.MethodName)));
				return;
			}
			_prefabsWithDelayedAddedNetworkIdentities.Add(hashStruct2);
			if (!_delayedPrefabsHookEnabled)
			{
				object obj = <>O.<0>__SetProperAssetIdForDelayedPrefabs;
				if (obj == null)
				{
					hook_OnStartClient val = SetProperAssetIdForDelayedPrefabs;
					<>O.<0>__SetProperAssetIdForDelayedPrefabs = val;
					obj = (object)val;
				}
				NetworkManagerSystem.OnStartClient += (hook_OnStartClient)obj;
				_delayedPrefabsHookEnabled = true;
			}
			static void AddToNetworkedObjectPrefabs(HashStruct h)
			{
				R2APISerializableContentPack orCreateSerializableContentPack = R2APIContentManager.GetOrCreateSerializableContentPack(h.Assembly);
				List<GameObject> list = orCreateSerializableContentPack.networkedObjectPrefabs.ToList();
				list.Add(h.Prefab);
				orCreateSerializableContentPack.networkedObjectPrefabs = list.ToArray();
			}
		}

		private static void SetProperAssetIdForDelayedPrefabs(orig_OnStartClient orig, NetworkManagerSystem self, NetworkClient newClient)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				foreach (HashStruct prefabsWithDelayedAddedNetworkIdentity in _prefabsWithDelayedAddedNetworkIdentities)
				{
					NetworkIdentity component = prefabsWithDelayedAddedNetworkIdentity.Prefab.GetComponent<NetworkIdentity>();
					if (Object.op_Implicit((Object)(object)component))
					{
						Reflection.SetFieldValue<NetworkHash128>((object)component, "m_AssetId", NetworkHash128.Parse(MakeHash(prefabsWithDelayedAddedNetworkIdentity.GoName + prefabsWithDelayedAddedNetworkIdentity.TypeName + prefabsWithDelayedAddedNetworkIdentity.MethodName)));
					}
					else
					{
						PrefabPlugin.Logger.LogError((object)$"{prefabsWithDelayedAddedNetworkIdentity.Prefab} don't have a NetworkIdentity Component but was marked for network registration.");
					}
				}
			}
			catch (Exception ex)
			{
				PrefabPlugin.Logger.LogError((object)ex);
			}
			orig.Invoke(self, newClient);
		}

		private static string MakeHash(string s)
		{
			MD5 mD = MD5.Create();
			byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(s));
			mD.Dispose();
			StringBuilder stringBuilder = new StringBuilder();
			byte[] array2 = array;
			foreach (byte b in array2)
			{
				stringBuilder.Append(b.ToString("x2"));
			}
			return stringBuilder.ToString();
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.bepis.r2api.prefab", "R2API.Prefab", "1.0.4")]
	public sealed class PrefabPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
		}
	}
}
namespace R2API.AutoVersionGen
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	internal class AutoVersionAttribute : Attribute
	{
	}
}