From 6737e8c11b6d5986f7d1f054cea7ee42f0228cac Mon Sep 17 00:00:00 2001 From: MengYX Date: Sun, 8 Aug 2021 07:46:19 +0800 Subject: [PATCH] optimize: `.kgm` mask loading --- src/decrypt/kgm.ts | 14 ++++++++++---- tsconfig.json | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/decrypt/kgm.ts b/src/decrypt/kgm.ts index 55fcc29..63facc8 100644 --- a/src/decrypt/kgm.ts +++ b/src/decrypt/kgm.ts @@ -8,6 +8,7 @@ import { } from "@/decrypt/utils.ts"; import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {DecryptResult} from "@/decrypt/entity"; +import config from "@/../package.json" const VprHeader = [ 0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43, @@ -22,9 +23,6 @@ const VprMaskDiff = [ export async function Decrypt(file: File, raw_filename: string, raw_ext: string): Promise { - if (self.location.protocol === "file:") { - throw Error("请使用 CLI版本 进行解锁") - } const oriData = new Uint8Array(await GetArrayBuffer(file)); if (raw_ext === "vpr") { @@ -84,8 +82,16 @@ function GetMask(pos: number) { let MaskV2: Uint8Array = new Uint8Array(0); async function LoadMaskV2(): Promise { + let mask_url = `https://cdn.jsdelivr.net/gh/unlock-music/unlock-music@${config.version}/public/static/kgm.mask` + if (["http:", "https:"].some(v => v == self.location.protocol)) { + if (!!self.document) {// using Web Worker + mask_url = "./static/kgm.mask" + } else {// using Main thread + mask_url = "../static/kgm.mask" + } + } try { - const resp = await fetch(process.env.NODE_ENV !== 'production'?"./static/kgm.mask":"../static/kgm.mask", {method: "GET"}) + const resp = await fetch(mask_url, {method: "GET"}) MaskV2 = new Uint8Array(await resp.arrayBuffer()); return true } catch (e) { diff --git a/tsconfig.json b/tsconfig.json index fcc7a6f..88ddb2c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,7 +26,8 @@ "dom", "dom.iterable", "scripthost" - ] + ], + "resolveJsonModule": true }, "include": [ "src/**/*.ts",