-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdump.zig
174 lines (162 loc) · 6.8 KB
/
dump.zig
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
const pkmn = @import("pkmn");
const std = @import("std");
const protocol = pkmn.protocol;
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
pub const pkmn_options = pkmn.Options{ .internal = true };
const Tool = enum {
markdown,
protocol,
layout,
};
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len != 2) usageAndExit(args[0]);
var tool: Tool = undefined;
if (std.mem.eql(u8, args[1], "markdown")) {
tool = .markdown;
} else if (std.mem.eql(u8, args[1], "protocol")) {
tool = .protocol;
} else if (std.mem.eql(u8, args[1], "layout")) {
tool = .layout;
} else {
usageAndExit(args[0]);
}
const out = std.io.getStdOut();
var buf = std.io.bufferedWriter(out.writer());
var w = buf.writer();
switch (tool) {
.markdown => {
inline for (@field(@typeInfo(protocol), @tagName(Struct)).decls) |decl| {
if (@TypeOf(@field(protocol, decl.name)) == type) {
switch (@typeInfo(@field(protocol, decl.name))) {
Enum => |e| {
try w.print(
"## {s}\n\n<details><summary>Reason</summary>\n",
.{decl.name},
);
try w.writeAll("\n|Raw|Description|\n|--|--|\n");
inline for (e.fields) |field| {
try w.print("|`0x{X:0>2}`|`{s}`|\n", .{ field.value, field.name });
}
try w.writeAll("</details>\n\n");
},
else => {},
}
}
}
},
.protocol => {
var outer = false;
try w.writeAll("{\n");
inline for (@field(@typeInfo(protocol), @tagName(Struct)).decls) |decl| {
if (@TypeOf(@field(protocol, decl.name)) == type) {
if (comptime std.mem.eql(u8, decl.name, "Kind")) continue;
switch (@typeInfo(@field(protocol, decl.name))) {
Enum => |e| {
if (outer) try w.writeAll(",\n");
try w.print(" \"{s}\": [\n", .{decl.name});
var inner = false;
inline for (e.fields) |field| {
if (inner) try w.writeAll(",\n");
// TODO: ziglang/zig#18888
@setEvalBranchQuota(2018);
try w.print(" \"{s}\"", .{field.name});
inner = true;
}
try w.writeAll("\n ]");
outer = true;
},
else => {},
}
}
}
try w.writeAll("\n}\n");
},
.layout => {
try w.writeAll("[\n");
{
try w.writeAll(" {\n \"sizes\": {\n");
{
try w.print(
" \"{s}\": {d},\n",
.{ "Battle", @sizeOf(pkmn.gen1.Battle(pkmn.gen1.PRNG)) },
);
try w.print(
" \"{s}\": {d},\n",
.{ "Side", @sizeOf(pkmn.gen1.Side) },
);
try w.print(
" \"{s}\": {d},\n",
.{ "Pokemon", @sizeOf(pkmn.gen1.Pokemon) },
);
try w.print(
" \"{s}\": {d},\n",
.{ "ActivePokemon", @sizeOf(pkmn.gen1.ActivePokemon) },
);
try w.print(
" \"{s}\": {d},\n",
.{ "Actions", @sizeOf(pkmn.gen1.chance.Actions) },
);
try w.print(
" \"{s}\": {d},\n",
.{ "Durations", @sizeOf(pkmn.gen1.chance.Durations) },
);
try w.print(
" \"{s}\": {d}\n",
.{ "Summaries", @sizeOf(pkmn.gen1.calc.Summaries) },
);
}
try w.writeAll(" },\n");
try w.writeAll(" \"offsets\": {\n");
{
try print(w, "Battle", pkmn.gen1.Battle(pkmn.gen1.PRNG), false);
try w.writeAll(",\n");
try print(w, "Side", pkmn.gen1.Side, false);
try w.writeAll(",\n");
try print(w, "Pokemon", pkmn.gen1.Pokemon, false);
try w.writeAll(",\n");
try print(w, "ActivePokemon", pkmn.gen1.ActivePokemon, false);
try w.writeAll(",\n");
try print(w, "Stats", pkmn.gen1.Stats(u16), false);
try w.writeAll(",\n");
try print(w, "Boosts", pkmn.gen1.Boosts, true);
try w.writeAll(",\n");
try print(w, "Volatiles", pkmn.gen1.Volatiles, true);
try w.writeAll(",\n");
try print(w, "Action", pkmn.gen1.chance.Action, true);
try w.writeAll(",\n");
try print(w, "Duration", pkmn.gen1.chance.Duration, true);
try w.writeAll(",\n");
try print(w, "Damage", pkmn.gen1.calc.Summary.Damage, false);
try w.writeAll("\n }\n");
}
try w.writeAll(" }\n");
}
try w.writeAll("]\n");
},
}
try buf.flush();
}
fn print(w: anytype, name: []const u8, comptime T: type, comptime bits: bool) !void {
try w.print(" \"{s}\": {{\n", .{name});
var inner = false;
inline for (@field(@typeInfo(T), @tagName(Struct)).fields) |field| {
if (field.name[0] != '_') {
if (inner) try w.writeAll(",\n");
const offset = @bitOffsetOf(T, field.name);
try w.print(" \"{s}\": {d}", .{ field.name, if (bits) offset else offset / 8 });
inner = true;
}
}
try w.writeAll("\n }");
}
fn usageAndExit(cmd: []const u8) noreturn {
const err = std.io.getStdErr().writer();
err.print("Usage: {s} <markdown|protocol|layout>\n", .{cmd}) catch {};
std.process.exit(1);
}