Skip to content

Instantly share code, notes, and snippets.

@TimVosch
Created January 9, 2018 19:38
Show Gist options
  • Select an option

  • Save TimVosch/b492221898025536e7ea59a6fc13ee57 to your computer and use it in GitHub Desktop.

Select an option

Save TimVosch/b492221898025536e7ea59a6fc13ee57 to your computer and use it in GitHub Desktop.
Resource explorer and extractor
import hxd.Res;
import hxd.App;
import hxd.res.Sound;
import hxd.fs.FileEntry;
import hxd.fmt.pak.FileSystem in PakSystem;
import sys.io.File;
import sys.FileSystem;
class Main{
static public function main():Void {
trace("Starting");
var pak = new PakSystem();
pak.loadPak("./res.pak");
var root = pak.getRoot();
trace("ROOT_NAME > " + root.name);
trace("ROOT_DIR? > " + root.isDirectory);
if (root.isDirectory) {
skim(root);
}
var path = new List<FileEntry>();
path.push(root);
do {
// Get input
var p = Sys.stdin().readLine();
// Functions
switch (p.split(" ")[0]) {
case "exit":
break;
case "extract":
if (p.indexOf(" ") < 0) {
// Extract dir
extract(path.first());
} else {
// Extract specific file
var _temp = p.substr(p.indexOf(" ") + 1);
if (path.first().exists(_temp)) {
extract(path.first().get(_temp));
} else {
trace("Path (" + _temp + ") does not exist!");
}
}
continue;
case "..":
if (path.length >1 )
path.pop();
skim(path.first());
continue;
}
// File exists?
if (path.first().exists(p)) {
var nextFile = path.first().get(p);
// Directory?
if (nextFile.isDirectory) {
// change dir
path.push(nextFile);
skim(path.first());
} else {
// Open file
openFile(nextFile);
}
} else {
trace("File not found!");
}
} while (true);
}
static public function extract(file:FileEntry):Void {
if(file.isDirectory) {
for (f in file) {
extract(f);
}
} else {
if (file.directory != "" && !FileSystem.exists(file.directory)) {
FileSystem.createDirectory(file.directory);
}
File.saveBytes(file.path, file.getBytes());
}
}
static public function skim(dir:FileEntry) {
for (f in dir) {
var o = "";
if (f.isDirectory)
o += ">> ";
o += f.name;
trace(o);
}
trace("------------");
}
static public function openFile(file:FileEntry):Void {
switch(file.extension) {
case "wav", "ogg", "mp3":
var sound = new Sound(file);
trace("PLAYING: " + sound.name);
sound.play();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment