summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/CDPlayerAction.java
blob: 0d1955ab06f941dc61f57a6fdc17a4c00cb1a91f (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.Main;
import NET.worlds.console.MainCallback;
import NET.worlds.console.MainTerminalCallback;
import java.io.IOException;

public class CDPlayerAction extends Action implements Runnable, MainCallback, MainTerminalCallback {
   private static final boolean DISABLE_ALL = true;
   private static final int STATE_INITIAL = 0;
   private static final int STATE_WAITING = 1;
   private static final int STATE_DONE = 2;
   private static int numDrives = -1;
   private static CDPlayerAction active;
   private String artist;
   private String title;
   private String hash;
   private int start;
   private int stop;
   private boolean isStopper;
   private boolean cancel;
   private int state = 0;
   private static Object classCookie = new Object();

   private boolean same(String str1, String str2) {
      if (str1 == null) {
         return str2 == null;
      } else {
         return str2 == null ? false : str1.equals(str2);
      }
   }

   private boolean same(CDPlayerAction other) {
      return other != null
         && this.same(this.artist, other.artist)
         && this.same(this.title, other.title)
         && this.same(this.hash, other.hash)
         && this.start == other.start
         && this.stop == other.stop;
   }

   @Override
   public Persister trigger(Event e, Persister seqID) {
      return null;
   }

   @Override
   public void run() {
      if (numDrives == -1) {
         numDrives = getNumDrives();
      }

      for (int i = 0; i < numDrives; i++) {
         try {
            CDTrackInfo trackInfo = getTrackList(i);
            String curHash = null;
            if (trackInfo != null) {
               curHash = CDDBHash.hashString(trackInfo);
               if (curHash.equals(this.hash)) {
                  this.play(i);
                  break;
               }
            }

            if (this.cancel) {
               break;
            }
         } catch (IOException var4) {
         }
      }

      this.state = 2;
   }

   @Override
   public void mainCallback() {
   }

   @Override
   public void terminalCallback() {
      this.cancel = true;
      if (this.state == 2) {
         Main.unregister(this);
      }
   }

   private void play(int drive) throws IOException {
      int driveID;
      if ((driveID = openDrive(drive)) != 0) {
         playAudio(driveID, this.start, this.stop);

         while (isPlaying(driveID)) {
            try {
               Thread.sleep(1000L);
            } catch (InterruptedException var4) {
            }

            if (this.cancel) {
               stopAudio(driveID);
               break;
            }
         }

         closeDrive(driveID);
      }
   }

   public static CDTrackInfo getTrackList(int drive) throws IOException {
      CDTrackInfo ret = null;
      int driveID;
      if ((driveID = openDrive(drive)) != 0) {
         ret = getDriveTrackList(driveID);
         closeDrive(driveID);
      }

      return ret;
   }

   public static native int getNumDrives();

   public static native int getDriveLetterOffset(int var0);

   public static native int openDrive(int var0) throws IOException;

   public static native void closeDrive(int var0) throws IOException;

   public static native void checkDrive(int var0) throws IOException;

   public static native CDTrackInfo getDriveTrackList(int var0) throws IOException;

   public static native void playAudio(int var0, int var1, int var2) throws IOException;

   public static native void stopAudio(int var0) throws IOException;

   public static native void pauseAudio(int var0) throws IOException;

   public static native void resumeAudio(int var0) throws IOException;

   public static native boolean isPlaying(int var0) throws IOException;

   public static native int getPosition(int var0) throws IOException;

   public static native void setNTAutoPlayCode(int var0);

   public static native boolean launchVolumeControlApp();

   @Override
   public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
      Object ret = null;
      switch (index - offset) {
         case 0:
            if (mode == 0) {
               ret = StringPropertyEditor.make(new Property(this, index, "Artist"));
            } else if (mode == 1) {
               ret = this.artist;
            } else if (mode == 2) {
               this.artist = (String)value;
            }
            break;
         case 1:
            if (mode == 0) {
               ret = StringPropertyEditor.make(new Property(this, index, "Title"));
            } else if (mode == 1) {
               ret = this.title;
            } else if (mode == 2) {
               this.title = (String)value;
            }
            break;
         case 2:
            if (mode == 0) {
               ret = StringPropertyEditor.make(new Property(this, index, "Disk ID (Edit or Delete to read CD)").allowSetNull());
            } else if (mode == 1) {
               if (this.hash == null) {
                  try {
                     CDTrackInfo trackList = getTrackList(0);
                     if (trackList != null) {
                        this.hash = CDDBHash.hashString(trackList);
                     }
                  } catch (IOException var7) {
                  }
               }

               ret = this.hash;
            } else if (mode == 2) {
               this.hash = (String)value;
            }
            break;
         case 3:
            if (mode == 0) {
               ret = CDPositionPropertyEditor.make(new Property(this, index, "Start (75ths of a second)"), false);
            } else if (mode == 1) {
               ret = new Integer(this.start);
            } else if (mode == 2) {
               this.start = (Integer)value;
            }
            break;
         case 4:
            if (mode == 0) {
               ret = CDPositionPropertyEditor.make(new Property(this, index, "Stop (75ths of a second)"), true);
            } else if (mode == 1) {
               ret = new Integer(this.stop);
            } else if (mode == 2) {
               this.stop = (Integer)value;
            }
            break;
         case 5:
            if (mode == 0) {
               ret = BooleanPropertyEditor.make(new Property(this, index, "Stop identical CDPlayerAction"), "No", "Yes");
            } else if (mode == 1) {
               ret = new Boolean(this.isStopper);
            } else if (mode == 2) {
               this.isStopper = (Boolean)value;
            }
            break;
         default:
            ret = super.properties(index, offset + 6, mode, value);
      }

      return ret;
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(2, classCookie);
      super.saveState(s);
      s.saveString(this.artist);
      s.saveString(this.title);
      s.saveString(this.hash);
      s.saveInt(this.start);
      s.saveInt(this.stop);
      s.saveBoolean(this.isStopper);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 1:
            super.restoreState(r);
            this.artist = r.restoreString();
            this.title = r.restoreString();
            this.hash = r.restoreString();
            this.start = r.restoreInt();
            this.stop = r.restoreInt();
            break;
         case 2:
            super.restoreState(r);
            this.artist = r.restoreString();
            this.title = r.restoreString();
            this.hash = r.restoreString();
            this.start = r.restoreInt();
            this.stop = r.restoreInt();
            this.isStopper = r.restoreBoolean();
            break;
         default:
            throw new TooNewException();
      }
   }
}