PVZFusionTranslation/Source/ModFeatures.cs

106 lines
3.4 KiB
C#

using PvZ_Fusion_Translator.AssetStore;
namespace PvZ_Fusion_Translator;
public static class ModFeatures
{
public enum ModType
{
ReloadStrings, ReloadTextures, ExportLevelStrings,
}
private class ModFeature
{
public string Name { get; private set; }
public ModType ModType { get; private set; }
public KeyCode KeyCode { get; private set; }
public bool IsActive { get; set; }
public ModFeature(string name, ModType modType, KeyCode keyCode, bool defaultValue = false)
{
Name = name;
ModType = modType;
KeyCode = keyCode;
IsActive = defaultValue;
}
public void ToggleFeature()
{
switch (ModType)
{
case ModType.ReloadStrings:
StringStore.Reload();
Core.ShowToast("Strings Reloaded!");
return;
case ModType.ReloadTextures:
TextureStore.Reload();
Core.ShowToast("Textures Reloaded!");
return;
case ModType.ExportLevelStrings:
{
var customMenu = UnityEngine.Object.FindObjectOfType<CustomMenu>();
if (!customMenu)
return;
if (customMenu.isActiveAndEnabled)
{
Patches.Modes.Super_Editor.GameLevel.LevelManager_Patch
.ExportLevelStrings(customMenu.LevelData);
Core.ShowToast("Dumped level strings!");
}
return;
}
}
}
}
public static bool IsDebug { get; } = IsDefined("PVZ_FUSION_TRANSLATOR_DEBUG");
public static bool IsTesting { get; } = IsDefined("PVZ_FUSION_TRANSLATOR_TESTING");
public static bool IsAudioTesting { get; } = IsDefined("PVZ_FUSION_TRANSLATOR_AUDIO_TESTING");
public static bool IsMultiLanguage { get; } = IsDefined("PVZ_FUSION_TRANSLATOR_MULTI_LANGUAGE", true);
private static Dictionary<ModType, ModFeature> s_featureLists = new()
{
{ ModType.ReloadStrings, new("Reload Strings", ModType.ReloadStrings, KeyCode.Home, true) },
{ ModType.ReloadTextures, new("Reload Textures", ModType.ReloadTextures, KeyCode.End, true) },
{
ModType.ExportLevelStrings,
new("Export Level Strings", ModType.ExportLevelStrings, KeyCode.N, true)
},
};
public static void ToggleFeature(ModType modType)
{
if (!s_featureLists.ContainsKey(modType))
return;
s_featureLists[modType].ToggleFeature();
}
public static void OnLateUpdate()
{
foreach (var (type, feature) in s_featureLists)
if (feature.KeyCode != KeyCode.None && Enum.IsDefined(typeof(KeyCode), feature.KeyCode))
if (Input.GetKeyDown(feature.KeyCode))
feature.ToggleFeature();
}
static bool IsDefined(string env, bool fallback = false)
{
var ret =
Environment.GetEnvironmentVariable(env)?.ToUpperInvariant() is not "FALSE" and not "0" and var value &&
(!string.IsNullOrWhiteSpace(value) || fallback);
Log.LogInfo($"The feature '{env}' is {(ret ? "enabled" : "disabled")}.");
return ret;
}
}