summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/BackgroundLoader.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/BackgroundLoader.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/scape/BackgroundLoader.java')
-rw-r--r--NET/worlds/scape/BackgroundLoader.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/NET/worlds/scape/BackgroundLoader.java b/NET/worlds/scape/BackgroundLoader.java
new file mode 100644
index 0000000..8f337a0
--- /dev/null
+++ b/NET/worlds/scape/BackgroundLoader.java
@@ -0,0 +1,95 @@
+package NET.worlds.scape;
+
+import NET.worlds.console.Main;
+import NET.worlds.console.MainCallback;
+import NET.worlds.network.URL;
+import java.util.Vector;
+
+public class BackgroundLoader implements MainCallback, Runnable {
+ private static BackgroundLoader bg = new BackgroundLoader();
+ private BackgroundLoaderQueue readQueue = new BackgroundLoaderQueue();
+ private BackgroundLoaderQueue asyncLoadQueue = new BackgroundLoaderQueue();
+ private Vector syncLoadQueue = new Vector();
+ private Thread asyncLoaderThread;
+ int nextSync = 0;
+
+ public static void get(BGLoaded loader, String remoteName) {
+ get(loader, URL.make(remoteName), false);
+ }
+
+ public static void get(BGLoaded loader, URL remoteName) {
+ get(loader, remoteName, false);
+ }
+
+ public static void get(BGLoaded loader, URL remoteName, boolean highPri) {
+ new BackgroundLoaderElement(loader, remoteName, highPri);
+ }
+
+ static void activeRoomChanged(Room room) {
+ bg.asyncLoadQueue.activeRoomChanged(room);
+ }
+
+ static void asyncLoad(BackgroundLoaderElement ele) {
+ bg.asyncLoadQueue.add(ele);
+ }
+
+ static void syncLoad(BackgroundLoaderElement ele) {
+ bg.syncLoadQueue.addElement(ele);
+ }
+
+ private BackgroundLoader() {
+ this.asyncLoaderThread = new Thread(this);
+ this.asyncLoaderThread.setDaemon(true);
+ this.asyncLoaderThread.start();
+ Main.register(this);
+ }
+
+ @Override
+ public void run() {
+ if ($assertionsDisabled || Thread.currentThread() == this.asyncLoaderThread) {
+ while (true) {
+ BackgroundLoaderElement ele = this.asyncLoadQueue.getItem();
+ ele.asyncLoad();
+ synchronized (this) {
+ this.syncLoadQueue.addElement(ele);
+
+ while (this.syncLoadQueue.size() > 0) {
+ try {
+ this.wait();
+ } catch (InterruptedException var4) {
+ }
+
+ if (this.asyncLoadQueue.hasHighPriorityItems()) {
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ throw new AssertionError();
+ }
+
+ @Override
+ public synchronized void mainCallback() {
+ if (this.syncLoadQueue.size() != 0) {
+ if (this.nextSync >= this.syncLoadQueue.size()) {
+ this.nextSync = 0;
+ }
+
+ BackgroundLoaderElement ele = (BackgroundLoaderElement)this.syncLoadQueue.elementAt(this.nextSync);
+ if (!ele.syncLoad()) {
+ this.syncLoadQueue.removeElementAt(this.nextSync);
+ if (this.syncLoadQueue.size() == 0) {
+ this.notify();
+ }
+ } else {
+ if (this.asyncLoadQueue.hasHighPriorityItems()) {
+ this.notify();
+ }
+
+ this.nextSync++;
+ }
+ }
+ }
+}