Skip to content

Conversation

@Garanas
Copy link
Member

@Garanas Garanas commented Jan 29, 2026

Description of the proposed changes

Makes it more friendly to add or remove templates for UI mods. The old approach was intended to be like an auto-discovery feature, but it turns out to be confusing. The old approach is still supported for compatibility reasons.

Adds support for hot module reload. This makes it more convenient to update the module while the game is loaded.

Testing done on the proposed changes

Load the game, confirm that everything still loads.

Additional context

Discord related chatter

Checklist

Summary by CodeRabbit

  • New Features

    • Context-based templates now support dynamic management at runtime, enabling registration and removal of templates as needed.
  • Bug Fixes

    • Improved UI initialization robustness to gracefully handle missing or malfunctioning template systems.
  • Refactor

    • Replaced hardcoded template bindings with a dynamic registration system.

✏️ Tip: You can customize this high-level summary in your review settings.

@github-actions github-actions bot marked this pull request as draft January 29, 2026 16:34
@Garanas Garanas marked this pull request as ready for review January 29, 2026 16:34
@coderabbitai
Copy link

coderabbitai bot commented Jan 29, 2026

📝 Walkthrough

Walkthrough

The changes refactor the context-based templates system by removing deprecated fields and exported template variables from the data module, while introducing new public APIs in the main templates module for dynamically registering and removing templates at runtime. A changelog entry documents these improvements.

Changes

