hiscore: only you can prevent namespace pollution (nw)

This commit is contained in:
cracyc 2016-03-29 07:42:29 -05:00
parent 8e41d3105b
commit 21390cd790

View File

@ -5,7 +5,7 @@
-- high-score saving with hiscore.dat infom just as older -- high-score saving with hiscore.dat infom just as older
-- builds did in the past. -- builds did in the past.
-- --
require('lfs')
local exports = {} local exports = {}
exports.name = "hiscore" exports.name = "hiscore"
exports.version = "1.0.0" exports.version = "1.0.0"
@ -22,19 +22,19 @@ end
function hiscore.startplugin() function hiscore.startplugin()
hiscoredata_path = "hiscore.dat"; local hiscoredata_path = "hiscore.dat";
hiscore_path = "hi"; local hiscore_path = "hi";
current_checksum = 0; local current_checksum = 0;
default_checksum = 0; local default_checksum = 0;
scores_have_been_read = false; local scores_have_been_read = false;
mem_check_passed = false; local mem_check_passed = false;
current_game = ""; local found_hiscore_entry = false;
positions = {}; local positions = {};
function parse_table ( dsting ) local function parse_table ( dsting )
local _table = {}; local _table = {};
for line in string.gmatch(dsting, '([^\n]+)') do for line in string.gmatch(dsting, '([^\n]+)') do
local cpu, mem; local cpu, mem;
@ -59,8 +59,9 @@ function hiscore.startplugin()
end end
function read_hiscore_dat () local function read_hiscore_dat ()
file = io.open( hiscoredata_path, "r" ); local file = io.open( hiscoredata_path, "r" );
local rm_match;
if not file then if not file then
file = io.open( hiscore_plugin_path .. "/hiscore.dat", "r" ); file = io.open( hiscore_plugin_path .. "/hiscore.dat", "r" );
end end
@ -69,8 +70,8 @@ function hiscore.startplugin()
else else
rm_match = '^' .. emu.romname() .. ':'; rm_match = '^' .. emu.romname() .. ':';
end end
cluster = ""; local cluster = "";
current_is_match = false; local current_is_match = false;
if file then if file then
repeat repeat
line = file:read("*l"); line = file:read("*l");
@ -99,7 +100,7 @@ function hiscore.startplugin()
end end
function check_mem ( posdata ) local function check_mem ( posdata )
if #posdata < 1 then if #posdata < 1 then
return false; return false;
end end
@ -116,7 +117,8 @@ function hiscore.startplugin()
end end
function get_file_name () local function get_file_name ()
local r;
if emu.softname() ~= "" then if emu.softname() ~= "" then
r = hiscore_path .. '/' .. emu.romname() .. "_" .. emu.softname() .. ".hi"; r = hiscore_path .. '/' .. emu.romname() .. "_" .. emu.softname() .. ".hi";
else else
@ -126,12 +128,12 @@ function hiscore.startplugin()
end end
function write_scores ( posdata ) local function write_scores ( posdata )
print("write_scores") print("write_scores")
local output = io.open(get_file_name(), "wb"); local output = io.open(get_file_name(), "wb");
if not output then if not output then
-- attempt to create the directory, and try again -- attempt to create the directory, and try again
os.execute( "mkdir " .. hiscore_path ); lfs.mkdir( hiscore_path );
output = io.open(get_file_name(), "wb"); output = io.open(get_file_name(), "wb");
end end
print("write_scores output") print("write_scores output")
@ -149,7 +151,7 @@ function hiscore.startplugin()
end end
function read_scores ( posdata ) local function read_scores ( posdata )
local input = io.open(get_file_name(), "rb"); local input = io.open(get_file_name(), "rb");
if input then if input then
for ri,row in ipairs(posdata) do for ri,row in ipairs(posdata) do
@ -166,12 +168,12 @@ function hiscore.startplugin()
end end
function check_scores ( posdata ) local function check_scores ( posdata )
local r = 0; local r = 0;
-- commonly the first entry will be for the entire table -- commonly the first entry will be for the entire table
-- so it will only trigger a write once a player enters -- so it will only trigger a write once a player enters
-- his/her name in. -- his/her name in.
row = positions[1]; local row = positions[1];
for i=0,row["size"]-1 do for i=0,row["size"]-1 do
r = r + row["mem"]:read_u8( row["addr"] + i ); r = r + row["mem"]:read_u8( row["addr"] + i );
end end
@ -179,7 +181,7 @@ function hiscore.startplugin()
end end
function init () local function init ()
if not scores_have_been_read then if not scores_have_been_read then
if check_mem( positions ) then if check_mem( positions ) then
default_checksum = check_scores( positions ); default_checksum = check_scores( positions );
@ -201,7 +203,7 @@ function hiscore.startplugin()
local last_write_time = -10; local last_write_time = -10;
function tick () local function tick ()
-- set up scores if they have been -- set up scores if they have been
init(); init();
-- only allow save check to run when -- only allow save check to run when
@ -225,27 +227,26 @@ function hiscore.startplugin()
end end
end end
function reset() local function reset()
-- the notifier will still be attached even if the running game has no hiscore.dat entry -- the notifier will still be attached even if the running game has no hiscore.dat entry
if mem_check_passed and (emu.romname() == current_game) then if mem_check_passed and found_hiscore_entry then
local checksum = check_scores(positions) local checksum = check_scores(positions)
if checksum ~= current_checksum and checksum ~= default_checksum then if checksum ~= current_checksum and checksum ~= default_checksum then
write_scores(positions) write_scores(positions)
end end
end end
current_game = "" found_hiscore_entry = false
mem_check_passed = false mem_check_passed = false
scores_have_been_read = false; scores_have_been_read = false;
end end
emu.register_start(function() emu.register_start(function()
current_game = "" found_hiscore_entry = false
mem_check_passed = false mem_check_passed = false
scores_have_been_read = false; scores_have_been_read = false;
last_write_time = -10 last_write_time = -10
print("Starting " .. emu.gamename()) print("Starting " .. emu.gamename())
current_game = emu.romname() local dat = read_hiscore_dat()
dat = read_hiscore_dat()
if dat and dat ~= "" then if dat and dat ~= "" then
print( "found hiscore.dat entry for " .. emu.romname() ); print( "found hiscore.dat entry for " .. emu.romname() );
positions = parse_table( dat ); positions = parse_table( dat );
@ -253,11 +254,14 @@ function hiscore.startplugin()
print("hiscore.dat parse error"); print("hiscore.dat parse error");
return; return;
end end
found_hiscore_entry = true
end
end)
emu.register_frame(function()
if found_hiscore_entry then
tick()
end end
emu.sethook( tick, "frame" );
end) end)
emu.register_stop(function() emu.register_stop(function()
reset() reset()
end) end)