--- trunk/RomCheater/Serialization/SearchResultReader.cs 2012/06/21 05:14:38 401 +++ trunk/RomCheater/Serialization/SearchResultReader.cs 2012/06/21 06:30:33 402 @@ -2,10 +2,106 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using RomCheater.Docking.MemorySearch; namespace RomCheater.Serialization { - public class SearchResultReader + public interface ISearchResultReader { + void CurrentAddress(out uint Address); + uint CurrentAddress(); + void CurrentResult<TValue>(out TValue Value); + TValue CurrentResult<TValue>(); + } + public class SearchResultReader : SerializationReader, ISearchResultReader + { + public SearchResultReader() : base() { ReadHeader(); } + + private void ReadHeader() + { + try + { + // SRD (string) + string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3)); + string SRD = "SRD"; + if (magic != SRD) + { + throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD)); + } + // version (int) + int version = binReader.ReadInt32(); + if (version != 1) + { + throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1}", version, 1)); + } + // resultcount + int resultcount = binReader.ReadInt32(); + if (resultcount == 0) + { + throw new InvalidOperationException(string.Format("Result Count is zero")); + } + ResultCount = resultcount; + } + catch (System.IO.EndOfStreamException) { } + } + #region ISearchResultReader members + public uint CurrentAddress() + { + uint Address = 0; + CurrentAddress(out Address); + return Address; + } + public void CurrentAddress(out uint Address) + { + Address = 0; + try + { + Address = binReader.ReadUInt32(); + } + catch (System.IO.EndOfStreamException) { } + } + public TValue CurrentResult<TValue>() + { + TValue Value = default(TValue); + CurrentResult<TValue>(out Value); + return Value; + } + public void CurrentResult<TValue>(out TValue Value) + { + Value = default(TValue); + try + { + Type t = typeof(TValue); + switch (t.Name.ToLower()) + { + case "byte": + Value = (TValue)Convert.ChangeType(binReader.ReadByte(), typeof(TValue)); + break; + case "sbyte": + Value = (TValue)Convert.ChangeType(binReader.ReadSByte(), typeof(TValue)); + break; + case "uint16": + Value = (TValue)Convert.ChangeType(binReader.ReadUInt16(), typeof(TValue)); + break; + case "int16": + Value = (TValue)Convert.ChangeType(binReader.ReadInt16(), typeof(TValue)); + break; + case "uint32": + Value = (TValue)Convert.ChangeType(binReader.ReadUInt32(), typeof(TValue)); + break; + case "int32": + Value = (TValue)Convert.ChangeType(binReader.ReadInt32(), typeof(TValue)); + break; + case "uint64": + Value = (TValue)Convert.ChangeType(binReader.ReadUInt64(), typeof(TValue)); + break; + case "int64": + Value = (TValue)Convert.ChangeType(binReader.ReadInt64(), typeof(TValue)); + break; + } + } + catch (System.IO.EndOfStreamException) { } + } + #endregion } } |