File IO Library for Battlezone 98 Redux
Quick and dirty tutorial:
-- Make sure to use require fix or manually set your package.cpath in order for the game
-- to load dlls
local bzfile = require("bzfile")
local filePath = bzfile.GetWorkingDirectory() .. "\\addon\\myfile.txt"
local file = bzfile.Open(filePath, "w", "app")
file:Writeln("Hello world!")
-- Optionally if you want the text to appear right away...
-- file:Flush()
-- Once you are done you can optionally close the file manually:
file:Close()
local readFile = bzfile.Open(filePath, "r")
-- Read line by line
local contents = file:Readln()
while contents ~= nil do
print(contents)
contents = file:Readln()
end
-- Or dump the whole contents of the file into a lua stirng:
local bigString = file:Dump()Specification:
bzfile.Open(filePath: string, openMode: string?, writeParameter: string?) -> handleOpens a handle to a file, creating a new one if write mode is specified and the file does not exist.
Valid options for open mode are:
- "r": read mode
- "w": write mode
Valid options for write parameters are:
- "app": append to file
- "trunc": truncate file (clears the file before writing)
Default options if only path is specified is "r" (read). In "w" (write) mode the default option is "app" (append).
file:Write(content: string) -> self (reference to the handle to allow method chaining)Unformatted write with no newline.
file:Writeln(content: string) -> selfWrite with newline.
file:Read(count: int?) -> content: stringUnformatted read of count characters, defaults to 1 if not specified.
file:Readln() -> content: stringReads a line.
file:Dump() -> content: stringDumps the entire contents of the file into one string
file:Flush() -> selfFlushes the input/output buffer, makes text appear immediately in the file, may affect performance if called frequently.
file:Close() -> nilCloses the handle to the file, file object becomes nil.
bzfile.GetWorkingDirectory() -> path: stringGets the root directory of the game (..\common\Battlezone98Redux).
bzfile.GetWorkshopDirectory() -> path: stringGets the workshop directory (..\content\301650).
bzfile.MakeDirectory(path: string) -> nilMakes a new directory at the given path.