mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-07 16:49:10 +00:00
finished everything
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package org.parabot.environment.scripts;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.parabot.core.ui.images.Images;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Dane
|
||||
*
|
||||
*/
|
||||
public enum Category
|
||||
{
|
||||
|
||||
AGILITY, COMBAT, COOKING, CRAFTING, FARMING, FIREMAKING, FISHING, FLETCHING, HERBLORE, MAGIC, MINING, OTHER, PRAYER, RUNECRAFTING, SLAYER, SMITHING, THIEVING, UTILITY, WOODCUTTING;
|
||||
|
||||
public BufferedImage getIcon() {
|
||||
return Category.getIcon(this.name().toLowerCase());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private static HashMap<String, BufferedImage> images = new HashMap<String, BufferedImage>();
|
||||
|
||||
static {
|
||||
images.put("script", Images.getResource("/org/parabot/core/ui/images/category/script.png"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
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;
|
||||
|
||||
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 = null;
|
||||
|
||||
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,145 @@
|
||||
package org.parabot.environment.scripts;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.ui.components.BotToolbar;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
import org.parabot.environment.scripts.framework.AbstractFramework;
|
||||
import org.parabot.environment.scripts.framework.LoopTask;
|
||||
import org.parabot.environment.scripts.framework.SleepCondition;
|
||||
import org.parabot.environment.scripts.framework.Strategy;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Clisprail
|
||||
*
|
||||
*/
|
||||
public class Script implements Runnable {
|
||||
private Collection<Strategy> strategies = null;
|
||||
private int frameWorkType = 0;
|
||||
private AbstractFramework frameWork = null;
|
||||
|
||||
public static final int TYPE_STRATEGY = 0;
|
||||
public static final int TYPE_LOOP = 1;
|
||||
public static final int TYPE_OTHER = 2;
|
||||
|
||||
private int state = 0;
|
||||
public static final int STATE_RUNNING = 0;
|
||||
public static final int STATE_PAUSE = 1;
|
||||
public static final int STATE_STOPPED = 2;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
Context.resolve().getServerProvider().initScript(this);
|
||||
if(!onExecute()) {
|
||||
Context.resolve().getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
return;
|
||||
}
|
||||
|
||||
Context.resolve().setRunningScript(this);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
if(this instanceof LoopTask) {
|
||||
frameWorkType = TYPE_LOOP;
|
||||
frameWork = Frameworks.getLooper((LoopTask) this);
|
||||
} else if(strategies != null && !strategies.isEmpty()) {
|
||||
frameWorkType = TYPE_STRATEGY;
|
||||
frameWork = Frameworks.getStrategyWorker(strategies);
|
||||
} else {
|
||||
frameWorkType = TYPE_OTHER;
|
||||
}
|
||||
try {
|
||||
while(this.state != STATE_STOPPED) {
|
||||
if(this.state == STATE_PAUSE) {
|
||||
sleep(500);
|
||||
continue;
|
||||
}
|
||||
if(!frameWork.execute()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
onFinish();
|
||||
|
||||
Context.resolve().getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
Context.resolve().setRunningScript(null);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleeps until the SleepCondition is valid.
|
||||
*
|
||||
* @param conn
|
||||
* the condition.
|
||||
* @param timeout
|
||||
* the time in miliseconds before it stops sleeping.
|
||||
* @return whether it ran successfully without timing out.
|
||||
*/
|
||||
public final boolean sleep(SleepCondition conn, int timeout) {
|
||||
long start = System.currentTimeMillis();
|
||||
while (!conn.isValid()) {
|
||||
if (start + timeout < System.currentTimeMillis()) {
|
||||
return false;
|
||||
}
|
||||
Time.sleep(50);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.parabot.environment.scripts;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* A script manifest
|
||||
* @author Clisprail
|
||||
*
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ScriptManifest {
|
||||
|
||||
String author();
|
||||
|
||||
String name();
|
||||
|
||||
Category category();
|
||||
|
||||
double version();
|
||||
|
||||
String description();
|
||||
|
||||
String[] servers();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.parabot.environment.scripts.framework;
|
||||
|
||||
public abstract class AbstractFramework {
|
||||
|
||||
public abstract boolean execute();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.parabot.environment.scripts.framework;
|
||||
|
||||
public interface LoopTask {
|
||||
|
||||
public int loop();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.parabot.environment.scripts.framework;
|
||||
|
||||
public interface SleepCondition {
|
||||
|
||||
public boolean isValid();
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.parabot.environment.scripts.framework;
|
||||
|
||||
public interface Strategy {
|
||||
|
||||
public boolean activate();
|
||||
|
||||
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 server
|
||||
*
|
||||
* @author Clisprail
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ScriptLoader extends ASMClassLoader {
|
||||
private ClassPath classPath = null;
|
||||
|
||||
public ScriptLoader(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()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user