EraDataStream.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace NTERA.EmuEra.Game.EraEmu.Sub
  7. {
  8. //難読化用属性。enum.ToString()やenum.Parse()を行うなら(Exclude=true)にすること。
  9. [Obfuscation(Exclude = false)]
  10. internal enum EraDataState
  11. {
  12. OK = 0,//ロード可能
  13. FILENOTFOUND = 1,//ファイルが存在せず
  14. GAME_ERROR = 2,//ゲームが違う
  15. VIRSION_ERROR = 3,//バージョンが違う
  16. ETC_ERROR = 4//その他のエラー
  17. }
  18. internal sealed class EraDataResult
  19. {
  20. public EraDataState State = EraDataState.OK;
  21. public string DataMes = "";
  22. }
  23. /// <summary>
  24. /// セーブデータ読み取り
  25. /// </summary>
  26. internal sealed class EraDataReader : IDisposable
  27. {
  28. //public EraDataReader(string filepath)
  29. //{
  30. // file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
  31. // reader = new StreamReader(file, Config.Encode);
  32. //}
  33. public EraDataReader(FileStream file)
  34. {
  35. this.file = file;
  36. file.Seek(0, SeekOrigin.Begin);
  37. reader = new StreamReader(file, Config.Config.Encode);
  38. }
  39. FileStream file;
  40. StreamReader reader;
  41. public const string FINISHER = "__FINISHED";
  42. public const string EMU_1700_START = "__EMUERA_STRAT__";
  43. public const string EMU_1708_START = "__EMUERA_1708_STRAT__";
  44. public const string EMU_1729_START = "__EMUERA_1729_STRAT__";
  45. public const string EMU_1803_START = "__EMUERA_1803_STRAT__";
  46. public const string EMU_1808_START = "__EMUERA_1808_STRAT__";
  47. public const string EMU_SEPARATOR = "__EMU_SEPARATOR__";
  48. #region eramaker
  49. public string ReadString()
  50. {
  51. if (reader == null)
  52. throw new FileEE("無効なストリームです");
  53. string str = reader.ReadLine();
  54. if (str == null)
  55. throw new FileEE("読み取るべき文字列がありません");
  56. return str;
  57. }
  58. public Int64 ReadInt64()
  59. {
  60. if (reader == null)
  61. throw new FileEE("無効なストリームです");
  62. Int64 ret = 0;
  63. string str = reader.ReadLine();
  64. if (str == null)
  65. throw new FileEE("読み取るべき数値がありません");
  66. if (!Int64.TryParse(str, out ret))
  67. throw new FileEE("数値として認識できません");
  68. return ret;
  69. }
  70. public void ReadInt64Array(Int64[] array)
  71. {
  72. if (reader == null)
  73. throw new FileEE("無効なストリームです");
  74. if (array == null)
  75. throw new FileEE("無効な配列が渡されました");
  76. int i = -1;
  77. string str = null;
  78. Int64 integer = 0;
  79. i = -1;
  80. while (true)
  81. {
  82. i++;
  83. str = reader.ReadLine();
  84. if (str == null)
  85. throw new FileEE("予期しないセーブデータの終端です");
  86. if (str.Equals(FINISHER, StringComparison.Ordinal))
  87. break;
  88. if (i >= array.Length)//配列を超えて保存されていても動じないで読み飛ばす。
  89. continue;
  90. if (!Int64.TryParse(str, out integer))
  91. throw new FileEE("数値として認識できません");
  92. array[i] = integer;
  93. }
  94. for (; i < array.Length; i++)//保存されている値が無いなら0に初期化
  95. array[i] = 0;
  96. }
  97. public void ReadStringArray(string[] array)
  98. {
  99. if (reader == null)
  100. throw new FileEE("無効なストリームです");
  101. if (array == null)
  102. throw new FileEE("無効な配列が渡されました");
  103. int i = -1;
  104. string str = null;
  105. i = -1;
  106. while (true)
  107. {
  108. i++;
  109. str = reader.ReadLine();
  110. if (str == null)
  111. throw new FileEE("予期しないセーブデータの終端です");
  112. if (str.Equals(FINISHER, StringComparison.Ordinal))
  113. break;
  114. if (i >= array.Length)//配列を超えて保存されていても動じないで読み飛ばす。
  115. continue;
  116. array[i] = str;
  117. }
  118. for (; i < array.Length; i++)//保存されている値が無いなら""に初期化
  119. array[i] = "";
  120. }
  121. #endregion
  122. #region Emuera
  123. int emu_version = -1;
  124. public int DataVersion => emu_version;
  125. public bool SeekEmuStart()
  126. {
  127. if (reader == null)
  128. throw new FileEE("無効なストリームです");
  129. if (reader.EndOfStream)
  130. return false;
  131. while (true)
  132. {
  133. string str = reader.ReadLine();
  134. if (str == null)
  135. return false;
  136. if (str.Equals(EMU_1700_START, StringComparison.Ordinal))
  137. {
  138. emu_version = 1700;
  139. return true;
  140. }
  141. if (str.Equals(EMU_1708_START, StringComparison.Ordinal))
  142. {
  143. emu_version = 1708;
  144. return true;
  145. }
  146. if (str.Equals(EMU_1729_START, StringComparison.Ordinal))
  147. {
  148. emu_version = 1729;
  149. return true;
  150. }
  151. if (str.Equals(EMU_1803_START, StringComparison.Ordinal))
  152. {
  153. emu_version = 1803;
  154. return true;
  155. }
  156. if (str.Equals(EMU_1808_START, StringComparison.Ordinal))
  157. {
  158. emu_version = 1808;
  159. return true;
  160. }
  161. }
  162. }
  163. public Dictionary<string, string> ReadStringExtended()
  164. {
  165. if (reader == null)
  166. throw new FileEE("無効なストリームです");
  167. Dictionary<string, string> strList = new Dictionary<string, string>();
  168. string str = null;
  169. while (true)
  170. {
  171. str = reader.ReadLine();
  172. if (str == null)
  173. throw new FileEE("予期しないセーブデータの終端です");
  174. if (str.Equals(FINISHER, StringComparison.Ordinal))
  175. throw new FileEE("セーブデータの形式が不正です");
  176. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  177. break;
  178. int index = str.IndexOf(':');
  179. if (index < 0)
  180. throw new FileEE("セーブデータの形式が不正です");
  181. string key = str.Substring(0, index);
  182. string value = str.Substring(index + 1, str.Length - index - 1);
  183. if (!strList.ContainsKey(key))
  184. strList.Add(key, value);
  185. }
  186. return strList;
  187. }
  188. public Dictionary<string, Int64> ReadInt64Extended()
  189. {
  190. if (reader == null)
  191. throw new FileEE("無効なストリームです");
  192. Dictionary<string, Int64> intList = new Dictionary<string, Int64>();
  193. string str = null;
  194. while (true)
  195. {
  196. str = reader.ReadLine();
  197. if (str == null)
  198. throw new FileEE("予期しないセーブデータの終端です");
  199. if (str.Equals(FINISHER, StringComparison.Ordinal))
  200. throw new FileEE("セーブデータの形式が不正です");
  201. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  202. break;
  203. int index = str.IndexOf(':');
  204. if (index < 0)
  205. throw new FileEE("セーブデータの形式が不正です");
  206. string key = str.Substring(0, index);
  207. string valueStr = str.Substring(index + 1, str.Length - index - 1);
  208. Int64 value = 0;
  209. if (!Int64.TryParse(valueStr, out value))
  210. throw new FileEE("数値として認識できません");
  211. if (!intList.ContainsKey(key))
  212. intList.Add(key, value);
  213. }
  214. return intList;
  215. }
  216. public Dictionary<string, List<Int64>> ReadInt64ArrayExtended()
  217. {
  218. if (reader == null)
  219. throw new FileEE("無効なストリームです");
  220. Dictionary<string, List<Int64>> ret = new Dictionary<string, List<Int64>>();
  221. string str = null;
  222. while (true)
  223. {
  224. str = reader.ReadLine();
  225. if (str == null)
  226. throw new FileEE("予期しないセーブデータの終端です");
  227. if (str.Equals(FINISHER, StringComparison.Ordinal))
  228. throw new FileEE("セーブデータの形式が不正です");
  229. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  230. break;
  231. string key = str;
  232. List<Int64> valueList = new List<Int64>();
  233. while (true)
  234. {
  235. str = reader.ReadLine();
  236. if (str == null)
  237. throw new FileEE("予期しないセーブデータの終端です");
  238. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  239. throw new FileEE("セーブデータの形式が不正です");
  240. if (str.Equals(FINISHER, StringComparison.Ordinal))
  241. break;
  242. Int64 value = 0;
  243. if (!Int64.TryParse(str, out value))
  244. throw new FileEE("数値として認識できません");
  245. valueList.Add(value);
  246. }
  247. if (!ret.ContainsKey(key))
  248. ret.Add(key, valueList);
  249. }
  250. return ret;
  251. }
  252. public Dictionary<string, List<string>> ReadStringArrayExtended()
  253. {
  254. if (reader == null)
  255. throw new FileEE("無効なストリームです");
  256. Dictionary<string, List<string>> ret = new Dictionary<string, List<string>>();
  257. string str = null;
  258. while (true)
  259. {
  260. str = reader.ReadLine();
  261. if (str == null)
  262. throw new FileEE("予期しないセーブデータの終端です");
  263. if (str.Equals(FINISHER, StringComparison.Ordinal))
  264. throw new FileEE("セーブデータの形式が不正です");
  265. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  266. break;
  267. string key = str;
  268. List<string> valueList = new List<string>();
  269. while (true)
  270. {
  271. str = reader.ReadLine();
  272. if (str == null)
  273. throw new FileEE("予期しないセーブデータの終端です");
  274. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  275. throw new FileEE("セーブデータの形式が不正です");
  276. if (str.Equals(FINISHER, StringComparison.Ordinal))
  277. break;
  278. valueList.Add(str);
  279. }
  280. if (!ret.ContainsKey(key))
  281. ret.Add(key, valueList);
  282. }
  283. return ret;
  284. }
  285. public Dictionary<string, List<Int64[]>> ReadInt64Array2DExtended()
  286. {
  287. if (reader == null)
  288. throw new FileEE("無効なストリームです");
  289. Dictionary<string, List<Int64[]>> ret = new Dictionary<string, List<Int64[]>>();
  290. if (emu_version < 1708)
  291. return ret;
  292. string str = null;
  293. while (true)
  294. {
  295. str = reader.ReadLine();
  296. if (str == null)
  297. throw new FileEE("予期しないセーブデータの終端です");
  298. if (str.Equals(FINISHER, StringComparison.Ordinal))
  299. throw new FileEE("セーブデータの形式が不正です");
  300. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  301. break;
  302. string key = str;
  303. List<Int64[]> valueList = new List<Int64[]>();
  304. while (true)
  305. {
  306. str = reader.ReadLine();
  307. if (str == null)
  308. throw new FileEE("予期しないセーブデータの終端です");
  309. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  310. throw new FileEE("セーブデータの形式が不正です");
  311. if (str.Equals(FINISHER, StringComparison.Ordinal))
  312. break;
  313. if (str.Length == 0)
  314. {
  315. valueList.Add(new Int64[0]);
  316. continue;
  317. }
  318. string[] tokens = str.Split(',');
  319. Int64[] intTokens = new Int64[tokens.Length];
  320. for (int x = 0; x < tokens.Length; x++)
  321. if (!Int64.TryParse(tokens[x], out intTokens[x]))
  322. throw new FileEE(tokens[x] + "は数値として認識できません");
  323. valueList.Add(intTokens);
  324. }
  325. if (!ret.ContainsKey(key))
  326. ret.Add(key, valueList);
  327. }
  328. return ret;
  329. }
  330. public Dictionary<string, List<string[]>> ReadStringArray2DExtended()
  331. {
  332. if (reader == null)
  333. throw new FileEE("無効なストリームです");
  334. Dictionary<string, List<string[]>> ret = new Dictionary<string, List<string[]>>();
  335. if (emu_version < 1708)
  336. return ret;
  337. string str = null;
  338. while (true)
  339. {
  340. str = reader.ReadLine();
  341. if (str == null)
  342. throw new FileEE("予期しないセーブデータの終端です");
  343. if (str.Equals(FINISHER, StringComparison.Ordinal))
  344. throw new FileEE("セーブデータの形式が不正です");
  345. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  346. break;
  347. throw new FileEE("StringArray2Dのロードには対応していません");
  348. }
  349. return ret;
  350. }
  351. public Dictionary<string, List<List<Int64[]>>> ReadInt64Array3DExtended()
  352. {
  353. if (reader == null)
  354. throw new FileEE("無効なストリームです");
  355. Dictionary<string, List<List<Int64[]>>> ret = new Dictionary<string, List<List<Int64[]>>>();
  356. if (emu_version < 1729)
  357. return ret;
  358. string str = null;
  359. while (true)
  360. {
  361. str = reader.ReadLine();
  362. if (str == null)
  363. throw new FileEE("予期しないセーブデータの終端です");
  364. if (str.Equals(FINISHER, StringComparison.Ordinal))
  365. throw new FileEE("セーブデータの形式が不正です");
  366. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  367. break;
  368. string key = str;
  369. List<List<Int64[]>> valueList = new List<List<Int64[]>>();
  370. while (true)
  371. {
  372. str = reader.ReadLine();
  373. if (str == null)
  374. throw new FileEE("予期しないセーブデータの終端です");
  375. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  376. throw new FileEE("セーブデータの形式が不正です");
  377. if (str.Equals(FINISHER, StringComparison.Ordinal))
  378. break;
  379. if (str.Contains("{"))
  380. {
  381. List<Int64[]> tokenList = new List<long[]>();
  382. while (true)
  383. {
  384. str = reader.ReadLine();
  385. if (str == "}")
  386. break;
  387. if (str.Length == 0)
  388. {
  389. tokenList.Add(new Int64[0]);
  390. continue;
  391. }
  392. string[] tokens = str.Split(',');
  393. Int64[] intTokens = new Int64[tokens.Length];
  394. for (int x = 0; x < tokens.Length; x++)
  395. if (!Int64.TryParse(tokens[x], out intTokens[x]))
  396. throw new FileEE(tokens[x] + "は数値として認識できません");
  397. tokenList.Add(intTokens);
  398. }
  399. valueList.Add(tokenList);
  400. }
  401. }
  402. if (!ret.ContainsKey(key))
  403. ret.Add(key, valueList);
  404. }
  405. return ret;
  406. }
  407. public Dictionary<string, List<List<string[]>>> ReadStringArray3DExtended()
  408. {
  409. if (reader == null)
  410. throw new FileEE("無効なストリームです");
  411. Dictionary<string, List<List<string[]>>> ret = new Dictionary<string, List<List<string[]>>>();
  412. if (emu_version < 1729)
  413. return ret;
  414. string str = null;
  415. while (true)
  416. {
  417. str = reader.ReadLine();
  418. if (str == null)
  419. throw new FileEE("予期しないセーブデータの終端です");
  420. if (str.Equals(FINISHER, StringComparison.Ordinal))
  421. throw new FileEE("セーブデータの形式が不正です");
  422. if (str.Equals(EMU_SEPARATOR, StringComparison.Ordinal))
  423. break;
  424. throw new FileEE("StringArray2Dのロードには対応していません");
  425. }
  426. return ret;
  427. }
  428. #endregion
  429. #region IDisposable メンバ
  430. public void Dispose()
  431. {
  432. if (reader != null)
  433. reader.Close();
  434. else if (file != null)
  435. file.Close();
  436. file = null;
  437. reader = null;
  438. }
  439. #endregion
  440. public void Close()
  441. {
  442. Dispose();
  443. }
  444. }
  445. /// <summary>
  446. /// セーブデータ書き込み
  447. /// </summary>
  448. internal sealed class EraDataWriter : IDisposable
  449. {
  450. //public EraDataWriter(string filepath)
  451. //{
  452. // FileStream file = new FileStream(filepath, FileMode.Create, FileAccess.Write);
  453. // writer = new StreamWriter(file, Config.SaveEncode);
  454. // //writer = new StreamWriter(filepath, false, Config.SaveEncode);
  455. //}
  456. public EraDataWriter(FileStream file)
  457. {
  458. this.file = file;
  459. writer = new StreamWriter(file, Config.Config.SaveEncode);
  460. }
  461. public const string FINISHER = EraDataReader.FINISHER;
  462. public const string EMU_START = EraDataReader.EMU_1808_START;
  463. public const string EMU_SEPARATOR = EraDataReader.EMU_SEPARATOR;
  464. FileStream file;
  465. StreamWriter writer;
  466. #region eramaker
  467. public void Write(Int64 integer)
  468. {
  469. if (writer == null)
  470. throw new FileEE("無効なストリームです");
  471. writer.WriteLine(integer.ToString());
  472. }
  473. public void Write(string str)
  474. {
  475. if (writer == null)
  476. throw new FileEE("無効なストリームです");
  477. if (str == null)
  478. writer.WriteLine("");
  479. else
  480. writer.WriteLine(str);
  481. }
  482. public void Write(Int64[] array)
  483. {
  484. if (writer == null)
  485. throw new FileEE("無効なストリームです");
  486. if (array == null)
  487. throw new FileEE("無効な配列が渡されました");
  488. int count = -1;
  489. for (int i = 0; i < array.Length; i++)
  490. if (array[i] != 0)
  491. count = i;
  492. count++;
  493. for (int i = 0; i < count; i++)
  494. writer.WriteLine(array[i].ToString());
  495. writer.WriteLine(FINISHER);
  496. }
  497. public void Write(string[] array)
  498. {
  499. if (writer == null)
  500. throw new FileEE("無効なストリームです");
  501. if (array == null)
  502. throw new FileEE("無効な配列が渡されました");
  503. int count = -1;
  504. for (int i = 0; i < array.Length; i++)
  505. if (!string.IsNullOrEmpty(array[i]))
  506. count = i;
  507. count++;
  508. for (int i = 0; i < count; i++)
  509. {
  510. if (array[i] == null)
  511. writer.WriteLine("");
  512. else
  513. writer.WriteLine(array[i]);
  514. }
  515. writer.WriteLine(FINISHER);
  516. }
  517. #endregion
  518. #region Emuera
  519. public void EmuStart()
  520. {
  521. if (writer == null)
  522. throw new FileEE("無効なストリームです");
  523. writer.WriteLine(EMU_START);
  524. }
  525. public void EmuSeparete()
  526. {
  527. if (writer == null)
  528. throw new FileEE("無効なストリームです");
  529. writer.WriteLine(EMU_SEPARATOR);
  530. }
  531. public void WriteExtended(string key, Int64 value)
  532. {
  533. if (writer == null)
  534. throw new FileEE("無効なストリームです");
  535. if (value == 0)
  536. return;
  537. writer.WriteLine("{0}:{1}", key, value);
  538. }
  539. public void WriteExtended(string key, string value)
  540. {
  541. if (writer == null)
  542. throw new FileEE("無効なストリームです");
  543. if (string.IsNullOrEmpty(value))
  544. return;
  545. writer.WriteLine("{0}:{1}", key, value);
  546. }
  547. public void WriteExtended(string key, Int64[] array)
  548. {
  549. if (writer == null)
  550. throw new FileEE("無効なストリームです");
  551. if (array == null)
  552. throw new FileEE("無効な配列が渡されました");
  553. int count = -1;
  554. for (int i = 0; i < array.Length; i++)
  555. if (array[i] != 0)
  556. count = i;
  557. count++;
  558. if (count == 0)
  559. return;
  560. writer.WriteLine(key);
  561. for (int i = 0; i < count; i++)
  562. writer.WriteLine(array[i].ToString());
  563. writer.WriteLine(FINISHER);
  564. }
  565. public void WriteExtended(string key, string[] array)
  566. {
  567. if (writer == null)
  568. throw new FileEE("無効なストリームです");
  569. if (array == null)
  570. throw new FileEE("無効な配列が渡されました");
  571. int count = -1;
  572. for (int i = 0; i < array.Length; i++)
  573. if (!string.IsNullOrEmpty(array[i]))
  574. count = i;
  575. count++;
  576. if (count == 0)
  577. return;
  578. writer.WriteLine(key);
  579. for (int i = 0; i < count; i++)
  580. {
  581. if (array[i] == null)
  582. writer.WriteLine("");
  583. else
  584. writer.WriteLine(array[i]);
  585. }
  586. writer.WriteLine(FINISHER);
  587. }
  588. public void WriteExtended(string key, Int64[,] array2D)
  589. {
  590. if (writer == null)
  591. throw new FileEE("無効なストリームです");
  592. if (array2D == null)
  593. throw new FileEE("無効な配列が渡されました");
  594. int countX = 0;
  595. int length0 = array2D.GetLength(0);
  596. int length1 = array2D.GetLength(1);
  597. int[] countY = new int[length0];
  598. for (int x = 0; x < length0; x++)
  599. {
  600. for (int y = 0; y < length1; y++)
  601. {
  602. if (array2D[x, y] != 0)
  603. {
  604. countX = x + 1;
  605. countY[x] = y + 1;
  606. }
  607. }
  608. }
  609. if (countX == 0)
  610. return;
  611. writer.WriteLine(key);
  612. for (int x = 0; x < countX; x++)
  613. {
  614. if (countY[x] == 0)
  615. {
  616. writer.WriteLine("");
  617. continue;
  618. }
  619. StringBuilder builder = new StringBuilder("");
  620. for (int y = 0; y < countY[x]; y++)
  621. {
  622. builder.Append(array2D[x, y].ToString());
  623. if (y != countY[x] - 1)
  624. builder.Append(",");
  625. }
  626. writer.WriteLine(builder.ToString());
  627. }
  628. writer.WriteLine(FINISHER);
  629. }
  630. public void WriteExtended(string key, string[,] array2D)
  631. {
  632. throw new NotImplementedException("まだ実装してないよ");
  633. }
  634. public void WriteExtended(string key, Int64[, ,] array3D)
  635. {
  636. if (writer == null)
  637. throw new FileEE("無効なストリームです");
  638. if (array3D == null)
  639. throw new FileEE("無効な配列が渡されました");
  640. int countX = 0;
  641. int length0 = array3D.GetLength(0);
  642. int length1 = array3D.GetLength(1);
  643. int length2 = array3D.GetLength(2);
  644. int[] countY = new int[length0];
  645. int[,] countZ = new int[length0, length1];
  646. for (int x = 0; x < length0; x++)
  647. {
  648. for (int y = 0; y < length1; y++)
  649. {
  650. for (int z = 0; z < length2; z++)
  651. {
  652. if (array3D[x, y, z] != 0)
  653. {
  654. countX = x + 1;
  655. countY[x] = y + 1;
  656. countZ[x, y] = z + 1;
  657. }
  658. }
  659. }
  660. }
  661. if (countX == 0)
  662. return;
  663. writer.WriteLine(key);
  664. for (int x = 0; x < countX; x++)
  665. {
  666. writer.WriteLine(x + "{");
  667. if (countY[x] == 0)
  668. {
  669. writer.WriteLine("}");
  670. continue;
  671. }
  672. for (int y = 0; y < countY[x]; y++)
  673. {
  674. StringBuilder builder = new StringBuilder("");
  675. if (countZ[x, y] == 0)
  676. {
  677. writer.WriteLine("");
  678. continue;
  679. }
  680. for (int z = 0; z < countZ[x, y]; z++)
  681. {
  682. builder.Append(array3D[x, y, z].ToString());
  683. if (z != countZ[x, y] - 1)
  684. builder.Append(",");
  685. }
  686. writer.WriteLine(builder.ToString());
  687. }
  688. writer.WriteLine("}");
  689. }
  690. writer.WriteLine(FINISHER);
  691. }
  692. public void WriteExtended(string key, string[, ,] array2D)
  693. {
  694. throw new NotImplementedException("まだ実装してないよ");
  695. }
  696. #endregion
  697. #region IDisposable メンバ
  698. public void Dispose()
  699. {
  700. if (writer != null)
  701. writer.Close();
  702. else if (file != null)
  703. file.Close();
  704. writer = null;
  705. file = null;
  706. }
  707. #endregion
  708. public void Close()
  709. {
  710. Dispose();
  711. }
  712. }
  713. }