Revisión | 6f1d34948fd66ea998b671bbd3029d9818321624 (tree) |
---|---|
Tiempo | 2017-05-09 22:56:53 |
Autor | yyagi <yyagi.dtxmania@gmai...> |
Commiter | yyagi |
#36500 書き込み最小化のクラスを作り直した。従来はsongs.db等で正しく書き込みがなされない(文字列の前に文字数の情報が出力されていない)問題があったが、これを修正している。
#xxxxx タイトルバーにマウスカーソルを合わせている場合は、マウスカーソルを消去しないようにした。
@@ -1082,8 +1082,8 @@ namespace DTXMania | ||
1082 | 1082 | this.nSongsDBへ出力できたスコア数 = 0; |
1083 | 1083 | try |
1084 | 1084 | { |
1085 | - BinaryWriter bw = new BinaryWriter( new FileStream( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); | |
1086 | - //BinaryWriter bw = new BinaryWriter( new FileStreamSSD( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); | |
1085 | + //BinaryWriter bw = new BinaryWriter( new FileStream( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); | |
1086 | + BinaryWriter bw = new BinaryWriter( new FileStreamSSD( SongsDBファイル名, FileMode.Create, FileAccess.Write ) ); | |
1087 | 1087 | bw.Write( SONGSDB_VERSION ); |
1088 | 1088 | this.tSongsDBにリストを1つ出力する(bw, this.list曲ルート); |
1089 | 1089 | bw.Close(); |
@@ -2828,7 +2828,11 @@ namespace DTXMania | ||
2828 | 2828 | //Trace.TraceInformation("n現在の経過時間ms" + ccMouseShow.n現在の経過時間ms + ", n現在の値=" + ccMouseShow.n現在の値 + ", b終了値に達した=" + ccMouseShow.b終了値に達した); |
2829 | 2829 | if (bマウスカーソル表示中 && ccMouseShow.b終了値に達した) |
2830 | 2830 | { |
2831 | - Hide(); | |
2831 | + Point client_point = CDTXMania.Instance.Window.PointToClient(Cursor.Position); | |
2832 | + if (client_point.Y >= 0) // タイトルバー上にマウスカーソルがある場合は、隠さない | |
2833 | + { | |
2834 | + Hide(); | |
2835 | + } | |
2832 | 2836 | } |
2833 | 2837 | |
2834 | 2838 | } |
@@ -16,8 +16,16 @@ namespace DTXMania | ||
16 | 16 | private string _filename; |
17 | 17 | private FileMode _mode; |
18 | 18 | private FileAccess _access; |
19 | - private MemoryStream _ms_new = null, _ms_org = null; | |
19 | + private MemoryStream _ms_org = null; | |
20 | 20 | private FileStream _fs; |
21 | + private bool disposed = false; | |
22 | + | |
23 | + /// <summary> | |
24 | + /// (baseではなく)このクラスのClose()が実行されたかどうか | |
25 | + /// </summary> | |
26 | + private bool bClosed = false; | |
27 | + | |
28 | + | |
21 | 29 | |
22 | 30 | #region [ コンストラクタ ] |
23 | 31 | public FileStreamSSD() |
@@ -26,55 +34,31 @@ namespace DTXMania | ||
26 | 34 | } |
27 | 35 | |
28 | 36 | public FileStreamSSD( string path ) |
37 | + : this(path, FileMode.Create, FileAccess.Write) | |
29 | 38 | { |
30 | -//Trace.TraceInformation( "FileStreamSSD(" + Path.GetFileName(path) + "): Constractor" ); | |
31 | - Initialize( path, FileMode.Create, FileAccess.Write ); | |
32 | 39 | } |
33 | 40 | public FileStreamSSD( string path, FileMode mode, FileAccess access ) |
41 | + : base() | |
34 | 42 | { |
35 | -//Trace.TraceInformation( "FileStreamSSD(" + Path.GetFileName(path) + ", " + mode.ToString() + ", " + access.ToString() + "): Constractor" ); | |
36 | - Initialize( path, mode, access ); | |
37 | - } | |
38 | - | |
39 | - private void Initialize( string path, FileMode mode, FileAccess access ) | |
40 | - { | |
41 | - if ( mode != FileMode.Create ) | |
43 | + if (mode != FileMode.Create) | |
42 | 44 | { |
43 | - throw new ArgumentException( mode.ToString() + "は、FileStreamSSD()でサポートしていません。" ); | |
45 | + throw new ArgumentException(mode.ToString() + "は、FileStreamSSD()でサポートしていません。"); | |
44 | 46 | } |
45 | - if ( access != FileAccess.Write ) | |
47 | + if (access != FileAccess.Write) | |
46 | 48 | { |
47 | - throw new ArgumentException( access.ToString() + "は、FileStreamSSD()でサポートしていません。" ); | |
49 | + throw new ArgumentException(access.ToString() + "は、FileStreamSSD()でサポートしていません。"); | |
48 | 50 | } |
49 | 51 | _filename = path; |
50 | 52 | _mode = mode; |
51 | 53 | _access = access; |
52 | - _ms_new = new MemoryStream(); | |
53 | 54 | } |
54 | 55 | #endregion |
55 | 56 | |
56 | - | |
57 | - /// <summary> | |
58 | - /// StreamのWrite。 | |
59 | - /// </summary> | |
60 | - /// <param name="buffer"></param> | |
61 | - /// <param name="offset"></param> | |
62 | - /// <param name="count"></param> | |
63 | - public override void Write( byte[] buffer, int offset, int count ) | |
64 | - { | |
65 | - _ms_new.Write( buffer, offset, count ); | |
66 | -//if ( Path.GetExtension( _filename ) != ".db" ) | |
67 | -//{ | |
68 | -//Debug.Write( "W" ); | |
69 | -//} | |
70 | - } | |
71 | - | |
72 | 57 | /// <summary> |
73 | 58 | /// StreamのClose。元ファイルとのコンペアを行い、一致していればファイルの上書きをしない |
74 | 59 | /// </summary> |
75 | - public override void Close() | |
60 | + public new void Close() | |
76 | 61 | { |
77 | -Debug.WriteLine( "---" ); | |
78 | 62 | bool bSame = true; |
79 | 63 | Flush(); |
80 | 64 |
@@ -82,68 +66,65 @@ Debug.WriteLine( "---" ); | ||
82 | 66 | if ( !File.Exists( _filename ) ) |
83 | 67 | { |
84 | 68 | bSame = false; |
85 | -Debug.WriteLine( Path.GetFileName( _filename ) + ": No file exists" ); | |
86 | 69 | } |
87 | 70 | else |
88 | 71 | // まず、既存ファイルをMemoryStreamにコピー |
89 | 72 | { |
90 | -Debug.WriteLine( "B2" ); | |
91 | - using ( _fs = new FileStream( _filename, FileMode.Open, FileAccess.Read ) ) | |
73 | + using ( _fs = new FileStream( _filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete ) ) | |
92 | 74 | { |
93 | - Debug.WriteLine( "Length old=" + _fs.Length + ", new=" + _ms_new.Length ); | |
94 | 75 | // 元ファイルとファイルサイズが異なる場合は、 |
95 | - if ( _fs.Length != _ms_new.Length ) | |
76 | + if ( _fs.Length != this.Length ) | |
96 | 77 | { |
97 | 78 | bSame = false; |
98 | -Debug.WriteLine( "B2: Set bSame=false" ); | |
99 | 79 | } |
100 | 80 | else |
101 | 81 | { |
102 | -Debug.WriteLine( "B2: copy from _fs to _ms_org" ); | |
103 | 82 | _ms_org = new MemoryStream(); |
104 | 83 | _fs.CopyTo( _ms_org ); |
105 | 84 | } |
106 | - }; // _fs will be closed and disposed here, by using() | |
85 | + }; | |
107 | 86 | _fs = null; |
108 | 87 | } |
109 | 88 | |
110 | -Debug.WriteLine( "C" ); | |
111 | 89 | if ( bSame ) // まだ新旧ファイルが一致している可能性があれば... |
112 | 90 | { |
113 | 91 | // MemoryStream同士のコンペア |
114 | 92 | _ms_org.Seek( 0, SeekOrigin.Begin ); |
115 | - _ms_new.Seek( 0, SeekOrigin.Begin ); | |
116 | - | |
117 | -Debug.WriteLine( "1" ); | |
118 | - while (_ms_new.Position < _ms_new.Length ) | |
93 | + this.Seek( 0, SeekOrigin.Begin ); | |
94 | + | |
95 | + try | |
119 | 96 | { |
120 | - int dorg = _ms_org.ReadByte(); | |
121 | - int dnew = _ms_new.ReadByte(); | |
122 | - if (dorg != dnew) | |
97 | + while (this.Position < this.Length) | |
123 | 98 | { |
124 | - bSame = false; | |
125 | - break; | |
99 | + int dorg = _ms_org.ReadByte(); | |
100 | + int dnew = this.ReadByte(); | |
101 | + if (dorg != dnew) | |
102 | + { | |
103 | + bSame = false; | |
104 | + break; | |
105 | + } | |
126 | 106 | } |
127 | 107 | } |
108 | + catch // ファイルサイズが同じ場合のみtry内に来るため、通常ここには来ないはずだが、念のためbSame=false側に倒しておく | |
109 | + { | |
110 | + bSame = false; | |
111 | + } | |
128 | 112 | } |
129 | -Debug.WriteLine( "2: bSame=" + bSame ); | |
130 | 113 | if ( _ms_org != null ) |
131 | 114 | { |
132 | 115 | _ms_org.Close(); |
133 | 116 | _ms_org.Dispose(); |
134 | 117 | _ms_org = null; |
135 | 118 | } |
136 | -Debug.WriteLine( "3" ); | |
137 | - _ms_new.Seek( 0, SeekOrigin.Begin ); | |
119 | + this.Seek( 0, SeekOrigin.Begin ); | |
138 | 120 | |
139 | -Debug.WriteLine( "new file length: " + _ms_new.Length ); | |
140 | 121 | // 元ファイルと新規ファイルが一致していない場合、新規ファイルで上書きする |
141 | 122 | if ( !bSame ) |
142 | 123 | { |
143 | 124 | Trace.TraceInformation( Path.GetFileName( _filename ) + ": 以前のファイルから変化があったため、書き込みを実行します。" ); |
144 | 125 | using ( _fs = new FileStream( _filename, _mode, _access ) ) |
145 | 126 | { |
146 | - _ms_new.CopyTo( _fs ); | |
127 | + this.CopyTo( _fs ); | |
147 | 128 | } // _fs will be closed and disposed, by using() |
148 | 129 | _fs = null; |
149 | 130 | } |
@@ -151,11 +132,9 @@ Debug.WriteLine( "new file length: " + _ms_new.Length ); | ||
151 | 132 | { |
152 | 133 | Trace.TraceInformation( Path.GetFileName( _filename ) + ": 以前のファイルから変化がなかったため、書き込みを行いません。" ); |
153 | 134 | } |
154 | - _ms_new.Close(); | |
155 | - _ms_new.Dispose(); | |
156 | - _ms_new = null; | |
157 | 135 | |
158 | - //base.Close(); | |
136 | + bClosed = true; // base.Close()の前にフラグ変更のこと。さもないと、無限に再帰実行される | |
137 | + Dispose(); | |
159 | 138 | } |
160 | 139 | |
161 | 140 |
@@ -168,23 +147,34 @@ Debug.WriteLine( "new file length: " + _ms_new.Length ); | ||
168 | 147 | GC.SuppressFinalize( this ); |
169 | 148 | base.Dispose(); |
170 | 149 | } |
171 | - protected override void Dispose( bool bManagedDispose ) | |
150 | + protected override void Dispose( bool disposing) | |
172 | 151 | { |
173 | - if ( this._ms_new != null ) | |
174 | - { | |
175 | - this.Close(); // Close()する前にDispose()された場合用 (DataContractSerializer経由だと、ここに来る) | |
176 | - //this._ms_new.Dispose(); // Close()内でDisposeとnullクリアがなされるため、Disposeはしない | |
177 | - //this._ms_new = null; // | |
178 | - } | |
179 | - if ( this._ms_org != null ) | |
152 | + if (!this.disposed) | |
180 | 153 | { |
181 | - this._ms_org.Dispose(); | |
182 | - this._ms_org = null; | |
183 | - } | |
184 | - if ( this._fs != null ) | |
185 | - { | |
186 | - this._fs.Dispose(); | |
187 | - this._fs = null; | |
154 | + try | |
155 | + { | |
156 | + if (this._ms_org != null) | |
157 | + { | |
158 | + this._ms_org.Close(); | |
159 | + this._ms_org.Dispose(); | |
160 | + this._ms_org = null; | |
161 | + } | |
162 | + if (this._fs != null) | |
163 | + { | |
164 | + this._fs.Close(); | |
165 | + this._fs.Dispose(); | |
166 | + this._fs = null; | |
167 | + } | |
168 | + if (!bClosed) | |
169 | + { | |
170 | + this.Close(); // Close()なしでDispose()された場合用 (DataContractSerializer経由だと、ここに来る) | |
171 | + } | |
172 | + this.disposed = true; | |
173 | + } | |
174 | + finally | |
175 | + { | |
176 | + base.Dispose(disposing); | |
177 | + } | |
188 | 178 | } |
189 | 179 | } |
190 | 180 | ~FileStreamSSD() |