public static Patch TryReadPatch(string patchFile, bool activeOnly) { try { UserFeedback.Info("Reading the patch file '{0}'", (object)patchFile); return(Patcher.ReadPatch(patchFile, activeOnly)); } catch (Exception ex) { UserFeedback.Error(ex); return(new Patch() { PatchFilepath = patchFile }); } }
public static void WritePatch(Patch patch, string filePath) { List <string> stringList = new List <string>() { "[header]", string.Format("size={0}", (object)patch.FileSize), string.Format("md5={0}", (object)patch.Md5), string.Format("version={0}", (object)patch.Version), string.Format("drspos={0:X8}", (object)patch.InterfaceDrsPosition), "[data]" }; IEnumerable <string> collection = Patcher.StringifyPatch(patch); stringList.AddRange(collection); File.WriteAllLines(filePath, stringList.ToArray()); }
public static Patch ConvertPatch(byte[] rootExe, byte[] otherExe, Patch patch) { return(new Patch() { InterfaceDrsPosition = Patcher.FindComparablePos(rootExe, otherExe, patch.InterfaceDrsPosition), Items = (IEnumerable <Item>)patch.Items.Select <Item, Item>((Func <Item, Item>)(item => new Item() { Pos = Patcher.FindComparablePos(rootExe, otherExe, item.Pos), Comments = item.Comments, ReferenceValue = item.ReferenceValue, Parameter = item.Parameter, Type = item.Type, OriginalPos = item.Pos })).ToArray <Item>() }); }
private static int FindComparablePos(byte[] correctExe, byte[] otherExe, int pos) { for (int length = 4; length < 25; length += 4) { byte[] window = new byte[length]; int num1 = 0; if (num1 + pos < 0) { num1 = -pos; } if (num1 + length >= correctExe.Length) { num1 = correctExe.Length - (pos + length); } for (int index = 0; index < length; ++index) { window[index] = correctExe[index + pos + num1]; } int[] array1 = Patcher.FindWindow(otherExe, window).ToArray <int>(); if (array1.Length == 1) { return(array1[0] - num1); } if (array1.Length == 0) { if (length == 8 && num1 == 0) { int num2 = -4; for (int index = 0; index < length; ++index) { window[index] = correctExe[index + pos + num2]; } int[] array2 = Patcher.FindWindow(otherExe, window).ToArray <int>(); if (array2.Length == 1) { return(array2[0] - num2); } } UserFeedback.Warning(string.Format("Found no matches for block {0:X8} {1}", (object)pos, (object)length)); return(-pos); } } UserFeedback.Warning("Found too many matches for block {0:X8}", (object)pos); return(-pos); }
public static Patch ReadPatch(string patchFile, bool activeOnly) { try { List <Item> objList = new List <Item>(1024); string[] strArray1 = Patcher.ReadAllResourceLines(Patcher.getPatchFromProperties(patchFile)); List <string> stringList = new List <string>(strArray1.Length); Patch patch = new Patch() { PatchFilepath = patchFile }; foreach (string str in strArray1) { if (str.StartsWith("size=")) { patch.FileSize = int.Parse(str.Substring(5)); } else if (str.StartsWith("md5=")) { patch.Md5 = str.Substring(4); } else if (str.StartsWith("version=")) { patch.Version = str.Substring(8); } else if (str.StartsWith("drspos=")) { patch.InterfaceDrsPosition = int.Parse(str.Substring(7), NumberStyles.HexNumber); } else if (str.StartsWith("x1drspos=")) { patch.InterfaceX1DrsPosition = int.Parse(str.Substring(9), NumberStyles.HexNumber); } else if (!str.StartsWith("[") && !str.StartsWith("#")) { string[] strArray2 = str.Split(new char[1] { '|' }, 2); string[] strArray3 = strArray2[0].Split(new char[2] { ' ', '\t' }, 4, StringSplitOptions.RemoveEmptyEntries); if (activeOnly) { if (strArray3.Length < 3 || !strArray3[2].Equals("H") && !strArray3[2].Equals("V") && (!strArray3[2].Equals("dH") && !strArray3[2].Equals("dV")) && !strArray3[2].Equals("HV")) { continue; } } else if (strArray3.Length < 2) { continue; } Item obj = new Item() { Pos = int.Parse(strArray3[0], NumberStyles.HexNumber), ReferenceValue = int.Parse(strArray3[1]), Type = "", Comments = "" }; if (strArray3.Length > 2) { obj.Type = strArray3[2]; } if (strArray3.Length > 3) { string[] strArray4 = strArray3[3].Split(new char[2] { ' ', '\t' }, 2, StringSplitOptions.RemoveEmptyEntries); obj.Comments = !int.TryParse(strArray4[0], out obj.Parameter) ? strArray3[3].TrimEnd() : (strArray4.Length == 1 ? "" : strArray4[1].TrimEnd()); } if (!activeOnly && strArray2.Length > 1) { obj.Asm = strArray2[1].Trim(); } obj.OriginalPos = obj.Pos; objList.Add(obj); stringList.Add(str); } } patch.Items = (IEnumerable <Item>)objList; return(patch); } catch (Exception ex) { throw new FatalError("Couldn't read or parse patch file"); } }