summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/WorldScriptLoader.java
blob: f209e1d6c3885edb9b76e233f10cd6641ad42ae9 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package NET.worlds.scape;

import NET.worlds.network.Cache;
import NET.worlds.network.CacheFile;
import NET.worlds.network.NetUpdate;
import NET.worlds.network.URL;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;

class WorldScriptLoader extends ClassLoader {
   Hashtable cache = new Hashtable();
   String lastWorldName;

   private byte[] loadClassData(String name) {
      String prefix = "NET.worlds.scape.";
      if (name.startsWith(prefix)) {
         name = name.substring(prefix.length());
      }

      int nameIdx = name.indexOf("WorldScript");
      if (nameIdx == -1) {
         nameIdx = 0;
      } else {
         nameIdx += new String("WorldScript").length();
      }

      int extIdx = name.indexOf(".class");
      String worldName;
      if (nameIdx == 0 && this.lastWorldName != null) {
         worldName = this.lastWorldName;
         if (!name.endsWith(".class")) {
            name = name + ".class";
         }
      } else if (extIdx != -1) {
         worldName = name.substring(nameIdx, extIdx);
      } else {
         worldName = name.substring(nameIdx);
         name = name + ".class";
      }

      URL scriptURL = URL.make(NetUpdate.getUpgradeServerURL() + worldName + "/" + name);
      CacheFile f = Cache.getFile(scriptURL, true);
      f.waitUntilLoaded();
      if (f.error()) {
         return null;
      } else {
         this.lastWorldName = worldName;
         String fileName = f.getLocalName();

         try {
            File file = new File(fileName);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            while (true) {
               try {
                  int bytesRead = fis.read(buffer);
                  if (bytesRead == -1) {
                     break;
                  }

                  os.write(buffer, 0, bytesRead);
               } catch (Exception var14) {
                  break;
               }
            }

            return os.toByteArray();
         } catch (Exception var15) {
            return null;
         }
      }
   }

   @Override
   public synchronized Class loadClass(String name, boolean resolve) {
      Class c = (Class)this.cache.get(name);
      if (c == null) {
         byte[] data = (byte[])null;
         if (!name.startsWith("java.")) {
            data = this.loadClassData(name);
         }

         if (data == null) {
            try {
               return this.findSystemClass(name);
            } catch (Error var6) {
               System.out.println("Could not load script " + name + " " + var6);
               return null;
            } catch (Exception var7) {
               System.out.println("Could not load script " + name + " " + var7);
               return null;
            }
         }

         c = this.defineClass(data, 0, data.length);
         this.cache.put(name, c);
      }

      if (resolve) {
         this.resolveClass(c);
      }

      return c;
   }
}