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; } } }