@pkmn/engine 0.1.0-dev.3a62fd35 → 0.1.0-dev.3d09f03a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +31 -19
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +39 -71
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +25 -4
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +40 -90
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +20 -1
  26. package/build/pkg/protocol.js +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +69 -76
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +152 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +359 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +33 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +241 -106
  39. package/package.json +58 -130
  40. package/src/bin/install-pkmn-engine +34 -8
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +200 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +50 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +8 -8
  47. package/src/lib/common/debug.zig +32 -11
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +11 -17
  50. package/src/lib/common/options.zig +6 -0
  51. package/src/lib/common/protocol.zig +201 -163
  52. package/src/lib/common/rational.zig +338 -255
  53. package/src/lib/common/rng.zig +11 -13
  54. package/src/lib/common/util.zig +33 -1
  55. package/src/lib/gen1/calc.zig +721 -172
  56. package/src/lib/gen1/chance.zig +461 -299
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +2 -4
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +43 -46
  61. package/src/lib/gen1/helpers.zig +91 -549
  62. package/src/lib/gen1/mechanics.zig +398 -265
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +91 -509
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +33 -35
  70. package/src/lib/gen2/generated.zig +18 -23
  71. package/src/lib/gen2/helpers.zig +45 -26
  72. package/src/lib/gen2/mechanics.zig +215 -157
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +4 -121
  75. package/src/lib/pkmn.zig +106 -33
  76. package/src/lib/wasm.zig +9 -9
  77. package/src/tools/display/dom.ts +93 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/gen1/README.md +0 -894
  82. package/src/lib/gen2/README.md +0 -329
  83. package/src/test/display/pkmn.html.tmpl +0 -368
package/build.zig CHANGED
@@ -1,5 +1,9 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
2
+ const std = @import("std");
3
+
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
3
7
 
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
@@ -9,16 +13,16 @@ pub const Options = struct {
9
13
  };
10
14
 
11
15
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
12
- const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
13
16
  const build_options = b.addOptions();
14
17
  build_options.addOption(?bool, "showdown", options.showdown);
15
18
  build_options.addOption(?bool, "log", options.log);
16
19
  build_options.addOption(?bool, "chance", options.chance);
17
20
  build_options.addOption(?bool, "calc", options.calc);
18
- return b.createModule(.{
19
- .root_source_file = .{ .path = dirname ++ "/src/lib/pkmn.zig" },
20
- .imports = &.{.{ .name = "build_options", .module = build_options.createModule() }},
21
- });
21
+
22
+ const root_source_file = b.path("src/lib/pkmn.zig");
23
+ const imports: []const std.Build.Module.Import =
24
+ &.{.{ .name = "build_options", .module = build_options.createModule() }};
25
+ return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
22
26
  }
23
27
 
