Skip to content

Commit

Permalink
Fixed a few bugs with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
WarlockD committed Jul 3, 2016
1 parent e720574 commit 2c24e75
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 36 deletions.
18 changes: 17 additions & 1 deletion GMdsam/GMContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 11 additions & 15 deletions GMdsam/GameMaker/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
Expand Down
57 changes: 37 additions & 20 deletions GMdsam/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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":
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2c24e75

Please sign in to comment.