using I2.Loc.SimpleJSON; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using UnityEngine; namespace CustomCosmeticLoader { public class Config { public const string CONFIG_FILE_NAME = "config.json"; public const string SKINS_DIR_NAME = "skins"; public const string PROPERTY_ENABLED = "enabled"; public const string PROPERTY_CURRENT_SKIN = "currentSkin"; public const string PROPERTY_SKIN_NAME_TO_HIJACK = "skinNameToHijack"; public const string PROPERTY_IN_MAIN_MENU = "inMainMenu"; public const string PROPERTY_IN_COSMETIC_MENU = "inCosmeticMenu"; public const string PROPERTY_IN_ALL_REPLAYS = "inAllReplays"; public const string PROPERTY_SHOW_SKIN_IDS = "showSkinIds"; public const string PROPERTY_OVERRIDE_REPLAY_COSMETICS = "overrideReplayCosmetics"; public const string PROPERTY_OTHER_PLAYERS = "otherPlayers"; public static bool enabled = true; public static string currentSkin; public static string skinNameToHijack = "Swirl_M"; // Mirage, simple skin with nice properties public static bool inMainMenu = true; public static bool inCosmeticMenu = false; public static bool inAllReplays = false; public static bool showSkinIds = false; public static bool overrideReplayCosmetics = false; public static Dictionary otherPlayers = new Dictionary(); public static Dictionary skins = new Dictionary(); public static void Init() { bool configExists = File.Exists(GetConfigPath()); if (configExists) ReadConfig(); ensureSkinSelected(); if (!configExists && enabled) SaveConfig(); } public static string GetConfigPath() { return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_FILE_NAME); } private static void ReadConfig() { if (!File.Exists(GetConfigPath())) return; JSONNode data; try { data = JSON.Parse(File.ReadAllText(GetConfigPath())); } catch (Exception e) { Shared.Log("Couldn't load CustomCosmeticLoader config!"); Debug.LogException(e); return; } if (data[PROPERTY_ENABLED] != null) enabled = data[PROPERTY_ENABLED].AsBool; if (data[PROPERTY_CURRENT_SKIN] != null) currentSkin = data[PROPERTY_CURRENT_SKIN].Value; if (data[PROPERTY_SKIN_NAME_TO_HIJACK] != null) skinNameToHijack = data[PROPERTY_SKIN_NAME_TO_HIJACK].Value; if (data[PROPERTY_IN_MAIN_MENU] != null) inMainMenu = data[PROPERTY_IN_MAIN_MENU].AsBool; if (data[PROPERTY_IN_COSMETIC_MENU] != null) inCosmeticMenu = data[PROPERTY_IN_COSMETIC_MENU].AsBool; if (data[PROPERTY_IN_ALL_REPLAYS] != null) inAllReplays = data[PROPERTY_IN_ALL_REPLAYS].AsBool; if (data[PROPERTY_SHOW_SKIN_IDS] != null) showSkinIds = data[PROPERTY_SHOW_SKIN_IDS].AsBool; if (data[PROPERTY_OVERRIDE_REPLAY_COSMETICS] != null) overrideReplayCosmetics = data[PROPERTY_OVERRIDE_REPLAY_COSMETICS].AsBool; if (data[PROPERTY_OTHER_PLAYERS] != null) { JSONClass otherPlayersData = data[PROPERTY_OTHER_PLAYERS].AsObject; if (otherPlayersData != null) { foreach (KeyValuePair node in otherPlayersData) { string nickname = node.Key; string skinName = node.Value.Value; if (skins.ContainsKey(skinName)) { Shared.Log("Adding other player: " + nickname + " -> " + skinName); otherPlayers.Add(nickname, skinName); } else { Shared.Log("NOT adding other player " + nickname + ", skin " + skinName + " not found"); } } } } } public static void SaveConfig() { JSONNode node = new JSONClass { { PROPERTY_ENABLED, new JSONData(enabled) }, { PROPERTY_CURRENT_SKIN, currentSkin }, { PROPERTY_SKIN_NAME_TO_HIJACK, skinNameToHijack }, { PROPERTY_IN_MAIN_MENU, new JSONData(inMainMenu) }, { PROPERTY_IN_COSMETIC_MENU, new JSONData(inCosmeticMenu) }, { PROPERTY_IN_ALL_REPLAYS, new JSONData(inAllReplays) }, { PROPERTY_SHOW_SKIN_IDS, new JSONData(showSkinIds) }, { PROPERTY_OVERRIDE_REPLAY_COSMETICS, new JSONData(overrideReplayCosmetics) }, }; JSONNode otherPlayersData = new JSONClass(); foreach (KeyValuePair otherPlayer in otherPlayers) otherPlayersData.Add(otherPlayer.Key, otherPlayer.Value); node.Add(PROPERTY_OTHER_PLAYERS, otherPlayersData); File.WriteAllText(GetConfigPath(), JsonHelper.FormatJson(node.ToString(), "\t")); } public static void ensureSkinSelected() { if (currentSkin == null || !skins.ContainsKey(currentSkin)) { if (skins.Count == 0) { Shared.Log("No custom skins found, disabling mod"); enabled = false; return; } currentSkin = skins.Keys.ToList()[0]; } } } }