aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/game_database.go (2969B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"io"
      7 	"io/fs"
      8 )
      9 
     10 type gameDatabase struct {
     11 	positions map[[2]uint32]uint32
     12 	size      int64
     13 	file      interface {
     14 		io.ReaderAt
     15 		io.Closer
     16 	}
     17 }
     18 
     19 func readGameDatabase(files fs.FS) (*gameDatabase, error) {
     20 	// The same packaged archive supplies the client and server geometry.
     21 	index, err := fs.ReadFile(files, "anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.idx")
     22 	if err != nil {
     23 		return nil, err
     24 	}
     25 	if len(index) < 76 {
     26 		return nil, fmt.Errorf("truncated game resource index")
     27 	}
     28 	database := &gameDatabase{positions: make(map[[2]uint32]uint32)}
     29 	seen := make(map[uint32]bool)
     30 	for block := binary.LittleEndian.Uint32(index[72:76]); block != 0; {
     31 		if seen[block] || uint64(block)+18 > uint64(len(index)) {
     32 			return nil, fmt.Errorf("invalid game resource index block %d", block)
     33 		}
     34 		seen[block] = true
     35 		count := int(binary.LittleEndian.Uint16(index[block+8 : block+10]))
     36 		if uint64(block)+18+uint64(count)*12 > uint64(len(index)) {
     37 			return nil, fmt.Errorf("truncated game resource index block %d", block)
     38 		}
     39 		for entry := range count {
     40 			data := index[int(block)+18+entry*12:]
     41 			identity := [2]uint32{binary.BigEndian.Uint32(data[4:8]), binary.BigEndian.Uint32(data[8:12])}
     42 			if _, exists := database.positions[identity]; exists {
     43 				return nil, fmt.Errorf("duplicate game resource %d/%d", identity[0], identity[1])
     44 			}
     45 			database.positions[identity] = binary.LittleEndian.Uint32(data[:4])
     46 		}
     47 		block = binary.LittleEndian.Uint32(index[block : block+4])
     48 	}
     49 
     50 	file, err := files.Open("anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.dat")
     51 	if err != nil {
     52 		return nil, err
     53 	}
     54 	reader, ok := file.(interface {
     55 		io.ReaderAt
     56 		io.Closer
     57 	})
     58 	if !ok {
     59 		file.Close()
     60 		return nil, fmt.Errorf("game resource database does not support random access")
     61 	}
     62 	info, err := file.Stat()
     63 	if err != nil {
     64 		file.Close()
     65 		return nil, err
     66 	}
     67 	database.size = info.Size()
     68 	database.file = reader
     69 	return database, nil
     70 }
     71 
     72 func (database *gameDatabase) read(kind, id uint32) ([]byte, uint32, error) {
     73 	position, exists := database.positions[[2]uint32{kind, id}]
     74 	if !exists {
     75 		return nil, 0, fmt.Errorf("game resource %d/%d: %w", kind, id, fs.ErrNotExist)
     76 	}
     77 	header := make([]byte, 34)
     78 	if _, err := database.file.ReadAt(header, int64(position)); err != nil {
     79 		return nil, 0, fmt.Errorf("game resource %d/%d header: %w", kind, id, err)
     80 	}
     81 	size := binary.LittleEndian.Uint32(header[18:22])
     82 	if binary.LittleEndian.Uint32(header[10:14]) != kind || binary.LittleEndian.Uint32(header[14:18]) != id || size < 12 || int64(position)+34+int64(size)-12 > database.size {
     83 		return nil, 0, fmt.Errorf("invalid game resource %d/%d header", kind, id)
     84 	}
     85 	data := make([]byte, size-12)
     86 	if _, err := database.file.ReadAt(data, int64(position)+34); err != nil {
     87 		return nil, 0, fmt.Errorf("game resource %d/%d payload: %w", kind, id, err)
     88 	}
     89 	return data, binary.LittleEndian.Uint32(header[30:34]), nil
     90 }