1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.IO; |
6 |
|
7 |
namespace RomCheater.Serialization |
8 |
{ |
9 |
public interface ISerializationResultCount |
10 |
{ |
11 |
int ResultCount { get; } |
12 |
} |
13 |
|
14 |
public abstract class SerializationWriter : ISerializationResultCount, IDisposable |
15 |
{ |
16 |
private static string CreateFilename(Guid guid) |
17 |
{ |
18 |
if (guid == Guid.Empty) |
19 |
{ |
20 |
return DEFAULT_PATH; |
21 |
} |
22 |
else |
23 |
{ |
24 |
return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString())); |
25 |
} |
26 |
} |
27 |
private const string DEFAULT_FOLDER = "tmp"; |
28 |
private const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp"; |
29 |
private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER,DEFAULT_FILENAME); |
30 |
//public SerializationWriter() : this(CreateFilename(Guid.Empty)) { } |
31 |
|
32 |
public SerializationWriter(int resultCount, Guid guid) : this(resultCount,guid, true) { } |
33 |
public SerializationWriter(int resultCount, Guid guid, bool delete) : this(CreateFilename(guid)) { ResultCount = resultCount; CreateFile(delete); } |
34 |
protected SerializationWriter(Guid guid) : this(0, guid, true) { } |
35 |
protected SerializationWriter(string filename) { FileName = filename; } |
36 |
|
37 |
#region ISerializationResultCount members |
38 |
public int ResultCount { get; protected set; } |
39 |
#endregion |
40 |
private FileStream fileStream; |
41 |
protected BinaryWriter binWriter; |
42 |
protected string FileName { get; private set; } |
43 |
private bool DeleteResultFile() |
44 |
{ |
45 |
try |
46 |
{ |
47 |
FileInfo fi = new FileInfo(FileName); |
48 |
if (fi.Exists) |
49 |
{ |
50 |
fi.Delete(); |
51 |
return true; |
52 |
} |
53 |
return true; |
54 |
} |
55 |
catch |
56 |
{ |
57 |
return false; |
58 |
} |
59 |
} |
60 |
private void CreateFile(bool delete) |
61 |
{ |
62 |
if (delete) |
63 |
DeleteResultFile(); |
64 |
FileInfo fi = new FileInfo(FileName); |
65 |
if (!fi.Exists) |
66 |
{ |
67 |
if (!fi.Directory.Exists) |
68 |
fi.Directory.Create(); |
69 |
FileStream t = fi.Create(); |
70 |
t.Close(); |
71 |
} |
72 |
fi = null; |
73 |
fileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Read); |
74 |
binWriter = new BinaryWriter(fileStream); |
75 |
} |
76 |
//protected void Write(byte[] data) |
77 |
//{ |
78 |
// binWriter.Write(data); |
79 |
//} |
80 |
|
81 |
#region IDisposable Support |
82 |
// Track whether Dispose has been called. |
83 |
private bool disposed = false; |
84 |
// Implement IDisposable. |
85 |
// Do not make this method virtual. |
86 |
// A derived class should not be able to override this method. |
87 |
public void Dispose() |
88 |
{ |
89 |
Dispose(true); |
90 |
// This object will be cleaned up by the Dispose method. |
91 |
// Therefore, you should call GC.SupressFinalize to |
92 |
// take this object off the finalization queue |
93 |
// and prevent finalization code for this object |
94 |
// from executing a second time. |
95 |
GC.SuppressFinalize(this); |
96 |
} |
97 |
// Dispose(bool disposing) executes in two distinct scenarios. |
98 |
// If disposing equals true, the method has been called directly |
99 |
// or indirectly by a user's code. Managed and unmanaged resources |
100 |
// can be disposed. |
101 |
// If disposing equals false, the method has been called by the |
102 |
// runtime from inside the finalizer and you should not reference |
103 |
// other objects. Only unmanaged resources can be disposed. |
104 |
protected virtual void Dispose(bool disposing) |
105 |
{ |
106 |
// Check to see if Dispose has already been called. |
107 |
if (!this.disposed) |
108 |
{ |
109 |
// If disposing equals true, dispose all managed |
110 |
// and unmanaged resources. |
111 |
if (disposing) |
112 |
{ |
113 |
// Dispose managed resources. |
114 |
//component.Dispose(); |
115 |
} |
116 |
|
117 |
// Call the appropriate methods to clean up |
118 |
// unmanaged resources here. |
119 |
// If disposing is false, |
120 |
// only the following code is executed. |
121 |
|
122 |
// clean up any resources |
123 |
binWriter.Flush(); |
124 |
binWriter.Close(); |
125 |
binWriter = null; |
126 |
fileStream = null; |
127 |
// Note disposing has been done. |
128 |
disposed = true; |
129 |
|
130 |
} |
131 |
} |
132 |
// Use C# destructor syntax for finalization code. |
133 |
// This destructor will run only if the Dispose method |
134 |
// does not get called. |
135 |
// It gives your base class the opportunity to finalize. |
136 |
// Do not provide destructors in types derived from this class. |
137 |
~SerializationWriter() |
138 |
{ |
139 |
// Do not re-create Dispose clean-up code here. |
140 |
// Calling Dispose(false) is optimal in terms of |
141 |
// readability and maintainability. |
142 |
Dispose(false); |
143 |
} |
144 |
#endregion |
145 |
} |
146 |
} |