more code changes for 3.5

This commit is contained in:
SillyStar-Github 2026-04-06 01:01:44 -04:00
parent 75060fc05e
commit bf3516898c
9 changed files with 504 additions and 63 deletions

View file

@ -6,8 +6,8 @@ namespace PvZ_Fusion_Translator.AssetStore
{
public static class TextureStore
{
internal static Dictionary<string, string> textureDict = [];
internal static Dictionary<string, string> spriteDict = [];
internal static Dictionary<string, byte[]> textureDict = [];
internal static Dictionary<string, byte[]> spriteDict = [];
internal static void Init() => FileLoader.LoadTextures();
@ -55,13 +55,13 @@ namespace PvZ_Fusion_Translator.AssetStore
}
#endif
public static void LogAll()
/*public static void LogAll()
{
Log.LogInfo("Logging all TextureStore entries.");
foreach (KeyValuePair<string, string> entry in textureDict)
{
Log.LogInfo("TextureDict Entry: " + entry.Key + " | " + entry.Value);
}
}
}*/
}
}

View file

@ -4,6 +4,7 @@ using JetBrains.Annotations;
using MelonLoader;
using MelonLoader.TinyJSON;
using Newtonsoft.Json;
using NVorbis.Contracts;
using PvZ_Fusion_Translator.AssetStore;
using PvZ_Fusion_Translator.Patches.GameObjects;
using PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects;
@ -312,7 +313,13 @@ namespace PvZ_Fusion_Translator
{
string stringDir = FileLoader.GetAssetDir(FileLoader.AssetType.Strings, Utils.Language);
string changelogDir = Path.Combine(stringDir, "changelog.txt");
string changelogText = File.ReadAllText(changelogDir);
string changelogText = "";
if (!File.Exists(changelogDir))
{
File.WriteAllText(changelogDir, "");
}
changelogText = File.ReadAllText(changelogDir);
if(!Utils.useLocal)
{
@ -333,7 +340,6 @@ namespace PvZ_Fusion_Translator
// Default or Custom Texturse -> English Textures -> Localized Textures
try
{
if (Utils.customTextures)
{
LoadCustomTextures();
@ -365,6 +371,69 @@ namespace PvZ_Fusion_Translator
}
try
{
Dictionary<string, string> localTOC = new Dictionary<string, string>();
string localTOCDir = Path.Combine(textureDir, "Texture_TOC.json");
if (!File.Exists(localTOCDir))
{
File.WriteAllText(localTOCDir, "{}");
}
string localTOCContent = File.ReadAllText(localTOCDir);
localTOC = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(localTOCContent);
string basePath = Core.Instance.modsDirectory;
if (!Utils.useLocal)
{
string textureTOCRequest = Utils.GetDataFromWeb($"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/Localization/{Utils.Language.ToString()}/Textures/Texture_TOC.json").Result;
if (textureTOCRequest != null)
{
Dictionary<string, string> textureTOC = JsonConvert.DeserializeObject<Dictionary<string, string>>(textureTOCRequest);
List<string> toDownload = new();
foreach(var pair in textureTOC)
{
if(!localTOC.ContainsKey(pair.Key))
{
toDownload.Add(pair.Key);
continue;
}
if(pair.Value != localTOC[pair.Key])
{
toDownload.Add(pair.Key);
}
if(!File.Exists(Path.Combine(basePath, pair.Key)))
{
toDownload.Add(pair.Key);
continue;
}
}
string toWriteText = SerializeWithIndentation(textureTOC);
File.WriteAllText(localTOCDir, toWriteText);
foreach(string path in toDownload)
{
string downloadPath = Path.Combine(basePath, path);
string baseUrl = $"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/";
string url = baseUrl + path;
byte[] imageData = Utils.GetImageDataFromWeb(url).Result;
if(imageData != null)
{
string fileName = Path.GetFileName(downloadPath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[fileName] = imageData;
File.WriteAllBytes(downloadPath, imageData);
}
}
}
}
foreach (string filepath in Directory.EnumerateFiles(textureDir, "*.png", SearchOption.AllDirectories))
{
if (filepath.Contains("[Custom Textures]", StringComparison.OrdinalIgnoreCase) && loadDefaultTextures)
@ -372,6 +441,9 @@ namespace PvZ_Fusion_Translator
continue;
}
if (!localTOC.ContainsKey(filepath.Replace(basePath + "\\", "").Replace("\\", "/"))
&& (SerializeWithIndentation(localTOC) != "{}")) continue;
#if OBFUSCATE
if (CheckSumStore.IsModified(filepath))
{
@ -384,8 +456,9 @@ namespace PvZ_Fusion_Translator
Log.LogDebug("Loading File : " + filepath);
#endif
Texture2D texture2D = Utils.LoadImage(filepath);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath;
byte[] imageData = File.ReadAllBytes(filepath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = imageData;
}
}
@ -406,6 +479,70 @@ namespace PvZ_Fusion_Translator
}
try
{
string trueTextureDir = GetAssetDir(AssetType.Textures, language);
Dictionary<string, string> localTOC = new Dictionary<string, string>();
string localTOCDir = Path.Combine(trueTextureDir, "Texture_TOC.json");
if (!File.Exists(localTOCDir))
{
File.WriteAllText(localTOCDir, "{}");
}
string localTOCContent = File.ReadAllText(localTOCDir);
localTOC = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(localTOCContent);
string basePath = Core.Instance.modsDirectory;
if(!Utils.useLocal)
{
string textureTOCRequest = Utils.GetDataFromWeb($"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/Localization/{Utils.Language.ToString()}/Textures/Texture_TOC.json").Result;
if (textureTOCRequest != null)
{
Dictionary<string, string> textureTOC = JsonConvert.DeserializeObject<Dictionary<string, string>>(textureTOCRequest);
List<string> toDownload = new();
foreach(var pair in textureTOC)
{
if(!localTOC.ContainsKey(pair.Key))
{
toDownload.Add(pair.Key);
continue;
}
if(pair.Value != localTOC[pair.Key])
{
toDownload.Add(pair.Key);
}
if(!File.Exists(Path.Combine(basePath, pair.Key)))
{
toDownload.Add(pair.Key);
continue;
}
}
string toWriteText = SerializeWithIndentation(textureTOC);
File.WriteAllText(localTOCDir, toWriteText);
foreach(string path in toDownload)
{
string downloadPath = Path.Combine(basePath, path);
string baseUrl = $"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/";
string url = baseUrl + path;
byte[] imageData = Utils.GetImageDataFromWeb(url).Result;
if(imageData != null)
{
string fileName = Path.GetFileName(downloadPath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.spriteDict[fileName] = imageData;
File.WriteAllBytes(downloadPath, imageData);
}
}
}
}
foreach (string filepath in Directory.EnumerateFiles(textureDir, "*.png", SearchOption.AllDirectories))
{
if (filepath.Contains("[Custom Textures]", StringComparison.OrdinalIgnoreCase) && loadDefaultTextures)
@ -413,6 +550,9 @@ namespace PvZ_Fusion_Translator
continue;
}
if (!localTOC.ContainsKey(filepath.Replace(basePath + "\\", "").Replace("\\", "/"))
&& (SerializeWithIndentation(localTOC) != "{}")) continue;
#if OBFUSCATE
if (CheckSumStore.IsModified(filepath))
{
@ -425,8 +565,9 @@ namespace PvZ_Fusion_Translator
Log.LogDebug("Loading File (Sprite): " + filepath);
#endif
Texture2D texture2D = Utils.LoadImage(filepath);
TextureStore.spriteDict[Path.GetFileNameWithoutExtension(filepath)] = filepath;
byte[] imageData = File.ReadAllBytes(filepath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.spriteDict[Path.GetFileNameWithoutExtension(filepath)] = imageData;
}
}
@ -448,8 +589,74 @@ namespace PvZ_Fusion_Translator
}
try
{
string trueTextureDir = Path.Combine(Core.Instance.modsDirectory, "[Custom Textures]");
Dictionary<string, string> localTOC = new Dictionary<string, string>();
string localTOCDir = Path.Combine(trueTextureDir, "Default_TOC.json");
if (!File.Exists(localTOCDir))
{
File.WriteAllText(localTOCDir, "{}");
}
string localTOCContent = File.ReadAllText(localTOCDir);
localTOC = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(localTOCContent);
string basePath = Core.Instance.modsDirectory;
if (!Utils.useLocal)
{
string textureTOCRequest = Utils.GetDataFromWeb($"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/{Uri.EscapeDataString("[Custom Textures]")}/Default_TOC.json").Result;
if (textureTOCRequest != null)
{
Dictionary<string, string> textureTOC = JsonConvert.DeserializeObject<Dictionary<string, string>>(textureTOCRequest);
List<string> toDownload = new();
foreach (var pair in textureTOC)
{
if (!localTOC.ContainsKey(pair.Key))
{
toDownload.Add(pair.Key);
continue;
}
if (pair.Value != localTOC[pair.Key])
{
toDownload.Add(pair.Key);
}
if(!File.Exists(Path.Combine(basePath, pair.Key)))
{
toDownload.Add(pair.Key);
continue;
}
}
string toWriteText = SerializeWithIndentation(textureTOC);
File.WriteAllText(localTOCDir, toWriteText);
foreach (string path in toDownload)
{
string downloadPath = Path.Combine(basePath, path);
string baseUrl = $"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/";
string url = baseUrl + path;
byte[] imageData = Utils.GetImageDataFromWeb(url).Result;
if (imageData != null)
{
string fileName = Path.GetFileName(downloadPath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[fileName] = imageData;
File.WriteAllBytes(downloadPath, imageData);
}
}
}
}
foreach (string filepath in Directory.EnumerateFiles(textureDefaultDir, "*.png", SearchOption.AllDirectories))
{
if (!localTOC.ContainsKey(filepath.Replace(basePath + "\\", "").Replace("\\", "/"))
&& (SerializeWithIndentation(localTOC) != "{}")) continue;
#if OBFUSCATE
if (CheckSumStore.IsModified(filepath))
@ -459,8 +666,9 @@ namespace PvZ_Fusion_Translator
}
#endif
Texture2D texture2D = Utils.LoadImage(filepath);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath;
byte[] imageData = File.ReadAllBytes(filepath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = imageData;
}
}
@ -482,6 +690,71 @@ namespace PvZ_Fusion_Translator
}
try
{
string trueTextureDir = Path.Combine(Core.Instance.modsDirectory, "[Custom Textures]");
Dictionary<string, string> localTOC = new Dictionary<string, string>();
string localTOCDir = Path.Combine(trueTextureDir, "Custom_TOC.json");
if (!File.Exists(localTOCDir))
{
File.WriteAllText(localTOCDir, "{}");
}
string localTOCContent = File.ReadAllText(localTOCDir);
localTOC = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(localTOCContent);
string basePath = Core.Instance.modsDirectory;
if (!Utils.useLocal)
{
string textureTOCRequest = Utils.GetDataFromWeb($"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/{Uri.EscapeDataString("[Custom Textures]")}/Custom_TOC.json").Result;
if (textureTOCRequest != null)
{
Dictionary<string, string> textureTOC = JsonConvert.DeserializeObject<Dictionary<string, string>>(textureTOCRequest);
List<string> toDownload = new();
foreach (var pair in textureTOC)
{
if (!localTOC.ContainsKey(pair.Key))
{
toDownload.Add(pair.Key);
continue;
}
if (pair.Value != localTOC[pair.Key])
{
toDownload.Add(pair.Key);
continue;
}
if(!File.Exists(Path.Combine(basePath, pair.Key)))
{
toDownload.Add(pair.Key);
continue;
}
}
string toWriteText = SerializeWithIndentation(textureTOC);
File.WriteAllText(localTOCDir, toWriteText);
foreach (string path in toDownload)
{
string downloadPath = Path.Combine(basePath, path);
string baseUrl = $"https://raw.githubusercontent.com/Teyliu/PVZF-Translation/refs/heads/main/PvZ_Fusion_Translator/";
string url = baseUrl + path;
byte[] imageData = Utils.GetImageDataFromWeb(url).Result;
if (imageData != null)
{
string fileName = Path.GetFileName(downloadPath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[fileName] = imageData;
File.WriteAllBytes(downloadPath, imageData);
}
}
}
}
foreach (string filepath in Directory.EnumerateFiles(texturePackDir, "*.png", SearchOption.AllDirectories))
{
#if OBFUSCATE
@ -492,8 +765,12 @@ namespace PvZ_Fusion_Translator
}
#endif
Texture2D texture2D = Utils.LoadImage(filepath);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath;
if (!localTOC.ContainsKey(filepath.Replace(basePath + "\\", "").Replace("\\", "/"))
&& (SerializeWithIndentation(localTOC) != "{}")) continue;
byte[] imageData = File.ReadAllBytes(filepath);
Texture2D texture2D = Utils.LoadImage(imageData);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = imageData;
}
}

View file

@ -22,7 +22,7 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{
if(__instance.textHead == null) return;
if(!__instance.textHead.active) return;
if(!__instance.textHead.active || !__instance.textMesh.gameObject.active) return;
TranslateInGameText(__instance);
}
@ -33,7 +33,7 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{
if(__instance.textHead == null) return;
if(!__instance.textHead.active) return;
if(!__instance.textHead.active || !__instance.textMesh.gameObject.active) return;
TranslateInGameText(__instance);
}
@ -44,7 +44,7 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{
if(__instance.textHead == null) return;
if(!__instance.textHead.active) return;
if(!__instance.textHead.active || !__instance.textMesh.gameObject.active) return;
TranslateInGameText(__instance);
}
@ -53,7 +53,7 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{
if (__instance.textMesh == null) return;
if(!__instance.textHead.active) return;
if(!__instance.textHead.active || !__instance.textMesh.gameObject.active) return;
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());

View file

@ -162,10 +162,23 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects
btn.gameObject.SetActive(true);
string buttonText = Utils.ToggleNames[toggle];
UpdateButtonText(btn, StringStore.TranslateText(buttonText), FontStore.LoadTMPFont(Utils.Language.ToString()));
if(i == 2)
string sourceMsg = "";
switch(i)
{
string sourceMsg = "<size=10>" + (!(Utils.useLocal) ? "Translation Source:\nOnline" : "Translation Source:\nLocal");
case 0:
sourceMsg = "<size=10>" + (!(Utils.customTextures) ? "Texture Source:\nDefault" : "Texture Source:\nCustom");
FlashMessage(btn, sourceMsg, 0.1f, false);
break;
case 1:
sourceMsg = "<size=10>" + (!(Utils.customAudio) ? "Audio Source:\nDefault" : "Audio Source:\nCustom");
FlashMessage(btn, sourceMsg, 0.1f, false);
break;
case 2:
sourceMsg = "<size=10>" + (!(Utils.useLocal) ? "Translation Source:\nOnline" : "Translation Source:\nLocal");
FlashMessage(btn, sourceMsg, 0.1f, false);
break;
default:
break;
}
}
else
@ -223,8 +236,10 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects
if (child != null)
{
var text = child.GetComponent<TextMeshProUGUI>();
string originalMsg = "";
if (text != null)
{
originalMsg = text.text;
text.text = message;
OptionButtonData data;
bool isToggle = ToggleBtnDict.TryGetValue(button.GetInstanceID(), out data);
@ -248,7 +263,24 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects
{
if (button != null && child != null && child.gameObject.activeSelf)
{
child.gameObject.SetActive(false);
OptionButtonData buttonData = ToggleBtnDict[button.GetInstanceID()];
if(buttonData.Toggle != null)
{
string sourceMsg = originalMsg;
switch(buttonData.Toggle)
{
case Utils.ToggleEnum.Textures:
sourceMsg = "<size=10>" + (!(Utils.customTextures) ? "Texture Source:\nDefault" : "Texture Source:\nCustom");
break;
case Utils.ToggleEnum.Audio:
sourceMsg = "<size=10>" + (!(Utils.customAudio) ? "Audio Source:\nDefault" : "Audio Source:\nCustom");
break;
default:
break;
}
text.text = sourceMsg;
}
}
});
}
@ -331,7 +363,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.ButtonObjects
ToggleCustomAssets(toggleType);
if (toggleType == "Textures")
{
FlashMessage(toggleData.Button, "<size=10>Toggled custom textures!", 0.1f);
FlashMessage(toggleData.Button, "<size=10>Toggled custom textures!\n(Restart Required)", 0.1f);
}
else if (toggleType == "Audio")
{

View file

@ -18,6 +18,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
Transform changelogObj = __instance.transform.Find("Grave/UpdateInfo");
Transform langObj = UnityEngine.Object.Instantiate(changelogObj, changelogObj.position + new Vector3(0.0f, 0.855f), changelogObj.rotation, __instance.transform.Find("Grave"));
langObj.name = "Languages";
__instance.collider2Ds.Add(langObj.GetComponent<Collider2D>());
UIButton langBtn = langObj.GetComponent<UIButton>();
foreach(TextMeshProUGUI text in langObj.GetComponentsInChildren<TextMeshProUGUI>())
@ -27,7 +28,6 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
langBtn.clickEvent = new UnityEvent();
langBtn.clickEvent.AddListener(new Action(() => OpenLanguageMenu(langBtn.gameObject)));
langBtn.clickEvent.AddListener(new Action(() => HideLangButton(langBtn.gameObject)));
}
public static void OpenLanguageMenu(GameObject langBtn)
@ -49,7 +49,6 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
OptLanguageBtn_Patch.OptLangBtn_Patch.Awake(templateButton);
UIButton goBackButton = newOptionsMenu.transform.Find("Goback").GetComponent<UIButton>();
goBackButton.clickEvent.AddListener(new Action(() => ShowLangButton(langBtn.gameObject)));
newOptionsMenu.transform.Find("F").gameObject.SetActive(false);

View file

@ -2,23 +2,23 @@
using Il2Cpp;
using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace PvZ_Fusion_Translator.Patches.Managers
{
[HarmonyPatch(typeof(GameAPP))]
public static class GameAPP_Patch
[HarmonyPatch(typeof(Screen))]
public static class Screen_Patch
{
[HarmonyPatch(nameof(GameAPP.Update))]
[HarmonyPatch(nameof(Screen.SetResolution), argumentTypes: [typeof(int), typeof(int), typeof(bool)])]
[HarmonyPrefix]
private static bool Update(GameAPP __instance)
private static bool SetResolution(Screen __instance)
{
if(EventSystem.current.currentSelectedGameObject != null)
{
if (EventSystem.current.currentSelectedGameObject.TryGetComponent<TMP_InputField>(out TMP_InputField field))
{
__instance.MusicUpdate();
return false;
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using Il2Cpp;
using Il2CppGameLevel;
using PvZ_Fusion_Translator.AssetStore;
using UnityEngine;
namespace PvZ_Fusion_Translator.Patches.Modes.Super_Editor.GameLevel
{
[HarmonyPatch(typeof(LevelManager))]
public static class LevelManager_Patch
{
[HarmonyPatch(nameof(LevelManager.SaveLevelData))]
[HarmonyPrefix]
public static void Pre_SaveLevelData(LevelManager __instance, ref SerializedLevelData serializedLevelData)
{
if(serializedLevelData.name == "未命名关卡")
{
serializedLevelData.name = StringStore.translationString.ContainsKey("LEVEL_UNNAMED") ? StringStore.TranslateText("LEVEL_UNNAMED") : "Unnamed";
}
}
}
}

View file

@ -428,7 +428,7 @@
<Compile Include="Patches\GameObjects\PlantDamageMenu_Patch.cs" />
<Compile Include="Patches\GameObjects\EveBuffMenu_Patch.cs" />
<Compile Include="Patches\GameObjects\EveOptionWindow_Patch.cs" />
<Compile Include="Patches\Managers\GameAPP_Patch.cs" />
<Compile Include="Patches\Managers\Screen_Patch.cs" />
<Compile Include="Patches\Modes\Odyssey\OppositeBuffManager_Patch.cs" />
<Compile Include="Patches\GameObjects\ButtonObjects\ExploreMakeSceneButton_Patch.cs" />
<Compile Include="Patches\GameObjects\ButtonObjects\ExploreMakeZombieButton_Patch.cs" />
@ -510,6 +510,7 @@
<Compile Include="Patches\Modes\Super Editor\CustomLevelMenu_Patch.cs" />
<Compile Include="Patches\Modes\Super Editor\CustomMenuPage_page3_Patch.cs" />
<Compile Include="Patches\Modes\Super Editor\CustomMenuPage_initZombies_Patch.cs" />
<Compile Include="Patches\Modes\Super Editor\GameLevel\LevelManager_Patch.cs" />
<Compile Include="Patches\OtherManagers\AchievementClip_Patch.cs" />
<Compile Include="Patches\GameObjects\ButtonObjects\Advanture_Btn_Patch.cs" />
<Compile Include="Patches\Managers\AlmanacMgrZombie_Patch.cs" />

View file

@ -28,11 +28,11 @@ namespace PvZ_Fusion_Translator
{
if (ogTexture != null)
{
if (TextureStore.textureDict.TryGetValue(ogTexture.name, out string texturePath))
if (TextureStore.textureDict.TryGetValue(ogTexture.name, out byte[] textureData))
{
try
{
ImageConversion.LoadImage(ogTexture, File.ReadAllBytes(texturePath));
ImageConversion.LoadImage(ogTexture, textureData);
Log.LogDebug("OK! Replaced Texture " + ogTexture.name);
@ -41,7 +41,7 @@ namespace PvZ_Fusion_Translator
}
catch (Exception ex)
{
Log.LogError("Failed to replace texture: " + ogTexture.name + " at path: " + texturePath);
Log.LogError("Failed to replace texture: " + ogTexture.name);
Log.LogError(ex.ToString());
}
}
@ -62,6 +62,19 @@ namespace PvZ_Fusion_Translator
return texture2D;
}
internal static Texture2D LoadImage(byte[] bytes)
{
if (bytes == null)
{
throw new FileNotFoundException($"Byte array was null.");
}
byte[] array = bytes;
Texture2D texture2D = new(2, 2, GraphicsFormat.R8G8B8A8_UNorm, TextureCreationFlags.None, 1, IntPtr.Zero, null);
ImageConversion.LoadImage(texture2D, array);
return texture2D;
}
public static Color HexToColor(string hex)
{
@ -362,7 +375,7 @@ namespace PvZ_Fusion_Translator
{
Utils.useLocal = (Utils.useLocal) ? false : true;
MelonPreferences.SetEntryValue<bool>("PvZ_Fusion_Translator", "UseLocal", Utils.useLocal);
StringStore.Reload();
ChangeLanguage(Utils.Language.ToString());
string sourceMsg = "<size=10>" + (!(Utils.useLocal) ? "Translation Source:\nOnline" : "Translation Source:\nLocal");
OptLanguageBtn_Patch.FlashMessage(OptLanguageBtn_Patch.toggleSlots[2], sourceMsg, 0.1f, false);
}
@ -373,7 +386,99 @@ namespace PvZ_Fusion_Translator
{
Log.LogInfo($"Attempting to read {url}");
}
var dataRequest = await new HttpClient().GetAsync(url);
HttpClient client = new HttpClient();
try
{
var dataRequest = await client.GetAsync(url);
if(dataRequest.StatusCode == System.Net.HttpStatusCode.OK)
{
if (isLog)
{
Log.LogInfo($"Successfully loaded data from {url}!");
}
string content = await dataRequest.Content.ReadAsStringAsync();
return content;
}
else
{
if (isLog)
{
Log.LogError($"Failed to load data from {url}! Falling back...");
Log.LogError($"Status code: {dataRequest.StatusCode}");
}
new CancellationTokenSource().Cancel();
return null;
}
}
catch(Exception ex)
{
Log.LogError($"Failed to load data from {url}! Falling back...");
Log.LogError(ex);
new CancellationTokenSource().Cancel();
return null;
}
}
public static async Task<byte[]> GetImageDataFromWeb(string url, bool isLog = true)
{
if (isLog)
{
Log.LogInfo($"Attempting to read {url}");
}
HttpClient client = new HttpClient();
try
{
var dataRequest = await client.GetAsync(url);
if (dataRequest.StatusCode == System.Net.HttpStatusCode.OK)
{
if (isLog)
{
Log.LogInfo($"Successfully loaded data from {url}!");
}
byte[] content = await dataRequest.Content.ReadAsByteArrayAsync();
return content;
}
else
{
if (isLog)
{
Log.LogError($"Failed to load data from {url}! Falling back...");
Log.LogError($"Status code: {dataRequest.StatusCode}");
}
new CancellationTokenSource().Cancel();
return null;
}
}
catch(Exception ex)
{
Log.LogError($"Failed to load data from {url}! Falling back...");
Log.LogError(ex);
new CancellationTokenSource().Cancel();
return null;
}
}
public static async Task<string> GetDataFromWebWithInput(string url, bool isLog = true)
{
if (isLog)
{
Log.LogInfo($"Attempting to read {url}");
}
HttpClient client = new HttpClient();
using var request = new HttpRequestMessage()
{
Method = HttpMethod.Get,
RequestUri = new Uri(url)
};
request.Headers.Add("Accept", "*/*");
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
request.Headers.Add("User-Agent", "TranslatorMod");
var dataRequest = await client.SendAsync(request);
if(dataRequest.StatusCode == System.Net.HttpStatusCode.OK)
{
if (isLog)
@ -478,8 +583,8 @@ namespace PvZ_Fusion_Translator
public static Dictionary<ToggleEnum, string> ToggleNames = new Dictionary<ToggleEnum, string>()
{
{ ToggleEnum.Textures, "Textures"},
{ ToggleEnum.Audio, "Audio"},
{ ToggleEnum.Textures, "<size=85%>Change Texture\nSource"},
{ ToggleEnum.Audio, "<size=85%>Change Audio\nSource"},
{ ToggleEnum.SwapLocal, "<size=85%>Change Translation\nSource"}
};