server/browser_resources.go (3441B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "io" 8 "net/http" 9 10 "sort" 11 "sync/atomic" 12 ) 13 14 var browserResourceCache atomic.Pointer[browserResourceOffsets] 15 16 type browserResourceOffsets struct { 17 version string 18 offsets []byte 19 err error 20 ready chan struct{} 21 } 22 23 func loadBrowserResourceOffsets(file io.Reader, version string) ([]byte, error) { 24 for { 25 cached := browserResourceCache.Load() 26 if cached != nil && cached.version == version { 27 <-cached.ready 28 return cached.offsets, cached.err 29 } 30 pending := &browserResourceOffsets{version: version, ready: make(chan struct{})} 31 if !browserResourceCache.CompareAndSwap(cached, pending) { 32 continue 33 } 34 // This request owns construction. Readers receive an immutable result 35 // when ready closes; a failed read leaves the next request free to retry. 36 pending.offsets, pending.err = readBrowserResourceOffsets(file) 37 if pending.err != nil { 38 browserResourceCache.CompareAndSwap(pending, nil) 39 } 40 close(pending.ready) 41 return pending.offsets, pending.err 42 } 43 } 44 45 func readBrowserResourceOffsets(file io.Reader) ([]byte, error) { 46 // Build the prefetch table once per asset version, outside the client process. 47 data, err := io.ReadAll(file) 48 if err != nil { 49 return nil, err 50 } 51 if len(data) < 76 { 52 return nil, fmt.Errorf("resource index header is truncated") 53 } 54 var records [][3]uint32 55 seen := make(map[uint32]bool) 56 for block := binary.LittleEndian.Uint32(data[72:]); block != 0; block = binary.LittleEndian.Uint32(data[block:]) { 57 if seen[block] || uint64(block)+10 > uint64(len(data)) { 58 return nil, fmt.Errorf("invalid resource index block %d", block) 59 } 60 seen[block] = true 61 count := binary.LittleEndian.Uint16(data[block+8:]) 62 if uint64(block)+18+uint64(count)*12 > uint64(len(data)) { 63 return nil, fmt.Errorf("truncated resource index block %d", block) 64 } 65 for entry := uint32(0); entry < uint32(count); entry++ { 66 offset := block + 18 + entry*12 67 records = append(records, [3]uint32{ 68 binary.BigEndian.Uint32(data[offset+4:]), 69 binary.BigEndian.Uint32(data[offset+8:]), 70 binary.LittleEndian.Uint32(data[offset:]), 71 }) 72 } 73 } 74 // Stable ordering preserves the last entry for duplicate identities. 75 sort.SliceStable(records, func(i, j int) bool { 76 return records[i][0] < records[j][0] || records[i][0] == records[j][0] && records[i][1] < records[j][1] 77 }) 78 offsets := make([]byte, len(records)*12) 79 for i, record := range records { 80 for j, value := range record { 81 binary.LittleEndian.PutUint32(offsets[i*12+j*4:], value) 82 } 83 } 84 return offsets, nil 85 } 86 87 func browserResourceIndex(w http.ResponseWriter, r *http.Request) { 88 file, err := resources.Open("anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.idx") 89 if err != nil { 90 http.Error(w, err.Error(), http.StatusInternalServerError) 91 return 92 } 93 defer file.Close() 94 info, err := file.Stat() 95 if err != nil { 96 http.Error(w, err.Error(), http.StatusInternalServerError) 97 return 98 } 99 version := browserAssetVersion("anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.idx", info) 100 offsets, err := loadBrowserResourceOffsets(file, version) 101 if err != nil { 102 http.Error(w, err.Error(), http.StatusInternalServerError) 103 return 104 } 105 w.Header().Set("Content-Type", "application/octet-stream") 106 w.Header().Set("Cache-Control", "no-cache") 107 w.Header().Set("ETag", version) 108 http.ServeContent(w, r, "resource-offsets.bin", info.ModTime(), bytes.NewReader(offsets)) 109 }