Load addressable file update

This commit is contained in:
HungDK
2025-12-26 11:42:24 +07:00
parent e9042d2da9
commit f95e9813aa
+85 -31
View File
@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace BrewMonster.UI
{
@@ -37,9 +39,9 @@ namespace BrewMonster.UI
public virtual void Init()
{
ImportStringTable("ingame.stf");
ImportAuiDialogStringTable("msgbox.stf");
}
ImportStringTable("Assets/Addressable/ingame.txt");
ImportAuiDialogStringTable("Assets/Addressable/msgbox.txt");
}
public string Translate(ushort[] str)
{
@@ -134,24 +136,33 @@ namespace BrewMonster.UI
public bool ImportStringTable(string pszFilename)
{
//AWScriptFile s = new AWScriptFile();
string szFilename = Path.Combine(Application.streamingAssetsPath, pszFilename);
foreach (var line in File.ReadLines(szFilename))
var ta = LoadStringTableTextAssetByAddressables(pszFilename);
if (ta == null || string.IsNullOrEmpty(ta.text))
{
if (string.IsNullOrWhiteSpace(line))
continue;
BMLogger.LogError($"[AUIManager] ImportStringTable failed: cannot load Addressables TextAsset for key='{pszFilename}'");
return false;
}
var parts = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
continue;
if (int.TryParse(parts[0], out int key))
using (var sr = new StringReader(ta.text))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string value = parts[1].Trim();
if (value.StartsWith("\"") && value.EndsWith("\""))
value = value.Substring(1, value.Length - 2);
if (string.IsNullOrWhiteSpace(line))
continue;
m_StringTable[key] = value;
var parts = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
continue;
if (int.TryParse(parts[0], out int key))
{
string value = parts[1].Trim();
if (value.StartsWith("\"") && value.EndsWith("\""))
value = value.Substring(1, value.Length - 2);
m_StringTable[key] = value;
}
}
}
@@ -211,7 +222,12 @@ namespace BrewMonster.UI
//int idString;
//string str;
//AWScriptFile s = new AWScriptFile();
string szFilename = Path.Combine(Application.streamingAssetsPath, pszFilename);
var ta = LoadStringTableTextAssetByAddressables(pszFilename);
if (ta == null || string.IsNullOrEmpty(ta.text))
{
BMLogger.LogError($"[AUIManager] ImportAuiDialogStringTable failed: cannot load Addressables TextAsset for key='{pszFilename}'");
return false;
}
//bval = s.Open(szFilename);
//if (!bval) return true; // Ignore error.
//while (!s.IsEnd())
@@ -235,27 +251,65 @@ namespace BrewMonster.UI
//}
//s.Close();
foreach (var line in File.ReadLines(szFilename))
using (var sr = new StringReader(ta.text))
{
if (string.IsNullOrWhiteSpace(line))
continue;
var parts = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
continue;
if (int.TryParse(parts[0], out int key))
string line;
while ((line = sr.ReadLine()) != null)
{
string value = parts[1].Trim();
if (value.StartsWith("\"") && value.EndsWith("\""))
value = value.Substring(1, value.Length - 2);
if (string.IsNullOrWhiteSpace(line))
continue;
m_auiDialog_stringTable[key] = value;
var parts = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
continue;
if (int.TryParse(parts[0], out int key))
{
string value = parts[1].Trim();
if (value.StartsWith("\"") && value.EndsWith("\""))
value = value.Substring(1, value.Length - 2);
m_auiDialog_stringTable[key] = value;
}
}
}
return true;
}
private static TextAsset LoadStringTableTextAssetByAddressables(string key)
{
try
{
// Initialize Addressables if not already initialized (Unity-safe)
Addressables.InitializeAsync().WaitForCompletion();
// Load using Addressables directly with WaitForCompletion (Unity-safe, won't deadlock)
// This matches the pattern used in EC_Game.cs
var handle = Addressables.LoadAssetAsync<TextAsset>(key);
var textAsset = handle.WaitForCompletion();
if (handle.Status == AsyncOperationStatus.Succeeded && textAsset != null)
{
// Keep the handle valid; string tables are used for the whole session
// Note: We don't release the handle here to keep the asset loaded
return textAsset;
}
if (handle.IsValid())
{
Addressables.Release(handle);
}
BMLogger.LogError($"[AUIManager] Failed to load TextAsset for key='{key}'");
return null;
}
catch (Exception e)
{
BMLogger.LogError($"[AUIManager] LoadStringTableTextAssetByAddressables exception for key='{key}': {e}");
return null;
}
}
public AUIDialog GetDialog(string pszName)
{
if(m_DlgName.TryGetValue(pszName, out AUIDialog dlg))