Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
Upgrade to latest Zig (f0dafd3f)
Browse files Browse the repository at this point in the history
  • Loading branch information
scurest committed Nov 6, 2017
1 parent 52dbb80 commit 8df208d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Based on [this 3DS Max script](https://pastebin.com/EafXb2ZU) by zaramot who rev

### Building

Install [zig](http://ziglang.org/) (I'm using commit hash 6663638) and run `zig build`
Install [zig](http://ziglang.org/) (I'm using commit hash f0dafd3f) and run `zig build`
in this directory.

### TODO
Expand Down
11 changes: 6 additions & 5 deletions src/gltf.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const Buffer = std.Buffer;
const Buffer = @import("std").Buffer;

const allocator = &@import("std").heap.c_allocator;

const byteorder = @import("byteorder.zig");
const Json = @import("json.zig").Json;
Expand All @@ -22,7 +23,7 @@ pub fn write_glb(model: &const Model) -> %Buffer {
model.buffer.len(); // BIN payload len


var glb = %%Buffer.initSize(&std.mem.c_allocator, 0);
var glb = %%Buffer.initSize(allocator, 0);
%defer glb.deinit();

// Header
Expand All @@ -44,7 +45,7 @@ pub fn write_glb(model: &const Model) -> %Buffer {
}

fn write_gltf(model: &const Model) -> %Buffer {
var b = %%Buffer.initSize(&std.mem.c_allocator, 0);
var b = %%Buffer.initSize(allocator, 0);
%defer b.deinit();

var j = Json.new(&b);
Expand Down Expand Up @@ -122,7 +123,7 @@ fn write_gltf(model: &const Model) -> %Buffer {
if (model.meshes.len != 0) {
j.prop("meshes");
j.begin_array();
{ var i:usize = 0; while (i != model.meshes.len) : (i += 1) {
{ var i: usize = 0; while (i != model.meshes.len) : (i += 1) {
const m = &model.meshes.items[i];
j.begin_obj();
j.prop("primitives");
Expand Down
5 changes: 2 additions & 3 deletions src/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
const std = @import("std");
const Buffer = std.Buffer;

fn buf_fmt_output(b: &Buffer, out: []const u8) -> bool {
b.append(out) %% return false;
true
fn buf_fmt_output(b: &Buffer, out: []const u8) -> %void {
b.append(out)
}

fn buf_fmt(b: &Buffer, comptime fmt: []const u8, args: ...) {
Expand Down
81 changes: 38 additions & 43 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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.

Copy link
@andrewrk

andrewrk Nov 6, 2017

I think the std.debug.warn API is what you want here.

const warn = @import("std").debug.warn;
warn("cli: bad command-line arguments\n");

This comment has been minimized.

Copy link
@scurest

scurest Nov 7, 2017

Author Owner

Thanks for looking :)

I didn't use it just because of the name. Since its not really a warning or a debug message it felt weird to use. I guess I could just use const whatever = @import("std").debug.warn; though, huh? I'll do that.

Btw, I was bitten around here by this

var o = stderr.out_stream; // note: no leading &

This compiles perfectly fine, but when you run it, writeFn uses @fieldParentPtr to try to access the File object the stream is in and since it isn't actually in one, Bad Things happen. It seems kind of like a footgun.

This comment has been minimized.

Copy link
@andrewrk

andrewrk Nov 7, 2017

Can you think of a better name than warn? The function's purpose is: print to stderr; ignore any error that occurs. I'd be happy to rename it if we can come up with a better name.

This is definitely a footgun. Thanks for pointing it out. I added ziglang/zig#591 to track it.

This comment has been minimized.

Copy link
@andrewrk

andrewrk Nov 7, 2017

If you find any more footguns, I would love to hear about them. Designing against footguns is important to zig.

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
Expand Down
4 changes: 3 additions & 1 deletion src/mod.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const std = @import("std");
const ArrayList = std.ArrayList;

const allocator = &std.heap.c_allocator;

const parser = @import("parser.zig");
const Parser = parser.Parser;

Expand Down Expand Up @@ -39,7 +41,7 @@ pub fn parse(ctx: &Parser) -> %Mod {
ctx.end();


var meshes = ArrayList(MeshInfo).init(&std.mem.c_allocator);
var meshes = ArrayList(MeshInfo).init(allocator);
%defer meshes.deinit();

ctx.pos = item_offset;
Expand Down
15 changes: 8 additions & 7 deletions src/mod2model.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mem = @import("std").mem;
const ArrayList = @import("std").ArrayList;
const Buffer = @import("std").Buffer;

const allocator = &@import("std").heap.c_allocator;

const mod = @import("mod.zig");
const Mod = mod.Mod;
const byteorder = @import("byteorder.zig");
Expand All @@ -21,9 +22,9 @@ pub const Model = struct {

pub fn init() -> Model {
Model {
.buffer = %%Buffer.initSize(&mem.c_allocator, 0),
.accessors = ArrayList.init(&mem.c_allocator),
.meshes = ArrayList.init(&mem.c_allocator),
.buffer = %%Buffer.initSize(allocator, 0),
.accessors = ArrayList.init(allocator),
.meshes = ArrayList.init(allocator),
}
}

Expand Down Expand Up @@ -60,9 +61,9 @@ pub const Mesh = struct {

pub fn convert(m: &const Mod) ->%Model {
var model = Model {
.buffer = %%Buffer.initSize(&mem.c_allocator, 0),
.accessors = ArrayList(Accessor).init(&mem.c_allocator),
.meshes = ArrayList(Mesh).init(&mem.c_allocator),
.buffer = %%Buffer.initSize(allocator, 0),
.accessors = ArrayList(Accessor).init(allocator),
.meshes = ArrayList(Mesh).init(allocator),
};
%defer model.deinit();

Expand Down
3 changes: 2 additions & 1 deletion src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub const Parser = struct {

pub fn log(self: &Parser, comptime format: []const u8, args: ...) {
if (!self.logging_on) { return; }
%%io.stdout.printf(format, args);
var stderr = %%io.getStdErr();

This comment has been minimized.

Copy link
@andrewrk

andrewrk Nov 6, 2017

you probably want std.debug.warn here.

%%stderr.out_stream.print(format, args);
}

pub fn warn(self: &Parser, comptime format: []const u8, args: ...) {
Expand Down

0 comments on commit 8df208d

Please sign in to comment.