|
| 1 | +// Copyright (c) 2025 Niema Moshiri and The Zaparoo Project. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +// |
| 4 | +// This file is part of go-gameid. |
| 5 | +// |
| 6 | +// go-gameid is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU General Public License as published by |
| 8 | +// the Free Software Foundation, either version 3 of the License, or |
| 9 | +// (at your option) any later version. |
| 10 | +// |
| 11 | +// go-gameid is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with go-gameid. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +// Package archive provides support for reading game files from archives. |
| 20 | +// It supports ZIP, 7z, and RAR formats. |
| 21 | +package archive |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "path/filepath" |
| 27 | + "strings" |
| 28 | +) |
| 29 | + |
| 30 | +// FileInfo contains information about a file in an archive. |
| 31 | +type FileInfo struct { |
| 32 | + Name string // Full path within archive |
| 33 | + Size int64 // Uncompressed size |
| 34 | +} |
| 35 | + |
| 36 | +// Archive provides read access to files within an archive. |
| 37 | +type Archive interface { |
| 38 | + // List returns all files in the archive. |
| 39 | + List() ([]FileInfo, error) |
| 40 | + |
| 41 | + // Open opens a file within the archive for reading. |
| 42 | + // Returns the reader, uncompressed size, and any error. |
| 43 | + Open(internalPath string) (io.ReadCloser, int64, error) |
| 44 | + |
| 45 | + // OpenReaderAt opens a file and returns an io.ReaderAt interface. |
| 46 | + // The file contents are buffered in memory to support random access. |
| 47 | + // The returned Closer must be called to release resources. |
| 48 | + OpenReaderAt(internalPath string) (io.ReaderAt, int64, io.Closer, error) |
| 49 | + |
| 50 | + // Close closes the archive. |
| 51 | + Close() error |
| 52 | +} |
| 53 | + |
| 54 | +// Open opens an archive file based on its extension. |
| 55 | +// Supported formats: .zip, .7z, .rar |
| 56 | +func Open(path string) (Archive, error) { |
| 57 | + ext := strings.ToLower(filepath.Ext(path)) |
| 58 | + |
| 59 | + switch ext { |
| 60 | + case ".zip": |
| 61 | + return OpenZIP(path) |
| 62 | + case ".7z": |
| 63 | + return OpenSevenZip(path) |
| 64 | + case ".rar": |
| 65 | + return OpenRAR(path) |
| 66 | + default: |
| 67 | + return nil, FormatError{Format: ext} |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// IsArchiveExtension checks if an extension is a supported archive format. |
| 72 | +func IsArchiveExtension(ext string) bool { |
| 73 | + ext = strings.ToLower(ext) |
| 74 | + switch ext { |
| 75 | + case ".zip", ".7z", ".rar": |
| 76 | + return true |
| 77 | + default: |
| 78 | + return false |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// nopCloser wraps a value that doesn't need closing. |
| 83 | +type nopCloser struct{} |
| 84 | + |
| 85 | +func (nopCloser) Close() error { return nil } |
| 86 | + |
| 87 | +// bufferFile reads the entire file into memory and returns a ReaderAt. |
| 88 | +// |
| 89 | +//nolint:revive // 4 return values is necessary for this interface pattern |
| 90 | +func bufferFile(arc Archive, internalPath string) (io.ReaderAt, int64, io.Closer, error) { |
| 91 | + reader, size, err := arc.Open(internalPath) |
| 92 | + if err != nil { |
| 93 | + return nil, 0, nil, fmt.Errorf("open file in archive: %w", err) |
| 94 | + } |
| 95 | + defer func() { _ = reader.Close() }() |
| 96 | + |
| 97 | + data := make([]byte, size) |
| 98 | + bytesRead, err := io.ReadFull(reader, data) |
| 99 | + if err != nil { |
| 100 | + return nil, 0, nil, fmt.Errorf("read file from archive: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + return &byteReaderAt{data: data}, int64(bytesRead), nopCloser{}, nil |
| 104 | +} |
| 105 | + |
| 106 | +// byteReaderAt implements io.ReaderAt for a byte slice. |
| 107 | +type byteReaderAt struct { |
| 108 | + data []byte |
| 109 | +} |
| 110 | + |
| 111 | +func (br *byteReaderAt) ReadAt(buf []byte, off int64) (int, error) { |
| 112 | + if off < 0 { |
| 113 | + return 0, fmt.Errorf("negative offset: %d", off) |
| 114 | + } |
| 115 | + if off >= int64(len(br.data)) { |
| 116 | + return 0, io.EOF |
| 117 | + } |
| 118 | + |
| 119 | + bytesRead := copy(buf, br.data[off:]) |
| 120 | + if bytesRead < len(buf) { |
| 121 | + return bytesRead, io.EOF |
| 122 | + } |
| 123 | + return bytesRead, nil |
| 124 | +} |
0 commit comments