DeskManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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(1300);
  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 fullPath = Path.GetFullPath(".\\");
  154. string text = fullPath + "SaveData";
  155. if (!Directory.Exists(text))
  156. {
  157. Directory.CreateDirectory(text);
  158. }
  159. DeskManager.old_item_data_from_system_.Clear();
  160. string path = text + "/DeskCustomize.save";
  161. if (!File.Exists(path))
  162. {
  163. return;
  164. }
  165. using (FileStream fileStream = new FileStream(path, FileMode.Open))
  166. {
  167. if (fileStream != null)
  168. {
  169. byte[] buffer = new byte[fileStream.Length];
  170. fileStream.Read(buffer, 0, (int)fileStream.Length);
  171. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)))
  172. {
  173. if (binaryReader.ReadString() != "CM3D2_DeskCustomize")
  174. {
  175. NDebug.Assert("DeskCustomizeファイルのヘッダーが不正です。", false);
  176. }
  177. else
  178. {
  179. int game_ver = binaryReader.ReadInt32();
  180. int num = binaryReader.ReadInt32();
  181. for (int i = 0; i < num; i++)
  182. {
  183. int num2 = binaryReader.ReadInt32();
  184. DeskManager.InstansData instansData = new DeskManager.InstansData(binaryReader, game_ver);
  185. if (instansData.item_data != null)
  186. {
  187. if (!DeskManager.old_item_data_from_system_.ContainsKey(instansData.item_data.id))
  188. {
  189. DeskManager.old_item_data_from_system_.Add(instansData.item_data.id, instansData);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. public static void DeserializeSingleSaveData(BinaryReader binary, int version)
  199. {
  200. DeskManager.item_inst_data_.Clear();
  201. Dictionary<int, DeskManager.InstansData> dictionary = new Dictionary<int, DeskManager.InstansData>();
  202. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair in DeskManager.init_item_data_)
  203. {
  204. dictionary.Add(keyValuePair.Key, new DeskManager.InstansData(keyValuePair.Value));
  205. }
  206. if (version <= 145)
  207. {
  208. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair2 in DeskManager.old_item_data_from_system_)
  209. {
  210. if (!dictionary.ContainsKey(keyValuePair2.Key))
  211. {
  212. dictionary.Add(keyValuePair2.Key, null);
  213. }
  214. dictionary[keyValuePair2.Key] = new DeskManager.InstansData(keyValuePair2.Value);
  215. }
  216. GameMain.Instance.CharacterMgr.status.SetFlag("desk_customize_savedata_warning", 1);
  217. }
  218. else
  219. {
  220. string a = binary.ReadString();
  221. NDebug.Assert(a == "CM3D2_DeskCustomize", "セーブデータ\nデスクカスタマイズのヘッダーが正しくありません");
  222. int game_ver = binary.ReadInt32();
  223. int num = binary.ReadInt32();
  224. for (int i = 0; i < num; i++)
  225. {
  226. int key = binary.ReadInt32();
  227. DeskManager.InstansData value = new DeskManager.InstansData(binary, game_ver);
  228. if (dictionary.ContainsKey(key))
  229. {
  230. dictionary[key] = value;
  231. }
  232. }
  233. }
  234. foreach (KeyValuePair<int, DeskManager.InstansData> keyValuePair3 in dictionary)
  235. {
  236. DeskManager.item_inst_data_.Add(new DeskManager.InstansData(keyValuePair3.Value));
  237. }
  238. }
  239. public static List<DeskManager.InstansData> item_inst_data
  240. {
  241. get
  242. {
  243. return DeskManager.item_inst_data_;
  244. }
  245. }
  246. public static Dictionary<int, string> item_category_data_dic
  247. {
  248. get
  249. {
  250. return DeskManager.item_category_data_dic_;
  251. }
  252. }
  253. public static Dictionary<int, DeskManager.ItemData> item_detail_data_dic
  254. {
  255. get
  256. {
  257. return DeskManager.item_detail_data_dic_;
  258. }
  259. }
  260. private static List<DeskManager.InstansData> item_inst_data_ = new List<DeskManager.InstansData>();
  261. private static Dictionary<int, DeskManager.InstansData> init_item_data_ = new Dictionary<int, DeskManager.InstansData>();
  262. private static Dictionary<int, DeskManager.InstansData> old_item_data_from_system_ = new Dictionary<int, DeskManager.InstansData>();
  263. private static Dictionary<int, string> item_category_data_dic_ = new Dictionary<int, string>();
  264. private static Dictionary<int, DeskManager.ItemData> item_detail_data_dic_ = new Dictionary<int, DeskManager.ItemData>();
  265. public class ItemData
  266. {
  267. public ItemData(CsvParser csv, int csv_y)
  268. {
  269. int num = 0;
  270. this.id = csv.GetCellAsInteger(num++, csv_y);
  271. this.name = csv.GetCellAsString(num++, csv_y);
  272. this.category_id = csv.GetCellAsInteger(num++, csv_y);
  273. this.prefab_name = csv.GetCellAsString(num++, csv_y);
  274. this.asset_name = csv.GetCellAsString(num++, csv_y);
  275. this.possession_flag = csv.GetCellAsString(num++, csv_y);
  276. string cellAsString = csv.GetCellAsString(num++, csv_y);
  277. this.seasonal_ = !string.IsNullOrEmpty(cellAsString);
  278. if (this.seasonal_)
  279. {
  280. string[] array = cellAsString.Split(new char[]
  281. {
  282. ','
  283. });
  284. this.seasonal_month = new int[array.Length];
  285. for (int i = 0; i < array.Length; i++)
  286. {
  287. this.seasonal_month[i] = int.Parse(array[i]);
  288. }
  289. }
  290. else
  291. {
  292. this.seasonal_month = new int[0];
  293. }
  294. this.init_visible = (csv.GetCellAsString(num++, csv_y) == "○");
  295. this.init_position = csv.GetCellAsVector3(num++, csv_y, ',');
  296. this.init_rotation = csv.GetCellAsVector3(num++, csv_y, ',');
  297. this.init_scale = csv.GetCellAsVector3(num++, csv_y, ',');
  298. this.init_ui_position_scale = csv.GetCellAsReal(num++, csv_y);
  299. this.init_ui_rotation_scale = csv.GetCellAsReal(num++, csv_y);
  300. }
  301. public bool seasonal
  302. {
  303. get
  304. {
  305. return !Product.supportMultiLanguage && this.seasonal_;
  306. }
  307. }
  308. public readonly int id;
  309. public readonly string name;
  310. public readonly int category_id;
  311. public readonly string prefab_name;
  312. public readonly string asset_name;
  313. private readonly bool seasonal_;
  314. public readonly string possession_flag;
  315. public readonly int[] seasonal_month;
  316. public readonly bool init_visible;
  317. public readonly Vector3 init_position;
  318. public readonly Vector3 init_rotation;
  319. public readonly Vector3 init_scale;
  320. public readonly float init_ui_position_scale;
  321. public readonly float init_ui_rotation_scale;
  322. }
  323. public class InstansData
  324. {
  325. public InstansData(DeskManager.ItemData data)
  326. {
  327. this.id = data.id;
  328. this.visible = true;
  329. this.only_season_visible = data.seasonal;
  330. this.visible = data.init_visible;
  331. this.position = data.init_position;
  332. this.rotation = data.init_rotation;
  333. this.scale = data.init_scale;
  334. this.item_data = data;
  335. }
  336. public InstansData(BinaryReader binary, int game_ver)
  337. {
  338. this.Deserialize(binary, game_ver);
  339. }
  340. public InstansData(DeskManager.InstansData instans_data)
  341. {
  342. this.id = instans_data.id;
  343. this.visible = instans_data.visible;
  344. this.only_season_visible = instans_data.only_season_visible;
  345. this.position = instans_data.position;
  346. this.rotation = instans_data.rotation;
  347. this.scale = instans_data.scale;
  348. this.item_data = instans_data.item_data;
  349. this.item_object = instans_data.item_object;
  350. }
  351. public void Apply(GameObject apply_object, bool season_visible_setting)
  352. {
  353. if (apply_object == null)
  354. {
  355. return;
  356. }
  357. Transform transform = apply_object.transform;
  358. transform.position = this.position;
  359. transform.rotation = Quaternion.Euler(this.rotation);
  360. transform.localScale = this.scale;
  361. apply_object.SetActive(this.visible);
  362. if (this.visible && season_visible_setting && this.only_season_visible && !this.IsNowMonthSeason())
  363. {
  364. apply_object.SetActive(false);
  365. }
  366. }
  367. public bool IsNowMonthSeason()
  368. {
  369. if (!this.item_data.seasonal)
  370. {
  371. return false;
  372. }
  373. DateTime now = DateTime.Now;
  374. return 0 <= Array.IndexOf<int>(this.item_data.seasonal_month, now.Month);
  375. }
  376. public bool IsPossessing()
  377. {
  378. return string.IsNullOrEmpty(this.item_data.possession_flag) || GameMain.Instance.CharacterMgr.status.GetFlag(this.item_data.possession_flag) == 1;
  379. }
  380. public void Serialize(BinaryWriter binary)
  381. {
  382. Action<Vector3> action = delegate(Vector3 data)
  383. {
  384. binary.Write(data.x);
  385. binary.Write(data.y);
  386. binary.Write(data.z);
  387. };
  388. binary.Write(this.id);
  389. binary.Write(this.visible);
  390. binary.Write(this.only_season_visible);
  391. action(this.position);
  392. action(this.rotation);
  393. action(this.scale);
  394. }
  395. public void Deserialize(BinaryReader binary, int game_ver)
  396. {
  397. Func<Vector3> func = delegate()
  398. {
  399. Vector3 zero = Vector3.zero;
  400. zero.x = binary.ReadSingle();
  401. zero.y = binary.ReadSingle();
  402. zero.z = binary.ReadSingle();
  403. return zero;
  404. };
  405. this.id = binary.ReadInt32();
  406. this.visible = binary.ReadBoolean();
  407. this.only_season_visible = binary.ReadBoolean();
  408. this.position = func();
  409. this.rotation = func();
  410. this.scale = func();
  411. if (DeskManager.item_detail_data_dic.ContainsKey(this.id))
  412. {
  413. this.item_data = DeskManager.item_detail_data_dic[this.id];
  414. }
  415. }
  416. public int id;
  417. public bool visible;
  418. public bool only_season_visible;
  419. public Vector3 position;
  420. public Vector3 rotation;
  421. public Vector3 scale;
  422. public DeskManager.ItemData item_data;
  423. public GameObject item_object;
  424. }
  425. }