162 lines
5.2 KiB
C#
162 lines
5.2 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.LogWarning($"EC_StringTab::LoadWideStrings: {szFile} Not enough memory");
|
|
return false;
|
|
}
|
|
|
|
if (!m_WStrTab.TryAdd(n, pstr))
|
|
{
|
|
BMLogger.LogWarning($"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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a string by index from the Unicode string table
|
|
/// </summary>
|
|
/// <param name="index">The index of the string to retrieve</param>
|
|
/// <returns>The string at the given index, or null if not found</returns>
|
|
public string GetString(int index)
|
|
{
|
|
if (m_WStrTab.TryGetValue(index, out string result))
|
|
{
|
|
return result;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a string by index from the ANSI string table
|
|
/// </summary>
|
|
/// <param name="index">The index of the string to retrieve</param>
|
|
/// <returns>The string at the given index, or null if not found</returns>
|
|
public string GetANSIString(int index)
|
|
{
|
|
if (m_AStrTab.TryGetValue(index, out string result))
|
|
{
|
|
return result;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if a string exists at the given index
|
|
/// </summary>
|
|
/// <param name="index">The index to check</param>
|
|
/// <returns>True if a string exists at the index</returns>
|
|
public bool HasString(int index)
|
|
{
|
|
return m_WStrTab.ContainsKey(index) || m_AStrTab.ContainsKey(index);
|
|
}
|
|
}
|
|
} |