Refactor context based templates to make it easier to mod#7004
Refactor context based templates to make it easier to mod#7004
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 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🧪 Generate unit tests (beta)
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.
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.
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.
🧩 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.
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.