package NET.worlds.scape; import NET.worlds.core.FastDataInput; import NET.worlds.network.URL; import java.io.IOException; import java.util.Hashtable; import java.util.Vector; public class URLSelfLoader implements BGLoaded { private static Hashtable loaded = new Hashtable(); private static Hashtable loading = new Hashtable(); private URL url; private Vector callbacks = new Vector(); private static final int INIT = 0; private static final int DONE = -1; private int state = 0; private FastDataInput inFile; private Restorer restorer; private URLSelf loadedObj; public Object otemp1; public Object otemp2; public int itemp1; public int itemp2; public static void load(URL url, LoadedURLSelf callback) { load(url, callback, false); } public static void load(URL url, LoadedURLSelf callback, boolean isHighPriority) { URLSelf o = (URLSelf)loaded.get(url); if (o != null) { o.incRef(); callback.loadedURLSelf(o, url, null); } else { URLSelfLoader cl = (URLSelfLoader)loading.get(url); if (cl != null) { cl.callbacks.addElement(callback); } else { new URLSelfLoader(url, callback, isHighPriority); } } } public static void unload(URLSelf self) { loaded.remove(self.getSourceURL()); } public static void immediateLoad(IncrementalRestorer o, Restorer r) throws IOException, TooNewException { new URLSelfLoader(o, r); } private URLSelfLoader(IncrementalRestorer o, Restorer r) throws IOException, TooNewException { this.url = null; while (this.state != -1) { try { this.state = o.incRestore(this.state, r, this); } catch (Exception var4) { var4.printStackTrace(System.out); assert false; } } } public URL getURL() { return this.url; } private URLSelfLoader(URL url, LoadedURLSelf callback, boolean isHighPriority) { loading.put(url, this); this.url = url; this.callbacks.addElement(callback); BackgroundLoader.get(this, url, isHighPriority); } @Override public Object asyncBackgroundLoad(String localName, URL remoteName) { if (localName == null) { return "Can't find file " + this.url; } else { try { return new FastDataInput(localName); } catch (IOException var4) { return "Can't load file " + this.url; } } } @Override public boolean syncBackgroundLoad(Object obj, URL remoteURL) { if (obj instanceof String) { this.doneLoading(null, (String)obj); return false; } else { assert this.state != -1; if (this.state == 0) { this.inFile = (FastDataInput)obj; Object o = null; try { this.restorer = new Restorer(this.inFile, this.url); o = this.restorer.restore(false); this.loadedObj = (URLSelf)o; this.loadedObj.setSourceURL(this.getURL()); if (!(this.loadedObj instanceof IncrementalRestorer)) { this.loadedObj.restoreState(this.restorer); this.restorer.done(); return this.doneLoading(this.loadedObj, null); } } catch (Exception var7) { var7.printStackTrace(System.out); return this.doneLoading(null, "Can't restore from file " + this.url + ": " + var7.toString()); } } try { this.state = ((IncrementalRestorer)this.loadedObj).incRestore(this.state, this.restorer, this); } catch (Exception var6) { var6.printStackTrace(System.out); return this.doneLoading(null, var6.toString()); } if (this.state == -1) { try { this.restorer.done(); } catch (IOException var5) { var5.printStackTrace(System.out); return this.doneLoading(null, var5.toString()); } if (this.restorer.version() < 3 && this.loadedObj instanceof Camera) { Camera cam = (Camera)this.loadedObj; World w = cam.getWorld(); w.setSourceURL(cam.getSourceURL()); } return this.doneLoading(this.loadedObj, null); } else { return true; } } } private boolean doneLoading(URLSelf o, String err) { if (this.inFile != null) { this.inFile.close(); this.inFile = null; } loading.remove(this.url); if (o != null) { if (this.restorer.version() < 4 && o instanceof Camera) { Camera c = (Camera)o; o = c.getWorld(); c.detach(); } loaded.put(this.url, o); } int end = this.callbacks.size(); for (int i = 0; i < end; i++) { LoadedURLSelf callback = (LoadedURLSelf)this.callbacks.elementAt(i); if (o != null) { o.incRef(); } callback.loadedURLSelf(o, this.url, err); } this.state = -1; return false; } @Override public Room getBackgroundLoadRoom() { return null; } }