Decompiled source of Custom Cans and Mugs v1.1.2

CustomTextures.dll

Decompiled 9 months ago
using System.Collections.Generic;
using System.Diagnostics;
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("CustomTextures")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomTextures")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d6b049a0-55ff-46f1-b46b-f6829fca90b7")]
[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 CustomTextures;

internal class ScrapType
{
	public string scrapType;

	public string folderName;

	public string holdText;

	public List<string> names;

	public List<Texture> textures;

	public ConfigEntry<bool> enabled;

	public ScrapType(string _scrapType, string _folderName, string _holdText = "")
	{
		scrapType = _scrapType;
		folderName = _folderName;
		holdText = _holdText;
		names = new List<string>();
		textures = new List<Texture>();
		enabled = ((BaseUnityPlugin)CustomTextureMod.Instance).Config.Bind<bool>("General", "Enable" + scrapType, true, "true = use custom textures, false = use vanilla textures only");
	}
}
[BepInPlugin("ironbean.CustomScrapTextures", "Custom Scrap Textures", "1.1.2")]
public class CustomTextureMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(GrabbableObject))]
	internal class ScrapPatch
	{
		[HarmonyPatch("SetScrapValue")]
		[HarmonyPostfix]
		private static void CreatePatch(GrabbableObject __instance)
		{
			int num = scraps.FindIndex((ScrapType scrap) => scrap.scrapType == ((Object)__instance.itemProperties).name);
			if (num == -1 || !scraps[num].enabled.Value || scraps[num].textures.Count == 0)
			{
				return;
			}
			MeshRenderer componentInChildren = ((Component)__instance).gameObject.GetComponentInChildren<MeshRenderer>();
			if ((Object)(object)componentInChildren == (Object)null)
			{
				return;
			}
			int index = __instance.scrapValue % scraps[num].textures.Count;
			if (__instance.itemProperties.materialVariants.Length != 0)
			{
				__instance.itemProperties.materialVariants = (Material[])(object)new Material[0];
			}
			((Renderer)componentInChildren).material.mainTexture = scraps[num].textures[index];
			if (!(scraps[num].holdText == ""))
			{
				ScanNodeProperties componentInChildren2 = ((Component)componentInChildren).GetComponentInChildren<ScanNodeProperties>();
				if (!((Object)(object)componentInChildren2 == (Object)null))
				{
					__instance.itemProperties.itemName = scraps[num].holdText;
					componentInChildren2.headerText = ((!scraps[num].names[index].StartsWith("-")) ? scraps[num].names[index] : scraps[num].holdText);
				}
			}
		}
	}

	private const string modGUID = "ironbean.CustomScrapTextures";

	private const string modName = "Custom Scrap Textures";

	private const string modVersion = "1.1.2";

	private readonly Harmony harmony = new Harmony("ironbean.CustomScrapTextures");

	internal static CustomTextureMod Instance;

	internal static ManualLogSource mls;

	private static readonly string[] ParentFolderNames = new string[1] { "ScrapTextures" };

	private static readonly List<string> ParentDirectories = new List<string>();

	internal static List<ScrapType> scraps = new List<ScrapType>();

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		mls = Logger.CreateLogSource("ironbean.CustomScrapTextures");
		string[] parentFolderNames = ParentFolderNames;
		foreach (string searchPattern in parentFolderNames)
		{
			Directory.GetDirectories(Paths.PluginPath, searchPattern, SearchOption.AllDirectories).ToList().ForEach(delegate(string dir)
			{
				ParentDirectories.Add(dir);
			});
		}
		scraps.Add(new ScrapType("Mug", "mugs"));
		scraps.Add(new ScrapType("SodaCanRed", "cans", "Soda can"));
		scraps.ForEach(delegate(ScrapType scrapType)
		{
			LoadTextures(scrapType.folderName, scrapType.names, scrapType.textures);
		});
		harmony.PatchAll(typeof(CustomTextureMod));
		harmony.PatchAll(typeof(ScrapPatch));
	}

	internal static void LoadTextures(string scrapType, List<string> scrapNames, List<Texture> scrapTextures)
	{
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Expected O, but got Unknown
		mls.LogInfo((object)("Loading custom textures for " + scrapType));
		List<string> list = new List<string>();
		foreach (string parentDirectory in ParentDirectories)
		{
			if (!Directory.Exists(Path.Combine(parentDirectory, scrapType)))
			{
				continue;
			}
			string[] files = Directory.GetFiles(Path.Combine(parentDirectory, scrapType));
			string[] array = files;
			foreach (string text in array)
			{
				if (Path.GetExtension(text) != ".old")
				{
					scrapNames.Add(Path.GetFileNameWithoutExtension(text));
					list.Add(text);
				}
			}
		}
		foreach (string item in list)
		{
			Texture2D val = new Texture2D(2, 2);
			ImageConversion.LoadImage(val, File.ReadAllBytes(item));
			scrapTextures.Add((Texture)(object)val);
		}
		mls.LogInfo((object)("Loaded " + list.Count + " " + scrapType));
	}
}