Decompiled source of YourFlagYourShip v1.0.0

BepInEx\plugins\YourFlagYourShip\YourFlagYourShip.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("YourFlagYourShip")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("YourFlagYourShip")]
[assembly: AssemblyTitle("YourFlagYourShip")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace YourFlagYourShip;

[BepInPlugin("com.maxi.yourflagyourship", "YourFlagYourShip", "1.0.0")]
public class YourFlagYourShipPlugin : BasePlugin
{
	public override void Load()
	{
		ClassInjector.RegisterTypeInIl2Cpp<FlagBehaviour>();
		((BasePlugin)this).AddComponent<FlagBehaviour>();
		((BasePlugin)this).Log.LogInfo((object)"YourFlagYourShip loaded! F10 = re-apply custom flag texture");
	}
}
public class FlagBehaviour : MonoBehaviour
{
	private static ManualLogSource _log;

	private Texture2D _customFlag;

	private Texture2D _blackTex;

	private bool _flagLoaded;

	private const string FlagFileName = "flag_custom.png";

	private bool _f10Prev;

	public FlagBehaviour(IntPtr ptr)
		: base(ptr)
	{
	}

	private void Awake()
	{
		_log = Logger.CreateLogSource("YourFlagYourShip");
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	private void Start()
	{
		LoadCustomFlag();
	}

	private void Update()
	{
		Keyboard current = Keyboard.current;
		if (current != null)
		{
			bool isPressed = ((ButtonControl)current[(Key)103]).isPressed;
			if (isPressed && !_f10Prev)
			{
				ReplaceFlag();
			}
			_f10Prev = isPressed;
		}
	}

	private void LoadCustomFlag()
	{
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Expected O, but got Unknown
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		_log.LogInfo((object)"LoadCustomFlag called, searching in PluginPath...");
		try
		{
			string pluginPath = Paths.PluginPath;
			_log.LogInfo((object)("Plugin dir: [" + pluginPath + "]"));
			string text = Path.Combine(pluginPath, "flag_custom.png");
			if (!File.Exists(text))
			{
				_log.LogWarning((object)("flag_custom.png not found: [" + text + "]"));
				return;
			}
			_log.LogInfo((object)("Loading flag via System.Drawing: [" + text + "]"));
			Bitmap val = new Bitmap(text);
			try
			{
				int width = ((Image)val).Width;
				int height = ((Image)val).Height;
				Texture2D val2 = new Texture2D(width, height, (TextureFormat)4, false);
				for (int i = 0; i < height; i++)
				{
					for (int j = 0; j < width; j++)
					{
						Color pixel = val.GetPixel(j, height - 1 - i);
						val2.SetPixel(j, i, new Color((float)(int)pixel.R / 255f, (float)(int)pixel.G / 255f, (float)(int)pixel.B / 255f, (float)(int)pixel.A / 255f));
					}
				}
				val2.Apply();
				((Object)val2).name = "flag_custom";
				_customFlag = val2;
				_flagLoaded = true;
				_log.LogInfo((object)("Flag loaded OK: " + width + "x" + height));
				ReplaceFlag();
			}
			finally
			{
				((IDisposable)val)?.Dispose();
			}
		}
		catch (Exception ex)
		{
			_log.LogError((object)("LoadCustomFlag error: " + ex.Message));
		}
	}

	private Texture2D GetBlackTex()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Expected O, but got Unknown
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_blackTex != (Object)null)
		{
			return _blackTex;
		}
		_blackTex = new Texture2D(4, 4, (TextureFormat)4, false);
		Color32[] array = (Color32[])(object)new Color32[16];
		for (int i = 0; i < 16; i++)
		{
			array[i] = new Color32((byte)0, (byte)0, (byte)0, byte.MaxValue);
		}
		_blackTex.SetPixels32(Il2CppStructArray<Color32>.op_Implicit(array));
		_blackTex.Apply();
		return _blackTex;
	}

	private void ReplaceFlag()
	{
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		if (!_flagLoaded || (Object)(object)_customFlag == (Object)null)
		{
			return;
		}
		int num = 0;
		foreach (Material item in Resources.FindObjectsOfTypeAll<Material>())
		{
			if ((Object)(object)item == (Object)null || !((Object)item).name.Contains("flag_fabric_mat"))
			{
				continue;
			}
			try
			{
				_log.LogInfo((object)("Found flag_fabric_mat: [" + ((Object)item).name + "] shader=" + ((Object)item.shader).name));
				try
				{
					if (item.HasProperty("_TintMask"))
					{
						item.SetTexture("_TintMask", (Texture)(object)GetBlackTex());
						_log.LogInfo((object)"TintMask cleared");
					}
				}
				catch (Exception ex)
				{
					_log.LogError((object)("TintMask clear FAILED: " + ex.Message));
				}
				try
				{
					item.SetTexture("_BaseColorMap", (Texture)(object)_customFlag);
					item.SetTextureScale("_BaseColorMap", Vector2.one);
					item.SetTextureOffset("_BaseColorMap", Vector2.zero);
					_log.LogInfo((object)"BaseColorMap set OK");
				}
				catch (Exception ex2)
				{
					_log.LogError((object)("BaseColorMap set FAILED: " + ex2.Message));
				}
				try
				{
					if (item.HasProperty("_BaseColor"))
					{
						item.SetColor("_BaseColor", Color.white);
					}
				}
				catch (Exception ex3)
				{
					_log.LogError((object)("BaseColor reset FAILED: " + ex3.Message));
				}
				num++;
			}
			catch (Exception ex4)
			{
				_log.LogWarning((object)("mat loop: " + ex4.Message));
			}
		}
		if (num > 0)
		{
			_log.LogInfo((object)("Flag replaced (" + num + " mats)"));
		}
		else
		{
			_log.LogWarning((object)"ReplaceFlag: flag_fabric_mat not loaded yet — walk near a Saborian flag and press F10");
		}
	}
}