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