Eonirr

Untitled

Mar 29th, 2026 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public static void Save<T>(T data, string path)
  2. {
  3. string jsonData = JsonUtility.ToJson(data);
  4.  
  5. string hash = SaveUtils.ComputeHash(jsonData);
  6.  
  7. SaveWrapper<T> wrapper = new SaveWrapper<T>
  8. {
  9. data = data,
  10. hash = hash
  11. };
  12.  
  13. string finalJson = JsonUtility.ToJson(wrapper, true);
  14.  
  15. string dir = Path.GetDirectoryName(path);
  16. if (!Directory.Exists(dir))
  17. Directory.CreateDirectory(dir);
  18.  
  19. File.WriteAllText(path, finalJson);
  20. }
  21.  
  22. public static T Load<T>(string path)
  23. {
  24. if (!File.Exists(path))
  25. return default;
  26.  
  27. string json = File.ReadAllText(path);
  28.  
  29. SaveWrapper<T> wrapper = JsonUtility.FromJson<SaveWrapper<T>>(json);
  30.  
  31. if (wrapper == null || wrapper.data == null)
  32. return default;
  33.  
  34. string jsonData = JsonUtility.ToJson(wrapper.data);
  35. string computedHash = SaveUtils.ComputeHash(jsonData);
  36.  
  37. if (computedHash != wrapper.hash)
  38. {
  39. Debug.LogError("T'as modif' coquinou !!!");
  40. return default;
  41. }
  42. return wrapper.data;
  43. }
  44.  
  45.  
  46. [System.Serializable]
  47. public class SaveWrapper<T>
  48. {
  49. public T data;
  50. public string hash;
  51. }
  52.  
  53. // Tu t'fais ton Utils
  54. public static class SaveUtils
  55. {
  56. public static string ComputeHash(string truc)
  57. {
  58. //Si t'es énervé
  59. const string SECRET_KEY = "PHOQUE_ME";
  60.  
  61. using (var sha = SHA256.Create())
  62. {
  63. byte[] bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(truc + SECRET_KEY));
  64. return System.Convert.ToBase64String(bytes);
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment