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 abstract class SerializationReader : ISerializationResultCount, IDisposable |
10 |
{ |
11 |
private static string CreateFilename(Guid guid) |
12 |
{ |
13 |
if (guid == Guid.Empty) |
14 |
{ |
15 |
return DEFAULT_PATH; |
16 |
} |
17 |
else |
18 |
{ |
19 |
return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString())); |
20 |
} |
21 |
} |
22 |
private const string DEFAULT_FOLDER = "tmp"; |
23 |
private const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp"; |
24 |
private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME); |
25 |
//public SerializationReader() : this(CreateFilename(Guid.Empty)) { CreateReader(Guid.Empty); } |
26 |
public SerializationReader(Guid guid) : this(CreateFilename(guid)) { CreateReader(guid); } |
27 |
protected SerializationReader(string filename) { FileName = filename; } |
28 |
|
29 |
|
30 |
protected bool Cancelled { get; set; } |
31 |
|
32 |
public void CancelRequest() { Cancelled = true; } |
33 |
|
34 |
#region ISerializationResultCount members |
35 |
public int ResultCount { get; protected set; } |
36 |
#endregion |
37 |
private FileStream fileStream; |
38 |
protected BinaryReader binReader; |
39 |
protected string FileName { get; private set; } |
40 |
private void CreateReader(Guid guid) |
41 |
{ |
42 |
if (Cancelled) { return; } |
43 |
fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
44 |
binReader = new BinaryReader(fileStream); |
45 |
} |
46 |
#region IDisposable Support |
47 |
// Track whether Dispose has been called. |
48 |
private bool disposed = false; |
49 |
// Implement IDisposable. |
50 |
// Do not make this method virtual. |
51 |
// A derived class should not be able to override this method. |
52 |
public void Dispose() |
53 |
{ |
54 |
Dispose(true); |
55 |
// This object will be cleaned up by the Dispose method. |
56 |
// Therefore, you should call GC.SupressFinalize to |
57 |
// take this object off the finalization queue |
58 |
// and prevent finalization code for this object |
59 |
// from executing a second time. |
60 |
GC.SuppressFinalize(this); |
61 |
} |
62 |
// Dispose(bool disposing) executes in two distinct scenarios. |
63 |
// If disposing equals true, the method has been called directly |
64 |
// or indirectly by a user's code. Managed and unmanaged resources |
65 |
// can be disposed. |
66 |
// If disposing equals false, the method has been called by the |
67 |
// runtime from inside the finalizer and you should not reference |
68 |
// other objects. Only unmanaged resources can be disposed. |
69 |
protected virtual void Dispose(bool disposing) |
70 |
{ |
71 |
// Check to see if Dispose has already been called. |
72 |
if (!this.disposed) |
73 |
{ |
74 |
// If disposing equals true, dispose all managed |
75 |
// and unmanaged resources. |
76 |
if (disposing) |
77 |
{ |
78 |
// Dispose managed resources. |
79 |
//component.Dispose(); |
80 |
} |
81 |
|
82 |
// Call the appropriate methods to clean up |
83 |
// unmanaged resources here. |
84 |
// If disposing is false, |
85 |
// only the following code is executed. |
86 |
|
87 |
// clean up any resources |
88 |
binReader.Close(); |
89 |
binReader = null; |
90 |
fileStream = null; |
91 |
// Note disposing has been done. |
92 |
disposed = true; |
93 |
|
94 |
} |
95 |
} |
96 |
// Use C# destructor syntax for finalization code. |
97 |
// This destructor will run only if the Dispose method |
98 |
// does not get called. |
99 |
// It gives your base class the opportunity to finalize. |
100 |
// Do not provide destructors in types derived from this class. |
101 |
~SerializationReader() |
102 |
{ |
103 |
// Do not re-create Dispose clean-up code here. |
104 |
// Calling Dispose(false) is optimal in terms of |
105 |
// readability and maintainability. |
106 |
Dispose(false); |
107 |
} |
108 |
#endregion |
109 |
} |
110 |
} |