Last active
May 2, 2024 23:38
-
-
Save radda-ui/070d360f0a0688b63a2402efd06ea81c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function newFile(filename, content) | |
-- Open the file in write mode, create if it doesn't exist, clear existing content | |
-- Extract directory path from filename | |
local directoryPath, fileName = filename:match("(.-)([^\\/]-%.?([^%.\\/]*))$") | |
-- Create directory if it doesn't exist | |
if directoryPath ~= "" and not love.filesystem.getInfo(directoryPath) then | |
local success, errorMsg = os.execute("mkdir \"" .. directoryPath .. "\"") | |
if not success then | |
print("Error creating directory: " .. errorMsg) | |
return | |
end | |
end | |
-- Open the file in write mode, create if it doesn't exist, clear existing content | |
local file = io.open(filename, "w") | |
if not file then | |
--print("Error: Unable to open file for writing") | |
return | |
end | |
-- Write content to the file | |
file:write(content) | |
-- Close the file handle | |
file:close() | |
-- print("File '" .. filename .. "' has been written successfully.") | |
end | |
-- Function to save love.window properties to a Lua file | |
function save(path, tbl) | |
local props = {} | |
props.width, props.height, props.flags = love.window.getMode() | |
props.x, props.y = love.window.getPosition() | |
--print(path) | |
local file = love.filesystem.newFile(path) | |
if tbl and type(tbl) == "table" then table.join(props, tbl)() end | |
file:open("w") | |
file:write(table.to_string(props)) | |
file:close() | |
end | |
-- Function to load a data from a Lua file | |
-- almost require but no need for it to be *.lua file | |
function loadData(file) | |
local data = love.filesystem.load(file)() | |
if type(data) == "table" then | |
return data | |
else return false end | |
end | |
--- edited fron http://lua-users.org/wiki/ | |
function table.to_string(data) | |
local serializedData = "" | |
-- Function to serialize a table | |
local function serializeTable(tbl) | |
local str = "{\n" | |
for k, v in pairs(tbl) do | |
if type(k) == "number" then | |
str = str .. "[" .. k .. "]=" | |
elseif type(k) == "string" then | |
str = str .. k .. "=" | |
else | |
-- Ignore non-string and non-numeric keys | |
-- print("Ignoring key of type " .. type(k)) | |
end | |
if type(v) == "table" then | |
str = str .. serializeTable(v) | |
elseif type(v) == "number" then | |
str = str .. v | |
elseif type(v) == "string" then | |
str = str .. string.format("%q", v) | |
elseif type(v) == "boolean" then | |
v = v and " true" or " false" | |
str = str .. v | |
elseif type(v) == "function" then | |
str = str .. "nil -- [[Function]]" | |
elseif type(v) == "userdata" then | |
str = str .. "nil -- [[userdata]]" | |
else | |
str = str .. "nil --[[unkown data type]]" | |
-- Ignore non-serializable types | |
-- print("Ignoring value of type " .. type(v)) | |
end | |
str = str .. ",\n" | |
end | |
str = str .. "}" | |
return str | |
end | |
local serializedData = "" | |
-- Serialize data | |
if type(data) == "table" then | |
serializedData = serializeTable(data) | |
elseif type(data) == "number" then | |
serializedData = tostring(data) | |
elseif type(data) == "string" then | |
serializedData = string.format("%q", data) | |
else | |
print("Unable to serialize" .. type(data)) | |
end | |
return "return " .. serializedData | |
end | |
---------------------------------------------------------------------------------------------------- | |
save love window props and restore them | |
like window positions, size title | |
---------------------------------------------------------------------------------------------------- | |
--- in main.lua | |
function love.conf(t) | |
t.window = false | |
end | |
function love.load() | |
love.restore() | |
end | |
-- as the name suggest it restore love setting | |
-- spercifecly the love.window props so that when love is opened | |
-- it will retain it position size mode you name the file props.lua is saved in | |
-- data folder %APPDATA%/romming/love/{game_folder} | |
-- to set {game_folder} just rename it in the conf.lua | |
-- t.identity = "xgame" or any other , | |
-- loaddata in just a fuction that execute a table look in : | |
-- ./libs/utils | |
function love.restore() | |
local props = loadData("props.lua") | |
if props then | |
if props.width and props.height and props.flags then | |
love.window.setMode(props.width, props.height, props.flags) | |
end | |
if props.x and props.y then | |
love.window.setPosition(props.x, props.y) | |
end | |
end | |
end | |
function love.quit() | |
save("props.lua") | |
love.event.quit() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment