Constants.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using Newtonsoft.Json;
  7. using MyRoomCustom;
  8. using UnityEngine;
  9. using wf;
  10. namespace MeidoPhotoStudio.Plugin
  11. {
  12. using static MenuFileUtility;
  13. public static class Constants
  14. {
  15. private static bool beginHandItemInit;
  16. private static bool beginMpnAttachInit;
  17. public const string customPoseDirectory = "Custom Poses";
  18. public const string customHandDirectory = "Hand Presets";
  19. public const string customFaceDirectory = "Face Presets";
  20. public const string sceneDirectory = "Scenes";
  21. public const string kankyoDirectory = "Environments";
  22. public const string configDirectory = "MeidoPhotoStudio";
  23. public const string translationDirectory = "Translations";
  24. public const string databaseDirectory = "Database";
  25. public static readonly string customPosePath;
  26. public static readonly string customHandPath;
  27. public static readonly string customFacePath;
  28. public static readonly string scenesPath;
  29. public static readonly string kankyoPath;
  30. public static readonly string configPath;
  31. public static readonly string databasePath;
  32. public static readonly int mainWindowID = 765;
  33. public static readonly int messageWindowID = 961;
  34. public static readonly int sceneManagerWindowID = 876;
  35. public static readonly int sceneManagerModalID = 283;
  36. public static readonly int dropdownWindowID = 777;
  37. public enum Window
  38. {
  39. Call, Pose, Face, BG, BG2, Main, Message, Save, SaveModal, Settings
  40. }
  41. public enum Scene
  42. {
  43. Daily = 3, Edit = 5
  44. }
  45. public static readonly List<string> PoseGroupList = new List<string>();
  46. public static readonly Dictionary<string, List<string>> PoseDict = new Dictionary<string, List<string>>();
  47. public static readonly List<string> CustomPoseGroupList = new List<string>();
  48. public static readonly Dictionary<string, List<string>> CustomPoseDict = new Dictionary<string, List<string>>();
  49. public static readonly List<string> CustomHandGroupList = new List<string>();
  50. public static readonly Dictionary<string, List<string>> CustomHandDict = new Dictionary<string, List<string>>();
  51. public static readonly List<string> FaceGroupList = new List<string>();
  52. public static readonly Dictionary<string, List<string>> FaceDict = new Dictionary<string, List<string>>();
  53. public static readonly List<string> CustomFaceGroupList = new List<string>();
  54. public static readonly Dictionary<string, List<string>> CustomFaceDict = new Dictionary<string, List<string>>();
  55. public static readonly List<string> BGList = new List<string>();
  56. public static readonly List<KeyValuePair<string, string>> MyRoomCustomBGList
  57. = new List<KeyValuePair<string, string>>();
  58. public static readonly List<string> DoguCategories = new List<string>();
  59. public static readonly Dictionary<string, List<string>> DoguDict = new Dictionary<string, List<string>>();
  60. public static readonly List<string> MyRoomPropCategories = new List<string>();
  61. public static readonly Dictionary<string, List<MyRoomItem>> MyRoomPropDict
  62. = new Dictionary<string, List<MyRoomItem>>();
  63. public static readonly Dictionary<string, List<ModItem>> ModPropDict
  64. = new Dictionary<string, List<ModItem>>(StringComparer.InvariantCultureIgnoreCase);
  65. public static readonly List<string> SceneDirectoryList = new List<string>();
  66. public static readonly List<string> KankyoDirectoryList = new List<string>();
  67. public static readonly List<MpnAttachProp> MpnAttachPropList = new List<MpnAttachProp>();
  68. public static int MyRoomCustomBGIndex { get; private set; } = -1;
  69. public static bool HandItemsInitialized { get; private set; }
  70. public static bool MpnAttachInitialized { get; private set; }
  71. public static bool MenuFilesInitialized { get; private set; }
  72. public static event EventHandler<MenuFilesEventArgs> MenuFilesChange;
  73. public static event EventHandler<PresetChangeEventArgs> CustomPoseChange;
  74. public static event EventHandler<PresetChangeEventArgs> CustomHandChange;
  75. public static event EventHandler<PresetChangeEventArgs> CustomFaceChange;
  76. public enum DoguCategory
  77. {
  78. Other, Mob, Desk, HandItem, BGSmall
  79. }
  80. public static readonly Dictionary<DoguCategory, string> customDoguCategories =
  81. new Dictionary<DoguCategory, string>()
  82. {
  83. [DoguCategory.Other] = "other",
  84. [DoguCategory.Mob] = "mob",
  85. [DoguCategory.Desk] = "desk",
  86. [DoguCategory.HandItem] = "handItem",
  87. [DoguCategory.BGSmall] = "bgSmall"
  88. };
  89. static Constants()
  90. {
  91. configPath = Path.Combine(BepInEx.Paths.ConfigPath, configDirectory);
  92. string presetPath = Path.Combine(configPath, "Presets");
  93. customPosePath = Path.Combine(presetPath, customPoseDirectory);
  94. customHandPath = Path.Combine(presetPath, customHandDirectory);
  95. customFacePath = Path.Combine(presetPath, customFaceDirectory);
  96. scenesPath = Path.Combine(configPath, sceneDirectory);
  97. kankyoPath = Path.Combine(configPath, kankyoDirectory);
  98. databasePath = Path.Combine(configPath, databaseDirectory);
  99. string[] directories = new[] {
  100. customPosePath, customHandPath, scenesPath, kankyoPath, configPath, customFacePath, databasePath
  101. };
  102. foreach (string directory in directories)
  103. {
  104. if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
  105. }
  106. }
  107. public static void Initialize()
  108. {
  109. InitializeSceneDirectories();
  110. InitializeKankyoDirectories();
  111. InitializePoses();
  112. InitializeHandPresets();
  113. InitializeFaceBlends();
  114. InitializeBGs();
  115. InitializeDogu();
  116. InitializeMyRoomProps();
  117. InitializeMpnAttachProps();
  118. }
  119. public static void AddFacePreset(Dictionary<string, float> faceData, string filename, string directory)
  120. {
  121. filename = Utility.SanitizePathPortion(filename);
  122. directory = Utility.SanitizePathPortion(directory);
  123. if (string.IsNullOrEmpty(filename)) filename = "face_preset";
  124. if (directory.Equals(customFaceDirectory, StringComparison.InvariantCultureIgnoreCase))
  125. {
  126. directory = string.Empty;
  127. }
  128. directory = Path.Combine(customFacePath, directory);
  129. if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
  130. string fullPath = Path.Combine(directory, filename);
  131. if (File.Exists($"{fullPath}.xml")) fullPath += $"_{Utility.Timestamp}";
  132. fullPath = Path.GetFullPath($"{fullPath}.xml");
  133. if (!fullPath.StartsWith(customFacePath))
  134. {
  135. Utility.LogError($"Could not save face preset! Path is invalid: '{fullPath}'");
  136. return;
  137. }
  138. XElement rootElement = new XElement("FaceData");
  139. foreach (KeyValuePair<string, float> kvp in faceData)
  140. {
  141. rootElement.Add(new XElement("elm", kvp.Value.ToString("G9"), new XAttribute("name", kvp.Key)));
  142. }
  143. XDocument fullDocument = new XDocument(
  144. new XDeclaration("1.0", "utf-8", "true"),
  145. new XComment("MeidoPhotoStudio Face Preset"),
  146. rootElement
  147. );
  148. fullDocument.Save(fullPath);
  149. FileInfo fileInfo = new FileInfo(fullPath);
  150. string category = fileInfo.Directory.Name;
  151. string faceGroup = CustomFaceGroupList.Find(
  152. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase)
  153. );
  154. if (string.IsNullOrEmpty(faceGroup))
  155. {
  156. CustomFaceGroupList.Add(category);
  157. CustomFaceDict[category] = new List<string>();
  158. CustomFaceGroupList.Sort((a, b) => KeepAtTop(a, b, customFaceDirectory));
  159. }
  160. else category = faceGroup;
  161. CustomFaceDict[category].Add(fullPath);
  162. CustomFaceDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  163. CustomFaceChange?.Invoke(null, new PresetChangeEventArgs(fullPath, category));
  164. }
  165. public static void AddPose(byte[] anmBinary, string filename, string directory)
  166. {
  167. // TODO: Consider writing a file system monitor
  168. filename = Utility.SanitizePathPortion(filename);
  169. directory = Utility.SanitizePathPortion(directory);
  170. if (string.IsNullOrEmpty(filename)) filename = "custom_pose";
  171. if (directory.Equals(customPoseDirectory, StringComparison.InvariantCultureIgnoreCase))
  172. {
  173. directory = string.Empty;
  174. }
  175. directory = Path.Combine(customPosePath, directory);
  176. if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
  177. string fullPath = Path.Combine(directory, filename);
  178. if (File.Exists($"{fullPath}.anm")) fullPath += $"_{Utility.Timestamp}";
  179. fullPath = Path.GetFullPath($"{fullPath}.anm");
  180. if (!fullPath.StartsWith(customPosePath))
  181. {
  182. Utility.LogError($"Could not save pose! Path is invalid: '{fullPath}'");
  183. return;
  184. }
  185. File.WriteAllBytes(fullPath, anmBinary);
  186. FileInfo fileInfo = new FileInfo(fullPath);
  187. string category = fileInfo.Directory.Name;
  188. string poseGroup = CustomPoseGroupList.Find(
  189. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase)
  190. );
  191. if (string.IsNullOrEmpty(poseGroup))
  192. {
  193. CustomPoseGroupList.Add(category);
  194. CustomPoseDict[category] = new List<string>();
  195. CustomPoseGroupList.Sort((a, b) => KeepAtTop(a, b, customPoseDirectory));
  196. }
  197. else category = poseGroup;
  198. CustomPoseDict[category].Add(fullPath);
  199. CustomPoseDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  200. CustomPoseChange?.Invoke(null, new PresetChangeEventArgs(fullPath, category));
  201. }
  202. public static void AddHand(byte[] handBinary, bool right, string filename, string directory)
  203. {
  204. filename = Utility.SanitizePathPortion(filename);
  205. directory = Utility.SanitizePathPortion(directory);
  206. if (string.IsNullOrEmpty(filename)) filename = "custom_hand";
  207. if (directory.Equals(customHandDirectory, StringComparison.InvariantCultureIgnoreCase))
  208. {
  209. directory = string.Empty;
  210. }
  211. directory = Path.Combine(customHandPath, directory);
  212. if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
  213. string fullPath = Path.Combine(directory, filename);
  214. if (File.Exists($"{fullPath}.xml")) fullPath += $"_{Utility.Timestamp}";
  215. fullPath = Path.GetFullPath($"{fullPath}.xml");
  216. if (!fullPath.StartsWith(customHandPath))
  217. {
  218. Utility.LogError($"Could not save hand! Path is invalid: '{fullPath}'");
  219. return;
  220. }
  221. int gameVersion = Misc.GAME_VERSION; // get game version from user's Assembly-CSharp
  222. XDocument finalXml = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
  223. new XComment("CM3D2 FingerData"),
  224. new XElement("FingerData",
  225. new XElement("GameVersion", gameVersion),
  226. new XElement("RightData", right),
  227. new XElement("BinaryData", Convert.ToBase64String(handBinary))
  228. )
  229. );
  230. finalXml.Save(fullPath);
  231. FileInfo fileInfo = new FileInfo(fullPath);
  232. string category = fileInfo.Directory.Name;
  233. string handGroup = CustomHandGroupList.Find(
  234. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase)
  235. );
  236. if (string.IsNullOrEmpty(handGroup))
  237. {
  238. CustomHandGroupList.Add(category);
  239. CustomHandDict[category] = new List<string>();
  240. CustomHandGroupList.Sort((a, b) => KeepAtTop(a, b, customHandDirectory));
  241. }
  242. else category = handGroup;
  243. CustomHandDict[category].Add(fullPath);
  244. CustomHandDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  245. CustomHandChange?.Invoke(null, new PresetChangeEventArgs(fullPath, category));
  246. }
  247. public static void InitializeSceneDirectories()
  248. {
  249. SceneDirectoryList.Clear();
  250. SceneDirectoryList.Add(sceneDirectory);
  251. foreach (string directory in Directory.GetDirectories(scenesPath))
  252. {
  253. SceneDirectoryList.Add(new DirectoryInfo(directory).Name);
  254. }
  255. SceneDirectoryList.Sort((a, b) => KeepAtTop(a, b, sceneDirectory));
  256. }
  257. public static void InitializeKankyoDirectories()
  258. {
  259. KankyoDirectoryList.Clear();
  260. KankyoDirectoryList.Add(kankyoDirectory);
  261. foreach (string directory in Directory.GetDirectories(kankyoPath))
  262. {
  263. KankyoDirectoryList.Add(new DirectoryInfo(directory).Name);
  264. }
  265. KankyoDirectoryList.Sort((a, b) => KeepAtTop(a, b, kankyoDirectory));
  266. }
  267. private static int KeepAtTop(string a, string b, string topItem)
  268. {
  269. if (a == b) return 0;
  270. if (a == topItem) return -1;
  271. if (b == topItem) return 1;
  272. else return WindowsLogicalComparer.StrCmpLogicalW(a, b);
  273. }
  274. public static void InitializePoses()
  275. {
  276. static void AddDefaultPose()
  277. {
  278. if (!PoseDict.ContainsKey("normal")) PoseDict["normal"] = new List<string>() { "maid_stand01" };
  279. if (!PoseGroupList.Contains("normal")) PoseGroupList.Insert(0, "normal");
  280. }
  281. // Load Poses
  282. string poseListPath = Path.Combine(databasePath, "mm_pose_list.json");
  283. try
  284. {
  285. string poseListJson = File.ReadAllText(poseListPath);
  286. foreach (SerializePoseList poseList in JsonConvert.DeserializeObject<List<SerializePoseList>>(poseListJson))
  287. {
  288. PoseDict[poseList.UIName] = poseList.PoseList;
  289. PoseGroupList.Add(poseList.UIName);
  290. }
  291. }
  292. catch (IOException e)
  293. {
  294. Utility.LogError($"Could not open pose database because {e.Message}");
  295. Utility.LogMessage("Pose list will be serverely limited.");
  296. AddDefaultPose();
  297. }
  298. catch (Exception e)
  299. {
  300. Utility.LogError($"Could not parse pose database because {e.Message}");
  301. AddDefaultPose();
  302. }
  303. // Get Other poses that'll go into Normal, Normal 2 and Ero 2
  304. string[] com3d2MotionList = GameUty.FileSystem.GetList("motion", AFileSystemBase.ListType.AllFile);
  305. if (com3d2MotionList?.Length > 0)
  306. {
  307. HashSet<string> poseSet = new HashSet<string>();
  308. foreach (List<string> poses in PoseDict.Values) poseSet.UnionWith(poses);
  309. string[] newCategories = new[] { "normal2", "ero2" };
  310. foreach (string category in newCategories)
  311. {
  312. if (!PoseDict.ContainsKey(category)) PoseDict[category] = new List<string>();
  313. }
  314. // TODO: Try to group these poses into more than "normal2" and "ero2"
  315. foreach (string path in com3d2MotionList)
  316. {
  317. if (Path.GetExtension(path) == ".anm")
  318. {
  319. string file = Path.GetFileNameWithoutExtension(path);
  320. if (!poseSet.Contains(file))
  321. {
  322. if (file.StartsWith("edit_")) PoseDict["normal"].Add(file);
  323. else if (file != "dance_cm3d2_001_zoukin" && file != "dance_cm3d2_001_mop"
  324. && file != "aruki_1_idougo_f" && file != "sleep2" && file != "stand_akire2"
  325. && !file.EndsWith("_3_") && !file.EndsWith("_5_") && !file.StartsWith("vr_")
  326. && !file.StartsWith("dance_mc") && !file.Contains("_kubi_") && !file.Contains("a01_")
  327. && !file.Contains("b01_") && !file.Contains("b02_") && !file.EndsWith("_m2")
  328. && !file.EndsWith("_m2_once_") && !file.StartsWith("h_") && !file.StartsWith("event_")
  329. && !file.StartsWith("man_") && !file.EndsWith("_m") && !file.Contains("_m_")
  330. && !file.Contains("_man_")
  331. )
  332. {
  333. if (path.Contains(@"\sex\")) PoseDict["ero2"].Add(file);
  334. else PoseDict["normal2"].Add(file);
  335. }
  336. }
  337. }
  338. }
  339. foreach (string category in newCategories)
  340. {
  341. if (PoseDict[category].Count > 0)
  342. {
  343. if (!PoseGroupList.Contains(category)) PoseGroupList.Add(category);
  344. }
  345. else PoseDict.Remove(category);
  346. }
  347. }
  348. InitializeCustomPoses();
  349. }
  350. public static void InitializeCustomPoses()
  351. {
  352. static void GetPoses(string directory)
  353. {
  354. IEnumerable<string> poseList = Directory.GetFiles(directory)
  355. .Where(file => Path.GetExtension(file) == ".anm");
  356. if (poseList.Any())
  357. {
  358. string poseGroupName = new DirectoryInfo(directory).Name;
  359. if (poseGroupName != customPoseDirectory) CustomPoseGroupList.Add(poseGroupName);
  360. CustomPoseDict[poseGroupName] = poseList.ToList();
  361. CustomPoseDict[poseGroupName].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  362. }
  363. }
  364. CustomPoseGroupList.Clear();
  365. CustomPoseDict.Clear();
  366. CustomPoseGroupList.Add(customPoseDirectory);
  367. CustomPoseDict[customPoseDirectory] = new List<string>();
  368. GetPoses(customPosePath);
  369. foreach (string directory in Directory.GetDirectories(customPosePath)) GetPoses(directory);
  370. CustomPoseGroupList.Sort((a, b) => KeepAtTop(a, b, customPoseDirectory));
  371. CustomPoseChange?.Invoke(null, PresetChangeEventArgs.Empty);
  372. }
  373. public static void InitializeHandPresets()
  374. {
  375. static void GetPresets(string directory)
  376. {
  377. IEnumerable<string> presetList = Directory.GetFiles(directory)
  378. .Where(file => Path.GetExtension(file) == ".xml");
  379. if (presetList.Any())
  380. {
  381. string presetCategory = new DirectoryInfo(directory).Name;
  382. if (presetCategory != customHandDirectory) CustomHandGroupList.Add(presetCategory);
  383. CustomHandDict[presetCategory] = presetList.ToList();
  384. CustomHandDict[presetCategory].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  385. }
  386. }
  387. CustomHandGroupList.Clear();
  388. CustomHandDict.Clear();
  389. CustomHandGroupList.Add(customHandDirectory);
  390. CustomHandDict[customHandDirectory] = new List<string>();
  391. GetPresets(customHandPath);
  392. foreach (string directory in Directory.GetDirectories(customHandPath)) GetPresets(directory);
  393. CustomHandGroupList.Sort((a, b) => KeepAtTop(a, b, customHandDirectory));
  394. CustomHandChange?.Invoke(null, PresetChangeEventArgs.Empty);
  395. }
  396. public static void InitializeFaceBlends()
  397. {
  398. PhotoFaceData.Create();
  399. FaceGroupList.AddRange(PhotoFaceData.popup_category_list.Select(kvp => kvp.Key));
  400. foreach (KeyValuePair<string, List<PhotoFaceData>> kvp in PhotoFaceData.category_list)
  401. {
  402. FaceDict[kvp.Key] = kvp.Value.ConvertAll(data => data.setting_name);
  403. }
  404. InitializeCustomFaceBlends();
  405. }
  406. public static void InitializeCustomFaceBlends()
  407. {
  408. static void GetFacePresets(string directory)
  409. {
  410. IEnumerable<string> presetList = Directory.GetFiles(directory)
  411. .Where(file => Path.GetExtension(file) == ".xml").ToList();
  412. if (presetList.Any())
  413. {
  414. string faceGroupName = new DirectoryInfo(directory).Name;
  415. if (faceGroupName != customFaceDirectory) CustomFaceGroupList.Add(faceGroupName);
  416. CustomFaceDict[faceGroupName] = presetList.ToList();
  417. CustomFaceDict[faceGroupName].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  418. }
  419. }
  420. CustomFaceGroupList.Clear();
  421. CustomFaceDict.Clear();
  422. CustomFaceGroupList.Add(customFaceDirectory);
  423. CustomFaceDict[customFaceDirectory] = new List<string>();
  424. GetFacePresets(customFacePath);
  425. foreach (string directory in Directory.GetDirectories(customFacePath)) GetFacePresets(directory);
  426. CustomFaceGroupList.Sort((a, b) => KeepAtTop(a, b, customFaceDirectory));
  427. CustomFaceChange?.Invoke(null, PresetChangeEventArgs.Empty);
  428. }
  429. public static void InitializeBGs()
  430. {
  431. // Load BGs
  432. PhotoBGData.Create();
  433. // COM3D2 BGs
  434. foreach (PhotoBGData bgData in PhotoBGData.data)
  435. {
  436. if (!string.IsNullOrEmpty(bgData.create_prefab_name))
  437. {
  438. string bg = bgData.create_prefab_name;
  439. BGList.Add(bg);
  440. }
  441. }
  442. // CM3D2 BGs
  443. if (GameUty.IsEnabledCompatibilityMode)
  444. {
  445. using CsvParser csvParser = OpenCsvParser("phot_bg_list.nei", GameUty.FileSystemOld);
  446. for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
  447. {
  448. if (csvParser.IsCellToExistData(3, cell_y))
  449. {
  450. string bg = csvParser.GetCellAsString(3, cell_y);
  451. BGList.Add(bg);
  452. }
  453. }
  454. }
  455. // Set index regardless of there being myRoom bgs or not
  456. MyRoomCustomBGIndex = BGList.Count;
  457. Dictionary<string, string> saveDataDict = CreativeRoomManager.GetSaveDataDic();
  458. if (saveDataDict != null)
  459. MyRoomCustomBGList.AddRange(saveDataDict);
  460. }
  461. public static void InitializeDogu()
  462. {
  463. foreach (string customCategory in customDoguCategories.Values)
  464. {
  465. DoguDict[customCategory] = new List<string>();
  466. }
  467. InitializeDeskItems();
  468. InitializePhotoBGItems();
  469. InitializeOtherDogu();
  470. InitializeHandItems();
  471. foreach (string category in PhotoBGObjectData.popup_category_list.Select(kvp => kvp.Key))
  472. {
  473. if (category == "マイオブジェクト") continue;
  474. DoguCategories.Add(category);
  475. }
  476. foreach (DoguCategory category in Enum.GetValues(typeof(DoguCategory)))
  477. {
  478. DoguCategories.Add(customDoguCategories[category]);
  479. }
  480. }
  481. private static void InitializeOtherDogu()
  482. {
  483. DoguDict[customDoguCategories[DoguCategory.BGSmall]] = BGList;
  484. DoguDict[customDoguCategories[DoguCategory.Mob]].AddRange(new[] {
  485. "Mob_Man_Stand001", "Mob_Man_Stand002", "Mob_Man_Stand003", "Mob_Man_Sit001", "Mob_Man_Sit002",
  486. "Mob_Man_Sit003", "Mob_Girl_Stand001", "Mob_Girl_Stand002", "Mob_Girl_Stand003", "Mob_Girl_Sit001",
  487. "Mob_Girl_Sit002", "Mob_Girl_Sit003"
  488. });
  489. List<string> DoguList = DoguDict[customDoguCategories[DoguCategory.Other]];
  490. // bg object extend
  491. HashSet<string> doguHashSet = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  492. doguHashSet.UnionWith(BGList);
  493. try
  494. {
  495. string ignoreListPath = Path.Combine(databasePath, "bg_ignore_list.json");
  496. string ignoreListJson = File.ReadAllText(ignoreListPath);
  497. string[] ignoreList = JsonConvert.DeserializeObject<string[]>(ignoreListJson);
  498. doguHashSet.UnionWith(ignoreList);
  499. }
  500. catch (IOException e)
  501. {
  502. Utility.LogWarning($"Could not open ignored BG database because {e.Message}");
  503. }
  504. catch { }
  505. foreach (List<string> doguList in DoguDict.Values)
  506. {
  507. doguHashSet.UnionWith(doguList);
  508. }
  509. foreach (string path in GameUty.FileSystem.GetList("bg", AFileSystemBase.ListType.AllFile))
  510. {
  511. if (Path.GetExtension(path) == ".asset_bg" && !path.Contains("myroomcustomize"))
  512. {
  513. string file = Path.GetFileNameWithoutExtension(path);
  514. if (!doguHashSet.Contains(file) && !file.EndsWith("_hit"))
  515. {
  516. DoguList.Add(file);
  517. doguHashSet.Add(file);
  518. }
  519. }
  520. }
  521. // Get cherry picked dogu that I can't find in the game files
  522. try
  523. {
  524. string doguExtendPath = Path.Combine(databasePath, "extra_dogu.json");
  525. string doguExtendJson = File.ReadAllText(doguExtendPath);
  526. DoguList.AddRange(JsonConvert.DeserializeObject<IEnumerable<string>>(doguExtendJson));
  527. }
  528. catch (IOException e)
  529. {
  530. Utility.LogError($"Could not open extra prop database because {e.Message}");
  531. }
  532. catch (Exception e)
  533. {
  534. Utility.LogError($"Could not parse extra prop database because {e.Message}");
  535. }
  536. foreach (string path in GameUty.FileSystemOld.GetList("bg", AFileSystemBase.ListType.AllFile))
  537. {
  538. if (Path.GetExtension(path) == ".asset_bg")
  539. {
  540. string file = Path.GetFileNameWithoutExtension(path);
  541. if (!doguHashSet.Contains(file) && !file.EndsWith("_not_optimisation"))
  542. {
  543. DoguList.Add(file);
  544. }
  545. }
  546. }
  547. }
  548. private static void InitializeDeskItems()
  549. {
  550. HashSet<int> enabledIDs = new HashSet<int>();
  551. CsvCommonIdManager.ReadEnabledIdList(
  552. CsvCommonIdManager.FileSystemType.Normal, true, "desk_item_enabled_id", ref enabledIDs
  553. );
  554. CsvCommonIdManager.ReadEnabledIdList(
  555. CsvCommonIdManager.FileSystemType.Old, true, "desk_item_enabled_id", ref enabledIDs
  556. );
  557. List<string> com3d2DeskDogu = DoguDict[customDoguCategories[DoguCategory.Desk]];
  558. void GetDeskItems(AFileSystemBase fs)
  559. {
  560. using CsvParser csvParser = OpenCsvParser("desk_item_detail.nei", fs);
  561. for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
  562. {
  563. if (csvParser.IsCellToExistData(0, cell_y))
  564. {
  565. int cell = csvParser.GetCellAsInteger(0, cell_y);
  566. if (enabledIDs.Contains(cell))
  567. {
  568. string dogu = string.Empty;
  569. if (csvParser.IsCellToExistData(3, cell_y))
  570. {
  571. dogu = csvParser.GetCellAsString(3, cell_y);
  572. }
  573. else if (csvParser.IsCellToExistData(4, cell_y))
  574. {
  575. dogu = csvParser.GetCellAsString(4, cell_y);
  576. }
  577. if (!string.IsNullOrEmpty(dogu))
  578. {
  579. com3d2DeskDogu.Add(dogu);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. GetDeskItems(GameUty.FileSystem);
  586. }
  587. private static void InitializePhotoBGItems()
  588. {
  589. PhotoBGObjectData.Create();
  590. List<PhotoBGObjectData> photoBGObjectList = PhotoBGObjectData.data;
  591. List<string> doguCategories = new List<string>();
  592. HashSet<string> addedCategories = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  593. foreach (PhotoBGObjectData photoBGObject in photoBGObjectList)
  594. {
  595. string category = photoBGObject.category;
  596. if (!addedCategories.Contains(category))
  597. {
  598. addedCategories.Add(category);
  599. doguCategories.Add(category);
  600. }
  601. if (!DoguDict.ContainsKey(category))
  602. {
  603. DoguDict[category] = new List<string>();
  604. }
  605. string dogu = string.Empty;
  606. if (!string.IsNullOrEmpty(photoBGObject.create_prefab_name))
  607. {
  608. dogu = photoBGObject.create_prefab_name;
  609. }
  610. else if (!string.IsNullOrEmpty(photoBGObject.create_asset_bundle_name))
  611. {
  612. dogu = photoBGObject.create_asset_bundle_name;
  613. }
  614. else if (!string.IsNullOrEmpty(photoBGObject.direct_file))
  615. {
  616. dogu = photoBGObject.direct_file;
  617. }
  618. if (!string.IsNullOrEmpty(dogu))
  619. {
  620. DoguDict[category].Add(dogu);
  621. }
  622. }
  623. DoguDict["パーティクル"].AddRange(new[] {
  624. "Particle/pLineY", "Particle/pLineP02", "Particle/pHeart01",
  625. "Particle/pLine_act2", "Particle/pstarY_act2"
  626. });
  627. }
  628. private static void InitializeHandItems()
  629. {
  630. if (HandItemsInitialized) return;
  631. if (!MenuFilesReady)
  632. {
  633. if (!beginHandItemInit) MenuFilesReadyChange += (s, a) => InitializeHandItems();
  634. beginHandItemInit = true;
  635. return;
  636. }
  637. MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
  638. HashSet<string> doguHashSet = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  639. doguHashSet.UnionWith(BGList);
  640. try
  641. {
  642. string ignoreListPath = Path.Combine(databasePath, "bg_ignore_list.json");
  643. string ignoreListJson = File.ReadAllText(ignoreListPath);
  644. string[] ignoreList = JsonConvert.DeserializeObject<string[]>(ignoreListJson);
  645. doguHashSet.UnionWith(ignoreList);
  646. }
  647. catch (IOException e)
  648. {
  649. Utility.LogWarning($"Could not open ignored BG database because {e.Message}");
  650. }
  651. catch (Exception e)
  652. {
  653. Utility.LogError($"Could not parse ignored BG database because {e.Message}");
  654. }
  655. foreach (List<string> doguList in DoguDict.Values)
  656. {
  657. doguHashSet.UnionWith(doguList);
  658. }
  659. string category = customDoguCategories[DoguCategory.HandItem];
  660. for (int i = 0; i < menuDataBase.GetDataSize(); i++)
  661. {
  662. menuDataBase.SetIndex(i);
  663. if ((MPN)menuDataBase.GetCategoryMpn() == MPN.handitem)
  664. {
  665. string menuFileName = menuDataBase.GetMenuFileName();
  666. if (menuDataBase.GetBoDelOnly() || menuFileName.EndsWith("_del.menu")) continue;
  667. string handItemAsOdogu = Utility.HandItemToOdogu(menuFileName);
  668. string isolatedHandItem = menuFileName.Substring(menuFileName.IndexOf('_') + 1);
  669. if (!doguHashSet.Contains(handItemAsOdogu) && !doguHashSet.Contains(isolatedHandItem))
  670. {
  671. doguHashSet.Add(isolatedHandItem);
  672. DoguDict[category].Add(menuFileName);
  673. // Check for a half deck of cards to add the full deck as well
  674. if (menuFileName == "handitemd_cards_i_.menu")
  675. {
  676. DoguDict[category].Add("handiteml_cards_i_.menu");
  677. }
  678. }
  679. }
  680. }
  681. HandItemsInitialized = true;
  682. OnMenuFilesChange(MenuFilesEventArgs.EventType.HandItems);
  683. }
  684. private static void InitializeMpnAttachProps()
  685. {
  686. if (MpnAttachInitialized) return;
  687. if (!MenuFilesReady)
  688. {
  689. if (!beginMpnAttachInit) MenuFilesReadyChange += (s, a) => InitializeMpnAttachProps();
  690. beginMpnAttachInit = true;
  691. return;
  692. }
  693. MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
  694. MPN[] attachMpn = { MPN.kousoku_lower, MPN.kousoku_upper };
  695. for (int i = 0; i < menuDataBase.GetDataSize(); i++)
  696. {
  697. menuDataBase.SetIndex(i);
  698. MPN itemMpn = (MPN)menuDataBase.GetCategoryMpn();
  699. if (attachMpn.Any(mpn => mpn == itemMpn))
  700. {
  701. string menuFileName = menuDataBase.GetMenuFileName();
  702. string mpnTag = menuDataBase.GetCategoryMpnText();
  703. if (menuDataBase.GetBoDelOnly() || menuFileName.EndsWith("_del.menu")) continue;
  704. MpnAttachPropList.Add(new MpnAttachProp(itemMpn, menuFileName));
  705. }
  706. }
  707. MpnAttachInitialized = true;
  708. OnMenuFilesChange(MenuFilesEventArgs.EventType.MpnAttach);
  709. }
  710. private static void InitializeMyRoomProps()
  711. {
  712. PlacementData.CreateData();
  713. List<PlacementData.Data> myRoomData = PlacementData.GetAllDatas(false);
  714. myRoomData.Sort((a, b) =>
  715. {
  716. int res = a.categoryID.CompareTo(b.categoryID);
  717. if (res == 0) res = a.ID.CompareTo(b.ID);
  718. return res;
  719. });
  720. foreach (PlacementData.Data data in myRoomData)
  721. {
  722. string category = PlacementData.GetCategoryName(data.categoryID);
  723. if (!MyRoomPropDict.ContainsKey(category))
  724. {
  725. MyRoomPropCategories.Add(category);
  726. MyRoomPropDict[category] = new List<MyRoomItem>();
  727. }
  728. string asset = !string.IsNullOrEmpty(data.resourceName) ? data.resourceName : data.assetName;
  729. MyRoomItem item = new MyRoomItem() { PrefabName = asset, ID = data.ID };
  730. MyRoomPropDict[category].Add(item);
  731. }
  732. }
  733. private static void InitializeModProps()
  734. {
  735. for (int i = 1; i < MenuCategories.Length; i++)
  736. {
  737. ModPropDict[MenuCategories[i]] = new List<ModItem>();
  738. }
  739. if (!PropManager.ModItemsOnly)
  740. {
  741. MenuDataBase menuDatabase = GameMain.Instance.MenuDataBase;
  742. for (int i = 0; i < menuDatabase.GetDataSize(); i++)
  743. {
  744. menuDatabase.SetIndex(i);
  745. ModItem modItem = new ModItem();
  746. if (ParseNativeMenuFile(i, modItem))
  747. {
  748. ModPropDict[modItem.Category].Add(modItem);
  749. }
  750. }
  751. }
  752. MenuFileCache cache = new MenuFileCache();
  753. foreach (string modMenuFile in GameUty.ModOnlysMenuFiles)
  754. {
  755. ModItem modItem;
  756. if (cache.Has(modMenuFile)) modItem = cache[modMenuFile];
  757. else
  758. {
  759. modItem = ModItem.Mod(modMenuFile);
  760. ParseMenuFile(modMenuFile, modItem);
  761. cache[modMenuFile] = modItem;
  762. }
  763. if (ValidBG2MenuFile(modItem)) ModPropDict[modItem.Category].Add(modItem);
  764. }
  765. cache.Serialize();
  766. foreach (string modFile in Menu.GetModFiles())
  767. {
  768. ModItem modItem = ModItem.OfficialMod(modFile);
  769. if (ParseModMenuFile(modFile, modItem))
  770. {
  771. ModPropDict[modItem.Category].Add(modItem);
  772. }
  773. }
  774. MenuFilesInitialized = true;
  775. }
  776. public static List<ModItem> GetModPropList(string category)
  777. {
  778. if (!PropManager.ModItemsOnly && !MenuFilesReady)
  779. {
  780. Utility.LogMessage("Menu files are not ready yet");
  781. return null;
  782. }
  783. if (!MenuFilesInitialized) InitializeModProps();
  784. if (!ModPropDict.ContainsKey(category)) return null;
  785. List<ModItem> selectedList = ModPropDict[category];
  786. if (selectedList[0].Icon == null)
  787. {
  788. selectedList.Sort((a, b) =>
  789. {
  790. int res = a.Priority.CompareTo(b.Priority);
  791. if (res == 0) res = string.Compare(a.Name, b.Name);
  792. return res;
  793. });
  794. string previousMenuFile = string.Empty;
  795. selectedList.RemoveAll(item =>
  796. {
  797. if (item.Icon == null)
  798. {
  799. Texture2D icon;
  800. string iconFile = item.IconFile;
  801. if (string.IsNullOrEmpty(iconFile))
  802. {
  803. Utility.LogWarning($"Could not find icon '{iconFile}' for menu '{item.MenuFile}");
  804. return true;
  805. }
  806. try
  807. {
  808. icon = ImportCM.CreateTexture(iconFile);
  809. }
  810. catch
  811. {
  812. try
  813. {
  814. icon = ImportCM.CreateTexture($"tex\\{iconFile}");
  815. }
  816. catch
  817. {
  818. Utility.LogWarning($"Could not load '{iconFile}' for menu '{item.MenuFile}");
  819. return true;
  820. }
  821. }
  822. item.Icon = icon;
  823. }
  824. return false;
  825. });
  826. }
  827. return selectedList;
  828. }
  829. private static CsvParser OpenCsvParser(string nei, AFileSystemBase fs)
  830. {
  831. try
  832. {
  833. if (fs.IsExistentFile(nei))
  834. {
  835. AFileBase file = fs.FileOpen(nei);
  836. CsvParser csvParser = new CsvParser();
  837. if (csvParser.Open(file)) return csvParser;
  838. file?.Dispose();
  839. }
  840. }
  841. catch { }
  842. return null;
  843. }
  844. private static void OnMenuFilesChange(MenuFilesEventArgs.EventType eventType)
  845. {
  846. MenuFilesChange?.Invoke(null, new MenuFilesEventArgs(eventType));
  847. }
  848. private class SerializePoseList
  849. {
  850. public string UIName { get; set; }
  851. public List<string> PoseList { get; set; }
  852. }
  853. }
  854. public class MenuFilesEventArgs : EventArgs
  855. {
  856. public EventType Type { get; }
  857. public enum EventType
  858. {
  859. HandItems, MenuFiles, MpnAttach
  860. }
  861. public MenuFilesEventArgs(EventType type) => Type = type;
  862. }
  863. public class PresetChangeEventArgs : EventArgs
  864. {
  865. public string Category { get; }
  866. public string Path { get; }
  867. public static new PresetChangeEventArgs Empty { get; } = new PresetChangeEventArgs(string.Empty, string.Empty);
  868. public PresetChangeEventArgs(string path, string category)
  869. {
  870. Path = path;
  871. Category = category;
  872. }
  873. }
  874. public readonly struct MpnAttachProp
  875. {
  876. public MPN Tag { get; }
  877. public string MenuFile { get; }
  878. public MpnAttachProp(MPN tag, string menuFile)
  879. {
  880. Tag = tag;
  881. MenuFile = menuFile;
  882. }
  883. }
  884. }