我正在尝试为无限引擎创建的游戏(Baldur's gate、planescape Torment 等)开发一个改装工具。我知道 Infinity Engine 文件中有哪些类型的数据结构,我正在从 byte[] 内容映射一些对象。然而,它缺乏一些性能,我对此并不满意。
我在托管世界上的映射对象是结构,我知道它们代表了多少字节。因此我决定在不安全的代码中进行映射过程。我对 Uint、ushort 之类的值类型值没有任何问题,但是在字符串类型中我遇到了 char[] 编码问题。
例如,您可以在 c# 中看到 struct gam 文件的 Header,
http://iesdp.gibberlings3.net/file_formats/ie_formats/gam_v2.0.htm
[StructLayout(LayoutKind.Explicit, Size = 180,CharSet = CharSet.Ansi)]
public unsafe struct Header
{
[FieldOffset(0)]
public fixed char Signature[4];
[FieldOffset(0x0004)]
public fixed char Version[4];
[FieldOffset(0x0008)] public uint GameTime;
[FieldOffset(0x000c)] public ushort SelectedFormation;
[FieldOffset(0x000e)] public ushort FormationButton1;
[FieldOffset(0x0010)] public ushort FormationButton2;
[FieldOffset(0x0012)] public ushort FormationButton3;
[FieldOffset(0x0014)] public ushort FormationButton4;
[FieldOffset(0x0016)] public ushort FormationButton5;
[FieldOffset(0x0018)] public uint PartyGold;
[FieldOffset(0x001c)] public ushort NpcStructCountForPartyEx;
[FieldOffset(0x001e)] public ushort WeatherBitfield;
[FieldOffset(0x0020)] public uint NpcStructOffsetForParty;
[FieldOffset(0x0024)] public uint NpcStructCountForPartyInc;
[FieldOffset(0x0028)] public uint Unknown1;
[FieldOffset(0x002c)] public uint Unknown2;
[FieldOffset(0x0030)] public uint NpcStructOffsetForNonParty;
[FieldOffset(0x0034)] public uint NpcStructCountForNonParty;
[FieldOffset(0x0038)] public uint GlobalNamespaceVarOffset;
[FieldOffset(0x003c)] public uint GlobalNamespaceVarCount;
[FieldOffset(0x0040)] public fixed char MainArea[8];
[FieldOffset(0x0048)] public uint FamilarExtraOffset;
[FieldOffset(0x004c)] public uint JournalEntriesCount;
[FieldOffset(0x0050)] public uint JournalEntriesOffset;
[FieldOffset(0x0054)] public uint PartyReputation;
[FieldOffset(0x0058)] public fixed char CurrentArea [8];
[FieldOffset(0x0060)] public uint GuiFlags;
[FieldOffset(0x0064)] public uint LoadingProgress;
[FieldOffset(0x0068)] public uint FamilarInfoOffset;
[FieldOffset(0x006c)] public uint StoredLocOffset;
[FieldOffset(0x0070)] public uint StoredLocCount;
[FieldOffset(0x0074)] public uint GameTimeSec;
[FieldOffset(0x0078)] public uint PocketPlaneOffset;
[FieldOffset(0x007c)] public uint PocketPlaneCount;
[FieldOffset(0x0080)] public fixed byte Unknown3 [52];
}
这就是映射 this 对象的方式;
string path = @"C:\Baldur.gam";
byte[] content = IoHelper.ReadBinaryFile(path);
unsafe
{
fixed (byte* pointContent = content)
{
Header* header = (Header*) pointContent;
}
}
这是我拥有的 char 签名的结果;
[0] = 21057 '剁' [1] = 13624 '㔸'
这些字符真的不合理。我猜这是由编码引起的。有谁知道这个的解决方案?或者我是否必须自己读取带有 Encoding.ASCII.GetString 的字符数组?如果我用这种方法读取一个特定大小的字节 [],我会得到真正的结果。
这些结构也有不同的版本。作为上面示例中的示例,有一个 V.2.0 版本的 Baldur's Gate 2 Gam 文件。然而,在 Baldur's Gate 1 中,版本更改为 v.1.0,这不适用于我的方法。我想阅读这些不同的方法并将它们映射到模型中,然后将它们发送回 UI 层。你对阅读不同类型的版本有什么建议吗?
提前致谢。