SerializeStorageManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class SerializeStorageManager
  6. {
  7. public SerializeStorageManager()
  8. {
  9. if (File.Exists(this.ConfigFilePath))
  10. {
  11. try
  12. {
  13. this.config = JsonUtility.FromJson<SerializeStorageManager.Config>(File.ReadAllText(this.ConfigFilePath));
  14. }
  15. catch
  16. {
  17. }
  18. }
  19. if (this.config.locationType != SerializeStorageManager.Config.LocationType.Normal)
  20. {
  21. try
  22. {
  23. if (!Directory.Exists(this.GetStoreDirectoryPath(this.config.locationType)))
  24. {
  25. Directory.CreateDirectory(this.GetStoreDirectoryPath(this.config.locationType));
  26. }
  27. }
  28. catch (Exception exception)
  29. {
  30. Debug.LogException(exception);
  31. }
  32. }
  33. this.StartSerializeStorageMigrationSequence();
  34. }
  35. private string ConfigFilePath
  36. {
  37. get
  38. {
  39. return Path.Combine(UTY.gameProjectPath, "serialize_storage_config.cfg");
  40. }
  41. }
  42. public string StoreDirectoryPath
  43. {
  44. get
  45. {
  46. return this.GetStoreDirectoryPath(this.config.locationType);
  47. }
  48. }
  49. public void StartSerializeStorageMigrationSequence()
  50. {
  51. if (this.config.locationType == SerializeStorageManager.Config.LocationType.Normal)
  52. {
  53. return;
  54. }
  55. List<string> list = new List<string>(new string[]
  56. {
  57. "config.xml",
  58. "dance_setting_br.dat",
  59. "system.dat",
  60. "OvrControllerShortcutConfig.json",
  61. "MaidFingerDataList.json",
  62. "OvrIK.json"
  63. });
  64. List<string> list2 = new List<string>(new string[]
  65. {
  66. "Thumb",
  67. "ScreenShot",
  68. "SaveData",
  69. "Preset",
  70. "PhotoModeData",
  71. "MyRoom"
  72. });
  73. string storeDirectoryPath = this.GetStoreDirectoryPath(this.config.locationType);
  74. string storeDirectoryPath2 = this.GetStoreDirectoryPath(SerializeStorageManager.Config.LocationType.Normal);
  75. bool flag = Directory.Exists(Path.Combine(storeDirectoryPath2, "SaveData"));
  76. bool flag2 = false;
  77. bool flag3 = false;
  78. foreach (string path in list)
  79. {
  80. string path2 = Path.Combine(storeDirectoryPath2, path);
  81. string path3 = Path.Combine(storeDirectoryPath, path);
  82. if (File.Exists(path2))
  83. {
  84. flag3 |= true;
  85. }
  86. if (Directory.Exists(path3))
  87. {
  88. flag2 |= true;
  89. }
  90. }
  91. foreach (string path4 in list2)
  92. {
  93. string path5 = Path.Combine(storeDirectoryPath2, path4);
  94. string path6 = Path.Combine(storeDirectoryPath, path4);
  95. if (Directory.Exists(path5))
  96. {
  97. flag3 |= true;
  98. }
  99. if (File.Exists(path6))
  100. {
  101. flag2 |= true;
  102. }
  103. }
  104. List<KeyValuePair<string, string>> list3 = new List<KeyValuePair<string, string>>();
  105. List<KeyValuePair<string, string>> list4 = new List<KeyValuePair<string, string>>();
  106. if (flag3)
  107. {
  108. if (flag)
  109. {
  110. NDebug.MessageBox(string.Empty, "Your save data will be transferred automatically.\nThe new save data location is:\n" + storeDirectoryPath);
  111. }
  112. if (!flag2)
  113. {
  114. string text = Path.Combine(this.GetStoreDirectoryPath(SerializeStorageManager.Config.LocationType.Normal), "BackupData_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  115. if (!Directory.Exists(storeDirectoryPath))
  116. {
  117. Directory.CreateDirectory(storeDirectoryPath);
  118. }
  119. if (!Directory.Exists(text))
  120. {
  121. Directory.CreateDirectory(text);
  122. }
  123. foreach (string path7 in list)
  124. {
  125. string text2 = Path.Combine(storeDirectoryPath2, path7);
  126. string text3 = Path.Combine(storeDirectoryPath, path7);
  127. if (File.Exists(text2) && !Directory.Exists(text3))
  128. {
  129. if (!File.Exists(text3))
  130. {
  131. File.Copy(text2, text3);
  132. }
  133. else
  134. {
  135. Debug.LogWarning("StartSerializeStorageMigrationSequence\ndestPath:" + text3 + "には既にファイルが存在するためコピーに失敗しました" + text2);
  136. }
  137. list3.Add(new KeyValuePair<string, string>(text2, Path.Combine(text, path7)));
  138. }
  139. }
  140. foreach (string path8 in list2)
  141. {
  142. string text4 = Path.Combine(storeDirectoryPath2, path8);
  143. string text5 = Path.Combine(storeDirectoryPath, path8);
  144. if (Directory.Exists(text4) && !File.Exists(text5))
  145. {
  146. SerializeStorageManager.DirectoryCopy(text4, text5, true);
  147. list4.Add(new KeyValuePair<string, string>(text4, Path.Combine(text, path8)));
  148. }
  149. }
  150. foreach (KeyValuePair<string, string> keyValuePair in list3)
  151. {
  152. File.Move(keyValuePair.Key, keyValuePair.Value);
  153. }
  154. foreach (KeyValuePair<string, string> keyValuePair2 in list4)
  155. {
  156. Directory.Move(keyValuePair2.Key, keyValuePair2.Value);
  157. }
  158. if (flag)
  159. {
  160. NDebug.MessageBox(string.Empty, "Transfer complete.");
  161. }
  162. }
  163. else if (flag)
  164. {
  165. NDebug.MessageBox(string.Empty, storeDirectoryPath + "There is already existing data here, so your save data cannot be transferred.");
  166. }
  167. }
  168. }
  169. public void SaveConfigFile()
  170. {
  171. File.WriteAllText(this.ConfigFilePath, JsonUtility.ToJson(this.config));
  172. }
  173. private string GetStoreDirectoryPath(SerializeStorageManager.Config.LocationType type)
  174. {
  175. if (type == SerializeStorageManager.Config.LocationType.Document)
  176. {
  177. return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "KISS\\COM3D2\\");
  178. }
  179. return UTY.gameProjectPath + "\\";
  180. }
  181. private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  182. {
  183. DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirName);
  184. if (!directoryInfo.Exists)
  185. {
  186. throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
  187. }
  188. DirectoryInfo[] directories = directoryInfo.GetDirectories();
  189. if (!Directory.Exists(destDirName))
  190. {
  191. Directory.CreateDirectory(destDirName);
  192. }
  193. FileInfo[] files = directoryInfo.GetFiles();
  194. foreach (FileInfo fileInfo in files)
  195. {
  196. string text = Path.Combine(destDirName, fileInfo.Name);
  197. if (!File.Exists(text))
  198. {
  199. fileInfo.CopyTo(text, false);
  200. }
  201. else
  202. {
  203. Debug.LogWarning("DirectoryCopy\ndestPath:" + text + "には既にファイルが存在するためコピーに失敗しました" + fileInfo.FullName);
  204. }
  205. }
  206. if (copySubDirs)
  207. {
  208. foreach (DirectoryInfo directoryInfo2 in directories)
  209. {
  210. string destDirName2 = Path.Combine(destDirName, directoryInfo2.Name);
  211. SerializeStorageManager.DirectoryCopy(directoryInfo2.FullName, destDirName2, copySubDirs);
  212. }
  213. }
  214. }
  215. private const string ConfigFileName = "serialize_storage_config.cfg";
  216. private SerializeStorageManager.Config config = new SerializeStorageManager.Config();
  217. [Serializable]
  218. private class Config : ISerializationCallbackReceiver
  219. {
  220. public void OnBeforeSerialize()
  221. {
  222. this.version = 1000;
  223. this.locationTypeText = this.locationType.ToString();
  224. }
  225. public void OnAfterDeserialize()
  226. {
  227. this.locationType = SerializeStorageManager.Config.LocationType.Normal;
  228. try
  229. {
  230. if (!string.IsNullOrEmpty(this.locationTypeText))
  231. {
  232. this.locationType = (SerializeStorageManager.Config.LocationType)Enum.Parse(typeof(SerializeStorageManager.Config.LocationType), this.locationTypeText, true);
  233. }
  234. }
  235. catch
  236. {
  237. }
  238. }
  239. private const int FixVersion = 1000;
  240. public int version = 1000;
  241. [NonSerialized]
  242. public SerializeStorageManager.Config.LocationType locationType;
  243. [SerializeField]
  244. public string locationTypeText;
  245. public enum LocationType
  246. {
  247. Normal,
  248. Document
  249. }
  250. }
  251. }