mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-10-02 02:17:22 +00:00
Added Uliratha
This commit is contained in:
@@ -4,6 +4,7 @@ import org.parabot.core.Core;
|
||||
import org.parabot.core.desc.ServerDescription;
|
||||
import org.parabot.core.lib.Library;
|
||||
import org.parabot.core.lib.javafx.JavaFX;
|
||||
import org.parabot.core.lib.naga.Naga;
|
||||
import org.parabot.core.parsers.servers.ServerParser;
|
||||
import org.parabot.core.ui.components.VerboseLoader;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
@@ -29,6 +30,7 @@ public class Environment {
|
||||
|
||||
LinkedList<Library> libs = new LinkedList<>();
|
||||
libs.add(new JavaFX());
|
||||
libs.add(new Naga());
|
||||
|
||||
for(Library lib : libs) {
|
||||
if(!lib.hasJar()) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.parabot.environment.api.utils;
|
||||
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.parabot.core.io.ProgressListener;
|
||||
import org.parabot.core.io.SizeInputStream;
|
||||
|
||||
@@ -17,6 +18,9 @@ import java.net.URLEncoder;
|
||||
*
|
||||
*/
|
||||
public class WebUtil {
|
||||
|
||||
private static JSONParser jsonParser;
|
||||
|
||||
private static String agent = "Mozilla/5.0 (Wind0ws NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
|
||||
/**
|
||||
@@ -267,4 +271,10 @@ public class WebUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static JSONParser getJsonParser() {
|
||||
if (jsonParser == null){
|
||||
jsonParser = new JSONParser();
|
||||
}
|
||||
return jsonParser;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.parabot.environment.scripts;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.ui.BotUI;
|
||||
import org.parabot.core.ui.Logger;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
import org.parabot.environment.scripts.framework.*;
|
||||
import org.parabot.environment.scripts.framework.Frameworks;
|
||||
@@ -66,14 +67,6 @@ public class Script implements Runnable {
|
||||
public final void run() {
|
||||
Context context = Context.getInstance();
|
||||
|
||||
// Core.verbose("Initializing security manager...");
|
||||
// String previousPolicy = System.getProperty("java.security.policy");
|
||||
// SecurityManager previousSecurityManager = System.getSecurityManager();
|
||||
|
||||
// System.setProperty("java.security.policy", Directories.getSettingsPath() + "/java.policy");
|
||||
// SecurityManagerDemo sm = new SecurityManagerDemo();
|
||||
// System.setSecurityManager(sm);
|
||||
|
||||
Core.verbose("Initializing script...");
|
||||
context.getServerProvider().initScript(this);
|
||||
Core.verbose("Done.");
|
||||
@@ -102,7 +95,7 @@ public class Script implements Runnable {
|
||||
frameWorkType = TYPE_OTHER;
|
||||
}
|
||||
Core.verbose("Running script...");
|
||||
System.out.println("Script started.");
|
||||
Logger.addMessage("Script started.");
|
||||
try {
|
||||
while(this.state != STATE_STOPPED) {
|
||||
if(context.getRandomHandler().checkAndRun()) {
|
||||
@@ -122,13 +115,17 @@ public class Script implements Runnable {
|
||||
}
|
||||
Core.verbose("Script stopped/finished, unloading and stopping...");
|
||||
onFinish();
|
||||
System.out.println("Script stopped.");
|
||||
Logger.addMessage("Script stopped.");
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
|
||||
if (context.getUlirathaClient() != null) {
|
||||
context.getUlirathaClient().disconnect();
|
||||
context.setUlirathaClient(null);
|
||||
}
|
||||
|
||||
BotUI.getInstance().toggleRun();
|
||||
// System.setProperty("java.security.policy", previousPolicy);
|
||||
// System.setSecurityManager(previousSecurityManager);
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
@@ -167,4 +164,7 @@ public class Script implements Runnable {
|
||||
Time.sleep(ms);
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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 org.parabot.environment.scripts.uliratha.UlirathaExecuter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -35,7 +36,7 @@ public class BDNScriptsExecuter extends ScriptExecuter {
|
||||
};
|
||||
|
||||
private int id = -1;
|
||||
|
||||
|
||||
public BDNScriptsExecuter(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
@@ -47,51 +48,52 @@ public class BDNScriptsExecuter extends ScriptExecuter {
|
||||
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);
|
||||
switch (contentType) {
|
||||
case "text/html":
|
||||
// failed to fetch script
|
||||
UILog.log("Error", "Failed to load BDN script, error: [Page returned: " + WebUtil.getContents(urlConnection) + "]", JOptionPane.ERROR_MESSAGE);
|
||||
break;
|
||||
case "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);
|
||||
|
||||
if (manager.getAccount().getApi() != null) {
|
||||
new UlirathaExecuter(manager.getAccount().getApi()).start(this.id);
|
||||
}
|
||||
} 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 ////
|
||||
break;
|
||||
default:
|
||||
UILog.log("Error", "Failed to load BDN script, error: [Unknown content type: " + contentType + "]", JOptionPane.ERROR_MESSAGE);
|
||||
break;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
UILog.log("Error", "Failed to load BDN script, post the stacktrace/error on the parabot forums.", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.parabot.environment.scripts.executers;
|
||||
|
||||
public class SecurityManagerDemo extends SecurityManager {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package org.parabot.environment.scripts.uliratha;
|
||||
|
||||
import naga.ExceptionObserver;
|
||||
import naga.NIOService;
|
||||
import naga.NIOSocket;
|
||||
import naga.SocketObserver;
|
||||
import naga.packetreader.RegularPacketReader;
|
||||
import naga.packetwriter.RegularPacketWriter;
|
||||
import org.parabot.core.ui.Logger;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author JKetelaar
|
||||
*/
|
||||
|
||||
public class UlirathaClient extends Thread {
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
private NIOSocket socket;
|
||||
private boolean connected;
|
||||
private int scriptID;
|
||||
private String api;
|
||||
private boolean valid;
|
||||
|
||||
public UlirathaClient(String host, int port, int scriptID, String api) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.scriptID = scriptID;
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
connect();
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
try {
|
||||
System.out.println("Connecting....");
|
||||
NIOService service = new NIOService();
|
||||
service.setExceptionObserver(new ExceptionObserver() {
|
||||
@Override
|
||||
public void notifyExceptionThrown(Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
if (valid) {
|
||||
reconnect();
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
socket = service.openSocket(host, port);
|
||||
socket.setPacketReader(new RegularPacketReader(4, true));
|
||||
socket.setPacketWriter(new RegularPacketWriter(4, true));
|
||||
socket.listen(new SocketObserver() {
|
||||
public void connectionOpened(NIOSocket nioSocket) {
|
||||
try {
|
||||
sendObjects(nioSocket, new Object[]{76, scriptID, api});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void packetReceived(NIOSocket socket, byte[] packet) {
|
||||
try {
|
||||
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(packet));
|
||||
int packetID = stream.readInt();
|
||||
|
||||
switch (packetID){
|
||||
case 75:
|
||||
valid = stream.readBoolean();
|
||||
if (valid) {
|
||||
Logger.addMessage("We're connected with the Uliratha server!");
|
||||
connected = true;
|
||||
}else{
|
||||
socket.close();
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(NIOSocket nioSocket, Object o) {
|
||||
|
||||
}
|
||||
|
||||
public void connectionBroken(NIOSocket nioSocket, Exception exception) {
|
||||
if (valid) {
|
||||
reconnect();
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
while (true) {
|
||||
service.selectBlocking();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (valid) {
|
||||
reconnect();
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reconnect() {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public void disconnect(){
|
||||
valid = false;
|
||||
socket.close();
|
||||
}
|
||||
|
||||
private void sendObjects(NIOSocket socket, Object[] objects) throws IOException {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
DataOutputStream dataStream = new DataOutputStream(stream);
|
||||
for (Object o : objects) {
|
||||
if (o instanceof String) {
|
||||
dataStream.writeUTF((String) o);
|
||||
} else if (o instanceof Integer) {
|
||||
dataStream.writeInt((Integer) o);
|
||||
} else if (o instanceof byte[]) {
|
||||
dataStream.write((byte[]) o);
|
||||
} else if (o instanceof Long) {
|
||||
dataStream.writeLong((Long) o);
|
||||
} else if (o instanceof Boolean) {
|
||||
dataStream.writeBoolean((Boolean) o);
|
||||
}
|
||||
}
|
||||
dataStream.flush();
|
||||
final byte[] content = stream.toByteArray();
|
||||
dataStream.close();
|
||||
socket.write(content);
|
||||
}
|
||||
|
||||
public void sendMessage(String message){
|
||||
try {
|
||||
sendObjects(socket, new Object[]{83, message});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.parabot.environment.scripts.uliratha;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author JKetelaar
|
||||
*/
|
||||
public class UlirathaExecuter {
|
||||
|
||||
private String api;
|
||||
private static boolean isVip = true;
|
||||
|
||||
public UlirathaExecuter(String api){
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void start(int scriptID){
|
||||
if (UlirathaExecuter.isVip) {
|
||||
String vipUrl = "http://bdn.parabot.org/api/v2/user/" + api + "/vip";
|
||||
JSONParser parser = new JSONParser();
|
||||
try {
|
||||
JSONObject vipObject = (JSONObject) parser.parse(WebUtil.getReader(vipUrl));
|
||||
|
||||
boolean isVip = (boolean) vipObject.get("result");
|
||||
if (isVip) {
|
||||
String serverUrl = "http://bdn.parabot.org/api/v2/clients/server";
|
||||
JSONObject serverObject = (JSONObject) parser.parse(WebUtil.getReader(serverUrl));
|
||||
JSONObject detailsObject = (JSONObject) serverObject.get("result");
|
||||
String host = (String) detailsObject.get("host");
|
||||
long port = (long) detailsObject.get("port");
|
||||
|
||||
UlirathaClient client = new UlirathaClient(host, (int) port, scriptID, api);
|
||||
client.start();
|
||||
Context.getInstance().setUlirathaClient(client);
|
||||
}else{
|
||||
UlirathaExecuter.isVip = false;
|
||||
}
|
||||
} catch (IOException | ParseException | ClassCastException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package org.parabot.environment.servers.executers;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.parsers.randoms.RandomParser;
|
||||
import org.parabot.core.ui.components.PaintComponent;
|
||||
import org.parabot.environment.scripts.executers.SecurityManagerDemo;
|
||||
import org.parabot.environment.servers.ServerProvider;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user