Cohort / File(s) Summary
Changelog
changelog/snippets/other.7004.md
Documents improvements to UIMods interaction with context-based templates, noting newly exposed dynamic template management functions.
Template Management APIs
lua/ui/game/hotkeys/context-based-templates.lua
Introduces three new public functions (RegisterTemplate, RemoveTemplateByReference, LoadDefaultTemplates) and adds an OnDirty module hook to enable dynamic template management and module reloading.
Template Data
lua/ui/game/hotkeys/context-based-templates-data.lua
Removes deprecated TriggersOnBuilding field and 25 exported template variable bindings, consolidating template definitions.
Game Main UI
lua/ui/game/gamemain.lua
Adds defensive pcall wrapper for LoadDefaultTemplates, applies minor formatting adjustments to spacing and table initialization, and updates parameter ordering in a nested callback invocation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 Templates leap and dance with glee,
New APIs set the templates free!
Remove the old, register the new,
Dynamic magic through and through! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.94% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main refactoring effort to improve template management for UI modders with new APIs and simplified workflows.
Description check ✅ Passed PR description covers key areas but reviewer request checklist item is incomplete; all other required sections are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Garanas Garanas requested a review from lL1l1 January 29, 2026 16:41
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@changelog/snippets/other.7004.md`:
- Around line 1-3: Update the changelog entry text to hyphenate "context based"
to "context-based" by replacing the phrase "context based templates feature" (as
seen in the diff snippet) with "context-based templates feature" so the entry
reads "Improve ability for UI mods to interact with the context-based templates
feature".

In `@lua/ui/game/gamemain.lua`:
- Around line 1118-1122: The callback assigns infoStr without local, creating a
global; change the assignment in the save callback (the block invoking
status:Destroy(), checking worked and using errmsg with
UIUtil.ShowInfoDialog/GetFrame) to declare infoStr as a local variable (local
infoStr = ...) to avoid polluting global scope.

In `@lua/ui/game/hotkeys/context-based-templates.lua`:
- Around line 100-103: The duplicate registration of the same template occurs
where
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
appears twice; remove the repeated RegisterTemplate call (or replace it with the
correct intended template file if that was the mistake) so each template (e.g.,
appendt2artillerywithpower.lua, appendt3fabricatorwithstorages.lua,
appendt3artillerywithpower.lua) is registered only once.
🧹 Nitpick comments (1)
lua/ui/game/gamemain.lua (1)

163-199: Log failures from LoadDefaultTemplates instead of swallowing them.
The pcall currently discards errors, which can hide missing templates; logging keeps the UI resilient and debuggable.

🛠️ Suggested fix
-    pcall(
-        function()
-            -- may be hooked and what not, in order to prevent complete UI failures I encapsulate it into a pcall
-            import("/lua/ui/game/hotkeys/context-based-templates.lua").LoadDefaultTemplates()
-        end
-    )
+    local ok, err = pcall(function()
+        -- may be hooked and what not, in order to prevent complete UI failures I encapsulate it into a pcall
+        import("/lua/ui/game/hotkeys/context-based-templates.lua").LoadDefaultTemplates()
+    end)
+    if not ok then
+        WARN("Failed to load context-based templates: " .. tostring(err))
+    end

Comment on lines +1 to +3
- (#7004) Improve ability for UI mods to interact with the context based templates feature

Exposes useful functions to add or remove templates on the fly.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Hyphenate “context-based” for consistency.
Small grammar polish in the changelog entry.

🛠️ Suggested fix
- (`#7004`) Improve ability for UI mods to interact with the context based templates feature
+ (`#7004`) Improve ability for UI mods to interact with the context-based templates feature
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- (#7004) Improve ability for UI mods to interact with the context based templates feature
Exposes useful functions to add or remove templates on the fly.
- (`#7004`) Improve ability for UI mods to interact with the context-based templates feature
Exposes useful functions to add or remove templates on the fly.
🧰 Tools
🪛 LanguageTool

[grammar] ~1-~1: Use a hyphen to join words.
Context: ...for UI mods to interact with the context based templates feature Exposes useful ...

(QB_NEW_EN_HYPHEN)

🤖 Prompt for AI Agents
In `@changelog/snippets/other.7004.md` around lines 1 - 3, Update the changelog
entry text to hyphenate "context based" to "context-based" by replacing the
phrase "context based templates feature" (as seen in the diff snippet) with
"context-based templates feature" so the entry reads "Improve ability for UI
mods to interact with the context-based templates feature".

Comment on lines +1118 to +1122
status:Destroy()
if not worked then
infoStr = LOC("<LOC uisaveload_0008>Save failed! ") .. errmsg
UIUtil.ShowInfoDialog(GetFrame(0), infoStr, "<LOC _Ok>")
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, let's look at the context around lines 1118-1122 in gamemain.lua
sed -n '1100,1140p' lua/ui/game/gamemain.lua | cat -n

Repository: FAForever/fa

Length of output: 1846


🏁 Script executed:

# Let's check the file size first to understand its structure
wc -l lua/ui/game/gamemain.lua

Repository: FAForever/fa

Length of output: 85


🏁 Script executed:

# Look for the function containing lines 1118-1122
# First, let's get a broader context - 50 lines before and after
sed -n '1070,1170p' lua/ui/game/gamemain.lua | cat -n

Repository: FAForever/fa

Length of output: 4293


Add local keyword to prevent global scope pollution.
The assignment to infoStr without the local keyword creates an unintended global variable. Define it as local within the callback function.

                infoStr = LOC("<LOC uisaveload_0008>Save failed! ") .. errmsg
+               local infoStr = LOC("<LOC uisaveload_0008>Save failed! ") .. errmsg
🤖 Prompt for AI Agents
In `@lua/ui/game/gamemain.lua` around lines 1118 - 1122, The callback assigns
infoStr without local, creating a global; change the assignment in the save
callback (the block invoking status:Destroy(), checking worked and using errmsg
with UIUtil.ShowInfoDialog/GetFrame) to declare infoStr as a local variable
(local infoStr = ...) to avoid polluting global scope.

Comment on lines +100 to +103
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3fabricatorwithstorages.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3artillerywithpower.lua").Template)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Avoid double-registering the same template.
appendt2artillerywithpower.lua is registered twice, which will duplicate it in the cycle list.

🛠️ Suggested fix
-    RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3fabricatorwithstorages.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3fabricatorwithstorages.lua").Template)
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt3artillerywithpower.lua").Template)
🤖 Prompt for AI Agents
In `@lua/ui/game/hotkeys/context-based-templates.lua` around lines 100 - 103, The
duplicate registration of the same template occurs where
RegisterTemplate(import("/lua/ui/game/hotkeys/context-based-templates-data/appendt2artillerywithpower.lua").Template)
appears twice; remove the repeated RegisterTemplate call (or replace it with the
correct intended template file if that was the mistake) so each template (e.g.,
appendt2artillerywithpower.lua, appendt3fabricatorwithstorages.lua,
appendt3artillerywithpower.lua) is registered only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants