Skip to content

Commit

Permalink
prep for v0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
chadvandy committed Oct 2, 2022
1 parent e5e376e commit 17fb637
Show file tree
Hide file tree
Showing 30 changed files with 6,757 additions and 2,506 deletions.
16 changes: 14 additions & 2 deletions script/_lib/mod/!vlib_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
---@type fun(className:string,attr:table) : Class
local new_class = require "script.vlib.includes.30-log"

do
local Old = ModLog
function ModLog(text)
text = tostring(text)
Old(text)
end
end

---@class VLib : Class
local defaults = {
---@type table<string, VLib.Log>
Expand Down Expand Up @@ -120,6 +128,10 @@ function VLib.init()
VLib.LoadModule("extensions", "script/vlib/")
VLib.LoadModule("helpers", "script/vlib/")
VLib.LoadModule("uic", "script/vlib/")

---@type CommandManager
VLib.CommandManager = VLib.LoadModule("command_manager", "script/vlib/")
VLib.CommandManager:init()
end

--- Create a new Class object, which can be used to simulate OOP systems.
Expand Down Expand Up @@ -211,7 +223,7 @@ end
--- Load every file, and return the Lua module, from within the folder specified, using the pattern specified.
---@param path string The path you're checking. Local to data, so if you're checking for any file within the script folder, use "script/" as the path.
---@param search_override string The file you're checking for. I believe it requires a wildcard somewhere, "*", but I haven't messed with it enough. Use "*" for any file, or "*.lua" for any lua file, or "*/main.lua" for any file within a subsequent folder with the name main.lua.
---@param func_for_each fun(filename:string, module:function)? Code to run for each module loaded.
---@param func_for_each fun(filename:string, module:table)? Code to run for each module loaded.
function VLib.LoadModules(path, search_override, func_for_each)
if not search_override then search_override = "*.lua" end
-- vlogf("Checking %s for all main.lua files!", path)
Expand Down Expand Up @@ -246,7 +258,7 @@ function VLib.LoadModules(path, search_override, func_for_each)

local module = VLib.LoadModule(filename, string.gsub(filename_for_out, filename..".lua", ""))
if func_for_each and is_function(func_for_each) then
func_for_each(filename_for_out, module)
func_for_each(filename, module)
end
end
end
Expand Down
128 changes: 15 additions & 113 deletions script/_lib/mod/mct.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
local mct_defaults = {
_mods_path = "/script/mct/settings/",
_self_path = "/script/vlib/mct/",

_finalized = false,
_initialized = false,

_registered_mods = {},
Expand Down Expand Up @@ -87,6 +85,7 @@ function mct:load_modules()
---@type MCT.Registry
self.registry = load_module("registry", obj_path)

---@type MCT.UI
self.ui = load_module("main", ui_path)

---@type MCT.Sync
Expand All @@ -106,11 +105,14 @@ function mct:load_modules()
---@type table<string, MCT.Page>
self._MCT_PAGE_TYPES = { }

--- TODO use the func_for_each thing
--- Load all Page types
load_modules(layout_path, "*.lua")

---@type table<string, MCT.Option>
self._MCT_TYPES = { }

--- TODO autoload
---@type MCT.Option
self._MCT_OPTION = load_module("option", obj_path)

Expand Down Expand Up @@ -163,55 +165,7 @@ end
--- TODO clean this the fuck up
function mct:load_and_start(loading_game_context, is_mp)
self._initialized = true

core:add_listener(
"who_is_the_host_tell_me_now_please",
"UITrigger",
function(context)
return context:trigger():starts_with("mct_host|")
end,
function(context)
out("UITrigger!")
local str = context:trigger()
local faction_key = string.gsub(str, "mct_host|", "")

cm:set_saved_value("mct_host", faction_key)

-- self.settings:mp_load()
end,
false
)

local function trigger(is_multi)
core:trigger_custom_event("MctInitialized", {["mct"] = self, ["is_multiplayer"] = is_multi})
end

if __game_mode == __lib_type_campaign then
if is_mp then
VLib.Log("Pre load game callback")
self.registry:load(loading_game_context)
VLib.Log("Post load game callback")
-- self.registry:load_game(loading_game_context)

trigger(true)
else
self.registry:load(loading_game_context)

trigger(false)
end
else
--log("frontend?")
-- read the settings file
local ok, msg = pcall(function()
-- self.settings:load()

self.registry:load()

trigger(false)
end) if not ok then verr(msg) end
end

out("End of load_and_start")
self.registry:load(loading_game_context, is_mp)
end

function mct:load_mods()
Expand Down Expand Up @@ -260,77 +214,25 @@ function mct:get_mod_with_name(mod_name)
return self:get_mod_by_key(mod_name)
end

function mct:finalize_new()

end

--- Internal use only. Triggers all the functionality for "Finalize Settings!"
function mct:finalize()
local ok, msg = pcall(function()
if __game_mode == __lib_type_campaign then
-- check if it's MP!
if cm.game_interface:model():is_multiplayer() then
-- check if it's the host
if cm:get_local_faction_name(true) == cm:get_saved_value("mct_host") then
vlog("Finalizing settings mid-campaign for MP.")
self.registry:save()

self._finalized = true
self.ui.locally_edited = false

-- communicate to both clients that this is happening!
local mct_data = {}
local all_mods = self:get_mods()
for mod_key, mod_obj in pairs(all_mods) do
vlog("Looping through mod obj ["..mod_key.."]")
mct_data[mod_key] = {}
local all_options = mod_obj:get_options()

for option_key, option_obj in pairs(all_options) do
if not option_obj:get_local_only() then
vlog("Looping through option obj ["..option_key.."]")
mct_data[mod_key][option_key] = {}

vlog("Setting: "..tostring(option_obj:get_finalized_setting()))

mct_data[mod_key][option_key]._setting = option_obj:get_finalized_setting()
else
--?
end
end
end
MultiplayerCommunicator:TriggerEvent("MctMpFinalized", 0, mct_data)

self.registry:local_only_finalize(true)
else
self._finalized = true
self.ui.locally_edited = false

self.registry:local_only_finalize(false)
end
if not self.registry:has_pending_changes() then return end

-- check if it's MP!
if __game_mode == __lib_type_campaign and cm.game_interface:model():is_multiplayer() then
-- check if it's the host
if cm:get_local_faction_name(true) == cm:get_saved_value("mct_host") then
-- Send finalized settings to all clients (including self to keep models synced!)
self.sync:distribute_finalized_settings()
self.registry:local_only_finalize(true)
else
-- it's SP, do regular stuff
self.registry:save()

self._finalized = true

-- remove the "locally_edited" field
self.ui.locally_edited = false

core:trigger_custom_event("MctFinalized", {["mct"] = self, ["mp_sent"] = false})
self.registry:local_only_finalize(false)
end
else
--- TODO if we haven't locally edited, don't do this?
self.registry:save()

self._finalized = true

-- remove the "locally_edited" field
self.ui.locally_edited = false

core:trigger_custom_event("MctFinalized", {["mct"] = self, ["mp_sent"] = false})
end
end) if not ok then verr(msg) end
end

--- Getter for the @{mct_mod} with the supplied key.
Expand Down
91 changes: 53 additions & 38 deletions script/mct/settings/mct_testing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,60 @@
local mct = get_mct()

local mct_mod = mct:register_mod("mct_demo")
-- local page = mct_mod:create_rowbased_settings_page("Row-based Page")

---@type MCT.Option.Dropdown
local glob = mct_mod:add_new_option("global_test", 'dropdown')
glob:set_text("User-specific Settings")
glob:set_is_global(true)
glob:add_dropdown_values({
{
key = "default",
text = "Default",
is_default = true,
},
{
key = "host",
text = "Host Option",
},
{
key = "client",
text = "Client option",
}
})

---@type MCT.Option.Dropdown
local camp = mct_mod:add_new_option("campaign_test", "dropdown")
camp:set_text("Campaign-wide Settings")
camp:add_dropdown_values({
{
key = "default",
text = "Default",
is_default = true,
},
{
key = "host",
text = "Host Option",
},
{
key = "client",
text = "Client option",
}
})
local b = mct_mod:add_new_section("section_b", "Section B (index first)")
mct_mod:add_new_option("test_b", 'checkbox'):set_text("Testing Checkbox")

local a = mct_mod:add_new_section("section_a", "Section A (key first)")
mct_mod:add_new_option("test", 'checkbox'):set_text("Testing Checkbox")

local c = mct_mod:add_new_section("section_c", "AAA Section C (text first)")
mct_mod:add_new_option("test_c", 'checkbox'):set_text("Testing Checkbox")

-- page:set_section_sort_function("text_sort")
-- page:assign_section_to_page(a)
-- page:assign_section_to_page(b)
-- page:assign_section_to_page(c)

-- ---@type MCT.Option.Dropdown
-- local glob = mct_mod:add_new_option("global_test", 'dropdown')
-- glob:set_text("User-specific Settings")
-- glob:set_is_global(true)
-- glob:add_dropdown_values({
-- {
-- key = "default",
-- text = "Default",
-- is_default = true,
-- },
-- {
-- key = "host",
-- text = "Host Option",
-- },
-- {
-- key = "client",
-- text = "Client option",
-- }
-- })

-- ---@type MCT.Option.Dropdown
-- local camp = mct_mod:add_new_option("campaign_test", "dropdown")
-- camp:set_text("Campaign-wide Settings")
-- camp:add_dropdown_values({
-- {
-- key = "default",
-- text = "Default",
-- is_default = true,
-- },
-- {
-- key = "host",
-- text = "Host Option",
-- },
-- {
-- key = "client",
-- text = "Client option",
-- }
-- })

-- local glob = mct_mod:add_new_option("global_test", 'text_input')
-- glob:set_default_value("Blorpa")
Expand Down
30 changes: 30 additions & 0 deletions script/vlib/command_manager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--- TODO command systems
---@alias command_table table<string, {text:string, tooltip:string, callback:fun()}>

---@class CommandManager : Class
local defaults = {
path = "script/vlib/mct/core/commands/",

---@type table<string, command_table[]>
commands = {},
}

---@class CommandManager : Class
local CommandManager = VLib.NewClass("CommandManager", defaults)

function CommandManager:init()
VLib.LoadModules(
self.path,
"*.lua",
function(filename, this_module)
--- Assign the commands table to self.commands[filename], ie. self.commands[mct_mod] = {commands = {command_a,}} etc.

---= self.commands.mct_mod_commands = {command_a}
self.commands[filename.."_commands"] = this_module

common.set_context_value(filename.."_commands", {commands = self.commands[filename.."_commands"]})
end
)
end

return CommandManager
4 changes: 3 additions & 1 deletion script/vlib/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ end
---@param popup_key string
---@param text string
---@param use_two_buttons boolean
---@param button_one_callback function
---@param button_one_callback function?
---@param button_two_callback function?
---@param creation_callback fun(popup:UIC)?
---@param opt_parent UIC?
Expand Down Expand Up @@ -97,6 +97,8 @@ function VLib.TriggerPopup(popup_key, text, use_two_buttons, button_one_callback

local parent = is_uicomponent(opt_parent) and opt_parent or core:get_ui_root()

if not is_function(button_one_callback) then button_one_callback = function() end end
if not is_function(button_two_callback) then button_two_callback = function() end end
if not use_two_buttons then button_two_callback = function() end end

local popup = core:get_or_create_component(popup_key, "ui/vandy_lib/dialogue_box", parent)
Expand Down
11 changes: 11 additions & 0 deletions script/vlib/mct/core/commands/mct_mod.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- MCT.Mod contextual commands.

---@type command_table[]
return {
revert_to_defaults = {
text = "Revert to Defaults",
tooltip = "My tooltip",
---@param mod_obj MCT.Mod
callback = function(mod_obj) mod_obj:revert_to_defaults() end,
}
}
Empty file.
Empty file.
Loading

0 comments on commit 17fb637

Please sign in to comment.