/[RomCheater]/trunk/RomCheater/Serialization/SerializationReader.cs
ViewVC logotype

Contents of /trunk/RomCheater/Serialization/SerializationReader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 448 - (show annotations) (download)
Sun Jun 2 20:22:04 2013 UTC (8 years, 4 months ago) by william
File size: 5166 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22