Decompiled source of ShotgunKinSwapper by ApacheThunder but red v1.0.0

ShotgunKinSwapper.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using MonoMod.RuntimeDetour;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ShotgunKinSwapper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ShotgunKinSwapper")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ee470e43-503f-44ee-9d59-1903178e1126")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
namespace ShotgunKinSwapper;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("ApacheThunder.etg.ShotgunKinSwapper", "ShotgunKinSwapper", "1.1.0")]
public class ShotgunKinSwapper : BaseUnityPlugin
{
	public const string GUID = "ApacheThunder.etg.ShotgunKinSwapper";

	public const string ModName = "ShotgunKinSwapper";

	public const string VERSION = "1.1.0";

	public static tk2dSpriteCollectionData shotgunkinskinCollection;

	public static Hook GameManagerHook;

	public void Start()
	{
		ETGModMainBehaviour.WaitForGameManagerStart((Action<GameManager>)GMStart);
	}

	public void GMStart(GameManager gameManager)
	{
		Assets.SetupSpritesFromAssembly(Assembly.GetExecutingAssembly(), "ShotgunKinSwapper/Sprites");
		DoFoyerChecks();
	}

	private void GameManager_Awake(Action<GameManager> orig, GameManager self)
	{
		orig(self);
		DoFoyerChecks();
	}

	public void DoFoyerChecks()
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		new GameObject("ShotgunMod Foyer Checker", new Type[1] { typeof(ShotGunEnableController) });
	}
}
public class ShotGunEnableController : BraveBehaviour
{
	private bool m_HasTriggerd;

	public ShotGunEnableController()
	{
		m_HasTriggerd = false;
	}

	public void Awake()
	{
	}

	public void Start()
	{
	}

	public void Update()
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		if (m_HasTriggerd || (Foyer.DoIntroSequence && Foyer.DoMainMenu))
		{
			return;
		}
		if (ShotgunKinSwapper.GameManagerHook == null)
		{
			ShotgunKinSwapper.GameManagerHook = new Hook((MethodBase)typeof(GameManager).GetMethod("Awake", BindingFlags.Instance | BindingFlags.NonPublic), typeof(ShotgunKinSwapper).GetMethod("GameManager_Awake", BindingFlags.Instance | BindingFlags.NonPublic), (object)typeof(GameManager));
		}
		CharacterCostumeSwapper[] array = Object.FindObjectsOfType<CharacterCostumeSwapper>();
		if (array == null || array.Length == 0)
		{
			return;
		}
		CharacterCostumeSwapper val = null;
		CharacterCostumeSwapper[] array2 = array;
		foreach (CharacterCostumeSwapper val2 in array2)
		{
			object obj;
			if (val2 == null)
			{
				obj = null;
			}
			else
			{
				tk2dSpriteAnimation targetLibrary = val2.TargetLibrary;
				obj = ((targetLibrary != null) ? ((Object)targetLibrary).name : null);
			}
			if ((string?)obj == "Playable_Shotgun_Man_Swap_Animation")
			{
				ShotgunKinSwapper.shotgunkinskinCollection = val2.TargetLibrary.clips[0].frames[0].spriteCollection;
				val = val2;
				break;
			}
		}
		if (!Object.op_Implicit((Object)(object)val))
		{
			return;
		}
		if (GameStatsManager.Instance.GetFlag((GungeonFlags)5005) && GameStatsManager.Instance.GetCharacterSpecificFlag(val.TargetCharacter, (CharacterSpecificGungeonFlags)1000))
		{
			typeof(CharacterCostumeSwapper).GetField("m_active", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(val, true);
			if (Object.op_Implicit((Object)(object)ShotgunKinSwapper.shotgunkinskinCollection))
			{
				Tools.ApplyCustomTexture(ShotgunKinSwapper.shotgunkinskinCollection, Tools.GetTextureFromResource("Textures\\Playable_Shotgun_Man_Swap.png"));
			}
			((BraveBehaviour)val.AlternateCostumeSprite).renderer.enabled = true;
			((BraveBehaviour)val.CostumeSprite).renderer.enabled = false;
		}
		m_HasTriggerd = true;
		Object.Destroy((Object)(object)((Component)this).gameObject);
	}

