summaryrefslogtreecommitdiff
path: root/NET/worlds/console/Gamma.java
blob: 422a88b6220a9ef51e0c640fc7f5b82713c390e7 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package NET.worlds.console;

import NET.worlds.core.IniFile;
import NET.worlds.core.Std;
import NET.worlds.network.NetUpdate;
import NET.worlds.scape.MusicManager;
import NET.worlds.scape.Pilot;
import NET.worlds.scape.TeleportAction;
import NET.worlds.scape.TeleportStatus;
import NET.worlds.scape.World;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.StringTokenizer;

public class Gamma implements Runnable, MainCallback, TeleportStatus {
   private static SplashScreen splash = null;
   public static ProgressBar loadProgress;
   static final int LOAD_PROGRESS_STEPS = 12;
   static final float failVersion = 1.13F;
   private static String _home = "file:";
   private static String _dllPath = "home:";
   private static Hashtable<String, String> _params = new Hashtable<String, String>();
   private static boolean _autoplay = false;
   static Shaper shaper;
   private String startingURL;
   private String loadErr;

   static boolean checkVersion(String ver) {
      StringTokenizer tok = new StringTokenizer(ver, "._");
      float version = 0.0F;

      try {
         Integer i = new Integer(tok.nextToken());
         version = i.intValue();
         i = new Integer(tok.nextToken());
         float min = i.floatValue() / 10.0F;
         if (!(min < 1.0F)) {
            return true;
         }

         version += min;
         i = new Integer(tok.nextToken());
         min = i.floatValue() / 100.0F;
         version += min;
      } catch (NoSuchElementException var9) {
      }

      if (version <= 1.13F) {
         BlockingDialog d = new BlockingDialog(new Frame(), "Worlds.com: Error", true);
         GridBagLayout gbl = new GridBagLayout();
         d.setLayout(gbl);
         d.setSize(200, 150);
         MultiLineLabel mll = new MultiLineLabel("Your system's Java Virtual Machine\nis out of date. Please download our\nfull installer and try again.");
         GridBagConstraints c = new GridBagConstraints();
         c.gridx = 0;
         c.gridy = 0;
         gbl.setConstraints(mll, c);
         d.add(mll);
         Button b = new Button("OK");
         b.addActionListener(d);
         GridBagConstraints c2 = new GridBagConstraints();
         c2.gridx = 0;
         c2.gridy = 1;
         gbl.setConstraints(b, c2);
         d.add(b);
         d.validate();
         d.show();
         d.waitForResponse();
         return false;
      } else {
         return true;
      }
   }

