Decompiled source of LCPosterEditor v1.0.3

LCPosterEditor.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
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: AssemblyTitle("LCPosterEditor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LCPosterEditor")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("80129ca5-58c6-4fee-bdbb-73f1029a0f5d")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LCPosterEditor;

internal class AtlasCreator
{
	private static List<Image> posterImages = (from f in Directory.EnumerateFiles(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "posters"))
		select Image.FromFile(f)).ToList();

	private static List<Image> tipImages = (from f in Directory.EnumerateFiles(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "tips"))
		select Image.FromFile(f)).ToList();

	private static readonly int[] TIPS_SIZE = new int[2] { 796, 1024 };

	private static readonly int[] POSTER_SIZE = new int[2] { 1024, 1024 };

	private static readonly int[,] POSTER_OFFSETS = new int[5, 4]
	{
		{ 0, 0, 341, 559 },
		{ 346, 0, 284, 559 },
		{ 641, 58, 274, 243 },
		{ 184, 620, 411, 364 },
		{ 632, 320, 372, 672 }
	};

	private static ManualLogSource Logger { get; set; }

	public static void Init(ManualLogSource logger)
	{
		Logger = logger;
		Logger.LogInfo((object)"Poster and/or Tips images found, creating assets now...");
		if (!Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "posters")).Any() && Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "posters")).Any())
		{
			Logger.LogInfo((object)"Creating Poster Atlases...");
			for (int i = 0; i < posterImages.Count; i++)
			{
				List<Image> list = new List<Image>();
				for (int j = 0; j < 5; j++)
				{
					list.Add((Image)(object)ResizeImage(posterImages[(i + j) % posterImages.Count], POSTER_OFFSETS[j, 2], POSTER_OFFSETS[j, 3], Plugin.KeepAspectRatio.Value));
				}
				CreatePosterAtlas(list, Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "posters", $"{i}.png"));
			}
			Logger.LogInfo((object)"Poster Atlases Created!");
		}
		if (!Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "tips")).Any() && Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "tips")).Any())
		{
			Logger.LogInfo((object)"Creating Tip Files...");
			for (int k = 0; k < tipImages.Count; k++)
			{
				CreateTipFile((Image)(object)ResizeImage(tipImages[k], TIPS_SIZE[0], TIPS_SIZE[1], Plugin.KeepAspectRatio.Value), Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "tips", $"{k}.png"));
			}
			Logger.LogInfo((object)"Tip Files Created!");
		}
	}

	private static void CreatePosterAtlas(List<Image> posters, string filename)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Expected O, but got Unknown
		Image val = (Image)new Bitmap(POSTER_SIZE[0], POSTER_SIZE[1]);
		Graphics val2 = Graphics.FromImage(val);
		try
		{
			for (int i = 0; i < posters.Count; i++)
			{
				val2.DrawImage(posters[i], new Point(POSTER_OFFSETS[i, 0], POSTER_OFFSETS[i, 1]));
			}
		}
		finally
		{
			((IDisposable)val2)?.Dispose();
		}
		val.Save(filename);
	}

	private static void CreateTipFile(Image tip, string filename)
	{
		tip.Save(filename);
	}

	private static Bitmap ResizeImage(Image image, int width, int height, bool keepAspectRatio)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		if (keepAspectRatio)
		{
			double val = (double)width / (double)image.Width;
			double val2 = (double)height / (double)image.Height;
			double num = Math.Min(val, val2);
			width = (int)((double)image.Width * num);
			height = (int)((double)image.Height * num);
		}
		Rectangle rectangle = new Rectangle(0, 0, width, height);
		Bitmap val3 = new Bitmap(width, height);
		val3.SetResolution(image.HorizontalResolution, image.VerticalResolution);
		Graphics val4 = Graphics.FromImage((Image)(object)val3);
		try
		{
			val4.CompositingMode = (CompositingMode)1;
			val4.CompositingQuality = (CompositingQuality)2;
			val4.InterpolationMode = (InterpolationMode)7;
			val4.SmoothingMode = (SmoothingMode)2;
			val4.PixelOffsetMode = (PixelOffsetMode)2;
			ImageAttributes val5 = new ImageAttributes();
			try
			{
				val5.SetWrapMode((WrapMode)3);
				val4.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, (GraphicsUnit)2, val5);
			}
			finally
			{
				((IDisposable)val5)?.Dispose();
			}
		}
		finally
		{
			((IDisposable)val4)?.Dispose();
		}
		return val3;
	}
}
internal class Config
{
	private static ConfigFile ConfigFile { get; set; }

