server/browser_startup.go (2815B)
1 package main 2 3 import ( 4 "io" 5 "net/http" 6 "strconv" 7 "strings" 8 ) 9 10 // ResourceDatabase.dat chunks observed through playable startup in playfield 705. 11 // Each index identifies a 1 MiB range; keep these sorted for sequential disk reads. 12 var browserStartupChunks = [...]int64{ 13 0, 1, 2, 3, 4, 6, 7, 8, 10, 12, 14, 15, 17, 18, 19, 14 20, 21, 22, 24, 26, 31, 32, 33, 35, 36, 37, 38, 39, 40, 42, 15 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 65, 16 66, 71, 80, 82, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 17 97, 98, 102, 104, 105, 106, 107, 108, 110, 111, 112, 113, 114, 115, 116, 18 117, 118, 119, 121, 122, 123, 124, 129, 142, 145, 147, 148, 150, 151, 152, 19 153, 157, 158, 162, 165, 168, 171, 175, 176, 177, 178, 179, 180, 181, 182, 20 183, 185, 186, 187, 189, 190, 191, 197, 198, 199, 200, 201, 202, 203, 204, 21 205, 206, 207, 208, 209, 210, 211, 212, 216, 217, 220, 221, 224, 229, 230, 22 232, 239, 243, 247, 284, 309, 313, 320, 350, 351, 353, 354, 362, 368, 372, 23 373, 374, 375, 376, 377, 378, 379, 380, 382, 397, 399, 411, 435, 437, 463, 24 464, 465, 473, 474, 475, 476, 477, 478, 479, 483, 494, 495, 498, 536, 537, 25 541, 542, 548, 552, 554, 26 } 27 28 func browserStartup(w http.ResponseWriter, r *http.Request) { 29 const name = "anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.dat" 30 const chunkSize = 1048576 31 file, err := resources.Open(name) 32 if err != nil { 33 http.Error(w, err.Error(), http.StatusInternalServerError) 34 return 35 } 36 defer file.Close() 37 info, err := file.Stat() 38 if err != nil { 39 http.Error(w, err.Error(), http.StatusInternalServerError) 40 return 41 } 42 if info.Size() < (browserStartupChunks[len(browserStartupChunks)-1]+1)*chunkSize { 43 http.Error(w, "Resource database is too small for startup chunks", http.StatusInternalServerError) 44 return 45 } 46 47 // Describe the concatenated ranges so the page can populate the normal asset cache. 48 indices := make([]string, len(browserStartupChunks)) 49 for i, chunk := range browserStartupChunks { 50 indices[i] = strconv.FormatInt(chunk, 10) 51 } 52 w.Header().Set("X-AO-Chunks", strings.Join(indices, ",")) 53 w.Header().Set("X-AO-Asset-Version", browserAssetVersion(name, info)) 54 w.Header().Set("Content-Type", "application/octet-stream") 55 w.Header().Set("Content-Length", strconv.Itoa(len(browserStartupChunks)*chunkSize)) 56 w.Header().Set("Cache-Control", "no-store") 57 w.Header().Set("Cross-Origin-Resource-Policy", "same-origin") 58 if r.Method == "HEAD" { 59 return 60 } 61 62 // Copy directly from the asset file without assembling a 185 MiB server buffer. 63 for _, chunk := range browserStartupChunks { 64 if r.Context().Err() != nil { 65 return 66 } 67 if _, err := file.(io.ReadSeeker).Seek(chunk*chunkSize, io.SeekStart); err != nil { 68 panic(http.ErrAbortHandler) 69 } 70 if _, err := io.CopyN(w, file, chunkSize); err != nil { 71 panic(http.ErrAbortHandler) 72 } 73 } 74 }