   public static void main(String[] args) {
      String worldURL = parseCommandLine(args);
      String s = System.getProperty("java.version");
      if (!checkVersion(s)) {
         System.exit(0);
      }

      String vendor = System.getProperty("java.vendor");
      System.out.println("User running java version " + s + " from vendor " + System.getProperty("java.vendor"));
      String awtDll = "winawt";
      String netDll = "net";
      boolean msvm = false;
      if (vendor.indexOf("Microsoft") != -1) {
         awtDll = "MSAWT";
         netDll = "MSNET32";
         msvm = true;
      }

      System.out.println("Loading: " + earlyURLUnalias(_dllPath + "gamma.dll"));
      System.load(earlyURLUnalias(_dllPath + "gamma.dll"));
      if (msvm) {
         Window.doMicrosoftVMHacks();
      }

      if (IniFile.gamma().getIniInt("MULTIRUN", 0) == 0 && !Startup.synchronizeStartup(worldURL, _autoplay)) {
         System.exit(0);
      }

      if (s.charAt(2) <= '1' && !msvm) {
         try {
            System.loadLibrary(awtDll);
         } catch (UnsatisfiedLinkError var35) {
         }

         try {
            System.loadLibrary(netDll);
         } catch (UnsatisfiedLinkError var34) {
         }
      } else {
         if (!msvm) {
            awtDll = "awt";
         }

         new Color(0, 0, 0);

         try {
            URL stupid = new URL("http://127.0.0.1/");
            URLConnection uc = stupid.openConnection();
            uc.connect();
         } catch (MalformedURLException var36) {
         } catch (IOException var37) {
         }
      }

      if (!msvm) {
         Window.hookWinAPIs(awtDll);
      }

      String inihome = earlyURLUnalias("home:worlds.ini");
      Std.initProductName();
      IniFile install = IniFile.override();
      String splashgif = install.getIniString("splashgif", Console.message("Opnscrnc.gif"));
      splash = new SplashScreen(GammaFrame.getDefaultTitle(), splashgif);
      splash.show();
      splash.toFront();
      String proxyIP = IniFile.gamma().getIniString("Proxy Server IP", "");
      String proxyPort = IniFile.gamma().getIniString("Proxy Server Port", "");
      if (proxyIP.length() > 7) {
         Properties p = System.getProperties();
         System.out.println("Using Proxy Server: " + proxyIP + ":" + proxyPort);
         p.remove("socksProxyHost");
         p.remove("socksProxyPort");
         p.put("socksProxyHost", proxyIP);
         p.put("socksProxyPort", proxyPort);
         System.setProperties(p);
      }

      loadProgress = new ProgressBar("Loading Worldsplayer...", 12);
      Dimension sd = splash.getSize();
      Dimension d = loadProgress.getSize();
      int px = splash.getLocationOnScreen().x + (sd.width >> 1) - (d.width >> 1);
      int py = splash.getLocationOnScreen().y + sd.height;
      loadProgress.setLocation(px, py);
      loadProgress.show();
      java.awt.Cursor c = java.awt.Cursor.getPredefinedCursor(3);
      loadProgress.setCursor(c);
      splash.setCursor(c);
      if (inihome.length() > 1 && inihome.charAt(1) == ':') {
         Startup.computeVolumeInfo(inihome.charAt(0) + ":\\");
      } else {
         Startup.computeVolumeInfo(null);
      }

      try {
         LogFile.open();
         Gamma g = new Gamma(worldURL);
         Main.register(g);
         if (loadProgress != null) {
            loadProgress.setMessage("Starting main thread...");
            loadProgress.advance();
         }

         Thread mainThread = new Thread(g, "Gamma Main");
         mainThread.setDaemon(true);
         mainThread.start();
         new Gamma.PriorityAdjuster(mainThread);
         if (loadProgress != null) {
            loadProgress.setMessage("Initializing ActiveX...");
            loadProgress.advance();
         }

         new Netscape();

         try {
            mainThread.join();
         } catch (InterruptedException var30) {
            throw new Error(var30.toString());
         }

         GammaFrame frame = Console.getFrame();
         if (frame != null) {
            frame.setVisible(false);
         }
      } catch (OutOfMemoryError var31) {
         System.out.println("ERROR:  Ran out of memory!!");
         System.out.println("Details: " + var31);
      } catch (Throwable var32) {
         System.out.println("Uncaught throwable: " + var32);
      } finally {
         LogFile.close();
      }

      System.exit(0);
   }

   public static void hideSplash() {
      if (splash != null) {
         splash.hide();
         splash.dispose();
         splash = null;
         if (loadProgress != null) {
            loadProgress.hide();
            loadProgress.dispose();
            loadProgress = null;
         }
      }
   }

   public static String earlyURLUnalias(String url) {
      if (url.startsWith("home:")) {
         url = _home + url.substring(5);
      }

      if (url.startsWith("file:")) {
         url = url.substring(5);
      }

      url = url.replace('\\', '/');
      if (url.length() < 2 || url.charAt(1) != ':' && !url.startsWith("//")) {
         String curDir = System.getProperty("user.dir").replace('\\', '/');
         if (!curDir.endsWith("/")) {
            curDir = curDir + "/";
         }

         url = curDir + url;
      }

      return url;
   }

   public static String getParam(String name) {
      return _params.get(name);
   }

