Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
cacher: extra validation and use fs
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Jan 16, 2024
1 parent 00817fa commit abd7119
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/core/native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const nmp = nativeModuleProxy;

const {
RTNFileManager
} = nativeModuleProxy;
} = nmp;

const ClientInfo = nmp.InfoDictionaryManager ?? nmp.RTNClientInfoManager;

export function getBuildNumber() {
return ClientInfo.Build;
}

export async function removeFile(path: string, prefix = "pyoncord/") {
return void await RTNFileManager.removeFile("documents", `${prefix}${path}`);
Expand Down Expand Up @@ -28,7 +36,7 @@ export async function readFile(path: string, fallback?: string, prefix = "pyonco
throw new Error(`File ${path} doesn't exist`);
}

writeFile(path, fallback);
await writeFile(path, fallback);
return fallback;
}
}
4 changes: 3 additions & 1 deletion src/entry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getBuildNumber } from "@native";

import commitHash from "~commit-hash";

console.log(`ピョン! (Pyoncord, hash=${commitHash}, dev=${__PYONCORD_DEV__})`);
Expand All @@ -23,7 +25,7 @@ console.log(`ピョン! (Pyoncord, hash=${commitHash}, dev=${__PYONCORD_DEV__})`
"Failed to load Pyoncord.\n",
`Build Hash: ${commitHash}`,
`Debug Build: ${__PYONCORD_DEV__}`,
`Build Number: ${nativeModuleProxy.RTNClientInfoManager?.Build}`,
`Build Number: ${getBuildNumber()}`,
error
].join("\n"));

Expand Down
26 changes: 13 additions & 13 deletions src/internal/cacher.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { findInitializedModule } from "@internal/metro";
import { getRequireDefintion } from "@internal/requireDef";
import { getBuildNumber, readFile, writeFile } from "@native";
import { before } from "spitroast";

import moduleDefinitionHash from "~module-definition-hash";

const MMKVManager = nativeModuleProxy.MMKVManager as any;
const currentVersion = nativeModuleProxy.RTNClientInfoManager.Build;
const currentVersion = getBuildNumber();
const window = globalThis as typeof globalThis & { [key: string]: any; };

async function getItem() {
const raw = await MMKVManager.getItem("pyon-cache");
if (raw == null) return;
return JSON.parse(raw);
async function getAndParseCache() {
try {
return JSON.parse(await readFile("pyon-cache"));
} catch {
return null;
}
}

async function updateItemAndRestart(value: any, key = "pyon-cache") {
MMKVManager.removeItem(key);
MMKVManager.setItem(key, JSON.stringify(value));
await MMKVManager.getItem(key);
async function writeCacheAndRestart(value: any, key = "pyon-cache") {
await writeFile(key, JSON.stringify(value));
window.nativeModuleProxy.BundleUpdaterManager.reload();
}

Expand Down Expand Up @@ -89,7 +89,7 @@ async function beginCache() {
}
}

await updateItemAndRestart(cache);
await writeCacheAndRestart(cache);
}

function isCacheInvalidated(cache: any) {
Expand All @@ -98,7 +98,7 @@ function isCacheInvalidated(cache: any) {
}

export async function initCache() {
const cached = await getItem();
const cached = await getAndParseCache();

if (cached && !isCacheInvalidated(cached)) {
window.pyonRequire = function pyonRequire(id: string) {
Expand All @@ -115,6 +115,6 @@ export async function initCache() {

window.ErrorUtils = void 0;
window.__startDiscord?.();
await beginCache().then(console.error);
await beginCache().catch(alert);
await new Promise(() => void 0);
}

0 comments on commit abd7119

Please sign in to comment.