Sharp Aion2 Lua SDK
Guides

Build an external window

Give a module its own desktop window written in HTML, CSS and JavaScript, with game icons.

A module can show its interface in a separate desktop window instead of an in-game panel. Sharp serves the module's window/ folder on your own computer (http://127.0.0.1:38473/<module>/) and opens it as an app window in Microsoft Edge or Google Chrome: no tabs, no address bar, its own icon. The Shugo Festival and Pet Farm modules in the Script Store use it.

For Store-installed windows, use Sharp 0.4.9 or newer and SDK 1.7.20 (native API 7). Use Download latest SDK in the Scripts tab so Scripts/lib/ has window_link.lua and json_lite.lua.

How it fits together

PieceWhereJob
main.luaScripts/modules/<id>/Game logic. Publishes a state table and handles commands.
window_linkScripts/lib/Sends the state to the window and receives the page's commands.
window/index.html, app.js, app.css, icon.pngScripts/modules/<id>/window/The page. Reads the state, sends commands.
Window hostSharp (DLL)Serves window/, relays state and commands, opens the window.

The module id can use letters, digits, _ and -, up to 64 characters (for example my_window). Store downloads receive names such as Shugo-Festival-6af4d20f. window_link obtains that actual installed ID from window.module_id(); the module option is only a fallback for an offline Python host. Keep page requests relative (api/state, api/command). The window opens by itself the first time the module starts; Open window in the Scripts tab, or window.open() from Lua, opens it again. "Window on start" in the Scripts tab turns the automatic opening off.

1. The module

Save as Scripts/modules/my_window/main.lua:

local WindowLink = require("window_link")

local count   = 0
local running = false

local link = WindowLink.New({
	module = "my_window",
	http = http,
	onCommand = function(command)
		-- Every command arrives once, in order, even if a reply was lost.
		if command.type == "toggle" then
			running = command.value == true
		elseif command.type == "reset" then
			count = 0
		end
	end,
})

function on_tick(dt)
	if running then
		count = count + 1
	end

	-- Published every frame; only sent when it changed.
	link:Publish({ count = count, running = running })
	link:Tick()
end

function on_http(requestId, status, body, failure)
	link:OnHttp(requestId, status, body, failure)
end

WindowLink.New takes the module id, the http table (it posts with http.post_json), and onCommand. Call Publish with a plain table (strings, numbers, booleans, nested tables) and Tick every frame, and forward on_http to OnHttp. link.Connected is true while the host answers. Keep the state small: one sync carries at most 64 KiB.

2. The page

Save as Scripts/modules/my_window/window/index.html:

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>My window</title>
	<link rel="icon" type="image/png" href="icon.png">
	<link rel="stylesheet" href="app.css">
</head>
<body>
	<h1>Counter <span id="status">Not connected</span></h1>
	<p id="count">0</p>
	<button id="toggle">Start</button>
	<button id="reset">Reset</button>
	<script src="app.js"></script>
</body>
</html>

And window/app.js:

let revision = null;
let state = null;

// The Lua JSON encoder writes an empty table as {}; treat both as empty.
const list = (value) => (Array.isArray(value) ? value : []);

async function send(command) {
	await fetch("api/command", {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify(command),
	});
}

function render(connected) {
	document.getElementById("status").textContent = connected ? "Connected" : "Not connected";
	document.getElementById("count").textContent = state ? state.count : "0";
	document.getElementById("toggle").textContent = state && state.running ? "Stop" : "Start";
}

async function poll() {
	for (;;) {
		try {
			// Waits up to 15 s for a newer state, so the page updates as soon as the module publishes.
			const query = revision === null ? "" : `?after=${revision}&wait=1`;
			const reply = await (await fetch(`api/state${query}`, { cache: "no-store" })).json();
			revision = reply.revision;
			state = reply.state;
			render(reply.connected);
		} catch (error) {
			render(false);
			await new Promise((resolve) => setTimeout(resolve, 1000));
		}
	}
}

document.getElementById("toggle").onclick = () => send({ type: "toggle", value: !(state && state.running) });
document.getElementById("reset").onclick = () => send({ type: "reset" });
poll();

Add app.css for the look and a square icon.png (256 px) for the window and taskbar icon. Load my_window in the Scripts tab: the window opens and counts while it runs.

The page's two endpoints

RequestAnswer
GET api/state{"revision": n, "connected": true/false, "state": {...}}
GET api/state?after=n&wait=1The same, as soon as the revision is newer than n (at most 15 s)
POST api/command with a JSON object that has a type{"queued": id}, or HTTP 409 when the module is not running

Commands come only from the module's own page, and the module only receives commands queued while it is connected. A reloaded module starts with an empty command queue.

3. Game icons and maps

The window can show the client's own pictures from the shared asset pack. Sharp downloads each file on first use, checks its SHA-256 and keeps it in Scripts/assets/:

PathPicture
/assets/items/<IconName>.pngItem icon, 64 px. <IconName> is the item's icon in sdk.data.items().
/assets/pets/<petId>.pngPet portrait, 64 px.
/assets/maps/<mapId>.jpgWorld map image.

Some items have no picture in the client. Show a fallback underneath and drop the image when it fails:

<div class="item">
	<span class="initials">RS</span>
	<img src="/assets/items/Icon_Item_Craft_SmeltStone_Equip_A_l_001.png" alt="" onerror="this.remove()">
</div>

Ship your own images (logos, backgrounds) inside window/ instead.

4. Publish it

Upload the module from the Scripts tab as usual. The Store package keeps window/ next to the Lua files, and players get the same window after installing it. Set the listing's minimum Sharp version to 0.4.9. Bundle window_link.lua and json_lite.lua with the module if it must also run on an older SDK. The Store packages include both libraries and need no Python installation on the player's PC; the window host is part of the Sharp client.

Rules the host enforces

  • Only 127.0.0.1 and localhost on port 38473; other host names get HTTP 403.
  • Files are served from window/ only: .html, .css, .js, .json, .png, .jpg, .webp, .svg, .ico, .woff2. Lua files are never served.
  • A browser cannot impersonate the module, and other web pages cannot send commands to it.
  • The window is a normal browser page: it cannot read game memory or call Lua directly. Everything goes through the published state and commands.

On this page