Sign in to follow this  
Followers 0
Knight

List All Variable Names In Script

7 posts in this topic

This script will list all the variable names in a different script. It took me a lot longer than I had expected :think:

It is not as fast as I thought it would be, but that is probably because it isn't the best way of doing this.

It will create an array of all the variables in the script; currently it writes that array to a file. That is easily changeable however.

To use; put the script to read in the $Source variable, and the file to be output with all variable names at $Results.

#include <File.au3>
#include <Array.au3>

$Source = "test.au3"
$Results = "results.txt"

Dim $File

$Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_$"
$Variables = _ArrayCreate(1)

_FileReadToArray($Source, $File)

For $i = 1 To (UBound($File, 1) - 1)
    $Instance = 0
    $Line = StringSplit($File[$i], "")
    Do
        $VarStart = _ArraySearch($Line, Chr(36), $Instance)
        $Instance = $VarStart + 1
        If $VarStart > 0 Then
            $Var = ""
            For $t = $VarStart To UBound($Line, 1) - 1
                If StringInStr($Char, $Line[$t]) > 0 Then
                    $Var = $Var&$Line[$t]
                Else
                    ExitLoop
                EndIf
            Next
            If $Var <> "" And _ArraySearch($Variables, $Var) = -1 Then _ArrayAdd($Variables, $Var)
        EndIf
    Until $VarStart <= 0
Next

$Variables[0] = UBound($Variables, 1) - 1

_FileWriteFromArray($Results, $Variables)
;_ArrayDisplay($Variables, "")

I would love to see any other idea's people come up with for doing this same thing. I plan on using it for a Variable Renamer later.

Thanks,

JKnight

Share this post


Link to post
Share on other sites



This is nice, keep working on it. I think this will be useful when you have more functions on it and you can rename things, that sort of things.

If i would build this, i would read char by char and check if it's a $ and then check to the right until you reach a space or a new line.. then store everything that is between and continue reading char by char

Share this post


Link to post
Share on other sites

This is nice, keep working on it. I think this will be useful when you have more functions on it and you can rename things, that sort of things.

If i would build this, i would read char by char and check if it's a $ and then check to the right until you reach a space or a new line.. then store everything that is between and continue reading char by char

That is what I had originally done, then i realized other ways of declaring variables as I was making it. So I had to start over. IE:

$Var=1
Dim $Var, $Tree, $Hi[900]

All those wouldn't work if I was looking for the variable to end with a space.

Share this post


Link to post
Share on other sites

Suggestion:

the output file should list separately variables that are not used in the code, so that users can clean up scripts.

Tanks anyway

Peppe

Share this post


Link to post
Share on other sites

Knight,

I like what you did. I wrote something similar for my script obfuscator a while back.

I changed the $Source to allow users to pick a script, starting from their desktop.

And I changed the $Results to use the name of the script with a .txt extension.

You don't have to keep my changes. Feel free to modify them as you see fit.

Keep working on it. I think it could be very useful.

taurus905

#include <File.au3>
#include <Array.au3>

$Source = FileOpenDialog("Choose a script to find Variables in:", @DesktopCommonDir, "Scripts (*.au3)", 1 + 2)

If @error Then Exit

$Results = StringTrimRight($Source, 4) & ".txt"

Dim $File

$Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_$"
$Variables = _ArrayCreate(1)

_FileReadToArray($Source, $File)

For $i = 1 To (UBound($File, 1) - 1)
    $Instance = 0
    $Line = StringSplit($File[$i], "")
    Do
        $VarStart = _ArraySearch($Line, Chr(36), $Instance)
        $Instance = $VarStart + 1
        If $VarStart > 0 Then
            $Var = ""
            For $t = $VarStart To UBound($Line, 1) - 1
                If StringInStr($Char, $Line[$t]) > 0 Then
                    $Var = $Var&$Line[$t]
                Else
                    ExitLoop
                EndIf
            Next
            If $Var <> "" And _ArraySearch($Variables, $Var) = -1 Then _ArrayAdd($Variables, $Var)
        EndIf
    Until $VarStart <= 0
Next

$Variables[0] = UBound($Variables, 1) - 1

_FileWriteFromArray($Results, $Variables)
;_ArrayDisplay($Variables, "")

"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

Just a FYI: Tidy has a documentation option (Indent+Proper+Doc button) that will generate a file containing the script showing all loops and a report of all variables used in which line.

:think:


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks taurus. I have added the changes since I really like them; with some alterations of course :think:

I have added a variable renamer that will replace variables with a random 15-digit variable. It will check for duplicates if by any chance it generates the same number twice. It will generate a new script with the changes at n&"_VarRename.au3".

Some limitations this script has is:

You will need to manually change variables when using:

IsDeclared(), Eval(), etc where it checks a variable through a string.

$Var = 1
If IsDeclared("Var") Then Exit

You will need to manually change the "Var" inside the IsDeclared to the newly renamed variable.

Another problem is with variables starting with the same characters as another variable.

$n = 1
$new = 2

Will result in:

$098237561294762 = 1
$098237561294762ew = 2

because of the string replace of $n. However they will both still be different variables so it shouldn't affect performance.

Finally, all $ signs will be considered a variable and will replace them ALL with a variable. One workaround is using Chr(36) when wanting to display a $ sign rather than actually typing it.

#include <File.au3>
#include <Array.au3>

$Source = FileOpenDialog("Choose a script to find Variables in:", @DesktopCommonDir, "Scripts (*.au3)", 3)
If @error Then Exit

$Results = StringTrimRight($Source, 4) & "_VarRename.au3"

Dim $File

$Char = Chr(36)&"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_"
$Variables = _ArrayCreate(1)

_FileReadToArray($Source, $File)

For $i = 1 To (UBound($File, 1) - 1)
    $Instance = 0
    $Line = StringSplit($File[$i], "")
    Do
        $VarStart = _ArraySearch($Line, Chr(36), $Instance)
        $Instance = $VarStart + 1
        If $VarStart > 0 Then
            $Var = ""
            For $t = $VarStart To UBound($Line, 1) - 1
                If StringInStr($Char, $Line[$t]) > 0 Then
                    $Var = $Var&$Line[$t]
                Else
                    ExitLoop
                EndIf
            Next
            If $Var <> "" And _ArraySearch($Variables, $Var) = -1 Then _ArrayAdd($Variables, $Var)
        EndIf
    Until $VarStart <= 0
Next

$Variables[0] = UBound($Variables, 1) - 1

Dim $NewVariables[UBound($Variables, 1)]
$Replace = FileRead($Source)

For $r = 1 To UBound($Variables, 1) - 1
    While 1
        $NewVariables[$r] = Chr(36)&Random(100, 999, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)&Random(0, 9, 1)
        If _ArraySearch($NewVariables, $NewVariables[$r]) = $r Then ExitLoop
    WEnd
    $Replace = StringReplace($Replace, $Variables[$r], $NewVariables[$r], 0)
Next

If FileExists($Results) Then FileDelete($Results)
FileWrite($Results, $Replace)

Any bugs, suggestions, improvements, or changes to the code that would speed it up or fix any limitations are greaty appreciated.

-JKnight

Edited by Knight
1 person likes this

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