sonic-radiance/sonic-radiance.love/birb/utils/datas.lua

106 lines
2.9 KiB
Lua
Raw Normal View History

-- loveutils.datas : simple functions for data manipulation.
--[[
Copyright © 2021 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
2021-04-03 10:39:34 +02:00
local DataUtils = {}
2020-08-04 16:55:09 +02:00
local DATADIR = "datas"
local GAMEDATADIR = "gamedata"
function DataUtils.isLuaFile(name)
local extension = name:sub(#name - 3, #name)
2020-08-04 16:55:09 +02:00
return (extension == ".lua")
end
function DataUtils.luaFileToModule(luaFile)
if (DataUtils.isLuaFile(luaFile)) then
2020-08-04 16:55:09 +02:00
return luaFile:sub(1, #luaFile - 4)
else
return luaFile
end
end
function DataUtils.luaFileListToModuleList(luaFileList)
local moduleList = {}
for i,luaFile in ipairs(luaFileList) do
table.insert(moduleList, DataUtils.luaFileToModule(luaFile))
end
return moduleList
end
function DataUtils.require(parent, filename)
return require(DataUtils.concatFolder(parent, filename, true))
end
function DataUtils.copy(filepath)
local orig = require(filepath)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function DataUtils.copyDataset(parent, filename)
local orig = DataUtils.require(parent, filename)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
2020-08-04 16:55:09 +02:00
function DataUtils.concatDataFolder(folder, lua)
return DataUtils.concatFolder(DataUtils.getGameDataDir(lua), folder, lua)
end
function DataUtils.getGameDataDir(lua)
return DataUtils.concatFolder(DATADIR, GAMEDATADIR, lua)
end
function DataUtils.concatFolder(parent, filename, lua)
return parent .. DataUtils.sep(lua) .. filename
end
function DataUtils.sep(lua)
if (lua) then
return "."
else
return "/"
end
end
return DataUtils