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 |
using RomCheater.Logging; |
20 |
using Enterprise.Logging; |
21 |
|
22 |
namespace RomCheater.Serialization |
23 |
{ |
24 |
public abstract class SerializationReader : ISerializationResultCount, IDisposable |
25 |
{ |
26 |
private string CreateDynamicFilename(Guid guid) |
27 |
{ |
28 |
if (guid == Guid.Empty) |
29 |
{ |
30 |
if (this.TemporaryFolder != string.Empty) |
31 |
{ |
32 |
return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME); |
33 |
} |
34 |
else |
35 |
{ |
36 |
return string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME); |
37 |
} |
38 |
|
39 |
} |
40 |
else |
41 |
{ |
42 |
if (this.TemporaryFolder != string.Empty) |
43 |
{ |
44 |
return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString())); |
45 |
} |
46 |
else |
47 |
{ |
48 |
return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString())); |
49 |
} |
50 |
} |
51 |
} |
52 |
private void CreateFilename(Guid guid) |
53 |
{ |
54 |
FileName = CreateDynamicFilename(guid); |
55 |
} |
56 |
|
57 |
public const string DEFAULT_FOLDER = "tmp"; |
58 |
protected const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp"; |
59 |
//private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME); |
60 |
//public SerializationReader() : this(CreateFilename(Guid.Empty)) { CreateReader(Guid.Empty); } |
61 |
public SerializationReader(Guid guid) : this(guid,false) { } |
62 |
public SerializationReader(Guid guid, bool delete) { Delete = delete; ReaderGuid = guid; CreateFilename(guid); } |
63 |
//protected SerializationReader(string filename) { FileName = filename; } |
64 |
|
65 |
|
66 |
private bool Delete { get; set; } |
67 |
|
68 |
protected virtual string TemporaryFolder { get { return string.Empty; } } |
69 |
|
70 |
protected bool Cancelled { get; set; } |
71 |
|
72 |
public void CancelRequest() { Cancelled = true; } |
73 |
|
74 |
protected Guid ReaderGuid { get; set; } |
75 |
|
76 |
#region ISerializationResultCount members |
77 |
public ulong ResultCount { get; protected set; } |
78 |
#endregion |
79 |
//private FileStream fileStream; |
80 |
//protected BinaryReader binReader; |
81 |
protected string FileName { get; private set; } |
82 |
protected FileStream CreateReader() |
83 |
{ |
84 |
return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
85 |
} |
86 |
protected FileStream CreateReader(Guid guid) |
87 |
{ |
88 |
return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
89 |
} |
90 |
private void DestroyReader(Guid guid) |
91 |
{ |
92 |
try |
93 |
{ |
94 |
FileInfo fi = new FileInfo(CreateDynamicFilename(guid)); |
95 |
try |
96 |
{ |
97 |
if (fi.Exists) |
98 |
{ |
99 |
fi.Delete(); |
100 |
} |
101 |
|
102 |
} |
103 |
catch (Exception ex) |
104 |
{ |
105 |
gLog.Error.WriteLine("Failed to delete result file: {0}", fi.Name); |
106 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
107 |
} |
108 |
} |
109 |
catch (Exception ex) |
110 |
{ |
111 |
gLog.Error.WriteLine("Failed to delete result file with guid: {0}", guid.ToString()); |
112 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
113 |
} |
114 |
|
115 |
} |
116 |
#region IDisposable Support |
117 |
// Track whether Dispose has been called. |
118 |
private bool disposed = false; |
119 |
// Implement IDisposable. |
120 |
// Do not make this method virtual. |
121 |
// A derived class should not be able to override this method. |
122 |
public void Dispose() |
123 |
{ |
124 |
Dispose(true); |
125 |
// This object will be cleaned up by the Dispose method. |
126 |
// Therefore, you should call GC.SupressFinalize to |
127 |
// take this object off the finalization queue |
128 |
// and prevent finalization code for this object |
129 |
// from executing a second time. |
130 |
GC.SuppressFinalize(this); |
131 |
} |
132 |
// Dispose(bool disposing) executes in two distinct scenarios. |
133 |
// If disposing equals true, the method has been called directly |
134 |
// or indirectly by a user's code. Managed and unmanaged resources |
135 |
// can be disposed. |
136 |
// If disposing equals false, the method has been called by the |
137 |
// runtime from inside the finalizer and you should not reference |
138 |
// other objects. Only unmanaged resources can be disposed. |
139 |
protected virtual void Dispose(bool disposing) |
140 |
{ |
141 |
// Check to see if Dispose has already been called. |
142 |
if (!this.disposed) |
143 |
{ |
144 |
// If disposing equals true, dispose all managed |
145 |
// and unmanaged resources. |
146 |
if (disposing) |
147 |
{ |
148 |
// Dispose managed resources. |
149 |
//component.Dispose(); |
150 |
} |
151 |
|
152 |
// Call the appropriate methods to clean up |
153 |
// unmanaged resources here. |
154 |
// If disposing is false, |
155 |
// only the following code is executed. |
156 |
|
157 |
//// clean up any resources |
158 |
//binReader.Close(); |
159 |
//binReader = null; |
160 |
//fileStream = null; |
161 |
// Note disposing has been done. |
162 |
|
163 |
if (Delete) |
164 |
{ |
165 |
DestroyReader(ReaderGuid); |
166 |
} |
167 |
|
168 |
disposed = true; |
169 |
|
170 |
} |
171 |
} |
172 |
// Use C# destructor syntax for finalization code. |
173 |
// This destructor will run only if the Dispose method |
174 |
// does not get called. |
175 |
// It gives your base class the opportunity to finalize. |
176 |
// Do not provide destructors in types derived from this class. |
177 |
~SerializationReader() |
178 |
{ |
179 |
// Do not re-create Dispose clean-up code here. |
180 |
// Calling Dispose(false) is optimal in terms of |
181 |
// readability and maintainability. |
182 |
Dispose(false); |
183 |
} |
184 |
#endregion |
185 |
} |
186 |
} |