mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Move player position parsing to shared util, fix player ID persistence
This commit is contained in:
1 parent
a36e7bddca
commit
093b71045b
6 files changed
+406
-141
No files matched your search
@@ -0,0 +1,41 @@
|
||||
// Environment detection
|
||||
export const isNode = typeof Buffer !== "undefined" && typeof process !== "undefined" && process.versions?.node;
|
||||
|
||||
// Cross-platform type for binary data
|
||||
export type BinaryData = Buffer | Uint8Array;
|
||||
|
||||
// Cross-platform helper functions for reading integers
|
||||
export function readUInt8(data: BinaryData, offset: number): number {
|
||||
if (isNode && data instanceof Buffer) {
|
||||
return data.readUInt8(offset);
|
||||
}
|
||||
return (data as Uint8Array)[offset];
|
||||
}
|
||||
|
||||
export function readUInt16LE(data: BinaryData, offset: number): number {
|
||||
if (isNode && data instanceof Buffer) {
|
||||
return data.readUInt16LE(offset);
|
||||
}
|
||||
const bytes = data as Uint8Array;
|
||||
return bytes[offset] | (bytes[offset + 1] << 8);
|
||||
}
|
||||
|
||||
export function readUInt16BE(data: BinaryData, offset: number): number {
|
||||
if (isNode && data instanceof Buffer) {
|
||||
return data.readUInt16BE(offset);
|
||||
}
|
||||
const bytes = data as Uint8Array;
|
||||
return (bytes[offset] << 8) | bytes[offset + 1];
|
||||
}
|
||||
|
||||
export function readUInt32BE(data: BinaryData, offset: number): number {
|
||||
if (isNode && data instanceof Buffer) {
|
||||
return data.readUInt32BE(offset);
|
||||
}
|
||||
const bytes = data as Uint8Array;
|
||||
return (bytes[offset] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3];
|
||||
}
|
||||
|
||||
export function sliceData(data: BinaryData, start: number, end: number): BinaryData {
|
||||
return data.slice(start, end);
|
||||
}
|
||||
Reference in new issue
Block a user