Files
test/Assets/PerfectWorld/Scripts/Common/EC_StringTab.cs
T
Le Duc Anh 142611dddd StringTab
2025-10-11 15:57:15 +07:00

124 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using ModelRenderer.Scripts.Common;
namespace BrewMonster.Common
{
public class CECStringTab
{
private Dictionary<int, string> m_AStrTab = new Dictionary<int, string>();
private Dictionary<int, string> m_WStrTab = new Dictionary<int, string>();
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;
}
}
}