unityweb

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 4 Imported by: 0

README

unityweb

Command line utility and library for parsing, unpacking, and repacking Unity Web data package files.

Command Line Usage

Installation

You can download a binary from the releases page or install the package using Go:

go get github.com/jozsefsallai/unityweb
Unpacking a Unity Web data package to a directory
unityweb unpack -i /path/to/unityweb.data -o /path/to/output/directory
Packaging a directory into a Unity Web data package
unityweb pack -i /path/to/input/directory -o /path/to/unityweb.data

Library Usage

package main

import (
	"github.com/jozsefsallai/unityweb"
)

func main() {
	// you can now access functions from `unityweb`
}
Unpacking a Unity Web data package to a directory
pkg, err := unityweb.FromPackageFile("/path/to/unityweb.data")

if err != nil {
	panic(err)
}

err = pkg.Dump("/path/to/output/directory")

if err != nil {
	panic(err)
}
Packaging a directory into a Unity Web data package
pkg, err := unityweb.PackDirectory("/path/to/input/directory")

if err != nil {
	panic(err)
}

err = pkg.PackToFile("/path/to/unityweb.data")

if err != nil {
	panic(err)
}

Unity Web package file structure reference

.data files used by Unity Web projects are just files stitched together with no compression whatsoever and some added metadata for identification. Here's an example that demonstrates the structure of these files:

Unity Web data package structure

License

MIT.

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.

func NewFile

func NewFile(contents []byte) *File

NewFile creates a new File object with the given contents.

func (*File) Dump

func (fo *File) Dump(path string) error

Dump will write the contents of the file to the given path. If a deeply nested path is provided, the function will recursively create all directories in the path (if they don't already exist), then write the file with the given file name.

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

func FromPackageFile(filename string) (*Package, error)

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

func PackDirectory(directoryPath string) (*Package, error)

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

func (p *Package) AddFile(filename string, contents []byte)

AddFile appends the metadata and contents of given byte slice with a given file name to the Package object.

func (*Package) Dump

func (p *Package) Dump(outputDirectoryPath string) error

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

func (p *Package) Pack() []byte

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

func (p *Package) PackToFile(filepath string) error

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

func (p *Package) ReadFromPackageFile(file *os.File) error

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.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL