Skip to content

Draft: completely scripted activation #6

@alexanderbuhler

Description

@alexanderbuhler

Hey there,

since all your content has helped me alot with setting up dockerized unity I wanted to share my working approach to activating unity by script.

node/puppeteer script:

const puppeteer = require('puppeteer');
const fs = require('fs');

try {
    fs.mkdirSync('debug_images');
    console.log('created debug folder.');

} catch (e) {
    console.log('debug folder already present.');
}


(async () => {

    const browser = await puppeteer.launch({
        args: ["--no-sandbox"]
    });
    const page = await browser.newPage();

    // open manual page & wait for login redirect

	await page.goto('https://license.unity3d.com/manual');

	const mailInputSelector = '#conversations_create_session_form_email',
		  passInputSelector = '#conversations_create_session_form_password';

	await page.waitForSelector(mailInputSelector);

    // enter credentials

	await page.type(mailInputSelector, process.env.UNITY_USERNAME);
	await page.type(passInputSelector, process.env.UNITY_PASSWORD);

	await page.screenshot({ path: 'debug_images/01_entered_credentials.png' });

    // click submit

	await page.click('input[name=commit]');

    // wait for license upload form

	const licenseUploadfield = '#licenseFile';

	await page.waitForSelector(licenseUploadfield);

	await page.screenshot({ path: 'debug_images/02_opened_form.png' });

    // enalbe interception
    
	await page.setRequestInterception(true);

    // upload license

	page.once("request", interceptedRequest => {
		
        interceptedRequest.continue({
            method: "POST",
            postData: fs.readFileSync(process.env.UNITY_ACTIVATION_FILE, 'utf8'),
            headers: { "Content-Type": "text/xml" },
        });

	});

	await page.goto('https://license.unity3d.com/genesis/activation/create-transaction');

	await page.screenshot({ path: 'debug_images/03_created_transaction.png' });

    // set license to be personal

    page.once("request", interceptedRequest => {
        interceptedRequest.continue({
            method: "PUT",
            postData: JSON.stringify({ transaction: { serial: { type: "personal" } } }),
            headers: { "Content-Type": "application/json" }
        });
    });

	await page.goto('https://license.unity3d.com/genesis/activation/update-transaction');

    await page.screenshot({ path: 'debug_images/04_updated_transaction.png' });
    
    // get license content

    page.once("request", interceptedRequest => {
        interceptedRequest.continue({
            method: "POST",
            postData: JSON.stringify({}),
            headers: { "Content-Type": "application/json" }
        });
    });

    page.on('response', async response => {  
                
        // write license

        try {
            const data = await response.text();
            const dataJson = await JSON.parse(data);
            fs.writeFileSync(process.env.UNITY_LICENSE_FILE, dataJson.xml);
            console.log('license file written.');

            await page.screenshot({ path: 'debug_images/05_received_license.png' });
            
        } catch (e) {
            console.log(e);
            console.log('failed to write license file.');
        }

    });

    await page.goto('https://license.unity3d.com/genesis/activation/download-license');
    await page.waitFor(1000);
    await browser.close();



})();

And then there's just a small shell script handling the rest. Please note that (only?) Unity 2018.3 cli supports manual generation of activation file, which comes in handy here.

#!/bin/sh

xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \
  /opt/Unity/Editor/Unity -batchmode -createManualActivationFile -nographics -logfile

find -name "Unity*" -exec mv {} ../$UNITY_ACTIVATION_FILE \;
if [ $? -ne 0 ]
then
    echo "error while preparing generated license activation file."
	exit 3
fi


cd ../

# run puppeteer activation macro
node index.js
if [ $? -ne 0 ]
then
    echo "error while retrieving license."
	exit 3
fi

mv $UNITY_LICENSE_FILE /root/.local/share/unity3d/Unity/

if [ $? -eq 0 ]
then
    echo "license file installed."
	exit 0
fi

Involved env vars:

[email protected]
UNITY_PASSWORD=XXXXXXXXXXXXX
UNITY_ACTIVATION_FILE=unity3d.alf
UNITY_LICENSE_FILE=Unity_lic.ulf

You might want to take a look at how to implement this into your images 👍

Best
Alex

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions