1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
use bufio;
use dejson;
use encoding::json;
use errors;
use fs;
use io;
use os;
use path;
use strings;
use trace;
def MISSINGNO = "minecraft:missingno";
def BUILTIN_MISSING = "minecraft:builtin/missing";
type Pack = struct {
fs: *fs::fs,
kind: str,
};
fn resource_splitpath(path: str) (str, str, str, str, str) = {
const (base, path) = strings::cut(path, "/");
const (ns, path) = strings::cut(path, "/");
const (kind, path) = strings::cut(path, "/");
const bnameindex = match (strings::rindex(path, '/')) {
case let index: size =>
yield index + 1;
case void =>
yield 0z;
};
const extindex = match (strings::rindex(
strings::sub(path, bnameindex, strings::end), '.')) {
case let index: size =>
yield bnameindex + index;
case void =>
yield len(path);
};
const name = strings::sub(path, 0, extindex);
const ext = strings::sub(path, extindex, strings::end);
return (base, ns, kind, name, ext);
};
fn resource_open(
pack: *Pack,
kind: str,
ident: str,
ext: str,
tr: *trace::tracer,
) (io::handle | trace::failed) = {
const (ns, name) = ident_split(ident);
const fname = strings::concat(name, ext);
defer free(fname);
let path = path::init()!;
if (path::set(&path, pack.kind, ns, kind, fname) is path::too_long) {
return trace::error(tr, "Path too long");
};
const path = path::string(&path);
match (fs::open(pack.fs, path, fs::flag::RDONLY)) {
case let f: io::handle =>
return f;
case let err: fs::error =>
return trace::error(tr, "open: {}", fs::strerror(err));
};
};
fn resource_load_json(
pack: *Pack,
kind: str,
ident: str,
ext: str,
tr: *trace::tracer,
) (json::value | trace::failed) = {
const f = resource_open(pack, kind, ident, ext, tr)?;
let rbuf: [os::BUFSZ]u8 = [0...];
let buf = bufio::init(f, rbuf, []);
const json = match (json::load(&buf)) {
case let json: json::value =>
yield json;
case let err: json::error =>
io::close(f): void;
return trace::error(tr, "Invalid JSON: {}",
json::strerror(err));
};
match (io::close(f)) {
case void => void;
case let err: io::error =>
json::finish(json);
return trace::error(tr, "close: {}", io::strerror(err));
};
return json;
};
fn resource_search(
pack: *Pack,
kind: str,
exts: str...
) [](str, str) = {
const tr = trace::ctx(&trace::root, "resource search");
let out: [](str, str) = [];
let path = path::init()!;
const it = match (fs::iter(pack.fs, pack.kind)) {
case let x: *fs::iterator =>
yield x;
case let err: fs::error =>
trace::error(&tr, "{}: fs::iter: {}",
pack.kind, fs::strerror(err)): void;
return out;
};
defer fs::finish(it);
for (true) match (fs::next(it)) {
case let ent: fs::dirent =>
if (!fs::isdir(ent.ftype)) continue;
if (strings::hasprefix(ent.name, ".")) continue;
if (path::set(&path, pack.kind, ent.name, kind)
is path::too_long) {
trace::error(&tr, "{}/{}/{}: Path too long",
pack.kind, ent.name, kind): void;
continue;
};
resource_search_ns(pack, &out, path::string(&path), &tr, exts...);
case done => break;
};
return out;
};
fn resource_search_ns(
pack: *Pack,
out: *[](str, str),
parent: str,
tr: *trace::tracer,
exts: str...
) void = {
let path = path::init()!;
const it = match (fs::iter(pack.fs, parent)) {
case let x: *fs::iterator =>
yield x;
case errors::noentry =>
return;
case let err: fs::error =>
trace::error(tr, "{}: fs::iter: {}",
parent, fs::strerror(err)): void;
return;
};
defer fs::finish(it);
for (true) match (fs::next(it)) {
case let ent: fs::dirent =>
if (strings::hasprefix(ent.name, ".")) continue;
if (path::set(&path, parent, ent.name) is path::too_long) {
trace::error(tr, "{}/{}: Path too long",
parent, ent.name): void;
continue;
};
const path = path::string(&path);
if (fs::isdir(ent.ftype)) {
resource_search_ns(pack, out, path, tr, exts...);
} else if (fs::isfile(ent.ftype)) {
const (_, ns, _, name, ext) =
resource_splitpath(path);
for (let i = 0z; i < len(exts); i += 1) {
if (ext == exts[i]) {
const ident = ident_make(ns, name);
// yes, this borrow from input is
// intentional. needs to be documented
// later.
append(out, (ident, exts[i]));
break;
};
};
};
case done => break;
};
};
fn wassert_fields(de: *dejson::deser, names: str...) (void | dejson::error) = {
dejson::object(de)?;
match (dejson::assert_fields(de, names...)) {
case let err: dejson::error =>
defer free(err);
trace::warn(&trace::root, "{}", err);
case void => void;
};
};
|