From da0dca792661022bd8f1ab71e8c1e4f0915eb9a7 Mon Sep 17 00:00:00 2001 From: yyagi Date: Tue, 18 Apr 2017 23:58:19 +0900 Subject: [PATCH] =?utf8?q?#36500=20=E8=A8=AD=E5=AE=9A=E6=83=85=E5=A0=B1?= =?utf8?q?=E3=82=84=E5=90=84=E7=A8=AEDB=E3=81=AE=E4=B8=AD=E8=BA=AB?= =?utf8?q?=E3=81=8C=E4=BB=A5=E5=89=8D=E3=81=A8=E5=90=8C=E3=81=98=E5=A0=B4?= =?utf8?q?=E5=90=88=E3=81=AF=E3=80=81=E4=B8=8A=E6=9B=B8=E3=81=8D=E3=82=92?= =?utf8?q?=E8=A1=8C=E3=82=8F=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB?= =?utf8?q?=E3=81=97=E3=81=9F=E3=80=82(SSD=E5=90=91=E3=81=91=E3=81=AE?= =?utf8?q?=E5=AF=BE=E5=BF=9C)=20=E3=81=97=E3=81=B0=E3=82=89=E3=81=8F?= =?utf8?q?=E6=A7=98=E5=AD=90=E3=82=92=E8=A6=8B=E3=81=9F=E3=81=84=E3=81=AE?= =?utf8?q?=E3=81=A7=E3=80=81=E3=83=87=E3=83=90=E3=83=83=E3=82=B0=E3=83=93?= =?utf8?q?=E3=83=AB=E3=83=89=E3=81=AE=E5=AE=9F=E8=A1=8C=E6=99=82=E3=81=AF?= =?utf8?q?=E3=81=A8=E3=80=81DTXMania.log=E3=81=AB=E6=9C=AC=E4=BB=B6?= =?utf8?q?=E7=94=A8=E3=81=AE=E3=83=87=E3=83=90=E3=83=83=E3=82=B0=E3=83=A1?= =?utf8?q?=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8=E3=81=8C=E8=BF=BD=E5=8A=A0?= =?utf8?q?=E5=87=BA=E5=8A=9B=E3=81=95=E3=82=8C=E3=81=BE=E3=81=99=E3=80=82?= =?utf8?q?=E3=81=94=E5=AE=B9=E8=B5=A6=E3=81=8F=E3=81=A0=E3=81=95=E3=81=84?= =?utf8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../DTXManiaプロジェクト.csproj | 1 + .../コード/スコア、曲/CSong管理.cs | 5 +- .../ステージ/02.タイトル/CEnumSongs.cs | 3 +- .../コード/全体/CDTXMania.cs | 4 +- .../コード/全体/FileStreamSSD.cs | 196 +++++++++++++++++++++ 5 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 DTXManiaプロジェクト/コード/全体/FileStreamSSD.cs diff --git a/DTXManiaプロジェクト/DTXManiaプロジェクト.csproj b/DTXManiaプロジェクト/DTXManiaプロジェクト.csproj index 4437584d..4b967b98 100644 --- a/DTXManiaプロジェクト/DTXManiaプロジェクト.csproj +++ b/DTXManiaプロジェクト/DTXManiaプロジェクト.csproj @@ -95,6 +95,7 @@ + diff --git a/DTXManiaプロジェクト/コード/スコア、曲/CSong管理.cs b/DTXManiaプロジェクト/コード/スコア、曲/CSong管理.cs index ba19684a..cb2cc6a1 100644 --- a/DTXManiaプロジェクト/コード/スコア、曲/CSong管理.cs +++ b/DTXManiaプロジェクト/コード/スコア、曲/CSong管理.cs @@ -1082,8 +1082,9 @@ namespace DTXMania this.nSongsDBへ出力できたスコア数 = 0; try { - BinaryWriter bw = new BinaryWriter(new FileStream(SongsDBファイル名, FileMode.Create, FileAccess.Write)); - bw.Write(SONGSDB_VERSION); + //BinaryWriter bw = new BinaryWriter( new FileStream( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); + BinaryWriter bw = new BinaryWriter( new FileStreamSSD( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); + bw.Write( SONGSDB_VERSION ); this.tSongsDBにリストを1つ出力する(bw, this.list曲ルート); bw.Close(); } diff --git a/DTXManiaプロジェクト/コード/ステージ/02.タイトル/CEnumSongs.cs b/DTXManiaプロジェクト/コード/ステージ/02.タイトル/CEnumSongs.cs index 7dfc0626..465bacdb 100644 --- a/DTXManiaプロジェクト/コード/ステージ/02.タイトル/CEnumSongs.cs +++ b/DTXManiaプロジェクト/コード/ステージ/02.タイトル/CEnumSongs.cs @@ -630,7 +630,8 @@ namespace DTXMania Stream output = null; try { - output = File.Create(strPathSongList); + //output = File.Create( strPathSongList ); + output = new FileStreamSSD( strPathSongList ); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(output, cs); } diff --git a/DTXManiaプロジェクト/コード/全体/CDTXMania.cs b/DTXManiaプロジェクト/コード/全体/CDTXMania.cs index afc617b1..990205fc 100644 --- a/DTXManiaプロジェクト/コード/全体/CDTXMania.cs +++ b/DTXManiaプロジェクト/コード/全体/CDTXMania.cs @@ -1997,7 +1997,7 @@ namespace DTXMania settings.Indent = true; settings.NewLineChars = Environment.NewLine; settings.Encoding = new System.Text.UTF8Encoding(false); - using (XmlWriter xw = XmlWriter.Create(xmlpath, settings)) + using ( XmlWriter xw = XmlWriter.Create( new FileStreamSSD( xmlpath ), settings ) ) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); serializer.WriteObject(xw, obj); @@ -2490,7 +2490,7 @@ namespace DTXMania ConfigIni.SwapGuitarBassInfos_PlaySettings(); } */ - string str = strEXEのあるフォルダ + "Config.ini"; + string str = strEXEのあるフォルダ + "Config.xml"; Trace.Indent(); try { diff --git a/DTXManiaプロジェクト/コード/全体/FileStreamSSD.cs b/DTXManiaプロジェクト/コード/全体/FileStreamSSD.cs new file mode 100644 index 00000000..aa130753 --- /dev/null +++ b/DTXManiaプロジェクト/コード/全体/FileStreamSSD.cs @@ -0,0 +1,196 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Diagnostics; + +namespace DTXMania +{ + /// + /// SSD向けFileStream (ファイルの上書き保存時に、同じファイル内容を保存する場合は、ファイルの更新を行わない) + /// + public class FileStreamSSD : MemoryStream, IDisposable + { + private string _filename; + private FileMode _mode; + private FileAccess _access; + private MemoryStream _ms_new = null, _ms_org = null; + private FileStream _fs; + + #region [ コンストラクタ ] + public FileStreamSSD() + { + throw new ArgumentException( "FileStreamSSD: 引数のないコンストラクタは使用できません。" ); + } + + public FileStreamSSD( string path ) + { +//Trace.TraceInformation( "FileStreamSSD(" + Path.GetFileName(path) + "): Constractor" ); + Initialize( path, FileMode.Create, FileAccess.Write ); + } + public FileStreamSSD( string path, FileMode mode, FileAccess access ) + { +//Trace.TraceInformation( "FileStreamSSD(" + Path.GetFileName(path) + ", " + mode.ToString() + ", " + access.ToString() + "): Constractor" ); + Initialize( path, mode, access ); + } + + private void Initialize( string path, FileMode mode, FileAccess access ) + { + if ( mode != FileMode.Create ) + { + throw new ArgumentException( mode.ToString() + "は、FileStreamSSD()でサポートしていません。" ); + } + if ( access != FileAccess.Write ) + { + throw new ArgumentException( access.ToString() + "は、FileStreamSSD()でサポートしていません。" ); + } + _filename = path; + _mode = mode; + _access = access; + _ms_new = new MemoryStream(); + } + #endregion + + + /// + /// StreamのWrite。 + /// + /// + /// + /// + public override void Write( byte[] buffer, int offset, int count ) + { + _ms_new.Write( buffer, offset, count ); +//if ( Path.GetExtension( _filename ) != ".db" ) +//{ +//Debug.Write( "W" ); +//} + } + + /// + /// StreamのClose。元ファイルとのコンペアを行い、一致していればファイルの上書きをしない + /// + public override void Close() + { +Debug.WriteLine( "---" ); + bool bSame = true; + Flush(); + + // 元ファイルがなければ、無条件に上書きコースへ。 + if ( !File.Exists( _filename ) ) + { + bSame = false; +Debug.WriteLine( Path.GetFileName( _filename ) + ": No file exists" ); + } + else + // まず、既存ファイルをMemoryStreamにコピー + { +Debug.WriteLine( "B2" ); + using ( _fs = new FileStream( _filename, FileMode.Open, FileAccess.Read ) ) + { + Debug.WriteLine( "Length old=" + _fs.Length + ", new=" + _ms_new.Length ); + // 元ファイルとファイルサイズが異なる場合は、 + if ( _fs.Length != _ms_new.Length ) + { + bSame = false; +Debug.WriteLine( "B2: Set bSame=false" ); + } + else + { +Debug.WriteLine( "B2: copy from _fs to _ms_org" ); + _ms_org = new MemoryStream(); + _fs.CopyTo( _ms_org ); + } + }; // _fs will be closed and disposed here, by using() + _fs = null; + } + +Debug.WriteLine( "C" ); + if ( bSame ) // まだ新旧ファイルが一致している可能性があれば... + { + // MemoryStream同士のコンペア + _ms_org.Seek( 0, SeekOrigin.Begin ); + _ms_new.Seek( 0, SeekOrigin.Begin ); + +Debug.WriteLine( "1" ); + while (_ms_new.Position < _ms_new.Length ) + { + int dorg = _ms_org.ReadByte(); + int dnew = _ms_new.ReadByte(); + if (dorg != dnew) + { + bSame = false; + break; + } + } + } +Debug.WriteLine( "2: bSame=" + bSame ); + if ( _ms_org != null ) + { + _ms_org.Close(); + _ms_org.Dispose(); + _ms_org = null; + } +Debug.WriteLine( "3" ); + _ms_new.Seek( 0, SeekOrigin.Begin ); + +Debug.WriteLine( "new file length: " + _ms_new.Length ); + // 元ファイルと新規ファイルが一致していない場合、新規ファイルで上書きする + if ( !bSame ) + { + Trace.TraceInformation( Path.GetFileName( _filename ) + ": 以前のファイルから変化があったため、書き込みを実行します。" ); + using ( _fs = new FileStream( _filename, _mode, _access ) ) + { + _ms_new.CopyTo( _fs ); + } // _fs will be closed and disposed, by using() + _fs = null; + } + else + { + Trace.TraceInformation( Path.GetFileName( _filename ) + ": 以前のファイルから変化がなかったため、書き込みを行いません。" ); + } + _ms_new.Close(); + _ms_new.Dispose(); + _ms_new = null; + + //base.Close(); + } + + + + #region [ Dispose-Finallizeパターン実装 ] + //----------------- + public new void Dispose() + { + this.Dispose( true ); + GC.SuppressFinalize( this ); + base.Dispose(); + } + protected override void Dispose( bool bManagedDispose ) + { + if ( this._ms_new != null ) + { + this.Close(); // Close()する前にDispose()された場合用 (DataContractSerializer経由だと、ここに来る) + //this._ms_new.Dispose(); // Close()内でDisposeとnullクリアがなされるため、Disposeはしない + //this._ms_new = null; // + } + if ( this._ms_org != null ) + { + this._ms_org.Dispose(); + this._ms_org = null; + } + if ( this._fs != null ) + { + this._fs.Dispose(); + this._fs = null; + } + } + ~FileStreamSSD() + { + this.Dispose( false ); + } + #endregion + } +} -- 2.11.0