Sign in to follow this  
Followers 0
geasiki

Is there a better way to code this script?

9 posts in this topic

#1 ·  Posted (edited)

I was wondering if there was a better way to code this script I wrote

so it uses less CPU or memory.

I left a lot of the script out to shorten it ^^;

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <Memory.au3>
#include <NomadMemory.au3>
#include <Array.au3>

$Jump_chkbox = GUICtrlCreateCheckbox("JUMP", 7, 71, 97, 17)
GUICtrlSetOnEvent(-1, "_JumpHack")

While 1   ;main loop
WEnd

Func _JumpHack()
    While BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED) ; wondering why 'if' doesn't work
        $Title = "Element Client"
        $Base_Address = 0x009398ec
        
        Global $OffSet_Jump[3]
        $OffSet_Jump[1] = 32
        $OffSet_Jump[2] = 2844
        
        Global $ProcessID = WinGetProcess($Title)
        Global $OpenMemory = _MemoryOpen($ProcessID)
        
        $Jump = _MemoryPointerRead($Base_Address, $OpenMemory, $OffSet_Jump)
        _MemoryPointerWrite($Base_Address, $OpenMemory, $OffSet_Jump, 1) 
                               ; pointer value to 1
        _MemoryClose($OpenMemory)
    WEnd
    
    if BitAND(GUICtrlRead($Jump_chkbox), $GUI_UNCHECKED) Then
        _MemoryClose($OpenMemory) ; disables hack
    
    EndIf
EndFunc
Edited by geasiki

Share this post


Link to post
Share on other sites



I did a brief reading but I came up with several notes.

- This While 1...WEnd loop should call Sleep(150) or so.

- I don't see any Opt('GuiOnEventMode', 1) in the code but I think it's typo

- Make a flag variable to store the check box state in the beginning of the routine to avoid calling GuiCtrlRead over and over.

- Try not to use BitAND, in my opinion it's just to make the code more readable but it involves function call.

- Why declaring a global variable withing function that get could again and again?

- Perhaps the If BitAND(GuiCtrlRead($Jump_chkbox), $GUI_CHECKED)) didn't work because you forgot to add Then?

may be some other cases but this is what I came with so far.

Share this post


Link to post
Share on other sites

@Authenticity, thank you for the quick reply^^

- This While 1...WEnd loop should call Sleep(150) or so.

I added sleep(1000) which helped reduce the fan on my laptop :)

- I don't see any Opt('GuiOnEventMode', 1) in the code but I think it's typo

I didn't add it on the above script but this is the reason I have it... If I don't need it please tell me

$Button = GUICtrlCreateButton("change", 255, 39, 41, 81, 0)
GUICtrlSetOnEvent(-1, "_NameChange")

Func _NameChange()
    $old = GUICtrlRead($Input1)
    $new = GUICtrlRead($Input2)
    $wgt = wingettitle($old)
    winsettitle($old,"",$new)
EndFuncoÝ÷ Ú«¨µãæ¨/j¸nW­¢Ëh­ëayÈ^rFèÆËZµè§¶z§)à¡ûazº.)Þ¶¯¢'jYb®+kvÞ­©Ý¢÷«þ«¨µà*.+Þæ§{¦¦W¨~0Û!¢é]mçhæ§w^­â,¥v)Üç^Ò!j÷¹rº1zÌ"¶")Z­û§rبʮ¢×¯)è¶Ú.±àb´Cy²¢§â·lë-¶ëayÊzj+z·u¦åy»­Ø§¾ozÇîËb¢w_êº^:G²¶$Ú~«¨µãÞ®©²Ø^!ðb´C趹Qy§tߢn!º1Ó~P"
ØmÂämçºÇ²¢çè®
-¶u8^ú®¢×v'ZuÝýN§ßÖî¶+)ev'gßÛp¢¹¢é]ÖÞ¶­¶·÷bíý穲Ø^½©nzÚÖéíX­ßÛ.È^rGÂ¥zØ^ßØßØî²×!jx²Ø^½©nzÜy©ÝضÊ/z¼+Û^«¨µå¡Éתâ Úö«¦å{­)à~éܶ*'¶­ë¢é]j¢©Ýj¢ú®¢×²¢éjybëaÆ®¶­sdgVæ2ô§V×6² vÆRuT7G&Å&VBb33c´§V×ö6&÷Òb33c´uTô4T4´T@  Æö6Âb33c´öfe6WEô§V׳5Ð b33c´öfe6WEô§V׳ÒÒ3  b33c´öfe6WEô§V׳%ÒÒ#C@  b33c´÷VäÖVÖ÷'ÒôÖVÖ÷'÷Vâb33cµ&ö6W74B b33c´§V×ÒôÖVÖ÷'öçFW%&VBb33c´&6UôFG&W72Âb33c´÷VäÖVÖ÷'Âb33c´öfe6WEô§V× ôÖVÖ÷'öçFW%w&FRb33c´&6UôFG&W72Âb33c´÷VäÖVÖ÷'Âb33c´öfe6WEô§V× ôÖVÖ÷'6Æ÷6Rb33c´÷VäÖVÖ÷' tVæ@  buT7G&Å&VBb33c´§V×ö6&÷Òb33c´uTõTä4T4´TBFVà ¶6öÆ÷#ÒgV÷C²4dddcgV÷CµÒb33c´÷VäÖVÖ÷'ÒôÖVÖ÷'÷Vâb33cµ&ö6W74B²ö6öÆ÷%ТôÖVÖ÷'6Æ÷6Rb33c´÷VäÖVÖ÷' VæD`¤VæDgVæ

