Skip to content
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

API Repair #224

Merged
merged 21 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5b0566a
Changed the arg handling in Application to use a sort of pre-processo…
GigiaJ Aug 2, 2022
eae5f0d
Moved the CLIHandling to its own class and adjusted the statements to…
GigiaJ Aug 2, 2022
850a55c
Merge remote-tracking branch 'origin/dev' into dev
GigiaJ Aug 2, 2022
5c6ec22
Re-add support for multi-classloaders for each bot instance
GigiaJ Aug 3, 2022
c07d7f4
Fix incorrect cases in switch since they weren't adjusted to lowercase
GigiaJ Aug 3, 2022
6109895
Change RuneLite launch to non-static
GigiaJ Aug 3, 2022
3e63c29
Add intellij run config for headless run
GigiaJ Aug 3, 2022
382033f
Merge branch 'OSRSB:master' into apibugfix
GigiaJ Aug 3, 2022
345a08f
Merge pull request #221 from GigiaJ/dev
GigiaJ Aug 3, 2022
e83e022
Merge pull request #223 from GigiaJ/apibugfix
GigiaJ Aug 3, 2022
661d94d
Added stop script to the CLI Handler
GigiaJ Aug 6, 2022
181d975
Removed leftover print statements and condensed an instanceof
GigiaJ Aug 6, 2022
3988498
Updated to be a URLClassloader
GigiaJ Aug 6, 2022
773c9ea
Removed extra print statements
GigiaJ Aug 6, 2022
5b0b606
Update run config
GigiaJ Aug 6, 2022
6a52e1c
Reverted launch usage and added stopscript
GigiaJ Aug 6, 2022
750259d
Adjusted method context loading
GigiaJ Aug 6, 2022
78bf7b7
Adjusted string check for class loading
GigiaJ Aug 6, 2022
d0f02e9
Finished implementation design for multi-bot
GigiaJ Aug 6, 2022
1d06ef6
Merge remote-tracking branch 'origin/apibugfix' into apibugfix
GigiaJ Aug 6, 2022
caad839
Merge pull request #229 from GigiaJ/apibugfix
GigiaJ Aug 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .run/headlessBotRun.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="headlessBotRun" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="17" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="net.runelite.rsb.botLauncher.Application" />
<module name="OSRSBot.main" />
<option name="PROGRAM_PARAMETERS" value="--bot-runelite --headless --developer-mode" />
<option name="VM_PARAMETERS" value="-debug" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
2 changes: 1 addition & 1 deletion src/main/java/net/runelite/client/modified/RuneLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class RuneLite extends net.runelite.client.RuneLite {
* @param args The args for the program (plus any extras added on)
* @throws Exception Any exception the client or RuneLite might throw
*/
public static void launch(String[] args) throws Exception {
public void launch(String[] args) throws Exception {
Locale.setDefault(Locale.ENGLISH);
OptionParser parser = new OptionParser();
ArgumentAcceptingOptionSpec<?>[] optionSpecs = handleParsing(parser);
Expand Down
110 changes: 60 additions & 50 deletions src/main/java/net/runelite/rsb/botLauncher/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;

@Slf4j
public class Application {

static BotLiteInterface[] bots = new BotLiteInterface[]{};
static String[] programArgs = new String[]{};
static ArgumentPreParser preParser;

/**
* Parses the command-line arguments and then passes the parsed arguments in the form of the parser, optionSpecs,
Expand All @@ -28,32 +26,11 @@ public class Application {
* @throws Throwable Any error that might be thrown
*/
public static void main(final String[] args) throws Throwable {
ArrayList<String> argList = new ArrayList<>(Arrays.asList(args));
if (argList.contains("--bot-runelite")) {
argList.remove("--bot-runelite");
programArgs = argList.toArray(new String[]{});
addBot(argList.contains("--headless"));
preParser = new ArgumentPreParser(args);
if (preParser.contains("--bot-runelite")) {
addBot(preParser.contains("--headless"));
checkForCacheAndLoad();
new Thread(() -> {
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
String[] command = input.nextLine().split(" ");
System.out.println(Arrays.toString(command));
if (command[0].equalsIgnoreCase("runScript")) {
BotLiteInterface botInterface = Application.getBots()[Integer.parseInt(command[1])];
botInterface.runScript(command[2], command[3]);

}
if (command[0].equalsIgnoreCase("addBot")) {
addBot(true);
}
if (command[0].equalsIgnoreCase("checkState")) {
for (BotLiteInterface botInstance : bots) {
System.out.println(botInstance.getClass().getClassLoader());
}
}
}
}).start();
CLIHandler.handleCLI();
} else {
net.runelite.client.RuneLite.main(args);
}
Expand Down Expand Up @@ -98,8 +75,6 @@ private static void checkForCacheAndLoad() throws IOException {

public static void setBot(int index) {
BotLiteInterface bot = getBots()[index];


}

/**
Expand All @@ -125,30 +100,28 @@ public static BotLiteInterface getBot(Object o) {
* @param headless To run the bot headless or not
*/
public static void addBot(boolean headless) {
BotLiteInterface bot;
String[] args = new String[programArgs.length + 1];
if (headless) {
System.arraycopy(programArgs, 0, args, 0, programArgs.length);
args[programArgs.length] = "--headless";
}
else {
args = programArgs;
}
BotClassLoader loader = new BotClassLoader("BotLoader" + bots.length);
// Class<?> c;
BotLiteInterface bot = null;

try {
// c = loader.loadClass("net.runelite.client.modified.RuneLite");
// bot = (BotLiteInterface) c.getConstructor().newInstance();
BotLite.launch(args);
BotLiteInterface[] update = new BotLiteInterface[bots.length + 1];
for (int i = 0; i < bots.length; i++) {
update[i] = bots[i];
if (headless) {
preParser.add("--headless");
BotClassLoader loader = new BotClassLoader("BotLoader" + bots.length + 1);
Class<?> c;
c = loader.loadClass("net.runelite.rsb.botLauncher.BotLite");
bot = (BotLiteInterface) c.getConstructor().newInstance();
} else {
preParser.remove("--headless");
bot = new BotLite();
}
update[bots.length] = BotLite.getInjector().getInstance(BotLite.class);
bots = update;
bot.launch(preParser.asArgs());
} catch (Exception e) {
log.error("Error while starting bot", e);
}

BotLiteInterface[] update = new BotLiteInterface[bots.length + 1];
System.arraycopy(bots, 0, update, 0, bots.length);
update[bots.length] = bot;
bots = update;
}

/**
Expand All @@ -159,4 +132,41 @@ public static BotLiteInterface[] getBots() {
return bots;
}

/**
* A class to handle bot related arguments before passing them off to RuneLite
*/
private static class ArgumentPreParser extends ArrayList<String> {

/**
* Creates a handler for the arguments before they're sent off to build the bot instance of RuneLite
* @param args The command line arguments for the program
*/
public ArgumentPreParser(String[] args) {
super(List.of(args));
}

/**
* Returns the program argument ArrayList as a built-in array
* @return a built-in String array containing program arguments
*/
public String[] asArgs() {
return this.toArray(new String[0]);
}

/**
* Modifies the contains function to remove elements upon checking if they exist within, but return the whether
* they did before-hand.
* As CLI args for the bot will be used once this will only pass to RuneLite arguments meant for it.
*/
@Override
public boolean contains(Object o) {
int index = indexOf(o);
boolean within = index >= 0;
if (within)
this.remove(index);
return within;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public class BotClassLoader extends ClassLoader {

Expand Down Expand Up @@ -57,7 +54,7 @@ public Class<?> findClass(String name) throws ClassNotFoundException
Class<?> loaded = realParent.findLoaded(name);
if( loaded != null )
return realParent.findClass(name);
if (name.contains("java.")) {
if (name.startsWith("java.")) {
return ClassLoader.getPlatformClassLoader().loadClass(name);
}
try {
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/net/runelite/rsb/botLauncher/BotLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,33 +302,28 @@ public void init(boolean startClientBare) throws Exception {

public BotLite() throws Exception {
im = new InputManager(this);
Executors.newSingleThreadScheduledExecutor().submit(() -> {
while(this.getClient() == null){}
if (getPanelSize() != null) {
final Dimension size = getPanelSize();
backBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
}
});
psh = new PassiveScriptHandler(this);
eventManager = new EventManager();
sh = new ScriptHandler(this);
bh = new BreakHandler(this);
paintEvent = new PaintEvent();
textPaintEvent = new TextPaintEvent();
listeners = new TreeMap<>();
// Botplugin botplugin = new Botplugin(injector);
// pluginManager.add(botplugin);
setMethodContext();

eventManager.start();


Executors.newSingleThreadScheduledExecutor().submit(() -> {
while(this.getClient() == null){}
setMethodContext();
sh.init();
if (getPanelSize() != null) {
final Dimension size = getPanelSize();
backBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
}
});
}

public void runScript(String account, String scriptName) {
getInjectorInstance().setAccount(account);
System.out.println(getInjectorInstance().getAccountName());
ScriptSelector ss = new ScriptSelector(getInjectorInstance());
ss.load();
ScriptDefinition def = ss.getScripts().stream().filter(x -> x.name.replace(" ", "").equals(scriptName)).findFirst().get();
Expand All @@ -339,4 +334,8 @@ public void runScript(String account, String scriptName) {
}
}

public void stopScript() {
sh.stopScript();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public interface BotLiteInterface {

void runScript(String account, String scriptName);

// void launch(String[]s) throws Exception;
void stopScript();

void launch(String[] args) throws Exception;

void init(boolean startClientBare) throws Exception;

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/net/runelite/rsb/botLauncher/CLIHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.runelite.rsb.botLauncher;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;

import static net.runelite.rsb.botLauncher.Application.*;

public class CLIHandler {

/**
* Starts a new thread which handles the command line arguments passed while the program is running.
* The switch case provides an easy-to-read implementation in which commands are available for usage.
*/
public static void handleCLI() {
Scanner input = new Scanner(System.in);
new Thread(() -> {
while(input.hasNext()) {
BotLiteInterface botInterface;
String[] command = input.nextLine().split(" ");
System.out.println(Arrays.toString(command));
switch (command[0].toLowerCase()) {
case "runscript":
botInterface = Application.getBots()[Integer.parseInt(command[1])];
botInterface.runScript(command[2], command[3]);
break;
case "stopscript":
botInterface = Application.getBots()[Integer.parseInt(command[1])];
botInterface.stopScript();
break;
case "addbot":
addBot(false);
break;
case "checkstate":
for (BotLiteInterface botInstance : bots) {
System.out.println(Application.class.getClassLoader());
System.out.println(botInstance.getClass().getClassLoader());
/*
Set<Thread> threadSet
= Thread.getAllStackTraces().keySet();
// iterating over the threads to get the names of
// all the active threads
for (Thread x : threadSet) {
System.out.println(x.getName());
}
*/
}
break;
default:
System.out.println("Invalid command");
break;
}
}
}).start();
}
}
2 changes: 0 additions & 2 deletions src/main/java/net/runelite/rsb/plugin/ScriptSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ public void load() {
scripts.clear();
File testFile = new File(GlobalConfiguration.Paths.getScriptsSourcesDirectory());
FileScriptSource test = new FileScriptSource(testFile);
test.list().forEach(System.out::println);
deleteTemporaryFiles();
scripts.addAll(SRC_BUNDLED.list());
scripts.addAll(SRC_PRECOMPILED.list());
scripts.addAll(SRC_SOURCES.list());
scripts.forEach(System.out::println);
//generateTestScripts();
//scripts.addAll(SRC_TEST.list());
if (search != null)
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/net/runelite/rsb/service/FileScriptSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ public List<ScriptDefinition> list() {


public Script load(ScriptDefinition def) throws ServiceException {
if (!(def instanceof FileScriptDefinition)) {
if (!(def instanceof FileScriptDefinition fsd)) {
throw new IllegalArgumentException("Invalid definition!");
}
FileScriptDefinition fsd = (FileScriptDefinition) def;
try {
return fsd.clazz.asSubclass(Script.class).newInstance();
} catch (Exception ex) {
Expand All @@ -84,15 +83,11 @@ private void load(ClassLoader loader, LinkedList<ScriptDefinition> scripts, File
if (file.isDirectory()) {
if (!file.getName().startsWith(".")) {
for (File f : file.listFiles()) {
System.out.println("TEST1");
System.out.println(file.toURI());
load(loader, scripts, f, prefix + file.getName() + ".");
}
}
} else {
String name = prefix + file.getName();
System.out.println("TEST2");
System.out.println(file.toURI());
String ext = ".class";
if (name.endsWith(ext) && !name.startsWith(".") && !name.contains("!") && !name.contains("$")) {
name = name.substring(0, name.length() - ext.length());
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/runelite/rsb/service/ScriptClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.net.URLClassLoader;

/**
* @author GigiaJ
*/
class ScriptClassLoader extends ClassLoader {
class ScriptClassLoader extends URLClassLoader {

private final URL base;

public ScriptClassLoader(URL url) {
super(new URL[]{url}, ScriptClassLoader.class.getClassLoader());
this.base = url;
}

Expand Down