Saturday, February 5, 2011

World of Warcraft automation using powershell

I have some crafting that I do in World of Warcraft that is repetitive, lucrative, and boring. If I sit at the keyboard doing it, I'm essentially just executing a for loop in my head. Not my idea of fun, especially when it could be an opportunity to hone my 'leet programming skills.

I used to automate this activity with AutoHotKey (AHK). It worked great, however, it was a new pseudo-programming language that I had to learn, and a very specific one at that. I've done some neat things with AutoHotKey, but I would prefer to use something like powershell instead. Powershell has the advantage that it can be used in a much broader set of domains than AutoHotKey, and therefor more likely to be useful to me in the future for some as of yet unknown task. I had toyed with the idea of embedding AHK inside of powershell and executing AHK commands within a powershell script, but I have found a better solution.

Up until .net 4.0 I had considered making AHK commands embeddable in Powershell. But! .net 4.0 brings us the System.Windows.Automation namespace.

With this small bit of knowledge, I was able to craft the following sample:


Import-Module C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\UIAutomationClient.dll
$a  = [System.Windows.Automation.AutomationElement]::RootElement.FindAll([System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.Condition]::TrueCondition)
$wow = $a | Where { $_.Current.Name -eq "World of Warcraft" }
$pattern = $wow.GetCurrentPattern([System.Windows.Automation.WindowPattern]::Pattern)
$pattern.SetWindowVisualState([System.Windows.Automation.WindowVisualState]::Normal)
[System.Windows.Forms.SendKeys]::SendWait("{Enter}Hi{Enter}")

Making my character say "Hi", isn't terribly useful, but it illustrates how I could send any sort of command at all to Wow.

This uses .Net 4.0 which screws up in most default configurations of powershell and powershell editors. In order to configure your powershell environment to run on .net 4.0 and be able to run this script, add the following configuration file to your editor of choice.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.30319" />
</startup>
</configuration>

A more thorough discussion of .net 4.0 and powershell.

That config file wasn't working with PowerGUI though. I had to resort to the registry keys:

New-ItemProperty -Name "OnlyUseLatestCLR" -Value 1 -Path "HKLM:\software\wow6432node\microsoft\.netframework\"
New-ItemProperty -Name "OnlyUseLatestCLR" -Value 1 -Path "HKLM:\software\microsoft\.netframework\"

No comments:

Post a Comment