2
0

encrypting_save_games.rst 3.2 KB

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