-
Notifications
You must be signed in to change notification settings - Fork 250
Refactor context based templates to make it easier to mod #7004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughThe 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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this 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 fromLoadDefaultTemplatesinstead of swallowing them.
Thepcallcurrently 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
| - (#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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| - (#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".
| status:Destroy() | ||
| if not worked then | ||
| infoStr = LOC("<LOC uisaveload_0008>Save failed! ") .. errmsg | ||
| UIUtil.ShowInfoDialog(GetFrame(0), infoStr, "<LOC _Ok>") | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 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 -nRepository: 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.luaRepository: 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 -nRepository: 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.
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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.
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
Bug Fixes
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.