Skip to content

Commit

Permalink
move minime compat to on_init and remove the on_nth_tick handler due …
Browse files Browse the repository at this point in the history
…to desync issues

hide the GUI, shortcuts, and player settings when in compat mode
  • Loading branch information
Penguin-Spy committed Nov 21, 2024
1 parent 032f2f3 commit e465681
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 90 deletions.
9 changes: 9 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---------------------------------------------------------------------------------------------------
Version: 1.3.3
Date: 20.11.2024
Compatibility:
- Hide the GUI, shortcuts, and player settings when running in compatibility mode.
Bugfixes:
- Fix a desync related to compatibility with miniMAXIme.
- Fixed the close button of the GUI not specifying the correct sprite due to 2.0 changes.

---------------------------------------------------------------------------------------------------
Version: 1.3.2
Date: 15.11.2024
Expand Down
99 changes: 45 additions & 54 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ remote.add_interface("skins-factored", remote_interface)

if script.active_mods["gvv"] then require("__gvv__.gvv")() end

-- Pass events to GUI
script.on_event(defines.events.on_gui_click, GUI.on_clicked)
script.on_event(defines.events.on_gui_closed, GUI.on_closed)
-- keybind
script.on_event("skins_factored_toggle_interface", function(event)
local player = game.get_player(event.player_index)
GUI.toggle_window(player)
end)
-- shortcut bar button
script.on_event(defines.events.on_lua_shortcut, function(event)
if event.prototype_name == "skins_factored_toggle_interface" then
local player = game.get_player(event.player_index)
GUI.toggle_window(player)
end
end)


-- [[ Local functions ]]

Expand Down Expand Up @@ -264,42 +248,6 @@ end

-- [[ Initalization ]]

-- used to invoke remote calls to other mods once the game and all mods have fully initalized and loaded
-- does not run on every load, only loads that run initalize()!
local function runtime_initalize()
-- unregister this event
script.on_nth_tick(1, nil)
log("Runtime initalizing")

if Common.compatibility_mode then
-- list of all character prototypes we've added
local available_prototypes = {}
for _, skin_id in ipairs(Common.added_skins) do
table.insert(available_prototypes, Common.skin_to_prototype(skin_id))
end

-- miniMAXIme compat
if script.active_mods["minime"] then
log("minime compat: " .. serpent.line(available_prototypes))
remote.call("minime", "register_characters", available_prototypes)
log("called")

-- RitnCharacters compat
elseif script.active_mods["RitnCharacters"] then
for _, prototype in ipairs(available_prototypes) do
local name = {"entity-name." .. prototype}
log("adding skin " .. prototype .. " to RitnCharacters: " .. serpent.line(name))
remote.call("RitnCharacters", "remove_character", prototype) -- remove the auto-generated entry (it has an untranslated name)
remote.call("RitnCharacters", "add_character", name, prototype)
end

-- disabled because scenario doesn't support characters
else
log("Skin switching disabled, no compat loaded.")
end
end
end

-- Ensures players have GUI & PreviewSurface properly set up
local function initalize_player(player)
log("Initalizing player "..player.name.."["..player.index.."]")
Expand Down Expand Up @@ -348,8 +296,33 @@ function initalize()
initalize_player(player)
end

-- Initalize function that runs during runtime, once everything is done loading (on_init_final_fixes if you will)
script.on_nth_tick(1, runtime_initalize)
if Common.compatibility_mode then
-- list of all character prototypes we've added
local available_prototypes = {}
for skin_id in pairs(Common.added_skins) do
table.insert(available_prototypes, Common.skin_to_prototype(skin_id))
end

-- miniMAXIme compat
if script.active_mods["minime"] then
log("minime compat: " .. serpent.line(available_prototypes))
remote.call("minime", "register_characters", available_prototypes)
log("called")

