diff --git a/src/main/java/com/github/manolo8/darkbot/Main.java b/src/main/java/com/github/manolo8/darkbot/Main.java index bfbadc4c0..5e24da1a7 100644 --- a/src/main/java/com/github/manolo8/darkbot/Main.java +++ b/src/main/java/com/github/manolo8/darkbot/Main.java @@ -17,6 +17,7 @@ import com.github.manolo8.darkbot.core.manager.GuiManager; import com.github.manolo8.darkbot.core.manager.HeroManager; import com.github.manolo8.darkbot.core.manager.MapManager; +import com.github.manolo8.darkbot.core.manager.PerformanceManager; import com.github.manolo8.darkbot.core.manager.PingManager; import com.github.manolo8.darkbot.core.manager.RepairManager; import com.github.manolo8.darkbot.core.manager.SettingsManager; @@ -99,6 +100,7 @@ public class Main extends Thread implements PluginListener, BotAPI { public final RepairManager repairManager; private final EventBrokerAPI eventBroker; + private final PerformanceManager performanceManager; private final MainGui form; private final BotInstaller botInstaller; @@ -160,8 +162,10 @@ public Main(StartupParams params) { API.setSize(config.BOT_SETTINGS.API_CONFIG.width, config.BOT_SETTINGS.API_CONFIG.height); pluginAPI.addInstance(API); + this.performanceManager = pluginAPI.requireInstance(PerformanceManager.class); + this.botInstaller.install(settingsManager, facadeManager, effectManager, guiManager, mapManager, - hero, statsManager, pingManager, repairManager); + hero, statsManager, pingManager, repairManager, performanceManager); this.botInstaller.invalid.add(value -> { if (!value) lastRefresh = System.currentTimeMillis(); @@ -204,7 +208,7 @@ public void run() { avgTick = ((avgTick * 9) + (current - time)) / 10; Time.sleepMax(time, botInstaller.invalid.get() ? 250 : - Math.max(config.BOT_SETTINGS.OTHER.MIN_TICK, Math.min((int) (avgTick * 1.25), 100))); + Math.max(performanceManager.getMinTickTime(), Math.min((int) (avgTick * 1.25), 100))); try { // Just in case, we can't risk the main loop dying. @@ -262,6 +266,7 @@ private void validTick() { guiManager.tick(); statsManager.tick(); repairManager.tick(); + performanceManager.tick(); API.tick(); tickingModule = running && guiManager.canTickModule(); diff --git a/src/main/java/com/github/manolo8/darkbot/config/Config.java b/src/main/java/com/github/manolo8/darkbot/config/Config.java index 2822ce833..9748a1bd5 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/Config.java +++ b/src/main/java/com/github/manolo8/darkbot/config/Config.java @@ -237,10 +237,8 @@ public static class APIConfig { public @Option boolean FORCE_GAME_LANGUAGE = false; public @Option boolean ENFORCE_HW_ACCEL = true; public @Option boolean USE_3D = false; - public @Option boolean DISABLE_RENDER = false; public @Option boolean USE_PROXY = false; public @Option boolean CLEAR_CACHE_ON_STUCK = true; - public @Option @Number(min = 1, max = 60) @Number.Disabled(value = 0, def = 30) int MAX_FPS = 0; @Option @Dropdown(multi = true) public Set DARK_HOOK_FLAGS = EnumSet.allOf(HookAdapter.Flag.class); @@ -283,11 +281,21 @@ public static class CustomBackground { public @Option ImageWrapper IMAGE = new ImageWrapper(); } + public @Option Performance PERFORMANCE = new Performance(); + public static class Performance { + public @Option boolean ALWAYS_ENABLED = true; + public @Option("config.bot_settings.api_config.disable_render") boolean DISABLE_RENDER = false; + public @Option("config.bot_settings.api_config.max_fps") + @Number(min = 1, max = 60) @Number.Disabled(value = 0, def = 15) int MAX_FPS = 0; + + public @Option("config.bot_settings.other.min_tick") @Visibility(Level.ADVANCED) + @Number(min = 10, max = 250) @Number.Disabled(value = 15, def = 15) int MIN_TICK = 15; + } + public @Option @Visibility(Level.INTERMEDIATE) Other OTHER = new Other(); public static class Other { public @Option boolean DISABLE_MASTER_PASSWORD = false; public @Option @Number(min = 10, max = 300) int ZONE_RESOLUTION = 30; - public @Option @Visibility(Level.DEVELOPER) @Number(min = 10, max = 250) int MIN_TICK = 15; public @Option @Visibility(Level.ADVANCED) boolean DEV_STUFF = false; } } diff --git a/src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java b/src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java index e547b9942..cfd15873d 100644 --- a/src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java +++ b/src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java @@ -31,7 +31,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.EnumSet; -import java.util.function.Consumer; import java.util.function.LongPredicate; import static com.github.manolo8.darkbot.Main.API; @@ -60,7 +59,6 @@ public class GameAPIImpl< protected final String version; private final ConfigAPI config; - private final Consumer fpsLimitListener; // Needs to be kept as a strong reference to avoid GC protected final LoginData loginData; // Used only if api supports LOGIN protected int pid; // Used only if api supports ATTACH @@ -105,14 +103,6 @@ public GameAPIImpl(StartupParams params, this.initiallyShown = hasCapability(Capability.INITIALLY_SHOWN) && !params.getAutoHide(); this.mapManager = main.mapManager; - if (hasCapability(Capability.DIRECT_LIMIT_FPS)) { - ConfigSetting maxFps = config.requireConfig("bot_settings.api_config.max_fps"); - maxFps.addListener(fpsLimitListener = this::setMaxFps); - setMaxFps(maxFps.getValue()); - } else { - this.fpsLimitListener = null; - } - if (hasCapability(Capability.PROXY)) { ConfigSetting useProxy = config.requireConfig("bot_settings.api_config.use_proxy"); if (useProxy.getValue() || OSUtil.isWindows7OrLess()) diff --git a/src/main/java/com/github/manolo8/darkbot/core/manager/MapManager.java b/src/main/java/com/github/manolo8/darkbot/core/manager/MapManager.java index 045ea91f4..c4c0f2d05 100644 --- a/src/main/java/com/github/manolo8/darkbot/core/manager/MapManager.java +++ b/src/main/java/com/github/manolo8/darkbot/core/manager/MapManager.java @@ -85,13 +85,8 @@ public class MapManager implements Manager, StarSystemAPI { private final Location pingLocationCache = new Location(); public Location pingLocation = null; - private ConfigSetting disableRender; private ConfigSetting avoidRadiation; - // If the disableRender setting has been properly applied, or needs re-checking - private boolean disableRenderApplied = false; - private final Consumer invalidateRender = b -> disableRenderApplied = false; - public MapManager(Main main, PluginAPI pluginAPI, EventBrokerAPI eventBroker, @@ -119,14 +114,8 @@ public void install(BotInstaller botInstaller) { switchMap(starManager.byId(-1)); checkNextMap(-1); } - - settings3DAddress = 0; - disableRenderApplied = false; }); - this.disableRender = config.requireConfig("bot_settings.api_config.disable_render"); - this.disableRender.addListener(invalidateRender); - this.avoidRadiation = config.requireConfig("miscellaneous.avoid_radiation"); } @@ -144,7 +133,6 @@ public void tick() { updateBounds(); checkMirror(); - checkUpdateRender(); } private void update(long address) { @@ -166,23 +154,6 @@ private void update(long address) { entities.update(address); } - private void checkUpdateRender() { - if (!is3DView || disableRenderApplied) return; - - if (!ByteUtils.isValidPtr(settings3DAddress)) { - settings3DAddress = API.searchClassClosure(l -> ByteUtils.readObjectName(l).equals("Settings3D$")); - return; - } - - if (ByteUtils.isScriptableObjectValid(settings3DAddress)) { - int render = disableRender.getValue() ? 0 : 1; - int oldValue = render == 1 ? 0 : 1; - API.replaceInt(API.readLong(settings3DAddress, 0xf8) + 0x20, oldValue, render); - } - - disableRenderApplied = true; - } - private int lastNextMap; public void checkNextMap(int next) { if (next != lastNextMap) @@ -204,6 +175,10 @@ public void checkJumpCpu() { } } + public boolean is3D() { + return is3DView; + } + private void switchMap(Map next) { Map old = main.hero.map; if (old.getId() == next.getId()) return; @@ -337,8 +312,6 @@ private void updateBounds() { if (viewAddress != temp) { viewAddress = temp; - - disableRenderApplied = false; is3DView = !main.settingsManager.is2DForced() && ByteUtils.readObjectName(API.readLong(viewAddress + 208)).contains("HUD"); boundsAddress = API.readMemoryLong(viewAddress + (is3DView ? 216 : 208)); diff --git a/src/main/java/com/github/manolo8/darkbot/core/manager/PerformanceManager.java b/src/main/java/com/github/manolo8/darkbot/core/manager/PerformanceManager.java new file mode 100644 index 000000000..5d0a0bdc2 --- /dev/null +++ b/src/main/java/com/github/manolo8/darkbot/core/manager/PerformanceManager.java @@ -0,0 +1,109 @@ +package com.github.manolo8.darkbot.core.manager; + +import com.github.manolo8.darkbot.Main; +import com.github.manolo8.darkbot.core.BotInstaller; +import com.github.manolo8.darkbot.core.api.Capability; +import com.github.manolo8.darkbot.core.itf.Manager; +import com.github.manolo8.darkbot.core.utils.ByteUtils; +import eu.darkbot.api.API; +import eu.darkbot.api.config.ConfigSetting; +import eu.darkbot.api.events.EventHandler; +import eu.darkbot.api.events.Listener; +import eu.darkbot.api.managers.BotAPI; +import eu.darkbot.api.managers.ConfigAPI; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class PerformanceManager implements Manager, Listener, API.Singleton { + + private final MapManager mapManager; + private final BotAPI bot; + + private final ConfigSetting alwaysEnabled; + private final ConfigSetting disableRender; + private final ConfigSetting maxFps; + private final ConfigSetting minTick; + + private final List> listeners = new ArrayList<>(); + + @Getter private int minTickTime; + private long settings3DAddress; + private boolean renderDisabled; + + public PerformanceManager(ConfigAPI config, MapManager mapManager, BotAPI bot) { + this.mapManager = mapManager; + this.bot = bot; + + this.alwaysEnabled = config.requireConfig("bot_settings.performance.always_enabled"); + this.disableRender = config.requireConfig("bot_settings.performance.disable_render"); + this.maxFps = config.requireConfig("bot_settings.performance.max_fps"); + this.minTick = config.requireConfig("bot_settings.performance.min_tick"); + + this.alwaysEnabled.addListener(storeListener(this::toggleAll)); + this.maxFps.addListener(storeListener(this::setMaxFps)); + this.minTick.addListener(storeListener(this::setMinTickTime)); + + setMaxFps(maxFps.getValue()); + setMinTickTime(minTick.getValue()); + } + + @Override + public void install(BotInstaller botInstaller) { + botInstaller.invalid.add(invalid -> { + settings3DAddress = 0; + renderDisabled = false; + }); + } + + public void tick() { + updateRender(shouldEnable() && disableRender.getValue()); + } + + private boolean shouldEnable() { + return alwaysEnabled.getValue() || bot.isRunning(); + } + + private Consumer storeListener(Consumer listener) { + this.listeners.add(listener); + return listener; + } + + private void toggleAll(boolean ignored) { + setMaxFps(maxFps.getValue()); + setMinTickTime(minTick.getValue()); + } + + private void setMaxFps(int maxFps) { + if (Main.API.hasCapability(Capability.DIRECT_LIMIT_FPS)) + Main.API.setMaxFps(shouldEnable() ? maxFps : 0); + } + + private void setMinTickTime(int minTickTime) { + this.minTickTime = shouldEnable() ? minTickTime : 15; + } + + private void updateRender(boolean disable) { + if (!mapManager.is3D() || renderDisabled == disable) return; + + if (!ByteUtils.isValidPtr(settings3DAddress)) { + settings3DAddress = Main.API.searchClassClosure(l -> ByteUtils.readObjectName(l).equals("Settings3D$")); + return; + } + + if (ByteUtils.isScriptableObjectValid(settings3DAddress)) { + int render = disable ? 0 : 1; + int oldValue = render == 1 ? 0 : 1; + Main.API.replaceInt(Main.API.readLong(settings3DAddress, 0xf8) + 0x20, oldValue, render); + } + + renderDisabled = disable; + } + + @EventHandler + public void onRunningToggle(BotAPI.RunningToggleEvent event) { + toggleAll(event.isRunning()); + } +} diff --git a/src/main/java/com/github/manolo8/darkbot/gui/titlebar/BackpageButton.java b/src/main/java/com/github/manolo8/darkbot/gui/titlebar/BackpageButton.java index a58a8bec5..2955cffb4 100644 --- a/src/main/java/com/github/manolo8/darkbot/gui/titlebar/BackpageButton.java +++ b/src/main/java/com/github/manolo8/darkbot/gui/titlebar/BackpageButton.java @@ -36,7 +36,7 @@ public JProgressBar addProgressBar() { progressBar.setValue(0); progressBar.setIndeterminate(true); add(progressBar, BorderLayout.SOUTH); - revalidate(); + repaint(); }); return progressBar; } @@ -44,7 +44,7 @@ public JProgressBar addProgressBar() { public void removeProgressBar() { SwingUtilities.invokeLater(() -> { remove(progressBar); - revalidate(); + repaint(); }); } } diff --git a/src/main/resources/lang/strings_en.properties b/src/main/resources/lang/strings_en.properties index 941c190de..040cac623 100644 --- a/src/main/resources/lang/strings_en.properties +++ b/src/main/resources/lang/strings_en.properties @@ -305,6 +305,8 @@ config.bot_settings.custom_background.opacity=Opacity config.bot_settings.custom_background.opacity.desc=Set amount of opacity for background config.bot_settings.custom_background.image=Image config.bot_settings.custom_background.image.desc=Pick Image to set as background +config.bot_settings.performance=Performance +config.bot_settings.performance.always_enabled=Always enabled config.bot_settings.other=Other config.bot_settings.other.disable_master_password=Disable master password config.bot_settings.other.disable_master_password.desc=Enable before having added any logins, or having an empty master password