SaveManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using Util;
  7. namespace CM3D2.MultipleMaids.Plugin
  8. {
  9. public partial class MultipleMaids
  10. {
  11. private static readonly byte[] pngEnd = Encoding.ASCII.GetBytes("IEND");
  12. private Rect saveManagerRect;
  13. private Vector2 saveManagerScrollPos = Vector2.zero;
  14. private bool saveManagerInitialize;
  15. private int selectedSave;
  16. private bool loadSaveFlag = false;
  17. private bool overwriteFlag = false;
  18. private bool createSaveFlag = false;
  19. private string saveScenePath = Path.Combine(Path.GetFullPath(".\\"), "Mod\\MultipleMaidsSave");
  20. private static string sceneData;
  21. private List<Tuple<FileInfo, Texture2D>> saveScenes = new List<Tuple<FileInfo, Texture2D>>(50);
  22. private Texture2D frame;
  23. public void InitializeSaveManager()
  24. {
  25. frame = MakeTex(2, 2, Color.white);
  26. if (!Directory.Exists(saveScenePath))
  27. {
  28. Directory.CreateDirectory(saveScenePath);
  29. }
  30. InitializeSaveList();
  31. saveManagerInitialize = true;
  32. }
  33. private void InitializeSaveList()
  34. {
  35. saveScenes.Clear();
  36. DirectoryInfo info = new DirectoryInfo(saveScenePath);
  37. foreach (var save in info.GetFiles("*.png"))
  38. {
  39. Texture2D screenshot = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  40. screenshot.LoadImage(File.ReadAllBytes(save.FullName));
  41. saveScenes.Add(new Tuple<FileInfo, Texture2D>(save, screenshot));
  42. }
  43. saveScenes.Sort((a, b) => a.Item1.LastWriteTime.CompareTo(b.Item1.LastWriteTime));
  44. selectedSave = saveScenes.Count - 1;
  45. }
  46. private static bool BytesEqual(byte[] a, byte[] b)
  47. {
  48. if (a.Length != b.Length) return false;
  49. for (int i = 0; i < a.Length; i++)
  50. {
  51. if (a[i] != b[i]) return false;
  52. }
  53. return true;
  54. }
  55. private void LoadSave()
  56. {
  57. string filePath = saveScenes[selectedSave].Item1.FullName;
  58. if (!File.Exists(filePath))
  59. {
  60. InitializeSaveList();
  61. sceneData = null;
  62. return;
  63. }
  64. using (FileStream fileStream = File.OpenRead(filePath))
  65. {
  66. byte[] buffer = new byte[pngEnd.Length];
  67. long position = 0;
  68. while (true)
  69. {
  70. int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
  71. if (bytesRead != pngEnd.Length)
  72. {
  73. return;
  74. }
  75. if (BytesEqual(buffer, pngEnd))
  76. {
  77. fileStream.Position += 4;
  78. break;
  79. }
  80. fileStream.Position = ++position;
  81. }
  82. using (MemoryStream sceneStream = LZMA.Decompress(fileStream))
  83. {
  84. sceneData = Encoding.Unicode.GetString(sceneStream.ToArray());
  85. }
  86. }
  87. }
  88. private void OverWrite()
  89. {
  90. string filePath = Path.Combine(saveScenePath, saveScenes[selectedSave].Item1.FullName);
  91. if (File.Exists(filePath))
  92. {
  93. File.Delete(filePath);
  94. }
  95. saveScenes.RemoveAt(selectedSave);
  96. }
  97. private void SaveScene()
  98. {
  99. string sceneString = SerializeScene(true);
  100. thum_byte_to_base64_ = "";
  101. #region MM GUI stuff
  102. Texture2D screenshot = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  103. screenshot.LoadImage(File.ReadAllBytes(thum_file_path_));
  104. float num2 = screenshot.width / (float)screenshot.height;
  105. Vector2 vector2 = new Vector2(480f, 270f);
  106. int newWidth = screenshot.width;
  107. int newHeight = screenshot.height;
  108. if (vector2.x < (double)screenshot.width && vector2.y < (double)screenshot.height)
  109. {
  110. newWidth = (int)vector2.x;
  111. newHeight = Mathf.RoundToInt(newWidth / num2);
  112. if (vector2.y < (double)newHeight)
  113. {
  114. newHeight = (int)vector2.y;
  115. newWidth = Mathf.RoundToInt(newHeight * num2);
  116. }
  117. }
  118. else if (vector2.x < (double)screenshot.width)
  119. {
  120. newWidth = (int)vector2.x;
  121. newHeight = Mathf.RoundToInt(newWidth / num2);
  122. }
  123. else if (vector2.y < (double)screenshot.height)
  124. {
  125. newHeight = (int)vector2.y;
  126. newWidth = Mathf.RoundToInt(newHeight * num2);
  127. }
  128. TextureScale.Bilinear(screenshot, newWidth, newHeight);
  129. #endregion
  130. string filePath = Path.Combine(saveScenePath, $"mmsave{DateTime.Now:yyyyMMddHHmmss}.png");
  131. using (FileStream fileStream = File.Create(filePath))
  132. using (MemoryStream sceneStream = new MemoryStream(Encoding.Unicode.GetBytes(sceneString)))
  133. {
  134. byte[] screenshotBuffer = screenshot.EncodeToPNG();
  135. byte[] sceneBuffer = LZMA.Compress(sceneStream);
  136. fileStream.Write(screenshotBuffer, 0, screenshotBuffer.Length);
  137. fileStream.Write(sceneBuffer, 0, sceneBuffer.Length);
  138. }
  139. saveScenes.Add(new Tuple<FileInfo, Texture2D>(new FileInfo(filePath), screenshot));
  140. selectedSave = saveScenes.Count - 1;
  141. thum_file_path_ = "";
  142. }
  143. private class Tuple<T1, T2>
  144. {
  145. public T1 Item1 { get; }
  146. public T2 Item2 { get; }
  147. public Tuple(T1 Item1, T2 Item2)
  148. {
  149. this.Item1 = Item1;
  150. this.Item2 = Item2;
  151. }
  152. }
  153. }
  154. }