-- RitnCharacters compat
elseif script.active_mods["RitnCharacters"] then
for _, prototype in ipairs(available_prototypes) do
local name = {"entity-name." .. prototype}
log("adding skin " .. prototype .. " to RitnCharacters: " .. serpent.line(name))
remote.call("RitnCharacters", "remove_character", prototype) -- remove the auto-generated entry (it has an untranslated name)
remote.call("RitnCharacters", "add_character", name, prototype)
end

-- disabled because scenario doesn't support characters
else
log("Skin switching disabled, no compat loaded.")
end
end
end

-- Runs every time the save is loaded (including the first time). Can't edit the game state
Expand All @@ -363,10 +336,28 @@ local function loadalize()
script.on_event(defines.events.on_player_respawned, swap_on_player_respawned)
end

-- general functionality
script.on_event(defines.events.on_tick, on_tick)
script.on_event(defines.events.on_runtime_mod_setting_changed, on_runtime_mod_setting_changed)
script.on_event(defines.events.on_cutscene_cancelled, on_cutscene_cancelled)
script.on_event(defines.events.on_force_created, on_force_created)

-- Pass events to GUI
script.on_event(defines.events.on_gui_click, GUI.on_clicked)
script.on_event(defines.events.on_gui_closed, GUI.on_closed)
-- keybind
script.on_event("skins_factored_toggle_interface", function(event)
local player = game.get_player(event.player_index)
GUI.toggle_window(player)
end)
-- shortcut bar button
script.on_event(defines.events.on_lua_shortcut, function(event)
if event.prototype_name == "skins_factored_toggle_interface" then
local player = game.get_player(event.player_index)
GUI.toggle_window(player)
end
end)

log("Added all event listeners")
else
log("Loading in compatibility mode; event listeners disabled")
Expand Down
42 changes: 22 additions & 20 deletions data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,28 @@ if mods["CharacterModHelper"] then
CharModHelper.enable_multi_character("skins-factored")
end

-- Prototypes
data:extend{
{
type = "custom-input",
name = "skins_factored_toggle_interface",
key_sequence = "ALT + S",
order = "a",
consuming = "game-only"
} --[[@as data.CustomInputPrototype]],
{
type = "shortcut",
name = "skins_factored_toggle_interface",
associated_control_input = "skins_factored_toggle_interface",
action = "lua",
icon = "__core__/graphics/icons/entity/character.png",
icon_size = 64,
small_icon = "__core__/graphics/icons/entity/character.png",
small_icon_size = 64
} --[[@as data.ShortcutPrototype]]
}
-- shortcut prototypes
if not (mods["minime"] or mods["RitnCharacters"]) then
data:extend{
{
type = "custom-input",
name = "skins_factored_toggle_interface",
key_sequence = "ALT + S",
order = "a",
consuming = "game-only"
} --[[@as data.CustomInputPrototype]],
{
type = "shortcut",
name = "skins_factored_toggle_interface",
associated_control_input = "skins_factored_toggle_interface",
action = "lua",
icon = "__core__/graphics/icons/entity/character.png",
icon_size = 64,
small_icon = "__core__/graphics/icons/entity/character.png",
small_icon_size = 64
} --[[@as data.ShortcutPrototype]]
}
end

local styles = data.raw["gui-style"].default

Expand Down
4 changes: 3 additions & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "skins-factored",
"version": "1.3.2",
"version": "1.3.3",
"title": "!skins (Skins Factored)",
"description": "Library mod that makes it easy for other mods to add custom characters (skins), with a character picker.\nWritten to be compatible with almost all mods that edit the default character prototype.\nExplicitly compatible with Jetpack, Space Exploration, RPG System, and CharacterModHelper",
"author": "Penguin_Spy",
"factorio_version": "2.0",
"dependencies": [
"? informatron >= 0.4.0",
"(?) CharacterModHelper >= 2.0.0",
"(?) minime",
"(?) RitnCharacters",
"(?) space-exploration"
]
}
19 changes: 7 additions & 12 deletions scripts/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local function create_window(player)
titlebar.add{ type = "label", style = "frame_title", caption = { "skins-factored.title_skins-factored" }, ignored_by_interaction = true }
titlebar.add{ type = "empty-widget", style = "skins_factored_titlebar_drag", ignored_by_interaction = true }
titlebar.add{ type = "sprite-button", style = "frame_action_button", tooltip = { "gui.close-instruction" },
sprite = "utility/close", hovered_sprite = "utility/close_black", clicked_sprite = "utility/close_black",
sprite = "utility/close", hover_sprite = "utility/close_black", click_sprite = "utility/close_black",
tags = tags{ action = "toggle_window" }
}

