pub struct DATFile { /* private fields */ }
Expand description
A reference to an open DAT file on the system. This emulates the standard lib
std::fs::File
but provides additional DAT-specific functionality.
Reads and writes to DAT files are performed only on the data contents of the file. XOR masks are automatically applied as necessary.
§Examples
use libxivdat::dat_file::DATFile;
use libxivdat::dat_type::DATType;
use std::io::Read;
let mut dat_file = match DATFile::open("./resources/TEST_XOR.DAT") {
Ok(dat_file) => dat_file,
Err(_) => panic!("Something broke!")
};
match dat_file.file_type() {
DATType::Macro => {
let mut macro_bytes = vec![0u8; dat_file.content_size() as usize - 1];
match dat_file.read(&mut macro_bytes) {
Ok(count) => println!("Read {} bytes.", count),
Err(_) => panic!("Reading broke!")
}
},
_ => panic!("Not a macro file!")
};
Implementations§
Source§impl DATFile
impl DATFile
Sourcepub fn content_size(&self) -> u32
pub fn content_size(&self) -> u32
Returns the size of the current content contained in the DAT file. DAT files store content as a null-terminated CString, so this size is one byte larger than the actual content.
§Examples
use libxivdat::dat_file::DATFile;
let mut dat_file = DATFile::open("./resources/TEST.DAT").unwrap();
let content_size = dat_file.content_size();
Sourcepub fn create<P: AsRef<Path>>(
path: P,
dat_type: DATType,
) -> Result<Self, DATError>
pub fn create<P: AsRef<Path>>( path: P, dat_type: DATType, ) -> Result<Self, DATError>
Creates a new DAT file with an empty content block in read/write mode. This will truncate an existing file if one exists at the specified path.
By default, this will use the default max size for the specified type from
get_default_max_size_for_type()
and
default header ending from get_default_end_byte_for_type()
.
To cicumvent this behavior, you can use create_unsafe()
. Note
that DAT files with nonstandard sizes may produce undefined behavior in the game client.
§Errors
If an I/O error creating the file occurs, a DATError::FileIO
error will be returned wrapping the underlying FS error.
§Examples
use libxivdat::dat_file::DATFile;
use libxivdat::dat_type::DATType;
let mut dat_file = DATFile::create(&path, DATType::Macro);
Sourcepub fn create_unsafe<P: AsRef<Path>>(
path: P,
dat_type: DATType,
content_size: u32,
max_size: u32,
end_byte: u8,
) -> Result<Self, DATError>
pub fn create_unsafe<P: AsRef<Path>>( path: P, dat_type: DATType, content_size: u32, max_size: u32, end_byte: u8, ) -> Result<Self, DATError>
Creates a new DAT file with a null-padded content bock of the specifed size in read/write mode. This will truncate an existing file if one exists at the specified path.
This function allows a custom, not-necessarily-valid maximum length and end byte to be set. Note that DAT files with nonstandard sizes and headers may produce undefined behavior in the game client.
§Errors
If an I/O error creating the file occurs, a DATError::FileIO
error will be returned wrapping the underlying FS error.
§Examples
use libxivdat::dat_file::DATFile;
use libxivdat::dat_type::DATType;
// Create an empty (content length 1) macro file with a custom max size and end byte. This probably isn't valid.
let mut dat_file = DATFile::create_unsafe(&path, DATType::Macro, 1, 1024, 0x01);
Sourcepub fn create_with_content<P: AsRef<Path>>(
path: P,
dat_type: DATType,
content: &[u8],
) -> Result<Self, DATError>
pub fn create_with_content<P: AsRef<Path>>( path: P, dat_type: DATType, content: &[u8], ) -> Result<Self, DATError>
Creates a new DAT file with a specific content block in read/write mode. This will truncate an existing file if one exists at the specified path.
This is shorthand for creating the DAT file, then calling
write()
. This function also resets the cursor to
the beginning of the content block after writing.
By default, this will use the default max size for the specified type from
get_default_max_size_for_type()
and
default header ending from get_default_end_byte_for_type()
.
To cicumvent this behavior, you can use create_unsafe()
. Note
that DAT files with nonstandard sizes may produce undefined behavior in the game client.
§Errors
If an I/O error creating the file occurs, a DATError::FileIO
error will be returned wrapping the underlying FS error.
A DATError::Overflow
is returned
if the provided content size is too large, or if the content size exceeds the maximum size.
§Examples
use libxivdat::dat_file::DATFile;
use libxivdat::dat_type::DATType;
let mut dat_file = DATFile::create_with_content(&path, DATType::Macro, b"Not really a macro.");
Sourcepub fn file_type(&self) -> DATType
pub fn file_type(&self) -> DATType
Returns the file type of the DAT file.
§Examples
use libxivdat::dat_file::DATFile;
use libxivdat::dat_type::DATType;
let mut dat_file = DATFile::open("./resources/TEST_XOR.DAT").unwrap();
match dat_file.file_type() {
DATType::Macro => println!("Macro file!"),
_ => panic!("Nope!")
}
Sourcepub fn header_end_byte(&self) -> u8
pub fn header_end_byte(&self) -> u8
Returns the terminating byte of the DAT file’s header. The purpose of this byte is unknown, but it is almost always 0xFF.
§Examples
use libxivdat::dat_file::DATFile;
let mut dat_file = DATFile::open("./resources/TEST_XOR.DAT").unwrap();
let header_end_byte = dat_file.header_end_byte();
Sourcepub fn max_size(&self) -> u32
pub fn max_size(&self) -> u32
Returns the maximum size allowed for the content block
of the DAT file. Content is stored as a null-terminated CString,
so the actual maximum allowed content is 1 byte less than max_size
.
§Examples
use libxivdat::dat_file::DATFile;
let mut dat_file = DATFile::open("./resources/TEST_XOR.DAT").unwrap();
let header_end_byte = dat_file.max_size();
Sourcepub fn metadata(&self) -> Result<Metadata, DATError>
pub fn metadata(&self) -> Result<Metadata, DATError>
Calls metadata()
on the underlying std::fs::File
.
§Errors
This function will return any underling I/O errors as a
DATError::FileIO
.
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self, DATError>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, DATError>
Attempts to open a DAT file in read-only mode.
To set different file access with OpenOptions
,
use open_options()
§Errors
If an I/O error opening the file occurs, a DATError::FileIO
error will be returned wrapping the underlying FS error.
A DATError::BadHeader
will be returned if the header
cannot be validated, indicating a non-DAT or corrupt file.
§Examples
use libxivdat::dat_file::DATFile;
let mut dat_file = DATFile::open("./resources/TEST.DAT");
Sourcepub fn open_options<P: AsRef<Path>>(
path: P,
options: &mut OpenOptions,
) -> Result<Self, DATError>
pub fn open_options<P: AsRef<Path>>( path: P, options: &mut OpenOptions, ) -> Result<Self, DATError>
Attempts to open a DAT file using an OpenOptions
builder.
A reference to the OpenOptions
struct itself should be passed in, not the File
it opens.
Do not end the options chain with open("foo.txt")
as with opening a standard file.
§Errors
If an I/O error opening the file occurs, a DATError::FileIO
error will be returned wrapping the underlying FS error.
A DATError::BadHeader
will be returned if the header
cannot be validated, indicating a non-DAT or corrupt file.
§Examples
use libxivdat::dat_file::DATFile;
use std::fs::OpenOptions;
let mut open_opts = OpenOptions::new();
open_opts.read(true).write(true);
let mut dat_file = DATFile::open_options("./resources/TEST.DAT", &mut open_opts);
Sourcepub fn set_content_size(&mut self, new_size: u32) -> Result<(), DATError>
pub fn set_content_size(&mut self, new_size: u32) -> Result<(), DATError>
Truncates or extends the readable content section of the DAT file.
This emulates the behavior of std::fs::File::set_len()
, but only
operates on the content region of the DAT file. Because DAT files store
content as null-terminated CStrings, the actual writeable space will be
one byte less than specified.
§Errors
This function will return any underling I/O errors as a
DATError::FileIO
.
Additionally, it may return a DATError::Overflow
error if the new content size would exceed the maximum allowed size. This size may be adjusted using
set_max_size()
, but modifying it may not produce a valid file for the game client.
Sourcepub fn set_max_size(&mut self, new_size: u32) -> Result<(), DATError>
pub fn set_max_size(&mut self, new_size: u32) -> Result<(), DATError>
Truncates or extends the full DAT file.
This emulates the behavior of std::fs::File::set_len()
.
Because DAT files store content as null-terminated CStrings,
the actual useable space will be one byte less than specified.
Files with a non-default maximum size may cause undefined behavior in the game client.
§Errors
This function will return any underling I/O errors as a
DATError::FileIO
.
A DATError::Overflow
is returned
if the maximum size would be shorter than the content size after shrinking. To correct this,
first set_content_size()
.
Sourcepub fn sync_all(&self) -> Result<(), DATError>
pub fn sync_all(&self) -> Result<(), DATError>
Calls sync_all()
on the underlying std::fs::File
.
§Errors
This function will return any underling I/O errors as a
DATError::FileIO
.
Sourcepub fn sync_data(&self) -> Result<(), DATError>
pub fn sync_data(&self) -> Result<(), DATError>
Calls sync_data()
on the underlying std::fs::File
.
§Errors
This function will return any underling I/O errors as a
DATError::FileIO
.
Trait Implementations§
Source§impl Read for DATFile
impl Read for DATFile
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Seek for DATFile
impl Seek for DATFile
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Source§impl Write for DATFile
impl Write for DATFile
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)