Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
fix: Unsafe access to global dojo
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Oct 13, 2022
1 parent c7bb23a commit 1cd58fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/userscript/source/ScienceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ScienceManager extends UpgradeManager {
continue;
}

let prices = dojo.clone(tech.prices);
let prices = UserScript.window.dojo.clone(tech.prices);
prices = this._host.gamePage.village.getEffectLeader("scientist", prices);
for (const resource of prices) {
if (this._workshopManager.getValueAvailable(resource.name, true) < resource.val) {
Expand Down
22 changes: 11 additions & 11 deletions packages/userscript/source/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ declare global {
const KG_SAVEGAME: string | null;
const KS_VERSION: string | null;
let unsafeWindow: Window | undefined;
const dojo: {
clone: <T>(subject: T) => T;
subscribe: (event: string, handler: () => void) => void;
};
interface Window {
dojo: {
clone: <T>(subject: T) => T;
subscribe: (event: string, handler: () => void) => void;
};
gamePage?: Maybe<GamePage>;
$: JQuery;
$I?: Maybe<I18nEngine>;
Expand Down Expand Up @@ -194,12 +194,12 @@ export class UserScript {
static async waitForGame(timeout = 30000): Promise<GamePage> {
const signals: Array<Promise<unknown>> = [sleep(2000)];

if (isNil(UserScript._gameStartSignal) && typeof dojo !== "undefined") {
if (isNil(UserScript._gameStartSignal) && typeof UserScript.window.dojo !== "undefined") {
UserScript._gameStartSignal = new Promise(resolve => {
UserScript._gameStartSignalResolver = resolve;
});

dojo.subscribe("game/start", () => {
UserScript.window.dojo.subscribe("game/start", () => {
cdebug("`game/start` signal caught. Fast-tracking script load...");
mustExist(UserScript._gameStartSignalResolver)(true);
});
Expand All @@ -214,7 +214,7 @@ export class UserScript {
}

if (UserScript._isGameLoaded()) {
return mustExist(UserScript._window.gamePage);
return mustExist(UserScript.window.gamePage);
}

cdebug(`Waiting for game... (timeout: ${Math.round(timeout / 1000)}s)`);
Expand All @@ -225,18 +225,18 @@ export class UserScript {

static getDefaultInstance(): UserScript {
const instance = new UserScript(
mustExist(UserScript._window.gamePage),
mustExist(UserScript._window.$I),
mustExist(UserScript.window.gamePage),
mustExist(UserScript.window.$I),
localStorage["com.nuclearunicorn.kittengame.language"] as SupportedLanguages | undefined
);
return instance;
}

private static _isGameLoaded(): boolean {
return !isNil(UserScript._window.gamePage);
return !isNil(UserScript.window.gamePage);
}

private static get _window(): Window {
static get window(): Window {
try {
return unsafeWindow as Window;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/userscript/source/WorkshopManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class WorkshopManager extends UpgradeManager implements Automation {
}

// Create a copy of the prices for this upgrade, so that we can apply effects to it.
let prices = dojo.clone(upgrade.prices);
let prices = UserScript.window.dojo.clone(upgrade.prices);
prices = this._host.gamePage.village.getEffectLeader("scientist", prices);
for (const resource of prices) {
// If we can't afford this resource price, continue with the next upgrade.
Expand Down

0 comments on commit 1cd58fa

Please sign in to comment.