Expand All @@ -41,16 +41,11 @@ local function create_window(player)
local header = body.add{ type = "frame", style = "skins_factored_subheader_frame", direction = "vertical" }

header.add{ type = "label", style = "skins_factored_skin_selector_label", caption = { "skins-factored.about" } }
header.add{ type = "label", style = "skins_factored_skin_selector_label", caption = { "skins-factored.skin-selector-instructions" } }

if not Common.compatibility_mode then -- instructions & frame for skins table
header.add{ type = "label", style = "skins_factored_skin_selector_label", caption = { "skins-factored.skin-selector-instructions" } }

-- need "scroll-pane" to put picker frame in
local picker_pane = body.add{ type = "scroll-pane", name = "picker_pane", style = "skins_factored_scroll_pane", vertical_scroll_policy = "always" }
picker_pane.add{ type = "frame", style = "deep_frame_in_shallow_frame_for_description", direction = "vertical", name = "picker_frame" }
else -- just compat instructions
body.add{ type = "label", style = "skins_factored_skin_selector_label", caption = Common.compatibility_message }
end
-- need "scroll-pane" to put picker frame in
local picker_pane = body.add{ type = "scroll-pane", name = "picker_pane", style = "skins_factored_scroll_pane", vertical_scroll_policy = "always" }
picker_pane.add{ type = "frame", style = "deep_frame_in_shallow_frame_for_description", direction = "vertical", name = "picker_frame" }

main.visible = false
return main
Expand All @@ -70,9 +65,9 @@ return function(PreviewSurface)
selector_window.destroy()
end

-- then create the window
selector_window = create_window(player)
-- then create the window (if not in compatibility mode)
if not Common.compatibility_mode then
selector_window = create_window(player)
GUI.attach_skins_table(selector_window.body.picker_pane.picker_frame, player)
end
end
Expand Down
11 changes: 8 additions & 3 deletions settings-updates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ log("Registered skins: " .. table.concat(skin_ids, ", "))

local skin_ids_string = table.concat(skin_ids, ";")

-- hide user settings if running in compatibility mode
local hide_settings = (mods["minime"] or mods["RitnCharacters"]) and true or false

-- Register all skin_ids as options for the dropdown
data:extend{
{ -- Actual setting for users to pick a skin
Expand All @@ -26,7 +29,8 @@ data:extend{
order = "a",
setting_type = "runtime-per-user",
default_value = (#skin_ids == 2 and skin_ids[2]) or "engineer", -- Default to the only custom skin if there's just 1, or to the engineer if there's multiple
allowed_values = skin_ids
allowed_values = skin_ids,
hidden = hide_settings
},
{ -- Fake setting to smuggle the list of skins into the control stage
type = "string-setting",
Expand All @@ -41,14 +45,15 @@ data:extend{
name = "skins-factored-render-character",
order = "b",
setting_type = "runtime-per-user",
default_value = true
default_value = true,
hidden = hide_settings
},
{ -- Should the skin selector button be added to the mod-gui flow in the top left
type = "bool-setting",
name = "skins-factored-mod-gui-button",
order = "c",
setting_type = "runtime-per-user",
default_value = true,
hidden = mods["informatron"] and true or false -- hide the setting if InformaTron is active, as the setting is ignored
hidden = mods["informatron"] and true or hide_settings -- hide the setting if InformaTron is active, as the setting is ignored
}
}

0 comments on commit e465681

Please sign in to comment.