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