Added `core.temp_filename()`

This commit is contained in:
rxi 2020-05-24 17:34:23 +01:00
parent 61a2a2c4e5
commit 257b9ab753
1 changed files with 26 additions and 0 deletions

View File

@ -120,8 +120,34 @@ function core.init()
end end
math.randomseed(system.get_time() * 1000)
local function uid()
return string.gsub("xxxxxx", ".", function()
local chars = "0123456789abcdefghijklmnopqrstuvwxyz"
local n = math.random(#chars)
return chars:sub(n, n)
end)
end
local temp_file_prefix = ".temp_" .. uid()
local function delete_temp_files()
for _, filename in ipairs(system.list_dir(EXEDIR)) do
if filename:find(temp_file_prefix, 1, true) == 1 then
os.remove(EXEDIR .. PATHSEP .. filename)
end
end
end
function core.temp_filename(ext)
return EXEDIR .. PATHSEP .. temp_file_prefix .. uid() .. (ext or "")
end
function core.quit(force) function core.quit(force)
if force then if force then
delete_temp_files()
os.exit() os.exit()
end end
local dirty_count = 0 local dirty_count = 0