-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameData.pde
executable file
·316 lines (260 loc) · 9.16 KB
/
GameData.pde
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
class GameData
{
boolean _completeData;
public boolean hasVideo;
public int slideshowPos = 0;
//text data to be displayed with the game, loaded from a JSON file in the parent directory for the game
String _name;
String _designers;
String _description;
//contains any command arguments the game needs to run properly with the menu
String _commandArgs;
//Game maker launches two exes, both of which must be killed on kill(). this contains the name of the other exe.
String[] associatedExes;
File _gameDirectory; //the parent directory for the game, containing folders for each platform, video/image data, and the JSON data file
File _executable; //the actual address of the executable, assigned in the constructor based on the global string platformName
File[] _videoFiles; //the address of the video, if any, to be displayed with the game.
File[] _imageFiles; //the address of the images, if any, to be displayed with the game.
public PImage[] _images;
public PImage _instructionsImage;
public Movie _video;
boolean findJSON(File gameDirectory)
{
File[] JSONs = gameDirectory.listFiles(jsonFilter);
if(JSONs.length != 0)
{
String tempGame = JSONs[0].toString();
JSONObject gameInfo = loadJSONObject(tempGame);
if(gameInfo != null)
{
_name = gameInfo.getString("name");
_designers = gameInfo.getString("designers");
//_name = gameInfo.getString("name", notFound);
_description = gameInfo.getString("description");
//_description = gameInfo.getString("description", notFound);
_commandArgs = gameInfo.getString("command arguments", "");
try{
JSONArray associatedExeList = gameInfo.getJSONArray("extra exes to kill");
associatedExes = new String[associatedExeList.size()];
for(int i = 0; i!= associatedExes.length; i++)
{
associatedExes[i] = associatedExeList.getJSONObject(i).getString("exe", "");
}
println("found extra exes to kill:");
for(int i = 0; i!= associatedExes.length; i++)
{
println(associatedExes[i]);
}
}catch(RuntimeException ex)
{
println("didn't find any extra exes to kill");
}
}
println("name: " + _name);
println("description: " + _description);
println("command arguments: " + _commandArgs);
return true;
}
else
{
println("name: didn't find a JSON object in " + _gameDirectory.getName());
println("description: didn't find a JSON object in " + _gameDirectory.getName());
return false;
}
}
GameData (File gameDirectory, PApplet parent)
{
println("\n----------\nCreating a new GameData object\n----------");
_completeData = true;
_gameDirectory = gameDirectory;
//look for the JSON file
_completeData = findJSON(gameDirectory);
//Get all child directories in the game folder
File[] childDirectories = gameDirectory.listFiles(directoryFilter);
for(File f : childDirectories)
{
String filename = f.getName();
//get image addresses
if(filename.equals("image"))
{
println("found image folder");
_imageFiles = f.listFiles();
listImages();
continue;
}
if(filename.equals("instructions"))
{
File[] imgs = f.listFiles();
if(imgs.length <= 0 )continue;
_instructionsImage = loadImage(imgs[0].getAbsolutePath());
}
//get video addresses
if(filename.equals("video"))
{
_videoFiles = f.listFiles(videoFilter);
listVideos();
continue;
}
//if current folder isn't the selected platform, skip it.
if(!filename.equals( platform ))
continue;
File[] contents = f.listFiles();
//if we found the folder but it has nothing in it, quit the loop and print an error.
if(contents.length<1)
{
println("executable: " + _gameDirectory.getName() + "/" + filename + " does not contain anything, skipping it.");
_completeData = false;
break;
}
//if we found a non-directory file, make that the executable.
if(contents.length != 0)
{
int i = 0;
for(; i!= contents.length; i++)
{
if(!contents[i].isDirectory() && contents[i].getName().indexOf(".exe") != -1)
{
_executable = contents[i];
break;
}
}
if(i == contents.length)
{
_completeData = false;
println("executable: "+ _gameDirectory.getName() + "/" + filename + " does not contain any files, only directories. skipping it");
}
else
{
println("executable: " + _executable.getName());
break;
}
}
}
loadImages();
loadVideo(parent, _videoFiles);
}
void loadImages()
{
//load the images from the addresses found.
if(_imageFiles == null) return;
_images = new PImage[_imageFiles.length];
for(int i = 0; i!= _imageFiles.length; i++)
_images[i] = loadImage(_imageFiles[i].getAbsolutePath());
}
void loadVideo(PApplet parent, File[] files)
{
if(files != null && files.length > 0)
{
_video = new Movie(parent, files[0].getAbsolutePath());
_video.stop();
// _video.volume(1);
hasVideo = true;
}
else
{
hasVideo = false;
//_video = noVid;
// _video.loop();
//_video.volume(0);
}
}
public void launchGame()
{
if(!gameIsRunning)
{
try
{
if(useJoyToKey == true)
{
String configName = stripExtension(_executable.getName());
Runtime.getRuntime().exec(joy2keyPath + " " + configName + ".cfg");
}
//String command = "cmd /c start /d \"" + _executable.getParentFile().getAbsolutePath() + "\" " + _executable.getName() + " " + _commandArgs + " /c start";
//String command = "pushd \"" + _executable.getParentFile().getAbsolutePath() + "\" & " + _executable.getName() + " " + _commandArgs;// + " /c start";
//print("commmmmmmmmmmmmmmmande : " + command);
String exePlusArgs = _executable.getName() + " " + _commandArgs;// + " /c start";
currentGame = Runtime.getRuntime().exec(_executable.getAbsolutePath() + " " + _commandArgs + "/c start", null, _executable.getParentFile());
//currentGame = Runtime.getRuntime().exec(_executable.getAbsolutePath() + " " + _commandArgs + "/c start");
println(staticWindow.getProcessID(currentGame));
//print(process);
println("Program should now open.");
gameIsRunning = true;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
void kill() {
if(gameIsRunning)
{
try {
System.out.println("killing.....");
loop();
//kill game
Runtime.getRuntime().exec("taskkill /F /IM "+_executable.getName());
System.out.println("Program should now be closed.");
//switch joy2key to the menu keybindings
Runtime.getRuntime().exec(joy2keyPath + " menuselect.cfg");
gameIsRunning = false;
} catch (Exception ex) {
System.out.print("???? : ");
ex.printStackTrace();
}
}
}
void listVideos()
{
println("videos: " + _videoFiles.length);
for(File vid : _videoFiles)
{
println(" " + vid.getName());
}
}
void listImages()
{
println("images: " + _imageFiles.length);
for(File img : _imageFiles)
{
println(" " + img.getName());
}
}
}
String stripExtension(String str)
{
if(str == null) return null;
int pos = str.lastIndexOf(".");
if(pos == -1) return str;
return str.substring(0,pos);
}
java.io.FilenameFilter directoryFilter = new java.io.FilenameFilter()
{
@Override
public boolean accept(File current, String name)
{
return new File(current, name).isDirectory();
}
};
java.io.FilenameFilter jsonFilter = new java.io.FilenameFilter() {
boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".json");
}
};
java.io.FilenameFilter imageFilter = new java.io.FilenameFilter()
{
boolean accept(File dir, String name)
{
return (name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".jpeg") || name.toLowerCase().endsWith(".png"));
}
};
java.io.FilenameFilter videoFilter = new java.io.FilenameFilter()
{
boolean accept(File dir, String name)
{
return (name.toLowerCase().endsWith(".mov") || name.toLowerCase().endsWith(".mp4") || name.toLowerCase().endsWith(".m4v") );
}
};
//This is needed for the video stuff.
void movieEvent(Movie m)
{
m.read();
}