SceneManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class SceneManager : IManager
  9. {
  10. public static bool Busy { get; private set; } = false;
  11. public bool Initialized { get; private set; } = false;
  12. private MeidoPhotoStudio meidoPhotoStudio;
  13. private SceneModalWindow sceneModal;
  14. private int SortDirection => SortDescending ? -1 : 1;
  15. public static Vector2 sceneDimensions = new Vector2(480, 270);
  16. public bool KankyoMode { get; set; } = false;
  17. public bool SortDescending { get; set; } = false;
  18. public List<Scene> SceneList { get; private set; } = new List<Scene>();
  19. public int CurrentDirectoryIndex { get; private set; } = -1;
  20. public string CurrentDirectoryName => CurrentDirectoryList[CurrentDirectoryIndex];
  21. public List<string> CurrentDirectoryList
  22. {
  23. get => KankyoMode ? Constants.KankyoDirectoryList : Constants.SceneDirectoryList;
  24. }
  25. public string CurrentBasePath => KankyoMode ? Constants.kankyoPath : Constants.scenesPath;
  26. public string CurrentScenesDirectory
  27. {
  28. get => CurrentDirectoryIndex == 0 ? CurrentBasePath : Path.Combine(CurrentBasePath, CurrentDirectoryName);
  29. }
  30. public SortMode CurrentSortMode { get; private set; } = SortMode.Name;
  31. public int CurrentSceneIndex { get; private set; } = -1;
  32. public Scene CurrentScene
  33. {
  34. get
  35. {
  36. if (SceneList.Count == 0) return null;
  37. return SceneList[CurrentSceneIndex];
  38. }
  39. }
  40. public enum SortMode
  41. {
  42. Name, DateCreated, DateModified
  43. }
  44. public SceneManager(MeidoPhotoStudio meidoPhotoStudio)
  45. {
  46. this.meidoPhotoStudio = meidoPhotoStudio;
  47. this.sceneModal = new SceneModalWindow(this);
  48. }
  49. public void Activate() { }
  50. public void Initialize()
  51. {
  52. if (!Initialized)
  53. {
  54. Initialized = true;
  55. SelectDirectory(0);
  56. }
  57. }
  58. public void Deactivate() => ClearSceneList();
  59. public void Update()
  60. {
  61. if (Utility.GetModKey(Utility.ModKey.Control))
  62. {
  63. if (Input.GetKeyDown(KeyCode.S)) QuickSaveScene();
  64. else if (Input.GetKeyDown(KeyCode.A)) QuickLoadScene();
  65. }
  66. }
  67. public void DeleteDirectory()
  68. {
  69. if (Directory.Exists(CurrentScenesDirectory))
  70. {
  71. Directory.Delete(CurrentScenesDirectory, true);
  72. }
  73. CurrentDirectoryList.RemoveAt(CurrentDirectoryIndex);
  74. }
  75. public void OverwriteScene() => SaveScene(overwrite: true);
  76. public void ToggleKankyoMode()
  77. {
  78. this.KankyoMode = !this.KankyoMode;
  79. CurrentDirectoryIndex = 0;
  80. UpdateSceneList();
  81. }
  82. public void SaveScene(bool overwrite = false)
  83. {
  84. if (Busy) return;
  85. if (!Directory.Exists(CurrentScenesDirectory)) Directory.CreateDirectory(CurrentScenesDirectory);
  86. meidoPhotoStudio.StartCoroutine(SaveSceneToFile(overwrite));
  87. }
  88. public void SelectDirectory(int directoryIndex)
  89. {
  90. if (directoryIndex == CurrentDirectoryIndex) return;
  91. CurrentDirectoryIndex = directoryIndex;
  92. UpdateSceneList();
  93. }
  94. public void SelectScene(int sceneIndex)
  95. {
  96. CurrentSceneIndex = sceneIndex;
  97. CurrentScene.GetNumberOfMaids();
  98. }
  99. public void AddDirectory(string directoryName)
  100. {
  101. if (!CurrentDirectoryList.Contains(directoryName, StringComparer.InvariantCultureIgnoreCase))
  102. {
  103. CurrentDirectoryList.Add(directoryName);
  104. Directory.CreateDirectory(Path.Combine(CurrentBasePath, directoryName));
  105. UpdateDirectoryList();
  106. }
  107. }
  108. public void Refresh()
  109. {
  110. Constants.InitializeScenes();
  111. UpdateSceneList();
  112. }
  113. public void SortScenes(SortMode sortMode)
  114. {
  115. CurrentSortMode = sortMode;
  116. Comparison<Scene> comparator;
  117. switch (CurrentSortMode)
  118. {
  119. case SortMode.DateModified: comparator = SortByDateModified; break;
  120. case SortMode.DateCreated: comparator = SortByDateCreated; break;
  121. default: comparator = SortByName; break;
  122. }
  123. SceneList.Sort(comparator);
  124. }
  125. public void DeleteScene()
  126. {
  127. if (CurrentScene.FileInfo.Exists)
  128. {
  129. CurrentScene.FileInfo.Delete();
  130. }
  131. SceneList.RemoveAt(CurrentSceneIndex);
  132. }
  133. public void LoadScene()
  134. {
  135. meidoPhotoStudio.ApplyScene(CurrentScene.FileInfo.FullName);
  136. }
  137. private int SortByName(Scene a, Scene b)
  138. {
  139. return SortDirection * string.Compare(a.FileInfo.Name, b.FileInfo.Name);
  140. }
  141. private int SortByDateCreated(Scene a, Scene b)
  142. {
  143. return SortDirection * DateTime.Compare(a.FileInfo.CreationTime, b.FileInfo.CreationTime);
  144. }
  145. private int SortByDateModified(Scene a, Scene b)
  146. {
  147. return SortDirection * DateTime.Compare(a.FileInfo.LastWriteTime, b.FileInfo.LastWriteTime);
  148. }
  149. private void UpdateSceneList()
  150. {
  151. ClearSceneList();
  152. if (!Directory.Exists(CurrentScenesDirectory))
  153. {
  154. Directory.CreateDirectory(CurrentScenesDirectory);
  155. }
  156. foreach (string filename in Directory.GetFiles(CurrentScenesDirectory))
  157. {
  158. if (Path.GetExtension(filename) == ".png") SceneList.Add(new Scene(filename));
  159. }
  160. SortScenes(CurrentSortMode);
  161. }
  162. private void UpdateDirectoryList()
  163. {
  164. string baseDirectoryName = KankyoMode ? Constants.kankyoDirectory : Constants.sceneDirectory;
  165. CurrentDirectoryList.Sort((a, b) =>
  166. {
  167. if (a.Equals(baseDirectoryName, StringComparison.InvariantCultureIgnoreCase)) return -1;
  168. else return a.CompareTo(b);
  169. });
  170. }
  171. private void ClearSceneList()
  172. {
  173. foreach (Scene scene in SceneList) scene.Destroy();
  174. SceneList.Clear();
  175. }
  176. private void QuickSaveScene()
  177. {
  178. if (Busy) return;
  179. byte[] data = meidoPhotoStudio.SerializeScene(kankyo: false);
  180. if (data == null) return;
  181. File.WriteAllBytes(Path.Combine(Constants.configPath, "mpstempscene"), data);
  182. }
  183. private void QuickLoadScene()
  184. {
  185. if (Busy) return;
  186. meidoPhotoStudio.ApplyScene(Path.Combine(Constants.configPath, "mpstempscene"));
  187. }
  188. private System.Collections.IEnumerator SaveSceneToFile(bool overwrite = false)
  189. {
  190. Busy = true;
  191. byte[] sceneData = meidoPhotoStudio.SerializeScene(KankyoMode);
  192. if (sceneData != null)
  193. {
  194. string screenshotPath = Utility.TempScreenshotFilename();
  195. MeidoPhotoStudio.TakeScreenshot(screenshotPath, 1, KankyoMode);
  196. do yield return new WaitForSecondsRealtime(0.2f);
  197. while (!File.Exists(screenshotPath));
  198. string scenePrefix = KankyoMode ? "mpskankyo" : "mpsscene";
  199. string fileName = $"{scenePrefix}{System.DateTime.Now:yyyyMMddHHmmss}.png";
  200. string savePath = Path.Combine(CurrentScenesDirectory, fileName);
  201. if (overwrite && CurrentScene != null)
  202. {
  203. savePath = CurrentScene.FileInfo.FullName;
  204. }
  205. else overwrite = false;
  206. Texture2D screenshot = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  207. screenshot.LoadImage(File.ReadAllBytes(screenshotPath));
  208. int sceneWidth = (int)SceneManager.sceneDimensions.x;
  209. int sceneHeight = (int)SceneManager.sceneDimensions.y;
  210. Utility.ResizeToFit(screenshot, sceneWidth, sceneHeight);
  211. using (FileStream fileStream = File.Create(savePath))
  212. {
  213. byte[] encodedPng = screenshot.EncodeToPNG();
  214. fileStream.Write(encodedPng, 0, encodedPng.Length);
  215. fileStream.Write(sceneData, 0, sceneData.Length);
  216. }
  217. UnityEngine.Object.DestroyImmediate(screenshot);
  218. if (overwrite)
  219. {
  220. File.SetCreationTime(savePath, CurrentScene.FileInfo.CreationTime);
  221. CurrentScene.Destroy();
  222. SceneList.RemoveAt(CurrentSceneIndex);
  223. }
  224. SceneList.Add(new Scene(savePath));
  225. SortScenes(CurrentSortMode);
  226. }
  227. Busy = false;
  228. }
  229. public class Scene
  230. {
  231. public const int initialNumberOfMaids = -2;
  232. public Texture2D Thumbnail { get; private set; }
  233. public FileInfo FileInfo { get; set; }
  234. public int NumberOfMaids { get; private set; } = initialNumberOfMaids;
  235. public Scene(string filePath)
  236. {
  237. FileInfo = new FileInfo(filePath);
  238. Thumbnail = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  239. Thumbnail.LoadImage(File.ReadAllBytes(FileInfo.FullName));
  240. }
  241. public void GetNumberOfMaids()
  242. {
  243. if (NumberOfMaids != initialNumberOfMaids) return;
  244. string filePath = FileInfo.FullName;
  245. byte[] sceneData = MeidoPhotoStudio.DecompressScene(filePath);
  246. if (sceneData == null) return;
  247. using (MemoryStream memoryStream = new MemoryStream(sceneData))
  248. using (BinaryReader binaryReader = new BinaryReader(memoryStream, System.Text.Encoding.UTF8))
  249. {
  250. try
  251. {
  252. if (binaryReader.ReadString() != "MPS_SCENE")
  253. {
  254. Utility.LogWarning($"'{filePath}' is not a {MeidoPhotoStudio.pluginName} scene");
  255. return;
  256. }
  257. if (binaryReader.ReadInt32() > MeidoPhotoStudio.sceneVersion)
  258. {
  259. Utility.LogWarning(
  260. $"'{filePath}' is made in a newer version of {MeidoPhotoStudio.pluginName}"
  261. );
  262. return;
  263. }
  264. NumberOfMaids = binaryReader.ReadInt32();
  265. }
  266. catch (Exception e)
  267. {
  268. Utility.LogWarning($"Failed to deserialize scene '{filePath}' because {e.Message}");
  269. return;
  270. }
  271. }
  272. }
  273. public void Destroy()
  274. {
  275. if (Thumbnail != null) UnityEngine.Object.DestroyImmediate(Thumbnail);
  276. }
  277. }
  278. }
  279. }