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

发布于 2024-10-13 02:20:48 字数 1215 浏览 7 评论 0原文

基本上,我正在用 C# 开发一个基于控制台的游戏。我已经准备好保存数据了。如果可能的话,我希望将所有数据保存到一个文件中。我想要像XML序列化这样的东西,玩家可以直接进去给自己一百万金币。

我应该用什么?这是我需要序列化的内容:

    GameWorld
    Inventory
    Player
    List<Item>

    Point
    List<String>

这是类中的变量:

    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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人迟暮 2024-10-20 02:20:48

使用二进制序列化会稍微混淆。

[不过,它不会阻止有人使用 Reflector 和对你的代码和数据文件进行一些攻击]

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]

哽咽笑 2024-10-20 02:20:48

您可以很容易地序列化为 xml,然后在将其保存到文件之前对其进行加密。

You could pretty easily serialize to xml then encrypt it before saving it to a file.

近箐 2024-10-20 02:20:48

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

Its easier to manage stored object, programmatically, using embedded object database like Db4o or Eloquera.

遮云壑 2024-10-20 02:20:48

如果您不担心泄露,您可以添加[您的序列化数据与一些魔术常量连接]的哈希值。如果黑客想要操纵你的序列化数据,他们就必须反汇编你的程序才能找到常量。

如果披露是一个问题,那么我会推荐使用魔法常数“加密”。这实际上只是内容扰乱的一种形式,因为魔法常数是“已知的”(尽管可能很难找到)。

顺便说一句,您的问题类似于 DRM 问题,除非您完全控制客户端平台,否则这实际上是无法解决的。 http://www.schneier.com/crypto-gram-0105.html#3

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文