Last active
May 2, 2017 05:13
-
-
Save TLaborde/60af3ef70d8b17f6e05e5ed62eaca66f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ScreenShot([Int]$TopRightX,[Int]$TopRightY,[Int]$BottomLeftX,[Int]$BottomLeftY, [string]$Name) { | |
$bounds = [Drawing.Rectangle]::FromLTRB($TopRightX,$TopRightY, $BottomLeftX,$BottomLeftY) | |
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height | |
$graphics = [Drawing.Graphics]::FromImage($bmp) | |
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size) | |
$screenCapturePathBase = "$pwd\$name.png" | |
if (Test-Path $screenCapturePathBase) { | |
Remove-Item $screenCapturePathBase | |
} | |
$bmp.Save($screenCapturePathBase) | |
$graphics.Dispose() | |
$bmp.Dispose() | |
} | |
$cSource = @' | |
using System; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
public class Clicker | |
{ | |
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx | |
[StructLayout(LayoutKind.Sequential)] | |
struct INPUT | |
{ | |
public int type; // 0 = INPUT_MOUSE, | |
// 1 = INPUT_KEYBOARD | |
// 2 = INPUT_HARDWARE | |
public MOUSEINPUT mi; | |
} | |
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx | |
[StructLayout(LayoutKind.Sequential)] | |
struct MOUSEINPUT | |
{ | |
public int dx ; | |
public int dy ; | |
public int mouseData ; | |
public int dwFlags; | |
public int time; | |
public IntPtr dwExtraInfo; | |
} | |
//This covers most use cases although complex mice may have additional buttons | |
//There are additional constants you can use for those cases, see the msdn page | |
const int MOUSEEVENTF_MOVED = 0x0001 ; | |
const int MOUSEEVENTF_LEFTDOWN = 0x0002 ; | |
const int MOUSEEVENTF_LEFTUP = 0x0004 ; | |
const int MOUSEEVENTF_RIGHTDOWN = 0x0008 ; | |
const int MOUSEEVENTF_RIGHTUP = 0x0010 ; | |
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ; | |
const int MOUSEEVENTF_MIDDLEUP = 0x0040 ; | |
const int MOUSEEVENTF_WHEEL = 0x0080 ; | |
const int MOUSEEVENTF_XDOWN = 0x0100 ; | |
const int MOUSEEVENTF_XUP = 0x0200 ; | |
const int MOUSEEVENTF_ABSOLUTE = 0x8000 ; | |
const int screen_length = 0x10000 ; | |
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx | |
[System.Runtime.InteropServices.DllImport("user32.dll")] | |
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); | |
public static void LeftClickAtPoint(int x, int y) | |
{ | |
//Move the mouse | |
INPUT[] input = new INPUT[3]; | |
input[0].mi.dx = x*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width); | |
input[0].mi.dy = y*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height); | |
input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE; | |
//Left mouse button down | |
input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; | |
//Left mouse button up | |
input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP; | |
SendInput(3, input, Marshal.SizeOf(input[0])); | |
} | |
} | |
'@ | |
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing | |
#helper | |
function Get-Position { | |
$X = [System.Windows.Forms.Cursor]::Position.X | |
$Y = [System.Windows.Forms.Cursor]::Position.Y | |
Write-Output "[Clicker]::LeftClickAtPoint($x,$y)" | |
} | |
function Invoke-FeatureGenerator{ | |
1..200 | % { | |
$x = 300..940 | Get-Random | |
$y = 120..615 | Get-Random | |
[Clicker]::LeftClickAtPoint($x,$y) | |
# force pickup color | |
$x = 50..277 | Get-Random | |
$y = 468..598 | Get-Random | |
[Clicker]::LeftClickAtPoint($x,$y) | |
} | |
} | |
function Get-SeedFromString([String] $String) { | |
$bytes = [System.Security.Cryptography.HashAlgorithm]::Create("MD5").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | |
[bitconverter]::ToInt32($bytes,0) | |
} | |
function Generate-Avatar ($name) { | |
#---------------------------------------------- | |
#region Import the Assemblies | |
#---------------------------------------------- | |
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | |
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | |
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | |
[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | |
[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | |
[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | |
[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | |
#endregion Import Assemblies | |
#---------------------------------------------- | |
#region Generated Form Objects | |
#---------------------------------------------- | |
[System.Windows.Forms.Application]::EnableVisualStyles() | |
$form1 = New-Object 'System.Windows.Forms.Form' | |
$webbrowser1 = New-Object 'System.Windows.Forms.WebBrowser' | |
$timer1 = New-Object 'System.Windows.Forms.Timer' | |
$timer2 = New-Object 'System.Windows.Forms.Timer' | |
$timer3 = New-Object 'System.Windows.Forms.Timer' | |
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' | |
#endregion Generated Form Objects | |
#---------------------------------------------- | |
# User Generated Script | |
#---------------------------------------------- | |
$FormEvent_Load={ | |
$path = (Join-Path $pwd "generator.html") -replace '\\','/' | |
$webBrowser1.Navigate("file:///$path") | |
$webBrowser1.Width = 960 | |
$webBrowser1.Height = 700 | |
$webBrowser1.Location = "0,0" | |
} | |
$timer1_Tick={ | |
[Clicker]::LeftClickAtPoint(476,520) | |
$timer1.remove_Tick($timer1_Tick) | |
$timer2.Enabled = $True | |
$timer2.Interval = 1000 | |
$timer2.add_Tick($timer2_Tick) | |
} | |
$timer2_Tick={ | |
$seed = Get-SeedFromString $name | |
get-random -SetSeed $seed | |
Invoke-FeatureGenerator -seed $seed | |
$timer2.remove_Tick($timer2_Tick) | |
$timer3.Enabled = $True | |
$timer3.Interval = 1000 | |
$timer3.add_Tick($timer3_Tick) | |
} | |
$timer3_Tick={ | |
Get-ScreenShot -TopRightX 50 -TopRightY 140 -BottomLeftX 280 -BottomLeftY 370 -name $name | |
$timer3.remove_Tick($timer3_Tick) | |
$form1.Close() | |
} | |
$Form_StateCorrection_Load= | |
{ | |
#Correct the initial state of the form to prevent the .Net maximized form issue | |
$form1.WindowState = $InitialFormWindowState | |
} | |
$Form_Cleanup_FormClosed= | |
{ | |
#Remove all event handlers from the controls | |
try | |
{ | |
$form1.remove_Load($FormEvent_Load) | |
$form1.remove_Load($Form_StateCorrection_Load) | |
$form1.remove_FormClosed($Form_Cleanup_FormClosed) | |
} | |
catch [Exception] | |
{ } | |
} | |
$form1.Controls.Add($webbrowser1) | |
$form1.ClientSize = '960, 620' | |
$form1.FormBorderStyle = 'FixedDialog' | |
$form1.MaximizeBox = $False | |
$form1.MinimizeBox = $False | |
$form1.Name = "form1" | |
$form1.StartPosition = "Manual" | |
$form1.Location = '0,0' | |
$form1.Text = "Form" | |
$form1.add_Load($FormEvent_Load) | |
$InitialFormWindowState = $form1.WindowState | |
#Init the OnLoad event to correct the initial state of the form | |
$form1.add_Load($Form_StateCorrection_Load) | |
#Clean up the control events | |
$form1.add_FormClosed($Form_Cleanup_FormClosed) | |
$timer1.Enabled = $True | |
$timer1.Interval = 1500 | |
$timer1.add_Tick($timer1_Tick) | |
#Show the Form | |
$form1.ShowDialog() | |
} #End Function | |
# For first run, add the other files | |
$generatorHTMLpath = (Join-Path $pwd "generator.html") | |
if (!(Test-Path $generatorHTMLpath)) { | |
@' | |
<object> | |
<embed src="anime_face_maker_2_by_gen8-d30uny4.swf" width="900" height="650"></embed> | |
</object> | |
'@ | Out-File $generatorHTMLpath -Encoding utf8 | |
} | |
$generatorSWFpath = (Join-Path $pwd "anime_face_maker_2_by_gen8-d30uny4.swf") | |
if (!(Test-Path $generatorSWFpath)) { | |
Invoke-WebRequest "http://tab_plus.addictivecode.org/Games/anime_face_maker_2_by_gen8-d30uny4.swf" -OutFile $generatorSWFpath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment