|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace ReClassNET.MemoryScanner.Comparer |
| 7 | +{ |
| 8 | + public class RegexStringMemoryComparer : IComplexScanComparer |
| 9 | + { |
| 10 | + public ScanCompareType CompareType => ScanCompareType.Equal; |
| 11 | + |
| 12 | + public Regex Pattern { get; } |
| 13 | + |
| 14 | + public Encoding Encoding { get; } |
| 15 | + |
| 16 | + public RegexStringMemoryComparer(string pattern, Encoding encoding) |
| 17 | + { |
| 18 | + Pattern = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled); |
| 19 | + |
| 20 | + Encoding = encoding; |
| 21 | + } |
| 22 | + |
| 23 | + public IEnumerable<ScanResult> Compare(byte[] data, int size) |
| 24 | + { |
| 25 | + var buffer = Encoding.GetString(data, 0, size); |
| 26 | + var bufferArray = buffer.ToCharArray(); |
| 27 | + |
| 28 | + var lastIndex = 0; |
| 29 | + var lastOffset = 0; |
| 30 | + |
| 31 | + var match = Pattern.Match(buffer); |
| 32 | + while (match.Success) |
| 33 | + { |
| 34 | + var byteOffset = Encoding.GetByteCount(bufferArray, lastIndex, match.Index) + lastOffset; |
| 35 | + |
| 36 | + lastIndex = match.Index; |
| 37 | + lastOffset = byteOffset; |
| 38 | + |
| 39 | + yield return new RegexStringScanResult(match.Value, Encoding) |
| 40 | + { |
| 41 | + Address = (IntPtr)byteOffset |
| 42 | + }; |
| 43 | + |
| 44 | + match = match.NextMatch(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public bool CompareWithPrevious(byte[] data, int size, ScanResult previous, out ScanResult result) |
| 49 | + { |
| 50 | + result = null; |
| 51 | + |
| 52 | + var byteOffset = previous.Address.ToInt32(); |
| 53 | + if (byteOffset >= size) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + var buffer = Encoding.GetString(data, byteOffset, size - byteOffset); |
| 59 | + |
| 60 | + var match = Pattern.Match(buffer); |
| 61 | + if (!match.Success) |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + result = new RegexStringScanResult(match.Value, Encoding) |
| 67 | + { |
| 68 | + Address = (IntPtr)byteOffset |
| 69 | + }; |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments