Decompiled source of RandomPaintingSwap FIX v1.0.0

RandomPaintingSwap.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RandomPaintingSwap")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+664772cf6cbf6fcb0de76bafb763299253f3a03f")]
[assembly: AssemblyProduct("Random Painting Swap")]
[assembly: AssemblyTitle("RandomPaintingSwap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 RandomPaintingSwap
{
	[BepInPlugin("ch.gabzdev.randompaintingswap", "Random Painting Swap", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(LoadingUI), "LevelAnimationComplete")]
		public class PatchLoadingUI
		{
			[HarmonyPostfix]
			private static void Postfix()
			{
				//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)
				Logger.LogInfo((object)"Remplacement des images de base par les images du plugin");
				if (loadedMaterials.Count == 0)
				{
					Logger.LogWarning((object)"No custom images loaded! Using default game paintings.");
					return;
				}
				Scene activeScene = SceneManager.GetActiveScene();
				List<GameObject> list = ((Scene)(ref activeScene)).GetRootGameObjects().ToList();
				foreach (GameObject item in list)
				{
					MeshRenderer[] componentsInChildren = item.GetComponentsInChildren<MeshRenderer>();
					foreach (MeshRenderer val in componentsInChildren)
					{
						Material[] sharedMaterials = ((Renderer)val).sharedMaterials;
						if (sharedMaterials == null)
						{
							continue;
						}
						for (int j = 0; j < sharedMaterials.Length; j++)
						{
							Material val2 = sharedMaterials[j];
							if ((Object)(object)val2 != (Object)null && targetMaterials.Contains(((Object)val2).name) && loadedMaterials.Count > 0)
							{
								sharedMaterials[j] = loadedMaterials[Random.Range(0, loadedMaterials.Count)];
							}
						}
						((Renderer)val).sharedMaterials = sharedMaterials;
					}
				}
			}
		}

		private const string IMAGE_FOLDER_NAME = "RandomPaintingSwap_Images";

		internal static ManualLogSource Logger;

		public static List<Material> loadedMaterials = new List<Material>();

		public static readonly HashSet<string> targetMaterials = new HashSet<string> { "Painting_H_Landscape", "Painting_V_Furman", "painting teacher02", "Painting_S_Tree" };

		public static readonly HashSet<string> imagePatterns = new HashSet<string> { "*.png", "*.jpg", "*.jpeg" };

		private readonly Harmony harmony = new Harmony("ch.gabzdev.randompaintingswap");

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin Random Painting Swap is loaded!");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			FindAndLoadImages();
		}

		private void FindAndLoadImages()
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string text = Directory.GetParent(directoryName)?.Parent?.FullName;
			if (text == null)
			{
				Logger.LogWarning((object)"Could not determine BepInEx plugins directory!");
				text = Directory.GetParent(directoryName)?.FullName ?? directoryName;
			}
			Logger.LogInfo((object)("Scanning for image folders in: " + text));
			List<string> list = new List<string>();
			try
			{
				string[] directories = Directory.GetDirectories(text, "*", SearchOption.AllDirectories);
				foreach (string path in directories)
				{
					string text2 = Path.Combine(path, "RandomPaintingSwap_Images");
					if (Directory.Exists(text2))
					{
						list.Add(text2);
						Logger.LogInfo((object)("Found images folder: " + text2));
					}
				}
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Error scanning plugin directories: " + ex.Message));
			}
			string text3 = Path.Combine(directoryName, "RandomPaintingSwap_Images");
			if (Directory.Exists(text3) && !list.Contains(text3))
			{
				list.Add(text3);
				Logger.LogInfo((object)("Found default images folder: " + text3));
			}
			if (list.Count == 0)
			{
				Directory.CreateDirectory(text3);
				list.Add(text3);
				Logger.LogInfo((object)("No existing image folders found. Created default folder: " + text3));
			}
			int num = 0;
			foreach (string item in list)
			{
				int num2 = LoadImagesFromDirectory(item);
				num += num2;
				Logger.LogInfo((object)$"Loaded {num2} images from {item}");
			}
			Logger.LogInfo((object)$"Total Images Loaded: {num} from {list.Count} folders");
		}

		private int LoadImagesFromDirectory(string directoryPath)
		{
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Expected O, but got Unknown
			if (!Directory.Exists(directoryPath))
			{
				Logger.LogWarning((object)("Le dossier " + directoryPath + " n'existe pas !"));
				return 0;
			}
			List<string> list = imagePatterns.SelectMany((string pattern) => Directory.GetFiles(directoryPath, pattern)).ToList();
			if (!list.Any())
			{
				Logger.LogWarning((object)("Aucune image trouvee dans le dossier " + directoryPath));
				return 0;
			}
			int num = 0;
			foreach (string item2 in list)
			{
				Texture2D val = LoadTextureFromFile(item2);
				if ((Object)(object)val == (Object)null)
				{
					Logger.LogWarning((object)("Erreur chargement image : " + item2));
					continue;
				}
				Material item = new Material(Shader.Find("Standard"))
				{
					mainTexture = (Texture)(object)val
				};
				loadedMaterials.Add(item);
				num++;
				Logger.LogInfo((object)("Image chargée: " + Path.GetFileNameWithoutExtension(item2)));
			}
			return num;
		}

		private Texture2D LoadTextureFromFile(string filePath)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			byte[] array = File.ReadAllBytes(filePath);
			Texture2D val = new Texture2D(2, 2);
			if (ImageConversion.LoadImage(val, array))
			{
				val.Apply();
				return val;
			}
			val = null;
			return null;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "RandomPaintingSwap";

		public const string PLUGIN_NAME = "Random Painting Swap";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}