diff --git a/GMdsam/GMContext.cs b/GMdsam/GMContext.cs index 1a09a30..d673cf8 100644 --- a/GMdsam/GMContext.cs +++ b/GMdsam/GMContext.cs @@ -190,7 +190,23 @@ public static void CheckAndSetOutputDirectory(string path=null) // whew, path string is valid lets verify we can write to the directory // } - + public static string FormatDebugOffset(byte[] data, int index) + { + short svalue = BitConverter.ToInt16(data, index); + int ivalue = BitConverter.ToInt32(data, index); + StringBuilder sb = new StringBuilder(); + sb.AppendFormat("Offset=0x{0:X8} ", index); + sb.AppendFormat("Int32(0x{0:X8},0) ", ivalue); + sb.AppendFormat("Int16(0x{0:X4},0) ", svalue); + sb.AppendFormat("Raw("); + for(int i = index;i < (index+4); i++) + { + if (i != 0) sb.Append(','); + sb.AppendFormat("0x{0:X2}",data[i]); + } + sb.Append(")"); + return sb.ToString(); + } static public CancellationToken ct; static public DirectoryInfo outputDirectory = null; static public bool saveChangedDataWin = false; diff --git a/GMdsam/GameMaker/Resources.cs b/GMdsam/GameMaker/Resources.cs index 2b798dd..e695b54 100644 --- a/GMdsam/GameMaker/Resources.cs +++ b/GMdsam/GameMaker/Resources.cs @@ -264,16 +264,23 @@ public static byte[] CopyData() public static void ChangeOffset(byte[] data, uint offset, int value) { if ((offset + 4) > data.Length) Context.FatalError("Cannot change {0} at offset 0x{1:8X}: offset to big", value, offset); + int ioffset = (int)offset; + Context.Message("Changed From {0}", Context.FormatDebugOffset(data, ioffset)); data[offset++] = (byte)value; data[offset++] = (byte)(value >> 8); data[offset++] = (byte)(value >> 0x10); data[offset++] = (byte)(value >> 0x18); + Context.Message("Changed To {0}", Context.FormatDebugOffset(data, ioffset)); + } public static void ChangeOffset(byte[] data, uint offset, short value) { if ((offset + 2) > data.Length) Context.FatalError("Cannot change {0} at offset 0x{1:8X}: offset to big", value, offset); + int ioffset = (int)offset; + Context.Message("Changed From {0}", Context.FormatDebugOffset(data, ioffset)); data[offset++] = (byte)value; data[offset++] = (byte)(value >> 8); + Context.Message("Changed To {0}", Context.FormatDebugOffset(data, ioffset)); } public static bool ChangeVarValue(byte[] data, File.Code code, string name, int from_value, int to_value) { @@ -292,25 +299,14 @@ public static bool ChangeVarValue(byte[] data, File.Code code, string name, int Context.dataWinChanged |= true; Debug.Assert(value.DataOffset != null); int offset = (int)value.DataOffset; - int convert = BitConverter.ToInt32(data, (int)value.DataOffset); if (value.Type == GM_Type.Short) - { - short snew_value = (short)to_value; - data[offset++] = (byte)snew_value; - data[offset++] = (byte)(snew_value >> 8); - } + ChangeOffset(data, (uint)offset, (short)to_value); else - { - data[offset++] = (byte)to_value; - data[offset++] = (byte)(to_value >> 8); - data[offset++] = (byte)(to_value >> 0x10); - data[offset++] = (byte)(to_value >> 0x18); - } - int newConvert = BitConverter.ToInt32(data, (int)value.DataOffset); - context.Message("Changed {0}={1} to {0}={2} at offset 0x{0:8x}", name, from_value, to_value, (int)value.DataOffset); + ChangeOffset(data, (uint)offset, to_value); + context.Message("Changed {0}={1} to {0}={2} at offset 0x{3:X8}", name, from_value, to_value, (int)value.DataOffset); } } - if(!found) context.Message("Could Not find {0}", name); + if(!found) context.Message("Could Not find var '{0}'", name); return found; } static void InternalLoad() diff --git a/GMdsam/Program.cs b/GMdsam/Program.cs index c90fc94..bfcd7da 100644 --- a/GMdsam/Program.cs +++ b/GMdsam/Program.cs @@ -128,6 +128,24 @@ static void GoodExit() static void PatchMode() { + } + static bool TryParseOffset(string s, out uint value) + { + if (string.IsNullOrWhiteSpace(s)) { value = default(uint); return false; } + if (s != null && s.Length > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) + return uint.TryParse(s.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value); + else + return uint.TryParse(s, System.Globalization.NumberStyles.Integer, null, out value); + } + + static bool TryParseHex(string s, out int value) + { + if (string.IsNullOrWhiteSpace(s)) { value = default(int); return false; } + System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer; + if (s != null && s.Length > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) + return int.TryParse(s.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value); + else + return int.TryParse(s, System.Globalization.NumberStyles.Integer, null, out value); } static void Main(string[] args) { @@ -158,25 +176,19 @@ static void Main(string[] args) if (string.IsNullOrWhiteSpace(a)) continue; // does this ever happen? switch (a) { - case "-changeInt": + case "-printOffset": { Context.saveChangedDataWin = true; string offset_name = args.ElementAtOrDefault(i + 1); - string to_value = args.ElementAtOrDefault(i + 2); - i += 2; + i += 1; uint offset_int; - short to_int; - if (!uint.TryParse(offset_name, out offset_int)) Context.FatalError("Cannot parse offset value in -changeInt"); - if (!short.TryParse(to_value, out to_int)) Context.FatalError("Cannot parse short value in -changeInt"); - if (changedData == null) - { - changedData = File.CopyData(); - File.LoadEveything(); - } - File.ChangeOffset(changedData, offset_int, to_int); + if (!TryParseOffset(offset_name, out offset_int)) Context.FatalError("Cannot parse {0} value in -printOffset", offset_name); + Context.Message(Context.FormatDebugOffset(changedData ?? File.DataWinRaw, (int)offset_int)); } break; + + case "-changeInt": case "-changeShort": { Context.saveChangedDataWin = true; @@ -186,14 +198,15 @@ static void Main(string[] args) i += 2; uint offset_int; int to_int; - if (!uint.TryParse(offset_name, out offset_int)) Context.FatalError("Cannot parse offset value in -changeShort"); - if (!int.TryParse(to_value, out to_int)) Context.FatalError("Cannot parse int value in -changeShort"); + if (!TryParseOffset(offset_name, out offset_int)) Context.FatalError("Cannot parse {0} value in -changeShort", offset_name); + if (!TryParseHex(to_value, out to_int)) Context.FatalError("Cannot parse {0} value in -changeShort", to_value); if (changedData == null) { changedData = File.CopyData(); File.LoadEveything(); } - File.ChangeOffset(changedData, offset_int, to_int); + if(a == "-changeShort") File.ChangeOffset(changedData, offset_int, (short)to_int); + else File.ChangeOffset(changedData, offset_int, to_int); } break; case "-changeVar": @@ -205,8 +218,8 @@ static void Main(string[] args) string to_value = args.ElementAtOrDefault(i + 4) ?? ""; int from_int; int to_int; - if (!int.TryParse(from_value, out from_int)) Context.FatalError("Cannot parse from value in -change"); - if (!int.TryParse(to_value, out to_int)) Context.FatalError("Cannot parse from value in -change"); + if (!TryParseHex(from_value, out from_int)) Context.FatalError("Cannot parse {0} value in -change",from_value); + if (!TryParseHex(to_value, out to_int)) Context.FatalError("Cannot parse {0} value in -change", to_value); i += 4; if (changedData == null) @@ -285,11 +298,15 @@ static void Main(string[] args) ErrorContext.StartErrorSystem(); if (Context.saveChangedDataWin) { - using (var file = Context.CreateFileStream("changed_data.win", FileMode.Create, true)) + if (changedData != null) { - file.Write(changedData, 0, changedData.Length); - file.Flush(); + using (var file = Context.CreateFileStream("changed_data.win", FileMode.Create, true)) + { + file.Write(changedData, 0, changedData.Length); + file.Flush(); + } } + } else { File.LoadEveything();