DeskManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using wf;
  7. internal class DeskManager
  8. {
  9. public static void CreateCsvData()
  10. {
  11. using (AFileBase afileBase = GameUty.FileSystem.FileOpen("desk_item_category.nei"))
  12. {
  13. using (CsvParser csvParser = new CsvParser())
  14. {
  15. bool condition = csvParser.Open(afileBase);
  16. NDebug.Assert(condition, "desk_item_category.nei\nopen failed.");
  17. for (int i = 1; i < csvParser.max_cell_y; i++)
  18. {
  19. if (!csvParser.IsCellToExistData(0, i))
  20. {
  21. break;
  22. }
  23. int cellAsInteger = csvParser.GetCellAsInteger(0, i);
  24. string cellAsString = csvParser.GetCellAsString(1, i);
  25. if (!DeskManager.item_category_data_dic_.ContainsKey(cellAsInteger))
  26. {
  27. DeskManager.item_category_data_dic_.Add(cellAsInteger, cellAsString);
  28. }
  29. }
  30. }
  31. }
  32. HashSet<int> hashSet = new HashSet<int>();
  33. CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Normal, true, "desk_item_enabled_id", ref hashSet);
  34. CsvCommonIdManager.ReadEnabledIdList(CsvCommonIdManager.FileSystemType.Old, true, "desk_item_enabled_id", ref hashSet);
  35. using (AFileBase afileBase2 = GameUty.FileSystem.FileOpen("desk_item_detail.nei"))
  36. {
  37. using (CsvParser csvParser2 = new CsvParser())
  38. {
  39. bool condition2 = csvParser2.Open(afileBase2);
  40. NDebug.Assert(condition2, "desk_item_detail.nei\nopen failed.");
  41. for (int j = 1; j < csvParser2.max_cell_y; j++)
  42. {
  43. if (csvParser2.IsCellToExistData(0, j))
  44. {
  45. int cellAsInteger2 = csvParser2.GetCellAsInteger(0, j);
  46. if (hashSet.Contains(cellAsInteger2))
  47. {
  48. DeskManager.ItemData itemData = new DeskManager.ItemData(csvParser2, j);
  49. DeskManager.item_detail_data_dic_.Add(itemData.id, itemData);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. DeskManager.item_inst_data_.Clear();
  56. DeskManager.init_item_data_.Clear();
  57. foreach (KeyValuePair<int, DeskManager.ItemData> keyValuePair in DeskManager.item_detail_data_dic_)
  58. {
  59. DeskManager.InstansData instansData = new DeskManager.InstansData(keyValuePair.Value);
  60. DeskManager.init_item_data_.Add(instansData.id, instansData);
  61. DeskManager.item_inst_data_.Add(new DeskManager.InstansData(instansData));
  62. }
  63. }
  64. public static void OnChangeBG(string bg_name, GameObject bg_object)
  65. {
  66. if (bg_object == null)
  67. {
  68. for (int i = 0; i < DeskManager.item_inst_data_.Count; i++)
  69. {
  70. DeskManager.item_inst_data_[i].item_object = null;
  71. }
  72. return;
  73. }
  74. string[] array = new string[]
  75. {
  76. "ShinShitsumu_ChairRot",
  77. "ShinShitsumu_ChairRot_Night",
  78. "ShinShitsumu",
  79. "ShinShitsumu_Night"
  80. };
  81. for (int j = 0; j < array.Length; j++)
  82. {
  83. array[j] = array[j].ToLower();
  84. }
  85. if (0 <= Array.IndexOf<string>(array, bg_name))
  86. {
  87. for (int k = 0; k < DeskManager.item_inst_data_.Count; k++)
  88. {
  89. DeskManager.InstansData instansData = DeskManager.item_inst_data_[k];
  90. if (instansData.IsPossessing())
  91. {
  92. GameObject gameObject = null;
  93. if (!string.IsNullOrEmpty(instansData.item_data.prefab_name))
  94. {
  95. gameObject = Utility.CreatePrefab(bg_object, "Prefab/" + instansData.item_data.prefab_name, true);
  96. }
  97. else if (!string.IsNullOrEmpty(instansData.item_data.asset_name))
  98. {
  99. gameObject = GameMain.Instance.BgMgr.CreateAssetBundle(instansData.item_data.asset_name);
  100. if (gameObject != null)
  101. {
  102. gameObject = UnityEngine.Object.Instantiate<GameObject>(gameObject);
  103. MeshRenderer[] componentsInChildren = gameObject.GetComponentsInChildren<MeshRenderer>();
  104. for (int l = 0; l < componentsInChildren.Length; l++)
  105. {
  106. if (componentsInChildren[l] != null)
  107. {
  108. componentsInChildren[l].shadowCastingMode = ShadowCastingMode.Off;
  109. }
  110. }
  111. }
  112. }
  113. if (gameObject != null)
  114. {
  115. gameObject.name = gameObject.name.Replace("(Clone)", string.Empty);
  116. gameObject.transform.SetParent(bg_object.transform, false);
  117. instansData.Apply(gameObject, true);
  118. instansData.item_object = gameObject;
  119. if (instansData.item_object.GetComponentInChildren<BoxCollider>() == null)
  120. {
  121. MeshRenderer componentInChildren = instansData.item_object.GetComponentInChildren<MeshRenderer>(true);
  122. if (componentInChildren != null)
  123. {
  124. componentInChildren.gameObject.AddComponent<BoxCollider>();
  125. }
  126. }
  127. }
  128. else if (!string.IsNullOrEmpty(instansData.item_data.prefab_name))
  129. {
  130. Debug.LogError("Prefab[" + instansData.item_data.prefab_name + "]を読み込めませんでした");
  131. }
  132. else
  133. {
  134. Debug.LogError("Asset[" + instansData.item_data.asset_name + "]を読み込めませんでした");
  135. }
  136. }
  137. }
  138. }
  139. }
  140. public static void SerializeSingleSaveData(BinaryWriter binary)
  141. {
  142. binary.Write("CM3D2_DeskCustomize");
  143. binary.Write(1570);
  144. binary.Write(DeskManager.item_inst_data_.Count);
  145. for (int i = 0; i < DeskManager.item_inst_data_.Count; i++)
  146. {
  147. binary.Write(DeskManager.item_inst_data_[i].id);
  148. DeskManager.item_inst_data_[i].Serialize(binary);
  149. }
  150. }
  151. public static void Deserialize()
  152. {
  153. string text = Path.Combine(GameMain.Instance.SerializeStorageManager.StoreDirectoryPath, "SaveData");
  154. if (!Directory.Exists(text))
  155. {
  156. Directory.CreateDirectory(text);
  157. }
  158. DeskManager.old_item_data_from_system_.Clear();
  159. string path = text + "/DeskCustomize.save";
  160. if (!File.Exists(path))
  161. {
  162. return;
  163. }
  164. using (FileStream fileStream = new FileStream(path, FileMode.Open))
  165. {
  166. if (fileStream != null)
  167. {
  168. byte[] buffer = new byte[fileStream.Length];
  169. fileStream.Read(buffer, 0, (int)fileStream.Length);
  170. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)))
  171. {
  172. if (binaryReader.ReadString() != "CM3D2_DeskCustomize")
  173. {
  174. NDebug.Assert("DeskCustomizeファイルのヘッダーが不正です。", false);
  175. }
  176. else
  177. {
  178. int game_ver = binaryReader.ReadInt32();
  179. int num = binaryReader.ReadInt32();
  180. for (int i = 0; i < num; i++)
  181. {
  182. int num2 = binaryReader.ReadInt32();
  183. DeskManager.InstansData instansData = new DeskManager.InstansData(binaryReader, game_ver);
  184. if (instansData.item_data != null)
  185. {
  186. if (!DeskManager.old_item_data_from_system_.ContainsKey(instansData.item_data.id))
  187. {
  188. DeskManager.old_item_data_from_system_.Add(instansData.item_data.id, instansData);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. public static void DeserializeSingleSaveData(BinaryReader binary, int version)
  198. {
  199. DeskManager.item_inst_data_.Clear();
  200. Dictionary<int, DeskManager.InstansData> dictionary = new Dictionary<int, DeskManager.InstansData>();
  201. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair in DeskManager.init_item_data_)
  202. {
  203. dictionary.Add(keyValuePair.Key, new DeskManager.InstansData(keyValuePair.Value));
  204. }
  205. if (version <= 145)
  206. {
  207. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair2 in DeskManager.old_item_data_from_system_)
  208. {
  209. if (!dictionary.ContainsKey(keyValuePair2.Key))
  210. {
  211. dictionary.Add(keyValuePair2.Key, null);
  212. }
  213. dictionary[keyValuePair2.Key] = new DeskManager.InstansData(keyValuePair2.Value);
  214. }
  215. GameMain.Instance.CharacterMgr.status.SetFlag("desk_customize_savedata_warning", 1);
  216. }
  217. else
  218. {
  219. string a = binary.ReadString();
  220. NDebug.Assert(a == "CM3D2_DeskCustomize", "セーブデータ\nデスクカスタマイズのヘッダーが正しくありません");
  221. int game_ver = binary.ReadInt32();
  222. int num = binary.ReadInt32();
  223. for (int i = 0; i < num; i++)
  224. {
  225. int key = binary.ReadInt32();
  226. DeskManager.InstansData value = new DeskManager.InstansData(binary, game_ver);
  227. if (dictionary.ContainsKey(key))
  228. {
  229. dictionary[key] = value;
  230. }
  231. }
  232. }
  233. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair3 in dictionary)
  234. {
  235. DeskManager.item_inst_data_.Add(new DeskManager.InstansData(keyValuePair3.Value));
  236. }
  237. }
  238. public static List<DeskManager.InstansData> item_inst_data
  239. {
  240. get
  241. {
  242. return DeskManager.item_inst_data_;
  243. }
  244. }
  245. public static Dictionary<int, string> item_category_data_dic
  246. {
  247. get
  248. {
  249. return DeskManager.item_category_data_dic_;
  250. }
  251. }
  252. public static Dictionary<int, DeskManager.ItemData> item_detail_data_dic
  253. {
  254. get
  255. {
  256. return DeskManager.item_detail_data_dic_;
  257. }
  258. }
  259. private static List<DeskManager.InstansData> item_inst_data_ = new List<DeskManager.InstansData>();
  260. private static Dictionary<int, DeskManager.InstansData> init_item_data_ = new Dictionary<int, DeskManager.InstansData>();
  261. private static Dictionary<int, DeskManager.InstansData> old_item_data_from_system_ = new Dictionary<int, DeskManager.InstansData>();
  262. private static Dictionary<int, string> item_category_data_dic_ = new Dictionary<int, string>();
  263. private static Dictionary<int, DeskManager.ItemData> item_detail_data_dic_ = new Dictionary<int, DeskManager.ItemData>();
  264. public class ItemData
  265. {
  266. public ItemData(CsvParser csv, int csv_y)
  267. {
  268. int num = 0;
  269. this.id = csv.GetCellAsInteger(num++, csv_y);
  270. this.name = csv.GetCellAsString(num++, csv_y);
  271. this.category_id = csv.GetCellAsInteger(num++, csv_y);
  272. this.prefab_name = csv.GetCellAsString(num++, csv_y);
  273. this.asset_name = csv.GetCellAsString(num++, csv_y);
  274. this.possession_flag = csv.GetCellAsString(num++, csv_y);
  275. string cellAsString = csv.GetCellAsString(num++, csv_y);
  276. this.seasonal_ = !string.IsNullOrEmpty(cellAsString);
  277. if (this.seasonal_)
  278. {
  279. string[] array = cellAsString.Split(new char[]
  280. {
  281. ','
  282. });
  283. this.seasonal_month = new int[array.Length];
  284. for (int i = 0; i < array.Length; i++)
  285. {
  286. this.seasonal_month[i] = int.Parse(array[i]);
  287. }
  288. }
  289. else
  290. {
  291. this.seasonal_month = new int[0];
  292. }
  293. this.init_visible = (csv.GetCellAsString(num++, csv_y) == "○");
  294. this.init_position = csv.GetCellAsVector3(num++, csv_y, ',');
  295. this.init_rotation = csv.GetCellAsVector3(num++, csv_y, ',');
  296. this.init_scale = csv.GetCellAsVector3(num++, csv_y, ',');
  297. this.init_ui_position_scale = csv.GetCellAsReal(num++, csv_y);
  298. this.init_ui_rotation_scale = csv.GetCellAsReal(num++, csv_y);
  299. }
  300. public bool seasonal
  301. {
  302. get
  303. {
  304. return !Product.supportMultiLanguage && this.seasonal_;
  305. }
  306. }
  307. public readonly int id;
  308. public readonly string name;
  309. public readonly int category_id;
  310. public readonly string prefab_name;
  311. public readonly string asset_name;
  312. private readonly bool seasonal_;
  313. public readonly string possession_flag;
  314. public readonly int[] seasonal_month;
  315. public readonly bool init_visible;
  316. public readonly Vector3 init_position;
  317. public readonly Vector3 init_rotation;
  318. public readonly Vector3 init_scale;
  319. public readonly float init_ui_position_scale;
  320. public readonly float init_ui_rotation_scale;
  321. }
  322. public class InstansData
  323. {
  324. public InstansData(DeskManager.ItemData data)
  325. {
  326. this.id = data.id;
  327. this.visible = true;
  328. this.only_season_visible = data.seasonal;
  329. this.visible = data.init_visible;
  330. this.position = data.init_position;
  331. this.rotation = data.init_rotation;
  332. this.scale = data.init_scale;
  333. this.item_data = data;
  334. }
  335. public InstansData(BinaryReader binary, int game_ver)
  336. {
  337. this.Deserialize(binary, game_ver);
  338. }
  339. public InstansData(DeskManager.InstansData instans_data)
  340. {
  341. this.id = instans_data.id;
  342. this.visible = instans_data.visible;
  343. this.only_season_visible = instans_data.only_season_visible;
  344. this.position = instans_data.position;
  345. this.rotation = instans_data.rotation;
  346. this.scale = instans_data.scale;
  347. this.item_data = instans_data.item_data;
  348. this.item_object = instans_data.item_object;
  349. }
  350. public void Apply(GameObject apply_object, bool season_visible_setting)
  351. {
  352. if (apply_object == null)
  353. {
  354. return;
  355. }
  356. Transform transform = apply_object.transform;
  357. transform.position = this.position;
  358. transform.rotation = Quaternion.Euler(this.rotation);
  359. transform.localScale = this.scale;
  360. apply_object.SetActive(this.visible);
  361. if (this.visible && season_visible_setting && this.only_season_visible && !this.IsNowMonthSeason())
  362. {
  363. apply_object.SetActive(false);
  364. }
  365. }
  366. public bool IsNowMonthSeason()
  367. {
  368. if (!this.item_data.seasonal)
  369. {
  370. return false;
  371. }
  372. DateTime now = DateTime.Now;
  373. return 0 <= Array.IndexOf<int>(this.item_data.seasonal_month, now.Month);
  374. }
  375. public bool IsPossessing()
  376. {
  377. return string.IsNullOrEmpty(this.item_data.possession_flag) || GameMain.Instance.CharacterMgr.status.GetFlag(this.item_data.possession_flag) == 1;
  378. }
  379. public void Serialize(BinaryWriter binary)
  380. {
  381. Action<Vector3> action = delegate(Vector3 data)
  382. {
  383. binary.Write(data.x);
  384. binary.Write(data.y);
  385. binary.Write(data.z);
  386. };
  387. binary.Write(this.id);
  388. binary.Write(this.visible);
  389. binary.Write(this.only_season_visible);
  390. action(this.position);
  391. action(this.rotation);
  392. action(this.scale);
  393. }
  394. public void Deserialize(BinaryReader binary, int game_ver)
  395. {
  396. Func<Vector3> func = delegate()
  397. {
  398. Vector3 zero = Vector3.zero;
  399. zero.x = binary.ReadSingle();
  400. zero.y = binary.ReadSingle();
  401. zero.z = binary.ReadSingle();
  402. return zero;
  403. };
  404. this.id = binary.ReadInt32();
  405. this.visible = binary.ReadBoolean();
  406. this.only_season_visible = binary.ReadBoolean();
  407. this.position = func();
  408. this.rotation = func();
  409. this.scale = func();
  410. if (DeskManager.item_detail_data_dic.ContainsKey(this.id))
  411. {
  412. this.item_data = DeskManager.item_detail_data_dic[this.id];
  413. }
  414. }
  415. public int id;
  416. public bool visible;
  417. public bool only_season_visible;
  418. public Vector3 position;
  419. public Vector3 rotation;
  420. public Vector3 scale;
  421. public DeskManager.ItemData item_data;
  422. public GameObject item_object;
  423. }
  424. }