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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations) (download)
Thu Jun 6 07:56:04 2013 UTC (8 years, 3 months ago) by william
File size: 6967 byte(s)

1 william 486 #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 william 401 using System.Collections.Generic;
16     using System.Linq;
17     using System.Text;
18 william 402 using System.IO;
19 william 486 using RomCheater.Logging;
20 william 401
21     namespace RomCheater.Serialization
22     {
23 william 402 public abstract class SerializationReader : ISerializationResultCount, IDisposable
24 william 401 {
25 william 472 private string CreateDynamicFilename(Guid guid)
26 william 444 {
27     if (guid == Guid.Empty)
28     {
29 william 448 if (this.TemporaryFolder != string.Empty)
30     {
31 william 472 return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME);
32 william 448 }
33     else
34     {
35 william 472 return string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
36 william 448 }
37 william 450
38 william 444 }
39     else
40     {
41 william 448 if (this.TemporaryFolder != string.Empty)
42     {
43 william 472 return string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString()));
44 william 448 }
45     else
46     {
47 william 472 return string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
48 william 448 }
49 william 444 }
50     }
51 william 472 private void CreateFilename(Guid guid)
52     {
53     FileName = CreateDynamicFilename(guid);
54     }
55 william 448
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 william 444 //public SerializationReader() : this(CreateFilename(Guid.Empty)) { CreateReader(Guid.Empty); }
60 william 486 public SerializationReader(Guid guid) : this(guid,false) { }
61     public SerializationReader(Guid guid, bool delete) { Delete = delete; ReaderGuid = guid; CreateFilename(guid); }
62 william 448 //protected SerializationReader(string filename) { FileName = filename; }
63 william 402
64 william 486
65     private bool Delete { get; set; }
66    
67 william 448 protected virtual string TemporaryFolder { get { return string.Empty; } }
68 william 402
69 william 446 protected bool Cancelled { get; set; }
70    
71     public void CancelRequest() { Cancelled = true; }
72    
73 william 456 protected Guid ReaderGuid { get; set; }
74    
75 william 402 #region ISerializationResultCount members
76 william 577 public ulong ResultCount { get; protected set; }
77 william 402 #endregion
78 william 451 //private FileStream fileStream;
79     //protected BinaryReader binReader;
80 william 402 protected string FileName { get; private set; }
81 william 456 protected FileStream CreateReader()
82 william 402 {
83 william 451 return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
84 william 402 }
85 william 472 protected FileStream CreateReader(Guid guid)
86     {
87     return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
88     }
89 william 486 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 william 402 #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 william 451 //// clean up any resources
157     //binReader.Close();
158     //binReader = null;
159     //fileStream = null;
160 william 402 // Note disposing has been done.
161 william 486
162     if (Delete)
163     {
164     DestroyReader(ReaderGuid);
165     }
166    
167 william 402 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 william 401 }
185     }

  ViewVC Help
Powered by ViewVC 1.1.22