Context
The silly idea of OS-tan, the personification of OS as cute manga characters made me think of a sillier idea: show server monitoring as anime girl. Let’s write down the requirements.
Requirements
- Each server/service should be represented by a unique avatar, like an identicon.
Best Implementation
- Create several dozen of body part layer template on transparent background, assign for each a palette of allowed colors.
- Generate a unique hash from a service/server name.
- From the hash, select layers and color and merge them.
Current Crappy Implementation
- Generate a unique hash from a service/server name.
- Feed the hash as a seed to a random number generator.
- Open a Flash game to create your own anime character, and click (pseudo-)randomly.
- Do a screenshot.
Part 1: Generate a unique seed from a string
This part will be the sanest part of the final result. We will use .Net to generate a MD5 hash of a string, then truncate it from an int128 to an int32, required to seed Get-Random
in PowerShell. MD5 shouldn’t be used as a hash to protect data, but for our purpose, which is to generate scattered numbers from an input of a small size, it’s good enough. Knuth probably would recommend something more robust, but it’s good enough for this first step. Now, the code:
Part 2: Feed the hash as a seed to a random number generator.
This part is very easy, we just need to set Get-Random
.
Every times SetSeed
is called, it will reset the generator, which will ensure that our process can be reproduced.
Part 4: Do a screenshot.
Wait, why is Part 4 before Part 3? Because while we start getting dirty, it’s not full madness yet, and it’s short. We will write a function doing a screenshot of a rectangular area of the screen, needing the coordinate of the top right corner and the bottom left corner.
A proper screenshot function would check if the file does not exist, and maybe increment the name. For our goal, we don’t need that.
Part 3: Open a Flash game to create your own anime character, and click (pseudo-)randomly.
The madness really starts now. Since I have not an artsy bone in my coder body, I will rely on more talented people. I tested a lot of Anime Avatar Generator games and finally decided to work with one of the latest fron Gen-8. The menus are relatively consistant, as is the color palette. We need to do several thing:
- Package the swf to open it in a webbrowser controlled by Powershell
- Click on the “play button” once the swf is loaded
- Click around a bunch of time
- Screenshot and quit
Helper function: Click somewhere on the screen
I just used some class copy/pasted from Internet, it create a static method to click somewhere. I think it’s buggy for weird multi screen setup, but we are not trying to do thing in the proper way, I think it’s now clear.
Packaging the Flash game to be open by PowerShell
We will use a Form with a WebBrowser object to manipulate the flash generator. For that, we need a html page wrapper, since we cannot just inline a .SWF file. Quick and dirty generator.html
content:
<object>
<embed src="anime_face_maker_2_by_gen8-d30uny4.swf" width="900" height="650"></embed>
</object>
Clicking around
We are really in the “meat” of the generator. Let’s say we have opened the window, clicked on “play” and that the game is fully loaded. We will simply use the Get-Random
function to generate coordinate where to click inside the window. To get more colorful picture, we will click first in the menus/feature selections, then in the palette. The proper way would be to define the clicking zone relative to the window, but since we force the position of the window, we will deal only in absolute (like a Sith).
Remember, since we forced a seed just before, we can replay the script and it will click at the same place in the same order. We loop 200 hundred times, because why not. Really, 50 would do too, but the difference of speed is negligible (since most of the is spent waiting for the windows and the SWF to load).
Putting it all together
The main function will create a form, add the webpage, click on “Play”, click (pseudo-)randomly around, do a screenshot, and close the form. The interesting part is the use of timers to have sequential actions. Each timer’ trigger unregisters itself (so it runs only once) and prepare the next one. Since the code is a bit long and full of boiler plate, here is just the overall gist, that can be run directly.
Once you have run the content of the gist once, you will have in the current folder the HTML template and the SWF file. You can then just run the command Generate-Avatar "PowerShell"
and check the result in the current directory.
Going further
I’m on the fence, either I make this script into a proper Module, or I just burn it down to the ground. I may want to also add a function to generate different expression, like a sleeping face (when the server is on maintenance) or anger (when the monitoring is getting errors).