简体   繁体   中英

C# Saving Multiple Objects to file

So basically, I'm working on a console based game in 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.

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]

您可以很容易地将其序列化为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. http://www.schneier.com/crypto-gram-0105.html#3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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