Decompiled source of FlagMaker v1.2.3

FlagMaker.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyCompany("FlagMaker")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FlagMaker")]
[assembly: AssemblyTitle("FlagMaker")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace FlagMaker
{
	public struct ColorUvPair
	{
		public Color32 color;

		public Vector2 uv;
	}
	public class TextureLoader : MonoBehaviour
	{
		public static TextureLoader Instance;

		private void Awake()
		{
			Instance = this;
		}

		public void LoadTextureFromPath(string filePath, Action<Texture2D> callback)
		{
			if (!Path.IsPathRooted(filePath))
			{
				filePath = Path.Combine(Directory.GetCurrentDirectory(), filePath);
			}
			((MonoBehaviour)this).StartCoroutine(LoadTextureCoroutine(filePath, callback));
		}

		private IEnumerator LoadTextureCoroutine(string filePath, Action<Texture2D> callback)
		{
			WWW www = new WWW("file://" + filePath);
			yield return www;
			if (!string.IsNullOrEmpty(www.error))
			{
				Debug.LogError((object)("[FlagMaker] WWW Error loading texture: " + www.error));
				callback(null);
			}
			else
			{
				Texture2D texture = new Texture2D(2, 2);
				www.LoadImageIntoTexture(texture);
				callback(texture);
			}
		}
	}
	[BepInPlugin("com.Dirtishurt.FlagMaker", "FlagMaker", "1.2.3")]
	public class ImageProcessorPlugin : BaseUnityPlugin
	{
		private const string SavedFlagsFolderName = "FlagMaker_SavedFlags";

		private const int TargetWidth = 100;

		private const int TargetHeight = 66;

		private ConfigEntry<string> sourceImagePath;

		private ConfigEntry<KeyCode> toggleKey;

		private string PaletteFileName;

		private string savedFlagsPath;

		private float brightness = 0f;

		private float sharpen = 0f;

		private float contrast = 1f;

		private int noise = 1;

		private bool showWindow = false;

		private Rect windowRect = new Rect(20f, 20f, 450f, 600f);

		private string statusMessage = "Ready.";

		private bool showFileBrowser = false;

		private Rect fileBrowserRect = new Rect(40f, 40f, 600f, 500f);

		private string currentBrowserPath;

		private Action<string> onFileSelectedCallback;

		private Vector2 fileBrowserScrollPosition;

		private Vector2 savedFlagsScrollPosition;

		private string searchQuery = "";

		private bool stylesInitialized = false;

		private GUIStyle windowStyle;

		private GUIStyle labelStyle;

		private GUIStyle buttonStyle;

		private GUIStyle headerLabelStyle;

		private GUIStyle textFieldStyle;

		private List<ColorUvPair> colorMap;

		private List<ColorUvPair> grayMap;

		private List<string> savedFlagFiles = new List<string>();

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			GameObject val = new GameObject("FlagMaker_TextureLoader");
			val.AddComponent<TextureLoader>();
			Object.DontDestroyOnLoad((Object)(object)val);
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			Debug.Log((object)directoryName);
			PaletteFileName = Path.Combine(directoryName, "palette.png");
			savedFlagsPath = Path.Combine(directoryName, "FlagMaker_SavedFlags");
			sourceImagePath = ((BaseUnityPlugin)this).Config.Bind<string>("1. File Paths", "SourceImagePath", "source.png", "Path to the source PNG image.");
			toggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("2. Hotkey", "ToggleWindowKey", (KeyCode)291, "Key to press to show/hide the processor window.");
			currentBrowserPath = Directory.GetCurrentDirectory();
			if (!Directory.Exists(savedFlagsPath))
			{
				Directory.CreateDirectory(savedFlagsPath);
			}
			RefreshSavedFlags();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"FlagMaker Plugin loaded!");
		}

		private void Update()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			if (Input.GetKeyDown(toggleKey.Value))
			{
				showWindow = !showWindow;
				if (showWindow)
				{
					stylesInitialized = false;
				}
				if (!showWindow)
				{
					showFileBrowser = false;
				}
			}
		}

		private void OnGUI()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			if (!stylesInitialized)
			{
				InitializeStyles();
			}
			if (showWindow)
			{
				windowRect = GUILayout.Window(12345, windowRect, new WindowFunction(DrawWindow), "Image Processor", windowStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			}
			if (showFileBrowser)
			{
				fileBrowserRect = GUILayout.Window(67890, fileBrowserRect, new WindowFunction(DrawFileBrowser), "Select a PNG File", windowStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			}
		}

		private void DrawWindow(int windowID)
		{
			//IL_0344: Unknown result type (might be due to invalid IL or missing references)
			//IL_035c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0361: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.Label("Press F10 to hide this window.", labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Status: " + statusMessage, labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Space(10f);
			GUILayout.Label("1. Create New Flag", headerLabelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Source PNG: " + Path.GetFileName(sourceImagePath.Value), labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(300f) });
			if (GUILayout.Button("Select...", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
			{
				OpenFileBrowser(delegate(string path)
				{
					sourceImagePath.Value = path;
					((BaseUnityPlugin)this).Config.Save();
				});
			}
			GUILayout.EndHorizontal();
			GUILayout.Space(15f);
			GUILayout.Label("2. Adjustments", headerLabelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Brightness: " + brightness.ToString("F2"), labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			brightness = GUILayout.HorizontalSlider(brightness, -1f, 1f, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Sharpen: " + sharpen.ToString("F2"), labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			sharpen = GUILayout.HorizontalSlider(sharpen, 0f, 2f, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Contrast: " + contrast.ToString("F2"), labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			contrast = GUILayout.HorizontalSlider(contrast, 1f, 10f, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Noise Reduction: " + noise, labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			noise = (int)GUILayout.HorizontalSlider((float)noise, 1f, 9f, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			if (noise % 2 == 0)
			{
				noise++;
			}
			if (GUILayout.Button("Generate and Set Flag", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
			{
				statusMessage = "Loading images...";
				TextureLoader.Instance.LoadTextureFromPath(sourceImagePath.Value, OnSourceTextureLoaded);
			}
			GUILayout.Space(15f);
			GUILayout.Label("3. Saved Flags", headerLabelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
			if (GUILayout.Button("Refresh List", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
			{
				RefreshSavedFlags();
			}
			if (GUILayout.Button("Open Folder", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
			{
				try
				{
					Process.Start(savedFlagsPath);
				}
				catch (Exception ex)
				{
					statusMessage = "Error opening folder.";
					((BaseUnityPlugin)this).Logger.LogError((object)("Could not open saved flags folder: " + ex.Message));
				}
			}
			GUILayout.EndHorizontal();
			savedFlagsScrollPosition = GUILayout.BeginScrollView(savedFlagsScrollPosition, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(150f) });
			if (savedFlagFiles.Count == 0)
			{
				GUILayout.Label("No saved flags found.", labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			}
			else
			{
				foreach (string savedFlagFile in savedFlagFiles)
				{
					if (GUILayout.Button(Path.GetFileNameWithoutExtension(savedFlagFile), buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
					{
						SetSavedFlag(savedFlagFile);
					}
				}
			}
			GUILayout.EndScrollView();
			GUI.DragWindow();
		}

		private void DrawFileBrowser(int windowID)
		{
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
			GUILayout.Label("Search:", labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(50f) });
			searchQuery = GUILayout.TextField(searchQuery, textFieldStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			if (GUILayout.Button("Close", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(60f) }))
			{
				showFileBrowser = false;
				searchQuery = "";
				return;
			}
			GUILayout.EndHorizontal();
			GUILayout.Label("Current Path: " + currentBrowserPath, labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			fileBrowserScrollPosition = GUILayout.BeginScrollView(fileBrowserScrollPosition, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			try
			{
				DirectoryInfo parent = Directory.GetParent(currentBrowserPath);
				if (parent != null && GUILayout.Button(".. [Up]", buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
				{
					currentBrowserPath = parent.FullName;
					searchQuery = "";
				}
				string[] directories = Directory.GetDirectories(currentBrowserPath);
				string[] array = directories;
				foreach (string path in array)
				{
					string fileName = Path.GetFileName(path);
					if ((string.IsNullOrEmpty(searchQuery) || fileName.ToLower().Contains(searchQuery.ToLower())) && GUILayout.Button("[D] " + fileName, buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
					{
						currentBrowserPath = path;
						searchQuery = "";
					}
				}
				string[] extensions = new string[4] { ".png", ".jpg", ".jpeg", ".bmp" };
				string[] array2 = (from f in Directory.GetFiles(currentBrowserPath)
					where extensions.Contains(Path.GetExtension(f).ToLower())
					select f).ToArray();
				string[] array3 = array2;
				foreach (string text in array3)
				{
					string fileName2 = Path.GetFileName(text);
					if ((string.IsNullOrEmpty(searchQuery) || fileName2.ToLower().Contains(searchQuery.ToLower())) && GUILayout.Button("[F] " + fileName2, buttonStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]))
					{
						onFileSelectedCallback(text);
						showFileBrowser = false;
						searchQuery = "";
					}
				}
			}
			catch (Exception ex)
			{
				GUILayout.Label("Error reading directory: " + ex.Message, labelStyle, (GUILayoutOption[])(object)new GUILayoutOption[0]);
			}
			GUILayout.EndScrollView();
			GUI.DragWindow();
		}

		private Texture2D MakeTex(Color col)
		{
			//IL_000a: 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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			Color[] pixels = (Color[])(object)new Color[1] { col };
			Texture2D val = new Texture2D(1, 1);
			val.SetPixels(pixels);
			val.Apply();
			Object.DontDestroyOnLoad((Object)(object)val);
			return val;
		}

		private void InitializeStyles()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			//IL_00cd: 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_00fa: Expected O, but got Unknown
			//IL_0114: 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_0134: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: 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_01fb: 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_021b: Expected O, but got Unknown
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			windowStyle = new GUIStyle(GUI.skin.window);
			windowStyle.normal.background = MakeTex(new Color(0.1f, 0.12f, 0.15f, 0.95f));
			windowStyle.onNormal.background = windowStyle.normal.background;
			windowStyle.normal.textColor = new Color(0.9f, 0.9f, 0.9f);
			windowStyle.onNormal.textColor = windowStyle.normal.textColor;
			headerLabelStyle = new GUIStyle(GUI.skin.label);
			headerLabelStyle.normal.textColor = Color.white;
			headerLabelStyle.fontStyle = (FontStyle)1;
			labelStyle = new GUIStyle(GUI.skin.label);
			labelStyle.normal.textColor = new Color(0.8f, 0.8f, 0.8f);
			buttonStyle = new GUIStyle(GUI.skin.button);
			buttonStyle.normal.background = MakeTex(new Color(0.3f, 0.35f, 0.4f, 1f));
			buttonStyle.hover.background = MakeTex(new Color(0.4f, 0.45f, 0.5f, 1f));
			buttonStyle.active.background = MakeTex(new Color(0.2f, 0.25f, 0.3f, 1f));
			buttonStyle.normal.textColor = Color.white;
			buttonStyle.hover.textColor = Color.white;
			buttonStyle.active.textColor = Color.white;
			textFieldStyle = new GUIStyle(GUI.skin.textField);
			textFieldStyle.normal.background = MakeTex(new Color(0.2f, 0.22f, 0.25f, 1f));
			textFieldStyle.normal.textColor = Color.white;
			stylesInitialized = true;
		}

		private void OpenFileBrowser(Action<string> callback)
		{
			showFileBrowser = true;
			onFileSelectedCallback = callback;
		}

		private void OnSourceTextureLoaded(Texture2D sourceTexture)
		{
			if ((Object)(object)sourceTexture == (Object)null)
			{
				statusMessage = "Error: Failed to load Source Image.";
				((BaseUnityPlugin)this).Logger.LogError((object)statusMessage);
			}
			else
			{
				TextureLoader.Instance.LoadTextureFromPath(PaletteFileName, delegate(Texture2D paletteTexture)
				{
					OnBothTexturesLoaded(sourceTexture, paletteTexture);
				});
			}
		}

		private void OnBothTexturesLoaded(Texture2D sourceTexture, Texture2D paletteTexture)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: 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)
			//IL_014c: 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_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)paletteTexture == (Object)null)
			{
				statusMessage = "Error: Failed to load Palette Image ('" + PaletteFileName + "').";
				((BaseUnityPlugin)this).Logger.LogError((object)statusMessage);
				return;
			}
			statusMessage = "Resizing source image...";
			Texture2D val = ResizeTexture(sourceTexture, 100, 66);
			statusMessage = "Processing...";
			CreateSplitReferenceMaps(paletteTexture);
			Texture2D val2 = new Texture2D(((Texture)val).width, ((Texture)val).height);
			val2.SetPixels(val.GetPixels());
			val2.Apply();
			if (brightness != 0f)
			{
				ApplyBrightness(val2, brightness);
			}
			if (contrast != 1f)
			{
				ApplyContrast(val2, contrast);
			}
			if (sharpen > 0f)
			{
				ApplySharpen(val2, sharpen);
			}
			if (noise > 1)
			{
				ApplyMedianFilter(val2, noise);
			}
			List<string> list = new List<string>();
			float num = 0.1f;
			float num2 = default(float);
			float num3 = default(float);
			float num4 = default(float);
			for (int i = 0; i < ((Texture)val2).width; i++)
			{
				for (int j = 0; j < ((Texture)val2).height; j++)
				{
					Color pixel = val2.GetPixel(i, j);
					Color.RGBToHSV(pixel, ref num2, ref num3, ref num4);
					Color32 pixelColor = Color32.op_Implicit(pixel);
					Vector2 val3 = ((num3 < num) ? FindClosestUV(pixelColor, grayMap) : FindClosestUV(pixelColor, colorMap));
					list.Add($"{val3.x:F6}:{val3.y:F6}");
				}
			}
			string text = string.Join(",", list.ToArray());
			try
			{
				string path = Path.GetFileNameWithoutExtension(sourceImagePath.Value) + ".txt";
				string path2 = Path.Combine(savedFlagsPath, path);
				File.WriteAllText(path2, text);
				SetFlagGridPlayerPref(text);
				RefreshSavedFlags();
			}
			catch (Exception ex)
			{
				statusMessage = "Error saving flag file.";
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not save flag file: " + ex.Message));
			}
		}

		private void SetFlagGridPlayerPref(string flagContent)
		{
			try
			{
				if (!string.IsNullOrEmpty(flagContent))
				{
					PlayerPrefs.SetString("flagGrid", flagContent);
					PlayerPrefs.Save();
					statusMessage = "Success! Flag has been set.";
					CallLoadFlagMethod(flagContent);
				}
				else
				{
					statusMessage = "Error: Flag content was empty.";
					((BaseUnityPlugin)this).Logger.LogError((object)statusMessage);
				}
			}
			catch (Exception ex)
			{
				statusMessage = "Error: Failed to set flag from content.";
				((BaseUnityPlugin)this).Logger.LogError((object)("SetFlag Error: " + ex.Message));
			}
		}

		private void CallLoadFlagMethod(string flagContent)
		{
			try
			{
				Type type = AccessTools.TypeByName("DrawPixels");
				if ((object)type == null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find the 'DrawPixels' class type.");
					return;
				}
				Object val = Object.FindObjectOfType(type);
				if (val == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find an active instance of 'DrawPixels' in the scene.");
					return;
				}
				MethodInfo method = type.GetMethod("ServerLoadFlag", BindingFlags.Instance | BindingFlags.NonPublic);
				if ((object)method == null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Could not find the 'ServerLoadFlag' method in 'DrawPixels'.");
					return;
				}
				method.Invoke(val, new object[1] { flagContent });
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Successfully called DrawPixels.ServerLoadFlag()");
			}
			catch (Exception ex)
			{
				statusMessage = "Error calling game method.";
				((BaseUnityPlugin)this).Logger.LogError((object)("An exception occurred while trying to call 'ServerLoadFlag': " + ex));
			}
		}

		private void RefreshSavedFlags()
		{
			savedFlagFiles.Clear();
			if (Directory.Exists(savedFlagsPath))
			{
				savedFlagFiles.AddRange(Directory.GetFiles(savedFlagsPath, "*.txt"));
			}
		}

		private void SetSavedFlag(string flagPath)
		{
			try
			{
				if (File.Exists(flagPath))
				{
					string flagGridPlayerPref = File.ReadAllText(flagPath);
					SetFlagGridPlayerPref(flagGridPlayerPref);
					statusMessage = "Loaded and set flag: " + Path.GetFileNameWithoutExtension(flagPath);
				}
			}
			catch (Exception ex)
			{
				statusMessage = "Error loading saved flag.";
				((BaseUnityPlugin)this).Logger.LogError((object)("Could not load saved flag: " + ex.Message));
			}
		}

		private void CreateSplitReferenceMaps(Texture2D paletteImage)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(((Texture)paletteImage).width, ((Texture)paletteImage).height);
			Color[] pixels = paletteImage.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				pixels[i].a = 1f;
			}
			val.SetPixels(pixels);
			val.Apply();
			colorMap = new List<ColorUvPair>();
			grayMap = new List<ColorUvPair>();
			HashSet<Color32> hashSet = new HashSet<Color32>();
			float num = 0.1f;
			int width = ((Texture)val).width;
			int height = ((Texture)val).height;
			float num4 = default(float);
			float num5 = default(float);
			float num6 = default(float);
			for (int j = 0; j < height; j++)
			{
				for (int k = 0; k < width; k++)
				{
					Color pixel = val.GetPixel(k, j);
					Color32 val2 = Color32.op_Implicit(pixel);
					if (!hashSet.Contains(val2))
					{
						hashSet.Add(val2);
						float num2 = (float)k / (float)(width - 1);
						float num3 = (float)j / (float)(height - 1);
						Color.RGBToHSV(pixel, ref num4, ref num5, ref num6);
						ColorUvPair colorUvPair = default(ColorUvPair);
						colorUvPair.color = val2;
						colorUvPair.uv = new Vector2(num2, num3);
						ColorUvPair item = colorUvPair;
						if (num5 < num)
						{
							grayMap.Add(item);
						}
						else
						{
							colorMap.Add(item);
						}
					}
				}
			}
		}

		private Vector2 FindClosestUV(Color32 pixelColor, List<ColorUvPair> referenceMap)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			int num = int.MaxValue;
			Vector2 result = Vector2.zero;
			foreach (ColorUvPair item in referenceMap)
			{
				int num2 = pixelColor.r - item.color.r;
				int num3 = pixelColor.g - item.color.g;
				int num4 = pixelColor.b - item.color.b;
				int num5 = num2 * num2 + num3 * num3 + num4 * num4;
				if (num5 < num)
				{
					num = num5;
					result = item.uv;
					if (num == 0)
					{
						break;
					}
				}
			}
			return result;
		}

		private void ApplyBrightness(Texture2D texture, float amount)
		{
			Color[] pixels = texture.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				pixels[i].r += amount;
				pixels[i].g += amount;
				pixels[i].b += amount;
			}
			texture.SetPixels(pixels);
			texture.Apply();
		}

		private void ApplyContrast(Texture2D texture, float factor)
		{
			Color[] pixels = texture.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				pixels[i].r = 0.5f + factor * (pixels[i].r - 0.5f);
				pixels[i].g = 0.5f + factor * (pixels[i].g - 0.5f);
				pixels[i].b = 0.5f + factor * (pixels[i].b - 0.5f);
			}
			texture.SetPixels(pixels);
			texture.Apply();
		}

		private void ApplySharpen(Texture2D texture, float amount)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			Color[] pixels = texture.GetPixels();
			Color[] array = (Color[])(object)new Color[pixels.Length];
			int width = ((Texture)texture).width;
			int height = ((Texture)texture).height;
			float[] array2 = new float[9] { 0f, -1f, 0f, -1f, 5f, -1f, 0f, -1f, 0f };
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					if (i == 0 || i >= height - 1 || j == 0 || j >= width - 1)
					{
						array[i * width + j] = pixels[i * width + j];
						continue;
					}
					float num = 0f;
					float num2 = 0f;
					float num3 = 0f;
					int num4 = 0;
					for (int k = -1; k <= 1; k++)
					{
						for (int l = -1; l <= 1; l++)
						{
							Color val = pixels[(i + k) * width + (j + l)];
							num += val.r * array2[num4];
							num2 += val.g * array2[num4];
							num3 += val.b * array2[num4];
							num4++;
						}
					}
					Color val2 = pixels[i * width + j];
					array[i * width + j] = new Color(Mathf.Lerp(val2.r, num, amount), Mathf.Lerp(val2.g, num2, amount), Mathf.Lerp(val2.b, num3, amount), val2.a);
				}
			}
			texture.SetPixels(array);
			texture.Apply();
		}

		private void ApplyMedianFilter(Texture2D texture, int size)
		{
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			Color[] pixels = texture.GetPixels();
			Color[] array = (Color[])(object)new Color[pixels.Length];
			int width = ((Texture)texture).width;
			int height = ((Texture)texture).height;
			int num = size / 2;
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					List<float> list = new List<float>();
					List<float> list2 = new List<float>();
					List<float> list3 = new List<float>();
					for (int k = -num; k <= num; k++)
					{
						for (int l = -num; l <= num; l++)
						{
							int num2 = Mathf.Clamp(j + l, 0, width - 1);
							int num3 = Mathf.Clamp(i + k, 0, height - 1);
							Color val = pixels[num3 * width + num2];
							list.Add(val.r);
							list2.Add(val.g);
							list3.Add(val.b);
						}
					}
					list.Sort();
					list2.Sort();
					list3.Sort();
					int index = list.Count / 2;
					array[i * width + j] = new Color(list[index], list2[index], list3[index], pixels[i * width + j].a);
				}
			}
			texture.SetPixels(array);
			texture.Apply();
		}

		private Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Expected O, but got Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			RenderTexture temporary = RenderTexture.GetTemporary(newWidth, newHeight);
			((Texture)temporary).filterMode = (FilterMode)1;
			RenderTexture.active = temporary;
			Graphics.Blit((Texture)(object)source, temporary);
			Texture2D val = new Texture2D(newWidth, newHeight);
			val.ReadPixels(new Rect(0f, 0f, (float)newWidth, (float)newHeight), 0, 0);
			val.Apply();
			RenderTexture.active = null;
			RenderTexture.ReleaseTemporary(temporary);
			return val;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "FlagMaker";

		public const string PLUGIN_NAME = "FlagMaker";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}