seijuro475:
Thanks for the reply.
I did not have that line near the top of the script, i added it in but now i recieved a different error.
Line 83 (File “C:\Users\Robert\Desktop\New
folder\sloth86_emg_swap_script.au3”):
MsgBox(4096, Default, “Info on the emg block you just replaced:”
@CRLF "Begin: " & $BeginBlock & @CRLF & "End: " & $EndBlock &
@CRLF & "Length in hex: " & Hex($OldEMGSize))
MsgBox(4096, Default, ^ERROR
Error: Error in expression.
Any idea on what it could be. Thanks
Dunno. Maybe I uploaded an outdated script. Try this script instead, it’s working for me:
#include <WinAPI.au3>
#include <GUIConstantsEX.au3>
#include <String.au3>
; Files:
Local Const $EmoFilePath = @ScriptDir & "\DSM_01.obj.emo" ;EMO file you want to swap an EMG into
Local Const $EmgFilePath = @ScriptDir & "\gold.emg" ;raw dump of the EMG you want to swap in
Local Const $ObjectNumberOfInterest = 9; the number of the EMG you want to swap
Local Const $NewFilePath = @ScriptDir & "
ew.emo" ;choose a name of the output EMO (will overwrite the original EMO file if you put in the same name)
#Region ; TAKING DEFAULT VALUES
Local $EMGSize = FileGetSize($EmgFilePath)
Local $OGEMOSize = FileGetSize($EmoFilePath)
Local Const $Offset_OffsetOf2NDPART[2] = [0x010, 4]
Local $OffsetOf2NDPART = _HexRead($EmoFilePath, $Offset_OffsetOf2NDPART[0], $Offset_OffsetOf2NDPART[1])
Local $Actual_OffsetOf2NDPART = _ReverseHex($OffsetOf2NDPART, 0)
Local Const $Offset_EMGCount[2] = [0x020, 1]
Local Const $EMGCount = _HexToDecimal(_HexRead($EmoFilePath, $Offset_EMGCount[0], $Offset_EMGCount[1]))
Local $ListOfOffsetsOfEMGs[$EMGCount] = [0]
Local $Actual_ListOfOffsetsOfEMGs[$EMGCount] = [0]
For $i = 0 to $EMGCount-1
Local $CurrentPosition = "0x" & _DecimalToHex(_HexToDecimal("0x028") + 4*$i)
$ListOfOffsetsOfEMGs[$i] = _HexRead($EmoFilePath, $CurrentPosition, 4)
$Actual_ListOfOffsetsOfEMGs[$i] = _ReverseHex($ListOfOffsetsOfEMGs[$i],30)
Next
Local $Offset_OffsetOfOffsetsOfEMGNames[2] = [0x24, 4]
Local $OffsetOfOffsetsOfEMGNames = _HexRead($EmoFilePath, $Offset_OffsetOfOffsetsOfEMGNames[0], $Offset_OffsetOfOffsetsOfEMGNames[1])
Local $Actual_OffsetOfOffsetsOfEMGNames = _ReverseHex($OffsetOfOffsetsOfEMGNames,20)
Local $OffsetsOfEMGNames[$EMGCount] = [0]
For $i = 0 to $EMGCount-1
$CurrentPosition = "0x" & _DecimalToHex(_HexToDecimal($Actual_OffsetOfOffsetsOfEMGNames) + 4*$i)
$OffsetsOfEMGNames[$i] = _HexRead($EmoFilePath, $CurrentPosition, 4)
Next
Local $Actual_OffsetsOfEMGNames[$EMGCount] = [0]
For $i = 0 to $EMGCount-1
$Actual_OffsetsOfEMGNames[$i] = _ReverseHex($OffsetsOfEMGNames[$i],20)
Next
Local $EMGNames[$EMGCount] = [0]
Local $EMGNamesInHex[$EMGCount] = [0]
For $i = 0 to $EMGCount-1
If $i = $EMGCount-1 Then
$LengthOfNameInDec = _HexToDecimal($Actual_OffsetOf2NDPART) - _HexToDecimal($Actual_OffsetsOfEMGNames[$i])
$EMGNamesInHex[$i] = _HexRead($EmoFilePath, $Actual_OffsetsOfEMGNames[$i],4)
Else
$LengthOfNameInDec = _HexToDecimal($Actual_OffsetsOfEMGNames[$i+1]) - _HexToDecimal($Actual_OffsetsOfEMGNames[$i])
$EMGNamesInHex[$i] = _HexRead($EmoFilePath, $Actual_OffsetsOfEMGNames[$i],$LengthOfNameInDec)
EndIf
$EMGNames[$i] = BinaryToString($EMGNamesInHex[$i])
Next
If $ObjectNumberOfInterest = $EMGCount Then
Local $OldEMGSize = _HexToDecimal($Actual_OffsetOfOffsetsOfEMGNames) - _HexToDecimal($Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest-1])
Local $ChangeInEMGSize = $EMGSize - $OldEMGSize
Else
Local $OldEMGSize = _HexToDecimal($Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest]) - _HexToDecimal($Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest-1])
Local $ChangeInEMGSize = $EMGSize - $OldEMGSize
EndIf
#Endregion
Local $RBlockLocation
If $ObjectNumberOfInterest = $EMGCount Then
$RBlockLocation = $Actual_OffsetOf2NDPART
Else
$RBlockLocation = $Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest]
EndIf
Local $LBlockLocation = $Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest-1]
#Region ; Message box
Local $BeginBlock = Hex($Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest-1])
If $ObjectNumberOfInterest = $EMGCount Then
Local $EndBlock = _DecimalToHex(_HexToDecimal($Actual_OffsetOfOffsetsOfEMGNames) - 1)
Else
Local $EndBlock = _DecimalToHex(_HexToDecimal($Actual_ListOfOffsetsOfEMGs[$ObjectNumberOfInterest]) - 1)
EndIf
MsgBox(4096, Default, "Info on the emg block you just replaced:" & @CRLF & "Begin: " & $BeginBlock & @CRLF & "End: " & $EndBlock & @CRLF & "Length in hex: " & Hex($OldEMGSize))
#EndRegion
#Region ;Changing Values
$Actual_OffsetOf2NDPART = "0x" & _DecimalToHex(_HexToDecimal($Actual_OffsetOf2NDPART) + $ChangeInEMGSize)
$OffsetOf2NDPART = _ReverseHexTwo($Actual_OffsetOf2NDPART,0)
For $i = $ObjectNumberOfInterest to $EMGCount-1
Local $CurrentOffset = $Actual_ListOfOffsetsOfEMGs[$i]
$Actual_ListOfOffsetsOfEMGs[$i] = "0x" & _DecimalToHex(_HexToDecimal($CurrentOffset) + $ChangeInEMGSize)
$Actual_ListOfOffsetsOfEMGs[$i] = _FixHex8Digits($Actual_ListOfOffsetsOfEMGs[$i])
$ListOfOffsetsOfEMGs[$i] = _ReverseHexTwo($Actual_ListOfOffsetsOfEMGs[$i], 30)
Next
Local Const $Old_Actual_OffsetOfOffsetsOfEMGNames = $Actual_OffsetOfOffsetsOfEMGNames
$Actual_OffsetOfOffsetsOfEMGNames = "0x" & _DecimalToHex(_HexToDecimal($Actual_OffsetOfOffsetsOfEMGNames) + $ChangeInEMGSize)
$OffsetOfOffsetsOfEMGNames = _ReverseHexTwo($Actual_OffsetOfOffsetsOfEMGNames,20)
For $i = $ObjectNumberOfInterest to $EMGCount-1
Local $CurrentOffset = $Actual_OffsetsOfEMGNames[$i]
$Actual_OffsetsOfEMGNames[$i] = "0x" & _DecimalToHex(_HexToDecimal($CurrentOffset) + $ChangeInEMGSize)
$Actual_OffsetsOfEMGNames[$i] = _FixHex8Digits($Actual_OffsetsOfEMGNames[$i])
$OffsetsOfEMGNames[$i] = _ReverseHexTwo($Actual_OffsetsOfEMGNames[$i], 20)
Next
#Endregion
#Region ; Changing Offset References in EMO
;create emo File
Local Const $OGData = _HexRead($EmoFilePath, 0x0, $OGEMOSize)
$NewFileID = FileOpen($NewFilePath, 14)
FileWrite($NewFileID, $OGData)
FileClose($NewFileID)
; change info
_HexWrite($NewFilePath, $Offset_OffsetOf2NDPART[0], Binary($OffsetOf2NDPART))
_HexWrite($NewFilePath, $Offset_OffsetOfOffsetsOfEMGNames[0], Binary($OffsetOfOffsetsOfEMGNames))
For $i = $ObjectNumberOfInterest to $EMGCount-1
$CurrentPosition = "0x" & _DecimalToHex(_HexToDecimal("0x028") + 4*$i)
_HexWrite($NewFilePath, $CurrentPosition , Binary($ListOfOffsetsOfEMGs[$i]))
Next
For $i = $ObjectNumberOfInterest to $EMGCount-1
$CurrentPosition = "0x" & _DecimalToHex(_HexToDecimal($Old_Actual_OffsetOfOffsetsOfEMGNames) + 4*$i)
_HexWrite($NewFilePath, $CurrentPosition , Binary($OffsetsOfEMGNames[$i]))
Next
#EndRegion
#Region ;Paste EMG over old
Local $LBlockSize = _HexToDecimal($LBlockLocation)
Local $RBlockSize = $OGEMOSize - _HexToDecimal($RBlockLocation)
Local $LBlock = _HexRead($NewFilePath, 0x0, $LBlockSize)
Local $RBlock = _HexRead($NewFilePath, $RBlockLocation, $RBlockSize)
Local $NewEMGData = _HexRead($EmgFilePath, 0x0, $EMGSize)
FileOpen($NewFilePath, 14)
FileWrite($NewFilePath, $LBlock & $NewEMGData & $RBlock)
FileClose($NewFilePath)
#EndRegion
#Region ;**** HexEdit Functions
Func _HexWrite($FilePath, $Offset, $BinaryValue)
Local $Buffer, $ptr, $bLen, $fLen, $hFile, $Result, $Written
;## Parameter Checks
If Not FileExists($FilePath) Then Return SetError(1, @error, 0)
$fLen = FileGetSize($FilePath)
If $Offset > $fLen Then Return SetError(2, @error, 0)
If Not IsBinary($BinaryValue) Then Return SetError(3, @error, 0)
$bLen = BinaryLen($BinaryValue)
If $bLen > $Offset + $fLen Then Return SetError(4, @error, 0)
;## Place the supplied binary value into a dll structure.
$Buffer = DllStructCreate("byte[" & $bLen & "]")
DllStructSetData($Buffer, 1, $BinaryValue)
If @error Then Return SetError(5, @error, 0)
$ptr = DllStructGetPtr($Buffer)
;## Open File
$hFile = _WinAPI_CreateFile($FilePath, 2, 4, 0)
If $hFile = 0 Then Return SetError(6, @error, 0)
;## Move file pointer to offset location
$Result = _WinAPI_SetFilePointer($hFile, $Offset)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return SetError(7, $err, 0)
EndIf
;## Write new Value
$Result = _WinAPI_WriteFile($hFile, $ptr, $bLen, $Written)
$err = @error
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return SetError(8, $err, 0)
EndIf
;## Close File
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return SetError(9, @error, 0)
EndFunc
Func _HexRead($FilePath, $Offset, $Length)
Local $Buffer, $ptr, $fLen, $hFile, $Result, $Read, $err, $Pos
;## Parameter Checks
If Not FileExists($FilePath) Then Return SetError(1, @error, 0)
$fLen = FileGetSize($FilePath)
If $Offset > $fLen Then Return SetError(2, @error, 0)
If $fLen < $Offset + $Length Then Return SetError(3, @error, 0)
;## Define the dll structure to store the data.
$Buffer = DllStructCreate("byte[" & $Length & "]")
$ptr = DllStructGetPtr($Buffer)
;## Open File
$hFile = _WinAPI_CreateFile($FilePath, 2, 2, 0)
If $hFile = 0 Then Return SetError(5, @error, 0)
;## Move file pointer to offset location
$Pos = $Offset
$Result = _WinAPI_SetFilePointer($hFile, $Pos)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return SetError(6, $err, 0)
EndIf
;## Read from file
$Read = 0
$Result = _WinAPI_ReadFile($hFile, $ptr, $Length, $Read)
$err = @error
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return SetError(7, $err, 0)
EndIf
;## Close File
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return SetError(8, @error, 0)
;## Return Data
$Result = DllStructGetData($Buffer, 1)
Return $Result
EndFunc
Func _HexSearch($FilePath, $BinaryValue, $StartOffset = Default)
Local $Buffer, $ptr, $hFile, $Result, $Read, $SearchValue, $Pos, $BufferSize = 2048
;## Parameter Defaults
If $StartOffset = Default Then $StartOffset = 0
;## Parameter Checks
If Not FileExists($FilePath) Then Return SetError(1, @error, 0)
$fLen = FileGetSize($FilePath)
If $StartOffset > $fLen Then Return SetError(2, @error, 0)
If Not IsBinary($BinaryValue) Then Return SetError(3, @error, 0)
If Not IsNumber($StartOffset) Then Return SetError(4, @error, 0)
;## Prep the supplied binary value for search
$SearchValue = BinaryToString($BinaryValue)
;## Define the dll structure to store the data.
$Buffer = DllStructCreate("byte[" & $BufferSize & "]")
$ptr = DllStructGetPtr($Buffer)
;## Open File
$hFile = _WinAPI_CreateFile($FilePath, 2, 2, 1)
If $hFile = 0 Then Return SetError(5, @error, 0)
;## Move file pointer to offset location
$Result = _WinAPI_SetFilePointer($hFile, $StartOffset)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return SetError(5, $err, 0)
EndIf
;## Track the file pointer's position
$Pos = $StartOffset
;## Start Search Loop
While True
;## Read from file
$Read = 0
$Result = _WinAPI_ReadFile($hFile, $ptr, $BufferSize, $Read)
$err = @error
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return SetError(6, $err, 0)
EndIf
;## Prep read data for search
$Result = DllStructGetData($Buffer, 1)
$Result = BinaryToString($Result)
;## Search the read data for first match
$Result = StringInStr($Result, $SearchValue)
If $Result > 0 Then ExitLoop
;## Test for EOF and return -1 to signify value was not found
If $Read < $BufferSize Then
_WinAPI_CloseHandle($hFile)
Return -1
EndIf
;## Value not found, Continue Tracking file pointer's position
$Pos += $Read
WEnd
;## Close File
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return SetError(7, @error, 0)
;## Calculate the offset and return
$Result = $Pos + $Result - 1
Return $Result
EndFunc
Func _HexToDecimal($hx_hex)
Const $HX_REF="0123456789ABCDEF"
If StringLeft($hx_hex, 2) = "0x" Then $hx_hex = StringMid($hx_hex, 3)
If StringIsXDigit($hx_hex) = 0 Then
SetError(1)
MsgBox(0,"Error","Wrong input, try again ...")
Return ""
EndIf
Local $ret="", $hx_count=0, $hx_array = StringSplit($hx_hex, ""), $Ii, $hx_tmp
For $Ii = $hx_array[0] To 1 Step -1
$hx_tmp = StringInStr($HX_REF, $hx_array[$Ii]) - 1
$ret += $hx_tmp * 16 ^ $hx_count
$hx_count += 1
Next
Return $ret
EndFunc
Func _DecimalToHex($hx_dec, $hx_length = 21)
If IsInt($hx_dec) = 0 Then
SetError(1)
MsgBox(0,"Error","Wrong input, try again ...")
Return ""
EndIf
Local $ret = "", $Ii, $hx_tmp, $hx_max
If $hx_dec < 4294967296 Then
If $hx_length < 9 Then Return Hex($hx_dec, $hx_length)
If $hx_length = 21 Then
$ret = Hex($hx_dec)
While StringLeft($ret, 1) = "0"
$ret = StringMid($ret, 2)
WEnd
Return $ret
EndIf
EndIf
For $Ii = $hx_length - 1 To 0 Step -1
$hx_max = 16 ^ $Ii - 1
If $ret = "" And $hx_length = 21 And $hx_max > $hx_dec Then ContinueLoop
$hx_tmp = Int($hx_dec/($hx_max+1))
If $ret = "" And $hx_length = 21 And $Ii > 0 And $hx_tmp = 0 Then ContinueLoop
$ret &= StringMid($HX_REF, $hx_tmp+1, 1)
$hx_dec -= $hx_tmp * ($hx_max + 1)
Next
$ret=String($ret)
If $hx_length < 21 And StringLen($ret) < $hx_length Then SetError(1)
Return $ret
EndFunc
#EndRegion
#Region ;Functions
;Fix Hex to 8 digits
Func _FixHex8Digits($InHex)
$HexString = Hex($InHex)
$CharMissing = 8 - StringLen($InHex)
If StringLen($HexString) < 8 Then
For $i = 1 to $CharMissing
$HexString = "0" & $HexString
Next
EndIf
$OutHex = "0x" & $HexString
Return $OutHex
EndFunc
;Fix Hex to 4 digits
Func _FixHex4Digits($InHex)
$HexString = Hex($InHex)
$OutHex = "0x" & StringMid($HexString, 5, 4)
Return $OutHex
EndFunc
;Reverses a 8 digit Hex and adds offset
Func _ReverseHex($InHex, $AddPlaces)
$AddPlacesFix = "0x000000" & $AddPlaces
$InHex = _FixHex8Digits($InHex)
$InHexString = Hex($InHex)
$NewHexString = StringMid($InHexString,7,2) & StringMid($InHexString,5,2) & StringMid($InHexString,3,2) & StringMid($InHexString,1,2)
$BackToHex = "0x" & $NewHexString
$HexWithAddedPlacesInDec = _HexToDecimal($BackToHex) + _HexToDecimal($AddPlacesFix)
$BackToHexAgain = "0x" & _DecimalToHex($HexWithAddedPlacesInDec)
$BackToHexAgain = _FixHex8Digits($BackToHexAgain)
Return $BackToHexAgain
EndFunc
;Reverses a 4 digit Hex and adds offset
Func _ReverseSmallHex($InHex, $AddPlaces)
$InHexString = Hex($InHex)
While StringLen($InHexString) < 8
$InHexString = "0" & $InHexString
WEnd
$NewHexString2 = StringMid($InHexString,7,2) & StringMid($InHexString,5,2)
$BackToHex = "0x" & $NewHexString2
Return $BackToHex
EndFunc
;2nd Reversal
Func _ReverseHexTwo($InHex, $SubPlaces)
$SubPlacesFix = "0x000000" & $SubPlaces
$InHexSubPlacesInDec = _HexToDecimal($InHex) - _HexToDecimal($SubPlacesFix)
$NewHex = "0x" & _DecimalToHex($InHexSubPlacesInDec)
$NewHex = _FixHex8Digits($NewHex)
$NewHexString = Hex($NewHex)
$RevHexString = StringMid($NewHexString,7,2) & StringMid($NewHexString,5,2) & StringMid($NewHexString,3,2) & StringMid($NewHexString,1,2)
$RevHex = "0x" & $RevHexString
Return $RevHex
EndFunc
;Hex insert (not overwrite)
Func _HexInsert($InFile, $Offset, $InHex, $OutFile) ;$InHex is in "0x" form
$OGFileSize = FileGetSize($InFile)
$LeftBlockSize = _HexToDecimal($Offset)
$DisplacedBlockSize = $OGFileSize - $LeftBlockSize
;MsgBox(4096, Default, $LeftBlockSize)
$DisplacedBlock = _HexRead($InFile, $Offset, $DisplacedBlockSize) ;is in binary
$LeftBlock = _HexRead($InFile, 0x0, $LeftBlockSize) ;is in binary
FileOpen($OutFile, 14)
FileWrite($OutFile, $LeftBlock & $InHex & $DisplacedBlock)
FileClose($OutFile)
EndFunc
Sorry but I’m very much an amateur when it comes to programming.