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 System.Diagnostics; |
20 |
|
21 |
namespace RomCheater.Serialization |
22 |
{ |
23 |
public interface ISearchResultWriter |
24 |
{ |
25 |
void WriteResult<TValue>(ulong address, TValue value) where TValue : IConvertible; |
26 |
} |
27 |
public class SearchResultWriter : SerializationWriter, ISearchResultWriter |
28 |
{ |
29 |
|
30 |
private ulong ResultsWritten = 0; |
31 |
protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } } |
32 |
//public SearchResultWriter() : base() { } |
33 |
public SearchResultWriter(Guid guid) : this(0, guid) { } |
34 |
public SearchResultWriter(Guid guid, bool delete) : this(0, guid, delete) { } |
35 |
public SearchResultWriter(ulong resultCount, Guid guid) : this(resultCount, guid, false) { } |
36 |
public SearchResultWriter(ulong resultCount, Guid guid, bool delete) : base(resultCount, guid, delete) { WriteHeader(guid); } |
37 |
//public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); } |
38 |
//public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); } |
39 |
//public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); } |
40 |
#region ISearchResultWriter members |
41 |
public void WriteResult<TValue>(ulong address, TValue value) where TValue : IConvertible |
42 |
{ |
43 |
try |
44 |
{ |
45 |
Type t = typeof(TValue); |
46 |
|
47 |
binWriter.Write(address); |
48 |
switch (t.Name.ToLower()) |
49 |
{ |
50 |
case "byte": |
51 |
binWriter.Write(Convert.ToByte(value)); |
52 |
break; |
53 |
case "sbyte": |
54 |
binWriter.Write(Convert.ToSByte(value)); |
55 |
break; |
56 |
case "uint16": |
57 |
binWriter.Write(Convert.ToUInt16(value)); |
58 |
break; |
59 |
case "int16": |
60 |
binWriter.Write(Convert.ToInt16(value)); |
61 |
break; |
62 |
case "uint32": |
63 |
binWriter.Write(Convert.ToUInt32(value)); |
64 |
break; |
65 |
case "int32": |
66 |
binWriter.Write(Convert.ToInt32(value)); |
67 |
break; |
68 |
case "uint64": |
69 |
binWriter.Write(Convert.ToUInt64(value)); |
70 |
break; |
71 |
case "int64": |
72 |
binWriter.Write(Convert.ToInt64(value)); |
73 |
break; |
74 |
} |
75 |
ResultsWritten++; |
76 |
} |
77 |
catch (Exception ex) |
78 |
{ |
79 |
throw ex; |
80 |
} |
81 |
} |
82 |
#endregion |
83 |
|
84 |
long ResultCountOffset = 0; |
85 |
private void WriteHeader(Guid guid) |
86 |
{ |
87 |
try |
88 |
{ |
89 |
// write magic: SRD |
90 |
binWriter.Write(Encoding.UTF8.GetBytes("SRD")); |
91 |
|
92 |
if (guid == Guid.Empty) |
93 |
{ |
94 |
binWriter.Write((int)1); |
95 |
} |
96 |
else |
97 |
{ |
98 |
binWriter.Write((int)2); |
99 |
// related guid - length |
100 |
binWriter.Write(guid.ToByteArray().Length); |
101 |
// related guid - data |
102 |
binWriter.Write(guid.ToByteArray()); |
103 |
} |
104 |
// write count (int) |
105 |
binWriter.Write(ResultCount); |
106 |
ResultCountOffset = binWriter.BaseStream.Position - sizeof(ulong); |
107 |
} |
108 |
catch (Exception ex) |
109 |
{ |
110 |
throw ex; |
111 |
} |
112 |
} |
113 |
|
114 |
protected override void Dispose(bool disposing) |
115 |
{ |
116 |
if (Cancelled) { binWriter.Close(); fileStream.Close(); return; } |
117 |
try |
118 |
{ |
119 |
|
120 |
//// ensure ResultCount and ResultsWritten are equal |
121 |
//if (ResultCount != ResultsWritten) |
122 |
//{ |
123 |
// string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten); |
124 |
// Logging.logger.Error.WriteLine(message); |
125 |
// throw new InvalidOperationException(message); |
126 |
//} |
127 |
if (ResultCount != ResultsWritten) |
128 |
{ |
129 |
// update the number of results written |
130 |
binWriter.BaseStream.Seek(ResultCountOffset, SeekOrigin.Begin); |
131 |
binWriter.Write(ResultsWritten); |
132 |
} |
133 |
base.Dispose(disposing); |
134 |
} |
135 |
catch (Exception ex) |
136 |
{ |
137 |
throw ex; |
138 |
} |
139 |
} |
140 |
} |
141 |
} |