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 BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MultiCustomizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MultiCustomizer")]
[assembly: AssemblyTitle("MultiCustomizer")]
[assembly: AssemblyVersion("1.0.0.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 MultiCustomizer
{
public class MaterialConditional
{
public TextureState state;
private bool isActive;
public Action<TextureState, bool> ActivityChangedEvent;
public bool IsActive
{
get
{
return isActive;
}
set
{
if (value != isActive)
{
isActive = value;
ActivityChangedEvent?.Invoke(state, isActive);
}
}
}
public MaterialConditional(TextureState state)
{
this.state = state;
}
}
public class TextureState
{
public string Name { get; }
public Dictionary<Material, Texture> MaterialsToTextures { get; }
public TextureState(string name)
{
Name = name;
MaterialsToTextures = new Dictionary<Material, Texture>();
}
}
[BepInPlugin("com.unniisme.MultiCustomizer", "MultiCustomizer", "1.0.0")]
public class MultiCustomizer : BaseUnityPlugin
{
internal static ManualLogSource Logger;
internal static object _multiCustomizerLock = new object();
private static readonly Dictionary<string, TextureState> TextureStates = new Dictionary<string, TextureState>();
private static readonly List<TextureState> ActiveStates = new List<TextureState>();
private static TextureState defaultTextureState = new TextureState("");
private static TextureState currentTextureState = defaultTextureState;
private void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
Logger.LogInfo((object)"Plugin MultiCustomizer loaded");
Harmony.CreateAndPatchAll(typeof(MultiCustomizer), (string)null);
if (Chainloader.PluginInfos.ContainsKey("customizer"))
{
Logger.LogError((object)"Detected customizer. Error: MultiCustomizer is not compatable with Customizer");
Logger.LogError((object)"Aborting MultiCustomizer");
}
else
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
}
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
LoadTextures();
Logger.LogInfo((object)("Loaded scene: " + ((Scene)(ref scene)).name));
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
public static MaterialConditional AddTexturePack(string name)
{
TextureState textureState = new TextureState(name);
MaterialConditional materialConditional = new MaterialConditional(textureState);
materialConditional.ActivityChangedEvent = (Action<TextureState, bool>)Delegate.Combine(materialConditional.ActivityChangedEvent, new Action<TextureState, bool>(HandleMaterialConditional));
TextureStates.Add(name, textureState);
return materialConditional;
}
private static void HandleMaterialConditional(TextureState state, bool isActive)
{
if (!TextureStates.ContainsKey(state.Name))
{
return;
}
if (isActive)
{
if (!ActiveStates.Contains(state))
{
ActiveStates.Add(state);
}
SetTexturePack(state.Name);
}
else
{
if (!ActiveStates.Contains(state))
{
return;
}
ActiveStates.Remove(state);
if (currentTextureState.Name == state.Name)
{
if (Extensions.IsNullOrEmpty<TextureState>((ICollection<TextureState>)ActiveStates))
{
ResetTexturePack();
}
else
{
SetTexturePack(ActiveStates.Last().Name);
}
}
}
}
private static void ResetTexturePack()
{
lock (_multiCustomizerLock)
{
if (!(currentTextureState.Name != ""))
{
return;
}
Logger.LogDebug((object)("Switching to texture pack " + currentTextureState.Name));
currentTextureState = defaultTextureState;
foreach (Material key in defaultTextureState.MaterialsToTextures.Keys)
{
if (currentTextureState.MaterialsToTextures.ContainsKey(key))
{
key.mainTexture = currentTextureState.MaterialsToTextures[key];
}
else
{
key.mainTexture = defaultTextureState.MaterialsToTextures[key];
}
}
}
}
private static void SetTexturePack(string name)
{
lock (_multiCustomizerLock)
{
if (!(currentTextureState.Name != name))
{
return;
}
Logger.LogDebug((object)("Switching to texture pack " + currentTextureState.Name));
currentTextureState = TextureStates[name];
foreach (Material key in defaultTextureState.MaterialsToTextures.Keys)
{
if (currentTextureState.MaterialsToTextures.ContainsKey(key))
{
key.mainTexture = currentTextureState.MaterialsToTextures[key];
}
else
{
key.mainTexture = defaultTextureState.MaterialsToTextures[key];
}
}
}
}
private static void LoadTextures()
{
tk2dSpriteCollectionData[] array = Resources.FindObjectsOfTypeAll<tk2dSpriteCollectionData>();
Dictionary<string, tk2dSpriteCollectionData> collections = array.ToDictionary((tk2dSpriteCollectionData c) => ((Object)c).name, (tk2dSpriteCollectionData c) => c);
GetDefaultTextures(array);
GetTextureStates(collections);
}
private static void GetDefaultTextures(tk2dSpriteCollectionData[] collectionsArray)
{
Logger.LogDebug((object)"Loading default textures");
for (int i = 0; i < collectionsArray.Length; i++)
{
Material[] materials = collectionsArray[i].materials;
foreach (Material val in materials)
{
lock (_multiCustomizerLock)
{
if (!defaultTextureState.MaterialsToTextures.ContainsKey(val))
{
defaultTextureState.MaterialsToTextures[val] = val.mainTexture;
}
}
}
}
}
private static void GetTextureStates(Dictionary<string, tk2dSpriteCollectionData> collections)
{
Logger.LogDebug((object)"Entering GetTextureStates");
Directory.GetDirectories(Paths.PluginPath, "Customizer", SearchOption.AllDirectories).ToList();
foreach (string key in TextureStates.Keys)
{
Logger.LogDebug((object)("textureStateName : " + key));
string text = Path.Combine(Paths.PluginPath, key, "Customizer");
Logger.LogDebug((object)("packDir : " + text));
if (Directory.Exists(text))
{
Logger.LogDebug((object)("Loading Textures for " + key));
GetTextureState(TextureStates[key], text, collections);
}
}
}
private static void GetTextureState(TextureState textureState, string dir, Dictionary<string, tk2dSpriteCollectionData> spriteCollection)
{
//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
//IL_00fe: Expected O, but got Unknown
Logger.LogDebug((object)("Setting textures for pack " + textureState.Name));
string[] directories = Directory.GetDirectories(dir);
foreach (string text in directories)
{
Logger.LogDebug((object)("subDir : " + text));
string fileName = Path.GetFileName(text);
Logger.LogDebug((object)("fileName : " + fileName));
if (!spriteCollection.ContainsKey(fileName))
{
continue;
}
Material[] materials = spriteCollection[fileName].materials;
foreach (Material val in materials)
{
string text2 = Path.Combine(text, ((Object)val.mainTexture).name + ".png");
Logger.LogDebug((object)("atlasFile : " + text2));
if (!File.Exists(text2))
{
continue;
}
lock (_multiCustomizerLock)
{
Logger.LogDebug((object)("Loading file " + text2));
byte[] array = File.ReadAllBytes(text2);
Texture2D val2 = new Texture2D(2, 2);
((Object)val2).name = ((Object)val.mainTexture).name;
if (ImageConversion.LoadImage(val2, array))
{
textureState.MaterialsToTextures[val] = (Texture)(object)val2;
}
}
}
}
}
}
}