Function in script randomly not working properly

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
XJDHDR
Posts: 15
Joined: 30 Nov 2014, 23:44

Function in script randomly not working properly

30 Jun 2018, 17:19

I have written a script that hex edits a particular game's EXE that uses a slightly modified version of the Hex Editing function that was posted here: https://autohotkey.com/boards/viewtopic.php?t=20524

The script I currently have is here:
Spoiler
My edits involve the function writing the changes it made to the file to a log file followed by a note stating what the line above it should say. Here is an example of this working properly:

Code: Select all

Data written to address 778164: 0x64 replaced with 0x00
	-  Above should say 778164: 0x64 replaced with 0x00
My problem is that I am occasionally seeing segments like this appear in the log file:

Code: Select all

Data written to address 777344: 0x64 replaced with 0x00
	-  Above should say 777344: 0x64 replaced with 0x00

Data written to address 777373: 0x64 replaced with 0x00
	-  Above should say 777373: 0x64 replaced with 0x00

	-  Above should say 777402: 0x64 replaced with 0x00

	-  Above should say 777520: 0x64 replaced with 0x00

	-  Above should say 777688: 0x64 replaced with 0x00

Data written to address 777819: 0x64 replaced with 0x00
	-  Above should say 777819: 0x64 replaced with 0x00

Data written to address 777888: 0x64 replaced with 0x00
	-  Above should say 777888: 0x64 replaced with 0x00
If I look at the EXE in a hex editor afterwards, I see that the addresses with a "Data written to address" line were edited by my script whereas the addresses without that line were not edited. This would seem to indicate that the HexEdit function is either not executing or is returning before it reaches the point where it edits the file. Strangely enough, if I then run the script a second time, those addresses now get edited by my script:

Code: Select all

Data written to address 777344: 0x0 replaced with 0x00
	-  Above should say 777344: 0x64 replaced with 0x00

Data written to address 777373: 0x0 replaced with 0x00
	-  Above should say 777373: 0x64 replaced with 0x00

Data written to address 777402: 0x64 replaced with 0x00
	-  Above should say 777402: 0x64 replaced with 0x00

Data written to address 777520: 0x64 replaced with 0x00
	-  Above should say 777520: 0x64 replaced with 0x00

Data written to address 777688: 0x64 replaced with 0x00
	-  Above should say 777688: 0x64 replaced with 0x00

Data written to address 777819: 0x0 replaced with 0x00
	-  Above should say 777819: 0x64 replaced with 0x00

Data written to address 777888: 0x0 replaced with 0x00
	-  Above should say 777888: 0x64 replaced with 0x00
Does anyone have any ideas about why this function is randomly deciding to not work and what I can do to fix it? Help will be greatly appreciated.

And for bonus points, if anyone can tell me how I can get lines like: "777888: 0x0 replaced" to say "777888: 0x00 replaced" instead, I would be even more grateful.
Edit: NVM, I figured it out.
XJDHDR
Posts: 15
Joined: 30 Nov 2014, 23:44

Re: Function in script randomly not working properly

16 Jul 2018, 04:59

I'm now having a new problem with my script that is preventing me from continued testing of the above problem I'm having. I hope someone can help me with it.

Basically, the script I posted in my first post opened and closed the game's EXE for hex editing every time the script's HexEdit function was called. I want to edit my script so that it will only open the file once, perform all the required hex editing then close and save the changes after the last edit has been made. I'm also making this change because I suspect that the following block is causing the HexEdit function to randomly fail (specifically, I suspect that the FileOpen(Filename, "rw") is occasionally failing to open the file):

Code: Select all

If Mod(StrLen(Nibbles), 2) Or RegExMatch(Nibbles, "[^0-9a-fA-F]") Or Not (f := FileOpen(Filename, "rw"))
        Return, False
The new problem I'm having is that after I select the location of the EXE I want hex edited, the message box I coded on line 59 appears which says "183 G:\Games\GOG.com\Zeus\Zeus Widescreen fixes pack - 25-03-2018\Other - Source files for creating above\Zeus.exe could not be opened and/or edited. Please ensure that ..." This message box should only appear if there is an error code (which would be the first number in the message box) that was generated by the FileOpen() command. I searched for the 183 error code and the info I found said that the code means "Cannot create a file when that file already exists". Does anyone have any ideas for why my use of the FileOpen command is failing?

Here is the script I currently have:

Code: Select all

#NoEnv

