Skip to content

MihailRis/mio-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MIO-Files

File subsystem originally created for Zendes2.5.

IODevice - base device interface.

Implementations:

  • ResDevice (internal files) - uses java resources (readonly)
  • DirDevice (directory) - creates IODevice with selected directory as paths root
  • AbsDevice (external files) - used only for work with external files ('abs:' is not allowed by IOPath.get, use Disk.absolute instead)
  • MemoryDevice (in-memory files) - used as virtual device with data stored in memory

Example of initialization:

// creates ResDevice with label 'res' at root of java resources
Disk.createResDevice("res", "/");
// creates DirDevice from given directory
Disk.createDirDevice("user", new File(gameDir));

Reading

Reading string:

String text = IOPath.get("res:texts/names.txt").readString();
// or
String text = IOPath.get("res:texts/names.txt").readString(charset);

Reading bytes array:

byte[] bytes = IOPath.get("res:colomaps/lights.cm").readBytes();

Reading properties:

Properties properties = new Properties();
IOPath.get("res:engine_settings.properties").read(properties);

Writing

Write string:

// 'user' device is added with Disk.createDirDevice("user", new File(...));
IOPath.get("user:error_log.txt").write(getErrorLog());

Write bytes array:

IOPath.get("user:save.bin").write(bytes);

Work with external files:

// initialization
Disk.createAbsDevice();
// create absolute IOPath
IOPath file = Disk.absolute("/home/user/segfault.wav");
// -> abs:home/user/segfault.wav

Add custom IODevice:

Disk.addDevice("name", new ...);

Close and remove IODevice from Disk (does not affect real files):

Disk.closeDevice("user");

Add IODevice temporary to use with Disk:

try (Disk.TempLabelHandle handle = Disk.addDeviceTemporary(device)) {
    IOPath.get(handle.label+":test.txt").write("Some text");    
}

Archives

ZIP:

Create memory device based on ZIP-file content:

MemoryDevice device = ZIPFile.createDevice(IOPath.get("user:archive.zip"));

Unpack ZIP-file:

ZIPFile.unpack(IOPath.get("user:archive.zip"), IOPath.get("user:destination/path"));

TAR:

Create memory device based on TAR-file content:

MemoryDevice device = TARFile.createDevice(new File("some_archive.tar"));

Unpack TAR-file into an existing MemoryDevice:

MemoryDevice device = ...;
TARFile.readInto(new File("some_archive.tar"), device);
// it only store offsets to files content, not actual content

Unpack TAR-file:

TARFile.unpack(new File("some_archive.tar"), IOPath.get("user:destination/path"));

Creating millions devices is only affecting memory and performance