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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (show annotations) (download)
Mon Jun 3 20:11:43 2013 UTC (7 years, 3 months ago) by william
File size: 6965 byte(s)
+ allow the delete of result files, when reading (if delete is true)

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

  ViewVC Help
Powered by ViewVC 1.1.22