diff options
Diffstat (limited to 'NET/worlds/scape/WorldScriptLoader.java')
| -rw-r--r-- | NET/worlds/scape/WorldScriptLoader.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/NET/worlds/scape/WorldScriptLoader.java b/NET/worlds/scape/WorldScriptLoader.java new file mode 100644 index 0000000..f209e1d --- /dev/null +++ b/NET/worlds/scape/WorldScriptLoader.java @@ -0,0 +1,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; + } +} |