encrypting_save_games.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. .. _doc_encrypting_save_games:
  2. Encrypting save games
  3. =====================
  4. Why?
  5. ----
  6. .. This introduction is an Easter egg and is not intended to be taken seriously.
  7. .. Please don't remove it :)
  8. Because the world today is not the world of yesterday. A capitalist
  9. oligarchy runs the world and forces us to consume in order to keep the
  10. gears of this rotten society on track. As such, the biggest market for
  11. video game consumption today is the mobile one. It is a market of poor
  12. souls forced to compulsively consume digital content in order to forget
  13. the misery of their everyday life, commute, or just any other brief
  14. free moment they have that they are not using to produce goods or
  15. services for the ruling class. These individuals need to keep focusing
  16. on their video games (because not doing so will fill them with
  17. tremendous existential angst), so they go as far as spending money on
  18. them to extend their experience, and their preferred way of doing so is
  19. through in-app purchases and virtual currency.
  20. But what if someone were to find a way to edit the saved games and
  21. assign the items and currency without effort? That would be terrible,
  22. because it would help players consume the content much faster, and therefore
  23. run out of it sooner than expected. If that happens, they will have
  24. nothing that avoids them to think, and the tremendous agony of realizing
  25. their own irrelevance would again take over their life.
  26. No, we definitely do not want that to happen, so let's see how to
  27. encrypt savegames and protect the world order.
  28. How?
  29. ----
  30. The class :ref:`File <class_File>` can open a file at a
  31. location and read/write data (integers, strings and variants).
  32. It also supports encryption.
  33. To create an encrypted file, a passphrase must be provided, like this:
  34. .. tabs::
  35. .. code-tab:: gdscript GDScript
  36. var f = File.new()
  37. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, "mypass")
  38. f.store_var(game_state)
  39. f.close()
  40. .. code-tab:: csharp
  41. var f = new File();
  42. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, "mypass");
  43. f.StoreVar(gameState);
  44. f.Close();
  45. This will make the file unreadable to users, but will still not prevent
  46. them from sharing savefiles. To solve this, use the device unique id or
  47. some unique user identifier, for example:
  48. .. tabs::
  49. .. code-tab:: gdscript GDScript
  50. var f = File.new()
  51. var err = f.open_encrypted_with_pass("user://savedata.bin", File.WRITE, OS.get_unique_id())
  52. f.store_var(game_state)
  53. f.close()
  54. .. code-tab:: csharp
  55. var f = new File();
  56. var err = f.OpenEncryptedWithPass("user://savedata.bin", (int)File.ModeFlags.Write, OS.GetUniqueId());
  57. f.StoreVar(gameState);
  58. f.Close();
  59. Note that ``OS.get_unique_id()`` does not work on UWP or HTML5.
  60. That is all! Thank you for your cooperation, citizen.
  61. .. note:: This method cannot really prevent players from editing their savegames
  62. locally because, since the encryption key is stored inside the game, the player
  63. can still decrypt and edit the file themselves. The only way to prevent this
  64. from being possible is to store the save data on a remote server, where players
  65. can only make authorized changes to their save data. If your game deals with
  66. real money, you need to be doing this anyway.