-
Notifications
You must be signed in to change notification settings - Fork 14
Log Overlay: load whitelist keywords from i18n #161
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
Open
Halizeur
wants to merge
5
commits into
Darkbot-Plugins:main
Choose a base branch
from
Halizeur:halizeur-log-overlay-i18n
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b91aa2a
Log Overlay: load whitelist keywords from i18n
Halizeur f523009
Merge branch 'main' into halizeur-log-overlay-i18n
dm94 5dd2654
LogOverlay: switch i18n whitelist to checkbox categories
Halizeur e52f3e5
LogOverlay: use GameResourcesAPI translation matchers
Halizeur 21758cc
Merge branch 'main' into halizeur-log-overlay-i18n
dm94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ | |
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Deque; | ||
| import java.util.EnumMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import eu.darkbot.api.PluginAPI; | ||
| import eu.darkbot.api.config.ConfigSetting; | ||
|
|
@@ -18,6 +20,8 @@ | |
| import eu.darkbot.api.extensions.MapGraphics; | ||
| import eu.darkbot.api.managers.EventBrokerAPI; | ||
| import eu.darkbot.api.managers.GameLogAPI; | ||
| import eu.darkbot.api.managers.GameResourcesAPI; | ||
| import eu.darkbot.api.managers.GameResourcesAPI.TranslationMatcher; | ||
|
|
||
| /** | ||
| * Renders the latest in-game DarkOrbit log messages as an overlay on the | ||
|
|
@@ -26,6 +30,11 @@ | |
| * Source: {@link GameLogAPI.LogMessageEvent} emitted by DarkBot for each | ||
| * new system message. Each line disappears after DISPLAY_MS ms so the | ||
| * canvas does not get cluttered. | ||
| * | ||
| * Filter: per category, the overlay matches messages against translation | ||
| * patterns from {@link GameResourcesAPI} (the official Bigpoint flashres | ||
| * keys), so the filter works on every game locale without per-language | ||
| * keyword maintenance. | ||
| */ | ||
| @Feature(name = "Log Overlay", | ||
| description = "Shows the latest in-game log messages as an overlay on the canvas", | ||
|
|
@@ -38,37 +47,117 @@ | |
| private static final int MAX_LINES = 5; | ||
| private static final long DISPLAY_MS = 5000L; | ||
|
|
||
| /** | ||
| * Case-insensitive keywords that mark a log message as "interesting": | ||
| * resource / experience / honor gains, or error messages. Anything | ||
| * else (combat, movement, generic info) is filtered out. | ||
| */ | ||
| private static final String[] WHITELIST = { | ||
| // Gains (FR) | ||
| "gagn", "obtenu", "récup", "recup", "récompense", "recompense", | ||
| "collect", "ramass", | ||
| // Gains (EN) | ||
| "gained", "received", "reward", "earned", | ||
| // Currencies / resources | ||
| "uridium", "credit", "crédit", "honor", "honneur", | ||
| "experience", "expérience", "xp ", | ||
| "prometium", "endurium", "terbium", "prometid", "duranium", | ||
| "promerium", "seprom", "xenomit", "palladium", | ||
| // Boosters / drops | ||
| "drop", "booster", | ||
| // Errors (FR) | ||
| "impossible", "erreur", "échec", "echec", "refusé", "refuse", | ||
| "plein", "indisponible", "non disponible", "interdit", | ||
| // Errors (EN) | ||
| "error", "failed", "refused", "denied", "unavailable", "full", | ||
| "cannot", "can't" | ||
| private enum Cat { | ||
| GAINS, BOOSTERS, ERRORS, COMBAT | ||
| } | ||
|
|
||
| /** flashres keys whose translation describes a generic loot / reward | ||
| * message — collection, ore refining, level up, quest reward, etc. */ | ||
| private static final String[] KEYS_GAINS = { | ||
| "oksammel", // "%! collected" | ||
| "boxfarming", // "Salvaging %!" | ||
| "farmresult", // "You collected:" | ||
| "auto_ore_update", | ||
| "special_item_found", | ||
| "lvlup_msg", | ||
| "quest_finish_reward_s", | ||
| "quest_finish_reward_p" | ||
| }; | ||
|
|
||
| /** flashres keys for booster activation / banking-multiplier drops. */ | ||
| private static final String[] KEYS_BOOSTERS = { | ||
| "booster_found", | ||
| "banking_doubler", | ||
| "banking_tripler", | ||
| "banking_quadruplicator", | ||
| "banking_multiplier_active" | ||
| }; | ||
|
|
||
| /** flashres keys for action refusals: cargo full, no ammo, ability | ||
| * on cooldown, ammo purchase failed, ship missing weapons, etc. */ | ||
| private static final String[] KEYS_ERRORS = { | ||
| "boxtoobig", | ||
| "boxdisabled", | ||
| "resourcedisabled", | ||
| "outofammo", | ||
| "rohstoffailed", | ||
| "loadfull", | ||
| "emptybat", | ||
| "ammobuy_fail_uri", | ||
| "ammobuy_fail_cre", | ||
| "ammobuy_fail_space", | ||
| "ammobuy_fail_offer", | ||
| "smartbomb_failed_mines", | ||
| "smartbomb_failed_xenomit", | ||
| "instashield_failed_mines", | ||
| "instashield_failed_xenomit", | ||
| "no_lasers_on_board", | ||
| "msg_laser_not_equipped", | ||
| "msg_rocketlauncher_not_equipped", | ||
| "loot_theft" | ||
| }; | ||
|
|
||
| /** flashres key for the "X destroyed" combat message. */ | ||
| private static final String[] KEYS_COMBAT = { | ||
| "destroyed" | ||
| }; | ||
|
|
||
| /** Currency names. DarkOrbit keeps these mostly untranslated across | ||
| * locales, so a static list works as a substring filter against any | ||
| * raw log message. Lower-cased at init time for case-insensitive | ||
| * comparison. */ | ||
|
Comment on lines
+105
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comments like this |
||
| private static final String[] CURRENCY_NAMES = { | ||
| "uridium", "uri", | ||
| "credit", "crédit", | ||
| "honor", "honour", "honneur", | ||
| "experience", "expérience", "exp", " xp" | ||
| }; | ||
|
|
||
| /** flashres keys whose translation is the localized name of an ore | ||
| * / resource. Resolved at init via {@link GameResourcesAPI#findTranslation}. */ | ||
| private static final String[] RESOURCE_KEYS = { | ||
| "ore_prometium", "ore_endurium", "ore_terbium", | ||
| "ore_prometid", "ore_duranium", "ore_promerium", | ||
| "ore_seprom", "ore_xenomit", "ore_palladium", "ore_osmium" | ||
| }; | ||
|
|
||
| private final Deque<Entry> entries = new ArrayDeque<>(); | ||
| private final Map<Cat, List<TranslationMatcher>> matchers = new EnumMap<>(Cat.class); | ||
| private final List<String> currencyNeedles = new ArrayList<>(); | ||
| private final List<String> resourceNeedles = new ArrayList<>(); | ||
| private LogOverlayConfig config; | ||
|
|
||
| public LogOverlay(PluginAPI api) { | ||
| api.requireAPI(EventBrokerAPI.class).registerListener(this); | ||
|
|
||
| GameResourcesAPI res = api.requireAPI(GameResourcesAPI.class); | ||
| buildMatchers(res, Cat.GAINS, KEYS_GAINS); | ||
| buildMatchers(res, Cat.BOOSTERS, KEYS_BOOSTERS); | ||
| buildMatchers(res, Cat.ERRORS, KEYS_ERRORS); | ||
| buildMatchers(res, Cat.COMBAT, KEYS_COMBAT); | ||
|
|
||
| for (String name : CURRENCY_NAMES) { | ||
| this.currencyNeedles.add(name); | ||
| } | ||
| for (String key : RESOURCE_KEYS) { | ||
| res.findTranslation(key).ifPresent(t -> { | ||
| String lower = t.toLowerCase(); | ||
| if (!this.resourceNeedles.contains(lower)) { | ||
| this.resourceNeedles.add(lower); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** Pre-builds {@link TranslationMatcher} instances for every key in a | ||
| * category. Keys whose translation is missing in the active locale | ||
| * return {@link java.util.Optional#empty()} and are skipped silently. */ | ||
| private void buildMatchers(GameResourcesAPI res, Cat cat, String[] keys) { | ||
| List<TranslationMatcher> list = new ArrayList<>(); | ||
| for (String key : keys) { | ||
| res.getTranslationMatcher(key).ifPresent(list::add); | ||
| } | ||
| this.matchers.put(cat, list); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -100,13 +189,42 @@ | |
| } | ||
|
|
||
| /** | ||
| * Whitelist filter: only display the message if it matches a keyword | ||
| * from {@link #WHITELIST} (case-insensitive). | ||
| * Filter: a message is kept when at least one checked category | ||
| * matches. Most categories use TranslationMatcher (built from the | ||
| * official Bigpoint translations); Currencies and Resources fall | ||
| * back to substring matching against localized names because the | ||
| * actual values appear inside the {@code %!} placeholder of the | ||
| * generic gain templates and are easier to detect this way. | ||
| */ | ||
| private boolean isAllowed(String msg) { | ||
|
Check failure on line 199 in src/main/java/dev/shared/halizeur/log_overlay/LogOverlay.java
|
||
| String lower = msg.toLowerCase(); | ||
| for (String kw : WHITELIST) { | ||
| if (lower.contains(kw)) return true; | ||
| LogOverlayConfig.Categories c = this.config.categories; | ||
| if (c == null) return false; | ||
|
|
||
| if (c.gains && anyMatcherFinds(Cat.GAINS, msg)) return true; | ||
| if (c.boosters && anyMatcherFinds(Cat.BOOSTERS, msg)) return true; | ||
| if (c.errors && anyMatcherFinds(Cat.ERRORS, msg)) return true; | ||
| if (c.combat && anyMatcherFinds(Cat.COMBAT, msg)) return true; | ||
|
|
||
| if (c.currencies || c.resources) { | ||
| String lower = msg.toLowerCase(); | ||
| if (c.currencies && containsAny(lower, this.currencyNeedles)) return true; | ||
| if (c.resources && containsAny(lower, this.resourceNeedles)) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean anyMatcherFinds(Cat cat, String msg) { | ||
| List<TranslationMatcher> list = this.matchers.get(cat); | ||
| if (list == null) return false; | ||
| for (TranslationMatcher m : list) { | ||
| if (m.find(msg)) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static boolean containsAny(String haystack, List<String> needles) { | ||
| for (String n : needles) { | ||
| if (haystack.contains(n)) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove comments like this