aboutsummaryrefslogtreecommitdiff
path: root/content-system/loader/loader.ts
blob: 97a6c0f4a0c625c171f9269023aac51890c496a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { ContentManifest, ChunkPayload } from '@hollowdark/content-system/manifest/schema'
import { HollowdarkContentCache } from '@hollowdark/persistence/db'
import type { JsonValue } from '@hollowdark/utils/types/json'

let cacheInstance: HollowdarkContentCache | null = null

function cache(): HollowdarkContentCache {
  if (cacheInstance === null) {
    cacheInstance = new HollowdarkContentCache()
  }
  return cacheInstance
}

const MANIFEST_CACHE_KEY = 'latest'

/**
 * Fetch the content manifest from static hosting. Falls back to the last
 * cached manifest when the network request fails — the game works fully
 * offline once the first download has succeeded.
 */
export async function fetchManifest(baseUrl: string): Promise<ContentManifest> {
  const url = `${baseUrl}/content-manifest.json`
  try {
    const response = await fetch(url, { cache: 'no-cache' })
    if (!response.ok) throw new Error(`manifest ${response.status}`)
    const live = (await response.json()) as ContentManifest
    await cache().manifest.put({
      id: MANIFEST_CACHE_KEY,
      contentVersion: live.content_version,
      appVersion: live.app_version,
      generatedAt: live.generated_at,
      payload: live as unknown as JsonValue
    })
    return live
  } catch {
    const cached = await cache().manifest.get(MANIFEST_CACHE_KEY)
    if (cached === undefined) {
      throw new Error('Content manifest unavailable and no cached copy exists.')
    }
    return cached.payload as unknown as ContentManifest
  }
}

/**
 * Load a single chunk by id, using the manifest to resolve the hashed
 * URL. Served from the IndexedDB cache whenever the cached hash matches
 * the manifest's; refetched otherwise. Falls back to the cached payload
 * on network failure so gameplay continues offline.
 */
export async function loadChunk(
  chunkId: string,
  manifest: ContentManifest,
  baseUrl: string
): Promise<ChunkPayload> {
  const entry = manifest.chunks[chunkId]
  if (entry === undefined) {
    throw new Error(`Content chunk not listed in manifest: ${chunkId}`)
  }

  const cached = await cache().content.get(chunkId)
  if (cached !== undefined && cached.hash === entry.hash) {
    return { chunkId, data: cached.data, version: cached.version, hash: cached.hash }
  }

  try {
    const response = await fetch(`${baseUrl}${entry.url}`)
    if (!response.ok) throw new Error(`chunk ${response.status}`)
    const data = (await response.json()) as JsonValue
    await cache().content.put({
      chunkId,
      version: entry.version,
      hash: entry.hash,
      data,
      fetchedAt: new Date().toISOString()
    })
    return { chunkId, data, version: entry.version, hash: entry.hash }
  } catch (err) {
    if (cached !== undefined) {
      return { chunkId, data: cached.data, version: cached.version, hash: cached.hash }
    }
    throw err
  }
}