简体   繁体   English

C#XML序列化和涉及的继承

[英]C# XML serialization and involved inheritance

I have a 2D game engine in XNA that is developing quite well. 我在XNA中拥有一个2D游戏引擎,该引擎发展得很好。 It's laid out as such: 布局如下:

A central GameManager, contains a List of SceneObjectManagers (for layering). 中央GameManager包含一个SceneObjectManagers列表(用于分层)。

SceneObjectManager contains a collection of SceneObjects (a class that inehrits from List, with a operator [string] overloaded), which is an abstract base class with a string for the object's ID. SceneObjectManager包含一个SceneObjects集合(一个从List初始化的类,重载了运算符[string]),这是一个抽象基类,其中包含对象ID的字符串。

There is a SceneObject2D and AnimatedSceneObject2D classes, which inherit in a chain (2D from SceneObject, aniamted from 2D). 有一个SceneObject2D和AnimatedSceneObject2D类,它们是在链中继承的(来自SceneObject的2D,来自2D的动画)。 They contain relevent information (position, source rectangle, animations, etc). 它们包含相关信息(位置,源矩形,动画等)。

I then have a second project (the actual game which uses the engine), which has things like PlayerObject, AIObject, FireballScript, etc, which inherit from SceneObject2D and AnimatedSceneObject2D depending on what it needs (if it's displayed, animated, or even neither). 然后,我有第二个项目(使用引擎的实际游戏),其中包含PlayerObject,AIObject,FireballScript等内容,这些内容根据需要(如果是显示,动画或什至都不显示)从SceneObject2D和AnimatedSceneObject2D继承。 。

There's more to it, but you get the picture. 还有更多,但您会明白。

I've begun the idea of tackling a level editor, because writing out hundreds to thousands of entities to build a level up and positioning them perfectly by trial and error or extensive on-paper math would just be madness. 我已经开始提出关卡编辑器的想法,因为写出成百上千个实体来建立关卡并通过反复试验或大量的纸上数学对其进行完美定位将是疯狂的。

Fairly simple concept... brush, select entities, set attributes, and ultimately export a XML file which would contain every entity, their positions and attributes, ID's, etc. The problem, though, is with the XML serialization itself to save objects. 相当简单的概念……刷,选择实体,设置属性,并最终导出一个XML文件,其中将包含每个实体,它们的位置和属性,ID等。但是,问题在于XML序列化本身用于保存对象。

Essentially, I'd serialize the GameManager, which contains all sceneobjectmanagers, which contains all scene objects. 本质上,我将序列化包含所有场景对象管理器的GameManager,其中包含所有场景对象。 Then I could just load the XML file in and there would be my level. 然后,我可以只加载XML文件,这就是我的水平。 But then there's the problem of inheritance, and more complex objects containing objects (and interfaces and all that). 但是,还有继承的问题,更复杂的对象包含对象(以及接口等)。

I can't see myself writing out [XmlInclude(typeof(T))] for every single entity for every class that will be a base class, not to mention the more complex objects. 我看不到自己会为将成为基类的每个类的每个实体写出[XmlInclude(typeof(T))],更不用说更复杂的对象了。

I started inheriting IXmlSerializable, but before I go too far into that endeavor of writing out WriteXml and ReadXml for all my objects, I'd like to know if there's a better, easier way to go about this. 我开始继承IXmlSerializable,但是在深入研究为所有对象写WriteXml和ReadXml的努力之前,我想知道是否有更好,更容易的方法进行此操作。

I'd point out that it needs to be a portable format (hence XML) since I'd like to re-use the editor's exports, with some tweaks, for use in a C++ engine which uses the same classes layout (I'm thinking of using Boost.serialization for it). 我要指出的是,它必须是一种可移植的格式(因此是XML),因为我想通过一些调整重新使用编辑器的导出,以便在使用相同类布局的C ++引擎中使用(我考虑使用Boost.serialization。

I ended up using an open-source XML serializer called YAXLib . 我最终使用了名为YAXLib的开源XML序列化程序 It stores the "real" type of the concrete classes when serialized, and I've managed to serialize my collection of scene object managers. 序列化时,它存储具体类的“真实”类型,并且我设法序列化了场景对象管理器的集合。

I can deserialize them later on in my C++ engine once I have all of my engine ported over (I'm probably going to write my own deserializer using TinyXML, due to previous experience with it). 移植完所有引擎后,我可以在以后的C ++引擎中反序列化它们(由于先前的经验,我可能会使用TinyXML编写自己的反序列化器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM