@pkmn/engine 0.1.0-dev.1aa30742 → 0.1.0-dev.1c183b1a

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 (45) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +23 -54
  3. package/build/pkg/addon/wasm.js.map +1 -1
  4. package/build/pkg/addon.d.ts +4 -4
  5. package/build/pkg/addon.js.map +1 -1
  6. package/build/pkg/protocol.d.ts +11 -2
  7. package/build/pkg/protocol.js.map +1 -1
  8. package/build/tools/debug.js +37 -3
  9. package/build/tools/debug.js.map +1 -1
  10. package/build/tools/display/debug.jsx +1 -87
  11. package/build/tools/display/debug.jsx.map +1 -1
  12. package/build/tools/display/ui.jsx +3 -2
  13. package/build/tools/display/ui.jsx.map +1 -1
  14. package/build/tools/display/util.js +125 -1
  15. package/build/tools/display/util.js.map +1 -1
  16. package/build.zig +138 -230
  17. package/package.json +24 -22
  18. package/src/bin/install-pkmn-engine +22 -7
  19. package/src/lib/bindings/c.zig +27 -82
  20. package/src/lib/bindings/node.zig +7 -11
  21. package/src/lib/bindings/wasm.zig +6 -15
  22. package/src/lib/common/array.zig +5 -12
  23. package/src/lib/common/debug.zig +33 -59
  24. package/src/lib/common/js.zig +3 -9
  25. package/src/lib/common/optional.zig +11 -21
  26. package/src/lib/common/protocol.zig +224 -189
  27. package/src/lib/common/rational.zig +91 -173
  28. package/src/lib/common/util.zig +14 -46
  29. package/src/lib/gen1/calc.zig +121 -108
  30. package/src/lib/gen1/chance.zig +112 -75
  31. package/src/lib/gen1/data/types.zig +2 -2
  32. package/src/lib/gen1/data.zig +5 -5
  33. package/src/lib/gen1/helpers.zig +8 -10
  34. package/src/lib/gen1/mechanics.zig +41 -32
  35. package/src/lib/gen2/calc.zig +3 -6
  36. package/src/lib/gen2/chance.zig +12 -18
  37. package/src/lib/gen2/data/types.zig +6 -3
  38. package/src/lib/gen2/data.zig +8 -7
  39. package/src/lib/gen2/helpers.zig +4 -6
  40. package/src/lib/gen2/mechanics.zig +17 -17
  41. package/src/lib/napi.h +2 -0
  42. package/src/lib/node.zig +1 -5
  43. package/src/lib/pkmn.zig +91 -17
  44. package/src/lib/wasm.zig +2 -6
  45. package/src/tools/display/dom.ts +43 -28
package/build.zig CHANGED
@@ -1,6 +1,10 @@
1
1
  const builtin = @import("builtin");
2
2
  const std = @import("std");
3
3
 
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
7
+
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
6
10
  log: ?bool = null,
@@ -8,48 +12,27 @@ pub const Options = struct {
8
12
  calc: ?bool = null,
9
13
  };
10
14
 
11
- const Step = if (@hasDecl(std.Build, "Step")) std.Build.Step else .{
12
- .Compile = std.Build.CompileStep,
13
- .Options = std.Build.OptionsStep,
14
- .Run = std.Build.RunStep,
15
- };
16
- const has_path = @hasField(std.Build.LazyPath, "path");
17
- const root_module = @hasField(Step.Compile, "root_module");
18
- const force_pic = !@hasField(std.Build.SharedLibraryOptions, "pic");
19
- const ResolvedTarget =
20
- if (@hasDecl(std.Build, "ResolvedTarget")) std.Build.ResolvedTarget else std.zig.CrossTarget;
21
-
22
15
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
23
- const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
24
16
  const build_options = b.addOptions();
25
17
  build_options.addOption(?bool, "showdown", options.showdown);
26
18
  build_options.addOption(?bool, "log", options.log);
