ludic/packages/ludic.telemetry/ident.ludic
Orkuncakilkaya 3ac1179440 fix(allocs): ludic.update, steps, effects and telemetry at 0 frame allocs and 0 keeps
ludic.update: whether this copy can update itself is worked out once, when the updater is configured
(the panel asked every frame, building the path to the executable each time); the notes are a list
the state keeps, filled by update_notes_for when the version or the language changes, which the
game calls from its update tick. The feed's address is declared (once, when a check starts).
ludic.steps, ludic.effects, ludic.telemetry: what is left is made on an event and declared with its
bound - an arc's tables, a chapter's columns, a step's fact, an effect's start, end and clear, the
fact pool's growth, the player id, a props record's nesting stack - and two pushes into a caller's
kept list, at most a chapter's steps and the ring's size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 20:13:06 +03:00

92 lines
4.4 KiB
Text

# ident.ludic - one player id per machine: the operating system's own id (Windows MachineGuid,
# macOS IOPlatformUUID, Linux /etc/machine-id) hashed with the game's salt, else random
# n random bytes as hex. Crypto.random_hex reads /dev/urandom, which Windows lacks (zeros there),
# so it is mixed with the clock and the process and hashed: distinct per start everywhere
@alloc_ok("the player id: once a start, and never again once written")
export function telemetry_random_hex(telemetry_st: mut TelemetryState, n: int) -> string {
telemetry_st.telemetry__rand_n += 1
let seed = Crypto.random_hex(n) + "|" + `{Time.now()}|{Crypto.random_u32()}|{Os.pid()}|{telemetry_st.telemetry__rand_n}`
return Crypto.sha256(seed)[0..n * 2]
}
# the id outright (a game with accounts of its own, a test); written to the id file
export function telemetry_set_id(telemetry_st: mut TelemetryState, id: string) -> void {
telemetry_st.telemetry__id = id
let path = telemetry__conf(telemetry_st).id_file
if len(path) > 0 {
var v = telemetry__read_obj(path)
if v == null { v = Value.object() }
Value.put(v, "id", Value.str(id))
Json.write_file(v, path)
Json.free(v)
}
telemetry__fact(telemetry_st, TELEMETRY_ID, 0, 0, 0)
}
@alloc_ok("the player id: once a start")
function telemetry__random_id(telemetry_st: mut TelemetryState) -> void { telemetry_set_id(telemetry_st, "r-" + telemetry_random_hex(telemetry_st, 16)) }
@alloc_ok("the player id: once a start")
function telemetry__from_machine(telemetry_st: mut TelemetryState, raw: string) -> void {
let salt = telemetry__conf(telemetry_st).id_salt
telemetry_set_id(telemetry_st, "m-" + Crypto.sha256(salt + ":" + Text.lower(raw))[0..32])
}
function telemetry__id_begin(telemetry_st: mut TelemetryState) -> void {
if len(telemetry__conf(telemetry_st).id_salt) == 0 {
telemetry__random_id(telemetry_st)
return
}
let plat = Os.platform()
if plat == "linux" {
let m = Fs.read_text("/etc/machine-id")
if m != null and len(Text.trim(m)) > 0 { telemetry__from_machine(telemetry_st, Text.trim(m)) } else { telemetry__random_id(telemetry_st) }
return
}
telemetry_st.telemetry__proc_out = telemetry__conf(telemetry_st).id_scratch
if len(telemetry_st.telemetry__proc_out) == 0 { telemetry_st.telemetry__proc_out = Os.temp_dir() + "/ludic-telemetry-machine.txt" }
if Fs.exists(telemetry_st.telemetry__proc_out) { Fs.remove(telemetry_st.telemetry__proc_out) }
telemetry_st.telemetry__proc = telemetry__spawn_machine(telemetry_st, plat)
telemetry_st.telemetry__proc_t0 = Time.now()
if telemetry_st.telemetry__proc < 0 { telemetry__random_id(telemetry_st) }
}
function telemetry__spawn_machine(telemetry_st: TelemetryState, plat: string) -> int {
let args = new []string
if plat == "windows" {
var root = "C:\\Windows"
if Os.has_env("SystemRoot") and len(Os.env("SystemRoot")) > 0 { root = Os.env("SystemRoot") }
let out = "'" + Text.replace(telemetry_st.telemetry__proc_out, "'", "''") + "'"
push(args, "-NoProfile")
push(args, "-NonInteractive")
push(args, "-Command")
push(args, "(Get-ItemProperty -LiteralPath 'HKLM:\\SOFTWARE\\Microsoft\\Cryptography').MachineGuid | Out-File -Encoding ascii -LiteralPath " + out)
return Process.spawn(root + "\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", args)
}
push(args, "-c")
push(args, "ioreg -rd1 -c IOPlatformExpertDevice | awk -F'\"' '/IOPlatformUUID/{print $4}' > '" + telemetry_st.telemetry__proc_out + "'")
return Process.spawn("/bin/sh", args)
}
# the machine id, once the child has written it (ten seconds at most)
function telemetry__id_tick(telemetry_st: mut TelemetryState) -> void {
if telemetry_st.telemetry__proc < 0 { return }
let code = Process.poll(telemetry_st.telemetry__proc)
if code < 0 {
if Time.now() - telemetry_st.telemetry__proc_t0 > 10 {
Process.kill(telemetry_st.telemetry__proc)
Process.free(telemetry_st.telemetry__proc)
telemetry_st.telemetry__proc = -1
telemetry__random_id(telemetry_st)
}
return
}
Process.free(telemetry_st.telemetry__proc)
telemetry_st.telemetry__proc = -1
var raw = ""
let s = Fs.read_text(telemetry_st.telemetry__proc_out)
if s != null { raw = Text.trim(s) }
if Fs.exists(telemetry_st.telemetry__proc_out) { Fs.remove(telemetry_st.telemetry__proc_out) }
if code == 0 and len(raw) >= 8 { telemetry__from_machine(telemetry_st, raw) } else { telemetry__random_id(telemetry_st) }
}