Skip to content

Commit

Permalink
Refector performance management into PerformanceManager (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Punikekk authored Nov 25, 2023
1 parent 11ea884 commit 8e24f8c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 48 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/github/manolo8/darkbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -262,6 +266,7 @@ private void validTick() {
guiManager.tick();
statsManager.tick();
repairManager.tick();
performanceManager.tick();
API.tick();

tickingModule = running && guiManager.canTickModule();
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/github/manolo8/darkbot/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<HookAdapter.Flag> DARK_HOOK_FLAGS = EnumSet.allOf(HookAdapter.Flag.class);
Expand Down Expand Up @@ -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;
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +59,6 @@ public class GameAPIImpl<
protected final String version;

private final ConfigAPI config;
private final Consumer<Integer> 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
Expand Down Expand Up @@ -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<Integer> 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<Boolean> useProxy = config.requireConfig("bot_settings.api_config.use_proxy");
if (useProxy.getValue() || OSUtil.isWindows7OrLess())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,8 @@ public class MapManager implements Manager, StarSystemAPI {
private final Location pingLocationCache = new Location();
public Location pingLocation = null;

private ConfigSetting<Boolean> disableRender;
private ConfigSetting<Boolean> avoidRadiation;

// If the disableRender setting has been properly applied, or needs re-checking
private boolean disableRenderApplied = false;
private final Consumer<Boolean> invalidateRender = b -> disableRenderApplied = false;

public MapManager(Main main,
PluginAPI pluginAPI,
EventBrokerAPI eventBroker,
Expand Down Expand Up @@ -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");
}

Expand All @@ -144,7 +133,6 @@ public void tick() {

updateBounds();
checkMirror();
checkUpdateRender();
}

private void update(long address) {
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> alwaysEnabled;
private final ConfigSetting<Boolean> disableRender;
private final ConfigSetting<Integer> maxFps;
private final ConfigSetting<Integer> minTick;

private final List<Consumer<?>> 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 <T> Consumer<T> storeListener(Consumer<T> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public JProgressBar addProgressBar() {
progressBar.setValue(0);
progressBar.setIndeterminate(true);
add(progressBar, BorderLayout.SOUTH);
revalidate();
repaint();
});
return progressBar;
}

public void removeProgressBar() {
SwingUtilities.invokeLater(() -> {
remove(progressBar);
revalidate();
repaint();
});
}
}
2 changes: 2 additions & 0 deletions src/main/resources/lang/strings_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e24f8c

Please sign in to comment.