.net - 具有多维数组的 Protobuf

我正在为用 c# xna 开发的游戏保存和加载。我有一长串我正在尝试序列化的类数组,但我遇到了其中一个问题。我完成了如下类(class)

 [ProtoContract]
public class CompleteTile
{
    int _id;
    [ProtoMember(1)]
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    bool _Passable;
    [ProtoMember(2)]
    public bool Passable
    {
        get { return _Passable; }
        set { _Passable = value; }
    }

我可以使用

成功序列化它
using (var stream = File.OpenWrite("" + saveDestination + "level.dat"))
        { Serializer.Serialize(stream, Globals.levelArray); }

但是当我尝试反序列化时,我得到以下信息 - 类型不是预期的,也无法推断出契约(Contract):GameName1.CompleteTile[,,]

反序列化代码——

        using (var stream = File.OpenRead("" + loadDestination + "level.dat"))
        { Globals.levelArray = Serializer.Deserialize<CompleteTile[, ,]>(stream); }

我怀疑这是因为它是一个多维数组,但我不确定,如有任何帮助,我们将不胜感激。

最佳答案

如前所述,protobuf 不支持多维数组。但是,您可以通过将多维数组转换为一维数组 + 整数维数组来规避此限制。为了减轻痛苦,我使用了一些扩展

public static class Extensions
{

    #region Multidimensional array handling

    public static ProtoArray<T> ToProtoArray<T>(this Array array)
    {
        // Copy dimensions (to be used for reconstruction).
        var dims = new int[array.Rank];
        for (int i = 0; i < array.Rank; i++) dims[i] = array.GetLength(i);
        // Copy the underlying data.
        var data = new T[array.Length];
        var k = 0;
        array.MultiLoop(indices => data[k++] = (T) array.GetValue(indices));

        return new ProtoArray<T> {Dimensions = dims, Data = data};
    }

    public static Array ToArray<T>(this ProtoArray<T> protoArray)
    {
        // Initialize array dynamically.
        var result = Array.CreateInstance(typeof(T), protoArray.Dimensions);
        // Copy the underlying data.
        var k = 0;
        result.MultiLoop(indices => result.SetValue(protoArray.Data[k++], indices));

        return result;
    }

    #endregion

    #region Array extensions

    public static void MultiLoop(this Array array, Action<int[]> action)
    {
        array.RecursiveLoop(0, new int[array.Rank], action);
    }

    private static void RecursiveLoop(this Array array, int level, int[] indices, Action<int[]> action)
    {
        if (level == array.Rank)
        {
            action(indices);
        }
        else
        {
            for (indices[level] = 0; indices[level] < array.GetLength(level); indices[level]++)
            {
                RecursiveLoop(array, level + 1, indices, action);
            }
        }
    }

    #endregion
}

[ProtoContract]
public class ProtoArray<T>
{
    [ProtoMember(1)]
    public int[] Dimensions { get; set; }
    [ProtoMember(2)]
    public T[] Data { get; set; }
}

并将每个多维数组持久化为 ProtoArray。

https://stackoverflow.com/questions/22023405/

相关文章:

iteration - 如何确定 Kohonen 自组织图上的最佳聚类数和迭代数?

ruby-on-rails - 在 rails 开发环境 smtp cpanel 上发送电子邮件 r

jdbc - 在 Netty 消息处理中使用 JDBC 事务

asp.net-mvc - MVC 层和 DAL 和数据层

Django 1.6 - 如何将显示的文本缩短到模板中?

command-line - 无法在 cygwin 上运行 isql

localization - 使用 Google 翻译插件自动翻译网站(IP 和接受语言)?

arrays - 简单数组追加操作的时间复杂度

lua - 什么 lua 功能可以用作 python 或 tcl expect 中的 pexpect

qt - 如何动态创建Q_Properties