Fixed bot dialog

This commit is contained in:
JKetelaar
2015-01-31 02:18:25 +01:00
commit 000bd80699
262 changed files with 54509 additions and 0 deletions
@@ -0,0 +1,58 @@
package org.parabot.environment.scripts;
import org.parabot.core.ui.images.Images;
import java.awt.image.BufferedImage;
import java.util.HashMap;
/**
*
* Holds script categories
*
* @author Dane, Paradox
*
*/
public enum Category
{
AGILITY, COMBAT, COOKING, CRAFTING, CONSTRUCTION, DUNGEONEERING, FARMING, FIREMAKING, FISHING, FLETCHING, HERBLORE, HUNTER, MAGIC, MINIGAMES, MINING, OTHER, PRAYER, RUNECRAFTING, SLAYER, SMITHING, THIEVING, UTILITY, WOODCUTTING;
/**
* Gets image belonging to this category
* @return icon
*/
public BufferedImage getIcon() {
return Category.getIcon(this.name().toLowerCase());
}
/**
* Gets category icon image from filename
* @param s Name of the image - used for the hashmap index
* @return icon
*/
public static BufferedImage getIcon(String s) {
if (images.get(s) == null) {
images.put(s, Images.getResource("/org/parabot/core/ui/images/category/" + s + ".png"));
}
return images.get(s);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append(name().charAt(0));
b.append(name().toLowerCase().substring(1));
return new String(b);
}
/**
* Cache
*/
private static HashMap<String, BufferedImage> images = new HashMap<>();
static {
images.put("script", Images.getResource("/org/parabot/core/ui/images/category/script.png"));
}
}
@@ -0,0 +1,67 @@
package org.parabot.environment.scripts;
import java.util.Collection;
import org.parabot.environment.scripts.framework.AbstractFramework;
import org.parabot.environment.scripts.framework.LoopTask;
import org.parabot.environment.scripts.framework.Strategy;
/**
*
* Holds various script frameworks
*
* @author Everel
*
*/
public class Frameworks {
public static Looper getLooper(LoopTask loopTask) {
return new Looper(loopTask);
}
public static StrategyWorker getStrategyWorker(Collection<Strategy> strategies) {
return new StrategyWorker(strategies);
}
}
class Looper extends AbstractFramework {
private LoopTask loopTask = null;
public Looper(LoopTask loopTask) {
this.loopTask = loopTask;
}
@Override
public boolean execute() {
int sleepTime = loopTask.loop();
if(sleepTime < 0) {
return false;
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
}
class StrategyWorker extends AbstractFramework {
private Collection<Strategy> strategies;
public StrategyWorker(Collection<Strategy> strategies) {
this.strategies = strategies;
}
@Override
public boolean execute() {
for(Strategy s : strategies) {
if(s.activate()) {
s.execute();
return true;
}
}
return true;
}
}
@@ -0,0 +1,163 @@
package org.parabot.environment.scripts;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.ui.BotUI;
import org.parabot.environment.api.utils.Time;
import org.parabot.environment.scripts.framework.AbstractFramework;
import org.parabot.environment.scripts.framework.Frameworks;
import org.parabot.environment.scripts.framework.LoopTask;
import org.parabot.environment.scripts.framework.SleepCondition;
import org.parabot.environment.scripts.framework.Strategy;
import org.parabot.environment.scripts.randoms.Random;
import java.util.Collection;
/**
*
* Script template, scripts are 'add-ons' which executes various tasks in-game
*
* @author Everel
*
*/
public class Script implements Runnable {
public static final int TYPE_STRATEGY = 0;
public static final int TYPE_LOOP = 1;
public static final int TYPE_OTHER = 2;
public static final int STATE_RUNNING = 0;
public static final int STATE_PAUSE = 1;
public static final int STATE_STOPPED = 2;
private Collection<Strategy> strategies;
private AbstractFramework frameWork;
private int state;
private int frameWorkType;
public boolean onExecute() {
return true;
}
public void onFinish() {
}
public final void provide(final Collection<Strategy> strategies) {
this.strategies = strategies;
}
public final int getFrameWorkType() {
return frameWorkType;
}
public final void setFrameWork(int frameWorkType) {
if(frameWorkType < 0 || frameWorkType > 2) {
throw new RuntimeException("Invalid framework type");
}
this.frameWorkType = frameWorkType;
}
public final void setAbstractFrameWork(AbstractFramework f) {
this.frameWork = f;
}
public final void addRandom(Random random) {
Context.getInstance().getRandomHandler().addRandom(random);
}
@Override
public final void run() {
Context context = Context.getInstance();
Core.verbose("Initializing script...");
context.getServerProvider().initScript(this);
Core.verbose("Done.");
if(!onExecute()) {
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
context.getServerProvider().unloadScript(this);
this.state = STATE_STOPPED;
Core.verbose("Done.");
return;
}
Core.verbose("Detecting script framework...");
context.setRunningScript(this);
BotUI.getInstance().toggleRun();
if(this instanceof LoopTask) {
Core.verbose("Script framework detected: LoopTask");
frameWorkType = TYPE_LOOP;
frameWork = Frameworks.getLooper((LoopTask) this);
} else if(strategies != null && !strategies.isEmpty()) {
Core.verbose("Script framework detected: Strategies");
frameWorkType = TYPE_STRATEGY;
frameWork = Frameworks.getStrategyWorker(strategies);
} else {
Core.verbose("Unknown script framework: Other");
frameWorkType = TYPE_OTHER;
}
Core.verbose("Running script...");
System.out.println("Script started.");
try {
while(this.state != STATE_STOPPED) {
if(context.getRandomHandler().checkAndRun()) {
continue;
}
if(this.state == STATE_PAUSE) {
sleep(500);
continue;
}
if(!frameWork.execute()) {
break;
}
}
} catch (Throwable t) {
t.printStackTrace();
}
Core.verbose("Script stopped/finished, unloading and stopping...");
onFinish();
System.out.println("Script stopped.");
context.getServerProvider().unloadScript(this);
this.state = STATE_STOPPED;
context.setRunningScript(null);
BotUI.getInstance().toggleRun();
Core.verbose("Done.");
}
/**
* Sleeps until the SleepCondition is valid.
*
* <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)}
*
* @param conn
* the condition.
* @param timeout
* the time in miliseconds before it stops sleeping.
* @return whether it ran successfully without timing out.
*/
@Deprecated
public final boolean sleep(SleepCondition conn, int timeout) {
return Time.sleep(conn, timeout);
}
/**
* Sets the script's state
* @param state
*/
public final void setState(final int state) {
if(state < 0 || state > 2) {
throw new IllegalArgumentException("Illegal state");
}
this.state = state;
}
/**
* Sleeps for an amount of milliseconds
* @param ms
*/
public final void sleep(int ms) {
Time.sleep(ms);
}
}
@@ -0,0 +1,30 @@
package org.parabot.environment.scripts;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A script manifest, holds all script data
* @author Everel
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ScriptManifest {
String author();
String name();
Category category();
double version();
String description();
String[] servers();
boolean vip() default false;
boolean premium() default false;
}
@@ -0,0 +1,97 @@
package org.parabot.environment.scripts.executers;
import org.parabot.core.Configuration;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.Script;
import org.parabot.environment.scripts.loader.JavaScriptLoader;
import javax.swing.*;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLConnection;
/**
*
* Loads a script from the BDN
*
* @author Everel
*
*/
public class BDNScriptsExecuter extends ScriptExecuter {
private static AccountManager manager;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
BDNScriptsExecuter.manager = manager;
}
};
private int id = -1;
public BDNScriptsExecuter(final int id) {
this.id = id;
}
@Override
public void run(ThreadGroup tg) {
try {
final URLConnection urlConnection = WebUtil.getConnection(new URL(
Configuration.GET_SCRIPT + this.id), manager.getAccount().getURLUsername(), manager.getAccount().getURLPassword());
final String contentType = urlConnection.getHeaderField("Content-type");
if(contentType.equals("text/html")) {
// failed to fetch script
UILog.log("Error", "Failed to load BDN script, error: [Page returned: " + WebUtil.getContents(urlConnection) + "]", JOptionPane.ERROR_MESSAGE);
} else if(contentType.equals("application/jar")) {
//// JAR LOADING PART ////////
// succesfull request, jar returned
final ClassPath classPath = new ClassPath();
classPath.addJar(urlConnection);
final JavaScriptLoader loader = new JavaScriptLoader(classPath);
final String[] scriptClasses = loader.getScriptClassNames();
if(scriptClasses == null || scriptClasses.length == 0) {
UILog.log("Error", "Failed to load BDN script, error: [No script found in jar file.]", JOptionPane.ERROR_MESSAGE);
return;
} else if(scriptClasses.length > 1) {
UILog.log("Error", "Failed to load BDN script, error: [Multiple scripts found in jar file.]");
return;
}
final String className = scriptClasses[0];
try {
final Class<?> scriptClass = loader.loadClass(className);
final Constructor<?> con = scriptClass.getConstructor();
final Script script = (Script) con.newInstance();
super.finalize(tg, script);
} catch (NoClassDefFoundError | ClassNotFoundException ignored) {
UILog.log("Error", "Failed to load BDN script, error: [This server provider does not support this script]", JOptionPane.ERROR_MESSAGE);
} catch (Throwable t) {
t.printStackTrace();
UILog.log("Error", "Failed to load BDN script, post the stacktrace/error on the parabot forums.", JOptionPane.ERROR_MESSAGE);
}
//// END JAR LOADING ////
} else {
UILog.log("Error", "Failed to load BDN script, error: [Unknown content type: " + contentType + "]", JOptionPane.ERROR_MESSAGE);
}
} catch (Throwable t) {
t.printStackTrace();
UILog.log("Error", "Failed to load BDN script, post the stacktrace/error on the parabot forums.", JOptionPane.ERROR_MESSAGE);
}
}
}
@@ -0,0 +1,30 @@
package org.parabot.environment.scripts.executers;
import java.lang.reflect.Constructor;
import org.parabot.environment.scripts.Script;
/**
*
* Loads a locally stored script
*
* @author Everel
*
*/
public class LocalScriptExecuter extends ScriptExecuter {
private Constructor<?> scriptConstructor;
public LocalScriptExecuter(final Constructor<?> scriptConstructor) {
this.scriptConstructor = scriptConstructor;
}
@Override
public void run(ThreadGroup tg) {
try {
super.finalize(tg, (Script) scriptConstructor.newInstance(new Object[] { }));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@@ -0,0 +1,26 @@
package org.parabot.environment.scripts.executers;
import org.parabot.environment.scripts.Script;
/**
*
* Executes a script
*
* @author Everel
*
*/
public abstract class ScriptExecuter {
public abstract void run(final ThreadGroup tg);
/**
* Start script.
* @param tg
* @param script
*/
public final void finalize(final ThreadGroup tg, final Script script) {
new Thread(tg, script).start();
}
}
@@ -0,0 +1,18 @@
package org.parabot.environment.scripts.framework;
/**
*
* Abstract framework for a script
*
* @author Everel
*
*/
public abstract class AbstractFramework {
/**
* Executes this frame
* @return <b>true</b> if it should keep executing this framework, otherwise <b>false</b>.
*/
public abstract boolean execute();
}
@@ -0,0 +1,63 @@
package org.parabot.environment.scripts.framework;
import java.util.Collection;
/**
*
* Holds various script frameworks
*
* @author Everel
*
*/
public class Frameworks {
public static Looper getLooper(LoopTask loopTask) {
return new Looper(loopTask);
}
public static StrategyWorker getStrategyWorker(Collection<Strategy> strategies) {
return new StrategyWorker(strategies);
}
}
class Looper extends AbstractFramework {
private LoopTask loopTask = null;
public Looper(LoopTask loopTask) {
this.loopTask = loopTask;
}
@Override
public boolean execute() {
int sleepTime = loopTask.loop();
if(sleepTime < 0) {
return false;
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
}
class StrategyWorker extends AbstractFramework {
private Collection<Strategy> strategies;
public StrategyWorker(Collection<Strategy> strategies) {
this.strategies = strategies;
}
@Override
public boolean execute() {
for(Strategy s : strategies) {
if(s.activate()) {
s.execute();
return true;
}
}
return true;
}
}
@@ -0,0 +1,18 @@
package org.parabot.environment.scripts.framework;
/**
*
* A LoopTask interface is used to keep calling the loop method which should return the sleep time
*
* @author Everel
*
*/
public interface LoopTask {
/**
*
* @return sleepTime in ms
*/
public int loop();
}
@@ -0,0 +1,18 @@
package org.parabot.environment.scripts.framework;
/**
* Keeps sleeping till a condition is valid
*
* @author Everel
*
*/
public interface SleepCondition {
/**
* Determine if condition is valid
* @return <b>true</b> if valid, otherwise <b>false</b>.
*/
public boolean isValid();
}
@@ -0,0 +1,22 @@
package org.parabot.environment.scripts.framework;
/**
* Strategy framework for scripts
*
* @author Everel
*
*/
public interface Strategy {
/**
* Whether to activate this strategy
* @return <b>true</b> if this strategy should be executed, otherwise <b>false</b>.
*/
public boolean activate();
/**
* Executes this strategy
*/
public void execute();
}
@@ -0,0 +1,43 @@
package org.parabot.environment.scripts.loader;
import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.tree.ClassNode;
import org.parabot.core.asm.ASMClassLoader;
import org.parabot.core.classpath.ClassPath;
import org.parabot.environment.scripts.Script;
/**
*
* An environment to load a script
*
* @author Everel
*
*
*/
public class JavaScriptLoader extends ASMClassLoader {
private ClassPath classPath;
public JavaScriptLoader(ClassPath classPath) {
super(classPath);
this.classPath = classPath;
}
/**
* Gets all classes that extends ServerProvider
* @return string array of class names that extends ServerProvider
*/
public final String[] getScriptClassNames() {
final List<String> classNames = new ArrayList<String>();
for (ClassNode c : classPath.classes.values())
if (c.superName.replace('/', '.').equals(
Script.class.getName())) {
classNames.add(c.name.replace('/', '.'));
}
return classNames.toArray(new String[classNames.size()]);
}
}
@@ -0,0 +1,33 @@
package org.parabot.environment.scripts.randoms;
/**
*
* @author Everel
*
*/
public interface Random {
/**
* Determines whether this random should activate
* @return <b>true</b> if this random should activate
*/
public boolean activate();
/**
* Executes this random
*/
public void execute();
/**
* Returns the name of the random
* @return Name of the random
*/
public String getName();
/**
* Returns the name of the server which the random is made for
* @return Name of the server
*/
public String getServer();
}
@@ -0,0 +1,107 @@
package org.parabot.environment.scripts.randoms;
import org.parabot.core.Core;
import java.util.ArrayList;
/**
*
* @author Everel
*
*/
public class RandomHandler {
private ArrayList<Random> randoms;
/**
* The randoms that will actually run
*/
private ArrayList<Random> activeRandoms;
public RandomHandler() {
this.randoms = new ArrayList<>();
this.activeRandoms = new ArrayList<>();
}
/**
* Adds a random to the random list
* @param random The random that will be added to the arraylist
*/
public void addRandom(Random random) {
if(random == null) {
throw new NullPointerException("Null random");
}
for(Random r : randoms) {
if(r.getClass() == random.getClass()) {
Core.verbose("Ignored added random, duplicate.");
return;
}
}
randoms.add(random);
setActive(random);
}
/**
* Adds a random to the active randoms
* @param random
*/
public void setActive(Random random){
this.activeRandoms.add(random);
}
/**
* Adds a random to the active randoms
* @param random
*/
public void setActive(String random){
for (Random r : this.randoms){
if (r.getName().equalsIgnoreCase(random.toLowerCase())){
this.activeRandoms.add(r);
}
}
}
/**
* Sets the whole random arraylist to the arraylist given as argument
* @param randoms The new random arraylist
*/
public void setRandoms(ArrayList<Random> randoms){
this.randoms = randoms;
}
/**
* Clears all added randoms
*/
public void clearRandoms() {
this.randoms.clear();
}
/**
* Clears all active randoms
*/
public void clearActiveRandoms(){
this.activeRandoms.clear();
}
/**
* Checks if random occurs and runs it
* @return returns <b>true</b> if a random has been executed, otherwise <b>false</b>
*/
public boolean checkAndRun() {
for(Random r : this.activeRandoms) {
if(r.activate()) {
Core.verbose("Running random '" + r.getName() + "'.");
r.execute();
return true;
}
}
return false;
}
public ArrayList<Random> getRandoms(){
return this.randoms;
}
public ArrayList<Random> getActiveRandoms(){
return this.activeRandoms;
}
}