结构阅读理论问题 [英] Structure Reading Theory Problem

查看:74
本文介绍了结构阅读理论问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DBC文件,它是游戏的数据库文件,其中包含游戏中可用的咒语数据,例如ID,SpellName,Category等. 结构是这样的:

Iam have a DBC file, which is a database file for a game, containing ingame usable spell data, like ID, SpellName, Category etc... Struct is something like this:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct SpellEntry
    {
        public uint ID;
        public uint Category;
        public float speed;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.I4)]
        public int[] Reagent;
        public int EquippedItemClass;
        [MarshalAs(UnmanagedType.LPStr)] // Crash here
        public string SpellName;
    }

我用二进制读取器读取文件,并将其编组到结构中.片段:

Iam reading the file with a binary reader, and marshaling it to the struct. Snippet:

                    binReader.BaseStream.Seek(DBCFile.HEADER_SIZE + (index * 4 * 234), SeekOrigin.Begin);
                    buff = binReader.ReadBytes(buff.Length);
                    GCHandle handdle = GCHandle.Alloc(buff, GCHandleType.Pinned);
                    Spell.SpellEntry testspell = (Spell.SpellEntry)Marshal.PtrToStructure(handdle.AddrOfPinnedObject(), typeof(Spell.SpellEntry));
                    handdle.Free();

现在变得更复杂了,让我们看看DBC文件如何存储字符串,例如SpellName.它不在记录中,字符串包含在文件的末尾字符串表"块中.记录中的字符串数据包含字符串表中字符串的数字(偏移量). (因此它不是真正的字符串).

Now to be more complex, lets see how does the DBC file storing the strings, for example the SpellName. Its not in the records, strings are contained in the end of the file, in a "string table" block. The string data in the records contains a number (offset) to the string in the string table. (so its not really a string).

我设法从字符串块(位于文件末尾)读取所有字符串,直到一个string []. (在开始阅读记录之前不要这样做) 然后,我将开始阅读记录,但是第一个问题是:

I managed to read all the strings from the string block (at the end of the file), to a string[]. (this is dont before start reading the records) Then I would start reading the records, but first problem Is :

1.)我无法阅读它,因为它在结构的最后一行崩溃"了(因为它实际上不是字符串) 2.)我无法为数字指定字符串.

1.) I cant read it, because it "crashes" on the last line of my struct (because its not a string really) 2.) I cant assign a string to the number.

当我阅读它时,它将是一个数字,但是最后,我必须将该字符串分配给SpellName,该字符串由数字在字符串表中指出.耶兹.

When I read it, it will be a number, but at the end, as a result, I have to assign that string to the SpellName, thats got pointed by the number, in the string table. Jeez .

推荐答案

public struct SpellEntry
{
    //...
    private int SpellNameOffset;
    public string SpellName {
        get { return Mumble.GetString(SpellNameOffset); }
    }
}

这很难解决,因为您不能将任何成员添加到SpellEntry中,所以Mumble必须是静态类.这弄糟了Marshal.SizeOf(),使其过大.您需要初始化Mumble,以便其静态GetString()方法可以访问字符串表.将SpellName属性移到另一个类中可以解决此问题,但也会使代码难看.

This is hard to get right, Mumble must be a static class since you cannot add any members to SpellEntry. That screws up Marshal.SizeOf(), making it too large. You'll need to initialize Mumble so that its static GetString() method can access the string table. Moving the SpellName property into another class solves the problem but makes the code ugly too.

这很容易使您感到困惑.如果您使用的是使用BitConverter的版本,那么改用它肯定会更好.实际上,将文件格式与运行时格式分开很重要.

This is liable to confuse you badly. If you got a version going that uses BitConverter then you're definitely better off by using it instead. Separating the file format from the runtime format is in fact an asset here.

这篇关于结构阅读理论问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