	protected override void OnDestroy()
	{
		((BraveBehaviour)this).OnDestroy();
	}
}
public class Tools
{
	public static void ApplyCustomTexture(tk2dSpriteCollectionData targetSpriteCollection, Texture2D newTexture = null, List<Texture2D> spriteList = null, Shader overrideShader = null)
	{
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Expected O, but got Unknown
		//IL_0165: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0211: Unknown result type (might be due to invalid IL or missing references)
		//IL_0216: Unknown result type (might be due to invalid IL or missing references)
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_0229: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_024c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Expected O, but got Unknown
		if (!((Object)(object)targetSpriteCollection != (Object)null))
		{
			return;
		}
		if ((Object)(object)newTexture != (Object)null)
		{
			Material[] materials = targetSpriteCollection.materials;
			Material[] array = (Material[])(object)new Material[materials.Length];
			if (materials == null)
			{
				return;
			}
			for (int i = 0; i < materials.Length; i++)
			{
				array[i] = materials[i].Copy(newTexture);
				if (Object.op_Implicit((Object)(object)overrideShader))
				{
					array[i].shader = overrideShader;
				}
			}
			targetSpriteCollection.materials = array;
			Material[] materials2 = targetSpriteCollection.materials;
			foreach (Material val in materials2)
			{
				tk2dSpriteDefinition[] spriteDefinitions = targetSpriteCollection.spriteDefinitions;
				foreach (tk2dSpriteDefinition val2 in spriteDefinitions)
				{
					if ((Object)(object)val != (Object)null && ((Object)val2.material).name.Equals(((Object)val).name))
					{
						val2.material = val;
						val2.materialInst = new Material(val);
						if (Object.op_Implicit((Object)(object)overrideShader))
						{
							val2.material.shader = overrideShader;
							val2.materialInst.shader = overrideShader;
						}
					}
				}
			}
		}
		else
		{
			if (spriteList == null)
			{
				return;
			}
			RuntimeAtlasPage val3 = new RuntimeAtlasPage(0, 0, (TextureFormat)4, 2);
			foreach (Texture2D sprite in spriteList)
			{
				float num = (float)((Texture)sprite).width / 16f;
				float num2 = (float)((Texture)sprite).height / 16f;
				tk2dSpriteDefinition spriteDefinition = targetSpriteCollection.GetSpriteDefinition(((Object)sprite).name);
				if (spriteDefinition != null)
				{
					if (spriteDefinition.boundsDataCenter != Vector3.zero)
					{
						RuntimeAtlasSegment val4 = val3.Pack(sprite, false);
						spriteDefinition.materialInst.mainTexture = (Texture)(object)val4.texture;
						spriteDefinition.uvs = val4.uvs;
						spriteDefinition.extractRegion = true;
						spriteDefinition.position0 = Vector3.zero;
						spriteDefinition.position1 = new Vector3(num, 0f, 0f);
						spriteDefinition.position2 = new Vector3(0f, num2, 0f);
						spriteDefinition.position3 = new Vector3(num, num2, 0f);
						spriteDefinition.boundsDataCenter = Vector2.op_Implicit(new Vector2(num / 2f, num2 / 2f));
						spriteDefinition.untrimmedBoundsDataCenter = spriteDefinition.boundsDataCenter;
						spriteDefinition.boundsDataExtents = Vector2.op_Implicit(new Vector2(num, num2));
						spriteDefinition.untrimmedBoundsDataExtents = spriteDefinition.boundsDataExtents;
					}
					else
					{
						ETGMod.ReplaceTexture(spriteDefinition, sprite, true);
					}
				}
			}
			val3.Apply();
		}
	}