   private static String parseCommandLine(String[] args) {
      String world = null;

      for (int i = 0; i < args.length; i++) {
         if (args[i].length() > 0 && args[i].charAt(0) == '-') {
            if (args[i].equalsIgnoreCase("-help")) {
               usage("Help message");
            } else if (args[i].equalsIgnoreCase("-home")) {
               if (++i == args.length) {
                  usage("-home must be followed by a path:");
               }

               _home = "file:" + args[i];
               System.out.println("Home: " + args[i]);
            } else if (args[i].equalsIgnoreCase("-dllpath")) {
               if (++i == args.length) {
                  usage("-dllpath must be followed by a path:");
               }

               _dllPath = args[i];
            } else if (args[i].equalsIgnoreCase("-set")) {
               label51: {
                  if (++i != args.length) {
                     if (++i != args.length) {
                        break label51;
                     }
                  }

                  usage("-set must be followed by a name of a parameter to set and its value");
               }

               _params.put(args[i - 1], args[i]);
            } else if (args[i].equalsIgnoreCase("-autoplay")) {
               _autoplay = true;
            } else if (!args[i].equalsIgnoreCase("-embedding")
               && !args[i].equalsIgnoreCase("/embedding")
               && !args[i].equalsIgnoreCase("-automation")
               && !args[i].equalsIgnoreCase("/automation")) {
               usage("Unrecognized command line option: " + args[i]);
            }
         } else {
            if (world != null) {
               usage("There may be only one command-line world URL");
            }

            world = args[i];
         }
      }

      _home = makeEndWithSlash(_home);
      _dllPath = makeEndWithSlash(_dllPath);
      return world;
   }

   private static String makeEndWithSlash(String url) {
      if (!url.endsWith("\\") && !url.endsWith("/") && !url.endsWith(":")) {
         url = url + "\\";
      }

      return url;
   }

   public static void usage(String msg) {
      System.out
         .println(
            "Usage: javaw {-java_options} NET.worlds.console.Gamma {-options} WorldURL\nWorldURL is optional, the default world is the first WorldsMark.\nYes, it's gross, but the 'NET.worlds.console.Gamma' is required\nSome useful -java_options:\n    -classpath Path   Class search path [default is CLASSPATH env var]\n-options:\n    -help             Print this message (still runs Gamma)\n    -home HomeDir     Gamma home directory [defaults to current dir]\n    -dllpath DLLPath  Path to the native code DLL [current directory]\n    -set Name Value   Set the named parameter to the specified value"
         );
      throw new Error(msg);
   }

   public static String getHome() {
      return _home;
   }

   public static String getExePath() {
      return getHome() + "bin\\";
   }

   public static void dllLoad(String dll) {
      System.load(NET.worlds.network.URL.make(_dllPath + dll).unalias());
   }

   public static Shaper getShaper() {
      return shaper;
   }

   public static boolean shaperEnabled() {
      return IniFile.gamma().getIniInt("DISABLESHAPER", 1) == 0 || IniFile.override().getIniInt("DISABLESHAPER", 1) == 0;
   }

   private Gamma(String url) {
      this.startingURL = url;
   }

   private void die(Throwable e) {
      e.printStackTrace(System.out);
      if (getShaper() != null) {
         Console.println(Console.message("Saving-modified"));
         Enumeration<World> worlds = World.getWorlds();

         while (worlds.hasMoreElements()) {
            World w = worlds.nextElement();
            if (w.getEdited()) {
               String base = w.getSourceURL().unalias();
               if (base.toLowerCase().endsWith(".wor")) {
                  base = base.substring(0, base.length() - 4);
               }

               if (base.toLowerCase().endsWith(".world")) {
                  base = base.substring(0, base.length() - 6);
               }

               int dash = base.lastIndexOf("-");
               if (dash != -1) {
                  for (int i = dash + 1; i < base.length(); i++) {
                     if (!Character.isDigit(base.charAt(i))) {
                        dash = -1;
                        break;
                     }
                  }
               }

               if (dash != -1) {
                  base = base.substring(0, dash);
               }

               String name = base + ".world";

               for (int number = 1; new File(name).exists(); number++) {
                  name = base + "-" + number + ".world";
               }

               Object[] arguments = new Object[]{new String(w.getName()), new String("" + w.getSourceURL()), new String(name)};
               Console.println(MessageFormat.format(Console.message("Saving-name"), arguments));

               try {
                  Shaper.doSave(name, w, false);
               } catch (Exception var10) {
                  Console.println(Console.message("Ignoring") + var10);
               } catch (Error var11) {
                  Console.println(Console.message("Ignoring") + var11);
               }
            }
         }
      }
   }

