diff --git a/Assets/PerfectWorld/Scripts/Common/EC_StringTab.cs b/Assets/PerfectWorld/Scripts/Common/EC_StringTab.cs index 985a0f79bf..1ac8e27d3a 100644 --- a/Assets/PerfectWorld/Scripts/Common/EC_StringTab.cs +++ b/Assets/PerfectWorld/Scripts/Common/EC_StringTab.cs @@ -1,124 +1,162 @@ -using System; -using System.Collections.Generic; -using System.IO; -using ModelRenderer.Scripts.Common; - -namespace BrewMonster.Common -{ - public class CECStringTab - { - - private Dictionary m_AStrTab = new Dictionary(); - private Dictionary m_WStrTab = new Dictionary(); - - protected bool m_bInit = false; - protected bool m_bUnicode = false; - - public bool IsInitialized() => m_bInit; - - public void Clear() - { - m_AStrTab.Clear(); - m_WStrTab.Clear(); - m_bInit = false; - m_bUnicode = false; - } - - public bool Init(string szFile, bool bUnicode) - { - bool bRet = false; - - if (bUnicode) - { - bRet = LoadWideStrings(szFile); - } - - if (!bRet) - { - BMLogger.LogError($"EC_StringTab::Init: {szFile} File load failed"); - return false; - } - - m_bInit = true; - return true; - } - - private bool LoadWideStrings(string szFile) - { - AWScriptFile ScriptFile = new AWScriptFile(); - if (!ScriptFile.Open(szFile)) return false; - - bool bIndex = false; - bool bFileEnd = true; - - // Read configs - while (ScriptFile.GetNextToken(true)) - { - string tokenStr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); - if (ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken).StartsWith("#_index")) - bIndex = true; - else if (ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken).StartsWith("#_begin")) - { - bFileEnd = false; - break; - } - } - - if (bFileEnd) - { - ScriptFile.Close(); - return true; - } - - if (bIndex) - { - // Every string has a preset index - while (ScriptFile.PeekNextToken(true)) - { - int n = ScriptFile.GetNextTokenAsInt(true); - ScriptFile.GetNextToken(false); - - string pstr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); - if (string.IsNullOrEmpty(pstr)) - { - ScriptFile.Close(); - BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Not enough memory"); - return false; - } - - if (!m_WStrTab.TryAdd(n, pstr)) - { - BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Failed to add string to dictionary"); - return false; - } - } - } - else - { - int iCnt = 0; - - // Read strings sequently - while (ScriptFile.GetNextToken(true)) - { - string pstr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); - if (string.IsNullOrEmpty(pstr)) - { - ScriptFile.Close(); - BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Not enough memory"); - return false; - } - - if (!m_WStrTab.TryAdd(iCnt++, pstr)) - { - BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Failed to add string to dictionary"); - return false; - } - } - } - - ScriptFile.Close(); - - return true; - } - } +using System; +using System.Collections.Generic; +using System.IO; +using ModelRenderer.Scripts.Common; + +namespace BrewMonster.Common +{ + public class CECStringTab + { + + private Dictionary m_AStrTab = new Dictionary(); + private Dictionary m_WStrTab = new Dictionary(); + + protected bool m_bInit = false; + protected bool m_bUnicode = false; + + public bool IsInitialized() => m_bInit; + + public void Clear() + { + m_AStrTab.Clear(); + m_WStrTab.Clear(); + m_bInit = false; + m_bUnicode = false; + } + + public bool Init(string szFile, bool bUnicode) + { + bool bRet = false; + + if (bUnicode) + { + bRet = LoadWideStrings(szFile); + } + + if (!bRet) + { + BMLogger.LogError($"EC_StringTab::Init: {szFile} File load failed"); + return false; + } + + m_bInit = true; + return true; + } + + private bool LoadWideStrings(string szFile) + { + AWScriptFile ScriptFile = new AWScriptFile(); + if (!ScriptFile.Open(szFile)) return false; + + bool bIndex = false; + bool bFileEnd = true; + + // Read configs + while (ScriptFile.GetNextToken(true)) + { + string tokenStr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); + if (ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken).StartsWith("#_index")) + bIndex = true; + else if (ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken).StartsWith("#_begin")) + { + bFileEnd = false; + break; + } + } + + if (bFileEnd) + { + ScriptFile.Close(); + return true; + } + + if (bIndex) + { + // Every string has a preset index + while (ScriptFile.PeekNextToken(true)) + { + int n = ScriptFile.GetNextTokenAsInt(true); + ScriptFile.GetNextToken(false); + + string pstr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); + if (string.IsNullOrEmpty(pstr)) + { + ScriptFile.Close(); + BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Not enough memory"); + return false; + } + + if (!m_WStrTab.TryAdd(n, pstr)) + { + BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Failed to add string to dictionary"); + return false; + } + } + } + else + { + int iCnt = 0; + + // Read strings sequently + while (ScriptFile.GetNextToken(true)) + { + string pstr = ByteToStringUtils.UshortArrayToUnicodeString(ScriptFile.m_szToken); + if (string.IsNullOrEmpty(pstr)) + { + ScriptFile.Close(); + BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Not enough memory"); + return false; + } + + if (!m_WStrTab.TryAdd(iCnt++, pstr)) + { + BMLogger.LogError($"EC_StringTab::LoadWideStrings: {szFile} Failed to add string to dictionary"); + return false; + } + } + } + + ScriptFile.Close(); + + return true; + } + + /// + /// Get a string by index from the Unicode string table + /// + /// The index of the string to retrieve + /// The string at the given index, or null if not found + public string GetString(int index) + { + if (m_WStrTab.TryGetValue(index, out string result)) + { + return result; + } + return null; + } + + /// + /// Get a string by index from the ANSI string table + /// + /// The index of the string to retrieve + /// The string at the given index, or null if not found + public string GetANSIString(int index) + { + if (m_AStrTab.TryGetValue(index, out string result)) + { + return result; + } + return null; + } + + /// + /// Check if a string exists at the given index + /// + /// The index to check + /// True if a string exists at the index + public bool HasString(int index) + { + return m_WStrTab.ContainsKey(index) || m_AStrTab.ContainsKey(index); + } + } } \ No newline at end of file