2023-07-05 13:34:05 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
const xplane_proto = @import("//tsl:xplane_proto");
|
|
|
|
|
const xplane_schema = @import("xplane_schema.zig");
|
|
|
|
|
|
|
|
|
|
pub const XPlaneVisitor = struct {
|
|
|
|
|
plane: *const xplane_proto.XPlane,
|
|
|
|
|
event_metadata_by_id: std.AutoHashMapUnmanaged(i64, *const xplane_proto.XEventMetadata) = .{},
|
|
|
|
|
stat_metadata_by_id: std.AutoHashMapUnmanaged(i64, *const xplane_proto.XStatMetadata) = .{},
|
|
|
|
|
|
|
|
|
|
pub fn init(
|
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
|
plane: *const xplane_proto.XPlane,
|
|
|
|
|
) !XPlaneVisitor {
|
|
|
|
|
var res: XPlaneVisitor = .{ .plane = plane };
|
|
|
|
|
|
2023-12-20 17:18:02 +00:00
|
|
|
try res.event_metadata_by_id.ensureUnusedCapacity(allocator, @intCast(plane.event_metadata.items.len));
|
2023-07-05 13:34:05 +00:00
|
|
|
// build event metadata map
|
|
|
|
|
for (plane.event_metadata.items) |*event_metadata| {
|
2023-12-20 17:18:02 +00:00
|
|
|
res.event_metadata_by_id.putAssumeCapacity(event_metadata.key, &event_metadata.value.?);
|
2023-07-05 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build stat metadata map
|
2023-12-20 17:18:02 +00:00
|
|
|
try res.stat_metadata_by_id.ensureUnusedCapacity(allocator, @intCast(plane.stat_metadata.items.len));
|
2023-07-05 13:34:05 +00:00
|
|
|
for (plane.stat_metadata.items) |*stat_metadata| {
|
2023-12-20 17:18:02 +00:00
|
|
|
res.stat_metadata_by_id.putAssumeCapacity(stat_metadata.key, &stat_metadata.value.?);
|
2023-07-05 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 17:18:02 +00:00
|
|
|
pub fn deinit(self: *XPlaneVisitor, allocator: std.mem.Allocator) void {
|
|
|
|
|
self.stat_metadata_by_id.deinit(allocator);
|
|
|
|
|
self.event_metadata_by_id.deinit(allocator);
|
2023-07-05 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-20 17:18:02 +00:00
|
|
|
pub fn name(self: XPlaneVisitor) []const u8 {
|
2023-07-05 13:34:05 +00:00
|
|
|
return self.plane.name.getSlice();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 17:18:02 +00:00
|
|
|
pub fn getEventType(self: XPlaneVisitor, event_metadata_id: i64) xplane_schema.HostEventType {
|
|
|
|
|
if (self.event_metadata_by_id.get(event_metadata_id)) |v| {
|
|
|
|
|
return xplane_schema.HostEventType.fromString(v.name.getSlice());
|
|
|
|
|
} else return .unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn getStatMetadataName(self: XPlaneVisitor, stat_metadata_id: i64) []const u8 {
|
2023-07-05 13:34:05 +00:00
|
|
|
if (self.stat_metadata_by_id.get(stat_metadata_id)) |v| {
|
|
|
|
|
return v.name.getSlice();
|
|
|
|
|
} else return &[_]u8{};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 17:18:02 +00:00
|
|
|
pub fn getStatType(self: XPlaneVisitor, stat_metadata_id: i64) xplane_schema.StatType {
|
2023-07-05 13:34:05 +00:00
|
|
|
if (self.stat_metadata_by_id.get(stat_metadata_id)) |v| {
|
|
|
|
|
return xplane_schema.StatType.fromString(v.name.getSlice());
|
|
|
|
|
} else return .unknown;
|
|
|
|
|
}
|
2023-12-20 17:18:02 +00:00
|
|
|
|
|
|
|
|
pub fn xstatToString(self: XPlaneVisitor, stat: xplane_proto.XStat, writer: anytype) !void {
|
|
|
|
|
if (stat.value == null) return;
|
|
|
|
|
|
|
|
|
|
switch (stat.value.?) {
|
|
|
|
|
inline .int64_value, .uint64_value, .double_value => |v| try writer.print("{d}", .{v}),
|
|
|
|
|
.str_value => |*v| try writer.writeAll(v.getSlice()),
|
|
|
|
|
.bytes_value => try writer.writeAll("<opaque bytes>"),
|
|
|
|
|
.ref_value => |v| try writer.writeAll(self.getStatMetadataName(@intCast(v))),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-05 13:34:05 +00:00
|
|
|
};
|