fork(7) download
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. #if UNITY_SWITCH
  6. using nn.account;
  7. using nn.fs;
  8. #endif
  9. using System.IO;
  10.  
  11. [Serializable]
  12. public class SaveData
  13. {
  14. public Dictionary<string, int> SavedInt = new Dictionary<string, int>();
  15. public Dictionary<string, float> SavedFloat = new Dictionary<string, float>();
  16. public Dictionary<string, string> SavedString = new Dictionary<string, string>();
  17. }
  18.  
  19. public static class SaveManager
  20. {
  21. #if UNITY_SWITCH
  22. private static Uid userId; // user ID for the user account on the Nintendo Switch
  23. private const string mountName = "saveData";
  24. private static string saveDataPath = mountName + ":/";
  25. private static FileHandle fileHandle = new nn.fs.FileHandle();
  26.  
  27. // Save journaling memory is used for each time files are created, deleted, or written.
  28. // The journaling memory is freed after nn::fs::CommitSaveData is called.
  29. // For any single time you save data, check the file size against your journaling size.
  30. // Check against the total save data size only when you want to be sure all files don't exceed the limit.
  31. // The variable journalSaveDataSize is only a value that is checked against in this code. The actual journal size is set in the
  32. // Unity editor in PlayerSettings > Publishing Settings > User account save data
  33. private const int journalSaveDataSize = 65536; // 65 KB. This value should be the actual journal size in bytes. This should by 32KB less than
  34. // the value entered in PlayerSettings > Publishing Settings > User account save data
  35. private const int loadBufferSize = 32768; // 32 KB
  36. #endif
  37.  
  38. static string buildTitle = "Immortal_Save";
  39. #if !UNITY_SWITCH
  40. static string dataPath = Application.persistentDataPath + "/SavesDir/";
  41. #endif
  42.  
  43. private static bool loadedData = false;
  44.  
  45. private static bool initialized = false;
  46.  
  47. public static SaveData data = new SaveData();
  48.  
  49. public static bool HasKey(string key)
  50. {
  51. if (!initialized) Initialize();
  52.  
  53. if (!loadedData) LoadData();
  54.  
  55. bool haskey = false;
  56. if (data.SavedFloat.ContainsKey(key)) haskey = true;
  57. if (data.SavedInt.ContainsKey(key)) haskey = true;
  58. if (data.SavedString.ContainsKey(key)) haskey = true;
  59. return haskey;
  60. }
  61.  
  62. public static void Initialize()
  63. {
  64. #if UNITY_SWITCH //&& !UNITY_EDITOR
  65. nn.account.Account.Initialize();
  66. nn.account.UserHandle userHandle = new nn.account.UserHandle();
  67. nn.account.Account.OpenPreselectedUser(ref userHandle);
  68. nn.account.Account.GetUserId(ref userId, userHandle);
  69.  
  70. // mount save data
  71. nn.Result result = nn.fs.SaveData.Mount(mountName, userId);
  72.  
  73. //print out error (debug only) and abort if the filesystem couldn't be mounted
  74. if (result.IsSuccess() == false)
  75. {
  76. Debug.Log("Critical Error: File System could not be mounted.");
  77. result.abortUnlessSuccess();
  78. initialized = false;
  79. }
  80. else
  81. {
  82. initialized = true;
  83. }
  84. #endif
  85. }
  86.  
  87. public static void Unmount()
  88. {
  89. #if UNITY_SWITCH
  90. nn.fs.FileSystem.Unmount(mountName);
  91. #endif
  92. }
  93.  
  94. public static int GetInt(string key, int defaultValue = 0)
  95. {
  96. #if UNITY_XBOXONE
  97. return defaultValue;
  98. #endif
  99. if(!initialized) Initialize();
  100.  
  101. if (!loadedData) LoadData();
  102.  
  103. int returnValue = defaultValue;
  104. if (!SaveManager.data.SavedInt.TryGetValue(key, out returnValue))
  105. {
  106. returnValue = PlayerPrefs.GetInt(key, defaultValue);
  107. }
  108. return returnValue;
  109. }
  110.  
  111. public static float GetFloat(string key, float defaultValue = 0)
  112. {
  113. #if UNITY_XBOXONE
  114. return defaultValue;
  115. #endif
  116. if (!initialized) Initialize();
  117.  
  118. if (!loadedData) LoadData();
  119.  
  120. float returnValue = defaultValue;
  121. if (!SaveManager.data.SavedFloat.TryGetValue(key, out returnValue))
  122. {
  123. returnValue = PlayerPrefs.GetFloat(key, defaultValue);
  124. }
  125. return returnValue;
  126. }
  127.  
  128. public static string GetString(string key, string defaultValue = "")
  129. {
  130. #if UNITY_XBOXONE
  131. return defaultValue;
  132. #endif
  133. if (!initialized) Initialize();
  134.  
  135. if (!loadedData) LoadData();
  136.  
  137. string returnValue = defaultValue;
  138. if (!SaveManager.data.SavedString.TryGetValue(key, out returnValue))
  139. {
  140. returnValue = PlayerPrefs.GetString(key, defaultValue);
  141. }
  142. return returnValue;
  143. }
  144.  
  145. public static void SetInt(string key, int setValue)
  146. {
  147. data.SavedInt[key] = setValue;
  148. }
  149.  
  150. public static void SetFloat(string key, float setValue)
  151. {
  152. data.SavedFloat[key] = setValue;
  153. }
  154.  
  155. public static void SetString(string key, string setValue)
  156. {
  157. data.SavedString[key] = setValue;
  158. }
  159.  
  160. public static void Save()
  161. {
  162. //Debug.Log("Writing Stream to Disk.", saveDataPath);
  163.  
  164. #if UNITY_SWITCH && !UNITY_EDITOR
  165. string filePath = saveDataPath + buildTitle;
  166.  
  167. byte[] dataByteArray;
  168. using (MemoryStream stream = new MemoryStream(journalSaveDataSize)) // the stream size must be less than or equal to the save journal size
  169. {
  170. BinaryWriter binaryWriter = new BinaryWriter(stream);
  171. binaryWriter.Write(data.SavedInt.Count);
  172. binaryWriter.Write(data.SavedFloat.Count);
  173. binaryWriter.Write(data.SavedString.Count);
  174. foreach (var _keyValuePair in data.SavedInt)
  175. {
  176. binaryWriter.Write(_keyValuePair.Key);
  177. binaryWriter.Write(_keyValuePair.Value);
  178. }
  179. foreach (var _keyValuePair in data.SavedFloat)
  180. {
  181. binaryWriter.Write(_keyValuePair.Key);
  182. binaryWriter.Write(_keyValuePair.Value);
  183. }
  184. foreach (var _keyValuePair in data.SavedString)
  185. {
  186. binaryWriter.Write(_keyValuePair.Key);
  187. binaryWriter.Write(_keyValuePair.Value);
  188. }
  189. stream.Close();
  190. dataByteArray = stream.GetBuffer();
  191. }
  192.  
  193. #endif
  194.  
  195. #if UNITY_SWITCH && !UNITY_EDITOR
  196. // This next line prevents the user from quitting the game while saving.
  197. // This is required for Nintendo Switch Guideline 0080
  198. UnityEngine.Switch.Notification.EnterExitRequestHandlingSection();
  199. #endif
  200.  
  201.  
  202. #if UNITY_SWITCH && !UNITY_EDITOR
  203. // If you only ever save the entire file, it may be simpler just to delete the file and create a new one every time you save.
  204. // Most of the functions return an nn.Result which can be used for debugging purposes.
  205. nn.fs.File.Delete(filePath);
  206. nn.fs.File.Create(filePath, journalSaveDataSize); //this makes a file the size of your save journal. You may want to make a file smaller than this.
  207. nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Write);
  208. nn.fs.File.Write(fileHandle, 0, dataByteArray, dataByteArray.LongLength, nn.fs.WriteOption.Flush); // Writes and flushes the write at the same time
  209. nn.fs.File.Close(fileHandle);
  210. nn.fs.SaveData.Commit(mountName); //you must commit the changes.
  211. #endif
  212.  
  213. #if UNITY_SWITCH && !UNITY_EDITOR
  214. // End preventing the user from quitting the game while saving.
  215. UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
  216. #endif
  217.  
  218. #if !UNITY_SWITCH
  219. if (!Directory.Exists(dataPath))
  220. {
  221. Directory.CreateDirectory(dataPath);
  222. }
  223.  
  224. string saveDataPath = dataPath + buildTitle + ".sf";
  225.  
  226. FileStream fileStream = File.Open(saveDataPath, FileMode.Create);
  227. using (BinaryWriter writer = new BinaryWriter(fileStream))
  228. {
  229. writer.Write(data.SavedInt.Count);
  230. writer.Write(data.SavedFloat.Count);
  231. writer.Write(data.SavedString.Count);
  232. foreach (var _keyValuePair in data.SavedInt)
  233. {
  234. writer.Write(_keyValuePair.Key);
  235. writer.Write(_keyValuePair.Value);
  236. //Debug.Log("Writing "+_keyValuePair.Key+" / "+_keyValuePair.Value);
  237. }
  238. foreach (var _keyValuePair in data.SavedFloat)
  239. {
  240. writer.Write(_keyValuePair.Key);
  241. writer.Write(_keyValuePair.Value);
  242. }
  243. foreach (var _keyValuePair in data.SavedString)
  244. {
  245. writer.Write(_keyValuePair.Key);
  246. writer.Write(_keyValuePair.Value);
  247. }
  248. }
  249. fileStream.Close();
  250. Debug.Log("Saved to " + saveDataPath);
  251. #endif
  252. }
  253.  
  254. public static void DeleteAll()
  255. {
  256. data.SavedInt.Clear();
  257. data.SavedFloat.Clear();
  258. data.SavedString.Clear();
  259. Save();
  260. }
  261.  
  262. public static bool LoadData()
  263. {
  264. //CreateDefaultInputMappings();
  265. //CreateWorldData();
  266. SaveManager.loadedData = true;
  267. #if UNITY_XBOXONE
  268. return;
  269. #endif
  270.  
  271. data.SavedInt.Clear();
  272. data.SavedFloat.Clear();
  273. data.SavedString.Clear();
  274.  
  275.  
  276. #if UNITY_SWITCH && !UNITY_EDITOR
  277. Debug.Log("Loading data");
  278.  
  279. data.SavedInt.Clear();
  280. data.SavedFloat.Clear();
  281. data.SavedString.Clear();
  282.  
  283. nn.fs.EntryType entryType = 0; //init to a dummy value (C# requirement)
  284. nn.fs.FileSystem.GetEntryType(ref entryType, saveDataPath);
  285. nn.Result result = nn.fs.File.Open(ref fileHandle, saveDataPath + buildTitle, nn.fs.OpenFileMode.Read);
  286. if (result.IsSuccess() == false)
  287. {
  288. return false; // Could not open file. This can be used to detect if this is the first time a user has launched your game.
  289. // (However, be sure you are not getting this error due to your file being locked by another process, etc.)
  290. }
  291. byte[] loadedData = new byte[loadBufferSize];
  292. nn.fs.File.Read(fileHandle, 0, loadedData, loadBufferSize);
  293. nn.fs.File.Close(fileHandle);
  294.  
  295. using (MemoryStream stream = new MemoryStream(loadedData))
  296. {
  297. BinaryReader reader = new BinaryReader(stream);
  298. int IntCount = reader.ReadInt32();
  299. int FloatCount = reader.ReadInt32();
  300. int StringCount = reader.ReadInt32();
  301. for (int i = 0; i < IntCount; i++)
  302. {
  303. data.SavedInt[reader.ReadString()] = reader.ReadInt32();
  304. }
  305. for (int i = 0; i < FloatCount; i++)
  306. {
  307. data.SavedFloat[reader.ReadString()] = reader.ReadSingle();
  308. }
  309. for (int i = 0; i < StringCount; i++)
  310. {
  311. data.SavedString[reader.ReadString()] = reader.ReadString();
  312. }
  313. }
  314. #endif
  315. Debug.Log("Data Loaded");
  316. #if !UNITY_SWITCH
  317. if (!Directory.Exists(dataPath))
  318. {
  319. Directory.CreateDirectory(dataPath);
  320. }
  321. string saveDataPath = dataPath + buildTitle + ".sf";
  322.  
  323. if (File.Exists(saveDataPath))
  324. {
  325. FileStream fileStream = File.Open(saveDataPath, FileMode.Open);
  326. using (BinaryReader reader = new BinaryReader(fileStream))
  327. {
  328. int IntCount = reader.ReadInt32();
  329. int FloatCount = reader.ReadInt32();
  330. int StringCount = reader.ReadInt32();
  331. for (int i = 0; i < IntCount; i++)
  332. {
  333. data.SavedInt[reader.ReadString()] = reader.ReadInt32();
  334. }
  335. for (int i = 0; i < FloatCount; i++)
  336. {
  337. data.SavedFloat[reader.ReadString()] = reader.ReadSingle();
  338. }
  339. for (int i = 0; i < StringCount; i++)
  340. {
  341. data.SavedString[reader.ReadString()] = reader.ReadString();
  342. }
  343. }
  344. fileStream.Close();
  345. }
  346. #endif
  347. return true;
  348. }
  349.  
  350. public static void DeleteKey(string keyToDel)
  351. {
  352. if (data.SavedInt.ContainsKey(keyToDel)) data.SavedInt.Remove(keyToDel);
  353. if (data.SavedFloat.ContainsKey(keyToDel)) data.SavedFloat.Remove(keyToDel);
  354. if (data.SavedString.ContainsKey(keyToDel)) data.SavedString.Remove(keyToDel);
  355. }
  356. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(2,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty