Decompiled source of ShotgunImage v1.0.3

BepInEx/plugins/ShotgunImage.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using ComputerysModdingUtilities;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: StraftatMod(true)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("TestPlugin")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TestPlugin")]
[assembly: AssemblyTitle("TestPlugin")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.unconscious.shotgunimage", "Shotgun Image Plugin", "1.0.0")]
public class TestPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Shotgun))]
	[HarmonyPatch("Fire")]
	public static class Shotgun_Fire_Patch
	{
		private static bool Prefix(Shotgun __instance)
		{
			int[,] array = maps[mapPos];
			int num = mapBulletCounts[mapPos];
			Type type = ((object)__instance).GetType();
			FieldInfo field = type.GetField("hitPosition", BindingFlags.Instance | BindingFlags.NonPublic);
			FieldInfo field2 = type.GetField("bulletAmount", BindingFlags.Instance | BindingFlags.NonPublic);
			field2.SetValue(__instance, num);
			int num2 = (int)field2.GetValue(__instance);
			Vector3[] value = (Vector3[])(object)new Vector3[num2];
			field.SetValue(__instance, value);
			return true;
		}
	}

	[HarmonyPatch(typeof(Shotgun), "Fire")]
	private static class ShotgunPatch
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Expected O, but got Unknown
			List<CodeInstruction> codes = instructions.ToList();
			int num = codes.FindIndex((CodeInstruction c) => c.opcode == OpCodes.Ldc_I4_0 && codes[codes.IndexOf(c) + 1].opcode == OpCodes.Stloc_0);
			if (num < 0)
			{
				Debug.LogError((object)"Start of spread loop not found!");
				return codes;
			}
			int num2 = codes.FindIndex(num, (CodeInstruction c) => c.opcode.FlowControl == FlowControl.Cond_Branch && c.operand is Label);
			if (num2 < 0)
			{
				Debug.LogError((object)"End of spread loop not found!");
				return codes;
			}
			List<CodeInstruction> list = new List<CodeInstruction>();
			FieldInfo fieldInfo = AccessTools.Field(typeof(Shotgun), "hitPosition");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(Shotgun), "bulletAmount");
			MethodInfo methodInfo = AccessTools.Method(typeof(ShotgunPatch), "FillHitPositions", (Type[])null, (Type[])null);
			list.Add(new CodeInstruction(OpCodes.Ldarg_0, (object)null));
			list.Add(new CodeInstruction(OpCodes.Call, (object)methodInfo));
			codes.InsertRange(num2 + 1, list);
			return codes;
		}

		public static void FillHitPositions(Shotgun __instance)
		{
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			int[,] array = maps[mapPos];
			mapPos = (mapPos + 1) % maps.Count;
			Type type = ((object)__instance).GetType();
			FieldInfo field = type.GetField("hitPosition", BindingFlags.Instance | BindingFlags.NonPublic);
			FieldInfo field2 = type.GetField("bulletAmount", BindingFlags.Instance | BindingFlags.NonPublic);
			FieldInfo field3 = type.GetField("spread", BindingFlags.Instance | BindingFlags.NonPublic);
			float num = (float)field3.GetValue(__instance);
			int num2 = (int)field2.GetValue(__instance);
			Vector3[] array2 = (Vector3[])(object)new Vector3[num2];
			int[,] array3 = array;
			int length = array3.GetLength(1);
			int length2 = array3.GetLength(0);
			List<Vector3> list = new List<Vector3>();
			for (int i = 0; i < length2; i++)
			{
				for (int j = 0; j < length; j++)
				{
					if (array3[i, j] == 1)
					{
						float num3 = (float)j / (float)(length - 1) * 2f - 1f;
						float num4 = (float)i / (float)(length2 - 1) * 2f - 1f;
						Vector3 item = ((Component)__instance).transform.right * num3 * num + ((Component)__instance).transform.up * num4 * num;
						list.Add(item);
					}
				}
			}
			for (int k = 0; k < num2; k++)
			{
				array2[k] = list[k % list.Count];
			}
			field.SetValue(__instance, array2);
		}
	}

	private static List<int[,]> maps = new List<int[,]>();

	private static List<int> mapBulletCounts = new List<int>();

	private static int mapPos = 0;

	private static int testAmount = 4;

	private void Awake()
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Expected O, but got Unknown
		string bepInExRootPath = Paths.BepInExRootPath;
		string text = Path.Combine(bepInExRootPath, "config/Bitmaps");
		string[] files = Directory.GetFiles(text, "*.png", SearchOption.TopDirectoryOnly);
		string text2 = Path.Combine(text, "image.png");
		List<string> list = new List<string>(files);
		foreach (string item2 in list)
		{
			byte[] array = File.ReadAllBytes(item2);
			Texture2D val = new Texture2D(2, 2);
			if (!ImageConversion.LoadImage(val, array))
			{
				Debug.LogWarning((object)("Failed to load: " + item2));
				continue;
			}
			int[,] array2 = ToBinaryArray(val);
			int item = CountBlackPixels(array2);
			maps.Add(array2);
			mapBulletCounts.Add(item);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Loaded image: " + item2 + " with " + item + " filled pixels"));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Shotgun Image 1.0 is loaded!");
		Harmony val2 = new Harmony("com.unconscious.testplugin");
		val2.PatchAll();
	}

	public static int CountBlackPixels(int[,] map)
	{
		if (map == null)
		{
			return 0;
		}
		int length = map.GetLength(0);
		int length2 = map.GetLength(1);
		int num = 0;
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < length2; j++)
			{
				if (map[i, j] == 1)
				{
					num++;
				}
			}
		}
		return num;
	}

	public static int[,] ToBinaryArray(Texture2D tex, float threshold = 0.5f)
	{
		//IL_0036: 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_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		int width = ((Texture)tex).width;
		int height = ((Texture)tex).height;
		int[,] array = new int[height, width];
		Color32[] pixels = tex.GetPixels32();
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				int num = i * width + j;
				Color32 val = pixels[num];
				float num2 = (float)(val.r + val.g + val.b) / 3f / 255f;
				array[i, j] = ((!(num2 >= threshold)) ? 1 : 0);
			}
		}
		return array;
	}
}