1
0

SaveManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using UnityEngine;
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.IO;
  7. using Util;
  8. namespace CM3D2.MultipleMaids.Plugin
  9. {
  10. public partial class MultipleMaids
  11. {
  12. private static readonly byte[] pngEnd = Encoding.ASCII.GetBytes("IEND");
  13. private static string sceneData;
  14. private const string baseDirectoryName = "< Base Directory >";
  15. private string saveScenePath = Path.Combine(Path.GetFullPath(".\\"), "Mod\\MultipleMaidsSave");
  16. private List<SavePng> saveScenes = new List<SavePng>(50);
  17. private GUIStyle selectedButtonStyle;
  18. private string[] directoryList;
  19. private Texture2D frame;
  20. private Rect saveManagerRect;
  21. private Rect saveModalRect;
  22. private Rect resizeManagerRect;
  23. private Vector2 saveManagerScrollPos = Vector2.zero;
  24. private Vector2 dirListScrollPos = Vector2.zero;
  25. private bool saveManagerInitialize = false;
  26. private bool loadSaveFlag = false;
  27. private bool overwriteFlag = false;
  28. private bool createSaveFlag = false;
  29. private bool manageSaveFlag = false;
  30. private bool createDirectory = false;
  31. private bool deleteDirectoryFlag = false;
  32. private bool deleteFileFlag = false;
  33. private bool resizeManager = false;
  34. private int selectedSave = 0;
  35. private int selectedDirectory = 0;
  36. private string currentDirectory = "";
  37. private string textFieldValue = "";
  38. public void InitializeSaveManager()
  39. {
  40. frame = MakeTex(2, 2, Color.white);
  41. if (!Directory.Exists(saveScenePath))
  42. {
  43. Directory.CreateDirectory(saveScenePath);
  44. }
  45. selectedButtonStyle = new GUIStyle("button");
  46. selectedButtonStyle.normal.background = MakeTex(1, 1, new Color(0.5f, 0.5f, 0.5f, 0.4f));
  47. selectedButtonStyle.normal.textColor = Color.white;
  48. GetSaveDirectories();
  49. InitializeSaveList();
  50. saveManagerInitialize = true;
  51. }
  52. private void RefreshSaveManager()
  53. {
  54. SwitchDirectory(currentDirectory);
  55. GetSaveDirectories();
  56. InitializeSaveList();
  57. }
  58. private void InitializeSaveList()
  59. {
  60. saveScenes.Clear();
  61. string workingPath = Path.Combine(saveScenePath, currentDirectory);
  62. DirectoryInfo info = new DirectoryInfo(workingPath);
  63. foreach (var save in info.GetFiles("*.png"))
  64. {
  65. Texture2D screenshot = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  66. screenshot.LoadImage(File.ReadAllBytes(save.FullName));
  67. saveScenes.Add(new SavePng(save, screenshot));
  68. }
  69. selectedSave = saveScenes.Count == 0 ? 0 : saveScenes.Count - 1;
  70. saveScenes.Sort((a, b) => a.info.LastWriteTime.CompareTo(b.info.LastWriteTime));
  71. saveManagerScrollPos.y = 0;
  72. }
  73. private void GetSaveDirectories()
  74. {
  75. DirectoryInfo[] directoryInfo = new DirectoryInfo(saveScenePath).GetDirectories();
  76. directoryList = new string[directoryInfo.Length + 1];
  77. directoryList[0] = baseDirectoryName;
  78. for (int i = 0; i < directoryInfo.Length; i++)
  79. {
  80. directoryList[i + 1] = directoryInfo[i].Name;
  81. }
  82. }
  83. private void SwitchDirectory(string target)
  84. {
  85. if (target.Equals(baseDirectoryName)) target = "";
  86. string targetDirectory = Path.Combine(saveScenePath, target);
  87. if (!Directory.Exists(targetDirectory))
  88. {
  89. currentDirectory = "";
  90. selectedDirectory = 0;
  91. GetSaveDirectories();
  92. }
  93. else
  94. {
  95. selectedDirectory = Array.FindIndex(directoryList, d => d.Equals(target, StringComparison.OrdinalIgnoreCase));
  96. selectedDirectory = selectedDirectory == -1 ? 0 : selectedDirectory;
  97. if (target == currentDirectory) return;
  98. currentDirectory = target;
  99. }
  100. InitializeSaveList();
  101. }
  102. private void LoadSave()
  103. {
  104. string filePath = saveScenes[selectedSave].info.FullName;
  105. if (!File.Exists(filePath))
  106. {
  107. InitializeSaveList();
  108. sceneData = null;
  109. return;
  110. }
  111. using (FileStream fileStream = File.OpenRead(filePath))
  112. {
  113. byte[] buffer = new byte[pngEnd.Length];
  114. long position = 0;
  115. while (true)
  116. {
  117. int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
  118. if (bytesRead != pngEnd.Length)
  119. {
  120. sceneData = null;
  121. return;
  122. }
  123. if (BytesEqual(buffer, pngEnd))
  124. {
  125. fileStream.Position += 4;
  126. break;
  127. }
  128. fileStream.Position = ++position;
  129. }
  130. using (MemoryStream sceneStream = LZMA.Decompress(fileStream))
  131. {
  132. sceneData = Encoding.Unicode.GetString(sceneStream.ToArray());
  133. }
  134. }
  135. SavePng temp = saveScenes[selectedSave];
  136. saveScenes.RemoveAt(selectedSave);
  137. saveScenes.Add(temp);
  138. saveManagerScrollPos.y = 0;
  139. }
  140. private void Overwrite()
  141. {
  142. string filePath = saveScenes[selectedSave].info.FullName;
  143. if (!File.Exists(filePath))
  144. {
  145. RefreshSaveManager();
  146. }
  147. else
  148. {
  149. File.Delete(filePath);
  150. saveScenes.RemoveAt(selectedSave);
  151. saveManagerScrollPos.y = 0;
  152. }
  153. }
  154. private void MakeNewDirectory(string directoryName)
  155. {
  156. directoryName = string.Join("", directoryName.Split(Path.GetInvalidFileNameChars()));
  157. string newDirectory = Path.Combine(saveScenePath, directoryName);
  158. if (!Directory.Exists(newDirectory))
  159. {
  160. Directory.CreateDirectory(newDirectory);
  161. }
  162. GetSaveDirectories();
  163. SwitchDirectory(directoryName);
  164. }
  165. private void DeleteDirectory()
  166. {
  167. string directory = Path.Combine(saveScenePath, directoryList[selectedDirectory]);
  168. if (Directory.Exists(directory))
  169. {
  170. DirectoryInfo dirInfo = new DirectoryInfo(directory);
  171. foreach (var finfo in dirInfo.GetFiles())
  172. {
  173. finfo.Delete();
  174. }
  175. dirInfo.Delete();
  176. }
  177. RefreshSaveManager();
  178. }
  179. private void DeleteFile()
  180. {
  181. string file = saveScenes[selectedSave].info.FullName;
  182. if (File.Exists(file))
  183. {
  184. saveScenes[selectedSave].info.Delete();
  185. saveScenes.RemoveAt(selectedSave);
  186. }
  187. RefreshSaveManager();
  188. }
  189. private void SaveScene()
  190. {
  191. string saveDirectory = Path.Combine(saveScenePath, currentDirectory);
  192. if (!Directory.Exists(saveDirectory))
  193. {
  194. thum_byte_to_base64_ = "";
  195. thum_file_path_ = "";
  196. RefreshSaveManager();
  197. return;
  198. }
  199. string sceneString = SerializeScene(true);
  200. #region MM GUI stuff
  201. Texture2D screenshot = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  202. screenshot.LoadImage(File.ReadAllBytes(thum_file_path_));
  203. float num2 = screenshot.width / (float)screenshot.height;
  204. Vector2 vector2 = new Vector2(480f, 270f);
  205. int newWidth = screenshot.width;
  206. int newHeight = screenshot.height;
  207. if (vector2.x < (double)screenshot.width && vector2.y < (double)screenshot.height)
  208. {
  209. newWidth = (int)vector2.x;
  210. newHeight = Mathf.RoundToInt(newWidth / num2);
  211. if (vector2.y < (double)newHeight)
  212. {
  213. newHeight = (int)vector2.y;
  214. newWidth = Mathf.RoundToInt(newHeight * num2);
  215. }
  216. }
  217. else if (vector2.x < (double)screenshot.width)
  218. {
  219. newWidth = (int)vector2.x;
  220. newHeight = Mathf.RoundToInt(newWidth / num2);
  221. }
  222. else if (vector2.y < (double)screenshot.height)
  223. {
  224. newHeight = (int)vector2.y;
  225. newWidth = Mathf.RoundToInt(newHeight * num2);
  226. }
  227. TextureScale.Bilinear(screenshot, newWidth, newHeight);
  228. #endregion
  229. thum_byte_to_base64_ = "";
  230. thum_file_path_ = "";
  231. string filePath = Path.Combine(saveDirectory, $"mmsave{DateTime.Now:yyyyMMddHHmmss}.png");
  232. using (FileStream fileStream = File.Create(filePath))
  233. using (MemoryStream sceneStream = new MemoryStream(Encoding.Unicode.GetBytes(sceneString)))
  234. {
  235. byte[] screenshotBuffer = screenshot.EncodeToPNG();
  236. byte[] sceneBuffer = LZMA.Compress(sceneStream);
  237. fileStream.Write(screenshotBuffer, 0, screenshotBuffer.Length);
  238. fileStream.Write(sceneBuffer, 0, sceneBuffer.Length);
  239. }
  240. saveScenes.Add(new SavePng(new FileInfo(filePath), screenshot));
  241. selectedSave = saveScenes.Count - 1;
  242. }
  243. private static bool BytesEqual(byte[] a, byte[] b)
  244. {
  245. if (a.Length != b.Length) return false;
  246. for (int i = 0; i < a.Length; i++)
  247. {
  248. if (a[i] != b[i]) return false;
  249. }
  250. return true;
  251. }
  252. class SavePng
  253. {
  254. public FileInfo info { get; }
  255. public Texture2D screenshot { get; }
  256. public SavePng(FileInfo info, Texture2D screenshot)
  257. {
  258. this.info = info;
  259. this.screenshot = screenshot;
  260. }
  261. }
  262. }
  263. }