SceneManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using BepInEx.Configuration;
  7. namespace COM3D2.MeidoPhotoStudio.Plugin
  8. {
  9. using Input = InputManager;
  10. public class SceneManager : IManager
  11. {
  12. private static byte[] tempSceneData;
  13. private static string TempScenePath => Path.Combine(Constants.configPath, "mpstempscene");
  14. public static bool Busy { get; private set; }
  15. public static readonly Vector2 sceneDimensions = new Vector2(480, 270);
  16. private static readonly ConfigEntry<bool> sortDescending;
  17. private static readonly ConfigEntry<SortMode> currentSortMode;
  18. private readonly MeidoPhotoStudio meidoPhotoStudio;
  19. private int SortDirection => SortDescending ? -1 : 1;
  20. public bool Initialized { get; private set; }
  21. public bool KankyoMode { get; set; }
  22. public bool SortDescending
  23. {
  24. get => sortDescending.Value;
  25. set => sortDescending.Value = value;
  26. }
  27. public List<MPSScene> SceneList { get; } = new();
  28. public int CurrentDirectoryIndex { get; private set; } = -1;
  29. public string CurrentDirectoryName => CurrentDirectoryList[CurrentDirectoryIndex];
  30. public List<string> CurrentDirectoryList
  31. => KankyoMode ? Constants.KankyoDirectoryList : Constants.SceneDirectoryList;
  32. public string CurrentBasePath => KankyoMode ? Constants.kankyoPath : Constants.scenesPath;
  33. public string CurrentScenesDirectory
  34. => CurrentDirectoryIndex == 0 ? CurrentBasePath : Path.Combine(CurrentBasePath, CurrentDirectoryName);
  35. public SortMode CurrentSortMode
  36. {
  37. get => currentSortMode.Value;
  38. private set => currentSortMode.Value = value;
  39. }
  40. public int CurrentSceneIndex { get; private set; } = -1;
  41. public MPSScene CurrentScene => SceneList.Count == 0 ? null : SceneList[CurrentSceneIndex];
  42. public enum SortMode
  43. {
  44. Name, DateCreated, DateModified
  45. }
  46. static SceneManager()
  47. {
  48. sortDescending = Configuration.Config.Bind(
  49. "SceneManager", "SortDescending",
  50. false,
  51. "Sort scenes descending (Z-A)"
  52. );
  53. currentSortMode = Configuration.Config.Bind(
  54. "SceneManager", "SortMode",
  55. SortMode.Name,
  56. "Scene sorting mode"
  57. );
  58. Input.Register(MpsKey.OpenSceneManager, KeyCode.F8, "Hide/show scene manager");
  59. Input.Register(MpsKey.SaveScene, KeyCode.S, "Quick save scene");
  60. Input.Register(MpsKey.LoadScene, KeyCode.A, "Load quick saved scene");
  61. }
  62. public SceneManager(MeidoPhotoStudio meidoPhotoStudio)
  63. {
  64. this.meidoPhotoStudio = meidoPhotoStudio;
  65. Activate();
  66. }
  67. public void Activate() { }
  68. public void Initialize()
  69. {
  70. if (!Initialized)
  71. {
  72. Initialized = true;
  73. SelectDirectory(0);
  74. }
  75. }
  76. public void Deactivate() => ClearSceneList();
  77. public void Update()
  78. {
  79. if (Input.Control)
  80. {
  81. if (Input.GetKeyDown(MpsKey.SaveScene)) QuickSaveScene();
  82. else if (Input.GetKeyDown(MpsKey.LoadScene)) QuickLoadScene();
  83. }
  84. }
  85. public void DeleteDirectory()
  86. {
  87. if (Directory.Exists(CurrentScenesDirectory)) Directory.Delete(CurrentScenesDirectory, true);
  88. CurrentDirectoryList.RemoveAt(CurrentDirectoryIndex);
  89. CurrentDirectoryIndex = Mathf.Clamp(CurrentDirectoryIndex, 0, CurrentDirectoryList.Count - 1);
  90. UpdateSceneList();
  91. }
  92. public void OverwriteScene() => SaveScene(overwrite: true);
  93. public void ToggleKankyoMode()
  94. {
  95. KankyoMode = !KankyoMode;
  96. CurrentDirectoryIndex = 0;
  97. UpdateSceneList();
  98. }
  99. public void SaveScene(bool overwrite = false)
  100. {
  101. if (Busy) return;
  102. Busy = true;
  103. if (!Directory.Exists(CurrentScenesDirectory)) Directory.CreateDirectory(CurrentScenesDirectory);
  104. MeidoPhotoStudio.NotifyRawScreenshot += SaveScene;
  105. MeidoPhotoStudio.TakeScreenshot(new ScreenshotEventArgs() { InMemory = true });
  106. void SaveScene(object sender, ScreenshotEventArgs args)
  107. {
  108. MeidoPhotoStudio.NotifyRawScreenshot -= SaveScene;
  109. SaveSceneToFile(args.Screenshot, overwrite);
  110. }
  111. }
  112. public void SelectDirectory(int directoryIndex)
  113. {
  114. directoryIndex = Mathf.Clamp(directoryIndex, 0, CurrentDirectoryList.Count - 1);
  115. if (directoryIndex == CurrentDirectoryIndex) return;
  116. CurrentDirectoryIndex = directoryIndex;
  117. UpdateSceneList();
  118. }
  119. public void SelectScene(int sceneIndex)
  120. {
  121. CurrentSceneIndex = Mathf.Clamp(sceneIndex, 0, SceneList.Count - 1);
  122. CurrentScene.Preload();
  123. }
  124. public void AddDirectory(string directoryName)
  125. {
  126. directoryName = Utility.SanitizePathPortion(directoryName);
  127. if (!CurrentDirectoryList.Contains(directoryName, StringComparer.InvariantCultureIgnoreCase))
  128. {
  129. string finalPath = Path.Combine(CurrentBasePath, directoryName);
  130. string fullPath = Path.GetFullPath(finalPath);
  131. if (!fullPath.StartsWith(CurrentBasePath))
  132. {
  133. string baseDirectoryName = KankyoMode ? Constants.kankyoDirectory : Constants.sceneDirectory;
  134. Utility.LogError($"Could not add directory to {baseDirectoryName}. Path is invalid: '{fullPath}'");
  135. return;
  136. }
  137. CurrentDirectoryList.Add(directoryName);
  138. Directory.CreateDirectory(finalPath);
  139. UpdateDirectoryList();
  140. CurrentDirectoryIndex = CurrentDirectoryList.IndexOf(directoryName);
  141. UpdateSceneList();
  142. }
  143. }
  144. public void Refresh()
  145. {
  146. if (!Directory.Exists(CurrentScenesDirectory)) CurrentDirectoryIndex = 0;
  147. if (KankyoMode) Constants.InitializeKankyoDirectories();
  148. else Constants.InitializeSceneDirectories();
  149. UpdateSceneList();
  150. }
  151. public void SortScenes(SortMode sortMode)
  152. {
  153. CurrentSortMode = sortMode;
  154. Comparison<MPSScene> comparator = CurrentSortMode switch
  155. {
  156. SortMode.DateModified => SortByDateModified,
  157. SortMode.DateCreated => SortByDateCreated,
  158. _ => SortByName,
  159. };
  160. SceneList.Sort(comparator);
  161. }
  162. public void DeleteScene()
  163. {
  164. if (CurrentScene.FileInfo.Exists)
  165. {
  166. CurrentScene.FileInfo.Delete();
  167. }
  168. SceneList.RemoveAt(CurrentSceneIndex);
  169. CurrentSceneIndex = Mathf.Clamp(CurrentSceneIndex, 0, SceneList.Count - 1);
  170. }
  171. public void LoadScene(MPSScene scene) => meidoPhotoStudio.LoadScene(scene.Data);
  172. private int SortByName(MPSScene a, MPSScene b)
  173. {
  174. return SortDirection * LexicographicStringComparer.Comparison(a.FileInfo.Name, b.FileInfo.Name);
  175. }
  176. private int SortByDateCreated(MPSScene a, MPSScene b)
  177. {
  178. return SortDirection * DateTime.Compare(a.FileInfo.CreationTime, b.FileInfo.CreationTime);
  179. }
  180. private int SortByDateModified(MPSScene a, MPSScene b)
  181. {
  182. return SortDirection * DateTime.Compare(a.FileInfo.LastWriteTime, b.FileInfo.LastWriteTime);
  183. }
  184. private void UpdateSceneList()
  185. {
  186. ClearSceneList();
  187. if (!Directory.Exists(CurrentScenesDirectory))
  188. {
  189. Directory.CreateDirectory(CurrentScenesDirectory);
  190. }
  191. foreach (string filename in Directory.GetFiles(CurrentScenesDirectory))
  192. {
  193. if (Path.GetExtension(filename) == ".png") SceneList.Add(new MPSScene(filename));
  194. }
  195. SortScenes(CurrentSortMode);
  196. CurrentSceneIndex = Mathf.Clamp(CurrentSceneIndex, 0, SceneList.Count - 1);
  197. }
  198. private void UpdateDirectoryList()
  199. {
  200. string baseDirectoryName = KankyoMode ? Constants.kankyoDirectory : Constants.sceneDirectory;
  201. CurrentDirectoryList.Sort((a, b)
  202. => a.Equals(baseDirectoryName, StringComparison.InvariantCultureIgnoreCase)
  203. ? -1 : LexicographicStringComparer.Comparison(a, b));
  204. }
  205. private void ClearSceneList()
  206. {
  207. foreach (MPSScene scene in SceneList) scene.Destroy();
  208. SceneList.Clear();
  209. }
  210. private void QuickSaveScene()
  211. {
  212. if (Busy) return;
  213. var data = meidoPhotoStudio.SaveScene();
  214. if (data == null) return;
  215. tempSceneData = data;
  216. File.WriteAllBytes(TempScenePath, data);
  217. }
  218. private void QuickLoadScene()
  219. {
  220. if (Busy) return;
  221. if (tempSceneData == null)
  222. {
  223. if (File.Exists(TempScenePath)) tempSceneData = File.ReadAllBytes(TempScenePath);
  224. else return;
  225. }
  226. meidoPhotoStudio.LoadScene(tempSceneData);
  227. }
  228. private void SaveSceneToFile(Texture2D screenshot, bool overwrite = false)
  229. {
  230. Busy = true;
  231. byte[] sceneData = meidoPhotoStudio.SaveScene(KankyoMode);
  232. if (sceneData != null)
  233. {
  234. string scenePrefix = KankyoMode ? "mpskankyo" : "mpsscene";
  235. string fileName = $"{scenePrefix}{Utility.Timestamp}.png";
  236. string savePath = Path.Combine(CurrentScenesDirectory, fileName);
  237. if (overwrite && CurrentScene?.FileInfo != null) savePath = CurrentScene.FileInfo.FullName;
  238. else overwrite = false;
  239. Utility.ResizeToFit(screenshot, (int)sceneDimensions.x, (int)sceneDimensions.y);
  240. using (FileStream fileStream = File.Create(savePath))
  241. {
  242. byte[] encodedPng = screenshot.EncodeToPNG();
  243. fileStream.Write(encodedPng, 0, encodedPng.Length);
  244. fileStream.Write(sceneData, 0, sceneData.Length);
  245. }
  246. if (overwrite)
  247. {
  248. File.SetCreationTime(savePath, CurrentScene.FileInfo.CreationTime);
  249. CurrentScene.Destroy();
  250. SceneList.RemoveAt(CurrentSceneIndex);
  251. }
  252. SceneList.Add(new MPSScene(savePath));
  253. SortScenes(CurrentSortMode);
  254. }
  255. Busy = false;
  256. }
  257. }
  258. }