Search This Blog

Tuesday, December 26, 2023

PowerShell: Automating Cheats in Star Wars Knights of the Old Republic

Hello,

This blog post builds a bit on techniques such as sendkey/sendwait, allowing the PowerShell user to automate keystrokes which helps a lot when entering lengthy cheat codes into older games.
As I am using a nordic keyboard layout the way to open the Kotor 2 console is to press shift + backtick.
Keep in mind that you have to edit one of the files in the Steam folder to enable cheats for Knights of the Old Republic 2.

The PowerShell code:

# KOTOR 2 Auto Cheater #

# Sendkey functions #
$Backtick = "+(´)"
$Space = " "
$Enter = "{ENTER}"

<# Cheats hard codes from https://steamcommunity.com/sharedfiles/filedetails/?id=149123471
These variable each contains the actual cheat code of choice, giving you a long list of favorite codes.#>
$treat = "settreatinjury 40"
$compuse = "setcomputeruse 40"
$demo = "setdemolitions 40"
$stealth = "setstealth 40"
$aware = "setawareness 40"
$charm = "setcharisma 40"
$wis = "setwisdom 40"
$int = "setintelligence 40"
$const = "setconstitution 40"
$dex = "setdexterity 40"
$str = "setstrength 40"
$sec = "setsecurity 40"
$rep = "setrepair 40"
$pers = "setpersuade 40"
$exp = "addexp 999999"

# List used in loop, here you simply add variables that you want to include in the loop. #
$listofcheats = $treat, $compuse, $demo, $stealth, $aware, $charm, $wis, $int, $const, $dex, $str, $sec, $rep, $pers, $exp

# This function is responsible for opening the console only. One function, one purpose. #
function Open-Console {
[System.Windows.Forms.SendKeys]::SendWait($Backtick)
[System.Windows.Forms.SendKeys]::SendWait($Space)
}

# This function is responsible for using the cheat that was typed into the Kotor 2 console. #
function Send-Cheat {
[System.Windows.Forms.SendKeys]::SendWait($Enter)
}

<# This is a simple one of function, that accurately lets you reuse a single cheat code without the worry of misspelling #>
function Use-Cheat($code) {
Start-Sleep -Seconds 5;
Open-Console;
[System.Windows.Forms.SendKeys]::SendWait($code);
Send-Cheat;
}

# This is is the automatic function, it runs through the entire list you customize in the script. #
function Use-AllCheats {
    foreach ($cheat in $listofcheats) {
        Start-Sleep -Seconds 5;
        Open-Console;
        [System.Windows.Forms.SendKeys]::SendWait($cheat);
        Send-Cheat;
    }
}

No comments:

Post a Comment