简体   繁体   English

C#将多个对象保存到文件

[英]C# Saving Multiple Objects to file

So basically, I'm working on a console based game in C#. 因此,基本上,我正在使用C#开发基于控制台的游戏。 I'm to the point where I'm ready to save the data. 我已经准备好保存数据了。 I want all the data saved into one file, if possible. 如果可能,我希望将所有数据保存到一个文件中。 I don't wan't something like XML Serialization, where the player can just go in and give himself a million gold. 不需要像XML序列化之类的东西,玩家可以进去并给自己一百万美元。

What should I use? 我应该使用什么? Here's the things I need to serialize: 这是我需要序列化的内容:

    GameWorld
    Inventory
    Player
    List<Item>

    Point
    List<String>

Here's the variables that are in the classes: 这是类中的变量:

    public class GameWorld
    {
        Dictionary<Point, Room>
    }

    public class Item
    {
        String Creator
        String Verb
        String RequiredItem
        Action OnActivate
        int Difficulty
        Action OnDefeatEnemy
        Action OnDefeatedByEnemy 
    }

    public class Room
    {
        String Title
        String Description
        List<Item> Items
        List<String> ItemNames
        String Creator
    }

    public class Inventory
    {
        List<Items>
    }

    public enum ActionType
    {
        ACQUIRE_GOLD, DECREASE_HEALTH, INCREASE_HEALTH, DECREASE_STRENGTH, INCREASE_STRENGTH, GET_ITEM, PRINT_MESSAGE
    }

    public class Action
    {
        ActionType actionType
        int Amount
        String GiveItem
        String Message
        String Creator
        bool SingleActivation
        bool HasActivated
    }

Using Binary serialization would obfuscate slightly. 使用二进制序列化会有点模糊。

[It won't stop someone with Reflector and a bit of a poke around your code and data files though] [虽然它不会阻止使用Reflector的人,但是会在您的代码和数据文件上戳出一点点]

您可以很容易地将其序列化为xml,然后将其加密,然后再将其保存到文件中。

使用嵌入式对象数据库(如Db4o或Eloquera),可以更容易地以编程方式管理存储的对象。

If you're not worried about disclosure, you could just add a hash of [ your serialized data concatenated with some magic constant ]. 如果您不担心公开,则可以添加[与某些魔术常数连接的序列化数据]的哈希。 A hacker would then have to disassemble your program to find the constant if they wanted to manipulate your serialized data. 然后,如果黑客想要操纵您的序列化数据,则他们将不得不反汇编程序以查找常量。

If disclosure is a problem, then I would recommend "encryption" with a magic constant. 如果公开是一个问题,那么我建议使用魔术常数进行“加密”。 This is really just a form of content scrambling, since the magic constant is "known" (although, perhaps a nuisance to find). 这实际上只是内容加扰的一种形式,因为魔术常数是“已知的”(尽管可能很难找到)。

By the way, your problem is analogous to the DRM problem, which really is unsolvable unless you have complete control over the client platform. 顺便说一句,您的问题类似于DRM问题,除非您完全控制客户端平台,否则这确实无法解决。 http://www.schneier.com/crypto-gram-0105.html#3 http://www.schneier.com/crypto-gram-0105.html#3

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

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