Documentation
¶
Overview ¶
Package unityweb is a library for parsing, unpacking, and repacking Unity Web data files.
Unpacking a Unity Web data file into a directory ¶
pkg, err := unityweb.FromPackageFile("/path/to/test.data") if err != nil { panic(err) } err = pkg.Dump("/path/to/output/directory") if err != nil { panic(err) }
Packing a directory into a Unity Web data file ¶
pkg, err := unityweb.PackDirectory("/path/to/input/directory") if err != nil { panic(err) } err = pkg.PackToFile("/path/to/output.data") if err != nil { panic(err) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct { // Contents is a byte slice that holds the contents of the file. Contents []byte }
File represents the contents of a single file in a Unity Web data file.
type FileMetadata ¶
type FileMetadata struct { // Offset represents at which byte offset the file's contents begin. Until // recalculated, this offset will always be zero. Offset uint32 // 4 bytes // Size represents the size of the file's contents in bytes. Size uint32 // 4 bytes // FilenameLength represents the length of the file's name in bytes. FilenameLength uint32 // 4 bytes // Filename is a byte slice containing the file's name. It is exactly the // same length as FilenameLength. Filename []byte // x bytes }
FileMetadata contains information about a single file in a Unity Web data file.
func NewFileMetadata ¶
func NewFileMetadata(filename string, contents []byte) *FileMetadata
NewFileMetadata takes in a filename as an argument and its byte buffer, and initializes a FileMetadata object with the given information. The offset in the created object will be zero until recalculated in the Package object.
func (*FileMetadata) BlockSize ¶
func (m *FileMetadata) BlockSize() uint32
BlockSize calculates the size of the FileMetadata block. The size consists of the size of each field in the struct (4 bytes each), plus the length of the filename.
func (*FileMetadata) FromPackageFile ¶
func (m *FileMetadata) FromPackageFile(file *os.File) error
FromPackageFile will populate the FileMetadata object with data read from the given package file.
func (*FileMetadata) ToBytes ¶
func (m *FileMetadata) ToBytes() []byte
ToBytes will return a byte slice containing the FileMetadata object's data. These bytes can then be written to a package file.
type Package ¶
type Package struct { // Magic is the magic header of the package file. It must always be 16 bytes // long and contain the null-terminated string "UnityWebData1.0". Magic [16]byte // UnityWebData1.0, 16 bytes with NUL // StartOffset represents the byte offset at which the very first file's // contents begins. This will also always be the value of the offset field // in the first metadata object. It is represented as a 4-byte unsigned // integer in little-endian format. StartOffset uint32 // 4 bytes // FileMetadata is a slice of FileMetadata objects, each representing meta // information about a single file in the package. FileMetadata []FileMetadata // x * (4 + 4 + 4 + y) bytes // Files is a slice of File objects, each representing a single file in the // package. The number of files in this slice will always be the same as the // number of FileMetadata objects in the FileMetadata slice. Files []File // rest of the bytes }
Package is an object representation of a single Unity Web package file.
func FromPackageFile ¶
FromPackageFile takes in a file path as an argument and returns a Package object representing the data inside the Unity Web data package file.
func NewPackage ¶
func NewPackage() *Package
NewPackage initializes a new Package object with the correct magic header. The offset fields, as well as the slices in the object will be zero/empty.
func PackDirectory ¶
PackDirectory takes in a directory path as an argument and constructs a new Package object with all the necessary metadata and file contents. This method can fail if there are file I/O errors (such as permission errors). After calling this method, you may call the PackToFile method to create a new Unity Web data package file.
func (*Package) AddFile ¶
AddFile appends the metadata and contents of given byte slice with a given file name to the Package object.
func (*Package) Dump ¶
Dump will write all files in the package to the given directory. The method will recursively create directories if they don't exist. This method can fail if there are file I/O errors (such as permission errors).
func (*Package) Pack ¶
Pack will create a byte slice representing the package file. This method will make sure to recalculate the offsets before performing the packing.
func (*Package) PackToFile ¶
PackToFile will pack the package object into a given output file. The method will recursively create directories if they don't exist. This method can fail if there are file I/O errors (such as permission errors).
func (*Package) ReadFromPackageFile ¶
ReadFromPackageFile takes in an os.File object representing a Unity Web data package file, and parses it into a Package object. The file must be opened with read permissions. This method can fail if the file doesn't start with the correct magic header or if there are file I/O errors (such as premature EOF).
func (*Package) RecalculateOffsets ¶
func (p *Package) RecalculateOffsets()
RecalculateOffsets recalculates the offset fields in the FileMetadata slice and the StartOffset field in the Package object. This method should be called only after all files have been added.
type ParseError ¶
type ParseError int
ParseError is an error that occurs while parsing a Unity Web data file.
const ( // ErrInvalidMagicHeader is returned when the magic header is invalid. A // header is valid if it's a sequence of 16-bytes, consisting of the // null-terminated string "UnityWebData1.0" ErrInvalidMagicHeader ParseError = iota )
func (ParseError) Error ¶
func (e ParseError) Error() string
Error returns a string representation of the error.