If A_Args.Length() = 0
{
	FileDelete, %A_ScriptDir%\Zeus - Fix Animation speed.log
	SelectZeusLocation:
	FileSelectFolder, sZeusLocation, ::{20d04fe0-3aea-1069-a2d8-08002b30309d}, 2, Please select the location you installed Zeus. This is where Zeus.exe is located.
	If %ErrorLevel% = 1
		ExitApp
	sZeusExe := sZeusLocation . "\Zeus.exe"
	If TestZeuslocation(sZeusExe) = False
		GoTo, SelectZeusLocation
	PerformHexEditing(sZeusExe)
}
Else
{
	FileDelete, %A_ScriptDir%\Zeus - Fix Animation speed.log
	For n, arParameter in A_Args
	{
		Loop Files, %arParameter%, FD
		{
			sParameter := A_LoopFileFullPath
		}
		If InStr(sParameter, "\Zeus.exe")
		{
			PerformHexEditing(sParameter)
			FileAppend, `n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,
		}
		Else
			FileAppend, Zeus.exe not found in parameter: %sParameter%`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,
	}
}

MsgBox, Patching has been completed.
ExitApp


TestZeuslocation(sPassedEXELocation)
{
	If Not FileExist(sPassedEXELocation)
	{
		MsgBox, 21, Zeus.exe not found, Zeus.exe was not found in the specified location.`nPlease select the correct location that Zeus is installed.,
		IfMsgBox, Retry
			Return, False
		Else IfMsgBox, Cancel
			ExitApp
	}
	Return, True
}


PerformHexEditing(sPassedExe)
{
	Global objFile
	objFile := FileOpen(sPassedExe, "rw")
	iLastError := A_LastError
	If iLastError > 0
	{
		MsgBox, %iLastError% %sPassedExe% could not be opened and/or edited. Please ensure that this program has write permission to the location where Zeus is installed, by either moving Zeus.exe to a location where you have write permission or running this program with admin privileges.
		objFile.Close()
        	ExitApp
	}
	HexEdit(objFile, "00", 197640)
	FileAppend, %A_Tab%-  Above should say 197640: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 735582)
	FileAppend, %A_Tab%-  Above should say 735582: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 735635)
	FileAppend, %A_Tab%-  Above should say 735635: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 742979)
	FileAppend, %A_Tab%-  Above should say 742979: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 744174)
	FileAppend, %A_Tab%-  Above should say 744174: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 744934)
	FileAppend, %A_Tab%-  Above should say 744934: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 747008)
	FileAppend, %A_Tab%-  Above should say 747008: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 747960)
	FileAppend, %A_Tab%-  Above should say 747960: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 758231)
	FileAppend, %A_Tab%-  Above should say 758231: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 760499)
	FileAppend, %A_Tab%-  Above should say 760499: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 760572)
	FileAppend, %A_Tab%-  Above should say 760572: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 760701)
	FileAppend, %A_Tab%-  Above should say 760701: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 761266)
	FileAppend, %A_Tab%-  Above should say 761266: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 761864)
	FileAppend, %A_Tab%-  Above should say 761864: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 764961)
	FileAppend, %A_Tab%-  Above should say 764961: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 764978)
	FileAppend, %A_Tab%-  Above should say 764978: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 764995)
	FileAppend, %A_Tab%-  Above should say 764995: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 765012)
	FileAppend, %A_Tab%-  Above should say 765012: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 766453)
	FileAppend, %A_Tab%-  Above should say 766453: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 766850)
	FileAppend, %A_Tab%-  Above should say 766850: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 766950)
	FileAppend, %A_Tab%-  Above should say 766950: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 766988)
	FileAppend, %A_Tab%-  Above should say 766988: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767026)
	FileAppend, %A_Tab%-  Above should say 767026: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767064)
	FileAppend, %A_Tab%-  Above should say 767064: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767102)
	FileAppend, %A_Tab%-  Above should say 767102: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767140)
	FileAppend, %A_Tab%-  Above should say 767140: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767178)
	FileAppend, %A_Tab%-  Above should say 767178: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767213)
	FileAppend, %A_Tab%-  Above should say 767213: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767248)
	FileAppend, %A_Tab%-  Above should say 767248: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767283)
	FileAppend, %A_Tab%-  Above should say 767283: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767380)
	FileAppend, %A_Tab%-  Above should say 767380: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767406)
	FileAppend, %A_Tab%-  Above should say 767406: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767432)
	FileAppend, %A_Tab%-  Above should say 767432: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767461)
	FileAppend, %A_Tab%-  Above should say 767461: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 767575)
	FileAppend, %A_Tab%-  Above should say 767575: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 774962)
	FileAppend, %A_Tab%-  Above should say 774962: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 774986)
	FileAppend, %A_Tab%-  Above should say 774986: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 775091)
	FileAppend, %A_Tab%-  Above should say 775091: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777315)
	FileAppend, %A_Tab%-  Above should say 777315: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777344)
	FileAppend, %A_Tab%-  Above should say 777344: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777373)
	FileAppend, %A_Tab%-  Above should say 777373: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777402)
	FileAppend, %A_Tab%-  Above should say 777402: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777520)
	FileAppend, %A_Tab%-  Above should say 777520: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777688)
	FileAppend, %A_Tab%-  Above should say 777688: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777819)
	FileAppend, %A_Tab%-  Above should say 777819: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777888)
	FileAppend, %A_Tab%-  Above should say 777888: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 777957)
	FileAppend, %A_Tab%-  Above should say 777957: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778026)
	FileAppend, %A_Tab%-  Above should say 778026: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778095)
	FileAppend, %A_Tab%-  Above should say 778095: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778164)
	FileAppend, %A_Tab%-  Above should say 778164: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778233)
	FileAppend, %A_Tab%-  Above should say 778233: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778302)
	FileAppend, %A_Tab%-  Above should say 778302: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778371)
	FileAppend, %A_Tab%-  Above should say 778371: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 778440)
	FileAppend, %A_Tab%-  Above should say 778440: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 785476)
	FileAppend, %A_Tab%-  Above should say 785476: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 785913)
	FileAppend, %A_Tab%-  Above should say 785913: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 786248)
	FileAppend, %A_Tab%-  Above should say 786248: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 796370)
	FileAppend, %A_Tab%-  Above should say 796370: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 796481)
	FileAppend, %A_Tab%-  Above should say 796481: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 796900)
	FileAppend, %A_Tab%-  Above should say 796900: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 796933)
	FileAppend, %A_Tab%-  Above should say 796933: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 797657)
	FileAppend, %A_Tab%-  Above should say 797657: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 801401)
	FileAppend, %A_Tab%-  Above should say 801401: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 820246)
	FileAppend, %A_Tab%-  Above should say 820246: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 820477)
	FileAppend, %A_Tab%-  Above should say 820477: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 826861)
	FileAppend, %A_Tab%-  Above should say 826861: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 827009)
	FileAppend, %A_Tab%-  Above should say 827009: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 831960)
	FileAppend, %A_Tab%-  Above should say 831960: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 831985)
	FileAppend, %A_Tab%-  Above should say 831985: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,

	HexEdit(objFile, "00", 832064)
	FileAppend, %A_Tab%-  Above should say 832064: 0x64 replaced with 0x00`n`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,
	
	objFile.Close()
	Return
}


;	Many thanks to Wolf_II of the AutoHotkey forums for the HexEdit function below:
;	https://autohotkey.com/boards/viewtopic.php?t=20524
;
;-------------------------------------------------------------------------------
HexEdit(Filename, Nibbles = "", Pos = 1) { ; write Nibbles at pos
;-------------------------------------------------------------------------------
    ; If Filename does not exist, it will be created.
    ; Unacceptable Nibbles will be rejected.
    ; If Pos is larger than file length
    ;   Filename's length will be adjusted accordingly.
    ;   all undefined bytes in the file will be set to zero.
    ;---------------------------------------------------------------------------
    ; returns 1 if successful, 0 otherwise.
    ;---------------------------------------------------------------------------
;	f := FileOpen(Filename, "rw")
	objFile.Seek(Pos - 1)
	sOldCharacters := SubStr("00" . DecToBase(objFile.ReadChar(2), 16),-1)
	objFile.Seek(Pos - 1)
    If (Pos != 1) And (Nibbles = "")
        objFile.WriteChar(0)
	
	sCurNibblesVal := Nibbles
    While Nibbles != ""
        objFile.WriteChar("0x" SubStr(Nibbles, 1, 2))
        , Nibbles := SubStr(Nibbles, 3)
	FileAppend, Data written to address %Pos%: 0x%sOldCharacters% replaced with 0x%sCurNibblesVal%`n, %A_ScriptDir%\Zeus - Fix Animation speed.log,
;    f.Close()

    Return, True
}

DecToBase(n, Base)
{
	Static U := A_IsUnicode ? "w" : "a"
	VarSetCapacity(S,65,0)
	DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
	Return, S
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 120 guests