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