encrypting_save_games.rst 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 every day 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 produce them a
  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, imagine if someone was to find a way to edit the saved games and
  19. assign the items and currency without effort? This would be terrible,
  20. because it would help players consume the content much faster, and as
  21. such run out of it sooner than expected. If this 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 this 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 sharing save files. 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. This is all! Thanks for your cooperation, citizen.