Skip to content

Commit

Permalink
Support for upstream EasyRPG
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Jan 8, 2024
1 parent 90252a1 commit 4b969bf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion chat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Module["onRuntimeInitialized"] = initChat;
easyrpgPlayer["onRuntimeInitialized"] = initChat;
if (typeof ENV === "undefined")
initChat();

Expand Down
19 changes: 17 additions & 2 deletions gamecanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ const canvas = document.getElementById('canvas');
const gameContainer = document.getElementById('gameContainer');
let lastTouchedId;

// Launch the Player and configure it
window.addEventListener('load', (event) => {
createEasyRpgPlayer(easyrpgPlayer)
.then(function(Module) {
// Module is ready
easyrpgPlayer = Module;
easyrpgPlayer.initApi();

for (let loadFunc of easyrpgPlayerLoadFuncs)
loadFunc();

canvas.focus();
});
});

// Make EasyRPG player embeddable
gameContainer.addEventListener('mouseenter', () => canvas.focus());
gameContainer.addEventListener('click', () => canvas.focus());
canvas.addEventListener('mouseenter', () => canvas.focus());
canvas.addEventListener('click', () => canvas.focus());

// Handle clicking on the fullscreen button
document.querySelector('#controls-fullscreen').addEventListener('click', () => {
Expand Down
26 changes: 14 additions & 12 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ const tippyConfig = {
allowHTML: true
};

let easyrpgPlayer = {
initialized: false,
game: ynoGameId,
saveFs: undefined,
wsUrl: 'wss://connect.ynoproject.net/' + ynoGameId + '/'
};
let easyrpgPlayerLoadFuncs = [];

const sessionIdKey = 'ynoproject_sessionId';
const serverUrl = `https://connect.ynoproject.net/${ynoGameId}`;
const apiUrl = `${serverUrl}/api`;
const adminApiUrl = `${serverUrl}/admin`;
const ynomojiUrlPrefix = 'images/ynomoji/';

Module = {
INITIALIZED: false,
EASYRPG_GAME: ynoGameId,
EASYRPG_WS_URL: 'wss://connect.ynoproject.net/' + ynoGameId + '/'
};

async function injectScripts() {
const supportsSimd = await wasmFeatureDetect.simd();

Expand Down Expand Up @@ -78,11 +80,11 @@ async function injectScripts() {
if (globalConfig.preloads)
initPreloads();

Module.postRun.push(() => {
Module.INITIALIZED = true;
Module._SetNametagMode(config.nametagMode);
Module._SetSoundVolume(globalConfig.soundVolume);
Module._SetMusicVolume(globalConfig.musicVolume);
easyrpgPlayerLoadFuncs.push(() => {
easyrpgPlayer.initialized = true;
easyrpgPlayer._SetNametagMode(config.nametagMode);
easyrpgPlayer._SetSoundVolume(globalConfig.soundVolume);
easyrpgPlayer._SetMusicVolume(globalConfig.musicVolume);
const loadingOverlay = document.getElementById('loadingOverlay');
removeLoader(loadingOverlay);
checkShowVersionUpdate().then(() => loadingOverlay.classList.add('loaded'));
Expand All @@ -98,7 +100,7 @@ async function injectScripts() {
};
});
if (typeof onResize !== 'undefined')
Module.postRun.push(onResize);
easyrpgPlayerLoadFuncs.push(onResize);
};

const scriptTag = document.createElement('script');
Expand Down
34 changes: 17 additions & 17 deletions play.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ function fetchAndUpdatePlayerInfo() {
const isLogout = !cookieSessionId && loginToken && cookieSessionId !== loginToken;
if (isLogin || isLogout) {
loginToken = isLogin ? cookieSessionId : null;
const ptr = Module.allocate(Module.intArrayFromString(isLogin ? loginToken : ''), Module.ALLOC_NORMAL);
Module._SetSessionToken(ptr);
Module._free(ptr);
const ptr = easyrpgPlayer.allocate(easyrpgPlayer.intArrayFromString(isLogin ? loginToken : ''), easyrpgPlayer.ALLOC_NORMAL);
easyrpgPlayer._SetSessionToken(ptr);
easyrpgPlayer._free(ptr);
}
apiFetch('info')
.then(response => response.json())
Expand Down Expand Up @@ -480,8 +480,8 @@ function onReceiveInputFeedback(inputId) {
config[configKey] = toggled;
updateConfig(isGlobal ? globalConfig : config, isGlobal);
if (configKey === 'mute') {
Module._SetSoundVolume(toggled ? 0 : globalConfig.soundVolume);
Module._SetMusicVolume(toggled ? 0 : globalConfig.musicVolume);
easyrpgPlayer._SetSoundVolume(toggled ? 0 : globalConfig.soundVolume);
easyrpgPlayer._SetMusicVolume(toggled ? 0 : globalConfig.musicVolume);
}
}
}
Expand Down Expand Up @@ -741,7 +741,7 @@ document.getElementById('privateModeButton').onclick = function () {
if (connStatus == 1 || connStatus == 3)
onUpdateConnectionStatus(config.privateMode ? 3 : 1);

Module._SessionReady();
easyrpgPlayer._SessionReady();
};

let reconnectCooldownTimer;
Expand Down Expand Up @@ -877,8 +877,8 @@ document.getElementById('clearChatButton').onclick = function () {
document.getElementById('settingsButton').onclick = () => openModal('settingsModal');

document.getElementById('muteButton').onclick = function () {
if (Module.INITIALIZED)
Module._ToggleMute();
if (easyrpgPlayer.INITIALIZED)
easyrpgPlayer._ToggleMute();
};

document.getElementById('soundVolume').oninput = function () {
Expand All @@ -902,13 +902,13 @@ document.getElementById('lang').onchange = function () {
};

document.getElementById('nametagMode').onchange = function () {
if (Module.INITIALIZED)
Module._SetNametagMode(this.value);
if (easyrpgPlayer.INITIALIZED)
easyrpgPlayer._SetNametagMode(this.value);
};

document.getElementById('playerSoundsButton').onclick = () => {
if (Module.INITIALIZED)
Module._TogglePlayerSounds();
if (easyrpgPlayer.INITIALIZED)
easyrpgPlayer._TogglePlayerSounds();
};

if (gameId === '2kki') {
Expand Down Expand Up @@ -1448,7 +1448,7 @@ document.onmousemove = function () {
function setLang(lang, isInit) {
globalConfig.lang = lang;
if (isInit && gameIds.indexOf(gameId) > -1)
Module.EASYRPG_LANGUAGE = (gameDefaultLangs.hasOwnProperty(gameId) ? gameDefaultLangs[gameId] !== lang : lang !== 'en') ? lang : 'default';
easyrpgPlayer.language = (gameDefaultLangs.hasOwnProperty(gameId) ? gameDefaultLangs[gameId] !== lang : lang !== 'en') ? lang : 'default';
initLocalization(isInit);
if (!isInit)
updateConfig(globalConfig, true);
Expand All @@ -1463,8 +1463,8 @@ function setName(name, isInit) {
function setSoundVolume(value, isInit) {
if (isNaN(value))
return;
if (Module.INITIALIZED && !config.mute)
Module._SetSoundVolume(value);
if (easyrpgPlayer.INITIALIZED && !config.mute)
easyrpgPlayer._SetSoundVolume(value);
globalConfig.soundVolume = value;
if (!isInit)
updateConfig(globalConfig, true);
Expand All @@ -1473,8 +1473,8 @@ function setSoundVolume(value, isInit) {
function setMusicVolume(value, isInit) {
if (isNaN(value))
return;
if (Module.INITIALIZED && !config.mute)
Module._SetMusicVolume(value);
if (easyrpgPlayer.INITIALIZED && !config.mute)
easyrpgPlayer._SetMusicVolume(value);
globalConfig.musicVolume = value;
if (!isInit)
updateConfig(globalConfig, true);
Expand Down
8 changes: 4 additions & 4 deletions screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ function takeScreenshot(retryCount) {

const mapId = cachedMapId;

const coords = Module._GetPlayerCoords();
const coords = easyrpgPlayer._GetPlayerCoords();

const mapX = Module.getValue(coords, 'int*');
const mapY = Module.getValue(coords + 4, 'int*');
const mapX = easyrpgPlayer.getValue(coords, 'int*');
const mapY = easyrpgPlayer.getValue(coords + 4, 'int*');

Module._free(coords);
easyrpgPlayer._free(coords);

if (notificationConfig.all && notificationConfig.screenshots.all && notificationConfig.screenshots.screenshotTaken) {
const toast = showScreenshotToastMessage('screenshotTaken', 'image', true, null, true);
Expand Down
2 changes: 1 addition & 1 deletion session.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function initSessionWs(attempt) {
return;
setTimeout(() => initSessionWs(1), 5000);
};
Module._SessionReady();
easyrpgPlayer._SessionReady();
if (config.privateMode)
sendSessionCommand('pr', [ 1 ]);
if (!hasConnected) {
Expand Down

0 comments on commit 4b969bf

Please sign in to comment.