24
28
  pub fn build(b: *std.Build) !void {
@@ -28,6 +32,10 @@ pub fn build(b: *std.Build) !void {
28
32
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
29
33
  const node_import_lib =
30
34
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
35
+ const demo = if (try exists(b, "src/tools/demo.zig"))
36
+ b.option(bool, "demo", "Build the demo library") orelse false
37
+ else
38
+ false;
31
39
  const wasm = b.option(bool, "wasm", "Build a WASM library") orelse false;
32
40
  const wasm_stack_size =
33
41
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
@@ -44,7 +52,7 @@ pub fn build(b: *std.Build) !void {
44
52
  defer parsed.deinit();
45
53
  const version = parsed.value.object.get("version").?.string;
46
54
  const description = parsed.value.object.get("description").?.string;
47
- var repository = std.mem.split(u8, parsed.value.object.get("repository").?.string, ":");
55
+ var repository = std.mem.splitScalar(u8, parsed.value.object.get("repository").?.string, ':');
48
56
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
49
57
 
50
58
  const showdown =
@@ -52,6 +60,7 @@ pub fn build(b: *std.Build) !void {
52
60
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
53
61
  const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
54
62
  const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
63
+ const extra = b.option([]const []const u8, "option", "Enable extra option");
55
64
 
56
65
  const options = b.addOptions();
57
66
  options.addOption(?bool, "showdown", showdown);
@@ -59,32 +68,67 @@ pub fn build(b: *std.Build) !void {
59
68
  options.addOption(?bool, "chance", chance);
60
69
  options.addOption(?bool, "calc", calc);
61
70
 
71
+ if (extra) |opts| {
72
+ for (opts) |opt| {
73
+ if (std.mem.indexOfScalar(u8, opt, '=')) |i| {
74
+ options.addOption(?bool, opt[0..i], if (std.mem.eql(u8, opt[i + 1 ..], "true"))
75
+ true
76
+ else if (std.mem.eql(u8, opt[i + 1 ..], "false"))
77
+ false
78
+ else {
79
+ std.log.err("Invalid option: {s}\n", .{opt});
80
+ return error.InvalidOption;
81
+ });
82
+ } else {
83
+ options.addOption(?bool, opt, true);
84
+ }
85
+ }
86
+ }
87
+
62
88
  const name = if (showdown) "pkmn-showdown" else "pkmn";
63
89
 
90
+ const pkmn: *std.Build.Module = b.addModule("pkmn", .{
91
+ .root_source_file = b.path("src/lib/pkmn.zig"),
92
+ .optimize = optimize,
93
+ .target = target,
94
+ .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
95
+ });
96
+
64
97
  var c = false;
65
98
  if (node_headers) |headers| {
99
+ const translate_c = b.addTranslateC(.{
100
+ .root_source_file = b.path("src/lib/napi.h"),
101
+ .target = target,
102
+ .optimize = optimize,
103
+ });
104
+ translate_c.addSystemIncludePath(b.path(headers));
66
105
  const addon = b.fmt("{s}.node", .{name});
67
- const lib = b.addSharedLibrary(.{
106
+ const path = b.path("src/lib/node.zig");
107
+ const lib = b.addLibrary(.{
108
+ .linkage = .dynamic,
68
109
  .name = addon,
69
- .root_source_file = .{ .path = "src/lib/node.zig" },
70
- .optimize = optimize,
71
- .target = target,
72
- .strip = strip,
73
- .pic = pic,
110
+ .root_module = b.createModule(.{
111
+ .root_source_file = path,
112
+ .optimize = optimize,
113
+ .target = target,
114
+ .strip = strip,
115
+ .pic = pic,
116
+ }),
74
117
  });
75
118
  lib.root_module.addOptions("build_options", options);
76
- lib.addSystemIncludePath(.{ .path = headers });
77
- lib.linkLibC();
119
+ lib.root_module.addImport("napi", translate_c.createModule());
120
+ lib.root_module.link_libc = true;
78
121
  if (node_import_lib) |il| {
79
- lib.addObjectFile(.{ .path = il });
122
+ lib.root_module.addObjectFile(b.path(il));
80
123
  } else if (target.result.os.tag == .windows) {
81
- try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
124
+ var err = std.Io.File.stderr().writer(b.graph.io, &.{});
125
+ try err.interface.writeAll("Must provide --node-import-library path on Windows\n");
82
126
  std.process.exit(1);
83
127
  }
84
128
  lib.linker_allow_shlib_undefined = true;
85
129
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
86
130
  // Always emit to build/lib because this is where the driver code expects to find it
87
- // TODO(ziglang/zig#2231): using the following used to work (though was hacky):
131
+ // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
88
132
  //
89
133
  // lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
90
134
  // b.getInstallStep().dependOn(&lib.step);
@@ -93,67 +137,46 @@ pub fn build(b: *std.Build) !void {
93
137
  // rename the file ourself in install-pkmn-engine
94
138
  b.installArtifact(lib);
95
139
  } else if (wasm) {
96
- const exe = b.addExecutable(.{
97
- .name = name,
98
- .root_source_file = .{ .path = "src/lib/wasm.zig" },
99
- .optimize = switch (optimize) {
100
- .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
101
- else => optimize,
102
- },
103
- .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
104
- .strip = strip,
105
- .pic = pic,
106
- });
107
- exe.root_module.addOptions("build_options", options);
108
- exe.root_module.export_symbol_names = &[_][]const u8{
109
- "SHOWDOWN",
110
- "LOG",
111
- "CHANCE",
112
- "CALC",
113
- "GEN1_CHOICES_SIZE",
114
- "GEN1_LOGS_SIZE",
115
- };
116
- exe.entry = .disabled;
117
- exe.stack_size = wasm_stack_size;
118
- const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
119
- if (optimize != .Debug and opt != null) {
120
- const out = b.fmt("build/lib/{s}.wasm", .{name});
121
- const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
122
- sh.addArtifactArg(exe);
123
- sh.addArg("-o");
124
- sh.addFileArg(.{ .path = out });
125
- b.getInstallStep().dependOn(&sh.step);
126
- } else {
127
- b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
128
- .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
129
- }).step);
130
- }
140
+ const path = "src/lib/wasm.zig";
141
+ try buildWasm(b, name, path, optimize, strip, pic, wasm_stack_size, null, options);
142
+ } else if (demo) {
143
+ const path = "src/tools/demo.zig";
144
+ const n = if (showdown) "demo-showdown" else "demo";
145
+ const mod = pkmn;
146
+ try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
131
147
  } else if (dynamic) {
132
- const lib = b.addSharedLibrary(.{
148
+ const path = b.path("src/lib/c.zig");
149
+ const lib = b.addLibrary(.{
150
+ .linkage = .dynamic,
133
151
  .name = name,
134
- .root_source_file = .{ .path = "src/lib/c.zig" },
135
- .version = try std.SemanticVersion.parse(version),
136
- .optimize = optimize,
137
- .target = target,
138
- .strip = strip,
139
- .pic = pic,
152
+ .root_module = b.createModule(.{
153
+ .root_source_file = path,
154
+ .optimize = optimize,
155
+ .target = target,
156
+ .strip = strip,
157
+ .pic = pic,
158
+ }),
140
159
  });
141
160
  lib.root_module.addOptions("build_options", options);
142
- lib.addIncludePath(.{ .path = "src/include" });
161
+ lib.root_module.addIncludePath(b.path("src/include"));
143
162
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
144
163
  b.installArtifact(lib);
145
164
  c = true;
146
165
  } else {
147
- const lib = b.addStaticLibrary(.{
166
+ const path = b.path("src/lib/c.zig");
167
+ const lib = b.addLibrary(.{
168
+ .linkage = .static,
148
169
  .name = name,
149
- .root_source_file = .{ .path = "src/lib/c.zig" },
150
- .optimize = optimize,
151
- .target = target,
152
- .strip = strip,
153
- .pic = pic,
170
+ .root_module = b.createModule(.{
171
+ .root_source_file = path,
172
+ .optimize = optimize,
173
+ .target = target,
174
+ .strip = strip,
175
+ .pic = pic,
176
+ }),
154
177
  });
155
178
  lib.root_module.addOptions("build_options", options);
156
- lib.addIncludePath(.{ .path = "src/include" });
179
+ lib.root_module.addIncludePath(b.path("src/include"));
157
180
  lib.bundle_compiler_rt = true;
158
181
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
159
182
  if (emit_asm) {
@@ -175,20 +198,25 @@ pub fn build(b: *std.Build) !void {
175
198
  }
176
199
 
177
200
  if (c) {
178
- const header = b.addInstallFileWithDir(
179
- .{ .path = "src/include/pkmn.h" },
180
- .header,
181
- "pkmn.h",
182
- );
201
+ const path = b.path("src/include/pkmn.h");
202
+ const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
183
203
  b.getInstallStep().dependOn(&header.step);
184
204
 
185
205
  const pc = b.fmt("lib{s}.pc", .{name});
186
- const file = try b.cache_root.join(b.allocator, &.{pc});
187
- const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
206
+ const cwd = try std.process.currentPathAlloc(b.graph.io, b.allocator);
207
+ const file = try std.Io.Dir.path.relative(
208
+ b.allocator,
209
+ cwd,
210
+ &b.graph.environ_map,
211
+ cwd,
212
+ try b.cache_root.join(b.allocator, &.{pc}),
213
+ );
214
+ const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
215
+ defer pkgconfig_file.close(b.graph.io);
188
216
 
189
217
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
190
- const writer = pkgconfig_file.writer();
191
- try writer.print(
218
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
219
+ try writer.interface.print(
192
220
  \\prefix={0s}/{1s}
193
221
  \\includedir=${{prefix}}/include
194
222
  \\libdir=${{prefix}}/lib
@@ -200,12 +228,11 @@ pub fn build(b: *std.Build) !void {
200
228
  \\Cflags: -I${{includedir}}
201
229
  \\Libs: -L${{libdir}} -l{2s}
202
230
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
203
- defer pkgconfig_file.close();
204
231
 
205
232
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
206
233
  }
207
234
 
208
- const config = .{
235
+ const config: Config = .{
209
236
  .target = target,
210
237
  .optimize = optimize,
211
238
  .pic = pic,
@@ -216,7 +243,7 @@ pub fn build(b: *std.Build) !void {
216
243
  // TODO: tests can be run multiple times due to @imports
217
244
  const tests = TestStep.create(b, options, config);
218
245
 
219
- var exes = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
246
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
220
247
  const tools: ToolConfig = .{
221
248
  .options = .{
222
249
  .showdown = showdown,
@@ -224,6 +251,7 @@ pub fn build(b: *std.Build) !void {
224
251
  .chance = chance,
225
252
  .calc = calc,
226
253
  },
254
+ .module = pkmn,
227
255
  .general = config,
228
256
  .tool = .{
229
257
  .tests = if (tests.build) tests else null,
@@ -262,6 +290,85 @@ pub fn build(b: *std.Build) !void {
262
290
  }
263
291
  }
264
292
 
293
+ fn buildWasm(
294
+ b: *std.Build,
295
+ name: []const u8,
296
+ root_src_file: []const u8,
297
+ optimize: std.builtin.OptimizeMode,
298
+ strip: ?bool,
299
+ pic: ?bool,
300
+ wasm_stack_size: u64,
301
+ import: ?*std.Build.Module,
302
+ options: anytype,
303
+ ) !void {
304
+ const mode = switch (optimize) {
305
+ .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
306
+ else => optimize,
307
+ };
308
+ // https://webassembly.org/features/
309
+ var features = std.Target.wasm.featureSet(&.{
310
+ .atomics,
311
+ .bulk_memory,
312
+ // .exception_handling,
313
+ .extended_const,
314
+ // .half_precision,
315
+ // .multimemory,
316
+ .mutable_globals,
317
+ .nontrapping_fptoint,
318
+ .reference_types,
319
+ // .relaxed_simd,
320
+ .sign_ext,
321
+ .simd128,
322
+ // .tail_call,
323
+ });
324
+ if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
325
+ features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
326
+ }
327
+ const freestanding = b.resolveTargetQuery(.{
328
+ .cpu_arch = .wasm32,
329
+ .os_tag = .freestanding,
330
+ .cpu_features_add = features,
331
+ });
332
+ const path = b.path(root_src_file);
333
+ const exe = b.addExecutable(.{
334
+ .name = name,
335
+ .root_module = b.createModule(.{
336
+ .root_source_file = path,
337
+ .optimize = mode,
338
+ .target = freestanding,
339
+ .strip = strip,
340
+ .pic = pic,
341
+ }),
342
+ });
343
+
344
+ var file = try std.Io.Dir.cwd().openFile(b.graph.io, root_src_file, .{});
345
+ defer file.close(b.graph.io);
346
+ var reader = file.reader(b.graph.io, &.{});
347
+ const bytes = try reader.interface.allocRemaining(b.allocator, .unlimited);
348
+ exe.root_module.export_symbol_names = try exports(b, bytes);
349
+ exe.entry = .disabled;
350
+
351
+ if (import) |i| {
352
+ exe.root_module.addImport("pkmn", i);
353
+ }
354
+ exe.stack_size = wasm_stack_size;
355
+ exe.root_module.addOptions("build_options", options);
356
+
357
+ const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
358
+ if (optimize != .Debug and opt != null) {
359
+ const out = b.fmt("build/lib/{s}.wasm", .{name});
360
+ const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
361
+ sh.addArtifactArg(exe);
362
+ sh.addArg("-o");
363
+ sh.addFileArg(b.path(out));
364
+ b.getInstallStep().dependOn(&sh.step);
365
+ } else {
366
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
367
+ .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
368
+ }).step);
369
+ }
370
+ }
371
+
265
372
  fn maybeStrip(
266
373
  b: *std.Build,
267
374
  artifact: *std.Build.Step.Compile,
@@ -294,7 +401,6 @@ const TestStep = struct {
294
401
 
295
402
  pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
296
403
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
297
- const test_file = b.option([]const u8, "test-file", "Input file for test");
298
404
  const test_filter =
299
405
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
300
406
 
@@ -302,21 +408,20 @@ const TestStep = struct {
302
408
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
303
409
  self.* = .{ .step = step, .build = test_filter == null };
304
410
 
305
- const path = test_file orelse "src/lib/test.zig";
411
+ const path = b.path("src/lib/test.zig");
306
412
  const tests = b.addTest(.{
307
- .name = b.fmt("{s}-{s}", .{
308
- std.fs.path.basename(std.fs.path.dirname(path).?),
309
- std.fs.path.stem(std.fs.path.basename(path)),
413
+ .root_module = b.createModule(.{
414
+ .root_source_file = path,
415
+ .optimize = config.optimize,
416
+ .target = config.target,
417
+ .single_threaded = true,
418
+ .strip = config.strip,
419
+ .pic = config.pic,
310
420
  }),
311
- .root_source_file = .{ .path = path },
312
- .optimize = config.optimize,
313
- .target = config.target,
314
- .filter = test_filter,
315
- .single_threaded = true,
316
- .strip = config.strip,
317
- .pic = config.pic,
421
+ .filters = if (test_filter) |filter| &.{filter} else &.{},
318
422
  });
319
423
  tests.root_module.addOptions("build_options", options);
424
+
320
425
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
321
426
  if (coverage) |c| {
322
427
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
@@ -329,19 +434,17 @@ const TestStep = struct {
329
434
 
330
435
  const ToolConfig = struct {
331
436
  options: Options,
437
+ module: ?*std.Build.Module,
332
438
  general: Config,
333
439
  tool: struct {
334
440
  tests: ?*TestStep,
335
441
  name: ?[]const u8 = null,
336
- exes: *std.ArrayList(*std.Build.Step.Compile),
442
+ exes: *ArrayList(*std.Build.Step.Compile),
337
443
  },
338
444
  };
339
445
 
340
446
  fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
341
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
342
- error.FileNotFound => return null,
343
- else => |e| return e,
344
- };
447
+ if (!try exists(b, path)) return null;
345
448
  var name = config.tool.name orelse std.fs.path.basename(path);
346
449
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
347
450
  if (index) |i| name = name[0..i];
@@ -349,17 +452,23 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
349
452
 
350
453
  const exe = b.addExecutable(.{
351
454
  .name = name,
352
- .root_source_file = .{ .path = path },
353
- .target = config.general.target,
354
- .optimize = config.general.optimize,
355
- .single_threaded = true,
356
- .strip = config.general.strip,
357
- .pic = config.general.pic,
455
+ .root_module = b.createModule(.{
456
+ .root_source_file = b.path(path),
457
+ .target = config.general.target,
458
+ .optimize = config.general.optimize,
459
+ .strip = config.general.strip,
460
+ .pic = config.general.pic,
461
+ }),
358
462
  });
359
- exe.root_module.addImport("pkmn", module(b, config.options));
463
+
464
+ const import = if (config.module) |m| m else module(b, config.options);
465
+ exe.root_module.addImport("pkmn", import);
466
+ if (config.general.target.result.os.tag == .windows) {
467
+ exe.root_module.linkSystemLibrary("advapi32", .{});
468
+ }
360
469
 
361
470
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
362
- config.tool.exes.append(exe) catch @panic("OOM");
471
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
363
472
 
364
473
  const run = b.addRunArtifact(exe);
365
474
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -368,10 +477,18 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
368
477
  return run;
369
478
  }
370
479
 
480
+ fn exists(b: *std.Build, path: []const u8) !bool {
481
+ std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
482
+ error.FileNotFound => return false,
483
+ else => |e| return e,
484
+ };
485
+ return true;
486
+ }
487
+
371
488
  const ToolsStep = struct {
372
489
  step: std.Build.Step,
373
490
 
374
- pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.Step.Compile)) *ToolsStep {
491
+ pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
375
492
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
376
493
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
377
494
  self.* = .{ .step = step };
@@ -379,3 +496,21 @@ const ToolsStep = struct {
379
496
  return self;
380
497
  }
381
498
  };
499
+
500
+ pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
501
+ var symbols: ArrayList([]const u8) = .empty;
502
+
503
+ var it = std.mem.splitSequence(u8, bytes, "export ");
504
+ _ = it.next();
505
+ while (it.next()) |s| {
506
+ if (std.mem.startsWith(u8, s, "const ")) {
507
+ const i = std.mem.indexOf(u8, s[6..], " ").?;
508
+ try symbols.append(b.allocator, s[6 .. 6 + i]);
509
+ } else { // "fn "
510
+ const i = std.mem.indexOf(u8, s[3..], "(").?;
511
+ try symbols.append(b.allocator, s[3 .. 3 + i]);
512
+ }
513
+ }
514
+
515
+ return symbols.items;
516
+ }