	static Config()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		ConfigFile = new ConfigFile(Paths.ConfigPath + "\\LethalPosters.cfg", true);
		foreach (string posterFolder in Plugin.PosterFolders)
		{
			int num = posterFolder.IndexOf("plugins\\", StringComparison.Ordinal) + "plugins\\".Length;
			int num2 = posterFolder.IndexOf("\\LethalPosters", num, StringComparison.Ordinal);
			string text = posterFolder.Substring(num, num2 - num);
			ConfigEntry<bool> val = ConfigFile.Bind<bool>(text, "Enabled", true, "Enable or disable " + text);
			if (!val.Value)
			{
				Directory.Move(posterFolder, posterFolder + ".Disabled");
			}
		}
	}
}
internal class Patches
{
	private static ManualLogSource Logger { get; set; }

	public static void Init(ManualLogSource logger)
	{
		Logger = logger;
	}

	[HarmonyPatch(typeof(StartOfRound), "Start")]
	[HarmonyPostfix]
	private static void StartPatch()
	{
		Logger.LogInfo((object)"Patching Start in StartOfRound");
		UpdateMaterials(0);
	}

	[HarmonyPatch(typeof(RoundManager), "GenerateNewLevelClientRpc")]
	[HarmonyPostfix]
	private static void GenerateNewLevelClientRpcPatch(int randomSeed)
	{
		Logger.LogInfo((object)"Patching GenerateNewLevelClientRpc in RoundManager");
		UpdateMaterials(randomSeed);
	}

	private static void UpdateMaterials(int seed)
	{
		Logger.LogInfo((object)"Patching the textures");
		Plugin.Rand = new Random(seed);
		Material[] materials = ((Renderer)GameObject.Find("HangarShip/Plane.001").GetComponent<MeshRenderer>()).materials;
		UpdateTexture(Plugin.PosterFiles, materials[0]);
		UpdateTexture(Plugin.TipFiles, materials[1]);
	}

	private static void UpdateTexture(IReadOnlyList<string> files, Material material)
	{
		if (files.Count != 0)
		{
			int index = Plugin.Rand.Next(files.Count);
			Logger.LogInfo((object)("Patching " + ((Object)material).name + " with " + files[index]));
			Texture2D mainTexture = ES3.LoadImage(File.ReadAllBytes(files[index]));
			material.mainTexture = (Texture)(object)mainTexture;
		}
	}
}
[BepInPlugin("nyoo-LCPosterEditor", "LCPosterEditor", "1.0.3")]
public class Plugin : BaseUnityPlugin
{
	public static List<string> PosterFolders = new List<string>();

	public static readonly List<string> PosterFiles = new List<string>();

	public static readonly List<string> TipFiles = new List<string>();

	public static Random Rand = new Random();

	public static ConfigEntry<bool> KeepAspectRatio;

	private void Awake()
	{
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Expected O, but got Unknown
		LoadConfigs();
		CreateDirs();
		if ((!Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "posters")).Any() && Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "posters")).Any()) || (!Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "tips")).Any() && Directory.EnumerateFileSystemEntries(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "tips")).Any()))
		{
			AtlasCreator.Init(((BaseUnityPlugin)this).Logger);
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Poster/Tip files found or Input files missing... continuing to load");
		}
		PosterFolders = Directory.GetDirectories(Paths.PluginPath, "LCPosterEditor", SearchOption.AllDirectories).ToList();
		PosterFolders.AddRange(Directory.GetDirectories(Paths.PluginPath, "LethalPosters", SearchOption.AllDirectories).ToList());
		foreach (string posterFolder in PosterFolders)
		{
			string[] files = Directory.GetFiles(Path.Combine(posterFolder, "posters"));
			foreach (string text in files)
			{
				if (Path.GetExtension(text) != ".old")
				{
					PosterFiles.Add(text);
				}
			}
			string[] files2 = Directory.GetFiles(Path.Combine(posterFolder, "tips"));
			foreach (string text2 in files2)
			{
				if (Path.GetExtension(text2) != ".old")
				{
					TipFiles.Add(text2);
				}
			}
		}
		Patches.Init(((BaseUnityPlugin)this).Logger);
		Harmony val = new Harmony("nyoo-LCPosterEditor");
		val.PatchAll(typeof(Patches));
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin LCPosterEditor (1.0.3) is loaded!");
	}

	private void CreateDirs()
	{
		Directory.CreateDirectory(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "posters"));
		Directory.CreateDirectory(Path.Combine(PluginInfo.PLUGIN_DIR, "LCPosterEditor", "tips"));
		Directory.CreateDirectory(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "posters"));
		Directory.CreateDirectory(Path.Combine(PluginInfo.PLUGIN_DIR, "Input", "tips"));
	}

	private void LoadConfigs()
	{
		KeepAspectRatio = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "KeepAspectRatio", true, "Determines whether or not to restrict the image generation to the original image's aspect ratio");
	}
}
public static class PluginInfo
{
	public const string PLUGIN_GUID = "nyoo-LCPosterEditor";

	public const string PLUGIN_NAME = "LCPosterEditor";

	public const string PLUGIN_VERSION = "1.0.3";

	public static readonly string PLUGIN_DIR = Path.Combine(Paths.PluginPath, "nyoo-LCPosterEditor");
}