summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/MusicManager.java
blob: 9ef0cac7e4329a0c53da4fea1cd52d3bf7fd60eb (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
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;
   }
}