PhotoModeSaveAndLoad.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. using System;
  2. using System.IO;
  3. using System.Xml.Linq;
  4. using UnityEngine;
  5. using wf;
  6. public class PhotoModeSaveAndLoad : MonoBehaviour
  7. {
  8. public string folder_path
  9. {
  10. get
  11. {
  12. return this.folder_path_;
  13. }
  14. set
  15. {
  16. if (!Directory.Exists(value))
  17. {
  18. Directory.CreateDirectory(value);
  19. }
  20. this.folder_path_ = value;
  21. }
  22. }
  23. public void Awake()
  24. {
  25. NDebug.Assert(0 < this.Pages, "pages error.");
  26. EventDelegate.Add(UTY.GetChildObject(base.gameObject, "Cancel", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.Close));
  27. for (int i = 1; i <= this.Pages; i++)
  28. {
  29. GameObject gameObject = Utility.CreatePrefab(this.PageButtonParent.gameObject, "SceneDaily/SaveAndLoad/Prefab/PhotSaveLoagPageBtn", true);
  30. UIWFTabButton component = gameObject.GetComponent<UIWFTabButton>();
  31. component.gameObject.name = i.ToString();
  32. component.normalSprite = "cm3d2_save_load_page" + i.ToString("d2");
  33. EventDelegate.Add(component.onSelect, new EventDelegate.Callback(this.OnClickPageBtn));
  34. }
  35. this.PageButtonParent.Reposition();
  36. this.PageButtonParent.GetComponent<UIWFTabPanel>().UpdateChildren();
  37. this.data_unit_ = new PhotSaveLoagDataUnit[6];
  38. for (int j = 0; j < this.data_unit_.Length; j++)
  39. {
  40. GameObject gameObject2 = Utility.CreatePrefab(this.DataUnitParent.gameObject, "SceneDaily/SaveAndLoad/Prefab/PhotSaveLoagDataUnit", true);
  41. gameObject2.name = j.ToString();
  42. this.data_unit_[j] = gameObject2.GetComponent<PhotSaveLoagDataUnit>();
  43. this.data_unit_[j].visible_content = false;
  44. this.data_unit_[j].onClick = new Action<PhotSaveLoagDataUnit>(this.OnClickDataUnit);
  45. this.data_unit_[j].onChangeTitle = new Action<PhotSaveLoagDataUnit>(this.OnChangeTitle);
  46. this.data_unit_[j].onChangeComment = new Action<PhotSaveLoagDataUnit>(this.OnChangeComment);
  47. }
  48. EventDelegate.Add(UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplaySave", false).GetComponent<UIButton>().onClick, delegate()
  49. {
  50. this.ChangeType((this.cur_type_ != PhotoModeSaveAndLoad.Type.Save) ? PhotoModeSaveAndLoad.Type.Save : PhotoModeSaveAndLoad.Type.Load);
  51. this.UpdateDataUnit();
  52. });
  53. EventDelegate.Add(UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplayLoad", false).GetComponent<UIButton>().onClick, delegate()
  54. {
  55. this.ChangeType((this.cur_type_ != PhotoModeSaveAndLoad.Type.Save) ? PhotoModeSaveAndLoad.Type.Save : PhotoModeSaveAndLoad.Type.Load);
  56. this.UpdateDataUnit();
  57. });
  58. this.ChangeType(PhotoModeSaveAndLoad.Type.Save);
  59. this.PageButtonParent.GetComponent<UIWFTabPanel>().Select(this.PageButtonParent.GetChild(0).GetComponent<UIWFTabButton>());
  60. }
  61. public void Start()
  62. {
  63. }
  64. public void Open(bool is_save_mode)
  65. {
  66. if (this.is_fade_processed)
  67. {
  68. return;
  69. }
  70. UICamera[] array = NGUITools.FindActive<UICamera>();
  71. foreach (UICamera uicamera in array)
  72. {
  73. UIPanel component = uicamera.transform.parent.GetComponent<UIPanel>();
  74. if (0 <= component.name.ToLower().IndexOf("fix"))
  75. {
  76. component.alpha = 0f;
  77. }
  78. }
  79. base.gameObject.SetActive(true);
  80. this.ChangeType((!is_save_mode) ? PhotoModeSaveAndLoad.Type.Load : PhotoModeSaveAndLoad.Type.Save);
  81. this.UpdateDataUnit();
  82. TweenAlpha tweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
  83. tweenAlpha.from = 0f;
  84. tweenAlpha.to = 1f;
  85. tweenAlpha.duration = 0.3f;
  86. tweenAlpha.PlayForward();
  87. EventDelegate.Set(tweenAlpha.onFinished, delegate()
  88. {
  89. TweenAlpha component2 = base.gameObject.GetComponent<TweenAlpha>();
  90. if (component2 != null)
  91. {
  92. UnityEngine.Object.DestroyImmediate(component2);
  93. }
  94. });
  95. }
  96. public void Close()
  97. {
  98. if (this.is_fade_processed)
  99. {
  100. return;
  101. }
  102. UICamera[] array = NGUITools.FindActive<UICamera>();
  103. foreach (UICamera uicamera in array)
  104. {
  105. UIPanel component = uicamera.transform.parent.GetComponent<UIPanel>();
  106. if (0 <= component.name.ToLower().IndexOf("fix"))
  107. {
  108. component.alpha = 1f;
  109. }
  110. }
  111. TweenAlpha tweenAlpha = base.gameObject.AddComponent<TweenAlpha>();
  112. tweenAlpha.from = 1f;
  113. tweenAlpha.to = 0f;
  114. tweenAlpha.duration = 0.3f;
  115. tweenAlpha.PlayForward();
  116. EventDelegate.Set(tweenAlpha.onFinished, delegate()
  117. {
  118. TweenAlpha component2 = base.gameObject.GetComponent<TweenAlpha>();
  119. if (component2 != null)
  120. {
  121. UnityEngine.Object.DestroyImmediate(component2);
  122. }
  123. base.gameObject.SetActive(false);
  124. });
  125. }
  126. public void OnEnable()
  127. {
  128. this.CreateThumbnail();
  129. }
  130. private void Update()
  131. {
  132. if (string.IsNullOrEmpty(this.thum_byte_to_base64_) && File.Exists(this.thum_file_path_))
  133. {
  134. Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  135. texture2D.LoadImage(File.ReadAllBytes(this.thum_file_path_));
  136. float num = (float)texture2D.width / (float)texture2D.height;
  137. Vector2 vector = new Vector2(480f, 270f);
  138. int num2 = texture2D.width;
  139. int num3 = texture2D.height;
  140. if (vector.x < (float)texture2D.width && vector.y < (float)texture2D.height)
  141. {
  142. num2 = (int)vector.x;
  143. num3 = Mathf.RoundToInt((float)num2 / num);
  144. if (vector.y < (float)num3)
  145. {
  146. num3 = (int)vector.y;
  147. num2 = Mathf.RoundToInt((float)num3 * num);
  148. }
  149. }
  150. else if (vector.x < (float)texture2D.width)
  151. {
  152. num2 = (int)vector.x;
  153. num3 = Mathf.RoundToInt((float)num2 / num);
  154. }
  155. else if (vector.y < (float)texture2D.height)
  156. {
  157. num3 = (int)vector.y;
  158. num2 = Mathf.RoundToInt((float)num3 * num);
  159. }
  160. TextureScale.Bilinear(texture2D, num2, num3);
  161. this.thum_byte_to_base64_ = Convert.ToBase64String(texture2D.EncodeToPNG());
  162. UnityEngine.Object.DestroyImmediate(texture2D);
  163. }
  164. }
  165. private void ChangeType(PhotoModeSaveAndLoad.Type type)
  166. {
  167. if (type == PhotoModeSaveAndLoad.Type.Save)
  168. {
  169. UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplaySave", false).SetActive(false);
  170. UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplayLoad", false).SetActive(true);
  171. UTY.GetChildObject(base.gameObject, "TitleGroup/TitleSave", false).SetActive(true);
  172. UTY.GetChildObject(base.gameObject, "TitleGroup/TitleLoad", false).SetActive(false);
  173. }
  174. else
  175. {
  176. UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplaySave", false).SetActive(true);
  177. UTY.GetChildObject(base.gameObject, "DisplayButtonGroup/DisplayLoad", false).SetActive(false);
  178. UTY.GetChildObject(base.gameObject, "TitleGroup/TitleSave", false).SetActive(false);
  179. UTY.GetChildObject(base.gameObject, "TitleGroup/TitleLoad", false).SetActive(true);
  180. }
  181. this.cur_type_ = type;
  182. }
  183. public string GetFileName(int page_no, int unit_no)
  184. {
  185. return string.Concat(new string[]
  186. {
  187. "SaveData_",
  188. page_no.ToString("d2"),
  189. "_",
  190. (unit_no + 1).ToString("d2"),
  191. ".xml"
  192. });
  193. }
  194. public string GetSaveFullPath(int page_no, int unit_no)
  195. {
  196. return Path.Combine(this.folder_path, this.GetFileName(page_no, unit_no));
  197. }
  198. private void CreateThumbnail()
  199. {
  200. this.thum_byte_to_base64_ = string.Empty;
  201. this.thum_file_path_ = Path.Combine(Path.GetTempPath(), "cm3d2_" + Guid.NewGuid().ToString() + ".png");
  202. GameMain.Instance.MainCamera.ScreenShot(this.thum_file_path_, 1, true);
  203. }
  204. private void OnClickDataUnit(PhotSaveLoagDataUnit unit)
  205. {
  206. if (this.is_fade_processed)
  207. {
  208. return;
  209. }
  210. string save_path = this.GetSaveFullPath(this.cur_page_no_, int.Parse(unit.name));
  211. if (UICamera.currentTouchID == -2)
  212. {
  213. if (unit.save_data_object == null)
  214. {
  215. return;
  216. }
  217. if (GameMain.Instance.SysDlg.IsDecided)
  218. {
  219. GameMain.Instance.SysDlg.Show("データを削除しますが宜しいですか?", SystemDialog.TYPE.OK_CANCEL, delegate
  220. {
  221. GameMain.Instance.SysDlg.Close();
  222. this.RemoveSave(save_path);
  223. this.UpdateDataUnit(unit);
  224. }, null);
  225. }
  226. }
  227. else if (UICamera.currentTouchID == -1)
  228. {
  229. if (this.cur_type_ == PhotoModeSaveAndLoad.Type.Save)
  230. {
  231. if (unit.save_data_object != null)
  232. {
  233. if (GameMain.Instance.SysDlg.IsDecided)
  234. {
  235. GameMain.Instance.SysDlg.Show("上書きして保存しますが宜しいですか?", SystemDialog.TYPE.OK_CANCEL, delegate
  236. {
  237. GameMain.Instance.SysDlg.Close();
  238. this.WriteSave(save_path, unit.title, unit.comment);
  239. this.UpdateDataUnit(unit);
  240. }, null);
  241. }
  242. }
  243. else
  244. {
  245. this.WriteSave(save_path, string.Empty, string.Empty);
  246. this.UpdateDataUnit(unit);
  247. }
  248. }
  249. else
  250. {
  251. if (unit.save_data_object == null)
  252. {
  253. return;
  254. }
  255. if (GameMain.Instance.SysDlg.IsDecided)
  256. {
  257. GameMain.Instance.SysDlg.Show("データをロードしますか?", SystemDialog.TYPE.OK_CANCEL, delegate
  258. {
  259. GameMain.Instance.SysDlg.Close();
  260. this.ReadSave(save_path);
  261. this.Close();
  262. }, null);
  263. }
  264. }
  265. }
  266. }
  267. public void WriteSave(string save_full_path, string title, string comment)
  268. {
  269. XDocument xdocument = this.WriteSaveToXdocument(title, comment);
  270. xdocument.Save(save_full_path);
  271. }
  272. public XDocument WriteSaveToXdocument(string title, string comment)
  273. {
  274. XElement xelement = this.CreateCommonData(title, comment);
  275. XElement xelement2 = null;
  276. if (this.onSerializeEvent != null)
  277. {
  278. xelement2 = this.onSerializeEvent();
  279. }
  280. return new XDocument(new XDeclaration("1.0", "utf-8", "true"), new object[]
  281. {
  282. new XComment("CM3D2 PhotModeSaveData"),
  283. new XElement("Data", new object[]
  284. {
  285. xelement2,
  286. xelement
  287. })
  288. });
  289. }
  290. public void ReadSave(string save_full_path)
  291. {
  292. if (!File.Exists(save_full_path))
  293. {
  294. return;
  295. }
  296. this.ReadSave(XDocument.Load(save_full_path));
  297. }
  298. public void ReadSave(byte[] saveBinary)
  299. {
  300. using (MemoryStream memoryStream = new MemoryStream(saveBinary))
  301. {
  302. this.ReadSave(XDocument.Load(memoryStream));
  303. }
  304. }
  305. public void ReadSave(XDocument xml)
  306. {
  307. if (this.onDeserializeEvent != null)
  308. {
  309. this.onDeserializeEvent(xml.Element("Data"));
  310. }
  311. }
  312. private void RemoveSave(string save_full_path)
  313. {
  314. if (!File.Exists(save_full_path))
  315. {
  316. return;
  317. }
  318. try
  319. {
  320. File.Delete(save_full_path);
  321. }
  322. catch (Exception ex)
  323. {
  324. Debug.LogWarning("削除失敗 " + ex.Message);
  325. }
  326. }
  327. private void OnChangeTitle(PhotSaveLoagDataUnit unit)
  328. {
  329. if (unit == null || unit.save_data_object == null)
  330. {
  331. return;
  332. }
  333. XDocument xdocument = unit.save_data_object as XDocument;
  334. XElement xelement = xdocument.Element("Data").Element("Common");
  335. XElement xelement2 = xelement.Element("Title");
  336. if (xelement2 != null && xelement2.Value != UIInput.current.value)
  337. {
  338. xelement2.SetValue(UIInput.current.value);
  339. xdocument.Save(this.GetSaveFullPath(this.cur_page_no_, int.Parse(unit.name)));
  340. }
  341. }
  342. private void OnChangeComment(PhotSaveLoagDataUnit unit)
  343. {
  344. if (unit == null || unit.save_data_object == null)
  345. {
  346. return;
  347. }
  348. XDocument xdocument = unit.save_data_object as XDocument;
  349. XElement xelement = xdocument.Element("Data").Element("Common");
  350. XElement xelement2 = xelement.Element("Comment");
  351. if (xelement2 != null && xelement2.Value != UIInput.current.value)
  352. {
  353. xelement2.SetValue(UIInput.current.value);
  354. xdocument.Save(this.GetSaveFullPath(this.cur_page_no_, int.Parse(unit.name)));
  355. }
  356. }
  357. private void OnClickPageBtn()
  358. {
  359. if (!UIWFSelectButton.current.isSelected)
  360. {
  361. return;
  362. }
  363. this.cur_page_no_ = int.Parse(UIWFSelectButton.current.gameObject.name);
  364. this.UpdateDataUnit();
  365. }
  366. public void OnClickBG()
  367. {
  368. if (this.is_fade_processed)
  369. {
  370. return;
  371. }
  372. if (UICamera.currentTouchID == -2)
  373. {
  374. this.Close();
  375. }
  376. }
  377. private void UpdateDataUnit()
  378. {
  379. if (this.cur_page_no_ < 0)
  380. {
  381. return;
  382. }
  383. for (int i = 0; i < this.data_unit_.Length; i++)
  384. {
  385. this.UpdateDataUnit(this.data_unit_[i]);
  386. }
  387. }
  388. private void UpdateDataUnit(PhotSaveLoagDataUnit unit)
  389. {
  390. string text = Path.Combine(this.folder_path, this.GetFileName(this.cur_page_no_, Array.IndexOf<PhotSaveLoagDataUnit>(this.data_unit_, unit)));
  391. if (File.Exists(text))
  392. {
  393. XDocument xdocument = XDocument.Load(text);
  394. unit.save_data_object = xdocument;
  395. unit.visible_content = true;
  396. XElement xmle = xdocument.Element("Data").Element("Common");
  397. unit.date_text = Xml.GetElement(xmle, "SaveTime", string.Empty);
  398. unit.title = Xml.GetElement(xmle, "Title", string.Empty);
  399. unit.comment = Xml.GetElement(xmle, "Comment", string.Empty);
  400. unit.maid_count = int.Parse(Xml.GetElement(xmle, "MaidCount", "0"));
  401. string element = Xml.GetElement(xmle, "Thumbnail", string.Empty);
  402. if (!string.IsNullOrEmpty(element))
  403. {
  404. unit.thumbnail = Convert.FromBase64String(element);
  405. }
  406. else
  407. {
  408. unit.thumbnail = null;
  409. }
  410. }
  411. else
  412. {
  413. unit.save_data_object = null;
  414. unit.visible_content = false;
  415. }
  416. if (this.cur_type_ == PhotoModeSaveAndLoad.Type.Load && unit.save_data_object == null)
  417. {
  418. unit.enabled_button = false;
  419. }
  420. else
  421. {
  422. unit.enabled_button = true;
  423. }
  424. }
  425. private XElement CreateCommonData(string title, string comment)
  426. {
  427. string value = DateTime.ParseExact(DateTime.Now.ToString("yyyyMMddHHmmss"), "yyyyMMddHHmmss", null).ToString("yyyy.MM.dd HH:mm");
  428. XElement xelement = new XElement("GameVersion", 1200);
  429. XElement xelement2 = new XElement("SaveTime", value);
  430. XElement xelement3 = new XElement("Title", title);
  431. XElement xelement4 = new XElement("Comment", comment);
  432. XElement xelement5 = new XElement("MaidCount", this.PhotoMgr.GetActiveMaidCount().ToString());
  433. XComment xcomment = new XComment("encoding png image at base64.");
  434. XElement xelement6 = new XElement("Thumbnail", this.thum_byte_to_base64_);
  435. XNode[] contents = new XNode[]
  436. {
  437. xelement2,
  438. xelement3,
  439. xelement4,
  440. xelement5,
  441. xcomment,
  442. xelement6
  443. };
  444. return new XElement("Common", contents);
  445. }
  446. private bool is_fade_processed
  447. {
  448. get
  449. {
  450. TweenAlpha component = base.gameObject.GetComponent<TweenAlpha>();
  451. if (component != null && !component.enabled)
  452. {
  453. component.enabled = true;
  454. }
  455. return component != null;
  456. }
  457. }
  458. public PhotoWindowManager PhotoMgr;
  459. public LoadReplaceMaid LoadReplaceMaidPanel;
  460. public UIGrid PageButtonParent;
  461. public int Pages;
  462. public UITable DataUnitParent;
  463. public Func<XElement> onSerializeEvent;
  464. public Action<XElement> onDeserializeEvent;
  465. private PhotSaveLoagDataUnit[] data_unit_;
  466. private int cur_page_no_;
  467. private PhotoModeSaveAndLoad.Type cur_type_;
  468. private string folder_path_;
  469. private string thum_file_path_;
  470. private string thum_byte_to_base64_ = string.Empty;
  471. private enum Type
  472. {
  473. Save,
  474. Load
  475. }
  476. }