我正在为c#xna开发的游戏保存和加载。我有一长串的类数组,我正在尝试序列化,我遇到了其中一个问题。我完成了以下课程
[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); }
但是当我尝试反序列化时,我得到以下内容 - 不期望Type,并且不能推断出合同:GameName1.CompleteTile [,,]
消毒代码 -
using (var stream = File.OpenRead("" + loadDestination + "level.dat"))
{ Globals.levelArray = Serializer.Deserialize<CompleteTile[, ,]>(stream); }
我怀疑这是因为它是一个多维数组,但我不确定,感谢任何帮助。
答案 0 :(得分:4)
如前所述,protobuf不支持多维数组。但是,您可以通过将多维数组转换为1D数组+整数的维数组来规避此限制。为了减轻痛苦,我使用了一些扩展
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。