summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/BackgroundLoaderElement.java
blob: aba150a989fde0c9faad8a3564980e206f22e211 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package NET.worlds.scape;

import NET.worlds.console.Main;
import NET.worlds.core.IniFile;
import NET.worlds.network.Cache;
import NET.worlds.network.CacheFile;
import NET.worlds.network.URL;
import java.util.Observable;
import java.util.Observer;

class BackgroundLoaderElement implements Observer {
   private BGLoaded object;
   private Object arg;
   private URL url;
   private Room room;
   private boolean inAllRooms;
   private CacheFile rfile;
   private static boolean immedLoadLocals = IniFile.gamma().getIniInt("BackgroundLoadLocalFiles", 0) == 0;

   BackgroundLoaderElement(BGLoaded object, URL url, boolean inAllRooms) {
      boolean isMain = Main.isMainThread();
      this.object = object;
      this.url = url;
      if (inAllRooms) {
         this.inAllRooms = true;
      } else {
         this.getRoom(isMain);
      }

      this.read(isMain);
   }

   @Override
   public void update(Observable o, Object url) {
      BackgroundLoader.asyncLoad(this);
   }

   private void read(boolean isMain) {
      this.rfile = this.url != null && this.url.isRemote() ? Cache.getFile(this.url) : null;
      if ((this.rfile == null || this.rfile.done()) && immedLoadLocals) {
         this.asyncLoad();
         if (!isMain || this.syncLoad()) {
            BackgroundLoader.syncLoad(this);
         }
      } else if (this.rfile == null) {
         BackgroundLoader.asyncLoad(this);
      } else {
         this.rfile.callWhenLoaded(this);
      }
   }

   void asyncLoad() {
      String name;
      if (this.rfile == null) {
         name = this.url.unalias();
      } else {
         name = this.rfile.getLocalName();
      }

      this.arg = this.object.asyncBackgroundLoad(name, this.url);
   }

   boolean syncLoad() {
      if (this.object.syncBackgroundLoad(this.arg, this.url)) {
         return true;
      } else {
         if (this.rfile != null) {
            this.rfile.finalize();
            this.rfile = null;
         }

         return false;
      }
   }

   boolean inAllRooms() {
      return this.inAllRooms;
   }

   Room getRoom(boolean isMainThread) {
      if (this.room == null && !this.inAllRooms && isMainThread) {
         this.room = this.object.getBackgroundLoadRoom();
      }

      return this.room;
   }
}