summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Restorer.java
blob: 4659518fbca461f43b01abeb5407dd07e6059155 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class Restorer {
   private DataInput is;
   private boolean myFile;
   private Hashtable<Integer, Class<?>> classTable;
   private Hashtable<Object, Persister> objectTable;
   private Hashtable<Object, Integer> cookieTable;
   private URL reference;
   private int _version;
   private boolean _oldFlag;
   private int _dblevel = 0;
   private Integer _firstRead = null;

   public URL getReferenceURL() {
      return this.reference;
   }

   public final int version() {
      return this._version;
   }

   private Restorer(String fileName) throws IOException, BadFormatException, TooNewException {
      this(new DataInputStream(new FileInputStream(new File(fileName))));
      if (this._dblevel > 0) {
         System.out.println("Restoring from " + fileName);
      }

      this.myFile = true;
   }

   public Restorer(String fileName, URL url) throws IOException, BadFormatException, TooNewException {
      this(fileName);
      this.reference = url;
   }

   public Restorer(URL url) throws IOException, BadFormatException, TooNewException {
      this(url.unalias(), url);
   }

   public Restorer(DataInput is) throws IOException, BadFormatException, TooNewException {
      this.is = is;
      this._dblevel = IniFile.gamma().getIniInt("RESTORE_DEBUG", 0);
      String header = this.restoreString();
      if (header != null && header.equals("PERSISTER Worlds, Inc.")) {
         this._version = this.restoreInt();
         if (this._dblevel > 0) {
            System.out.println("Persister version " + this._version + " {");
         }

         if (this._version < 1) {
            throw new BadFormatException();
         } else if (this._version > Saver.version()) {
            throw new TooNewException();
         } else {
            this.classTable = new Hashtable<Integer, Class<?>>();
            this.objectTable = new Hashtable<Object, Persister>();
            this.cookieTable = new Hashtable<Object, Integer>();
         }
      } else {
         throw new BadFormatException();
      }
   }

   public Restorer(DataInput is, URL url) throws IOException, BadFormatException, TooNewException {
      this(is);
      this.reference = url;
   }

   public int restoreVersion(Object cookie) throws IOException {
      if (this._version == 1) {
         return 0;
      } else {
         Integer oldv = this.cookieTable.get(cookie);
         int v;
         if (oldv == null) {
            v = this.restoreInt();
            this.cookieTable.put(cookie, new Integer(v));
         } else {
            v = oldv;
         }

         return v;
      }
   }

   public Persister restore() throws IOException, TooNewException {
      return this.restore(true);
   }

   public Persister restore(boolean restoreContents) throws IOException, TooNewException {
      boolean firstCall = this._firstRead == null;
      Integer hc = new Integer(this.restoreInt());
      if (this._dblevel > 0) {
         System.out.print("Object:" + Integer.toString(hc, 16));
      }

      if (this.objectTable.containsKey(hc)) {
         Persister obj = this.objectTable.get(hc);
         if (this._dblevel > 0) {
            System.out.println(" - old: " + obj.getClass().getName() + " (" + Integer.toString(hc, 16) + ")");
         }

         return obj;
      } else {
         Persister p = null;

         try {
            Class<?> c = this.restoreClass(true);
            if (this._dblevel > 0) {
               System.out.println(" {");
            }

            try {
               p = (Persister)c.newInstance();
            } catch (NoSuchMethodError var7) {
               System.out.println(c);
               var7.printStackTrace(System.out);
               throw var7;
            }
         } catch (InstantiationException var8) {
            var8.printStackTrace(System.out);

            assert false;
         } catch (IllegalAccessException var9) {
            var9.printStackTrace(System.out);

            assert false;
         }

         this.objectTable.put(hc, p);
         if (restoreContents) {
            p.restoreState(this);
         }

         if (this._dblevel > 0) {
            System.out.println((restoreContents ? "} Done with: " : "} Created: ") + Integer.toString(hc, 16));
         }

         Persister obj = this.objectTable.get(hc);
         if (firstCall) {
            this._firstRead = hc;
         }

         return obj;
      }
   }

   public Vector<Object> restoreVectorMaybeNull() throws IOException, TooNewException {
      return this.restoreBoolean() ? this.restoreVector() : null;
   }

   public Persister restoreMaybeNull() throws IOException, TooNewException {
      if (this.restoreBoolean()) {
         if (this._dblevel > 0) {
            System.out.println("null");
         }

         return null;
      } else {
         return this.restore();
      }
   }

   public Class<?> restoreClass() throws IOException {
      return this.restoreClass(true);
   }

   public Class<?> restoreClass(boolean printdebug) throws IOException {
      Class<?> c = null;
      Integer classhc = new Integer(this.restoreInt());
      if (this.classTable.containsKey(classhc)) {
         c = this.classTable.get(classhc);
         if (this._dblevel > 0 && printdebug) {
            System.out.print("..." + c.getName() + " [" + Integer.toString(classhc, 16) + "]");
         }
      } else {
         String s = this.restoreString();
         if (this._dblevel > 0 && printdebug) {
            System.out.print("---" + s + " [" + Integer.toString(classhc, 16) + "]");
         }

         try {
            c = Class.forName(s);
         } catch (ClassNotFoundException var8) {
            try {
               if (s.equals("NET.worlds.network.World")) {
                  if (this._dblevel > 0) {
                     System.out.print(" (Converting NET.worlds.network.World to NET.worlds.scape.World)");
                  }

                  c = Class.forName("NET.worlds.scape.World");
               } else {
                  if (this._dblevel > 0) {
                     System.out.print(" (Converting " + s + " to NET.worlds.scape." + s + ")");
                  }

                  c = Class.forName("NET.worlds.scape." + s);
               }
            } catch (ClassNotFoundException var7) {
               throw new Error("Can't find class " + s);
            }
         }

         this.classTable.put(classhc, c);
      }

      return c;
   }

   private Object makeArray(Class<?> var1, int var2) {
      return java.lang.reflect.Array.newInstance(var1, var2);
   }

   public Persister[] restoreArray() throws IOException, TooNewException {
      Class<?> arrayclass = null;
      if (this._version > 4) {
         arrayclass = this.restoreClass(false);
      }

      int length = this.restoreInt();
      Persister[] pa = (Persister[])null;
      if (arrayclass == null) {
         if (this._dblevel > 0) {
            System.out.println("Array of " + Integer.toString(length, 16) + " items {");
         }

         pa = new Persister[length];
      } else {
         String classname = arrayclass.getName();

         assert classname.startsWith("[L");

         assert classname.endsWith(";");

         classname = classname.substring(2);
         classname = classname.substring(0, classname.length() - 1);
         if (this._dblevel > 0) {
            System.out.println("Array of " + Integer.toString(length, 16) + " " + classname + " {");
         }

         try {
            arrayclass = Class.forName(classname);
         } catch (ClassNotFoundException var6) {
            throw new Error("Can't find class " + classname.substring(1));
         }

         Object ob = this.makeArray(arrayclass, length);
         pa = (Persister[])ob;
      }

      for (int i = 0; i < length; i++) {
         pa[i] = this.restoreMaybeNull();
      }

      if (this._dblevel > 0) {
         System.out.println("} Done with array");
      }

      return pa;
   }

   public Vector<Object> restoreVector() throws IOException, TooNewException {
      int length = this.restoreInt();
      if (this._dblevel > 0) {
         System.out.println("Vector of " + Integer.toString(length, 16) + " items {");
      }

      Vector<Object> v = new Vector<Object>(length);

      for (int i = 0; i < length; i++) {
         v.addElement(this.restore());
      }

      if (this._dblevel > 0) {
         System.out.println("} Done with vector");
      }

      return v;
   }

   public Vector<SuperRoot> restoreVectorSuperRoot() throws IOException, TooNewException {
      int length = this.restoreInt();
      if (this._dblevel > 0) {
         System.out.println("Vector of " + Integer.toString(length, 16) + " items {");
      }

      Vector<SuperRoot> v = new Vector<SuperRoot>(length);

      for (int i = 0; i < length; i++) {
         v.addElement((SuperRoot)this.restore());
      }

      if (this._dblevel > 0) {
         System.out.println("} Done with vector");
      }

      return v;
   }

   public Vector<Action> restoreVectorActions() throws IOException, TooNewException {
      int length = this.restoreInt();
      if (this._dblevel > 0) {
         System.out.println("Vector of " + Integer.toString(length, 16) + " items {");
      }

      Vector<Action> v = new Vector<Action>(length);

      for (int i = 0; i < length; i++) {
         v.addElement((Action)this.restore());
      }

      if (this._dblevel > 0) {
         System.out.println("} Done with vector");
      }

      return v;
   }

   public String restoreString() throws IOException {
      return this.restoreBoolean() ? null : this.is.readUTF();
   }

   public boolean restoreBoolean() throws IOException {
      return this.is.readBoolean();
   }

   public byte restoreByte() throws IOException {
      return this.is.readByte();
   }

   public short restoreShort() throws IOException {
      return this.is.readShort();
   }

   public int restoreInt() throws IOException {
      return this.is.readInt();
   }

   public long restoreLong() throws IOException {
      return this.is.readLong();
   }

   public float restoreFloat() throws IOException {
      return this.is.readFloat();
   }

   public double restoreDouble() throws IOException {
      return this.is.readDouble();
   }

   public void done() throws IOException, Error {
      if (this._dblevel > 0) {
         System.out.println("Calling all postRestores");
      }

      Enumeration<Persister> e = this.objectTable.elements();

      while (e.hasMoreElements()) {
         Persister p = e.nextElement();
         p.postRestore(this._version);
      }

      e = this.objectTable.elements();

      while (e.hasMoreElements()) {
         Object obj = e.nextElement();
         if (obj != this.objectTable.get(this._firstRead) && obj instanceof SuperRoot) {
            SuperRoot s = (SuperRoot)obj;
            if (s.getOwner() == null) {
               System.out.println("Warning: " + s.getName() + " was not owned by any object.");
               s.finalize();
            }
         }
      }

      String trailer = this.restoreString();
      if (!trailer.equals("END PERSISTER")) {
         throw new Error("Format error in save file");
      } else {
         if (this.myFile) {
            ((DataInputStream)this.is).close();
         }

         this.classTable = null;
         this.objectTable = null;
         this.cookieTable = null;
         if (this._dblevel > 0) {
            System.out.println("} Done with persister");
         }
      }
   }

   public void setOldFlag() {
      this._oldFlag = true;
      if (this._dblevel > 0) {
         System.out.println("----OLD!----");
      }
   }

   public boolean oldFlag() {
      return this._oldFlag;
   }

   public void replace(Persister old, Persister replacement) {
      System.out.println("Converting a " + old.getClass().getName() + " to a " + replacement.getClass().getName());
      boolean found = false;
      Enumeration<Object> oe = this.objectTable.keys();

      while (oe.hasMoreElements()) {
         Object k = oe.nextElement();
         if (this.objectTable.get(k) == old) {
            this.objectTable.put(k, replacement);
            found = true;
            break;
         }
      }

      assert found;
   }
}