I don't know why, but if I take the 'global' off and I don't rewrite it here(yellow)

I get an error when I uncheck the box. (Error: Variable used without being declared.)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

if GUICtrlRead($Jump_chkbox)= $GUI_UNCHECKED Then
        $OpenMemory = _MemoryOpen($ProcessID)
        _MemoryClose($OpenMemory)
EndIf

What is this? Why open memory and then immeidiately close memory?

And about Global what authtenticity meant was to declare your variables outside of the function so that you aren't constantly declaring variables and then killing them at the end of the function call.

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Try:

;Include necessary include files
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <Memory.au3>
#include <NomadMemory.au3>
#include <Array.au3>
;Declare constants
Global $OffSet_Jump[3]
$OffSet_Jump[1] = 32
$OffSet_Jump[2] = 2844

Global $ProcessID = WinGetProcess($Title)
Global $OpenMemory = _MemoryOpen($ProcessID)

;Create GUI ETC
$Jump_chkbox = GUICtrlCreateCheckbox("JUMP", 7, 71, 97, 17)
GUICtrlSetOnEvent(-1, "_JumpHack")

;main loop
While 1
;Small sleep to conserve CPU
    Sleep(10)
WEnd

;Jump Hack function...
Func _JumpHack()
    Local $Base_Address = 0x009398ec
    Local $Title = "Element Client"
    
    If BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED) Then; wondering why 'if' doesn't work
        $Jump = _MemoryPointerRead($Base_Address, $OpenMemory, $OffSet_Jump)
        _MemoryPointerWrite($Base_Address, $OpenMemory, $OffSet_Jump, 1); pointer value to 1
    Else
;Do the opposite of disabling?
;Maybe?
        $Jump = _MemoryPointerRead($Base_Address, $OpenMemory, $OffSet_Jump)
        _MemoryPointerWrite($Base_Address, $OpenMemory, $OffSet_Jump, 0); pointer value to 1
    EndIf
EndFunc ;==>_JumpHack

;ON EXIT FUNC
Func OnAutoITExit()
;Close memory
    If IsDeclared("OpenMemory") Then _MemoryClose($OpenMemory)
EndFunc ;==>OnAutoITExit

Cheers,

Brett

EDIT:

Use BitAnd as several states can be returned by GUICtrlRead for the checkbox (includes $GUI_FOCUS and $GUI_CHECKED).

Opt('GuiOnEventMode', 1) is required for your script to run in OnEvent mode.

A large sleep is not necessary. To retain a responsive GUI keep it between 10-50ms. 1000ms is one second remember.

Edited by BrettF

Share this post


Link to post
Share on other sites

I added sleep(1000) which helped reduce the fan on my laptop :)

Yes it would reduce the cpu load but your thread is suspended for a second that in the mean of computers time it's like few thousands INC EAX ^^.

Try to keep it around 100-300 ms to get quicker interaction with the user.

I don't know why, but if I take the 'global' off and I don't rewrite it here(yellow)

I get an error when I uncheck the box. (Error: Variable used without being declared.)