   @Override
   public void run() {
      Main.register(new Gamma.RecordPosition());

      try {
         Main.mainLoop();
      } catch (Throwable var2) {
         this.die(var2);
      }
   }

   @Override
   public void mainCallback() {
      Main.unregister(this);
      if (NetUpdate.doUpdate(false)) {
         Main.end();
      } else {
         File bin = new File(earlyURLUnalias("home:bin"));
         File prg = new File(bin, "gdkup.prg");
         File exe = new File(bin, "gdkup.exe");
         if (prg.exists() && (!exe.exists() || prg.lastModified() > exe.lastModified())) {
            if (exe.exists()) {
               exe.delete();
            }

            try {
               FileInputStream i = new FileInputStream(prg);
               FileOutputStream o = new FileOutputStream(exe);
               byte[] buf = new byte[8192];

               int len;
               while ((len = i.read(buf)) > 0) {
                  o.write(buf, 0, len);
               }

               i.close();
               o.close();
            } catch (Exception var10) {
               var10.printStackTrace(System.out);
               throw new Error("Can't copy gdkup.prg");
            }
         }

         if (shaperEnabled()) {
            shaper = new Shaper();
         }

         IniFile install = IniFile.override();
         String splashOver = install.getIniString("splashover", Console.message("Pwc.gif"));
         int splashXOver = install.getIniInt("splashxover", 141);
         int splashYOver = install.getIniInt("splashyover", 140);
         if (splashXOver >= 0) {
            splash.addOverlay(splashOver, splashXOver, splashYOver);
         }

         if (loadProgress != null) {
            loadProgress.setMessage("Teleporting to start location...");
            loadProgress.advance();
         }

         try {
            TeleportAction.teleport(this.startingURL, this, false);
         } catch (Exception var9) {
            this.loadErr = "Couldn't teleport to " + this.startingURL + ": " + var9;
         }

         if (this.loadErr != null) {
            System.out.println(this.loadErr);
            this.loadErr = null;
            TeleportAction.teleport("world:", this, false);
            if (this.loadErr != null) {
               this.loadErr = null;
               TeleportAction.teleport("home:NewWorld.world", this, false);
               if (this.loadErr != null) {
                  Main.end();
                  return;
               }
            }

            Main.register(new Gamma.StartupTeleport());
         }

         new MusicManager();
      }
   }

   @Override
   public void teleportStatus(String err, String url) {
      if (err == null) {
         this.loadErr = null;
      } else {
         this.loadErr = err;
      }
   }

   static class PriorityAdjuster implements MainCallback {
      private Thread mainThread;
      boolean wasActivated = true;

      PriorityAdjuster(Thread t) {
         this.mainThread = t;
         this.mainThread.setPriority(5);
         Main.register(this);
      }

      @Override
      public void mainCallback() {
         if (Window.isActivated() != this.wasActivated) {
            this.wasActivated = !this.wasActivated;
            this.mainThread.setPriority(this.wasActivated ? 5 : 1);
         }
      }
   }

   public class RecordPosition implements MainCallback, MainTerminalCallback {
      @Override
      public void mainCallback() {
      }

      @Override
      public void terminalCallback() {
         Pilot pilot = Pilot.getActive();
         if (pilot != null) {
            IniFile.gamma().setIniString("RestartAt", pilot.getURL());
         }

         Main.unregister(this);
      }
   }

   class StartupTeleport implements MainCallback {
      @Override
      public void mainCallback() {
         if (Console.getFrame() != null && Console.getFrame().isShowing()) {
            Main.unregister(this);

            try {
               TeleportAction.teleport(Gamma.this.startingURL, Gamma.this, true);
            } catch (Exception var2) {
            }
         }
      }
   }
}