Decompiled source of SafeCustomizationPatch v1.0.1

SafeCustomizationPatch.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Zorro.Core;

[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("SafeCustomizationPatch")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("SafeCustomizationPatch")]
[assembly: AssemblyTitle("SafeCustomizationPatch")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace CustomizationRemovalPatch
{
	[BepInPlugin("com.snosz.safecustomizationpatch", "SafeCustomizationPatch", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(CharacterCustomization), "OnPlayerDataChange")]
		private class SafePlayerDataPatch
		{
			private static void Prefix(CharacterCustomization __instance, PersistentPlayerData playerData)
			{
				Customization instance = Singleton<Customization>.Instance;
				CharacterCustomizationData customizationData = playerData.customizationData;
				customizationData.currentSkin = SafeIndex(customizationData.currentSkin, instance.skins.Length);
				customizationData.currentOutfit = SafeIndex(customizationData.currentOutfit, instance.fits.Length);
				customizationData.currentEyes = SafeIndex(customizationData.currentEyes, instance.eyes.Length);
				customizationData.currentMouth = SafeIndex(customizationData.currentMouth, instance.mouths.Length);
				customizationData.currentAccessory = SafeIndex(customizationData.currentAccessory, instance.accessories.Length);
				customizationData.currentHat = SafeIndex(customizationData.currentHat, instance.hats.Length);
				if ((Object)(object)__instance.refs != (Object)null && __instance.refs.sashAscentMaterials != null)
				{
					customizationData.currentSash = SafeIndex(customizationData.currentSash, __instance.refs.sashAscentMaterials.Length);
				}
				playerData.customizationData = customizationData;
			}

			private static int SafeIndex(int index, int length)
			{
				if (length == 0)
				{
					return 0;
				}
				if (index < 0 || index >= length)
				{
					return 0;
				}
				return index;
			}
		}

		[HarmonyPatch]
		public static class CharacterCustomizationSafeIndexPatches
		{
			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterSkinColor")]
			public static void SetCharacterSkinColor_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.skins.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterEyes")]
			public static void SetCharacterEyes_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.eyes.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterMouth")]
			public static void SetCharacterMouth_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.mouths.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterAccessory")]
			public static void SetCharacterAccessory_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.accessories.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterOutfit")]
			public static void SetCharacterOutfit_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.fits.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterHat")]
			public static void SetCharacterHat_Prefix(CharacterCustomization __instance, ref int index)
			{
				index = SafeIndex(index, Singleton<Customization>.Instance.hats.Length);
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(CharacterCustomization), "SetCharacterSash")]
			public static bool SetCharacterSash_Prefix(CharacterCustomization __instance, ref int index)
			{
				if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.refs == (Object)null)
				{
					return false;
				}
				Material[] sashAscentMaterials = __instance.refs.sashAscentMaterials;
				if (sashAscentMaterials == null || sashAscentMaterials.Length == 0)
				{
					return false;
				}
				index = SafeIndex(index, sashAscentMaterials.Length);
				return true;
			}

			private static int SafeIndex(int index, int length)
			{
				if (length == 0)
				{
					return 0;
				}
				if (index < 0 || index >= length)
				{
					return 0;
				}
				return index;
			}
		}

		private void Awake()
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.snosz.safecustomizationpatch");
		}
	}
}