Skip to content

Commit

Permalink
WIP: Add launcher written in AHK v2. (#191)
Browse files Browse the repository at this point in the history
Need to add CI to compile ahk to exe and include in build.
  • Loading branch information
jmctune authored Apr 9, 2024
1 parent 13beb58 commit 330a843
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 4 deletions.
35 changes: 34 additions & 1 deletion .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@
types: [created]

jobs:
compile_launcher:
runs-on: windows-latest
if: github.event.issue.pull_request && contains(github.event.comment.body, '/build')
steps:
- name: Fetch Branch Name
id: fetch-branch-name
uses: xt0rted/pull-request-comment-branch@v2
- name: Checkout PR branch (${{ steps.fetch-branch-name.outputs.head_ref }})
uses: actions/checkout@v4
with:
ref: ${{ steps.fetch-branch-name.outputs.head_ref }}
- name: Compile Exe
run: |
cd app\launcher;
Invoke-WebRequest "https://github.com/AutoHotkey/Ahk2Exe/releases/download/Ahk2Exe1.1.37.01c1/Ahk2Exe1.1.37.01c1.zip" -OutFile "$cwd\ahk2exe.zip";
Invoke-WebRequest "https://www.autohotkey.com/download/ahk-v2.zip" -OutFile "$cwd\ahk.zip";
Expand-Archive -Path "$cwd\ahk2exe.zip" -DestinationPath . -Force;
Expand-Archive -Path "$cwd\ahk.zip" -DestinationPath . -Force;
& .\Ahk2Exe.exe /in "dqxclarity.ahk" /base "AutoHotkey64.exe" /icon "img/rosie.ico";
- name: Upload exe
uses: actions/upload-artifact@v4
with:
name: dqxclarity.exe
path: app\launcher\dqxclarity.exe
if-no-files-found: error
retention-days: 1
overwrite: true

build:
runs-on: ubuntu-latest
if: github.event.issue.pull_request && contains(github.event.comment.body, '/build')
Expand All @@ -19,12 +47,17 @@
with:
ref: ${{ steps.fetch-branch-name.outputs.head_ref }}

- name: Download exe
uses: actions/download-artifact@v4
with:
name: dqxclarity.exe

- name: Create SHORT_SHA env
run: echo "SHORT_SHA=`echo ${{ steps.fetch-branch-name.outputs.head_sha }} | cut -c1-8`" >> $GITHUB_ENV

- name: Build package
run: |
rsync -av --exclude="imgs/" app/* dqxclarity/;
rsync -av --exclude="imgs/" --exclude="launcher/" app/* dqxclarity/;
cp version.update requirements.txt user_settings.ini dqxclarity/;
cp clarity_dialog.db dqxclarity/misc_files;
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ app/misc_files/*.xlsx
app/misc_files/*.db
app/logs
app/user_settings*
app/launcher/user_settings*
app/launcher/*.exe
2 changes: 2 additions & 0 deletions app/launcher/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:: convenience for building locally. ci will build this otherwise.
"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in "dqxclarity.ahk" /base "C:\Program Files\AutoHotkey\v2\AutoHotkey64.exe" /icon "img/rosie.ico"
289 changes: 289 additions & 0 deletions app/launcher/dqxclarity.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
#Requires AutoHotkey v2.0
#SingleInstance force
#Include <json>
#Include <image>
#Include <md5>

/* read ini */
ini := {}
ini.enabledeepl := IniRead(".\user_settings.ini", "translation", "enabledeepltranslate", "False")
ini.deeplkey := IniRead(".\user_settings.ini", "translation", "deepltranslatekey", "")
ini.enablegoogletranslate := IniRead(".\user_settings.ini", "translation", "enablegoogletranslate", "False")
ini.googletranslatekey := IniRead(".\user_settings.ini", "translation", "googletranslatekey", "")
ini.communitylogging := IniRead(".\user_settings.ini", "launcher", "communitylogging", "False")
ini.playernames := IniRead(".\user_settings.ini", "launcher", "playernames", "False")
ini.npcnames := IniRead(".\user_settings.ini", "launcher", "npcnames", "False")
ini.updategamefiles := IniRead(".\user_settings.ini", "launcher", "updategamefiles", "False")
ini.disableupdates := IniRead(".\user_settings.ini", "launcher", "disableupdates", "False")
/* main window */
Launcher := Gui()
Launcher.Opt("-MaximizeBox")
Launcher.AddPicture("YP+1 w150 h-1 vImage", LoadImageFromResource("img/rosie.png"))
/* configuration group */
Launcher.AddGroupBox("ys+10 w200 h120 c0B817C", "Configuration")
Launcher.AddCheckBox("XP+10 YP+20 vCommunityLogging Checked" . ConvertBoolToState(ini.communitylogging), "Community Logging")
Launcher.AddCheckBox("vPlayerNames Checked" . ConvertBoolToState(ini.playernames), "Player Names")
Launcher.AddCheckBox("vNPCNames Checked" . ConvertBoolToState(ini.npcnames), "NPC Names")
Launcher.AddCheckBox("vUpdateGameFiles Checked" . ConvertBoolToState(ini.updategamefiles), "Update Game Files")
Launcher.AddCheckBox("vDisableUpdates Checked" . ConvertBoolToState(ini.disableupdates), "Disable Updates")
Launcher.AddStatusBar("vStatusBar", "")
/* api group */
Launcher.AddGroupBox("Section YP+30 XP-10 w200 h150 c0B817C", "API Settings")
Launcher.AddCheckBox("XP+10 YP+20 vUseDeepL Checked" . ConvertBoolToState(ini.enabledeepl), "Use DeepL")
Launcher.AddEdit("YP+20 W180 r1 vDeepLKey", ini.deeplkey).Opt("+Password")
Launcher.AddCheckBox("XP vUseGoogleTranslate Checked" . ConvertBoolToState(ini.enablegoogletranslate), "Use Google Translate")
Launcher.AddEdit("YP+20 W180 r1 vGoogleTranslateKey", ini.googletranslatekey).Opt("+Password")
Launcher.AddButton("YP+30 w180 vValidateKey", "Validate Enabled Key").OnEvent("Click", ValidateKey)
/* launch */
Launcher.AddButton("YP+60 XS+10 w80 h30 vRunProgram", "Run").Opt("+Default")
Launcher.AddButton("x+20 vGitHub w80 h30", "GitHub").OnEvent("Click", OpenGitHub)
/* tooltips */
Launcher["CommunityLogging"].ToolTip := "Enables logging of internal game files to a text file."
Launcher["PlayerNames"].ToolTip := "Transliterates Japanese player names to English."
Launcher["NPCNames"].ToolTip := "Translate Japanese NPC names to English."
Launcher["UpdateGameFiles"].ToolTip := "Downloads/updates the modded DAT/IDX files."
Launcher["DisableUpdates"].ToolTip := "Don't check for dqxclarity updates on launch."
Launcher["UseDeepL"].ToolTip := "Enable DeepL as your choice of external translation."
Launcher["UseGoogleTranslate"].ToolTip := "Enable Google Translate as your choice of external translation."
Launcher["DeepLKey"].ToolTip := "Paste your DeepL API Key here."
Launcher["GoogleTranslateKey"].ToolTip := "Paste your Google Translate API Key here."
Launcher["ValidateKey"].ToolTip := "Validate that the selected API key works. Check here for status."
Launcher["Run"].ToolTip := "Run the program."
Launcher["GitHub"].ToolTip := "View the source code in your default browser."
/* function handlers */
Launcher["CommunityLogging"].OnEvent("Click", CommunityLoggingWarning)
Launcher["UseDeepL"].OnEvent("Click", CheckedDeepL)
Launcher["UseGoogleTranslate"].OnEvent("Click", CheckedGoogleTranslate)
Launcher["RunProgram"].OnEvent("Click", RunProgram)
/* show launcher */
Launcher.Show("AutoSize Center")
OnMessage(0x0200, On_WM_MOUSEMOVE)
CheckedDeepL(*) {
/*
Behavior when the "Use DeepL" checkbox is checked.
*/
Launcher["UseGoogleTranslate"].value := 0
Launcher["GoogleTranslateKey"].Opt("+Disabled")
Launcher["DeepLKey"].Opt("-Disabled")
}


CheckedGoogleTranslate(*) {
/*
Behavior when the "Use Google Translate" checkbox is checked.
*/
Launcher["UseDeepL"].value := 0
Launcher["DeepLKey"].Opt("+Disabled")
Launcher["GoogleTranslateKey"].Opt("-Disabled")
}


ValidateKey(*) {
/*
Validates the enabled API key by reaching out to whichever
API service is checked.
*/
if (Launcher["UseDeepL"].value = 1) {
DeepLKey := Launcher["DeepLKey"].value
if (DeepLKey) {
if (SubStr(DeepLKey, "-3") = ":fx")
url := "https://api-free.deepl.com/v2/usage"
else
url := "https://api.deepl.com/v2/usage"

web := ComObject('WinHttp.WinHttpRequest.5.1')
web.Open("GET", url)
web.SetRequestHeader("Authorization", "DeepL-Auth-Key " . DeepLKey)
web.Send()
web.WaitForResponse()
Response := JSON.parse(web.ResponseText)

try {
PercentageUsed := Round(Response["character_count"] / Response["character_limit"] * 100, 2)
UpdateStatusBar(Response["character_count"] . "/" . Response["character_limit"] . " characters used, or approximately " . PercentageUsed . "%.")
} catch {
UpdateStatusBar("Failed to validate key.")
}
} else {
UpdateStatusBar("Enter a key before attempting to validate.")
}
} else if (Launcher["UseGoogleTranslate"].value = 1){
GoogleTranslateKey := Launcher["GoogleTranslateKey"].value
if (GoogleTranslateKey) {
url := "https://translation.googleapis.com/language/translate/v2?q=a&target=es&source=en&key=" . GoogleTranslateKey
web := ComObject('WinHttp.WinHttpRequest.5.1')
web.Open("GET", url)
web.Send()
web.WaitForResponse()
Response := JSON.parse(web.ResponseText)
try {
if (Response["data"]["translations"]){
UpdateStatusBar("Key successfully validated.")
}
} catch {
UpdateStatusBar(Response["error"]["message"])
}
}
} else {
UpdateStatusBar("Enable an API service before validating.")
}
}


UpdateStatusBar(text) {
/*
Updates the status bar at the bottom of the GUI.
@param text Text to update the status bar.
*/
Launcher["StatusBar"].SetText(text)
}


CommunityLoggingWarning(*) {
if Launcher["CommunityLogging"].value = 1 {
Result := MsgBox("You have enabled community logging.`n`nThis feature is unstable and may result in unexpected behavior while playing, up to and including crashes. Do not report issues of crashing if you have this enabled.`n`nIf you still want to enable this to help with the project, click `"Yes.`" Otherwise, click `"No.`"", "Community Logging", "YN Icon! Default2 0x1000")
if (Result = "No") {
Launcher["CommunityLogging"].value := 0
}
}
}


OpenGitHub(*) {
/*
Opens the dqxclarity repository in the user's browser.
*/
Run("https://github.com/dqx-translation-project/dqxclarity")
}


HttpRequest(verb, url) {
/*
Sends an HTTP request and returns a response.
@param verb Type of request to send (GET, POST, etc.)
@param url The URL to send the request to.
*/
web := ComObject('WinHttp.WinHttpRequest.5.1')
web.Open(verb, url)
web.Send()
web.WaitForResponse()
return web.ReponseText
}


ConvertBoolToState(value) {
/*
Used for interpreting the values of the user_settings.ini file.
@param value Value to convert to a state.
*/
if (value = "True")
return 1
else
return 0
}


ConvertStateToBool(value) {
/*
Used for interpreting the values of the user_settings.ini file.
@param value Value to convert to a bool.
*/
if (value = "1")
return "True"
else
return "False"
}


On_WM_MOUSEMOVE(wParam, lParam, msg, Hwnd) {
/*
Updates the status bar at the bottom of the GUI
based on what control the user has their mouse
hovered over.
*/
static PrevHwnd := 0
if (Hwnd != PrevHwnd)
{
CurrControl := GuiCtrlFromHwnd(Hwnd)
if CurrControl
{
if !CurrControl.HasProp("ToolTip")
return
UpdateStatusBar(CurrControl.ToolTip)
}
PrevHwnd := Hwnd
}
}


FakeFileInstall(*) {
/*
This is necessary to exist during compile. This tells Ahk2exe to
include the file during compilation, but since we never call the
function, it won't extract the image
*/
FileInstall("img/rosie.png", "*")
}


SaveToIni(*) {
/*
Saves all of the user settings to user_settings.ini.
*/
IniWrite(ConvertStateToBool(Launcher["CommunityLogging"].value), ".\user_settings.ini", "launcher", "communitylogging")
IniWrite(ConvertStateToBool(Launcher["PlayerNames"].value), ".\user_settings.ini", "launcher", "playernames")
IniWrite(ConvertStateToBool(Launcher["NPCNames"].value), ".\user_settings.ini", "launcher", "npcnames")
IniWrite(ConvertStateToBool(Launcher["UpdateGameFiles"].value), ".\user_settings.ini", "launcher", "updategamefiles")
IniWrite(ConvertStateToBool(Launcher["DisableUpdates"].value), ".\user_settings.ini", "launcher", "disableupdates")
IniWrite(ConvertStateToBool(Launcher["UseDeepL"].value), ".\user_settings.ini", "translation", "enabledeepltranslate")
IniWrite(Launcher["DeepLKey"].value, ".\user_settings.ini", "translation", "deepltranslatekey")
IniWrite(ConvertStateToBool(Launcher["UseGoogleTranslate"].value), ".\user_settings.ini", "translation", "enablegoogletranslate")
IniWrite(Launcher["GoogleTranslateKey"].value, ".\user_settings.ini", "translation", "googletranslatekey")
}


GetClarityArgs(*) {
/*
Read the GUI to see which arguments we need to pass to dqxclarity.
Returns the appropriate arguments.
*/
args := ""
if (Launcher["CommunityLogging"].value = 1)
args := args . "l"
if (Launcher["PlayerNames"].value = 1)
args := args . "p"
if (Launcher["NPCNames"].value = 1)
args := args . "n"
if (Launcher["UpdateGameFiles"].value = 1)
args := args . "d"
if (Launcher["DisableUpdates"].value = 1)
args := args . "u"
if (Launcher["UseDeepL"].value = 1 or Launcher["UseGoogleTranslate"].value = 1)
args := args . "c"

if (args)
return "-ClarityArgs `"-" . args . "`""
else
return ""
return args
}

RunProgram(*) {
SaveToIni
Run("cmd.exe /c powershell.exe -ExecutionPolicy Unrestricted -File run_clarity.ps1 " . GetClarityArgs())
ExitApp
}
Binary file added app/launcher/img/rosie.ico
Binary file not shown.
Binary file added app/launcher/img/rosie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 330a843

Please sign in to comment.