From b6699df3aae7bb4f13ddad0b2b71ebfc4521aa2d Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sat, 23 May 2020 22:50:34 +0100 Subject: [PATCH] Added datastore to debug --- expcore/datastore.lua | 38 +++++- modules/gui/debug/expcore_datastore_view.lua | 131 +++++++++++++++++++ modules/gui/debug/global_view.lua | 2 +- modules/gui/debug/main_view.lua | 1 + 4 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 modules/gui/debug/expcore_datastore_view.lua diff --git a/expcore/datastore.lua b/expcore/datastore.lua index 31f65189..f9f81b59 100644 --- a/expcore/datastore.lua +++ b/expcore/datastore.lua @@ -91,6 +91,13 @@ function DatastoreManager.ingest(action, tableName, key, valueJson) end +--- Debug, Use to get all datastores, or return debug info on a datastore +function DatastoreManager.debug(tableName) + if not tableName then return Datastores end + local datastore = assert(Datastores[tableName], 'Datastore not found '..tostring(tableName)) + return datastore:debug() +end + --- Commonly used serializer, returns the name of the object function DatastoreManager.name_serializer(rawKey) return rawKey.name @@ -99,6 +106,30 @@ end ----- Datastore ----- -- @section datastore +--- Debug, Get the debug info for this datastore +function Datastore:debug() + local debug_info = {} + + if self.parent then + debug_info.parent = self.parent.name + else + debug_info.settings = { auto_save = self.auto_save, save_to_disk = self.save_to_disk, propagate_changes = self.propagate_changes, serializer = not not self.serializer } + end + + local children = {} + for name in pairs(self.children) do children[#children+1] = name end + if #children > 0 then debug_info.children = children end + + local events = {} + for name, handlers in pairs(self.events) do events[name] = #handlers end + if next(events) then debug_info.events = events end + + if next(self.metadata) then debug_info.metadata = self.metadata end + debug_info.data = self:get_all() + + return debug_info +end + --- Internal, Get data following combine logic function Datastore:raw_get(key, fromChild) local data = self.data @@ -171,7 +202,7 @@ function Datastore:save(key) if not self.save_to_disk then return end key = self:serialize(key) local value = self:raise_event('on_save', key, copy(self:raw_get(key))) - local action = self.propagateChanges and 'propagate' or 'save' + local action = self.propagate_changes and 'propagate' or 'save' self:write_action(action, key, value) end @@ -253,9 +284,8 @@ function Datastore:get_all(callback) if not self.parent then return filter(self.data, callback) else - local table_name = self.table_name - local data = self.parent:get_all() - for key, value in pairs(data) do + local data, table_name = {}, self.table_name + for key, value in pairs(self.parent:get_all()) do data[key] = value[table_name] end return filter(data, callback) diff --git a/modules/gui/debug/expcore_datastore_view.lua b/modules/gui/debug/expcore_datastore_view.lua new file mode 100644 index 00000000..f7a9912f --- /dev/null +++ b/modules/gui/debug/expcore_datastore_view.lua @@ -0,0 +1,131 @@ +local Gui = require 'utils.gui' --- @dep utils.gui +local Datastore = require 'expcore.datastore' --- @dep expcore.datastore +local Color = require 'utils.color_presets' --- @dep utils.color_presets +local Model = require 'modules.gui.debug.model' --- @dep modules.gui.debug.model + +local dump = Model.dump +local dump_text = Model.dump_text +local concat = table.concat + +local Public = {} + +local header_name = Gui.uid_name() +local left_panel_name = Gui.uid_name() +local right_panel_name = Gui.uid_name() +local input_text_box_name = Gui.uid_name() +local refresh_name = Gui.uid_name() + +Public.name = 'Datastore' + +function Public.show(container) + local main_flow = container.add {type = 'flow', direction = 'horizontal'} + + local left_panel = main_flow.add {type = 'scroll-pane', name = left_panel_name} + local left_panel_style = left_panel.style + left_panel_style.width = 300 + + for name in pairs(table.keysort(Datastore.debug())) do + local header = left_panel.add({type = 'flow'}).add {type = 'label', name = header_name, caption = name} + Gui.set_data(header, name) + end + + local right_flow = main_flow.add {type = 'flow', direction = 'vertical'} + + local right_top_flow = right_flow.add {type = 'flow', direction = 'horizontal'} + + local input_text_box = right_top_flow.add {type = 'text-box', name = input_text_box_name} + local input_text_box_style = input_text_box.style + input_text_box_style.horizontally_stretchable = true + input_text_box_style.height = 32 + input_text_box_style.maximal_width = 1000 + + local refresh_button = + right_top_flow.add {type = 'sprite-button', name = refresh_name, sprite = 'utility/reset', tooltip = 'refresh'} + local refresh_button_style = refresh_button.style + refresh_button_style.width = 32 + refresh_button_style.height = 32 + + local right_panel = right_flow.add {type = 'text-box', name = right_panel_name} + right_panel.read_only = true + right_panel.selectable = true + + local right_panel_style = right_panel.style + right_panel_style.vertically_stretchable = true + right_panel_style.horizontally_stretchable = true + right_panel_style.maximal_width = 1000 + right_panel_style.maximal_height = 1000 + + local data = { + right_panel = right_panel, + input_text_box = input_text_box, + selected_header = nil + } + + Gui.set_data(input_text_box, data) + Gui.set_data(left_panel, data) + Gui.set_data(refresh_button, data) +end + +Gui.on_click( + header_name, + function(event) + local element = event.element + local tableName = Gui.get_data(element) + + local left_panel = element.parent.parent + local data = Gui.get_data(left_panel) + local right_panel = data.right_panel + local selected_header = data.selected_header + local input_text_box = data.input_text_box + + if selected_header then + selected_header.style.font_color = Color.white + end + + element.style.font_color = Color.orange + data.selected_header = element + + input_text_box.text = tableName + input_text_box.style.font_color = Color.black + + local content = Datastore.debug(tableName) + local content_string = {} + for key, value in pairs(content) do + content_string[#content_string+1] = key:gsub('^%l', string.upper)..' = '..dump(value) + end + right_panel.text = concat(content_string, '\n') + end +) + +local function update_dump(text_input, data) + local content = Datastore.debug(text_input.text) + local content_string = {} + for key, value in pairs(content) do + content_string[#content_string+1] = key:gsub('^%l', string.upper)..' = '..dump(value) + end + data.right_panel.text = concat(content_string, '\n') +end + +Gui.on_text_changed( + input_text_box_name, + function(event) + local element = event.element + local data = Gui.get_data(element) + + update_dump(element, data) + end +) + +Gui.on_click( + refresh_name, + function(event) + local element = event.element + local data = Gui.get_data(element) + + local input_text_box = data.input_text_box + + update_dump(input_text_box, data) + end +) + +return Public diff --git a/modules/gui/debug/global_view.lua b/modules/gui/debug/global_view.lua index 3ab51d8d..aff9a6fd 100644 --- a/modules/gui/debug/global_view.lua +++ b/modules/gui/debug/global_view.lua @@ -8,7 +8,7 @@ local concat = table.concat local Public = {} -local ignore = {tokens = true, data_store = true} +local ignore = {tokens = true, data_store = true, datastores = true} local header_name = Gui.uid_name() local left_panel_name = Gui.uid_name() diff --git a/modules/gui/debug/main_view.lua b/modules/gui/debug/main_view.lua index 0771bdb1..bdc93f43 100644 --- a/modules/gui/debug/main_view.lua +++ b/modules/gui/debug/main_view.lua @@ -5,6 +5,7 @@ local Public = {} local pages = { require 'modules.gui.debug.redmew_global_view', + require 'modules.gui.debug.expcore_datastore_view', require 'modules.gui.debug.expcore_store_view', require 'modules.gui.debug.expcore_gui_view', require 'modules.gui.debug.global_view',