123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- using System;
- using System.IO;
- using System.Text;
- namespace NTERA.EmuEra.Game.EraEmu.Sub
- {
- //reader/writer共通のデータはreaderの方に
- /// <summary>
- /// 1808追加 新しいデータ保存形式
- /// Reader と違ってWriterは最新の書き込み方式だけ知っていればよい
- /// WriteHeader -> WriteFileType -> ... -> WriteEFO
- /// </summary>
- internal sealed class EraBinaryDataWriter : IDisposable
- {
- public EraBinaryDataWriter(FileStream fs)
- {
- writer = new BinaryWriter(fs, Encoding.Unicode);
- }
- BinaryWriter writer;
-
- public void WriteHeader()
- {
- writer.Write(EraBDConst.Header);
- writer.Write(EraBDConst.Version1808);
- writer.Write(EraBDConst.DataCount);
- for (int i = 0; i < EraBDConst.DataCount; i++)
- {
- writer.Write((UInt32)0);
- }
- }
- public void WriteFileType(EraSaveFileType type)
- {
- writer.Write((byte)type);
- }
- /// <summary>
- /// システム用。keyなしでInt64を保存
- /// </summary>
- /// <param name="v"></param>
- public void WriteInt64(Int64 v)
- {
- //圧縮しない
- writer.Write(v);
- }
- /// <summary>
- /// システム用。keyなしでstringを保存
- /// </summary>
- /// <param name="s"></param>
- public void WriteString(string s)
- {
- writer.Write(s);
- }
- public void WriteSeparator()
- {
- writer.Write((byte)EraSaveDataType.Separator);
- }
- public void WriteEOC()
- {
- writer.Write((byte)EraSaveDataType.EOC);
- }
- public void WriteEOF()
- {
- writer.Write((byte)EraSaveDataType.EOF);
- }
- public void WriteWithKey(string key, object v)
- {
- if (v is Int64)
- {
- writer.Write((byte)EraSaveDataType.Int);
- writer.Write(key);
- writeData((Int64)v);
- }
- else if (v is Int64[])
- {
- writer.Write((byte)EraSaveDataType.IntArray);
- writer.Write(key);
- writeData((Int64[])v);
- }
- else if (v is Int64[,])
- {
- writer.Write((byte)EraSaveDataType.IntArray2D);
- writer.Write(key);
- writeData((Int64[,])v);
- }
- else if (v is Int64[, ,])
- {
- writer.Write((byte)EraSaveDataType.IntArray3D);
- writer.Write(key);
- writeData((Int64[, ,])v);
- }
- else if (v is string)
- {
- writer.Write((byte)EraSaveDataType.Str);
- writer.Write(key);
- writeData((string)v);
- }
- else if (v is string[])
- {
- writer.Write((byte)EraSaveDataType.StrArray);
- writer.Write(key);
- writeData((string[])v);
- }
- else if (v is string[,])
- {
- writer.Write((byte)EraSaveDataType.StrArray2D);
- writer.Write(key);
- writeData((string[,])v);
- }
- else if (v is string[, ,])
- {
- writer.Write((byte)EraSaveDataType.StrArray3D);
- writer.Write(key);
- writeData((string[, ,])v);
- }
- }
- #region private
- private void m_WriteInt(Int64 v)
- {
- //セーブデータ容量の爆発を避けるためにできるだけWrite(Int64)はしない
- if (v >= 0 && v <= Ebdb.Byte)//0~207まではそのままbyteに詰め込む
- writer.Write((byte)v);
- else if (v >= Int16.MinValue && v <= Int16.MaxValue)//整数の範囲に応じて適当に
- {
- writer.Write(Ebdb.Int16);
- writer.Write((Int16)v);
- }
- else if (v >= Int32.MinValue && v <= Int32.MaxValue)
- {
- writer.Write(Ebdb.Int32);
- writer.Write((Int32)v);
- }
- else
- {
- writer.Write(Ebdb.Int64);
- writer.Write(v);
- }
- }
- private void writeData(Int64 v)
- {
- m_WriteInt(v);
- }
- private void writeData(Int64[] array)
- {
- //配列の記憶。0が連続する場合には圧縮を試みる。
- writer.Write(array.Length);
- int countZero = 0;//0については0が連続する数を記憶する。その他の数はそのまま記憶する。
- for(int x = 0; x < array.Length; x++)
- {
- if (array[x] == 0)
- countZero++;
- else
- {
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- m_WriteInt(array[x]);
- }
- }
- //記憶途中で配列の残りが全部0であるなら0の数も記憶せず配列の終わりを記憶
- writer.Write(Ebdb.EoD);
- }
- private void writeData(Int64[,] array)
- {
- int countZero = 0;//0については0が連続する数を記憶する。その他はそのまま記憶する。
- int countAllZero = 0;//列の要素が全て0である列の連続する数を記憶する。列の要素に一つでも非0があるなら通常の記憶方式。
- int length0 = array.GetLength(0);
- int length1 = array.GetLength(1);
- writer.Write(length0);
- writer.Write(length1);
-
- for(int x = 0; x < length0; x++)
- {
- for(int y = 0; y < length1; y++)
- {
- if (array[x,y] == 0)
- countZero++;
- else
- {
- if (countAllZero > 0)
- {
- writer.Write(Ebdb.ZeroA1);
- m_WriteInt(countAllZero);
- countAllZero = 0;
- }
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- m_WriteInt(array[x,y]);
- }
- }
- if (countZero == length1)//列の要素が全部0
- countAllZero++;
- else
- writer.Write(Ebdb.EoA1);//非0があるなら列終端記号を記憶
- countZero = 0;
- }
- writer.Write(Ebdb.EoD);
- }
- private void writeData(Int64[, ,] array)
- {
- int countZero = 0;//0については0が連続する数を記憶する。その他はそのまま記憶する。
- int countAllZero = 0;//列の要素が全て0である列の連続する数を記憶する。列の要素に一つでも非0があるなら通常の記憶方式。
- int countAllZero2D = 0;//行列の要素が全て0である行列の・・・
- int length0 = array.GetLength(0);
- int length1 = array.GetLength(1);
- int length2 = array.GetLength(2);
- writer.Write(length0);
- writer.Write(length1);
- writer.Write(length2);
- for(int x = 0; x < length0; x++)
- {
- for(int y = 0; y < length1; y++)
- {
- for(int z = 0; z < length2; z++)
- {
- if (array[x,y,z] == 0)
- countZero++;
- else
- {
- if (countAllZero2D > 0)
- {
- writer.Write(Ebdb.ZeroA2);
- m_WriteInt(countAllZero2D);
- countAllZero2D = 0;
- }
- if (countAllZero > 0)
- {
- writer.Write(Ebdb.ZeroA1);
- m_WriteInt(countAllZero);
- countAllZero = 0;
- }
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- m_WriteInt(array[x,y,z]);
- }
- }
- if (countZero == length2)
- countAllZero++;
- else
- writer.Write(Ebdb.EoA1);
- countZero = 0;
- }
- if (countAllZero == length1)
- countAllZero2D++;
- else
- writer.Write(Ebdb.EoA2);
- countAllZero = 0;
- }
- writer.Write(Ebdb.EoD);
- }
- private void writeData(string v)
- {
- if (v != null)
- writer.Write(v);
- else
- writer.Write("");
- }
- private void writeData(string[] array)
- {
- int countZero = 0;
- writer.Write(array.Length);
- for(int x = 0; x < array.Length; x++)
- {
- if (array[x] == null || array[x].Length == 0)
- countZero++;
- else
- {
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- writer.Write(Ebdb.String);
- writer.Write(array[x]);
- }
- }
- writer.Write(Ebdb.EoD);
- }
- private void writeData(string[,] array)
- {
- int countZero = 0;
- int countAllZero = 0;
- int length0 = array.GetLength(0);
- int length1 = array.GetLength(1);
- writer.Write(length0);
- writer.Write(length1);
- for(int x = 0; x < length0; x++)
- {
- for(int y = 0; y < length1; y++)
- {
- if (array[x,y] == null || array[x,y].Length == 0)
- countZero++;
- else
- {
- if (countAllZero > 0)
- {
- writer.Write(Ebdb.ZeroA1);
- m_WriteInt(countAllZero);
- countAllZero = 0;
- }
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- writer.Write(Ebdb.String);
- writer.Write(array[x,y]);
- }
- }
- if (countZero == length1)
- countAllZero++;
- else
- writer.Write(Ebdb.EoA1);
- countZero = 0;
- }
- writer.Write(Ebdb.EoD);
- }
- private void writeData(string[, ,] array)
- {
- int countZero = 0;
- int countAllZero = 0;
- int countAllZero2D = 0;
- int length0 = array.GetLength(0);
- int length1 = array.GetLength(1);
- int length2 = array.GetLength(2);
- writer.Write(length0);
- writer.Write(length1);
- writer.Write(length2);
- for(int x = 0; x < length0; x++)
- {
- for(int y = 0; y < length1; y++)
- {
- for(int z = 0; z < length2; z++)
- {
- if (array[x,y,z] == null || array[x,y,z].Length == 0)
- countZero++;
- else
- {
- if (countAllZero2D > 0)
- {
- writer.Write(Ebdb.ZeroA2);
- m_WriteInt(countAllZero2D);
- countAllZero2D = 0;
- }
- if (countAllZero > 0)
- {
- writer.Write(Ebdb.ZeroA1);
- m_WriteInt(countAllZero);
- countAllZero = 0;
- }
- if (countZero > 0)
- {
- writer.Write(Ebdb.Zero);
- m_WriteInt(countZero);
- countZero = 0;
- }
- writer.Write(Ebdb.String);
- writer.Write(array[x,y,z]);
- }
- }
- if (countZero == length2)
- countAllZero++;
- else
- writer.Write(Ebdb.EoA1);
- countZero = 0;
- }
- if (countAllZero == length1)
- countAllZero2D++;
- else
- writer.Write(Ebdb.EoA2);
- countAllZero = 0;
- }
- writer.Write(Ebdb.EoD);
- }
- #endregion
- #region IDisposable メンバ
- public void Dispose()
- {
- if (writer != null)
- writer.Close();
- writer = null;
- }
- #endregion
- public void Close()
- {
- Dispose();
- }
- }
- }
|