Decompiled source of NetSyncGuard v1.0.0

NetSyncGuard.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[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("NetSyncGuard")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NetSyncGuard")]
[assembly: AssemblyTitle("NetSyncGuard")]
[assembly: AssemblyMetadata("AI_Assisted_Creation", "Code generated with Anthropic Claude (Claude Opus 5.5 and other Claude models, via Claude Code); designed and tested by the author")]
[assembly: AssemblyMetadata("AI_Model_Vendor", "Anthropic")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NetSyncGuard;

[BepInPlugin("jacob.netsyncguard", "NetSyncGuard", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "jacob.netsyncguard";

	public const string Name = "NetSyncGuard";

	public const string Version = "1.0.0";

	internal static ManualLogSource Log;

	private static PropertyInfo _isSceneObject;

	private static readonly HashSet<int> Reported = new HashSet<int>();

	private static int _errors;

	private void Awake()
	{
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			_isSceneObject = AccessTools.Property(typeof(NetworkObject), "IsSceneObject");
			MethodInfo methodInfo = AccessTools.Method(typeof(NetworkObject), "HostCheckForGlobalObjectIdHashOverride", (Type[])null, (Type[])null);
			if (_isSceneObject == null || _isSceneObject.GetSetMethod(nonPublic: true) == null || methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"NetworkObject.IsSceneObject or HostCheckForGlobalObjectIdHashOverride not found (Netcode changed?); doing nothing");
				return;
			}
			new Harmony("jacob.netsyncguard").Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "HostCheckPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"NetSyncGuard 1.0.0 loaded");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Couldn't apply the join-sync guard: " + ex));
		}
	}

	private static void HostCheckPrefix(NetworkObject __instance)
	{
		if (_errors >= 10 || (Object)(object)__instance == (Object)null)
		{
			return;
		}
		try
		{
			if (!((bool?)_isSceneObject.GetValue(__instance, null)).HasValue)
			{
				_isSceneObject.GetSetMethod(nonPublic: true).Invoke(__instance, new object[1] { false });
				if (Reported.Add(((Object)__instance).GetInstanceID()))
				{
					Log.LogWarning((object)("Fixed an object that would have broken joining: " + Describe(__instance)));
				}
			}
		}
		catch (Exception ex)
		{
			_errors++;
			Log.LogWarning((object)("Guard failed (" + _errors + "/10): " + ex.Message));
		}
	}

	private static string Field(NetworkObject o, string name)
	{
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(NetworkObject), name);
			return (fieldInfo == null) ? "?" : Convert.ToString(fieldInfo.GetValue(o));
		}
		catch
		{
			return "?";
		}
	}

	private static string Describe(NetworkObject o)
	{
		string text = ((Object)o).name;
		try
		{
			Transform parent = ((Component)o).transform.parent;
			while ((Object)(object)parent != (Object)null)
			{
				text = ((Object)parent).name + "/" + text;
				parent = parent.parent;
			}
		}
		catch
		{
		}
		string text2 = "";
		try
		{
			text2 = string.Join(", ", (from c in ((Component)o).GetComponents<MonoBehaviour>()
				where (Object)(object)c != (Object)null
				select ((object)c).GetType().FullName + " [" + ((object)c).GetType().Assembly.GetName().Name + "]").Distinct().ToArray());
		}
		catch
		{
		}
		return "'" + text + "' id=" + o.NetworkObjectId + " spawned=" + o.IsSpawned + " hash=" + Field(o, "GlobalObjectIdHash") + " prefabHash=" + Field(o, "PrefabGlobalObjectIdHash") + " components: " + text2;
	}
}