/* */ package NET.worlds.scape; /* */ /* */ import NET.worlds.core.IniFile; /* */ import NET.worlds.network.URL; /* */ import java.io.DataInput; /* */ import java.io.DataInputStream; /* */ import java.io.File; /* */ import java.io.FileInputStream; /* */ import java.io.IOException; /* */ import java.io.PrintStream; /* */ import java.util.Enumeration; /* */ import java.util.Hashtable; /* */ import java.util.Vector; /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public class Restorer /* */ { /* */ public DataInput is; /* */ private boolean myFile; /* */ private Hashtable> classTable; /* */ private Hashtable objectTable; /* */ private Hashtable cookieTable; /* */ private URL reference; /* */ private int _version; /* */ private boolean _oldFlag; /* */ /* */ public URL getReferenceURL() /* */ { /* 139 */ return this.reference; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* 158 */ private int _dblevel = 0; /* */ /* */ /* */ /* */ /* */ /* 164 */ private Integer _firstRead = null; /* */ /* */ /* */ /* */ /* */ /* */ public final int version() /* */ { /* 172 */ return this._version; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ private Restorer(String fileName) /* */ throws IOException, BadFormatException, TooNewException /* */ { /* 182 */ this(new DataInputStream(new FileInputStream(new File(fileName)))); /* */ /* 184 */ if (this._dblevel > 0) { /* 185 */ System.out.println("Restoring from " + fileName); /* */ } /* */ /* 188 */ this.myFile = true; /* */ } /* */ /* */ /* */ /* */ /* */ public Restorer(String fileName, URL url) /* */ throws IOException, BadFormatException, TooNewException /* */ { /* 197 */ this(fileName); /* 198 */ this.reference = url; /* */ } /* */ /* */ public Restorer(URL url) /* */ throws IOException, BadFormatException, TooNewException /* */ { /* 204 */ this(url.unalias(), url); /* */ } /* */ /* */ /* */ /* */ /* */ public Restorer(DataInput is) /* */ throws IOException, BadFormatException, TooNewException /* */ { /* 213 */ this.is = is; /* */ /* 215 */ this._dblevel = IniFile.gamma().getIniInt("RESTORE_DEBUG", 0); /* */ /* */ /* 218 */ String header = restoreString(); /* 219 */ if ((header == null) || (!header.equals("PERSISTER Worlds, Inc."))) /* 220 */ throw new BadFormatException(); /* 221 */ this._version = restoreInt(); /* 222 */ if (this._dblevel > 0) /* 223 */ System.out.println("Persister version " + this._version + " {"); /* 224 */ if (this._version < 1) /* 225 */ throw new BadFormatException(); /* 226 */ if (this._version > Saver.version()) { /* 227 */ throw new TooNewException(); /* */ } /* 229 */ this.classTable = new Hashtable(); /* 230 */ this.objectTable = new Hashtable(); /* 231 */ this.cookieTable = new Hashtable(); /* */ } /* */ /* */ public Restorer(DataInput is, URL url) /* */ throws IOException, BadFormatException, TooNewException /* */ { /* 237 */ this(is); /* 238 */ this.reference = url; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public int restoreVersion(Object cookie) /* */ throws IOException /* */ { /* 251 */ if (this._version == 1) { /* 252 */ return 0; /* */ } /* 254 */ Integer oldv = (Integer)this.cookieTable.get(cookie); /* */ int v; /* 256 */ if (oldv == null) { /* 257 */ int v = restoreInt(); /* 258 */ this.cookieTable.put(cookie, new Integer(v)); /* */ } else { /* 260 */ v = oldv.intValue(); } /* 261 */ return v; /* */ } /* */ /* */ public Persister restore() /* */ throws IOException, TooNewException /* */ { /* 267 */ return restore(true); /* */ } /* */ /* */ /* */ /* */ /* */ /* */ public Persister restore(boolean restoreContents) /* */ throws IOException, TooNewException /* */ { /* 277 */ boolean firstCall = this._firstRead == null; /* */ /* 279 */ Integer hc = new Integer(restoreInt()); /* 280 */ if (this._dblevel > 0) /* 281 */ System.out.print("Object:" + Integer.toString(hc.intValue(), 16)); /* 282 */ if (this.objectTable.containsKey(hc)) { /* 283 */ Persister obj = (Persister)this.objectTable.get(hc); /* 284 */ if (this._dblevel > 0) { /* 285 */ System.out.println(" - old: " + obj.getClass().getName() + " (" + /* 286 */ Integer.toString(hc.intValue(), 16) + ")"); /* */ } /* 288 */ return obj; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* 297 */ Persister p = null; /* */ try { /* 299 */ Class c = restoreClass(true); /* 300 */ if (this._dblevel > 0) /* 301 */ System.out.println(" {"); /* */ try { /* 303 */ p = (Persister)c.newInstance(); /* */ } catch (NoSuchMethodError e) { /* 305 */ System.out.println(c); /* 306 */ e.printStackTrace(System.out); /* 307 */ throw e; /* */ } /* */ } catch (InstantiationException e) { /* 310 */ e.printStackTrace(System.out); /* 311 */ if (!$assertionsDisabled) throw new AssertionError(); /* */ } catch (IllegalAccessException e) { /* 313 */ e.printStackTrace(System.out); /* 314 */ if (!$assertionsDisabled) { throw new AssertionError(); /* */ } /* */ /* 317 */ if (p != null) /* */ { /* 319 */ this.objectTable.put(hc, p); /* */ /* 321 */ if (restoreContents) /* 322 */ p.restoreState(this); /* 323 */ if (this._dblevel > 0) { /* 324 */ System.out.println((restoreContents ? "} Done with: " : /* 325 */ "} Created: ") + Integer.toString(hc.intValue(), 16)); /* */ } /* */ } /* */ /* */ /* */ /* */ /* 332 */ Persister obj = (Persister)this.objectTable.get(hc); /* 333 */ if (firstCall) /* 334 */ this._firstRead = hc; /* 335 */ return obj; /* */ } /* */ } /* */ /* */ /* */ public Vector restoreVectorMaybeNull() /* */ throws IOException, TooNewException /* */ { /* 343 */ if (restoreBoolean()) { /* 344 */ return restoreVector(); /* */ } /* 346 */ return null; /* */ } /* */ /* */ /* */ /* */ /* */ public Persister restoreMaybeNull() /* */ throws IOException, TooNewException /* */ { /* 355 */ if (restoreBoolean()) { /* 356 */ if (this._dblevel > 0) /* 357 */ System.out.println("null"); /* 358 */ return null; /* */ } /* 360 */ return restore(); /* */ } /* */ /* */ public Class restoreClass() throws IOException /* */ { /* 365 */ return restoreClass(true); /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public Class restoreClass(boolean printdebug) /* */ throws IOException /* */ { /* 377 */ Class c = null; /* 378 */ Integer classhc = new Integer(restoreInt()); /* 379 */ if (this.classTable.containsKey(classhc)) /* */ { /* 381 */ c = (Class)this.classTable.get(classhc); /* 382 */ if ((this._dblevel > 0) && (printdebug)) { /* 383 */ System.out.print("..." + c.getName() + " [" + /* 384 */ Integer.toString(classhc.intValue(), 16) + "]"); /* */ } /* */ /* */ /* */ } /* */ else /* */ { /* */ /* 392 */ String s = restoreString(); /* 393 */ if ((this._dblevel > 0) && (printdebug)) /* 394 */ System.out.print("---" + s + " [" + /* 395 */ Integer.toString(classhc.intValue(), 16) + "]"); /* */ try { /* 397 */ if (s != null) { /* 398 */ c = Class.forName(s); /* */ } /* */ } catch (ClassNotFoundException e) { /* */ try { /* 402 */ if (s.equals("NET.worlds.network.World")) /* */ { /* 404 */ if (this._dblevel > 0) { /* 405 */ System.out.print(" (Converting NET.worlds.network.World to NET.worlds.scape.World)"); /* */ } /* */ /* 408 */ c = Class.forName("NET.worlds.scape.World"); /* */ /* */ } /* */ else /* */ { /* */ /* 414 */ if (this._dblevel > 0) /* 415 */ System.out.print(" (Converting " + s + /* 416 */ " to NET.worlds.scape." + s + ")"); /* 417 */ c = Class.forName("NET.worlds.scape." + s); /* */ } /* */ } catch (ClassNotFoundException e2) { /* 420 */ throw new Error("Can't find class " + s); /* */ } /* */ } /* */ /* 424 */ this.classTable.put(classhc, c); /* */ } /* 426 */ return c; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ private native Object makeArray(Class paramClass, int paramInt); /* */ /* */ /* */ /* */ /* */ /* */ /* */ public Persister[] restoreArray() /* */ throws IOException, TooNewException /* */ { /* 445 */ Class arrayclass = null; /* 446 */ if (this._version > 4) /* 447 */ arrayclass = restoreClass(false); /* 448 */ int length = restoreInt(); /* 449 */ Persister[] pa = null; /* 450 */ if (arrayclass == null) { /* 451 */ if (this._dblevel > 0) /* 452 */ System.out.println("Array of " + Integer.toString(length, 16) + /* 453 */ " items {"); /* 454 */ pa = new Persister[length]; /* */ } else { /* 456 */ String classname = arrayclass.getName(); /* */ /* 458 */ assert (classname.startsWith("[L")); /* 459 */ assert (classname.endsWith(";")); /* 460 */ classname = classname.substring(2); /* 461 */ classname = classname.substring(0, classname.length() - 1); /* 462 */ if (this._dblevel > 0) /* 463 */ System.out.println("Array of " + Integer.toString(length, 16) + /* 464 */ " " + classname + " {"); /* */ try { /* 466 */ arrayclass = Class.forName(classname); /* */ } /* */ catch (ClassNotFoundException cnfe) /* */ { /* 470 */ throw new Error("Can't find class " + classname.substring(1)); /* */ } /* 472 */ Object ob = makeArray(arrayclass, length); /* 473 */ pa = (Persister[])ob; /* */ } /* 475 */ for (int i = 0; i < length; i++) { /* 476 */ pa[i] = restoreMaybeNull(); /* */ } /* 478 */ if (this._dblevel > 0) /* 479 */ System.out.println("} Done with array"); /* 480 */ return pa; /* */ } /* */ /* */ /* */ /* */ /* */ public Vector restoreVector() /* */ throws IOException, TooNewException /* */ { /* 489 */ int length = restoreInt(); /* 490 */ if (this._dblevel > 0) /* 491 */ System.out.println("Vector of " + Integer.toString(length, 16) + /* 492 */ " items {"); /* 493 */ Vector v = new Vector(length); /* 494 */ for (int i = 0; i < length; i++) /* 495 */ v.addElement(restore()); /* 496 */ if (this._dblevel > 0) /* 497 */ System.out.println("} Done with vector"); /* 498 */ return v; /* */ } /* */ /* */ /* */ /* */ /* */ public Vector restoreVectorSuperRoot() /* */ throws IOException, TooNewException /* */ { /* 507 */ int length = restoreInt(); /* 508 */ if (this._dblevel > 0) /* 509 */ System.out.println("Vector of " + Integer.toString(length, 16) + /* 510 */ " items {"); /* 511 */ Vector v = new Vector(length); /* 512 */ for (int i = 0; i < length; i++) /* 513 */ v.addElement((SuperRoot)restore()); /* 514 */ if (this._dblevel > 0) /* 515 */ System.out.println("} Done with vector"); /* 516 */ return v; /* */ } /* */ /* */ /* */ /* */ /* */ public Vector restoreVectorActions() /* */ throws IOException, TooNewException /* */ { /* 525 */ int length = restoreInt(); /* 526 */ if (this._dblevel > 0) /* 527 */ System.out.println("Vector of " + Integer.toString(length, 16) + /* 528 */ " items {"); /* 529 */ Vector v = new Vector(length); /* 530 */ for (int i = 0; i < length; i++) /* 531 */ v.addElement((Action)restore()); /* 532 */ if (this._dblevel > 0) /* 533 */ System.out.println("} Done with vector"); /* 534 */ return v; /* */ } /* */ /* */ public String restoreString() throws IOException /* */ { /* 539 */ if (restoreBoolean()) { /* 540 */ return null; /* */ } /* 542 */ String s = this.is.readUTF(); /* 543 */ return s; /* */ } /* */ /* */ public boolean restoreBoolean() /* */ throws IOException /* */ { /* 549 */ return this.is.readBoolean(); /* */ } /* */ /* */ public byte restoreByte() throws IOException /* */ { /* 554 */ return this.is.readByte(); /* */ } /* */ /* */ public short restoreShort() throws IOException /* */ { /* 559 */ return this.is.readShort(); /* */ } /* */ /* */ public int restoreInt() throws IOException /* */ { /* 564 */ return this.is.readInt(); /* */ } /* */ /* */ public long restoreLong() throws IOException /* */ { /* 569 */ return this.is.readLong(); /* */ } /* */ /* */ public float restoreFloat() throws IOException /* */ { /* 574 */ return this.is.readFloat(); /* */ } /* */ /* */ public double restoreDouble() throws IOException /* */ { /* 579 */ return this.is.readDouble(); /* */ } /* */ /* */ public void done() throws IOException, Error /* */ { /* 584 */ if (this._dblevel > 0) { /* 585 */ System.out.println("Calling all postRestores"); /* */ } /* */ /* 588 */ Enumeration e = this.objectTable.elements(); /* 589 */ while (e.hasMoreElements()) { /* 590 */ Persister p = (Persister)e.nextElement(); /* 591 */ p.postRestore(this._version); /* */ } /* */ /* */ /* */ /* 596 */ e = this.objectTable.elements(); /* */ /* */ /* 599 */ while (e.hasMoreElements()) { /* 600 */ Object obj = e.nextElement(); /* */ /* 602 */ if ((obj != this.objectTable.get(this._firstRead)) && /* 603 */ ((obj instanceof SuperRoot))) { /* 604 */ SuperRoot s = (SuperRoot)obj; /* */ /* 606 */ if (s.getOwner() == null) { /* 607 */ System.out.println("Warning: " + s.getName() + /* 608 */ " was not owned by any object."); /* */ /* 610 */ s.finalize(); /* */ } /* */ } /* */ } /* */ /* */ /* */ /* */ /* */ /* 619 */ String trailer = restoreString(); /* 620 */ if (!trailer.equals("END PERSISTER")) { /* 621 */ throw new Error("Format error in save file"); /* */ } /* */ /* 624 */ if (this.myFile) /* 625 */ ((DataInputStream)this.is).close(); /* 626 */ this.classTable = null; /* 627 */ this.objectTable = null; /* 628 */ this.cookieTable = null; /* */ /* 630 */ if (this._dblevel > 0) { /* 631 */ System.out.println("} Done with persister"); /* */ } /* */ } /* */ /* */ /* */ public void setOldFlag() /* */ { /* 638 */ this._oldFlag = true; /* 639 */ if (this._dblevel > 0) { /* 640 */ System.out.println("----OLD!----"); /* */ } /* */ } /* */ /* */ public boolean oldFlag() { /* 645 */ return this._oldFlag; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public void replace(Persister old, Persister replacement) /* */ { /* 658 */ System.out.println("Converting a " + old.getClass().getName() + /* 659 */ " to a " + replacement.getClass().getName()); /* 660 */ boolean found = false; /* 661 */ Enumeration oe = this.objectTable.keys(); /* 662 */ while (oe.hasMoreElements()) { /* 663 */ Object k = oe.nextElement(); /* 664 */ if (this.objectTable.get(k) == old) { /* 665 */ this.objectTable.put(k, replacement); /* 666 */ found = true; /* 667 */ break; /* */ } /* */ } /* 670 */ assert (found); /* */ } /* */ } /* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\scape\Restorer.class * Java compiler version: 6 (50.0) * JD-Core Version: 0.7.1 */