1 |
william |
401 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.Linq; |
4 |
|
|
using System.Text; |
5 |
|
|
using System.IO; |
6 |
william |
405 |
using System.Diagnostics; |
7 |
william |
401 |
|
8 |
|
|
namespace RomCheater.Serialization |
9 |
|
|
{ |
10 |
|
|
public interface ISearchResultWriter |
11 |
|
|
{ |
12 |
|
|
void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible; |
13 |
|
|
} |
14 |
|
|
public class SearchResultWriter : SerializationWriter, ISearchResultWriter |
15 |
|
|
{ |
16 |
william |
405 |
|
17 |
|
|
private int ResultsWritten = 0; |
18 |
|
|
|
19 |
william |
444 |
//public SearchResultWriter() : base() { } |
20 |
|
|
public SearchResultWriter(int resultCount, Guid guid) : base(resultCount, guid) { WriteHeader(guid); } |
21 |
|
|
//public SearchResultWriter(string filename, Guid guid) : base(CreateFileName(filename,guid), guid) { WriteHeader(guid); } |
22 |
|
|
//public SearchResultWriter(string filename, int resultCount, Guid guid) : base(filename, resultCount, guid, false) { WriteHeader(guid); } |
23 |
|
|
//public SearchResultWriter(string filename, int resultCount, Guid guid, bool delete) : base(filename, resultCount, guid, delete) { WriteHeader(guid); } |
24 |
william |
401 |
#region ISearchResultWriter members |
25 |
|
|
public void WriteResult<TValue>(uint address, TValue value) where TValue : IConvertible |
26 |
|
|
{ |
27 |
william |
445 |
try |
28 |
|
|
{ |
29 |
|
|
Type t = typeof(TValue); |
30 |
william |
401 |
|
31 |
william |
445 |
binWriter.Write(address); |
32 |
|
|
switch (t.Name.ToLower()) |
33 |
|
|
{ |
34 |
|
|
case "byte": |
35 |
|
|
binWriter.Write(Convert.ToByte(value)); |
36 |
|
|
break; |
37 |
|
|
case "sbyte": |
38 |
|
|
binWriter.Write(Convert.ToSByte(value)); |
39 |
|
|
break; |
40 |
|
|
case "uint16": |
41 |
|
|
binWriter.Write(Convert.ToUInt16(value)); |
42 |
|
|
break; |
43 |
|
|
case "int16": |
44 |
|
|
binWriter.Write(Convert.ToInt16(value)); |
45 |
|
|
break; |
46 |
|
|
case "uint32": |
47 |
|
|
binWriter.Write(Convert.ToUInt32(value)); |
48 |
|
|
break; |
49 |
|
|
case "int32": |
50 |
|
|
binWriter.Write(Convert.ToInt32(value)); |
51 |
|
|
break; |
52 |
|
|
case "uint64": |
53 |
|
|
binWriter.Write(Convert.ToUInt64(value)); |
54 |
|
|
break; |
55 |
|
|
case "int64": |
56 |
|
|
binWriter.Write(Convert.ToInt64(value)); |
57 |
|
|
break; |
58 |
|
|
} |
59 |
|
|
ResultsWritten++; |
60 |
|
|
} |
61 |
|
|
catch (Exception ex) |
62 |
william |
401 |
{ |
63 |
william |
445 |
throw ex; |
64 |
william |
401 |
} |
65 |
|
|
} |
66 |
|
|
#endregion |
67 |
|
|
|
68 |
william |
444 |
private void WriteHeader(Guid guid) |
69 |
william |
401 |
{ |
70 |
william |
445 |
try |
71 |
|
|
{ |
72 |
|
|
// write magic: SRD |
73 |
|
|
binWriter.Write(Encoding.UTF8.GetBytes("SRD")); |
74 |
william |
444 |
|
75 |
william |
445 |
if (guid == Guid.Empty) |
76 |
|
|
{ |
77 |
|
|
binWriter.Write((int)1); |
78 |
|
|
} |
79 |
|
|
else |
80 |
|
|
{ |
81 |
|
|
binWriter.Write((int)2); |
82 |
|
|
// related guid - length |
83 |
|
|
binWriter.Write(guid.ToByteArray().Length); |
84 |
|
|
// related guid - data |
85 |
|
|
binWriter.Write(guid.ToByteArray()); |
86 |
|
|
} |
87 |
|
|
// write count (int) |
88 |
|
|
binWriter.Write(ResultCount); |
89 |
|
|
} |
90 |
|
|
catch (Exception ex) |
91 |
william |
444 |
{ |
92 |
william |
445 |
throw ex; |
93 |
william |
444 |
} |
94 |
william |
401 |
} |
95 |
william |
405 |
|
96 |
|
|
protected override void Dispose(bool disposing) |
97 |
william |
445 |
{ |
98 |
|
|
try |
99 |
william |
405 |
{ |
100 |
william |
445 |
base.Dispose(disposing); |
101 |
|
|
// ensure ResultCount and ResultsWritten are equal |
102 |
|
|
if (ResultCount != ResultsWritten) |
103 |
|
|
{ |
104 |
|
|
string message = string.Format("ResultCount does not match ResultsWritten: 0x{0:x8} != 0x{1:x8} -- offset: 0x{2:x8}", ResultCount, ResultsWritten, ResultCount - ResultsWritten); |
105 |
|
|
Logging.logger.Error.WriteLine(message); |
106 |
|
|
throw new InvalidOperationException(message); |
107 |
|
|
} |
108 |
william |
429 |
} |
109 |
william |
445 |
catch (Exception ex) |
110 |
|
|
{ |
111 |
|
|
throw ex; |
112 |
|
|
} |
113 |
william |
405 |
} |
114 |
william |
401 |
} |
115 |
|
|
} |