hxresourcegroup Module

Source code: hxresourcegroup.py

The hxresourcegroup module provides an HxResourceGroupFile class which derives from ArchiveFile and is used to read and write Devil Daggers archive data.

HxResourceGroupFile Class

class vgio.devildaggers.hxresourcegroup.HxResourceGroupFile

Class with methods to open, read, close, and list resource group files.

Example

Print out file name and type of all entries in a resource group:

from vgio.devildaggers.hxresourcegroup import HxResourceGroupFile
with HxResourceGroupFile('dd') as resource_group:
    for info in resource_group.infolist():
        print(f'{info.filename}: {info.type}')
file

Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by HxResourceGroupFile.

mode

The file mode for the file-like object.

HxResourceGroupFile.__init__(file, mode='r')

Open an HxResourceGroup file, where file can be a path to a file (a string), or a file-like object.

The mode parameter should be ‘r’ to read an existing file, ‘w’ to truncate and write a new file, or ‘a’ to append to an existing file.

Parameters
  • file – A path or a file-like object.

  • mode – File mode for the file-like object.

HxResourceGroupFile.open(name, mode='r')

Access a member of the archive as a binary file-like object. name can be either the name of a file within the archive or an ArchiveInfo object. The mode parameter, if included, must be ‘r’ (the default) or ‘w’.

open() is also a context manager and supports the with statement:

with ArchiveFile('archive.file') as archive_file:
    with archive_file.open('entry') as entry_file:
        print(entry_file.read())
Parameters
  • name – Name or ArchiveInfo object.

  • mode – File mode to open object.

Returns

A binary file-like object.

Raises
  • ValueError – If mode isn’t ‘r’ or ‘w’.

  • RuntimeError – If file was already closed.

HxResourceGroupFile.close()

Close the archive file. You must call close() before exiting your program or essential records will not be written.

Raises

ValueError – If open writing handles exist.

HxResourceGroupFile.read(name)

Return the bytes of the file name in the archive. name is the name of the file in the archive, or a ArchiveInfo object. The archive must be open for read or append.

Parameters

name – ArchiveInfo name.

Returns

File as bytes.

HxResourceGroupFile.write(filename, arcname=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). The archive must be open with mode ‘w’, or ‘a’.

Parameters
  • filename

  • arcname – Optional. Name of the info object. If omitted filename will be used.

HxResourceGroupFile.writestr(info_or_arcname, data)

Write a file into the archive. The contents is data, which may be either a string or a bytes instance; if it is a string, it is encoded as UTF-8 first. info_or_arcname is either the file name it will be given in the archive, or a ArchiveInfo instance. If it’s an instance, at least the filename must be given. The archive must be opened with mode ‘w’ or ‘a’.

Parameters
  • info_or_arcname

  • data – Data to be written. Either a string or bytes.

HxResourceGroupFile.extract(member, path=None)

Extract a member from the archive to the current working directory; member must be its full name or a ArchiveInfo object. Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or an ArchiveInfo object.

Parameters
  • member – Either the name of the member to extract or a ArchiveInfo instance.

  • path – The directory to extract to. The current working directory will be used if None.

Returns

Path to extracted file.

HxResourceGroupFile.extractall(path=None, members=None)

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist().

Parameters
  • path – The directory to extract to. The current working directory will be used if None.

  • members – The names of the members to extract. This must be a subset of the list returned by namelist(). All members will be extracted if None.

HxResourceGroupFile.getinfo(name)

Return a ArchiveInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.

Parameters

name – AchiveInfo name.

Returns

An ArchiveInfo object.

Raises

KeyError – If no archive item exists for the given name.

HxResourceGroupFile.infolist()

Return a list containing an ArchiveInfo object for each member of the archive. The objects are in the same order as their entries in the actual archive file on disk if an existing archive was opened.

Returns

A sequence of ArchiveInfo objects.

HxResourceGroupFile.namelist()

Return a list of archive members by name.

Returns

A sequence of filenames.

ResourceGroupInfo Class

class vgio.devildaggers.hxresourcegroup.ResourceGroupInfo

Instances of the ResourceGroupInfo class are returned by the getinfo() and infolist() methods of HxResourceGroupFile objects. Each object stores information about a single member of the HxResourceGroupFile archive.

type

Type of the file.

filename

Name of the file in the archive.

file_offset
file_size

Size of file in bytes.

date_time

Last modified date as Unix timestamp.

ResourceGroupInfo.__init__(type_, filename, file_offset=0, file_size=0, date_time=0)

Constructs a ResourceGroupInfo object.

classmethod ResourceGroupInfo.from_file(filename)

Construct an ResourceGroupInfo instance for a file on the filesystem, in preparation for adding it to an archive file. filename should be the path to a file or directory on the filesystem.

Parameters

filename – A path to a file or directory.

Returns

A ResourceGroupInfo object.