	public static Texture2D BytesToTexture(byte[] bytes, string resourceName)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected O, but got Unknown
		Texture2D val = new Texture2D(1, 1, (TextureFormat)4, false);
		ImageConversion.LoadImage(val, bytes);
		((Texture)val).filterMode = (FilterMode)0;
		((Object)val).name = resourceName;
		val.Apply();
		return val;
	}

	public static Texture2D GetTextureFromResource(string texturePath, int Width = 1, int Height = 1)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Expected O, but got Unknown
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		string text = texturePath;
		text = text.Replace("/", ".");
		text = text.Replace("\\", ".");
		byte[] array = ExtractEmbeddedResource(string.Format("{0}.", "ShotgunKinSwapper") + text);
		if (array == null)
		{
			ETGModConsole.Log((object)("No bytes found in " + text), false);
			return null;
		}
		Texture2D val = new Texture2D(Width, Height, (TextureFormat)4, false);
		ImageConversion.LoadImage(val, array);
		((Texture)val).filterMode = (FilterMode)0;
		string text2 = text.Substring(0, text.LastIndexOf('.'));
		if (text2.LastIndexOf('.') >= 0)
		{
			text2 = text2.Substring(text2.LastIndexOf('.') + 1);
		}
		((Object)val).name = text2;
		return val;
	}

	public static byte[] ExtractEmbeddedResource(string filename)
	{
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		executingAssembly.GetManifestResourceNames();
		using Stream stream = executingAssembly.GetManifestResourceStream(filename);
		if (stream == null)
		{
			return null;
		}
		byte[] array = new byte[stream.Length];
		stream.Read(array, 0, array.Length);
		return array;
	}
}
public static class ShotgunKinExtensions
{
	public static Material Copy(this Material orig, Texture2D textureOverride = null, Shader shaderOverride = null)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: 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_0025: 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_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: 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_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Expected O, but got Unknown
		Material val = new Material(orig.shader)
		{
			name = ((Object)orig).name,
			shaderKeywords = orig.shaderKeywords,
			globalIlluminationFlags = orig.globalIlluminationFlags,
			enableInstancing = orig.enableInstancing,
			doubleSidedGI = orig.doubleSidedGI,
			mainTextureOffset = orig.mainTextureOffset,
			mainTextureScale = orig.mainTextureScale,
			renderQueue = orig.renderQueue,
			color = orig.color,
			hideFlags = ((Object)orig).hideFlags
		};
		if ((Object)(object)textureOverride != (Object)null)
		{
			val.mainTexture = (Texture)(object)textureOverride;
		}
		else
		{
			val.mainTexture = orig.mainTexture;
		}
		if ((Object)(object)shaderOverride != (Object)null)
		{
			val.shader = shaderOverride;
		}
		else
		{
			val.shader = orig.shader;
		}
		return val;
	}

	public static tk2dSpriteDefinition Copy(this tk2dSpriteDefinition orig)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Expected O, but got Unknown
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: 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_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: 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_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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Expected O, but got Unknown
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: 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_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: 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_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: 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_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Expected O, but got Unknown
		tk2dSpriteDefinition val = new tk2dSpriteDefinition();
		val.boundsDataCenter = orig.boundsDataCenter;
		val.boundsDataExtents = orig.boundsDataExtents;
		val.colliderConvex = orig.colliderConvex;
		val.colliderSmoothSphereCollisions = orig.colliderSmoothSphereCollisions;
		val.colliderType = orig.colliderType;
		val.colliderVertices = orig.colliderVertices;
		val.collisionLayer = orig.collisionLayer;
		val.complexGeometry = orig.complexGeometry;
		val.extractRegion = orig.extractRegion;
		val.flipped = orig.flipped;
		val.indices = orig.indices;
		if ((Object)(object)orig.material != (Object)null)
		{
			val.material = new Material(orig.material);
		}
		val.materialId = orig.materialId;
		if ((Object)(object)orig.materialInst != (Object)null)
		{
			val.materialInst = new Material(orig.materialInst);
		}
		val.metadata = orig.metadata;
		val.name = orig.name;
		val.normals = orig.normals;
		val.physicsEngine = orig.physicsEngine;
		val.position0 = orig.position0;
		val.position1 = orig.position1;
		val.position2 = orig.position2;
		val.position3 = orig.position3;
		val.regionH = orig.regionH;
		val.regionW = orig.regionW;
		val.regionX = orig.regionX;
		val.regionY = orig.regionY;
		val.tangents = orig.tangents;
		val.texelSize = orig.texelSize;
		val.untrimmedBoundsDataCenter = orig.untrimmedBoundsDataCenter;
		val.untrimmedBoundsDataExtents = orig.untrimmedBoundsDataExtents;
		val.uvs = orig.uvs;
		return val;
	}
}