Files
2026-11-14 15:40:07 -06:00

254 lines
8.5 KiB
Plaintext

#! Contains reading ASCII FBX: lines of `Name: property, property`, a node's
#! children within `{` and `}` after its properties, comments from `;` to the
#! line's end. A property is a number, text in quotes, a bare word, as `T`, or
#! an array, `*count { a: number, number }`. Properties go on past a line's end
#! after a comma. Numbers written with a point or an exponent are read as
#! floats, others as integers, and an array of both as floats.
module malunal::fbx;
# Reads ASCII FBX's nodes.
struct ascii_reader {
var text: strview;
var at: u64;
var line: u64 = 1;
}
# Reads ASCII FBX.
def read_ascii(text: strview): result<document, fbx_error> {
var made := document{ binary = false, version = 0 };
var r := ascii_reader{ text = text };
r.read_list(&mut made, absent) else (e) { return e; };
if made.child(absent, "FBXHeaderExtension") is some(header) {
if made.child(header, "FBXVersion") is some(version) && made.property(version, 0) is some(&given) {
made.version = given.whole() as u32;
}
}
return failed(failure::malformed, "It's neither binary FBX nor ASCII FBX of a header and objects.") if made.version == 0 && made.child(absent, "Objects") is none;
return failed(failure::unsupported, $"It's FBX {made.version}, of before 7.0, which names its objects rather than numbering them.".span) if made.version != 0 && made.version < 7000;
if made.version == 0 {
made.version = 7400;
}
return made;
}
# An error at the line it's reading.
def ascii_reader::wrong(&this, why: strview): fbx_error {
return failed(failure::malformed, $"line {this.line}: {why}".span);
}
# Passes over spaces, lines' ends, and comments.
def ascii_reader::skip_blank(&mut this): void {
while this.at < this.text.length {
val c := this.text[this.at];
if c == '\n' {
this.line++;
} else if c == ';' {
while this.at < this.text.length && this.text[this.at] != '\n' {
this.at++;
}
continue;
} else if c != ' ' && c != '\t' && c != '\r' {
return;
}
this.at++;
}
}
# Passes over spaces, but not a line's end.
def ascii_reader::skip_spaces(&mut this): void {
while this.at < this.text.length && (this.text[this.at] == ' ' || this.text[this.at] == '\t' || this.text[this.at] == '\r') {
this.at++;
}
}
# Reads nodes, beneath a parent, to the `}` closing it, or the text's end at the
# root.
def ascii_reader::read_list(&mut this, made: &mut document, parent: u32): result<void, fbx_error> {
while true {
this.skip_blank();
if this.at >= this.text.length {
return this.wrong("A `{` isn't closed.") if parent != absent;
return;
}
if this.text[this.at] == '}' {
return this.wrong("A `}` closes nothing.") if parent == absent;
this.at++;
return;
}
val begun := this.at;
while this.at < this.text.length && this.text[this.at] != ':' && this.text[this.at] != '\n' && this.text[this.at] != '{' && this.text[this.at] != '}' {
this.at++;
}
if this.at >= this.text.length || this.text[this.at] != ':' {
return this.wrong("A node's name isn't followed by `:`.");
}
val name := trimmed(this.text.substr(begun, this.at - begun));
this.at++;
val index := made.add(parent, name);
this.read_properties(made, index) else (e) { return e; };
}
return;
}
# Reads a node's properties, to its line's end, and its children, where it has
# them.
def ascii_reader::read_properties(&mut this, made: &mut document, index: u32): result<void, fbx_error> {
while true {
this.skip_spaces();
return if this.at >= this.text.length;
val c := this.text[this.at];
return if c == '\n' || c == '}';
if c == ';' {
while this.at < this.text.length && this.text[this.at] != '\n' {
this.at++;
}
return;
}
if c == '{' {
this.at++;
return this.read_list(made, index);
}
if c == ',' {
this.at++;
this.skip_blank();
continue;
}
if c == '*' {
this.at++;
var held := this.read_array() else (e) { return e; };
made.nodes[index as u64].properties.append(move(held));
continue;
}
if c == '"' {
this.at++;
val begun := this.at;
while this.at < this.text.length && this.text[this.at] != '"' {
if this.text[this.at] == '\n' {
this.line++;
}
this.at++;
}
return this.wrong("Text isn't closed by `\"`.") if this.at >= this.text.length;
var held := property{ kind = property_kind::text };
held.bytes.extend(this.text.substr(begun, this.at - begun));
this.at++;
made.nodes[index as u64].properties.append(move(held));
continue;
}
val word := this.read_word();
return this.wrong($"`{c}` begins no property.".span) if word.length == 0;
var held := word_property(word);
made.nodes[index as u64].properties.append(move(held));
}
return;
}
# Reads a word: a number, or a bare word, to the next comma, space, or brace.
def ascii_reader::read_word(&mut this): strview {
val begun := this.at;
while this.at < this.text.length {
val c := this.text[this.at];
break if c == ',' || c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '{' || c == '}' || c == ';';
this.at++;
}
return this.text.substr(begun, this.at - begun);
}
# A word as a property: an integer, a float, or text.
def word_property(word: strview): property {
if is_whole(word) && integer::parse::<i64>(word) is value(n) {
return property{ kind = property_kind::int64, integer = n };
}
if real::parse::<f64>(word) is value(x) {
return property{ kind = property_kind::float64, real = x };
}
var made := property{ kind = property_kind::text };
made.bytes.extend(word);
return made;
}
# Whether a word is written as a whole number: digits, after a sign.
def is_whole(word: strview): bool {
return false if word.length == 0;
var from: u64 = word[0] == '-' || word[0] == '+' ? 1 : 0;
return false if from >= word.length;
for i in from..word.length {
return false if word[i] < '0' || word[i] > '9';
}
return true;
}
# Reads an array, after its `*`: its count, then `{ a: ` and its numbers, to
# its `}`.
def ascii_reader::read_array(&mut this): result<property, fbx_error> {
val count_word := this.read_word();
val count := integer::parse::<i64>(count_word) else (_) { return this.wrong($"`*{count_word}` isn't an array's count.".span); };
this.skip_blank();
return this.wrong("An array's count isn't followed by `{`.") if this.at >= this.text.length || this.text[this.at] != '{';
this.at++;
this.skip_blank();
if this.at < this.text.length && this.text[this.at] == 'a' {
this.at++;
this.skip_spaces();
return this.wrong("An array's `a` isn't followed by `:`.") if this.at >= this.text.length || this.text[this.at] != ':';
this.at++;
}
var integers := vector::<i64>::new();
var reals := vector::<f64>::new();
var floating := false;
while true {
this.skip_blank();
return this.wrong("An array isn't closed by `}`.") if this.at >= this.text.length;
val c := this.text[this.at];
if c == '}' {
this.at++;
break;
}
if c == ',' {
this.at++;
continue;
}
val word := this.read_word();
return this.wrong($"`{c}` isn't a number of an array.".span) if word.length == 0;
if !floating && is_whole(word) {
val n := integer::parse::<i64>(word) else (_) { return this.wrong($"`{word}` isn't a number.".span); };
integers.append(n);
continue;
}
val x := real::parse::<f64>(word) else (_) { return this.wrong($"`{word}` isn't a number.".span); };
if !floating {
floating = true;
for i in 0..integers.length {
reals.append(integers[i] as f64);
}
integers.clear();
}
reals.append(x);
}
val read := floating ? reals.length : integers.length;
return this.wrong($"An array says it holds {count} numbers, but holds {read}.".span) if read != count as u64;
var made := property{};
if floating {
made.kind = property_kind::float64s;
made.reals = move(reals);
} else {
made.kind = property_kind::int64s;
made.integers = move(integers);
}
return made;
}
# Text with the spaces about it taken away.
def trimmed(text: strview): strview {
var first: u64 = 0;
var end := text.length;
while first < end && (text[first] == ' ' || text[first] == '\t' || text[first] == '\r' || text[first] == '\n') {
first++;
}
while end > first && (text[end - 1] == ' ' || text[end - 1] == '\t' || text[end - 1] == '\r' || text[end - 1] == '\n') {
end--;
}
return text.substr(first, end - first);
}