27
19
  build_options.addOption(?bool, "chance", options.chance);
28
20
  build_options.addOption(?bool, "calc", options.calc);
29
21
 
30
- const root_source_file: std.Build.LazyPath =
31
- .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
32
- const Import = if (@hasDecl(std.Build.Module, "Import"))
33
- std.Build.Module.Import
34
- else
35
- std.Build.ModuleDependency;
36
- const imports: []const Import =
22
+ const root_source_file = b.path("src/lib/pkmn.zig");
23
+ const imports: []const std.Build.Module.Import =
37
24
  &.{.{ .name = "build_options", .module = build_options.createModule() }};
38
- return b.createModule(if (@hasDecl(std.Build, "CreateModuleOptions"))
39
- .{ .source_file = root_source_file, .dependencies = imports }
40
- else
41
- .{ .root_source_file = root_source_file, .imports = imports });
25
+ return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
42
26
  }
43
27
 
44
28
  pub fn build(b: *std.Build) !void {
45
29
  const target = b.standardTargetOptions(.{});
46
30
  const optimize = b.standardOptimizeOption(.{});
47
31
 
48
- const default: ?bool = if (force_pic) false else null;
49
32
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
50
33
  const node_import_lib =
51
34
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
52
- const demo = if (try exists("src/tools/demo.zig"))
35
+ const demo = if (try exists(b, "src/tools/demo.zig"))
53
36
  b.option(bool, "demo", "Build the demo library") orelse false
54
37
  else
55
38
  false;
@@ -57,8 +40,8 @@ pub fn build(b: *std.Build) !void {
57
40
  const wasm_stack_size =
58
41
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
59
42
  const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
60
- const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse default;
61
- const pic = b.option(bool, "pic", "Force position independent code") orelse default;
43
+ const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
44
+ const pic = b.option(bool, "pic", "Force position independent code");
62
45
  const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
63
46
  const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
64
47
 
@@ -69,15 +52,9 @@ pub fn build(b: *std.Build) !void {
69
52
  defer parsed.deinit();
70
53
  const version = parsed.value.object.get("version").?.string;
71
54
  const description = parsed.value.object.get("description").?.string;
72
- var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
55
+ var repository = std.mem.splitScalar(u8, parsed.value.object.get("repository").?.string, ':');
73
56
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
74
57
 
75
- const fast = try std.SemanticVersion.parse("0.12.0-dev.866+3a47bc715");
76
- if (optimize != .Debug and fast.order(builtin.zig_version) == .lt) {
77
- std.log.warn("Release builds are ~10-20% slower than before Zig " ++
78
- "v0.12.0-dev.866+3a47bc715 due to ziglang/zig#17768", .{});
79
- }
80
-
81
58
  const showdown =
82
59
  b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
83
60
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
@@ -110,48 +87,46 @@ pub fn build(b: *std.Build) !void {
110
87
 
111
88
  const name = if (showdown) "pkmn-showdown" else "pkmn";
112
89
 
113
- const pkmn: ?*std.Build.Module = if (@hasDecl(std.Build, "CreateModuleOptions"))
114
- null
115
- else
116
- b.addModule("pkmn", .{
117
- .root_source_file = if (has_path)
118
- .{ .path = "src/lib/pkmn.zig" }
119
- else
120
- b.path("src/lib/pkmn.zig"),
121
- .optimize = optimize,
122
- .target = target,
123
- .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
124
- });
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
+ });
125
96
 
126
97
  var c = false;
127
98
  if (node_headers) |headers| {
128
- const addon = b.fmt("{s}.node", .{name});
129
- const path = if (has_path) .{ .path = "src/lib/node.zig" } else b.path("src/lib/node.zig");
130
- const lib = b.addSharedLibrary(if (force_pic) .{
131
- .name = addon,
132
- .root_source_file = path,
133
- .optimize = optimize,
99
+ const translate_c = b.addTranslateC(.{
100
+ .root_source_file = b.path("src/lib/napi.h"),
134
101
  .target = target,
135
- } else .{
136
- .name = addon,
137
- .root_source_file = path,
138
102
  .optimize = optimize,
139
- .target = target,
140
- .strip = strip,
141
- .pic = pic,
142
103
  });
143
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
144
- lib.addSystemIncludePath(if (has_path) .{ .path = headers } else b.path(headers));
145
- lib.linkLibC();
104
+ translate_c.addSystemIncludePath(b.path(headers));
105
+ const addon = b.fmt("{s}.node", .{name});
106
+ const path = b.path("src/lib/node.zig");
107
+ const lib = b.addLibrary(.{
108
+ .linkage = .dynamic,
109
+ .name = addon,
110
+ .root_module = b.createModule(.{
111
+ .root_source_file = path,
112
+ .optimize = optimize,
113
+ .target = target,
114
+ .strip = strip,
115
+ .pic = pic,
116
+ }),
117
+ });
118
+ lib.root_module.addOptions("build_options", options);
119
+ lib.root_module.addImport("napi", translate_c.createModule());
120
+ lib.root_module.link_libc = true;
146
121
  if (node_import_lib) |il| {
147
- lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
148
- } else if (detect(target).os.tag == .windows) {
149
- try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
122
+ lib.root_module.addObjectFile(b.path(il));
123
+ } else if (target.result.os.tag == .windows) {
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");
150
126
  std.process.exit(1);
151
127
  }
152
128
  lib.linker_allow_shlib_undefined = true;
153
129
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
154
- if (force_pic and pic orelse false) lib.force_pic = pic;
155
130
  // Always emit to build/lib because this is where the driver code expects to find it
156
131
  // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
157
132
  //
@@ -167,56 +142,43 @@ pub fn build(b: *std.Build) !void {
167
142
  } else if (demo) {
168
143
  const path = "src/tools/demo.zig";
169
144
  const n = if (showdown) "demo-showdown" else "demo";
170
- const mod = pkmn orelse module(b, .{
171
- .showdown = showdown,
172
- .log = log,
173
- .chance = chance,
174
- .calc = calc,
175
- });
145
+ const mod = pkmn;
176
146
  try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
177
147
  } else if (dynamic) {
178
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
179
- const lib = b.addSharedLibrary(if (force_pic) .{
180
- .name = name,
181
- .root_source_file = path,
182
- .version = try std.SemanticVersion.parse(version),
183
- .optimize = optimize,
184
- .target = target,
185
- } else .{
148
+ const path = b.path("src/lib/c.zig");
149
+ const lib = b.addLibrary(.{
150
+ .linkage = .dynamic,
186
151
  .name = name,
187
- .root_source_file = path,
188
- .version = try std.SemanticVersion.parse(version),
189
- .optimize = optimize,
190
- .target = target,
191
- .strip = strip,
192
- .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
+ }),
193
159
  });
194
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
195
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
160
+ lib.root_module.addOptions("build_options", options);
161
+ lib.root_module.addIncludePath(b.path("src/include"));
196
162
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
197
- if (force_pic and pic orelse false) lib.force_pic = pic;
198
163
  b.installArtifact(lib);
199
164
  c = true;
200
165
  } else {
201
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
202
- const lib = b.addStaticLibrary(if (force_pic) .{
203
- .name = name,
204
- .root_source_file = path,
205
- .optimize = optimize,
206
- .target = target,
207
- } else .{
166
+ const path = b.path("src/lib/c.zig");
167
+ const lib = b.addLibrary(.{
168
+ .linkage = .static,
208
169
  .name = name,
209
- .root_source_file = path,
210
- .optimize = optimize,
211
- .target = target,
212
- .strip = strip,
213
- .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
+ }),
214
177
  });
215
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
216
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
178
+ lib.root_module.addOptions("build_options", options);
179
+ lib.root_module.addIncludePath(b.path("src/include"));
217
180
  lib.bundle_compiler_rt = true;
218
181
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
219
- if (force_pic and pic orelse false) lib.force_pic = pic;
220
182
  if (emit_asm) {
221
183
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
222
184
  lib.getEmittedAsm(),
@@ -236,22 +198,25 @@ pub fn build(b: *std.Build) !void {
236
198
  }
237
199
 
238
200
  if (c) {
239
- const path =
240
- if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
201
+ const path = b.path("src/include/pkmn.h");
241
202
  const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
242
203
  b.getInstallStep().dependOn(&header.step);
243
204
 
244
205
  const pc = b.fmt("lib{s}.pc", .{name});
245
- const file = try std.fs.path.relative(
206
+ const cwd = try std.process.currentPathAlloc(b.graph.io, b.allocator);
207
+ const file = try std.Io.Dir.path.relative(
246
208
  b.allocator,
247
- try std.process.getCwdAlloc(b.allocator),
209
+ cwd,
210
+ &b.graph.environ_map,
211
+ cwd,
248
212
  try b.cache_root.join(b.allocator, &.{pc}),
249
213
  );
250
- const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
214
+ const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
215
+ defer pkgconfig_file.close(b.graph.io);
251
216
 
252
217
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
253
- const writer = pkgconfig_file.writer();
254
- try writer.print(
218
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
219
+ try writer.interface.print(
255
220
  \\prefix={0s}/{1s}
256
221
  \\includedir=${{prefix}}/include
257
222
  \\libdir=${{prefix}}/lib
@@ -263,7 +228,6 @@ pub fn build(b: *std.Build) !void {
263
228
  \\Cflags: -I${{includedir}}
264
229
  \\Libs: -L${{libdir}} -l{2s}
265
230
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
266
- defer pkgconfig_file.close();
267
231
 
268
232
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
269
233
  }
@@ -279,7 +243,7 @@ pub fn build(b: *std.Build) !void {
279
243
  // TODO: tests can be run multiple times due to @imports
280
244
  const tests = TestStep.create(b, options, config);
281
245
 
282
- var exes = std.ArrayList(*Step.Compile).init(b.allocator);
246
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
283
247
  const tools: ToolConfig = .{
284
248
  .options = .{
285
249
  .showdown = showdown,
@@ -337,7 +301,6 @@ fn buildWasm(
337
301
  import: ?*std.Build.Module,
338
302
  options: anytype,
339
303
  ) !void {
340
- const entry = @hasDecl(Step.Compile, "Entry");
341
304
  const mode = switch (optimize) {
342
305
  .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
343
306
  else => optimize,
@@ -361,92 +324,58 @@ fn buildWasm(
361
324
  if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
362
325
  features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
363
326
  }
364
- const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
365
- b.resolveTargetQuery(.{
366
- .cpu_arch = .wasm32,
367
- .os_tag = .freestanding,
368
- .cpu_features_add = features,
369
- })
370
- else
371
- ResolvedTarget{
372
- .cpu_arch = .wasm32,
373
- .os_tag = .freestanding,
374
- .cpu_features_add = features,
375
- };
376
- const path = if (has_path) .{ .path = root_src_file } else b.path(root_src_file);
377
- const bin = if (entry) bin: {
378
- const exe = b.addExecutable(if (force_pic) .{
379
- .name = name,
380
- .root_source_file = path,
381
- .optimize = mode,
382
- .target = freestanding,
383
- } else .{
384
- .name = name,
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(.{
385
336
  .root_source_file = path,
386
337
  .optimize = mode,
387
338
  .target = freestanding,
388
339
  .strip = strip,
389
340
  .pic = pic,
390
- });
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;
391
350
 
392
- var file = try std.fs.cwd().openFile(root_src_file, .{});
393
- defer file.close();
394
- const bytes = try file.readToEndAlloc(b.allocator, std.math.maxInt(usize));
395
- (if (root_module) exe.root_module else exe).export_symbol_names = try exports(b, bytes);
396
- exe.entry = .disabled;
397
- break :bin exe;
398
- } else bin: {
399
- const lib = b.addSharedLibrary(.{
400
- .name = name,
401
- .root_source_file = path,
402
- .optimize = mode,
403
- .target = freestanding,
404
- });
405
- lib.rdynamic = true;
406
- break :bin lib;
407
- };
408
351
  if (import) |i| {
409
- if (root_module) {
410
- bin.root_module.addImport("pkmn", i);
411
- } else {
412
- bin.addModule("pkmn", i);
413
- }
352
+ exe.root_module.addImport("pkmn", i);
414
353
  }
415
- bin.stack_size = wasm_stack_size;
416
- if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
417
- if (force_pic and pic orelse false) bin.force_pic = pic;
418
- (if (root_module) bin.root_module else bin).addOptions("build_options", options);
354
+ exe.stack_size = wasm_stack_size;
355
+ exe.root_module.addOptions("build_options", options);
419
356
 
420
357
  const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
421
358
  if (optimize != .Debug and opt != null) {
422
359
  const out = b.fmt("build/lib/{s}.wasm", .{name});
423
360
  const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
424
- sh.addArtifactArg(bin);
361
+ sh.addArtifactArg(exe);
425
362
  sh.addArg("-o");
426
- if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
427
- sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
428
- } else {
429
- sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
430
- }
363
+ sh.addFileArg(b.path(out));
431
364
  b.getInstallStep().dependOn(&sh.step);
432
- if (!entry) b.installArtifact(bin);
433
- } else if (entry) {
434
- b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
365
+ } else {
366
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
435
367
  .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
436
368
  }).step);
437
- } else {
438
- b.installArtifact(bin);
439
369
  }
440
370
  }
441
371
 
442
372
  fn maybeStrip(
443
373
  b: *std.Build,
444
- artifact: *Step.Compile,
374
+ artifact: *std.Build.Step.Compile,
445
375
  step: *std.Build.Step,
446
376
  strip: ?bool,
447
377
  cmd: ?[]const u8,
448
378
  ) void {
449
- if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
450
379
  if (!(strip orelse false) or cmd == null) return;
451
380
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
452
381
  const mac = builtin.os.tag == .macos;
@@ -459,7 +388,7 @@ fn maybeStrip(
459
388
  }
460
389
 
461
390
  const Config = struct {
462
- target: ResolvedTarget,
391
+ target: std.Build.ResolvedTarget,
463
392
  optimize: std.builtin.OptimizeMode,
464
393
  pic: ?bool,
465
394
  strip: ?bool,
@@ -470,7 +399,7 @@ const TestStep = struct {
470
399
  step: std.Build.Step,
471
400
  build: bool,
472
401
 
473
- pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
402
+ pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
474
403
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
475
404
  const test_filter =
476
405
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -479,28 +408,21 @@ const TestStep = struct {
479
408
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
480
409
  self.* = .{ .step = step, .build = test_filter == null };
481
410
 
482
- const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
483
- const tests = b.addTest(if (force_pic) .{
484
- .root_source_file = path,
485
- .optimize = config.optimize,
486
- .target = config.target,
487
- .filter = test_filter,
488
- } else .{
489
- .root_source_file = path,
490
- .optimize = config.optimize,
491
- .target = config.target,
492
- .filter = test_filter,
493
- .single_threaded = true,
494
- .strip = config.strip,
495
- .pic = config.pic,
411
+ const path = b.path("src/lib/test.zig");
412
+ const tests = b.addTest(.{
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,
420
+ }),
421
+ .filters = if (test_filter) |filter| &.{filter} else &.{},
496
422
  });
497
- (if (root_module) tests.root_module else tests).addOptions("build_options", options);
423
+ tests.root_module.addOptions("build_options", options);
498
424
 
499
425
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
500
- if (force_pic) {
501
- tests.single_threaded = true;
502
- if (config.pic orelse false) tests.force_pic = config.pic;
503
- }
504
426
  if (coverage) |c| {
505
427
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
506
428
  }
@@ -517,45 +439,36 @@ const ToolConfig = struct {
517
439
  tool: struct {
518
440
  tests: ?*TestStep,
519
441
  name: ?[]const u8 = null,
520
- exes: *std.ArrayList(*Step.Compile),
442
+ exes: *ArrayList(*std.Build.Step.Compile),
521
443
  },
522
444
  };
523
445
 
524
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
525
- if (!try exists(path)) return null;
446
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
447
+ if (!try exists(b, path)) return null;
526
448
  var name = config.tool.name orelse std.fs.path.basename(path);
527
449
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
528
450
  if (index) |i| name = name[0..i];
529
451
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
530
452
 
531
- const exe = b.addExecutable(if (force_pic) .{
532
- .name = name,
533
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
534
- .target = config.general.target,
535
- .optimize = config.general.optimize,
536
- } else .{
453
+ const exe = b.addExecutable(.{
537
454
  .name = name,
538
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
539
- .target = config.general.target,
540
- .optimize = config.general.optimize,
541
- .single_threaded = true,
542
- .strip = config.general.strip,
543
- .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
+ }),
544
462
  });
545
463
 
546
- if (root_module) {
547
- const import = if (config.module) |m| m else module(b, config.options);
548
- exe.root_module.addImport("pkmn", import);
549
- } else {
550
- exe.addModule("pkmn", module(b, config.options));
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", .{});
551
468
  }
552
469
 
553
- if (force_pic) {
554
- exe.single_threaded = true;
555
- if (config.general.pic orelse false) exe.force_pic = config.general.pic;
556
- }
557
470
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
558
- config.tool.exes.append(exe) catch @panic("OOM");
471
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
559
472
 
560
473
  const run = b.addRunArtifact(exe);
561
474
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -564,8 +477,8 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
564
477
  return run;
565
478
  }
566
479
 
567
- fn exists(path: []const u8) !bool {
568
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
480
+ fn exists(b: *std.Build, path: []const u8) !bool {
481
+ std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
569
482
  error.FileNotFound => return false,
570
483
  else => |e| return e,
571
484
  };
@@ -575,7 +488,7 @@ fn exists(path: []const u8) !bool {
575
488
  const ToolsStep = struct {
576
489
  step: std.Build.Step,
577
490
 
578
- pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
491
+ pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
579
492
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
580
493
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
581
494
  self.* = .{ .step = step };
@@ -584,23 +497,18 @@ const ToolsStep = struct {
584
497
  }
585
498
  };
586
499
 
587
- fn detect(target: anytype) std.Target {
588
- if (@hasField(@TypeOf(target), "result")) return target.result;
589
- return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
590
- }
591
-
592
500
  pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
593
- var symbols = std.ArrayList([]const u8).init(b.allocator);
501
+ var symbols: ArrayList([]const u8) = .empty;
594
502
 
595
503
  var it = std.mem.splitSequence(u8, bytes, "export ");
596
504
  _ = it.next();
597
505
  while (it.next()) |s| {
598
506
  if (std.mem.startsWith(u8, s, "const ")) {
599
507
  const i = std.mem.indexOf(u8, s[6..], " ").?;
600
- try symbols.append(s[6 .. 6 + i]);
508
+ try symbols.append(b.allocator, s[6 .. 6 + i]);
601
509
  } else { // "fn "
602
510
  const i = std.mem.indexOf(u8, s[3..], "(").?;
603
- try symbols.append(s[3 .. 3 + i]);
511
+ try symbols.append(b.allocator, s[3 .. 3 + i]);
604
512
  }
605
513
  }
606
514