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/scape/MusicManager.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/scape/MusicManager.java')
| -rw-r--r-- | NET/worlds/scape/MusicManager.java | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/NET/worlds/scape/MusicManager.java b/NET/worlds/scape/MusicManager.java new file mode 100644 index 0000000..9ef0cac --- /dev/null +++ b/NET/worlds/scape/MusicManager.java @@ -0,0 +1,258 @@ +package NET.worlds.scape; + +import NET.worlds.console.DialogReceiver; +import NET.worlds.console.Main; +import NET.worlds.console.MainCallback; +import NET.worlds.core.Sort; +import NET.worlds.network.URL; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.StringTokenizer; + +public class MusicManager implements MainCallback, DialogReceiver { + private static Hashtable managers = new Hashtable(); + private static MusicManagerDialog dialog; + private static boolean registered; + private static URL lastWorldURL; + private static boolean showDialog; + private static MusicManager curManager; + private static String lastRoomName = ""; + private static int lastCDTrack; + private static URL lastMIDIFile; + private String name; + private Hashtable tracks = new Hashtable(); + private Hashtable rooms = new Hashtable(); + private World world; + private String fileName; + private boolean maybeMusicChange; + + public static void showDialog() { + if (dialog == null) { + showDialog = true; + } + } + + public MusicManager() { + if (!registered) { + Main.register(this); + registered = true; + curManager = this; + } + } + + private MusicManager(World world, URL url) { + this.world = world; + this.name = world.getName(); + String file = url.unalias().toLowerCase(); + int index = file.lastIndexOf(".world"); + if (index != -1) { + this.fileName = url.unalias().substring(0, index) + ".music"; + this.load(); + } + } + + public World getWorld() { + return this.world; + } + + public String getName() { + return this.name; + } + + public String getFileName() { + return this.fileName; + } + + public Hashtable getMusic() { + return this.tracks; + } + + public MusicTrack getMusic(String name) { + return (MusicTrack)this.tracks.get(name); + } + + public Hashtable getRooms() { + return this.rooms; + } + + public MusicRoom getRoom(String name) { + return (MusicRoom)this.rooms.get(name); + } + + public synchronized void maybeChangedMusic() { + this.maybeMusicChange = true; + } + + public void save() { + PrintStream out = null; + + try { + out = new PrintStream(new FileOutputStream(this.fileName)); + Enumeration e = this.tracks.elements(); + String[] keys = Sort.sortKeys(this.tracks); + + for (int i = 0; i < keys.length; i++) { + MusicTrack m = (MusicTrack)this.tracks.get(keys[i]); + String midiName = m.getMIDIFileName(); + if (midiName.length() == 0) { + midiName = "-"; + } + + out.println("MUSIC|" + m.getName() + "|" + m.getVirtTrackNumber() + "|" + midiName + "|" + m.getLooping()); + } + + keys = Sort.sortKeys(this.rooms); + + for (int i = 0; i < keys.length; i++) { + MusicRoom r = (MusicRoom)this.rooms.get(keys[i]); + out.println("ROOM|" + r.getRoomName() + "|" + r.getMusicName()); + } + } catch (Exception var10) { + } finally { + if (out != null) { + out.close(); + } + } + } + + private void load() { + this.tracks.clear(); + this.rooms.clear(); + DataInputStream in = null; + String line = null; + + try { + in = new DataInputStream(new FileInputStream(this.fileName)); + + while ((line = in.readLine()) != null) { + StringTokenizer tok = new StringTokenizer(line, "|"); + String type = tok.nextToken(); + if (type.equals("MUSIC")) { + MusicTrack track = new MusicTrack(tok.nextToken(), Integer.parseInt(tok.nextToken()), tok.nextToken(), Boolean.valueOf(tok.nextToken())); + this.tracks.put(track.getName(), track); + } else if (type.equals("ROOM")) { + MusicRoom room = new MusicRoom(tok.nextToken(), tok.nextToken()); + this.rooms.put(room.getRoomName(), room); + } + } + } catch (Exception var14) { + } finally { + try { + if (in != null) { + in.close(); + } + } catch (IOException var13) { + } + } + } + + @Override + public void mainCallback() { + Pilot pilot = Pilot.getActive(); + if (pilot != null) { + World world = pilot.getWorld(); + boolean worldChange = false; + String newRoomName = ""; + if (world != null) { + URL url = world.getSourceURL(); + if (!url.equals(lastWorldURL)) { + MusicManager manager = (MusicManager)managers.get(url); + if (manager == null) { + manager = new MusicManager(world, url); + managers.put(url, manager); + } + + lastWorldURL = url; + curManager = manager; + worldChange = true; + } + + Room room = pilot.getRoom(); + if (room != null) { + String roomName = room.getName(); + if (roomName != null) { + newRoomName = roomName; + } + } + } else { + lastWorldURL = null; + } + + synchronized (this) { + if (worldChange || !newRoomName.equals(lastRoomName) || curManager.maybeMusicChange) { + curManager.maybeMusicChange = false; + lastRoomName = newRoomName; + MusicRoom mr = curManager.getRoom(lastRoomName); + CDAudio cd = CDAudio.get(); + boolean found = false; + if (mr != null) { + MusicTrack mt = curManager.getMusic(mr.getMusicName()); + if (mt != null) { + found = true; + int track = mt.getVirtTrackNumber(); + boolean changed = false; + if (track != cd.getCDTrack()) { + lastCDTrack = track; + cd.setCDTrack(track); + changed = true; + } + + String name = mt.getMIDIFileName(); + URL midiPath; + if (name.length() != 0) { + midiPath = URL.make(world.getSourceURL(), mt.getMIDIFileName()); + } else { + midiPath = cd.getDefaultMIDIFile(); + } + + if (!midiPath.equals(cd.getMIDIFile())) { + lastMIDIFile = midiPath; + cd.setMIDIFile(midiPath); + changed = true; + } + + if (changed) { + cd.change(mt.getLooping()); + } + } + } + + if (!found) { + if (lastCDTrack != 0 && cd.getCDTrack() == lastCDTrack || lastMIDIFile != null && lastMIDIFile.equals(cd.getMIDIFile())) { + cd.setCDTrack(0); + cd.setMIDIFile(cd.getDefaultMIDIFile()); + cd.stop(); + } + + lastCDTrack = 0; + lastMIDIFile = null; + } + } + } + } else { + lastWorldURL = null; + } + + if (lastWorldURL != null && dialog == null && showDialog) { + showDialog = false; + dialog = new MusicManagerDialog((MusicManager)managers.get(lastWorldURL)); + } + } + + @Override + public void dialogDone(Object who, boolean confirmed) { + if (confirmed) { + this.save(); + } else { + this.load(); + this.maybeChangedMusic(); + } + + dialog = null; + } +} |