gengxin
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// Transport-only lookup. Review eligibility continues to belong to the story
|
||||
// and release manifests; an HTTPS URL never confers audio approval.
|
||||
const manifest = require('./cosMediaManifest')
|
||||
const paths = Object.create(null)
|
||||
const urls = Object.create(null)
|
||||
const SHARE_ASSET_ID = 'image.tang.share-preview'
|
||||
|
||||
for (const entry of manifest.entries) {
|
||||
paths[entry.sourcePath] = entry
|
||||
urls[entry.url] = entry
|
||||
}
|
||||
|
||||
function entryFor(value) {
|
||||
if (typeof value !== 'string') return null
|
||||
if (urls[value]) return urls[value]
|
||||
let relative = value.replace(/^\//, '')
|
||||
if (relative.startsWith(`${manifest.namespace}/`)) {
|
||||
relative = relative.slice(manifest.namespace.length + 1)
|
||||
}
|
||||
return paths[relative] || null
|
||||
}
|
||||
|
||||
function resolve(value) {
|
||||
const entry = entryFor(value)
|
||||
return entry ? entry.url : ''
|
||||
}
|
||||
|
||||
function verifiedUrl(value, sha256, kind) {
|
||||
const entry = urls[value]
|
||||
return entry && entry.sha256 === sha256 && entry.kind === kind
|
||||
? entry.url : ''
|
||||
}
|
||||
|
||||
function mapMedia(value) {
|
||||
if (typeof value === 'string') {
|
||||
if (/^\/(?:[a-z0-9-]+\/)?(?:assets|package-[a-z0-9-]+)\/.*\.(?:jpe?g|png|webp|gif|svg|avif|mp3|wav|aac|m4a|ogg|mp4|webm|mov)$/i.test(value)) {
|
||||
// Unregistered declarations remain text-only; never guess an object URL.
|
||||
return resolve(value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
if (Array.isArray(value)) return value.map(mapMedia)
|
||||
if (!value || typeof value !== 'object') return value
|
||||
const mapped = {}
|
||||
Object.keys(value).forEach(key => { mapped[key] = mapMedia(value[key]) })
|
||||
return mapped
|
||||
}
|
||||
|
||||
function beginComicImage(page, pageData) {
|
||||
const previous = page._cosComicImage
|
||||
if (!previous || previous.pageId !== pageData.currentPageId) {
|
||||
page._cosComicImage = {
|
||||
pageId: pageData.currentPageId,
|
||||
generation: (previous ? previous.generation : 0) + 1,
|
||||
failed: Object.create(null),
|
||||
lastPatch: null,
|
||||
}
|
||||
}
|
||||
const state = page._cosComicImage
|
||||
pageData.comicImageGeneration = state.generation
|
||||
// Ordinary reader interactions re-render this page. They must not revive a
|
||||
// URL that already failed or replace terminal text fallback with an image.
|
||||
if (state.lastPatch) Object.assign(pageData, state.lastPatch)
|
||||
}
|
||||
|
||||
function isCurrentComicImageEvent(page, event) {
|
||||
const state = page._cosComicImage
|
||||
const dataset = event && event.currentTarget && event.currentTarget.dataset
|
||||
return Boolean(state && dataset && !page.__tangDead && !page.__tangOriginalUnloaded
|
||||
&& page.__tangVisible !== false && page.data.comicImageSrc
|
||||
&& state.pageId === page.data.currentPageId
|
||||
&& dataset.cosPageId === state.pageId
|
||||
&& Number(dataset.cosImageGeneration) === state.generation
|
||||
&& dataset.cosImageSrc === page.data.comicImageSrc)
|
||||
}
|
||||
|
||||
function recordComicImageError(page, event) {
|
||||
if (!isCurrentComicImageEvent(page, event)) return false
|
||||
const state = page._cosComicImage
|
||||
const src = page.data.comicImageSrc
|
||||
if (state.failed[src]) return false
|
||||
state.failed[src] = true
|
||||
return true
|
||||
}
|
||||
|
||||
function canUseComicFallback(page, src) {
|
||||
const state = page._cosComicImage
|
||||
const entry = entryFor(src)
|
||||
return Boolean(state && entry && entry.kind === 'image' && !state.failed[src])
|
||||
}
|
||||
|
||||
function applyComicImageFallback(page, patch) {
|
||||
if (page._cosComicImage) page._cosComicImage.lastPatch = { ...patch }
|
||||
page.setData(patch)
|
||||
}
|
||||
|
||||
function prepareSharePreview(page, assetManager) {
|
||||
if (page._sharePreviewPromise) return page._sharePreviewPromise
|
||||
const generation = page.__tangShowGeneration
|
||||
const alive = () => !page.__tangDead && !page.__tangOriginalUnloaded
|
||||
&& page.__tangVisible !== false && page.__tangShowGeneration === generation
|
||||
// Resolve again for each share; the manager rehashes cached bytes before use.
|
||||
const promise = Promise.resolve().then(() => assetManager.resolve(SHARE_ASSET_ID))
|
||||
.then(result => {
|
||||
if (!alive()) return ''
|
||||
const localPath = result && result.available && result.uri || ''
|
||||
page.setData({ sharePreviewLocalPath: localPath })
|
||||
return localPath
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive()) page.setData({ sharePreviewLocalPath: '' })
|
||||
return ''
|
||||
})
|
||||
.finally(() => {
|
||||
if (page._sharePreviewPromise === promise) page._sharePreviewPromise = null
|
||||
})
|
||||
page._sharePreviewPromise = promise
|
||||
return promise
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SHARE_ASSET_ID, entryFor, resolve, verifiedUrl, mapMedia, prepareSharePreview,
|
||||
beginComicImage, isCurrentComicImageEvent, recordComicImageError,
|
||||
canUseComicFallback, applyComicImageFallback,
|
||||
}
|
||||
@@ -0,0 +1,674 @@
|
||||
const CACHE_INDEX_VERSION = 2
|
||||
const CACHE_FOLDER = 'tang-detective-assets-v2'
|
||||
const CACHE_INDEX_FILE = 'index.json'
|
||||
const DEFAULT_IMAGE_BUDGET = 28 * 1024 * 1024
|
||||
const DEFAULT_AUDIO_BUDGET = 32 * 1024 * 1024
|
||||
const DEFAULT_AUDIO_MAX_ENTRIES = 32
|
||||
|
||||
const SHA256_ROUND_CONSTANTS = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
||||
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
||||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
||||
]
|
||||
|
||||
function utf8Bytes(value) {
|
||||
const encoded = encodeURIComponent(String(value || ''))
|
||||
const bytes = []
|
||||
for (let index = 0; index < encoded.length; index += 1) {
|
||||
if (encoded[index] === '%') {
|
||||
bytes.push(parseInt(encoded.slice(index + 1, index + 3), 16))
|
||||
index += 2
|
||||
} else {
|
||||
bytes.push(encoded.charCodeAt(index))
|
||||
}
|
||||
}
|
||||
return new Uint8Array(bytes)
|
||||
}
|
||||
|
||||
function toBytes(value) {
|
||||
if (typeof value === 'string') return utf8Bytes(value)
|
||||
if (value instanceof ArrayBuffer) return new Uint8Array(value)
|
||||
if (ArrayBuffer.isView(value)) {
|
||||
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
||||
}
|
||||
throw new Error('unsupported SHA-256 input')
|
||||
}
|
||||
|
||||
function rotateRight(value, bits) {
|
||||
return (value >>> bits) | (value << (32 - bits))
|
||||
}
|
||||
|
||||
function sha256Hex(value) {
|
||||
const bytes = toBytes(value)
|
||||
const paddedLength = Math.ceil((bytes.length + 9) / 64) * 64
|
||||
const message = new Uint8Array(paddedLength)
|
||||
message.set(bytes)
|
||||
message[bytes.length] = 0x80
|
||||
const bitLength = bytes.length * 8
|
||||
const highBits = Math.floor(bitLength / 0x100000000)
|
||||
const lowBits = bitLength >>> 0
|
||||
const lengthOffset = paddedLength - 8
|
||||
message[lengthOffset] = (highBits >>> 24) & 0xff
|
||||
message[lengthOffset + 1] = (highBits >>> 16) & 0xff
|
||||
message[lengthOffset + 2] = (highBits >>> 8) & 0xff
|
||||
message[lengthOffset + 3] = highBits & 0xff
|
||||
message[lengthOffset + 4] = (lowBits >>> 24) & 0xff
|
||||
message[lengthOffset + 5] = (lowBits >>> 16) & 0xff
|
||||
message[lengthOffset + 6] = (lowBits >>> 8) & 0xff
|
||||
message[lengthOffset + 7] = lowBits & 0xff
|
||||
|
||||
const hash = [
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
||||
]
|
||||
const words = new Uint32Array(64)
|
||||
for (let offset = 0; offset < paddedLength; offset += 64) {
|
||||
for (let index = 0; index < 16; index += 1) {
|
||||
const start = offset + index * 4
|
||||
words[index] = (
|
||||
(message[start] << 24)
|
||||
| (message[start + 1] << 16)
|
||||
| (message[start + 2] << 8)
|
||||
| message[start + 3]
|
||||
) >>> 0
|
||||
}
|
||||
for (let index = 16; index < 64; index += 1) {
|
||||
const word15 = words[index - 15]
|
||||
const word2 = words[index - 2]
|
||||
const sigma0 = (
|
||||
rotateRight(word15, 7)
|
||||
^ rotateRight(word15, 18)
|
||||
^ (word15 >>> 3)
|
||||
)
|
||||
const sigma1 = (
|
||||
rotateRight(word2, 17)
|
||||
^ rotateRight(word2, 19)
|
||||
^ (word2 >>> 10)
|
||||
)
|
||||
words[index] = (
|
||||
words[index - 16]
|
||||
+ sigma0
|
||||
+ words[index - 7]
|
||||
+ sigma1
|
||||
) >>> 0
|
||||
}
|
||||
|
||||
let a = hash[0]
|
||||
let b = hash[1]
|
||||
let c = hash[2]
|
||||
let d = hash[3]
|
||||
let e = hash[4]
|
||||
let f = hash[5]
|
||||
let g = hash[6]
|
||||
let h = hash[7]
|
||||
for (let index = 0; index < 64; index += 1) {
|
||||
const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25)
|
||||
const choice = (e & f) ^ (~e & g)
|
||||
const temp1 = (
|
||||
h + sum1 + choice + SHA256_ROUND_CONSTANTS[index] + words[index]
|
||||
) >>> 0
|
||||
const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22)
|
||||
const majority = (a & b) ^ (a & c) ^ (b & c)
|
||||
const temp2 = (sum0 + majority) >>> 0
|
||||
h = g
|
||||
g = f
|
||||
f = e
|
||||
e = (d + temp1) >>> 0
|
||||
d = c
|
||||
c = b
|
||||
b = a
|
||||
a = (temp1 + temp2) >>> 0
|
||||
}
|
||||
hash[0] = (hash[0] + a) >>> 0
|
||||
hash[1] = (hash[1] + b) >>> 0
|
||||
hash[2] = (hash[2] + c) >>> 0
|
||||
hash[3] = (hash[3] + d) >>> 0
|
||||
hash[4] = (hash[4] + e) >>> 0
|
||||
hash[5] = (hash[5] + f) >>> 0
|
||||
hash[6] = (hash[6] + g) >>> 0
|
||||
hash[7] = (hash[7] + h) >>> 0
|
||||
}
|
||||
return hash.map((word) => word.toString(16).padStart(8, '0')).join('')
|
||||
}
|
||||
|
||||
function constantTimeEqualHex(left, right) {
|
||||
const leftValue = String(left || '').toLowerCase()
|
||||
const rightValue = String(right || '').toLowerCase()
|
||||
const length = Math.max(leftValue.length, rightValue.length)
|
||||
let difference = leftValue.length ^ rightValue.length
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
const leftCode = index < leftValue.length ? leftValue.charCodeAt(index) : 0
|
||||
const rightCode = index < rightValue.length ? rightValue.charCodeAt(index) : 0
|
||||
difference |= leftCode ^ rightCode
|
||||
}
|
||||
return difference === 0
|
||||
}
|
||||
|
||||
function trimSlash(value) {
|
||||
return String(value || '').replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
function joinPath(left, right) {
|
||||
return `${trimSlash(left)}/${String(right || '').replace(/^\/+/, '')}`
|
||||
}
|
||||
|
||||
function normalizeIndex(value) {
|
||||
if (
|
||||
!value
|
||||
|| value.version !== CACHE_INDEX_VERSION
|
||||
|| !value.entries
|
||||
|| typeof value.entries !== 'object'
|
||||
) {
|
||||
return {
|
||||
version: CACHE_INDEX_VERSION,
|
||||
entries: {},
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function callFs(fs, method, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!fs || typeof fs[method] !== 'function') {
|
||||
reject(new Error(`fs.${method} unavailable`))
|
||||
return
|
||||
}
|
||||
fs[method]({
|
||||
...options,
|
||||
success: resolve,
|
||||
fail: reject,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function createAssetPlatformFacade() {
|
||||
if (typeof wx === 'undefined') return null
|
||||
const env = Object.freeze({
|
||||
USER_DATA_PATH: String(
|
||||
wx.env && wx.env.USER_DATA_PATH || '',
|
||||
),
|
||||
})
|
||||
return Object.freeze({
|
||||
downloadFile(options) {
|
||||
if (typeof wx.downloadFile !== 'function') {
|
||||
if (options && typeof options.fail === 'function') {
|
||||
options.fail(new Error('wx.downloadFile unavailable'))
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
return wx.downloadFile(options)
|
||||
},
|
||||
getFileSystemManager() {
|
||||
if (typeof wx.getFileSystemManager !== 'function') return null
|
||||
return wx.getFileSystemManager()
|
||||
},
|
||||
env,
|
||||
})
|
||||
}
|
||||
|
||||
function download(assetPlatform, url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!assetPlatform || typeof assetPlatform.downloadFile !== 'function') {
|
||||
reject(new Error('wx.downloadFile unavailable'))
|
||||
return
|
||||
}
|
||||
assetPlatform.downloadFile({
|
||||
url,
|
||||
success(result) {
|
||||
if (
|
||||
result
|
||||
&& result.statusCode >= 200
|
||||
&& result.statusCode < 300
|
||||
&& result.tempFilePath
|
||||
) {
|
||||
resolve(result)
|
||||
return
|
||||
}
|
||||
reject(new Error(`download status ${result && result.statusCode}`))
|
||||
},
|
||||
fail: reject,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function safeCacheName(assetId, asset) {
|
||||
const extensionMatch = String(asset.remotePath || '').match(/(\.[a-z0-9]+)$/i)
|
||||
const extension = extensionMatch ? extensionMatch[1].toLowerCase() : '.bin'
|
||||
const safeId = assetId.replace(/[^a-z0-9._-]/gi, '_')
|
||||
return `${safeId}.${asset.sha256.slice(0, 12)}${extension}`
|
||||
}
|
||||
|
||||
function createAssetManager(options = {}) {
|
||||
const assetPlatform = options.assetPlatform || createAssetPlatformFacade()
|
||||
const manifest = options.manifest || {}
|
||||
const cdnBaseUrl = trimSlash(options.cdnBaseUrl)
|
||||
const imageBudgetBytes = Number.isFinite(options.imageCacheBudgetBytes)
|
||||
? Math.max(0, options.imageCacheBudgetBytes)
|
||||
: DEFAULT_IMAGE_BUDGET
|
||||
const audioBudgetBytes = Number.isFinite(options.audioCacheBudgetBytes)
|
||||
? Math.max(0, options.audioCacheBudgetBytes)
|
||||
: DEFAULT_AUDIO_BUDGET
|
||||
const audioMaxEntries = Number.isFinite(options.audioCacheMaxEntries)
|
||||
? Math.max(0, Math.floor(options.audioCacheMaxEntries))
|
||||
: DEFAULT_AUDIO_MAX_ENTRIES
|
||||
const downloadConcurrency = Number.isFinite(options.downloadConcurrency)
|
||||
? Math.max(1, Math.floor(options.downloadConcurrency))
|
||||
: 2
|
||||
const now = typeof options.now === 'function' ? options.now : Date.now
|
||||
const fs = assetPlatform
|
||||
&& typeof assetPlatform.getFileSystemManager === 'function'
|
||||
? assetPlatform.getFileSystemManager()
|
||||
: null
|
||||
const userDataPath = (
|
||||
assetPlatform
|
||||
&& assetPlatform.env
|
||||
&& assetPlatform.env.USER_DATA_PATH
|
||||
) || ''
|
||||
const cacheRoot = userDataPath ? joinPath(userDataPath, CACHE_FOLDER) : ''
|
||||
const indexPath = cacheRoot ? joinPath(cacheRoot, CACHE_INDEX_FILE) : ''
|
||||
|
||||
let initialized = false
|
||||
let index = normalizeIndex(null)
|
||||
let activeDownloads = 0
|
||||
const inflight = new Map()
|
||||
const downloadWaiters = []
|
||||
|
||||
function fallback(assetId, reason) {
|
||||
return {
|
||||
assetId,
|
||||
available: false,
|
||||
uri: '',
|
||||
source: 'text-fallback',
|
||||
reason,
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureCacheFolder() {
|
||||
if (!cacheRoot) return false
|
||||
try {
|
||||
await callFs(fs, 'mkdir', {
|
||||
dirPath: cacheRoot,
|
||||
recursive: true,
|
||||
})
|
||||
} catch (error) {
|
||||
// EEXIST and older base-library mkdir failures are both safe to ignore.
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
async function persistIndex() {
|
||||
if (!indexPath) return
|
||||
try {
|
||||
await ensureCacheFolder()
|
||||
await callFs(fs, 'writeFile', {
|
||||
filePath: indexPath,
|
||||
data: JSON.stringify(index),
|
||||
encoding: 'utf8',
|
||||
})
|
||||
} catch (error) {
|
||||
// Cache metadata must never block the text-first game.
|
||||
}
|
||||
}
|
||||
|
||||
async function fileExists(filePath) {
|
||||
if (!filePath) return false
|
||||
try {
|
||||
await callFs(fs, 'access', { path: filePath })
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function fileMatchesSha256(filePath, expectedSha256) {
|
||||
try {
|
||||
const result = await callFs(fs, 'readFile', { filePath })
|
||||
return constantTimeEqualHex(
|
||||
sha256Hex(result.data),
|
||||
expectedSha256,
|
||||
)
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function init() {
|
||||
if (initialized) return
|
||||
initialized = true
|
||||
if (!indexPath) return
|
||||
try {
|
||||
const result = await callFs(fs, 'readFile', {
|
||||
filePath: indexPath,
|
||||
encoding: 'utf8',
|
||||
})
|
||||
index = normalizeIndex(JSON.parse(result.data))
|
||||
} catch (error) {
|
||||
index = normalizeIndex(null)
|
||||
}
|
||||
|
||||
let changed = false
|
||||
for (const [assetId, entry] of Object.entries(index.entries)) {
|
||||
if (!manifest[assetId] || !(await fileExists(entry.filePath))) {
|
||||
delete index.entries[assetId]
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if (changed) await persistIndex()
|
||||
await enforceImageBudget()
|
||||
await enforceAudioBudget()
|
||||
}
|
||||
|
||||
function cacheBytes(kind) {
|
||||
return Object.values(index.entries).reduce(
|
||||
(total, entry) => (
|
||||
entry.kind === kind
|
||||
? total + Math.max(0, Number(entry.size) || 0)
|
||||
: total
|
||||
),
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
function cacheEntries(kind) {
|
||||
return Object.values(index.entries)
|
||||
.filter((entry) => entry.kind === kind)
|
||||
.length
|
||||
}
|
||||
|
||||
function imageCacheBytes() {
|
||||
return cacheBytes('image')
|
||||
}
|
||||
|
||||
function audioCacheBytes() {
|
||||
return cacheBytes('audio')
|
||||
}
|
||||
|
||||
async function unlinkQuietly(filePath) {
|
||||
try {
|
||||
await callFs(fs, 'unlink', { filePath })
|
||||
} catch (error) {
|
||||
// A missing cache file is already evicted.
|
||||
}
|
||||
}
|
||||
|
||||
async function enforceImageBudget(protectedAssetId = '') {
|
||||
let total = imageCacheBytes()
|
||||
if (total <= imageBudgetBytes) return
|
||||
|
||||
const candidates = Object.entries(index.entries)
|
||||
.filter(([assetId, entry]) => (
|
||||
entry.kind === 'image' && assetId !== protectedAssetId
|
||||
))
|
||||
.sort((left, right) => (
|
||||
(Number(left[1].lastAccessedAt) || 0)
|
||||
- (Number(right[1].lastAccessedAt) || 0)
|
||||
))
|
||||
|
||||
for (const [assetId, entry] of candidates) {
|
||||
if (total <= imageBudgetBytes) break
|
||||
await unlinkQuietly(entry.filePath)
|
||||
total -= Math.max(0, Number(entry.size) || 0)
|
||||
delete index.entries[assetId]
|
||||
}
|
||||
await persistIndex()
|
||||
}
|
||||
|
||||
async function enforceAudioBudget(protectedAssetId = '') {
|
||||
let total = audioCacheBytes()
|
||||
let entries = cacheEntries('audio')
|
||||
if (total <= audioBudgetBytes && entries <= audioMaxEntries) return
|
||||
|
||||
const candidates = Object.entries(index.entries)
|
||||
.filter(([assetId, entry]) => (
|
||||
entry.kind === 'audio' && assetId !== protectedAssetId
|
||||
))
|
||||
.sort((left, right) => (
|
||||
(Number(left[1].lastAccessedAt) || 0)
|
||||
- (Number(right[1].lastAccessedAt) || 0)
|
||||
))
|
||||
|
||||
for (const [assetId, entry] of candidates) {
|
||||
if (total <= audioBudgetBytes && entries <= audioMaxEntries) break
|
||||
await unlinkQuietly(entry.filePath)
|
||||
total -= Math.max(0, Number(entry.size) || 0)
|
||||
entries -= 1
|
||||
delete index.entries[assetId]
|
||||
}
|
||||
await persistIndex()
|
||||
}
|
||||
|
||||
async function resolveCached(assetId, asset) {
|
||||
const entry = index.entries[assetId]
|
||||
if (!entry || entry.sha256 !== asset.sha256) return null
|
||||
if (!(await fileExists(entry.filePath))) {
|
||||
delete index.entries[assetId]
|
||||
await persistIndex()
|
||||
return null
|
||||
}
|
||||
if (!(await fileMatchesSha256(entry.filePath, asset.sha256))) {
|
||||
await unlinkQuietly(entry.filePath)
|
||||
delete index.entries[assetId]
|
||||
await persistIndex()
|
||||
return fallback(assetId, 'remote-integrity-failed')
|
||||
}
|
||||
entry.lastAccessedAt = now()
|
||||
await persistIndex()
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: entry.filePath,
|
||||
source: 'cache',
|
||||
kind: asset.kind,
|
||||
}
|
||||
}
|
||||
|
||||
async function getTempFileSize(tempFilePath, response) {
|
||||
if (Number.isFinite(response.fileSize)) return response.fileSize
|
||||
try {
|
||||
const statResult = await callFs(fs, 'stat', {
|
||||
path: tempFilePath,
|
||||
})
|
||||
const stat = statResult && statResult.stats
|
||||
return Math.max(0, Number(stat && stat.size) || 0)
|
||||
} catch (error) {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchRemote(assetId, asset) {
|
||||
if (!cdnBaseUrl || !asset.remotePath) {
|
||||
return fallback(assetId, 'remote-disabled')
|
||||
}
|
||||
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
const run = () => {
|
||||
activeDownloads += 1
|
||||
download(assetPlatform, joinPath(cdnBaseUrl, asset.remotePath))
|
||||
.then(resolve, reject)
|
||||
.finally(() => {
|
||||
activeDownloads -= 1
|
||||
const next = downloadWaiters.shift()
|
||||
if (next) next()
|
||||
})
|
||||
}
|
||||
if (activeDownloads < downloadConcurrency) run()
|
||||
else downloadWaiters.push(run)
|
||||
})
|
||||
if (!(await fileMatchesSha256(response.tempFilePath, asset.sha256))) {
|
||||
await unlinkQuietly(response.tempFilePath)
|
||||
return fallback(assetId, 'remote-integrity-failed')
|
||||
}
|
||||
const size = await getTempFileSize(response.tempFilePath, response)
|
||||
|
||||
const exceedsCachePolicy = (
|
||||
(asset.kind === 'image' && size > imageBudgetBytes)
|
||||
|| (
|
||||
asset.kind === 'audio'
|
||||
&& (size > audioBudgetBytes || audioMaxEntries < 1)
|
||||
)
|
||||
)
|
||||
if (!cacheRoot || exceedsCachePolicy) {
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: response.tempFilePath,
|
||||
source: 'remote',
|
||||
kind: asset.kind,
|
||||
persistent: false,
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await ensureCacheFolder()
|
||||
const filePath = joinPath(cacheRoot, safeCacheName(assetId, asset))
|
||||
await callFs(fs, 'saveFile', {
|
||||
tempFilePath: response.tempFilePath,
|
||||
filePath,
|
||||
})
|
||||
index.entries[assetId] = {
|
||||
assetId,
|
||||
filePath,
|
||||
kind: asset.kind,
|
||||
sha256: asset.sha256,
|
||||
size,
|
||||
lastAccessedAt: now(),
|
||||
}
|
||||
await enforceImageBudget(assetId)
|
||||
await enforceAudioBudget(assetId)
|
||||
await persistIndex()
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: filePath,
|
||||
source: 'remote',
|
||||
kind: asset.kind,
|
||||
persistent: true,
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: response.tempFilePath,
|
||||
source: 'remote',
|
||||
kind: asset.kind,
|
||||
persistent: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function resolve(assetId, resolveOptions = {}) {
|
||||
await init()
|
||||
const asset = manifest[assetId]
|
||||
if (!asset) return fallback(assetId, 'unknown-asset')
|
||||
if (
|
||||
asset.kind === 'audio'
|
||||
&& String(asset.reviewStatus || '').toLowerCase() !== 'approved'
|
||||
) {
|
||||
return fallback(assetId, 'audio-unapproved')
|
||||
}
|
||||
|
||||
// 包内种子永远优先:首屏无需等网络,离线也能继续读和玩。
|
||||
if (asset.localSeed && resolveOptions.preferRemote !== true) {
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: asset.localSeed,
|
||||
source: 'local',
|
||||
kind: asset.kind,
|
||||
}
|
||||
}
|
||||
|
||||
const cached = await resolveCached(assetId, asset)
|
||||
if (cached) return cached
|
||||
|
||||
if (resolveOptions.allowRemote === false) {
|
||||
if (asset.localSeed) {
|
||||
return {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: asset.localSeed,
|
||||
source: 'local',
|
||||
kind: asset.kind,
|
||||
}
|
||||
}
|
||||
return fallback(assetId, 'remote-disallowed')
|
||||
}
|
||||
|
||||
if (inflight.has(assetId)) return inflight.get(assetId)
|
||||
|
||||
const request = fetchRemote(assetId, asset)
|
||||
.catch(() => (
|
||||
asset.localSeed
|
||||
? {
|
||||
assetId,
|
||||
available: true,
|
||||
uri: asset.localSeed,
|
||||
source: 'local',
|
||||
kind: asset.kind,
|
||||
}
|
||||
: fallback(assetId, 'remote-failed')
|
||||
))
|
||||
.finally(() => inflight.delete(assetId))
|
||||
inflight.set(assetId, request)
|
||||
return request
|
||||
}
|
||||
|
||||
async function prefetch(assetIds) {
|
||||
const ids = Array.isArray(assetIds) ? assetIds : []
|
||||
return Promise.all(ids.map((assetId) => resolve(assetId)))
|
||||
}
|
||||
|
||||
async function clearCache() {
|
||||
await init()
|
||||
const entries = Object.values(index.entries)
|
||||
for (const entry of entries) await unlinkQuietly(entry.filePath)
|
||||
index = normalizeIndex(null)
|
||||
await persistIndex()
|
||||
}
|
||||
|
||||
function getCacheStats() {
|
||||
return {
|
||||
entries: Object.keys(index.entries).length,
|
||||
imageBytes: imageCacheBytes(),
|
||||
imageBudgetBytes,
|
||||
audioEntries: cacheEntries('audio'),
|
||||
audioBytes: audioCacheBytes(),
|
||||
audioBudgetBytes,
|
||||
audioMaxEntries,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
init,
|
||||
resolve,
|
||||
prefetch,
|
||||
clearCache,
|
||||
getCacheStats,
|
||||
getAsset(assetId) {
|
||||
return manifest[assetId] || null
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CACHE_INDEX_VERSION,
|
||||
DEFAULT_AUDIO_BUDGET,
|
||||
DEFAULT_AUDIO_MAX_ENTRIES,
|
||||
constantTimeEqualHex,
|
||||
createAssetPlatformFacade,
|
||||
createAssetManager,
|
||||
sha256Hex,
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
const { createPlatformBridge } = require('./platformCore')
|
||||
const config = require('./platformConfig')
|
||||
module.exports = createPlatformBridge(wx, config)
|
||||
@@ -0,0 +1,218 @@
|
||||
const { sha256Hex } = require('./identityHash')
|
||||
const { CONTENT_VERSION, emptyProgress, projectProgress } = require('./progressContract')
|
||||
const copy = value => JSON.parse(JSON.stringify(value))
|
||||
const KEY = 'tang-xuetang-v1:'
|
||||
|
||||
// Dependency-injected for offline tests. Tokens remain in the existing host key
|
||||
// and request header only; local slots use a SHA-256 fingerprint, not the token.
|
||||
function createPlatformBridge(platform, config) {
|
||||
let active = null
|
||||
const listeners = new Set()
|
||||
function token() { try { return String(platform.getStorageSync('token') || '') } catch (_) { return '' } }
|
||||
function read(key, fallback) { try { return platform.getStorageSync(key) || fallback } catch (_) { return fallback } }
|
||||
function write(key, value) { try { platform.setStorageSync(key, copy(value)); return true } catch (_) { return false } }
|
||||
function context() {
|
||||
const current = token()
|
||||
if (active && active.token === current) return active
|
||||
if (active && active.timer) clearTimeout(active.timer)
|
||||
const scope = KEY + (current ? sha256Hex(current) : 'guest')
|
||||
const mapped = current ? read(scope + ':user', null) : null
|
||||
const key = Number.isSafeInteger(mapped) && mapped > 0 ? KEY + 'user:' + mapped : scope
|
||||
const cached = read(key, {})
|
||||
active = { token: current, scope, key, verified: false, opening: null, flushing: null, timer: null,
|
||||
status: current ? 'offline' : 'guest', remote: null,
|
||||
state: { progress: emptyProgress(), revision: 0, story_generation: 0, user_id: null,
|
||||
dirty: false, resetPending: false, serial: 0, pending: null, ...cached } }
|
||||
return active
|
||||
}
|
||||
const current = c => active === c && token() === c.token
|
||||
function notify(c, status) {
|
||||
if (!current(c)) return
|
||||
c.status = status
|
||||
listeners.forEach(listener => { try { listener(status) } catch (_) {} })
|
||||
}
|
||||
function persist(c) {
|
||||
const ok = write(c.key, c.state)
|
||||
if (!ok) notify(c, 'storage-error')
|
||||
return ok
|
||||
}
|
||||
async function request(c, path, method = 'GET', data) {
|
||||
if (!current(c) || !c.token) throw new Error('SESSION_CHANGED')
|
||||
return new Promise((resolve, reject) => platform.request({
|
||||
url: String(config.apiBaseUrl).replace(/\/+$/, '') + '/api/tang/' + path,
|
||||
method, data, timeout: 8000,
|
||||
header: { token: c.token, 'content-type': 'application/json' },
|
||||
success(result) {
|
||||
if (!current(c)) return reject(new Error('SESSION_CHANGED'))
|
||||
const body = result.data
|
||||
if (result.statusCode !== 200 || !body || typeof body !== 'object') return reject(new Error('API_UNAVAILABLE'))
|
||||
if (body.code === -1) { c.verified = false; notify(c, 'auth-expired'); return reject(new Error('AUTH_EXPIRED')) }
|
||||
if (body.code !== 1) {
|
||||
const error = new Error(body.data && body.data.error_code || 'API_UNAVAILABLE')
|
||||
return reject(error)
|
||||
}
|
||||
resolve(body.data)
|
||||
}, fail() { reject(new Error('NETWORK_UNAVAILABLE')) },
|
||||
}))
|
||||
}
|
||||
function validateRemote(value) {
|
||||
if (!value || value.schema_version !== 1 || value.content_version !== CONTENT_VERSION
|
||||
|| !Number.isSafeInteger(value.user_id) || value.user_id <= 0
|
||||
|| !Number.isSafeInteger(value.revision) || value.revision < 0
|
||||
|| !Number.isSafeInteger(value.story_generation) || value.story_generation < 0
|
||||
|| !value.progress || typeof value.progress !== 'object') throw new Error('CONTENT_MISMATCH')
|
||||
return { ...value, progress: projectProgress(value.progress) }
|
||||
}
|
||||
function hydrate(c, remote) {
|
||||
c.state.progress = { ...c.state.progress, ...remote.progress }
|
||||
c.state.revision = remote.revision
|
||||
c.state.story_generation = remote.story_generation
|
||||
c.state.user_id = remote.user_id
|
||||
c.state.pending = null
|
||||
c.state.resetPending = false
|
||||
c.state.dirty = false
|
||||
const saved = persist(c)
|
||||
if (saved) notify(c, 'synced')
|
||||
return saved
|
||||
}
|
||||
function failure(c, error) {
|
||||
if (!current(c) || error.message === 'SESSION_CHANGED') return
|
||||
if (['AUTH_EXPIRED', 'AUTH_REQUIRED'].includes(error.message)) c.verified = false
|
||||
const permanent = ['INVALID_REQUEST', 'PAYLOAD_TOO_LARGE', 'IDEMPOTENCY_CONFLICT', 'UNSUPPORTED_MEDIA_TYPE', 'METHOD_NOT_ALLOWED']
|
||||
if (c.status !== 'storage-error') notify(c, ['AUTH_EXPIRED', 'AUTH_REQUIRED'].includes(error.message) ? 'auth-expired'
|
||||
: ['CONTENT_MISMATCH', 'UNSUPPORTED_CONTENT_VERSION'].includes(error.message) ? 'version-error'
|
||||
: permanent.includes(error.message) ? 'sync-error' : 'offline')
|
||||
}
|
||||
async function open(force = false) {
|
||||
const c = context()
|
||||
if (!c.token) return c.status
|
||||
if (c.opening) return c.opening
|
||||
if (c.verified && !force) return c.status
|
||||
c.opening = (async () => {
|
||||
c.verified = false
|
||||
notify(c, 'connecting')
|
||||
try {
|
||||
const [catalog, raw] = await Promise.all([request(c, 'catalog'), request(c, 'progress')])
|
||||
if (!current(c)) return 'session-changed'
|
||||
if (!catalog || catalog.schema_version !== 1 || catalog.content_version !== CONTENT_VERSION) throw new Error('CONTENT_MISMATCH')
|
||||
const remote = validateRemote(raw)
|
||||
if (c.state.user_id && c.state.user_id !== remote.user_id) throw new Error('CONTENT_MISMATCH')
|
||||
// Only the authenticated server identity may select a shared user slot.
|
||||
// A renewed token can recover the same user's unsynced local queue.
|
||||
const userKey = KEY + 'user:' + remote.user_id
|
||||
if (c.key !== userKey) {
|
||||
const candidate = read(userKey, null)
|
||||
const saved = candidate && typeof candidate === 'object' && !Array.isArray(candidate) ? candidate : null
|
||||
if (saved && !c.state.dirty) c.state = saved
|
||||
else if (saved && c.state.dirty && !write(c.scope + ':previous-user-backup', saved)) throw new Error('STORAGE_UNAVAILABLE')
|
||||
c.key = userKey
|
||||
if (!write(c.scope + ':user', remote.user_id)) { notify(c, 'storage-error'); return c.status }
|
||||
}
|
||||
c.verified = true
|
||||
c.state.user_id = remote.user_id
|
||||
if (!c.state.dirty) hydrate(c, remote)
|
||||
else if (c.state.pending) await flushContext(c) // retry the exact idempotent request first
|
||||
else if (c.state.revision !== remote.revision || c.state.story_generation !== remote.story_generation) {
|
||||
c.remote = remote; notify(c, 'conflict')
|
||||
} else await flushContext(c)
|
||||
} catch (error) { failure(c, error) }
|
||||
finally { c.opening = null }
|
||||
return c.status
|
||||
})()
|
||||
return c.opening
|
||||
}
|
||||
function schedule(c) {
|
||||
if (c.timer) clearTimeout(c.timer)
|
||||
c.timer = setTimeout(() => { c.timer = null; flushContext(c) }, 600)
|
||||
}
|
||||
async function flushContext(c) {
|
||||
if (!current(c) || !c.verified || !c.state.dirty || ['conflict', 'storage-error', 'sync-error', 'version-error'].includes(c.status)) return
|
||||
if (c.flushing) return c.flushing
|
||||
// Defer preparation so the promise is assigned before any early exit.
|
||||
// The outer finally also covers a failed durable queue write.
|
||||
c.flushing = Promise.resolve().then(async () => {
|
||||
try {
|
||||
if (!current(c)) return
|
||||
if (!c.state.pending) {
|
||||
const operation = c.state.resetPending ? 'reset_story' : 'replace'
|
||||
const progress = projectProgress(c.state.progress)
|
||||
c.state.pending = { serial: operation === 'reset_story' ? c.state.resetAt : c.state.serial, body: {
|
||||
schema_version: 1, content_version: CONTENT_VERSION,
|
||||
base_revision: c.state.revision, story_generation: c.state.story_generation,
|
||||
request_id: 'tang-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 14),
|
||||
operation, progress: operation === 'reset_story'
|
||||
? { ...emptyProgress(), collectedMemoryCards: progress.collectedMemoryCards } : progress,
|
||||
} }
|
||||
}
|
||||
if (!persist(c)) return
|
||||
const pending = copy(c.state.pending)
|
||||
notify(c, 'syncing')
|
||||
const remote = validateRemote(await request(c, 'saveProgress', 'POST', pending.body))
|
||||
if (!current(c)) return
|
||||
if (remote.user_id !== c.state.user_id) throw new Error('CONTENT_MISMATCH')
|
||||
c.state.revision = remote.revision
|
||||
c.state.story_generation = remote.story_generation
|
||||
c.state.pending = null
|
||||
// A reset created while another request was in flight must not be lost.
|
||||
if (pending.body.operation === 'reset_story' && (c.state.resetAt || 0) <= pending.serial) c.state.resetPending = false
|
||||
if (pending.serial === c.state.serial) hydrate(c, remote)
|
||||
else if (persist(c)) { notify(c, 'pending'); schedule(c) }
|
||||
} catch (error) {
|
||||
if (error.message === 'PROGRESS_CONFLICT' && current(c)) {
|
||||
try { c.remote = validateRemote(await request(c, 'progress')); notify(c, 'conflict') }
|
||||
catch (readError) { failure(c, readError) }
|
||||
} else failure(c, error)
|
||||
}
|
||||
}).finally(() => { c.flushing = null })
|
||||
return c.flushing
|
||||
}
|
||||
function saveProgress(value, reset = false) {
|
||||
const c = context()
|
||||
// A delayed callback from an old page/account must not enter the new slot.
|
||||
if (!value || value.__tangLocalScope !== c.scope) return false
|
||||
c.state.progress = copy(value)
|
||||
delete c.state.progress.__tangLocalScope
|
||||
c.state.dirty = true
|
||||
c.state.serial += 1
|
||||
if (reset) { c.state.resetPending = true; c.state.resetAt = c.state.serial }
|
||||
if (!persist(c)) return false
|
||||
if (!['conflict', 'sync-error', 'version-error', 'auth-expired'].includes(c.status)) notify(c, c.token ? 'pending' : 'guest')
|
||||
if (c.verified) schedule(c)
|
||||
return true
|
||||
}
|
||||
function getConflictContext() {
|
||||
const c = context()
|
||||
return c.status === 'conflict' && c.remote ? JSON.stringify([
|
||||
c.scope, c.remote.revision, c.remote.story_generation, c.state.serial,
|
||||
]) : ''
|
||||
}
|
||||
async function resolveConflict(choice, expectedContext) {
|
||||
const c = context()
|
||||
if (!expectedContext || expectedContext !== getConflictContext() || !c.verified) return false
|
||||
// Recoverable, account-scoped backup before either explicit resolution.
|
||||
if (!write(c.key + ':conflict-backup', { local: c.state, remote: c.remote })) { notify(c, 'storage-error'); return false }
|
||||
if (choice === 'cloud') { const saved = hydrate(c, c.remote); if (saved) c.remote = null; return saved }
|
||||
if (choice !== 'local') return false
|
||||
c.state.revision = c.remote.revision
|
||||
c.state.story_generation = c.remote.story_generation
|
||||
c.state.pending = null
|
||||
c.remote = null
|
||||
if (!persist(c)) return false
|
||||
notify(c, 'pending')
|
||||
await flushContext(c)
|
||||
return c.status === 'synced'
|
||||
}
|
||||
return {
|
||||
open, saveProgress, resolveConflict, getConflictContext,
|
||||
getProgress: () => { const c = context(); return { ...copy(c.state.progress || {}), ...projectProgress(c.state.progress), __tangLocalScope: c.scope } },
|
||||
getScope: () => context().scope,
|
||||
getStatus: () => context().status,
|
||||
flush: () => { const c = context(); if (c.timer) { clearTimeout(c.timer); c.timer = null } return flushContext(c) },
|
||||
subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener) },
|
||||
// Local preferences/audio are account-scoped and are never passed to request().
|
||||
readLocal: (suffix, fallback) => copy(read(context().key + ':' + suffix, fallback)),
|
||||
writeLocal: (suffix, value) => write(context().key + ':' + suffix, value),
|
||||
dispose() { if (active && active.timer) clearTimeout(active.timer); listeners.clear(); active = null },
|
||||
}
|
||||
}
|
||||
module.exports = { createPlatformBridge }
|
||||
@@ -0,0 +1,41 @@
|
||||
// Wire projection only: never send snapshots, answers, settings or audio state.
|
||||
const CONTENT_VERSION = 'season-01'
|
||||
const pad = value => String(value).padStart(2, '0')
|
||||
const record = value => value && typeof value === 'object' && !Array.isArray(value) ? value : {}
|
||||
const list = value => Array.isArray(value) ? value : []
|
||||
function emptyProgress() {
|
||||
return { completedHotspots: {}, completedChapters: [], lastChapter: 1,
|
||||
collectedMemoryCards: [], comicReaderByChapter: {}, lastPageId: '' }
|
||||
}
|
||||
function projectProgress(input) {
|
||||
const value = record(input)
|
||||
const result = emptyProgress()
|
||||
result.lastChapter = Number.isInteger(value.lastChapter) && value.lastChapter >= 1 && value.lastChapter <= 15 ? value.lastChapter : 1
|
||||
for (let n = 1; n <= 15; n++) {
|
||||
const id = `S01-C${pad(n)}`
|
||||
const saved = record(record(value.comicReaderByChapter)[id])
|
||||
const supplied = new Set(list(saved.completedEventIds || record(value.completedHotspots)[id]))
|
||||
const events = []
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const event = `S01-H${pad((n - 1) * 4 + i)}`
|
||||
if (!supplied.has(event)) break
|
||||
events.push(event)
|
||||
}
|
||||
const finished = events.length === 4 && (typeof saved.chapterFinished === 'boolean'
|
||||
? saved.chapterFinished : list(value.completedChapters).includes(id))
|
||||
if (Object.keys(saved).length || events.length || Object.prototype.hasOwnProperty.call(record(value.completedHotspots), id)) {
|
||||
const requested = String(saved.currentPageId || (n === result.lastChapter ? value.lastPageId : '') || '')
|
||||
const match = requested.match(new RegExp(`^${id}-P(0[1-8])$`))
|
||||
const page = Math.max(1, Math.min(finished ? 8 : 3 + events.length, match ? Number(match[1]) : 1))
|
||||
const currentPageId = `${id}-P${pad(page)}`
|
||||
result.completedHotspots[id] = events
|
||||
result.comicReaderByChapter[id] = { currentPageId, completedEventIds: events.slice(), chapterFinished: finished }
|
||||
if (finished) result.completedChapters.push(id)
|
||||
if (n === result.lastChapter) result.lastPageId = currentPageId
|
||||
}
|
||||
const card = `${id}-MC01`
|
||||
if (list(value.collectedMemoryCards).includes(card)) result.collectedMemoryCards.push(card)
|
||||
}
|
||||
return result
|
||||
}
|
||||
module.exports = { CONTENT_VERSION, emptyProgress, projectProgress }
|
||||
@@ -0,0 +1,22 @@
|
||||
const bridge = require('./platformBridge')
|
||||
const { emptyProgress } = require('./progressContract')
|
||||
function normalizeSettings(value = {}) {
|
||||
return { ...value, fontScale: value.fontScale === 'xlarge' ? 'xlarge' : 'large', sound: value.sound !== false }
|
||||
}
|
||||
function resetStoryProgress(expectedScope) {
|
||||
if (!expectedScope || bridge.getScope() !== expectedScope) return false
|
||||
const current = bridge.getProgress()
|
||||
const next = { ...current, ...emptyProgress(), collectedMemoryCards: current.collectedMemoryCards || [] }
|
||||
if (!bridge.saveProgress(next, true)) return false
|
||||
bridge.writeLocal('audio', {})
|
||||
return true
|
||||
}
|
||||
module.exports = {
|
||||
getProgress: bridge.getProgress,
|
||||
saveProgress: value => bridge.saveProgress(value),
|
||||
resetStoryProgress,
|
||||
getSettings: () => normalizeSettings(bridge.readLocal('settings', {})),
|
||||
saveSettings: value => bridge.writeLocal('settings', normalizeSettings(value)),
|
||||
getAudioProgress: () => bridge.readLocal('audio', {}),
|
||||
saveAudioProgress: value => bridge.writeLocal('audio', value),
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Keep the native Page lifecycle, but hydrate the correct account before any
|
||||
// story page reads/writes storage. This also covers cold starts from shares.
|
||||
const bridge = require('./platformBridge')
|
||||
const LIFECYCLE_METHODS = new Set(['onLoad', 'onShow', 'onReady', 'onHide', 'onUnload'])
|
||||
const CLEANUP_METHOD = /^(?:destroy|clear|unbind|pause|invalidate|beginPauseLock)/
|
||||
module.exports = function registerTangPage(options) {
|
||||
function isCurrent(page) {
|
||||
return !page.__tangDead && !page.__tangOriginalUnloaded
|
||||
&& page.__tangVisible && page.__tangScope === bridge.getScope()
|
||||
}
|
||||
function returnHome(page) {
|
||||
if (page.__tangRedirected || page.__tangDead) return
|
||||
page.__tangRedirected = true
|
||||
wx.reLaunch({ url: '/tang-detective/pages/home/home' })
|
||||
}
|
||||
function cleanUp(page, name) {
|
||||
if (!page.__tangLoaded || typeof options[name] !== 'function') return
|
||||
if (name !== 'onUnload' && page.__tangOriginalUnloaded) return
|
||||
if (name === 'onUnload') {
|
||||
if (page.__tangOriginalUnloaded) return
|
||||
page.__tangOriginalUnloaded = true
|
||||
}
|
||||
page.__tangCleaning = true
|
||||
try {
|
||||
options[name].call(page)
|
||||
} finally {
|
||||
// This exception is synchronous only; pending timers and audio callbacks
|
||||
// regain the normal visible/account/dead guards as soon as cleanup ends.
|
||||
page.__tangCleaning = false
|
||||
}
|
||||
}
|
||||
function deliverReady(page) {
|
||||
if (!isCurrent(page) || !page.__tangLoaded || !page.__tangHasShown
|
||||
|| !page.__tangReadyRequested || page.__tangReadyDelivered) return
|
||||
page.__tangReadyDelivered = true
|
||||
if (typeof options.onReady === 'function') options.onReady.call(page)
|
||||
}
|
||||
const guarded = { ...options }
|
||||
Object.keys(options).forEach(key => {
|
||||
// Event handlers such as onAudioTimeUpdate are ordinary guarded methods,
|
||||
// even though their names begin with "on".
|
||||
if (typeof options[key] !== 'function' || LIFECYCLE_METHODS.has(key)) return
|
||||
guarded[key] = function (...args) {
|
||||
if (this.__tangCleaning) {
|
||||
// Release resources across an account change, but do not run helpers
|
||||
// such as saveCurrentAudioTime against the newly selected account.
|
||||
if (this.__tangScope === bridge.getScope() || CLEANUP_METHOD.test(key)) {
|
||||
return options[key].apply(this, args)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (this.__tangDead || this.__tangOriginalUnloaded || !this.__tangLoaded || !this.__tangVisible) return
|
||||
if (this.__tangScope && this.__tangScope !== bridge.getScope()) {
|
||||
returnHome(this)
|
||||
return
|
||||
}
|
||||
return options[key].apply(this, args)
|
||||
}
|
||||
})
|
||||
Page({
|
||||
...guarded,
|
||||
data: { ...(options.data || {}), tangBootPending: true },
|
||||
tangIgnoreBootTap() {},
|
||||
onLoad(query) {
|
||||
this.__tangQuery = query
|
||||
this.__tangScope = bridge.getScope()
|
||||
this.__tangDead = false
|
||||
this.__tangVisible = false
|
||||
this.__tangLoaded = false
|
||||
this.__tangCleaning = false
|
||||
this.__tangOriginalUnloaded = false
|
||||
this.__tangRedirected = false
|
||||
this.__tangShowGeneration = 0
|
||||
this.__tangReadyRequested = false
|
||||
this.__tangReadyDelivered = false
|
||||
this.__tangHasShown = false
|
||||
const nativeSetData = this.setData
|
||||
this.setData = function (...args) {
|
||||
if (this.__tangScope !== bridge.getScope()) return
|
||||
if (!this.__tangCleaning && !isCurrent(this)) return
|
||||
return nativeSetData.apply(this, args)
|
||||
}
|
||||
this.__tangBoot = bridge.open()
|
||||
},
|
||||
onShow() {
|
||||
if (this.__tangDead) return
|
||||
this.__tangVisible = true
|
||||
const generation = ++this.__tangShowGeneration
|
||||
const scope = bridge.getScope()
|
||||
if (this.__tangOriginalUnloaded || scope !== this.__tangScope) {
|
||||
returnHome(this)
|
||||
return
|
||||
}
|
||||
Promise.resolve(this.__tangBoot).then(() => {
|
||||
if (this.__tangDead || !this.__tangVisible || generation !== this.__tangShowGeneration) return
|
||||
if (bridge.getScope() !== scope) {
|
||||
returnHome(this)
|
||||
return
|
||||
}
|
||||
if (!this.__tangLoaded) {
|
||||
this.__tangLoaded = true
|
||||
if (options.onLoad) options.onLoad.call(this, this.__tangQuery)
|
||||
if (!isCurrent(this)) return
|
||||
this.setData({ tangBootPending: false })
|
||||
}
|
||||
if (options.onShow) options.onShow.call(this)
|
||||
this.__tangHasShown = true
|
||||
deliverReady(this)
|
||||
}).catch(() => {
|
||||
if (isCurrent(this) && generation === this.__tangShowGeneration) {
|
||||
wx.showToast({ title: '故事暂时未能打开,请返回重试', icon: 'none' })
|
||||
}
|
||||
})
|
||||
},
|
||||
onReady() {
|
||||
this.__tangReadyRequested = true
|
||||
deliverReady(this)
|
||||
},
|
||||
onHide() {
|
||||
this.__tangVisible = false
|
||||
++this.__tangShowGeneration
|
||||
try {
|
||||
cleanUp(this, 'onHide')
|
||||
} finally {
|
||||
// A hidden page from an obsolete account must not retain contexts with
|
||||
// callbacks that directly reference storage instead of guarded methods.
|
||||
try {
|
||||
if (this.__tangScope !== bridge.getScope()) cleanUp(this, 'onUnload')
|
||||
} finally {
|
||||
bridge.flush()
|
||||
}
|
||||
}
|
||||
},
|
||||
onUnload() {
|
||||
this.__tangDead = true
|
||||
this.__tangVisible = false
|
||||
++this.__tangShowGeneration
|
||||
try {
|
||||
cleanUp(this, 'onUnload')
|
||||
} finally {
|
||||
bridge.flush()
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user