User File #73827041987802579

Upload All User Files

#73827041987802579 - Sync Scripts - Generate Sync Data

sync-collect.lua
190 downloads
Uploaded 8/29/2021 7:19 PM by Zinfidel (see all 12)
-- This script is for collecting "sync data" for a game that consists of some sort of canary data
-- that is very likely to indicate a desync happening. Good choices for this kind of data are transformation
-- matrices for models, or manually collected position data like x,y,z,pitch,yaw,roll,etc.

-- Change this to some amount of frames on which data will be flushed. 200 means flush to file every 200 frames.
FLUSH_FRAMES = 200

-- Check if a file exists on the filesystem
local function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

-- Create a "log.txt" file for the data, but add a number so we don't accidentally overwrite previous results.
local name = "log.txt"
local i = 1
while (file_exists(name)) do
	name = "log" .. tostring(i) .. ".txt"
	i = i + 1
end
LogFile = io.open(name, "w")

-- Flush and close the log file when the script closes.
local onExitEvent = event.onexit(
	function()
		LogFile:close()
		console.log("")
		console.log("Script stopping. Closing log file.")
	end
	, "Exit Event" );


-- Collect sync data
console.write("Flushing to file ")
while true do
	-- Custom data collection for the game goes in here. The example code below is for Star Wars Episode 1: Racer,
	-- it finds the player's position matrix and records each matrix entry as a float to 5 decimals. It writes these
	-- data to a comma-separated line.
	---------------------------------------------------------------------------------------------------------------
	Racedata = 0x118F90
	Statedata = mainmemory.read_u24_be(Racedata+0x84+0x1)

	MatrixStart = Statedata + 0x1C
	for j=0, 15, 1 do
		LogFile:write(string.format("%.5f",mainmemory.readfloat(MatrixStart + 4*j, true)))
		if (j ~= 15) then LogFile:write(',') end
	end
	---------------------------------------------------------------------------------------------------------------
	-- End custom collection code

	-- New line, and if we are at a flush boundary, flush the file.
	LogFile:write('\n')
	if (emu.framecount() % FLUSH_FRAMES) == 0 then
		LogFile:flush()
		console.write('.')
	end

	emu.frameadvance()
end