EraBinaryDataWriter.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace NTERA.EmuEra.Game.EraEmu.Sub
  5. {
  6. //reader/writer共通のデータはreaderの方に
  7. /// <summary>
  8. /// 1808追加 新しいデータ保存形式
  9. /// Reader と違ってWriterは最新の書き込み方式だけ知っていればよい
  10. /// WriteHeader -> WriteFileType -> ... -> WriteEFO
  11. /// </summary>
  12. internal sealed class EraBinaryDataWriter : IDisposable
  13. {
  14. public EraBinaryDataWriter(FileStream fs)
  15. {
  16. writer = new BinaryWriter(fs, Encoding.Unicode);
  17. }
  18. BinaryWriter writer;
  19. public void WriteHeader()
  20. {
  21. writer.Write(EraBDConst.Header);
  22. writer.Write(EraBDConst.Version1808);
  23. writer.Write(EraBDConst.DataCount);
  24. for (int i = 0; i < EraBDConst.DataCount; i++)
  25. {
  26. writer.Write((UInt32)0);
  27. }
  28. }
  29. public void WriteFileType(EraSaveFileType type)
  30. {
  31. writer.Write((byte)type);
  32. }
  33. /// <summary>
  34. /// システム用。keyなしでInt64を保存
  35. /// </summary>
  36. /// <param name="v"></param>
  37. public void WriteInt64(Int64 v)
  38. {
  39. //圧縮しない
  40. writer.Write(v);
  41. }
  42. /// <summary>
  43. /// システム用。keyなしでstringを保存
  44. /// </summary>
  45. /// <param name="s"></param>
  46. public void WriteString(string s)
  47. {
  48. writer.Write(s);
  49. }
  50. public void WriteSeparator()
  51. {
  52. writer.Write((byte)EraSaveDataType.Separator);
  53. }
  54. public void WriteEOC()
  55. {
  56. writer.Write((byte)EraSaveDataType.EOC);
  57. }
  58. public void WriteEOF()
  59. {
  60. writer.Write((byte)EraSaveDataType.EOF);
  61. }
  62. public void WriteWithKey(string key, object v)
  63. {
  64. if (v is Int64)
  65. {
  66. writer.Write((byte)EraSaveDataType.Int);
  67. writer.Write(key);
  68. writeData((Int64)v);
  69. }
  70. else if (v is Int64[])
  71. {
  72. writer.Write((byte)EraSaveDataType.IntArray);
  73. writer.Write(key);
  74. writeData((Int64[])v);
  75. }
  76. else if (v is Int64[,])
  77. {
  78. writer.Write((byte)EraSaveDataType.IntArray2D);
  79. writer.Write(key);
  80. writeData((Int64[,])v);
  81. }
  82. else if (v is Int64[, ,])
  83. {
  84. writer.Write((byte)EraSaveDataType.IntArray3D);
  85. writer.Write(key);
  86. writeData((Int64[, ,])v);
  87. }
  88. else if (v is string)
  89. {
  90. writer.Write((byte)EraSaveDataType.Str);
  91. writer.Write(key);
  92. writeData((string)v);
  93. }
  94. else if (v is string[])
  95. {
  96. writer.Write((byte)EraSaveDataType.StrArray);
  97. writer.Write(key);
  98. writeData((string[])v);
  99. }
  100. else if (v is string[,])
  101. {
  102. writer.Write((byte)EraSaveDataType.StrArray2D);
  103. writer.Write(key);
  104. writeData((string[,])v);
  105. }
  106. else if (v is string[, ,])
  107. {
  108. writer.Write((byte)EraSaveDataType.StrArray3D);
  109. writer.Write(key);
  110. writeData((string[, ,])v);
  111. }
  112. }
  113. #region private
  114. private void m_WriteInt(Int64 v)
  115. {
  116. //セーブデータ容量の爆発を避けるためにできるだけWrite(Int64)はしない
  117. if (v >= 0 && v <= Ebdb.Byte)//0~207まではそのままbyteに詰め込む
  118. writer.Write((byte)v);
  119. else if (v >= Int16.MinValue && v <= Int16.MaxValue)//整数の範囲に応じて適当に
  120. {
  121. writer.Write(Ebdb.Int16);
  122. writer.Write((Int16)v);
  123. }
  124. else if (v >= Int32.MinValue && v <= Int32.MaxValue)
  125. {
  126. writer.Write(Ebdb.Int32);
  127. writer.Write((Int32)v);
  128. }
  129. else
  130. {
  131. writer.Write(Ebdb.Int64);
  132. writer.Write(v);
  133. }
  134. }
  135. private void writeData(Int64 v)
  136. {
  137. m_WriteInt(v);
  138. }
  139. private void writeData(Int64[] array)
  140. {
  141. //配列の記憶。0が連続する場合には圧縮を試みる。
  142. writer.Write(array.Length);
  143. int countZero = 0;//0については0が連続する数を記憶する。その他の数はそのまま記憶する。
  144. for(int x = 0; x < array.Length; x++)
  145. {
  146. if (array[x] == 0)
  147. countZero++;
  148. else
  149. {
  150. if (countZero > 0)
  151. {
  152. writer.Write(Ebdb.Zero);
  153. m_WriteInt(countZero);
  154. countZero = 0;
  155. }
  156. m_WriteInt(array[x]);
  157. }
  158. }
  159. //記憶途中で配列の残りが全部0であるなら0の数も記憶せず配列の終わりを記憶
  160. writer.Write(Ebdb.EoD);
  161. }
  162. private void writeData(Int64[,] array)
  163. {
  164. int countZero = 0;//0については0が連続する数を記憶する。その他はそのまま記憶する。
  165. int countAllZero = 0;//列の要素が全て0である列の連続する数を記憶する。列の要素に一つでも非0があるなら通常の記憶方式。
  166. int length0 = array.GetLength(0);
  167. int length1 = array.GetLength(1);
  168. writer.Write(length0);
  169. writer.Write(length1);
  170. for(int x = 0; x < length0; x++)
  171. {
  172. for(int y = 0; y < length1; y++)
  173. {
  174. if (array[x,y] == 0)
  175. countZero++;
  176. else
  177. {
  178. if (countAllZero > 0)
  179. {
  180. writer.Write(Ebdb.ZeroA1);
  181. m_WriteInt(countAllZero);
  182. countAllZero = 0;
  183. }
  184. if (countZero > 0)
  185. {
  186. writer.Write(Ebdb.Zero);
  187. m_WriteInt(countZero);
  188. countZero = 0;
  189. }
  190. m_WriteInt(array[x,y]);
  191. }
  192. }
  193. if (countZero == length1)//列の要素が全部0
  194. countAllZero++;
  195. else
  196. writer.Write(Ebdb.EoA1);//非0があるなら列終端記号を記憶
  197. countZero = 0;
  198. }
  199. writer.Write(Ebdb.EoD);
  200. }
  201. private void writeData(Int64[, ,] array)
  202. {
  203. int countZero = 0;//0については0が連続する数を記憶する。その他はそのまま記憶する。
  204. int countAllZero = 0;//列の要素が全て0である列の連続する数を記憶する。列の要素に一つでも非0があるなら通常の記憶方式。
  205. int countAllZero2D = 0;//行列の要素が全て0である行列の・・・
  206. int length0 = array.GetLength(0);
  207. int length1 = array.GetLength(1);
  208. int length2 = array.GetLength(2);
  209. writer.Write(length0);
  210. writer.Write(length1);
  211. writer.Write(length2);
  212. for(int x = 0; x < length0; x++)
  213. {
  214. for(int y = 0; y < length1; y++)
  215. {
  216. for(int z = 0; z < length2; z++)
  217. {
  218. if (array[x,y,z] == 0)
  219. countZero++;
  220. else
  221. {
  222. if (countAllZero2D > 0)
  223. {
  224. writer.Write(Ebdb.ZeroA2);
  225. m_WriteInt(countAllZero2D);
  226. countAllZero2D = 0;
  227. }
  228. if (countAllZero > 0)
  229. {
  230. writer.Write(Ebdb.ZeroA1);
  231. m_WriteInt(countAllZero);
  232. countAllZero = 0;
  233. }
  234. if (countZero > 0)
  235. {
  236. writer.Write(Ebdb.Zero);
  237. m_WriteInt(countZero);
  238. countZero = 0;
  239. }
  240. m_WriteInt(array[x,y,z]);
  241. }
  242. }
  243. if (countZero == length2)
  244. countAllZero++;
  245. else
  246. writer.Write(Ebdb.EoA1);
  247. countZero = 0;
  248. }
  249. if (countAllZero == length1)
  250. countAllZero2D++;
  251. else
  252. writer.Write(Ebdb.EoA2);
  253. countAllZero = 0;
  254. }
  255. writer.Write(Ebdb.EoD);
  256. }
  257. private void writeData(string v)
  258. {
  259. if (v != null)
  260. writer.Write(v);
  261. else
  262. writer.Write("");
  263. }
  264. private void writeData(string[] array)
  265. {
  266. int countZero = 0;
  267. writer.Write(array.Length);
  268. for(int x = 0; x < array.Length; x++)
  269. {
  270. if (array[x] == null || array[x].Length == 0)
  271. countZero++;
  272. else
  273. {
  274. if (countZero > 0)
  275. {
  276. writer.Write(Ebdb.Zero);
  277. m_WriteInt(countZero);
  278. countZero = 0;
  279. }
  280. writer.Write(Ebdb.String);
  281. writer.Write(array[x]);
  282. }
  283. }
  284. writer.Write(Ebdb.EoD);
  285. }
  286. private void writeData(string[,] array)
  287. {
  288. int countZero = 0;
  289. int countAllZero = 0;
  290. int length0 = array.GetLength(0);
  291. int length1 = array.GetLength(1);
  292. writer.Write(length0);
  293. writer.Write(length1);
  294. for(int x = 0; x < length0; x++)
  295. {
  296. for(int y = 0; y < length1; y++)
  297. {
  298. if (array[x,y] == null || array[x,y].Length == 0)
  299. countZero++;
  300. else
  301. {
  302. if (countAllZero > 0)
  303. {
  304. writer.Write(Ebdb.ZeroA1);
  305. m_WriteInt(countAllZero);
  306. countAllZero = 0;
  307. }
  308. if (countZero > 0)
  309. {
  310. writer.Write(Ebdb.Zero);
  311. m_WriteInt(countZero);
  312. countZero = 0;
  313. }
  314. writer.Write(Ebdb.String);
  315. writer.Write(array[x,y]);
  316. }
  317. }
  318. if (countZero == length1)
  319. countAllZero++;
  320. else
  321. writer.Write(Ebdb.EoA1);
  322. countZero = 0;
  323. }
  324. writer.Write(Ebdb.EoD);
  325. }
  326. private void writeData(string[, ,] array)
  327. {
  328. int countZero = 0;
  329. int countAllZero = 0;
  330. int countAllZero2D = 0;
  331. int length0 = array.GetLength(0);
  332. int length1 = array.GetLength(1);
  333. int length2 = array.GetLength(2);
  334. writer.Write(length0);
  335. writer.Write(length1);
  336. writer.Write(length2);
  337. for(int x = 0; x < length0; x++)
  338. {
  339. for(int y = 0; y < length1; y++)
  340. {
  341. for(int z = 0; z < length2; z++)
  342. {
  343. if (array[x,y,z] == null || array[x,y,z].Length == 0)
  344. countZero++;
  345. else
  346. {
  347. if (countAllZero2D > 0)
  348. {
  349. writer.Write(Ebdb.ZeroA2);
  350. m_WriteInt(countAllZero2D);
  351. countAllZero2D = 0;
  352. }
  353. if (countAllZero > 0)
  354. {
  355. writer.Write(Ebdb.ZeroA1);
  356. m_WriteInt(countAllZero);
  357. countAllZero = 0;
  358. }
  359. if (countZero > 0)
  360. {
  361. writer.Write(Ebdb.Zero);
  362. m_WriteInt(countZero);
  363. countZero = 0;
  364. }
  365. writer.Write(Ebdb.String);
  366. writer.Write(array[x,y,z]);
  367. }
  368. }
  369. if (countZero == length2)
  370. countAllZero++;
  371. else
  372. writer.Write(Ebdb.EoA1);
  373. countZero = 0;
  374. }
  375. if (countAllZero == length1)
  376. countAllZero2D++;
  377. else
  378. writer.Write(Ebdb.EoA2);
  379. countAllZero = 0;
  380. }
  381. writer.Write(Ebdb.EoD);
  382. }
  383. #endregion
  384. #region IDisposable メンバ
  385. public void Dispose()
  386. {
  387. if (writer != null)
  388. writer.Close();
  389. writer = null;
  390. }
  391. #endregion
  392. public void Close()
  393. {
  394. Dispose();
  395. }
  396. }
  397. }