Sorry, it was a brief reading so maybe I made a mistake. Anyway if you want to reference this variable outside the function it should be declared with global scope in the header declarations section.

As for the While "GUICtrlRead($Jump_chkbox) = $GUI_CHECKED" I still don't get the point if it's on event it will get called upon request for event but if you prefer it this way so indeed GuiCtrlRead is required, unless you can enlighten my eyes.

Good-Luck

Share this post


Link to post
Share on other sites

Thank you Authenticity, jaberwocky6669, and BrettF for all the help :think:

As for the While "GUICtrlRead($Jump_chkbox) = $GUI_CHECKED" I still don't get the point if it's on event it will get called upon request for event

- I'm new to Autoit and it's probably my english, but can you please rewrite it?

I'm going to work on the 'flag' part tomorrow so I can avoid calling GuiCtrlRead over and over... Not sure where to start :shhh:

Still not sure why the 'While BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED)'

or the 'While GUICtrlRead($Jump_chkbox) = $GUI_CHECKED' doesn't work with an 'If ... then'

The only thought that comes to mind is that the 'while' keeps the value to a 1 until it's unchecked

while the 'if' just changes the value once and it gets overwritten by the client. :)

Anyway going to sleep, g'night everyone :lmao:

Share this post


Link to post
Share on other sites

Try this reproducer. All working here:

;Include necessary include files
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <Memory.au3>
;#include <NomadMemory.au3>
#include <Array.au3>
;Declare constants and options.
Opt("GUIOnEventMode", 1)

;Global $OffSet_Jump[3]
;$OffSet_Jump[1] = 32
;$OffSet_Jump[2] = 2844

;Global $ProcessID = WinGetProcess($Title)
;Global $OpenMemory = _MemoryOpen($ProcessID)

;Create GUI ETC
$GUI = GUICreate("", 800, 600)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
$Jump_chkbox = GUICtrlCreateCheckbox("JUMP", 7, 71, 97, 17)
GUICtrlSetOnEvent(-1, "_JumpHack")

GUISetState()
;main loop
While 1
;Small sleep to conserve CPU
    Sleep(10)
WEnd

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
        ;MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

    EndSelect
EndFunc  ;==>SpecialEvents

;Jump Hack function...
Func _JumpHack()
    MsgBox(0, "", "Jump Hack Function Called!")
    MsgBox(0, "State", BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED))
; Local $Base_Address = 0x009398ec
;Local $Title = "Element Client"

    If BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED) Then; wondering why 'if' doesn't work
        MsgBox(0, "Selected", "true")
    ;$Jump = _MemoryPointerRead($Base_Address, $OpenMemory, $OffSet_Jump)
    ;_MemoryPointerWrite($Base_Address, $OpenMemory, $OffSet_Jump, 1); pointer value to 1
    Else
        MsgBox(0, "Selected", "False")
    ;Do the opposite of disabling?
    ;Maybe?
    ;$Jump = _MemoryPointerRead($Base_Address, $OpenMemory, $OffSet_Jump)
    ;_MemoryPointerWrite($Base_Address, $OpenMemory, $OffSet_Jump, 0); pointer value to 1
    EndIf
EndFunc  ;==>_JumpHack

;ON EXIT FUNC
Func OnAutoITExit()
;Close memory
;If IsDeclared("OpenMemory") Then _MemoryClose($OpenMemory)
EndFunc  ;==>OnAutoITExit

If it still doesn't work, attach your whole script and we'll try find the problem :)

Brett

Share this post


Link to post
Share on other sites

As for the While "GUICtrlRead($Jump_chkbox) = $GUI_CHECKED" I still don't get the point if it's on event it will get called upon request for event

Still not sure why the 'While BitAND(GUICtrlRead($Jump_chkbox), $GUI_CHECKED)'

or the 'While GUICtrlRead($Jump_chkbox) = $GUI_CHECKED' doesn't work with an 'If ... then'

The only thought that comes to mind is that the 'while' keeps the value to a 1 until it's unchecked

while the 'if' just changes the value once and it gets overwritten by the client. :)

I think it's the question of the purpose of your script and what it should achieve so If...Then...EndIf won't work if you intend to process a While...WEnd loop but keep in mind that you don't need to set on event for this control as you keep checking the control's state so just make it a simple function and call it before the sleep loop. It's your decision what your script should achieve.

Anyway going to sleep, g'night everyone :lmao:

Night ;]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0