-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,16 @@ const std = @import("std"); | |
const mem = @import("std").mem; | ||
const Buffer = @import("std").Buffer; | ||
|
||
const allocator = &@import("std").heap.c_allocator; | ||
|
||
const mod = @import("mod.zig"); | ||
const Parser = @import("parser.zig").Parser; | ||
const mod2model = @import("mod2model.zig"); | ||
const gltf = @import("gltf.zig"); | ||
|
||
error CLIBadArguments; | ||
error CLINoInputFile; | ||
error CouldntOpenInputFile; | ||
error CouldntReadInputFile; | ||
error CouldntOpenOutputFile; | ||
error CouldntWriteOutputFile; | ||
|
||
pub fn main() -> %void { | ||
|
@@ -29,18 +29,17 @@ fn main2() -> %void { | |
var maybe_out_path: ?Buffer = null; | ||
defer if (maybe_out_path) |*b| { b.deinit(); }; | ||
var verbose = false; | ||
var help = false; | ||
var dry_run = false; | ||
|
||
var args = std.os.args(); | ||
_ = args.skip(); // skip exe name | ||
while (args.next(&mem.c_allocator)) |arg_or_err| { | ||
while (args.next(allocator)) |arg_or_err| { | ||
const arg = %return arg_or_err; | ||
defer mem.c_allocator.free(arg); | ||
defer allocator.free(arg); | ||
|
||
if (mem.eql(u8, "-h", arg) or mem.eql(u8, "--help", arg)) { | ||
help = true; | ||
break; | ||
help(); | ||
return; | ||
} | ||
if (mem.eql(u8, "-v", arg) or mem.eql(u8, "--verbose", arg)) { | ||
verbose = true; | ||
|
@@ -55,21 +54,15 @@ fn main2() -> %void { | |
} | ||
|
||
if (maybe_in_path == null) { | ||
maybe_in_path = %%Buffer.init(&mem.c_allocator, arg); | ||
maybe_in_path = %%Buffer.init(allocator, arg); | ||
} else if (maybe_out_path == null) { | ||
maybe_out_path = %%Buffer.init(&mem.c_allocator, arg); | ||
maybe_out_path = %%Buffer.init(allocator, arg); | ||
} else { | ||
// Too many arguments | ||
return error.CLIBadArguments; | ||
} | ||
} | ||
|
||
if (help) { | ||
%%std.io.stderr.printf("zimodre: convert Monster Hunter Stories MOD files to GLB\n"); | ||
usage(); | ||
return; | ||
} | ||
|
||
var in_path = maybe_in_path ?? return error.CLIBadArguments; | ||
maybe_in_path = null; | ||
defer in_path.deinit(); | ||
|
@@ -101,71 +94,73 @@ fn main2() -> %void { | |
|
||
/// Read a whole file into a Buffer. | ||
fn read_file(path: []const u8) -> %Buffer { | ||
var buffer = Buffer.initNull(&mem.c_allocator); | ||
var buffer = Buffer.initNull(allocator); | ||
%defer buffer.deinit(); | ||
|
||
var in = std.io.InStream.open(path, &mem.c_allocator) | ||
%% return error.CouldntOpenInputFile; | ||
defer in.close(); | ||
in.readAll(&buffer) %% return error.CouldntReadInputFile; | ||
var file = std.io.File.openRead(path, allocator) | ||
%% return error.CouldntReadInputFile; | ||
defer file.close(); | ||
var in = &file.in_stream; | ||
in.readAllBuffer(&buffer, @maxValue(usize)) | ||
%% return error.CouldntReadInputFile; | ||
|
||
buffer | ||
} | ||
|
||
fn write_file(path: []const u8, contents: []const u8) -> %void { | ||
var out = std.io.OutStream.open(path, &mem.c_allocator) | ||
%% return error.CouldntOpenOutputFile; | ||
defer out.close(); | ||
out.write(contents) %% return error.CouldntWriteOutputFile; | ||
out.flush() %% return error.CouldntWriteOutputFile; | ||
std.io.writeFile(path, contents, allocator) | ||
%% return error.CouldntWriteOutputFile; | ||
} | ||
|
||
fn print_error(err: error) { | ||
var o = std.io.stderr; | ||
var stderr = %%std.io.getStdErr(); | ||
var o = &stderr.out_stream; | ||
switch (err) { | ||
error.CLIBadArguments => { | ||
%%o.printf("cli: bad command-line arguments\n"); | ||
%%o.print("cli: bad command-line arguments\n"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
scurest
Author
Owner
|
||
usage(); | ||
}, | ||
error.CLINoInputFile => { | ||
%%o.printf("cli: need an input file\n"); | ||
%%o.print("cli: need an input file\n"); | ||
usage(); | ||
}, | ||
error.ParseErrorUnexpectedEOF => { | ||
%%o.printf( | ||
%%o.print( | ||
"parser: unexpected EOF\n" ++ | ||
"parser: file couldn't be understood as MOD\n"); | ||
}, | ||
error.CouldntOpenInputFile => { | ||
%%o.printf("input file: couldn't open for reading\n"); | ||
}, | ||
error.CouldntReadInputFile => { | ||
%%o.printf("input file: couldn't read file\n"); | ||
}, | ||
error.CouldntOpenOutputFile => { | ||
%%o.printf("output file: couldn't open for writing\n"); | ||
%%o.print("input file: couldn't read file\n"); | ||
}, | ||
error.CouldntWriteOutputFile => { | ||
%%o.printf("output file: couldn't write file\n"); | ||
%%o.print("output file: couldn't write file\n"); | ||
}, | ||
error.ValidateNotEnoughVertexData => { | ||
%%o.printf("validation: file asked for too much vertex data\n"); | ||
%%o.print("validation: file asked for too much vertex data\n"); | ||
}, | ||
error.ValidateNotEnoughIndexData => { | ||
%%o.printf("validation: file asked for too much index data\n"); | ||
%%o.print("validation: file asked for too much index data\n"); | ||
}, | ||
error.UnsupportedVertexSize => { | ||
%%o.printf("unimplemented: unsupported vertex format in MOD file\n"); | ||
%%o.print("unimplemented: unsupported vertex format in MOD file\n"); | ||
}, | ||
else => { | ||
%%o.printf("error: nonspecific error -- everyone's favorite kind :)\n"); | ||
%%o.print("error: nonspecific error -- everyone's favorite kind :)\n"); | ||
} | ||
} | ||
} | ||
|
||
fn help() { | ||
var stderr = %%std.io.getStdErr(); | ||
var o = &stderr.out_stream; | ||
%%o.print("zimodre: convert Monster Hunter Stories MOD files to GLB\n"); | ||
usage(); | ||
} | ||
|
||
fn usage() { | ||
var o = std.io.stderr; | ||
%%o.printf( | ||
var stderr = %%std.io.getStdErr(); | ||
var o = &stderr.out_stream; | ||
%%o.print( | ||
\\Usage: zimodre [options...] <input> <output> | ||
\\Options: | ||
\\ --dry-run Don't write the output file | ||
|
I think the
std.debug.warn
API is what you want here.