Decompiled source of QBZ191 v0.1.7

plugins/QBZ191.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace QBZ191;

[BepInPlugin("cn_xc.QBZ191", "QBZ-191", "0.1.7")]
public class Plugin : BaseUnityPlugin
{
	private Harmony _harmony;

	private static MethodInfo _toggleItem;

	private static MethodInfo _localViewId;

	internal static ManualLogSource Log;

	private static ConfigEntry<bool> _hideBullet;

	private static ConfigEntry<bool> _logPrefabs;

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

	private static int _semiLogCount;

	private void Awake()
	{
		//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Expected O, but got Unknown
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Expected O, but got Unknown
		//IL_012b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		_hideBullet = ((BaseUnityPlugin)this).Config.Bind<bool>("修复", "隐藏黑色小球", true, "开火时的黑色小球其实是子弹模型 bulletPrefab 材质丢失。开启后子弹照常命中,只是看不见。");
		_logPrefabs = ((BaseUnityPlugin)this).Config.Bind<bool>("调试", "输出预制体信息", false, "把 bulletPrefab 的名字与渲染器材质写进日志。");
		try
		{
			_toggleItem = AccessTools.Method(typeof(ItemToggle), "ToggleItem", new Type[2]
			{
				typeof(bool),
				typeof(int)
			}, (Type[])null);
			try
			{
				_localViewId = AccessTools.Method(typeof(SemiFunc), "PhotonViewIDPlayerAvatarLocal", (Type[])null, (Type[])null);
			}
			catch
			{
			}
			MethodInfo methodInfo = AccessTools.Method(typeof(ItemGun), "Shoot", new Type[0], (Type[])null);
			_harmony = new Harmony("cn_xc.QBZ191");
			if (methodInfo != null && _toggleItem != null)
			{
				_harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin).GetMethod("ShootPrefix", BindingFlags.Static | BindingFlags.NonPublic)), new HarmonyMethod(typeof(Plugin).GetMethod("ShootPostfix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null);
				Log.LogInfo((object)"QBZ-191 已加载(半自动 + 黑球修复)。");
			}
			else
			{
				Log.LogWarning((object)"QBZ-191:找不到 ItemGun.Shoot 或 ItemToggle.ToggleItem。");
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("QBZ-191 补丁加载失败: " + ex));
		}
	}

	internal static bool IsOurGun(ItemGun gun)
	{
		try
		{
			if ((Object)(object)gun == (Object)null)
			{
				return false;
			}
			string name = ((Object)((Component)gun).gameObject).name;
			if (string.IsNullOrEmpty(name))
			{
				return false;
			}
			return name.IndexOf("QBZ191", StringComparison.OrdinalIgnoreCase) >= 0 || name.IndexOf("AWP", StringComparison.OrdinalIgnoreCase) >= 0;
		}
		catch
		{
			return false;
		}
	}

	private static void Apply(ItemGun gun)
	{
		try
		{
			int instanceID = ((Object)((Component)gun).gameObject).GetInstanceID();
			bool flag = _applied.Add(instanceID);
			bool flag2 = ((Object)((Component)gun).gameObject).name != null && ((Object)((Component)gun).gameObject).name.IndexOf("AWP", StringComparison.OrdinalIgnoreCase) >= 0;
			if (!_hideBullet.Value || flag2 || (Object)(object)gun.bulletPrefab == (Object)null)
			{
				return;
			}
			GameObject bulletPrefab = gun.bulletPrefab;
			if (flag && _logPrefabs.Value)
			{
				Renderer[] componentsInChildren = bulletPrefab.GetComponentsInChildren<Renderer>(true);
				Log.LogInfo((object)("[预制体] bulletPrefab=" + ((Object)bulletPrefab).name + " 渲染器数=" + componentsInChildren.Length));
				for (int i = 0; i < componentsInChildren.Length && i < 6; i++)
				{
					Material sharedMaterial = componentsInChildren[i].sharedMaterial;
					Log.LogInfo((object)("  [" + i + "] " + ((Object)componentsInChildren[i]).name + " 材质=" + (((Object)(object)sharedMaterial != (Object)null) ? ((Object)sharedMaterial).name : "(null)") + " 着色器=" + (((Object)(object)sharedMaterial != (Object)null && (Object)(object)sharedMaterial.shader != (Object)null) ? ((Object)sharedMaterial.shader).name : "(null)")));
				}
			}
			if (!((Object)bulletPrefab).name.EndsWith("_NoVisual"))
			{
				GameObject val = Object.Instantiate<GameObject>(bulletPrefab);
				((Object)val).name = ((Object)bulletPrefab).name + "_NoVisual";
				Renderer[] componentsInChildren2 = val.GetComponentsInChildren<Renderer>(true);
				for (int j = 0; j < componentsInChildren2.Length; j++)
				{
					componentsInChildren2[j].enabled = false;
				}
				gun.bulletPrefab = val;
				Log.LogInfo((object)("[黑球] bulletPrefab 已换成无渲染副本(原 " + ((Object)bulletPrefab).name + ",隐藏渲染器 " + componentsInChildren2.Length + " 个)"));
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("应用失败:" + ex.Message));
		}
	}

	internal static void ShootPrefix(ItemGun __instance)
	{
		try
		{
			if (IsOurGun(__instance))
			{
				Apply(__instance);
			}
		}
		catch
		{
		}
	}

	internal static void ShootPostfix(ItemGun __instance, bool __result)
	{
		try
		{
			bool flag = IsOurGun(__instance);
			ItemToggle val = (flag ? ((Component)__instance).GetComponent<ItemToggle>() : null);
			if (_semiLogCount < 20)
			{
				_semiLogCount++;
				Log.LogInfo((object)("[半自动诊断] gun=" + (((Object)(object)__instance != (Object)null) ? ((Object)((Component)__instance).gameObject).name : "null") + " 是我们的=" + flag + " 结果=" + __result + " toggle=" + (((Object)(object)val != (Object)null) ? val.toggleState.ToString() : "null") + " 方法=" + ((_toggleItem != null) ? "有" : "缺")));
			}
			if (!flag || !__result)
			{
				return;
			}
			ItemToggle val2 = val;
			if ((Object)(object)val2 == (Object)null || !val2.toggleState)
			{
				return;
			}
			int num = 0;
			try
			{
				if (_localViewId != null)
				{
					num = (int)_localViewId.Invoke(null, null);
				}
			}
			catch
			{
			}
			_toggleItem.Invoke(val2, new object[2] { false, num });
		}
		catch
		{
		}
	}
}