From 4c098df30d898531e2cd80f0cf1c0a09e867cf88 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 15 May 2021 21:24:29 +0200 Subject: [PATCH] feat: add data loading and indexing to birb --- sonic-radiance.love/birb/classes/datapack.lua | 117 ++++++++++++++++++ sonic-radiance.love/birb/core/datas.lua | 60 +++++++++ sonic-radiance.love/birb/core/init.lua | 2 + 3 files changed, 179 insertions(+) create mode 100644 sonic-radiance.love/birb/classes/datapack.lua create mode 100644 sonic-radiance.love/birb/core/datas.lua diff --git a/sonic-radiance.love/birb/classes/datapack.lua b/sonic-radiance.love/birb/classes/datapack.lua new file mode 100644 index 0000000..06abb07 --- /dev/null +++ b/sonic-radiance.love/birb/classes/datapack.lua @@ -0,0 +1,117 @@ +-- datapack.lua :: a categorized and indexed pack of data. Aim to make easy to get +-- the category from an element, without having + +--[[ + 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. +]] + +local DataPack = Object:extend() + +local UNCATEGORIZED = "uncategorized" + +function DataPack:new(foldername, categories, areFiles, subfiles) + self.foldername = foldername + self.areFiles = (areFiles ~= false) + self.subfiles = subfiles or {} + self.categories = categories or {} + --print("loading " .. foldername) + self:loadAllDatas() +end + +function DataPack:getBaseDirectory(lua) + return utils.datas.concatDataFolder(self.foldername, lua) +end + +function DataPack:getCategoryDirectory(category, lua) + if (category == UNCATEGORIZED) then + return self:getBaseDirectory(lua) + else + return utils.datas.concatFolder(self:getBaseDirectory(lua), category, lua) + end +end + +function DataPack:haveCategories() + return (#self.categories > 0) +end + +function DataPack:loadAllDatas() + self.index = {} + self.reverseIndex = {} + + if (self:haveCategories()) then + for i, category in ipairs(self.categories) do + self:loadDatas(category) + end + else + self:loadDatas(UNCATEGORIZED) + end +end + +function DataPack:loadDatas(category) + local directory = self:getCategoryDirectory(category) + local items = love.filesystem.getDirectoryItems(directory) + self.reverseIndex[category] = {} + for i, filename in ipairs(items) do + if (utils.datas.isLuaFile(filename) == self.areFiles) then + if (self.areFiles) then + filename = utils.datas.luaFileToModule(filename) + end + + if (self.index[filename] ~= nil) then + error("Data " .. filename .. " already exists for " .. self.foldername) + end + --print("loading " .. filename .. " from category " .. category .. " for " .. self.foldername) + self.index[filename] = category + table.insert(self.reverseIndex[category], filename) + end + end +end + +function DataPack:dataExists(name) + return (self.index[name] ~= nil) +end + +function DataPack:getPath(name, lua) + if (not self:dataExists(name)) then + error("Data " .. name .. " do not exist for " .. self.foldername) + end + local category = self.index[name] + + local categoryDirectory = self:getCategoryDirectory(category, true) + + return utils.datas.concatFolder(categoryDirectory, name, lua) +end + +function DataPack:get(name) + local path = self:getPath(name, true) + local data = utils.datas.copy(path) + if (not self.areFiles) then + for i, subfile in ipairs(self.subfiles) do + data[subfile] = utils.datas.copyDataset(path, subfile) + end + end + return data +end + +function DataPack:getFromCategory(category) + return self.reverseIndex[category] or {} +end + +return DataPack diff --git a/sonic-radiance.love/birb/core/datas.lua b/sonic-radiance.love/birb/core/datas.lua new file mode 100644 index 0000000..8d12c55 --- /dev/null +++ b/sonic-radiance.love/birb/core/datas.lua @@ -0,0 +1,60 @@ +-- datas.lua :: The main file of the core system, an object full of subsystem +-- loaded by the game to handle the main functions (like screen, translation, +-- inputs…) + +--[[ + 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. +]] + +local DataManager = Object:extend() +local DataPack = require "birb.classes.datapack" +local index = require "datas.gamedata.index" + +function DataManager:new(core) + self.core = core + self:loadDatas() +end + +function DataManager:loadDatas() + self.datapacks = {} + for key, datas in pairs(index.datapacks) do + self.core.debug:debug("datamanager", "loading data for " .. key) + self.datapacks[key] = DataPack(datas[1], datas[2], datas[3], datas[4], datas[5]) + end +end + +function DataManager:get(datapack, name) + return self.datapacks[datapack]:get(name) +end + +function DataManager:exists(datapack, name) + return self.datapacks[datapack]:dataExists(name) +end + +function DataManager:getFromCategory(datapack, category) + return self.datapacks[datapack]:getFromCategory(category) +end + +function DataManager:getCategories(datapack) + return self.datapacks[datapack].categories +end + + +return DataManager \ No newline at end of file diff --git a/sonic-radiance.love/birb/core/init.lua b/sonic-radiance.love/birb/core/init.lua index e7fe764..50eb89e 100644 --- a/sonic-radiance.love/birb/core/init.lua +++ b/sonic-radiance.love/birb/core/init.lua @@ -35,6 +35,7 @@ local Screen = require(cwd .. "screen") local Lang = require(cwd .. "lang") local SceneManager = require(cwd .. "scenemanager") local MusicManager = require(cwd .. "music") +local DataManager = require(cwd .. "datas") -- INIT FUNCTIONS -- Initialize and configure the core object @@ -49,6 +50,7 @@ function CoreSystem:new() self.scenemanager = SceneManager(self) self.lang = Lang(self) self.music = MusicManager(self) + self.datas = DataManager(self) end function CoreSystem:setDefaultConf()