mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 11:17:05 -04:00
Refactor ClientEntityManager.serverUpdate
to use BinaryReader
(#1171)
* Use ReaderWriter for reading * Reformat switch * Apply review suggestions * Remove redundant comptime * Remove message length check
This commit is contained in:
parent
867e908981
commit
832f5e7e08
@ -16,6 +16,8 @@ const Vec3f = vec.Vec3f;
|
|||||||
const Vec4f = vec.Vec4f;
|
const Vec4f = vec.Vec4f;
|
||||||
const NeverFailingAllocator = main.utils.NeverFailingAllocator;
|
const NeverFailingAllocator = main.utils.NeverFailingAllocator;
|
||||||
|
|
||||||
|
const BinaryReader = main.utils.BinaryReader;
|
||||||
|
|
||||||
pub const ClientEntity = struct {
|
pub const ClientEntity = struct {
|
||||||
interpolatedValues: utils.GenericInterpolation(6) = undefined,
|
interpolatedValues: utils.GenericInterpolation(6) = undefined,
|
||||||
_interpolationPos: [6]f64 = undefined,
|
_interpolationPos: [6]f64 = undefined,
|
||||||
@ -226,33 +228,29 @@ pub const ClientEntityManager = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serverUpdate(time: i16, data: []const u8) void {
|
pub fn serverUpdate(time: i16, reader: *BinaryReader) !void {
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
defer mutex.unlock();
|
defer mutex.unlock();
|
||||||
timeDifference.addDataPoint(time);
|
timeDifference.addDataPoint(time);
|
||||||
std.debug.assert(data.len%(4 + 24 + 12 + 24) == 0);
|
|
||||||
var remaining = data;
|
while(reader.remaining.len != 0) {
|
||||||
while(remaining.len != 0) {
|
const id = try reader.readInt(u32);
|
||||||
const id = std.mem.readInt(u32, remaining[0..4], .big);
|
|
||||||
remaining = remaining[4..];
|
|
||||||
const pos = [_]f64{
|
const pos = [_]f64{
|
||||||
@bitCast(std.mem.readInt(u64, remaining[0..8], .big)),
|
try reader.readFloat(f64),
|
||||||
@bitCast(std.mem.readInt(u64, remaining[8..16], .big)),
|
try reader.readFloat(f64),
|
||||||
@bitCast(std.mem.readInt(u64, remaining[16..24], .big)),
|
try reader.readFloat(f64),
|
||||||
@floatCast(@as(f32, @bitCast(std.mem.readInt(u32, remaining[24..28], .big)))),
|
@floatCast(try reader.readFloat(f32)),
|
||||||
@floatCast(@as(f32, @bitCast(std.mem.readInt(u32, remaining[28..32], .big)))),
|
@floatCast(try reader.readFloat(f32)),
|
||||||
@floatCast(@as(f32, @bitCast(std.mem.readInt(u32, remaining[32..36], .big)))),
|
@floatCast(try reader.readFloat(f32)),
|
||||||
};
|
};
|
||||||
remaining = remaining[36..];
|
|
||||||
const vel = [_]f64{
|
const vel = [_]f64{
|
||||||
@bitCast(std.mem.readInt(u64, remaining[0..8], .big)),
|
try reader.readFloat(f64),
|
||||||
@bitCast(std.mem.readInt(u64, remaining[8..16], .big)),
|
try reader.readFloat(f64),
|
||||||
@bitCast(std.mem.readInt(u64, remaining[16..24], .big)),
|
try reader.readFloat(f64),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
};
|
};
|
||||||
remaining = remaining[24..];
|
|
||||||
for(entities.items()) |*ent| {
|
for(entities.items()) |*ent| {
|
||||||
if(ent.id == id) {
|
if(ent.id == id) {
|
||||||
ent.updatePosition(&pos, &vel, time);
|
ent.updatePosition(&pos, &vel, time);
|
||||||
|
@ -881,7 +881,7 @@ pub const Protocols = struct {
|
|||||||
const typ = try reader.readInt(u8);
|
const typ = try reader.readInt(u8);
|
||||||
const time = try reader.readInt(i16);
|
const time = try reader.readInt(i16);
|
||||||
if(typ == type_entity) {
|
if(typ == type_entity) {
|
||||||
main.entity.ClientEntityManager.serverUpdate(time, reader.remaining);
|
try main.entity.ClientEntityManager.serverUpdate(time, reader);
|
||||||
} else if(typ == type_item) {
|
} else if(typ == type_item) {
|
||||||
world.itemDrops.readPosition(reader.remaining, time);
|
world.itemDrops.readPosition(reader.remaining, time);
|
||||||
}
|
}
|
||||||
|
@ -1919,6 +1919,11 @@ pub const BinaryReader = struct {
|
|||||||
return std.mem.readInt(T, self.remaining[0..bufSize], self.endian);
|
return std.mem.readInt(T, self.remaining[0..bufSize], self.endian);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn readFloat(self: *BinaryReader, T: type) error{OutOfBounds, IntOutOfBounds}!T {
|
||||||
|
const IntT = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
|
||||||
|
return @as(T, @bitCast(try self.readInt(IntT)));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn readEnum(self: *BinaryReader, T: type) error{OutOfBounds, IntOutOfBounds, InvalidEnumTag}!T {
|
pub fn readEnum(self: *BinaryReader, T: type) error{OutOfBounds, IntOutOfBounds, InvalidEnumTag}!T {
|
||||||
const int = try self.readInt(@typeInfo(T).@"enum".tag_type);
|
const int = try self.readInt(@typeInfo(T).@"enum".tag_type);
|
||||||
return std.meta.intToEnum(T, int);
|
return std.meta.intToEnum(T, int);
|
||||||
@ -1963,6 +1968,11 @@ pub const BinaryWriter = struct {
|
|||||||
std.mem.writeInt(T, self.data.addMany(bufSize)[0..bufSize], value, self.endian);
|
std.mem.writeInt(T, self.data.addMany(bufSize)[0..bufSize], value, self.endian);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn writeFloat(self: *BinaryWriter, T: type, value: T) T {
|
||||||
|
const IntT = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
|
||||||
|
self.writeInt(IntT, @bitCast(value));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn writeEnum(self: *BinaryWriter, T: type, value: T) void {
|
pub fn writeEnum(self: *BinaryWriter, T: type, value: T) void {
|
||||||
self.writeInt(@typeInfo(T).@"enum".tag_type, @intFromEnum(value));
|
self.writeInt(@typeInfo(T).@"enum".tag_type, @intFromEnum(value));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user