-
-
Save anonymous/50ad5efb7e7cf3a3da8a097eadbd276d to your computer and use it in GitHub Desktop.
Dreambot Auto Script Restarter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal; | |
import org.dreambot.api.script.AbstractScript; | |
import org.dreambot.api.script.Category; | |
import org.dreambot.api.script.ScriptManifest; | |
import org.royal.events.EventBus; | |
import org.royal.frame.ScriptFrame; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
@ScriptManifest(category = Category.UTILITY, name = "Auto Script Restarter", author = "Royal Family", version = 1.0) | |
public class App extends AbstractScript { | |
@Override | |
public void onStart() { | |
ScriptStore.setContainer(this); | |
ScriptFrame.create(); | |
super.onStart(); | |
} | |
@Override | |
public void onExit() { | |
EventBus.getInstance().onStopClicked(); | |
super.onExit(); | |
} | |
@Override | |
public void onPause() { | |
EventBus.getInstance().onPauseClicked(); | |
super.onPause(); | |
} | |
@Override | |
public void onResume() { | |
EventBus.getInstance().onResumeClicked(); | |
super.onResume(); | |
} | |
@Override | |
public int onLoop() { | |
return Integer.MAX_VALUE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.events; | |
import org.royal.events.impl.IEventListener; | |
import org.royal.events.impl.IFrameListener; | |
import org.royal.events.impl.IScriptListener; | |
import org.royal.script.ScriptContainer; | |
import java.util.HashSet; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class EventBus implements IFrameListener, IScriptListener { | |
private static EventBus instance; | |
public static synchronized EventBus getInstance() { | |
if(instance == null) | |
instance = new EventBus(); | |
return instance; | |
} | |
private final HashSet<IEventListener> scriptListeners; | |
private final HashSet<IEventListener> frameListeners; | |
private EventBus() { | |
this.scriptListeners = new HashSet<>(); | |
this.frameListeners = new HashSet<>(); | |
} | |
public EventBus registerScriptListener(IScriptListener listener) { | |
if(listener == null) | |
return this; | |
this.scriptListeners.add(listener); | |
return this; | |
} | |
public EventBus registerFrameListener(IFrameListener listener) { | |
if(listener == null) | |
return this; | |
this.frameListeners.add(listener); | |
return this; | |
} | |
@Override | |
public void onRunClicked() { | |
this.frameListeners.forEach(listener -> | |
((IFrameListener) listener).onRunClicked()); | |
} | |
@Override | |
public void onPauseClicked() { | |
this.frameListeners.forEach(listener -> | |
((IFrameListener) listener).onPauseClicked()); | |
} | |
@Override | |
public void onStopClicked() { | |
this.frameListeners.forEach(listener -> | |
((IFrameListener) listener).onStopClicked()); | |
} | |
@Override | |
public void onResumeClicked() { | |
this.frameListeners.forEach(listener -> | |
((IFrameListener) listener).onResumeClicked()); | |
} | |
@Override | |
public void onScriptStart(ScriptContainer script) { | |
this.scriptListeners.forEach(listener -> | |
((IScriptListener) listener).onScriptStart(script)); | |
} | |
@Override | |
public void onScriptStop(ScriptContainer script) { | |
this.scriptListeners.forEach(listener -> | |
((IScriptListener) listener).onScriptStop(script)); | |
} | |
@Override | |
public void onScriptPause(ScriptContainer script) { | |
this.scriptListeners.forEach(listener -> | |
((IScriptListener) listener).onScriptPause(script)); | |
} | |
@Override | |
public void onScriptRestart(ScriptContainer script) { | |
this.scriptListeners.forEach(listener -> | |
((IScriptListener) listener).onScriptRestart(script)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.events.impl; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public interface IEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.events.impl; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public interface IEventListener {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.events.impl; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public interface IFrameListener extends IEventListener { | |
void onRunClicked(); | |
void onPauseClicked(); | |
void onStopClicked(); | |
void onResumeClicked(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.events.impl; | |
import org.royal.script.ScriptContainer; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public interface IScriptListener extends IEventListener { | |
void onScriptStart(ScriptContainer script); | |
void onScriptStop(ScriptContainer script); | |
void onScriptPause(ScriptContainer script); | |
void onScriptRestart(ScriptContainer script); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.frame; | |
import org.royal.events.EventBus; | |
import org.royal.events.impl.IScriptListener; | |
import org.royal.script.ScriptContainer; | |
import javax.swing.*; | |
import javax.swing.border.EmptyBorder; | |
import java.awt.*; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptFrame extends JFrame implements IScriptListener { | |
private JPanel contentPane; | |
private JButton pauseScriptButton; | |
private JButton stopScriptButton; | |
private JButton runScriptButton; | |
private JLabel status; | |
private JLabel restarts; | |
private int totalRestarts; | |
public static void create() { | |
EventQueue.invokeLater(() -> { | |
try { | |
ScriptFrame frame = new ScriptFrame(); | |
EventBus.getInstance().registerScriptListener(frame); | |
new ScriptFrameService(frame); | |
frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
/** | |
* Create the org.royal.frame. | |
*/ | |
private ScriptFrame() { | |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | |
setBounds(100, 100, 260, 282); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
runScriptButton = new JButton("Run Script"); | |
runScriptButton.setBounds(16, 18, 192, 55); | |
contentPane.add(runScriptButton); | |
stopScriptButton = new JButton("Stop Script"); | |
stopScriptButton.setBounds(16, 86, 192, 55); | |
contentPane.add(stopScriptButton); | |
pauseScriptButton = new JButton("Pause Script"); | |
pauseScriptButton.setBounds(16, 153, 192, 55); | |
contentPane.add(pauseScriptButton); | |
status = new JLabel("Status: No script running."); | |
status.setBounds(16, 224, 192, 16); | |
contentPane.add(status); | |
restarts = new JLabel("Total Restarts: " + totalRestarts); | |
restarts.setBounds(16, 239, 192, 16); | |
contentPane.add(restarts); | |
runScriptButton.addActionListener(e -> EventBus.getInstance().onRunClicked()); | |
pauseScriptButton.addActionListener(e -> { | |
if(pauseScriptButton.getText().equals("Resume Script")) { | |
EventBus.getInstance().onResumeClicked(); | |
pauseScriptButton.setText("Pause Script"); | |
return; | |
} | |
EventBus.getInstance().onPauseClicked(); | |
pauseScriptButton.setText("Resume Script"); | |
}); | |
stopScriptButton.addActionListener(e -> EventBus.getInstance().onStopClicked()); | |
} | |
@Override | |
public void onScriptStart(ScriptContainer script) { | |
System.out.println("Starting at gui..."); | |
runScriptButton.setEnabled(false); | |
stopScriptButton.setEnabled(true); | |
pauseScriptButton.setEnabled(true); | |
status.setText("Status: " + "Script running."); | |
} | |
@Override | |
public void onScriptStop(ScriptContainer script) { | |
runScriptButton.setEnabled(true); | |
stopScriptButton.setEnabled(false); | |
pauseScriptButton.setEnabled(false); | |
status.setText("Status: " + "No script running."); | |
} | |
@Override | |
public void onScriptPause(ScriptContainer script) { | |
status.setText("Status: " + "Script paused."); | |
} | |
@Override | |
public void onScriptRestart(ScriptContainer script) { | |
status.setText("Status: " + "Script restarting."); | |
totalRestarts++; | |
restarts.setText("Total Restarts: " + totalRestarts); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.frame; | |
import org.royal.ScriptStore; | |
import org.royal.events.EventBus; | |
import org.royal.events.impl.IFrameListener; | |
import org.royal.script.ScriptContainer; | |
import org.royal.script.ScriptLoader; | |
import org.royal.script.ScriptManager; | |
import org.royal.watcher.FileWatcher; | |
import javax.swing.*; | |
import javax.swing.filechooser.FileNameExtensionFilter; | |
import java.io.File; | |
import java.nio.file.Paths; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptFrameService implements IFrameListener { | |
private ScriptLoader loader; | |
private final JFrame frame; | |
private final FileWatcher watcher; | |
public ScriptFrameService(JFrame frame) { | |
this.loader = new ScriptLoader(); | |
this.frame = frame; | |
this.watcher = new FileWatcher(loader); | |
EventBus.getInstance().registerFrameListener(this); | |
} | |
@Override | |
public void onRunClicked() { | |
File jar = getJarFile(); | |
if(jar == null) | |
return; | |
try { | |
final ScriptContainer container = loader.loadScriptFromJar(jar.getPath()); | |
loader.startScript(container); | |
watcher.start(container.getScriptAsFile()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onPauseClicked() { | |
final ScriptManager manager = ScriptStore.getCurrentScriptManager(); | |
if(manager == null) | |
return; | |
manager.pause(); | |
} | |
@Override | |
public void onStopClicked() { | |
final ScriptManager manager = ScriptStore.getCurrentScriptManager(); | |
if(manager == null) | |
return; | |
manager.stop(); | |
} | |
@Override | |
public void onResumeClicked() { | |
final ScriptManager manager = ScriptStore.getCurrentScriptManager(); | |
if(manager == null) | |
return; | |
manager.resume(); | |
} | |
private File getJarFile() { | |
JFileChooser chooser = new JFileChooser(); | |
FileNameExtensionFilter filter = new FileNameExtensionFilter( | |
"Jar file", "jar"); | |
chooser.setFileFilter(filter); | |
int returnVal = chooser.showOpenDialog(frame); | |
if (returnVal != JFileChooser.APPROVE_OPTION) { | |
return null; | |
} | |
final File file = chooser.getSelectedFile(); | |
if (file == null) { | |
return null; | |
} | |
return file; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.script.impl; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public enum ScriptState { | |
RUNNING, PAUSED, STOPPED | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.script; | |
import org.dreambot.api.script.AbstractScript; | |
import java.io.File; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptContainer { | |
public File scriptAsFile; | |
public AbstractScript script; | |
public ScriptContainer(File scriptAsFile, AbstractScript script) { | |
this.scriptAsFile = scriptAsFile; | |
this.script = script; | |
} | |
public File getScriptAsFile() { | |
return scriptAsFile; | |
} | |
public AbstractScript getScript() { | |
return script; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.script; | |
import org.dreambot.api.script.AbstractScript; | |
import org.royal.ScriptStore; | |
import java.io.File; | |
import java.lang.reflect.Constructor; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
import java.util.Enumeration; | |
import java.util.jar.JarEntry; | |
import java.util.jar.JarFile; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptLoader { | |
private ScriptManager manager; | |
public ScriptLoader() { | |
this.manager = new ScriptManager(); | |
} | |
public ScriptContainer loadScriptFromJar(String path) | |
throws Exception { | |
final File file = new File(path); | |
final JarFile jar = new JarFile(file); | |
final Enumeration entries = jar.entries(); | |
while (entries.hasMoreElements()) { | |
final JarEntry entry = (JarEntry) entries.nextElement(); | |
String name = entry.getName(); | |
final ClassLoader loader = URLClassLoader.newInstance( | |
new URL[]{file.toURI().toURL()}, | |
getClass().getClassLoader() | |
); | |
if (!name.endsWith(".class")) { | |
continue; | |
} | |
name = name.replace("/", ".").replace(".class", ""); | |
try { | |
final Class<?> clazz = Class.forName(name, true, loader); | |
final Class<? extends AbstractScript> runClass = clazz.asSubclass(AbstractScript.class); | |
final Constructor<? extends AbstractScript> constructor = runClass.getConstructor(); | |
final AbstractScript script = constructor.newInstance(); | |
return new ScriptContainer(file, script); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
//if this is thrown, then the class is loaded | |
//does not extend royal org.royal.script, so we cannot run it. | |
//ignore | |
} | |
} | |
return null; | |
} | |
public void startScript(ScriptContainer script) { | |
if(script == null) | |
return; | |
script.getScript().registerMethodContext(ScriptStore.getContainer().getClient()); | |
manager.start(script); | |
ScriptStore.setCurrentScriptManager(manager); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.script; | |
import org.royal.events.EventBus; | |
import org.royal.script.impl.ScriptState; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.Executors; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptManager { | |
private ScriptState state; | |
private ScriptContainer container; | |
private Executor executor = Executors.newSingleThreadExecutor(); | |
public void start(ScriptContainer script) { | |
if (script == null) | |
return; | |
this.container = script; | |
executor.execute(() -> { | |
state = ScriptState.RUNNING; | |
script.getScript().onStart(); | |
EventBus.getInstance().onScriptStart(script); | |
while (this.state != ScriptState.STOPPED) { | |
synchronized (this) { | |
try { | |
if (this.state == ScriptState.PAUSED) { | |
Thread.sleep(50); | |
continue; | |
} | |
if(this.container == null) | |
break; | |
Thread.sleep(this.container.getScript().onLoop()); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
} | |
public void stop() { | |
if (container == null) | |
return; | |
System.out.println("Stopping container."); | |
state = ScriptState.STOPPED; | |
container.getScript().stop(); | |
EventBus.getInstance().onScriptStop(container); | |
container = null; | |
} | |
public void pause() { | |
if (container == null) | |
return; | |
System.out.println("Pausing container."); | |
EventBus.getInstance().onScriptPause(container); | |
state = ScriptState.PAUSED; | |
} | |
public void resume() { | |
System.out.println("Running container."); | |
if (container == null) | |
return; | |
state = ScriptState.RUNNING; | |
} | |
public ScriptContainer getContainer() { | |
return container; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal; | |
import org.dreambot.api.script.AbstractScript; | |
import org.royal.script.ScriptManager; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class ScriptStore { | |
private static AbstractScript container = null; | |
private static ScriptManager currentScriptManager = null; | |
static void setContainer(AbstractScript CONTAINER) { | |
ScriptStore.container = CONTAINER; | |
} | |
public static AbstractScript getContainer() { | |
return container; | |
} | |
public static ScriptManager getCurrentScriptManager() { | |
return currentScriptManager; | |
} | |
public static void setCurrentScriptManager(ScriptManager currentScriptManager) { | |
ScriptStore.currentScriptManager = currentScriptManager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.royal.watcher; | |
import org.royal.ScriptStore; | |
import org.royal.events.EventBus; | |
import org.royal.script.ScriptContainer; | |
import org.royal.script.ScriptLoader; | |
import org.royal.script.ScriptManager; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
/** | |
* Created by Royal Family on 9/9/17. | |
*/ | |
public class FileWatcher { | |
private ExecutorService executor = Executors.newSingleThreadExecutor(); | |
private long lastUpdateTime = System.currentTimeMillis(); | |
private boolean isFirstRun = true; | |
private ScriptLoader loader; | |
public FileWatcher(ScriptLoader loader) { | |
this.loader = loader; | |
} | |
public void start(final File file) { | |
executor.submit(() -> { | |
while (true) { | |
final ScriptManager manager = ScriptStore.getCurrentScriptManager(); | |
if (manager == null || manager.getContainer() == null) { | |
break; | |
} | |
doCheck(file); | |
try { | |
Thread.sleep(350); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
}; | |
}); | |
} | |
private void onFileChange(File file) { | |
final ScriptManager manager = ScriptStore.getCurrentScriptManager(); | |
if (manager == null || manager.getContainer() == null) { | |
return; | |
} | |
manager.stop(); | |
try { | |
ScriptContainer container = this.loader.loadScriptFromJar(file.getPath()); | |
if(container == null) | |
return; | |
EventBus.getInstance().onScriptRestart(container); | |
this.loader.startScript(container); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void doCheck(File file) { | |
try { | |
long lastUpdate = Files.getLastModifiedTime(file.toPath()).toMillis(); | |
if (lastUpdate != lastUpdateTime && !isFirstRun) { | |
lastUpdateTime = lastUpdate; | |
onFileChange(file); | |
return; | |
} | |
if (isFirstRun) { | |
lastUpdateTime = lastUpdate; | |
isFirstRun = false; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment