diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/console/BlackBox.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/console/BlackBox.java')
| -rw-r--r-- | NET/worlds/console/BlackBox.java | 389 |
1 files changed, 389 insertions, 0 deletions
diff --git a/NET/worlds/console/BlackBox.java b/NET/worlds/console/BlackBox.java new file mode 100644 index 0000000..8ab5868 --- /dev/null +++ b/NET/worlds/console/BlackBox.java @@ -0,0 +1,389 @@ +package NET.worlds.console; + +import NET.worlds.core.IniFile; +import NET.worlds.core.Std; +import NET.worlds.network.Galaxy; +import NET.worlds.network.URL; +import NET.worlds.scape.Camera; +import NET.worlds.scape.Drone; +import NET.worlds.scape.FrameEvent; +import NET.worlds.scape.FrameHandler; +import NET.worlds.scape.HoloPilot; +import NET.worlds.scape.Pilot; +import NET.worlds.scape.Postrenderable; +import NET.worlds.scape.Sound; +import NET.worlds.scape.SoundPlayer; +import NET.worlds.scape.WMPSoundPlayer; +import NET.worlds.scape.WavSoundPlayer; +import java.awt.FileDialog; +import java.awt.Frame; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.Enumeration; +import java.util.Properties; +import java.util.Vector; + +public class BlackBox implements BlackBoxCallback, FrameHandler, Postrenderable { + private static BlackBox instance = new BlackBox(); + private boolean disable = IniFile.gamma().getIniInt("disableRecorder", 0) == 1; + private String autoFile = null; + private SoundPlayer autoSound = null; + static final int PLAYING = 0; + static final int RECORDING = 1; + static final int STOPPED = 2; + private int state = 2; + static final int CHATCMD = 0; + static final int TELEPORTCMD = 1; + static final int ACTORLISTCMD = 2; + static final int MOVEDRONECMD = 3; + static final int OBJCLICKEDCMD = 4; + static final int APPEARDRONECMD = 5; + static final int DISAPPEARDRONECMD = 6; + static final int DRONEBITMAPCMD = 7; + static final int DRONEDELTACMD = 8; + static final int ANIMATECMD = 9; + public static final String PilotID = "@Pilot"; + private Vector<BlackBoxCommand> commandList = new Vector<BlackBoxCommand>(); + private int commandIdx; + private long basetime; + static final int NOCMD = 0; + static final int PLAYCMD = 1; + static final int RECCMD = 2; + static final int STOPCMD = 3; + private int pendingCommand = 0; + static final int FILE_VERSION = 1; + + public static BlackBox getInstance() { + return instance; + } + + private BlackBox() { + this.autoFile = IniFile.override().getIniString("AutoPlaybackFile", ""); + if (!this.autoFile.equals("")) { + this.play(); + } else { + this.autoFile = null; + } + + String soundFile = IniFile.override().getIniString("AutoPlaybackSound", ""); + if (!soundFile.equals("")) { + Sound owner = new Sound(URL.make(soundFile)); + if (soundFile.toLowerCase().endsWith(".wav")) { + this.autoSound = new WavSoundPlayer(owner); + } else { + this.autoSound = new WMPSoundPlayer(owner); + } + } + } + + @Override + public void finalize() { + this.stop(); + } + + @Override + public void postrender(Camera cam) { + if (!this.disable) { + if (Std.getSynchronizedTime() % 2 != 0) { + if (this.isPlaying()) { + cam.nDrawText(Console.message("PLAY"), 10, 10, 18, 16711680); + } else if (this.isRecording()) { + cam.nDrawText(Console.message("REC"), 10, 10, 18, 16711680); + } + } + } + } + + public synchronized void record() { + this.pendingCommand = 2; + } + + private boolean doRecord() { + if (this.state != 2) { + this.stop(); + } + + this.commandList.removeAllElements(); + this.basetime = Std.getFastTime(); + String url = ""; + Pilot pilot = Pilot.getActive(); + if (pilot == null) { + return false; + } else { + url = pilot.getURL(); + this.state = 1; + this.submitEvent(new BBTeleportCommand(url)); + URL avatar = Console.getActive().getAvatarName(); + if (avatar != null) { + this.submitEvent(new BBDroneBitmapCommand("@Pilot", avatar.toString())); + } + + ArmyOfZombies.instance().zombify(); + return true; + } + } + + public synchronized void play() { + this.pendingCommand = 1; + } + + public synchronized void play(URL recFile) { + this.autoFile = recFile.unalias(); + this.pendingCommand = 1; + } + + private void doPlay() { + if (this.state != 2) { + this.stop(); + } + + if (this.restore()) { + Galaxy.forceOffline(false); + Console.getActive().setChatname(""); + Pilot p = Pilot.getActive(); + if (p instanceof HoloPilot) { + HoloPilot hp = (HoloPilot)p; + hp.removeSmoothDriver(); + } + + if (this.autoSound != null) { + this.autoSound.start(1); + this.autoSound = null; + } + + this.commandIdx = 0; + this.basetime = Std.getFastTime(); + this.state = 0; + } + } + + @Override + public void commandCompleted(BlackBoxCommand c, boolean ok) { + if (ok) { + this.commandIdx++; + } else { + System.out.println("Failed command!"); + this.stop(); + } + } + + @Override + public boolean handle(FrameEvent e) { + if (this.disable) { + return false; + } else { + switch (this.pendingCommand) { + case 1: + this.doPlay(); + this.pendingCommand = 0; + break; + case 2: + this.doRecord(); + this.pendingCommand = 0; + break; + case 3: + this.doStop(); + this.pendingCommand = 0; + } + + if (this.isPlaying()) { + if (this.commandIdx >= this.commandList.size()) { + this.stop(); + return false; + } + + long elapsedTime = Std.getFastTime() - this.basetime; + BlackBoxCommand c = this.commandList.elementAt(this.commandIdx); + if (c.startTime <= elapsedTime) { + c.execute(this); + } + + Pilot p = Pilot.getActive(); + if (p instanceof HoloPilot) { + HoloPilot hp = (HoloPilot)p; + Drone d = hp.getInternalDrone(); + if (d != null) { + d.interpolate(e.time, 2000, p); + p.setZ(p.getPosition().z + hp.getSmoothDriver().getEyeHeight()); + } + } + } + + return false; + } + } + + public boolean isRecording() { + return this.state == 1; + } + + public boolean isPlaying() { + return this.state == 0; + } + + public void submitEvent(BlackBoxCommand c) { + if (!this.disable) { + if (this.isRecording()) { + c.timestamp(this.basetime); + this.commandList.addElement(c); + } + } + } + + public synchronized void stop() { + this.pendingCommand = 3; + } + + private void doStop() { + if (this.state != 2) { + if (this.isRecording()) { + this.save(); + } + + if (this.isPlaying()) { + ArmyOfZombies.instance().killZombies(); + Pilot p = Pilot.getActive(); + if (p instanceof HoloPilot) { + HoloPilot hp = (HoloPilot)p; + hp.returnSmoothDriver(); + } + + Console c = Console.getActive(); + if (c instanceof DefaultConsole) { + DefaultConsole dc = (DefaultConsole)c; + dc.getGalaxy().localForceOnline(); + dc.getGalaxy().waitForConnection(dc); + } + } + + this.state = 2; + } + } + + private void save() { + Frame parent = Console.getFrame(); + Properties p = System.getProperties(); + String oldDir = p.getProperty("user.dir"); + FileDialog fd = new FileDialog(parent, Console.message("Save-recording"), 1); + fd.setFile("record.rec"); + fd.show(); + String fname = fd.getFile(); + String path = fd.getDirectory(); + p.remove("user.dir"); + p.put("user.dir", oldDir); + System.setProperties(p); + if (fname != null) { + File f = new File(path, fname); + + try { + FileOutputStream fos = new FileOutputStream(f); + DataOutputStream dos = new DataOutputStream(fos); + dos.writeInt(1); + Enumeration<BlackBoxCommand> e = this.commandList.elements(); + + while (e.hasMoreElements()) { + BlackBoxCommand cmd = e.nextElement(); + cmd.save(dos); + } + + dos.close(); + fos.close(); + } catch (Exception var12) { + System.out.println(var12); + } + } + } + + private boolean restore() { + if (this.autoFile != null) { + this.restoreFile(new File(this.autoFile)); + this.autoFile = null; + return true; + } else { + Frame parent = Console.getFrame(); + Properties p = System.getProperties(); + String oldDir = p.getProperty("user.dir"); + FileDialog fd = new FileDialog(parent, Console.message("Load-recording"), 0); + fd.show(); + String filename = fd.getFile(); + String directory = fd.getDirectory(); + p.remove("user.dir"); + p.put("user.dir", oldDir); + System.setProperties(p); + if (filename == null) { + return false; + } else { + File f = new File(directory, filename); + this.restoreFile(f); + return true; + } + } + } + + private void restoreFile(File f) { + this.commandList.removeAllElements(); + + try { + FileInputStream fis = new FileInputStream(f); + DataInputStream dis = new DataInputStream(fis); + int version = dis.readInt(); + if (version != 1) { + Console.println("Invalid recorder file."); + return; + } + + try { + while (true) { + int cmdId = dis.readInt(); + BlackBoxCommand cmd = null; + switch (cmdId) { + case 0: + cmd = new BBChatCommand(); + break; + case 1: + cmd = new BBTeleportCommand(); + break; + case 2: + default: + System.out.println("Error! Unknown command type."); + break; + case 3: + cmd = new BBMoveDroneCommand(); + break; + case 4: + cmd = new BBWObjClickedCommand(); + break; + case 5: + cmd = new BBAppearDroneCommand(); + break; + case 6: + cmd = new BBDisappearDroneCommand(); + break; + case 7: + cmd = new BBDroneBitmapCommand(); + break; + case 8: + cmd = new BBDroneDeltaPosCommand(); + break; + case 9: + cmd = new BBAnimateDroneCommand(); + } + + if (cmd != null) { + cmd.load(dis); + this.commandList.addElement(cmd); + } + } + } catch (EOFException var7) { + dis.close(); + fis.close(); + } + } catch (Exception var8) { + System.out.println(var8); + } + } +} |