加密保存的游戏数据

为什么?

因为今日的世界不是昨日的世界。一个资本主义寡头统治着世界,强迫我们消费,以便使这个腐朽社会的齿轮保持运作。因此,当今游戏消费最大的市场是手机游戏市场。这是一个穷人被迫消费数字内容,以便忘记他们日常生活、通勤的痛苦,或仅仅享受他们没有被用来为统治阶级生产商品或服务的短暂自由时刻。这些人需要继续集中于他们的电子游戏(因为不这样做将给他们带来巨大的存在主义焦虑),所以他们甚至花钱在游戏上来扩展他们的体验,而他们这样做的首选方式是通过应用内购买和虚拟货币。

But what if someone were to find a way to edit the saved games and assign the items and currency without effort? That would be terrible, because it would help players consume the content much faster, and therefore run out of it sooner than expected. If that happens, they will have nothing that prevents them from thinking, and the tremendous agony of realizing their own irrelevance would again take over their life.

不,我们绝对不希望这种情况发生,所以让我们看看如何加密保存游戏数据从而保护世界秩序和平稳定。

怎么做?

File 可以在一个位置打开文件并读/写数据(整数、字符串和变量)。它还支持加密。要创建加密文件,必须提供一个密码,如下所示:

GDScript

C#

  1. var f = File.new()
  2. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, "mypass")
  3. f.store_var(game_state)
  4. f.close()
  1. var f = new File();
  2. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, "mypass");
  3. f.StoreVar(gameState);
  4. f.Close();

对用户而言,这将使文件不可读,但仍然不会阻止他们分享保存的文件。为了解决这个问题,可以使用设备唯一id或某个唯一用户标识符,例如:

GDScript

C#

  1. var f = File.new()
  2. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, OS.get_unique_id())
  3. f.store_var(game_state)
  4. f.close()
  1. var f = new File();
  2. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, OS.GetUniqueId());
  3. f.StoreVar(gameState);
  4. f.Close();

请注意, OS.get_unique_id() 只适用于iOS和Android系统。

That is all! Thank you for your cooperation, citizen.

注解

This method cannot really prevent players from editing their savegames locally because, since the encryption key is stored inside the game, the player can still decrypt and edit the file themselves. The only way to prevent this from being possible is to store the save data on a remote server, where players can only make authorized changes to their save data. If your game deals with real money, you need to be doing this anyway.