restree leftovers

This commit is contained in:
bird_egop
2026-05-09 18:39:26 +03:00
parent 8091a727ba
commit d73a6dde70
5 changed files with 169 additions and 74 deletions
+83 -42
View File
@@ -1,4 +1,4 @@
using System.Text;
using System.Text;
using Common;
using NResLib;
@@ -6,7 +6,7 @@ namespace ResTreeLib;
public class ResTreeParser
{
private static readonly Encoding CyrillicEncoding = CodePagesEncodingProvider.Instance.GetEncoding(1251)
private static readonly Encoding CyrillicEncoding = CodePagesEncodingProvider.Instance.GetEncoding(1251)
?? Encoding.GetEncoding("windows-1251");
public static List<ResearchNodeData> Parse(string filePath)
@@ -16,25 +16,23 @@ public class ResTreeParser
var nres = NResParser.ReadFile(fs);
fs.Seek(0, SeekOrigin.Begin);
return Parse(nres.Archive!, fs);
}
public static List<ResearchNodeData> Parse(NResArchive archive, Stream stream)
{
// Register provider for Windows-1251 if not already done globally
// Register provider for Windows-1251 if not already done globally.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (archive.Files.Any(x => x.FileType == "TRFB"))
// 1. Locate the master nodes (TRF0).
var trf0 = archive.Files.FirstOrDefault(f => f.FileType == "TRF0");
if (trf0 == null)
{
_ = 5;
return new List<ResearchNodeData>();
}
// 1. Locate the Master Nodes (TRF0)
var trf0 = archive.Files.FirstOrDefault(f => f.FileType == "TRF0");
if (trf0 == null) return new List<ResearchNodeData>();
// 2. Load the Node States (TRF1) - 1 byte per node
// 2. Load node states (TRF1): one byte per node.
byte[] initialStates = LoadRawBuffer(stream, archive, "TRF1");
var nodes = new List<Trf0Element>();
@@ -44,21 +42,21 @@ public class ResTreeParser
nodes.Add(ReadTrf0Element(stream));
}
// 3. Load String Pools
byte[] shortNamesPool_trf7 = LoadRawBuffer(stream, archive, "TRF7");
byte[] longNamesPool_trf8 = LoadRawBuffer(stream, archive, "TRF8");
byte[] helpTextPool_trf9 = LoadRawBuffer(stream, archive, "TRF9");
byte[] descriptionsPool_trfa = LoadRawBuffer(stream, archive, "TRFA");
// 3. Load string pools.
byte[] shortNamesPoolTrf7 = LoadRawBuffer(stream, archive, "TRF7");
byte[] longNamesPoolTrf8 = LoadRawBuffer(stream, archive, "TRF8");
byte[] helpTextPoolTrf9 = LoadRawBuffer(stream, archive, "TRF9");
byte[] descriptionsPoolTrfa = LoadRawBuffer(stream, archive, "TRFA");
// 4. Load Relationship Maps
// TRF2/3 = Prerequisites
var prereqMap = LoadRelationMap(stream, archive, "TRF2", "TRF3");
// TRF4/5 = Unlocks/Effects
var unlockMap = LoadRelationMap(stream, archive, "TRF4", "TRF5");
// TRFB/6 = unknown
var aux_trf6 = LoadRelationMap(stream, archive, "TRFB", "TRF6");
// 4. Load relationship maps.
var prereqMap = LoadRelationMap(stream, archive, "TRF2", "TRF3", strict: true);
var unlockMap = LoadRelationMap(stream, archive, "TRF4", "TRF5", strict: true);
// 5. Assemble
// TRFB/TRF6 is auxiliary and currently unknown.
// Real game files may have inconsistent counts here, so keep it non-strict.
_ = LoadRelationMap(stream, archive, "TRFB", "TRF6", strict: false);
// 5. Assemble DTOs.
var result = new List<ResearchNodeData>();
for (int i = 0; i < nodes.Count; i++)
{
@@ -67,12 +65,11 @@ public class ResTreeParser
{
Index = i,
Node = element,
// Cast the TRF1 byte to our NodeState Flags
State = (initialStates.Length > i) ? (NodeState)initialStates[i] : NodeState.Hidden,
ShortName = GetStringFromPool(shortNamesPool_trf7, element.OffsetShortName_TRF7),
LongName = GetStringFromPool(longNamesPool_trf8, element.OffsetLongName_TRF8),
HelpText = GetStringFromPool(helpTextPool_trf9, element.OffsetHelpText_TRF9),
Description = GetStringFromPool(descriptionsPool_trfa, element.OffsetDescription_TRFA),
State = initialStates.Length > i ? (NodeState)initialStates[i] : NodeState.Hidden,
ShortName = GetStringFromPool(shortNamesPoolTrf7, element.OffsetShortName_TRF7),
LongName = GetStringFromPool(longNamesPoolTrf8, element.OffsetLongName_TRF8),
HelpText = GetStringFromPool(helpTextPoolTrf9, element.OffsetHelpText_TRF9),
Description = GetStringFromPool(descriptionsPoolTrfa, element.OffsetDescription_TRFA),
PrerequisiteIds = prereqMap.TryGetValue(i, out var pValue) ? pValue : [],
UnlockIds = unlockMap.TryGetValue(i, out var uValue) ? uValue : []
});
@@ -83,7 +80,7 @@ public class ResTreeParser
private static Trf0Element ReadTrf0Element(Stream s)
{
// Total size must be 40 bytes (0x28)
// TRF0 element size is 40 bytes (0x28).
return new Trf0Element
{
ResearchCost = s.ReadFloatLittleEndian(),
@@ -104,14 +101,21 @@ public class ResTreeParser
};
}
// LoadRelationMap and LoadRawBuffer remain the same as your provided code
private static Dictionary<int, uint[]> LoadRelationMap(Stream s, NResArchive archive, string headerType, string dataType)
private static Dictionary<int, uint[]> LoadRelationMap(
Stream s,
NResArchive archive,
string headerType,
string dataType,
bool strict)
{
var header = archive.Files.FirstOrDefault(f => f.FileType == headerType);
var data = archive.Files.FirstOrDefault(f => f.FileType == dataType);
var map = new Dictionary<int, uint[]>();
if (header == null || data == null) return map;
if (header == null || data == null)
{
return map;
}
uint[] counts = new uint[header.ElementCount];
s.Position = header.OffsetInFile;
@@ -120,15 +124,46 @@ public class ResTreeParser
counts[i] = s.ReadUInt32LittleEndian();
}
long dataWordsRequired = 0;
for (int i = 0; i < counts.Length; i++)
{
dataWordsRequired += counts[i];
}
if (dataWordsRequired * sizeof(uint) > data.FileLength)
{
if (strict)
{
throw new InvalidDataException(
$"Relation map {headerType}/{dataType} expects {dataWordsRequired * sizeof(uint)} bytes but section has {data.FileLength} bytes.");
}
return map;
}
long dataEnd = data.OffsetInFile + data.FileLength;
s.Position = data.OffsetInFile;
for (int i = 0; i < counts.Length; i++)
{
uint count = counts[i];
if (s.Position + count * sizeof(uint) > dataEnd)
{
if (strict)
{
throw new EndOfStreamException(
$"Relation map {headerType}/{dataType} overreads section at node {i} (count={count}).");
}
break;
}
uint[] ids = new uint[count];
for (int j = 0; j < count; j++)
{
ids[j] = s.ReadUInt32LittleEndian();
}
map[i] = ids;
}
@@ -138,7 +173,10 @@ public class ResTreeParser
private static byte[] LoadRawBuffer(Stream s, NResArchive archive, string type)
{
var item = archive.Files.FirstOrDefault(f => f.FileType == type);
if (item == null) return [];
if (item == null)
{
return [];
}
byte[] buffer = new byte[item.FileLength];
s.Position = item.OffsetInFile;
@@ -148,11 +186,14 @@ public class ResTreeParser
private static string GetStringFromPool(byte[] pool, uint offset)
{
if (pool.Length == 0 || offset >= pool.Length) return string.Empty;
if (pool.Length == 0 || offset >= pool.Length)
{
return string.Empty;
}
int end = Array.IndexOf(pool, (byte)0, (int)offset);
int length = (end == -1) ? pool.Length - (int)offset : end - (int)offset;
int length = end == -1 ? pool.Length - (int)offset : end - (int)offset;
return CyrillicEncoding.GetString(pool, (int)offset, length);
}
}
}