ZebaiGuns.dll

Decompiled a day ago
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Photon.Pun;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ZebaiMinigun;

[BepInPlugin("com.zebai.minigun", "Zebai Minigun", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class MinigunPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	private static AssetBundle _bundle;

	private static Item _gunTemplateItem;

	private static GameObject _modelAsset;

	private static ItemGun _spawnedGun;

	private float _shootTimer;

	private bool _ready = false;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Harmony val = new Harmony("com.zebai.minigun");
		val.PatchAll();
		Log.LogInfo((object)"Minigun mod loaded");
	}

	private void Update()
	{
		if (!_ready)
		{
			if (Items.AllItems.Count > 0)
			{
				Init();
			}
			return;
		}
		if (Input.GetKeyDown((KeyCode)288))
		{
			SpawnMinigun();
		}
		if ((Object)(object)_spawnedGun != (Object)null && Input.GetKey((KeyCode)101))
		{
			_shootTimer -= Time.deltaTime;
			if (_shootTimer <= 0f)
			{
				((Component)_spawnedGun).SendMessage("Shoot", (SendMessageOptions)1);
				_shootTimer = _spawnedGun.shootCooldown;
			}
		}
		else
		{
			_shootTimer = 0f;
		}
	}

	private void Init()
	{
		try
		{
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "minigun.bundle");
			_bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)_bundle == (Object)null)
			{
				Log.LogError((object)"Failed to load bundle");
				return;
			}
			foreach (Item allItem in Items.AllItems)
			{
				FieldInfo field = typeof(Item).GetField("prefab");
				object value = field.GetValue(allItem);
				if (value == null)
				{
					continue;
				}
				PropertyInfo property = value.GetType().GetProperty("Prefab", BindingFlags.Instance | BindingFlags.Public);
				if (property != null)
				{
					object? value2 = property.GetValue(value, null);
					GameObject val = (GameObject)((value2 is GameObject) ? value2 : null);
					if ((Object)(object)val != (Object)null && (Object)(object)val.GetComponent<ItemGun>() != (Object)null)
					{
						_gunTemplateItem = allItem;
						break;
					}
				}
			}
			if ((Object)(object)_gunTemplateItem == (Object)null)
			{
				Log.LogError((object)"No gun found!");
				return;
			}
			_modelAsset = _bundle.LoadAsset<GameObject>("Assets/AssetBundles/M249.fbx");
			if ((Object)(object)_modelAsset == (Object)null)
			{
				Log.LogError((object)"Failed to load model");
				return;
			}
			_ready = true;
			Log.LogInfo((object)"Ready! Press F7 to spawn minigun.");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Init error: " + ex));
		}
	}

	private void SpawnMinigun()
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Transform transform = ((Component)Camera.main).transform;
			Vector3 val = transform.position + transform.forward * 2f;
			GameObject val2 = Items.SpawnItem(_gunTemplateItem, val, Quaternion.identity);
			((Object)val2).name = "M249";
			ItemGun component = val2.GetComponent<ItemGun>();
			if ((Object)(object)component != (Object)null)
			{
				component.shootCooldown = 0.13f;
				component.gunRecoilForce = 2f;
				component.gunRandomSpread = 0.05f;
				component.batteryDrain = 0.01f;
				component.misfirePercentageChange = 0f;
				component.hasBuildUp = false;
				component.hasOneShot = false;
				_spawnedGun = component;
			}
			ItemBattery component2 = val2.GetComponent<ItemBattery>();
			if ((Object)(object)component2 != (Object)null)
			{
				FieldInfo field = typeof(ItemBattery).GetField("batteryLife", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				((MonoBehaviour)this).StartCoroutine(FixBattery(component2, field));
			}
			Renderer[] componentsInChildren = val2.GetComponentsInChildren<Renderer>();
			Renderer[] array = componentsInChildren;
			foreach (Renderer val3 in array)
			{
				val3.enabled = false;
			}
			GameObject val4 = Object.Instantiate<GameObject>(_modelAsset, val2.transform);
			val4.transform.localPosition = new Vector3(0f, 0f, 0.5f);
			val4.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
			val4.transform.localScale = new Vector3(2.2f, 2.2f, 2.2f);
			Log.LogInfo((object)"Minigun spawned!");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Spawn error: " + ex));
		}
	}

	private IEnumerator FixBattery(ItemBattery battery, FieldInfo lifeField)
	{
		yield return (object)new WaitForSeconds(1f);
		if ((Object)(object)battery != (Object)null && lifeField != null)
		{
			lifeField.SetValue(battery, 1000f);
		}
	}
}
[HarmonyPatch(typeof(ItemGun), "Shoot")]
internal class ItemGun_Shoot_Patch
{
	private static bool Prefix(ItemGun __instance)
	{
		try
		{
			PhotonView component = ((Component)__instance).GetComponent<PhotonView>();
			if ((Object)(object)component != (Object)null && !component.IsMine)
			{
				MethodInfo method = typeof(ItemGun).GetMethod("ShootRPC", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method != null)
				{
					object[] parameters = new object[1];
					method.Invoke(__instance, parameters);
				}
				return false;
			}
		}
		catch (Exception ex)
		{
			MinigunPlugin.Log.LogError((object)("Shoot patch error: " + ex));
		}
		return true;
	}
}