Skip to content

Commit

Permalink
- Now using serialization for storing typed values in settings.ini in…
Browse files Browse the repository at this point in the history
…stead

  of manually fiddling around with strings.
- Fixed some copy-paste errors
- Fixed indentation (mixed tabs/spaces => spaces only)
- You may need to adjust settings.ini if you get error messages.
  • Loading branch information
Fritz Webering committed Jul 11, 2013
1 parent 0725f50 commit 5ffe279
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 54 deletions.
179 changes: 179 additions & 0 deletions Serialize.au3
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <Array.au3>

Func Serialize_AssertEquals($value, $expected, $erl=@ScriptLineNumber)
If $value <> $expected Then
ConsoleWrite("(" & $erl & ") := Assertion failed: Expected " & $expected & ", got " & $value & @LF)
EndIf
Return $value <> $expected
EndFunc

; Some test cases
If @ScriptName == "Serialize.au3" Then
Serialize_AssertEquals(Deserialize("5"), 5)
Serialize_AssertEquals(Deserialize("Str(ab\tc)"), "ab"&@TAB&"c")
Serialize_AssertEquals(Deserialize("Str(ab\""c)"), 'ab"c')
Serialize_AssertEquals(Deserialize("Str(ab\r\ncd\t)"), "ab"&@CRLF&"cd"&@TAB)

Local $test3[4] = ["7", 3.0, 3, 0]
Local $test2[4] = [$test3, 2.0, False]
Local $test[5] = [True, "False", $test2, 3.14, 42]
Local $serial = Serialize($test)
Serialize_AssertEquals(Serialize(Deserialize($serial)), $serial)

Exit 0
EndIf

Func Serialize($value)
If IsFloat($value) Or IsInt($value) Or IsBool($value) Then Return String($value)
If IsString($value) Then
$value = StringReplace($value, "\", "\\", Default, 1)
$value = StringReplace($value, @CR, "\r", Default, 1)
$value = StringReplace($value, @LF, "\n", Default, 1)
$value = StringReplace($value, @TAB, "\t", Default, 1)
$value = StringReplace($value, '"', '\"', Default, 1)
Return "Str("&$value&")"
EndIf
If IsArray($value) Then
Local $result = "[", $i
For $i = 0 To UBound($value) - 1
If $i > 0 Then $result &= ","
$result &= Serialize($value[$i])
Next
Return $result & "]"
EndIf
EndFunc

Func Deserialize($string, $start = 1)
Local Const $errInvalid = 1, $errLength = 2, $errArray = 3, $errString = 4, $errNumber = 5
Local $char, $end, $result, $length = StringLen($string)

If $start > $length Then
SetExtended($start)
SetError($errLength)
Return ""
EndIf

; Skip whitespace at current starting position
While $start <= $length And StringIsSpace(StringMid($string, $start, 1))
$start += 1
WEnd

; Parse Array
If StringMid($string, $start, 1) == "[" Then
;p("Parsing Array")
$end = $start + 1
While $end <= $length And StringMid($string, $end, 1) <> "]"
Local $value = Deserialize($string, $end)
$end = @extended
If @error Then Return ""
If IsArray($result) Then
_ArrayAdd($result, $value)
Else
Local $result[1] = [$value]
EndIf

; Skip whitespace after parsed array element
While $end <= $length And StringIsSpace(StringMid($string, $end, 1))
$end += 1
WEnd

If StringMid($string, $end, 1) == "," Then
$end = $end + 1
ElseIf StringMid($string, $end, 1) <> "]" Then
SetExtended($end)
SetError($errArray)
Return ""
EndIf
WEnd
If $end > $length Then
SetExtended($end)
SetError($errArray)
Return ""
Else
SetExtended($end + 1)
Return $result
EndIf
EndIf

; Parse Strings
If StringMid($string, $start, 4) == "Str(" Then
$result = ""
$end = $start + 4
$char = StringMid($string, $end, 1)
While $end <= $length And StringMid($string, $end, 1) <> ")"
$char = StringMid($string, $end, 1)
If $char == "\" Then
$end += 1
$char = StringMid($string, $end, 1)
If $end < $length Then
Switch $char
Case "n"
$char = @LF
Case "r"
$char = @CR
Case "t"
$char = @TAB
Case "v"
$char = Chr(11) ; Vertical Tab
Case Else
; Every other char is just mapped to itself
; For example \' => ', \" => ", \\ => \
EndSwitch
EndIf
EndIf
$result = $result & $char
$end = $end + 1
WEnd
If $end > $length Then
SetExtended($end)
SetError($errString)
Return ""
Else
SetExtended($end + 1)
Return $result
EndIf
EndIf

; Parse Numbers
$end = $start
If $end <= $length And (StringIsDigit(StringMid($string, $end, 1)) Or StringMid($string, $end, 1) == "-" Or StringMid($string, $end, 1) == "+" Or StringMid($string, $end, 1) == ".") Then
If $end <= $length And (StringMid($string, $end, 1) == "-" Or StringMid($string, $end, 1) == "+") Then
$end = $end + 1
EndIf
While $end <= $length And StringIsDigit(StringMid($string, $end, 1))
$end = $end + 1
WEnd
If $end <= $length And StringMid($string, $end, 1) == "." Then
$end = $end + 1
EndIf
While $end <= $length And StringIsDigit(StringMid($string, $end, 1))
$end = $end + 1
WEnd
$result = StringMid($string, $start, $end - $start)
If StringIsFloat($result) Or StringIsInt($result) Then
$result = Number($result)
SetExtended($end)
Return $result
Else
SetExtended($end)
SetError($errNumber)
Return ""
EndIf
EndIf

; Parse Booleans
If StringLower(StringMid($string, $start, 4)) == "true" Then
SetExtended($start + 4)
Return True
EndIf
If StringLower(StringMid($string, $start, 5)) == "false" Then
SetExtended($start + 5)
Return False
EndIf

; Unknown Data
SetExtended($start)
SetError($errInvalid)
Return ""
EndFunc

2 changes: 1 addition & 1 deletion Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1
124 changes: 76 additions & 48 deletions gw2cursor.au3
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
#include <WinAPI.au3>
#include <Constants.au3>
#include <Misc.au3>
#include "Serialize.au3"

Global $settingsFile = "settings.ini"

; Determine overlay cursor image and position relative to the real cursor
Global $whichCursor = "Cursor" & IniRead($settingsFile, "General", "Which Cursor", "1")
Global $pngSrc = IniRead($settingsFile, "Cursors", $whichCursor & " Image", "cursor1.png")
Global $offsetX = Int(IniRead($settingsFile, "Cursors", $whichCursor & " Offset X", "2"))
Global $offsetY = Int(IniRead($settingsFile, "Cursors", $whichCursor & " Offset Y", "5"))


; Milliseconds delay between updating the cursor position
Global $interval = 1000/Int(IniRead($settingsFile, "General", "Updates Per Second", "120"))

Global $websiteUrl = "https://github.com/fritzw/gw2-cursor"
Global $version = "0.1.0"

; Global Constants
Global Const $websiteUrl = "https://github.com/fritzw/gw2-cursor"
Global Const $version = "0.1.1"
Global Const $settingsFile = "settings.ini"

; Install files needed for operation
FileInstall("cursor1.png", "cursor1.png")
FileInstall("cursor2.png", "cursor2.png")
FileInstall("tray-icon.ico", "tray-icon.ico")
FileInstall("settings-example.ini", "settings-example.ini")

$hideWhileDragging = StringLower(IniRead($settingsFile, "General", "Hide Cursor While Dragging", "false")) == "true"
; Determine overlay cursor image and position relative to the real cursor
Global $whichCursor = "Cursor" & ReadSetting("Which Cursor", "1")
Global $pngSrc = ReadSetting($whichCursor & " Image", "cursor1.png", "Cursors")
Global $offsetX = ReadSetting($whichCursor & " Offset X", 2, "Cursors")
Global $offsetY = ReadSetting($whichCursor & " Offset Y", 5, "Cursors")

; Milliseconds delay between updating the cursor position
Global $interval = 1000/ReadSetting("Updates Per Second", "120")

; Load user settings
$hideWhileDragging = ReadSetting("Hide Cursor While Dragging", False)

; Initialize tray icon
Opt("TrayAutoPause", 0)
Expand All @@ -43,26 +43,28 @@ $trayItemExit = TrayCreateItem("Exit")


; Find out if the user wants update notifications
$autoUpdate = StringLower(IniRead($settingsFile, "General", "Auto Update", ""))
If $autoUpdate <> "true" And $autoUpdate <> "false" Then
$autoUpdate = ReadSetting("Auto Update", "invalid")
If Not IsBool($autoUpdate) Then
$answer = MsgBox(3 + 32, "GW2-Cursor", "Do you want to be notified of updates when starting the script?" & @LF & "(Note: Updates will NOT be installed automatically)")
If $answer == 6 Then
$autoUpdate = "true"
ElseIf $answer == 7 Then
$autoUpdate = "false"
If $answer == 6 Then ; Yes
$autoUpdate = True
WriteSetting("Auto Update", True)
ElseIf $answer == 7 Then ; No
$autoUpdate = False
WriteSetting("Auto Update", False)
EndIf
IniWrite($settingsFile, "General", "Auto Update", $autoUpdate)
EndIf

; Check for updates
If $autoUpdate == "true" Then
If $autoUpdate == True Then
TraySetToolTip("GW2-Cursor -- Checking for updates, please wait...")
Local $remoteVersion = HttpGet("https://raw.github.com/fritzw/gw2-cursor/master/Version.txt")
If $remoteVersion <> "" And _VersionCompare($remoteVersion, $version) > 0 Then
If MsgBox(4 + 64, "GW2-Cursor", "An update to version " & $remoteVersion & " is available. You are currently running version " & $version & ". Do you want to go to the website now?") == 6 Then
ShellExecute($websiteUrl)
EndIf
If MsgBox(4 + 64, "GW2-Cursor", "An update to version " & $remoteVersion & " is available. You are currently running version " & $version & ". Do you want to go to the website now?") == 6 Then
ShellExecute($websiteUrl)
EndIf
EndIf
TraySetToolTip($trayTip)
EndIf


Expand Down Expand Up @@ -91,17 +93,17 @@ While True

; Do some things periodically (every second)
If $counter <= 0 Then
$counter = 1000 / $interval
$gw2handle = WinGetHandle("[TITLE:Guild Wars 2; CLASS:ArenaNet_Dx_Window_Class]")
$clientArea = _WinAPI_GetClientRect($gw2handle)
$counter = 1000 / $interval
$gw2handle = WinGetHandle("[TITLE:Guild Wars 2; CLASS:ArenaNet_Dx_Window_Class]")
$clientArea = _WinAPI_GetClientRect($gw2handle)
EndIf

; Leave the cursor where it is, while a mouse button is held down
If _IsPressed(2) Or _IsPressed(1) Then
$pos = MouseGetPos()
$distance = Abs($mouseX - $pos[0]) + Abs($mouseY - $pos[1])
If $hideWhileDragging And $distance > 4 And $overlayVisible Then HideOverlay()
ContinueLoop
$pos = MouseGetPos()
$distance = Abs($mouseX - $pos[0]) + Abs($mouseY - $pos[1])
If $hideWhileDragging And $distance > 4 And $overlayVisible Then HideOverlay()
ContinueLoop
Endif

$mouse = _WinAPI_GetMousePos()
Expand All @@ -110,22 +112,22 @@ While True
$hwnd = _WinAPI_WindowFromPoint($mouse)
_WinApi_ScreenToClient($hwnd, $mouse)
If $hwnd == $gw2handle And RectContains($clientArea, $mouse) Then
If Not $overlayVisible Then ShowOverlay()
MoveOverlay($mouseX, $mouseY)
If Not $overlayVisible Then ShowOverlay()
MoveOverlay($mouseX, $mouseY)
Else
If $overlayVisible Then HideOverlay()
If $overlayVisible Then HideOverlay()
EndIf
$counter = $counter - 1

Switch TrayGetMsg()
Case $trayItemHide
$hideWhileDragging = Not $hideWhileDragging
IniWrite($settingsFile, "General", "Hide Cursor While Dragging", String($hideWhileDragging))
CheckTrayItem($trayItemHide, $hideWhileDragging)
Case $trayItemWebsite
ShellExecute($websiteUrl)
Case $trayItemExit
Exit 0
Case $trayItemHide
$hideWhileDragging = Not $hideWhileDragging
WriteSetting("Hide Cursor While Dragging", $hideWhileDragging)
CheckTrayItem($trayItemHide, $hideWhileDragging)
Case $trayItemWebsite
ShellExecute($websiteUrl)
Case $trayItemExit
Exit 0
EndSwitch
WEnd

Expand All @@ -148,9 +150,9 @@ EndFunc

Func CheckTrayItem($item, $bool)
If $bool Then
TrayItemSetState($item, $TRAY_CHECKED)
TrayItemSetState($item, $TRAY_CHECKED)
Else
TrayItemSetState($item, $TRAY_UNCHECKED)
TrayItemSetState($item, $TRAY_UNCHECKED)
EndIf
EndFunc

Expand All @@ -166,6 +168,32 @@ Func RectContains($rectangle, $point)
EndFunc


Func ReadSetting($key, $default="", $section="General")
Local Const $invalid = "sv8one765sl874vn6o87465bv7w45687v6s87w348v7"
Local $value = IniRead($settingsFile, $section, $key, $invalid)
If $value == $invalid Then
Return $default
Else
Local $decoded = Deserialize($value)
If @error Then ErrorBox("Error decoding '" & $key & "=" & $value & " in section [" & $section & "] in " & $settingsFile)
Return $decoded
EndIf
EndFunc

Func ErrorBox($text, $fatal=True, $code=1)
MsgBox(48, "GW2-Cursor Error", $text)
If $fatal Then Exit $code
EndFunc

Func WriteSetting($key, $value="", $section="General")
$data = Serialize($value)
; Strings containing , or ; should be quoted in ini files.
If StringInStr($data, ",") <> 0 Or StringInStr($data, ";") <> 0 Then
$data = '"' & $data & '"'
EndIf
IniWrite($settingsFile, $section, $key, $data)
EndFunc

Func Debug($message)
ConsoleWrite($message & @CRLF)
EndFunc
Expand All @@ -184,7 +212,7 @@ Func Terminate()
Exit 0
EndFunc

; Courtesy of
; Courtesy of Pinguin94 (http://autoit.de/index.php?page=Thread&threadID=17900)
Func SetBitmap($hGUI, $hImage, $iOpacity)
Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
Local Const $AC_SRC_ALPHA = 1
Expand Down
Binary file modified gw2cursor.exe
Binary file not shown.
Loading

0 comments on commit 5ffe279

Please sign in to comment.