1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #define FORBIDDEN_SYMBOL_ALLOW_ALL
24 
25 #include <common/scummsys.h>
26 #include <engines/engine.h>
27 #include <engines/metaengine.h>
28 #include <engines/game.h>
29 #include <base/plugins.h>
30 #include <common/fs.h>
31 #include <common/events.h>
32 #include <common/config-manager.h>
33 #include "dc.h"
34 #include "icon.h"
35 #include "label.h"
36 
37 #include <ronin/gddrive.h>
38 
39 
40 #define MAX_GAMES 100
41 #define MAX_DIR 100
42 #define MAX_PLUGIN_DIRS 100
43 
44 
draw_solid_quad(float x1,float y1,float x2,float y2,int c0,int c1,int c2,int c3)45 void draw_solid_quad(float x1, float y1, float x2, float y2,
46 		     int c0, int c1, int c2, int c3)
47 {
48   struct polygon_list mypoly;
49   struct packed_colour_vertex_list myvertex;
50 
51   mypoly.cmd =
52     TA_CMD_POLYGON|TA_CMD_POLYGON_TYPE_OPAQUE|TA_CMD_POLYGON_SUBLIST|
53     TA_CMD_POLYGON_STRIPLENGTH_2|TA_CMD_POLYGON_PACKED_COLOUR|
54     TA_CMD_POLYGON_GOURAUD_SHADING;
55   mypoly.mode1 = TA_POLYMODE1_Z_ALWAYS|TA_POLYMODE1_NO_Z_UPDATE;
56   mypoly.mode2 =
57     TA_POLYMODE2_BLEND_SRC|TA_POLYMODE2_FOG_DISABLED;
58   mypoly.texture = 0;
59 
60   mypoly.red = mypoly.green = mypoly.blue = mypoly.alpha = 0;
61 
62   ta_commit_list(&mypoly);
63 
64   myvertex.cmd = TA_CMD_VERTEX;
65   myvertex.ocolour = 0;
66   myvertex.z = 0.5;
67   myvertex.u = 0.0;
68   myvertex.v = 0.0;
69 
70   myvertex.colour = c0;
71   myvertex.x = x1;
72   myvertex.y = y1;
73   ta_commit_list(&myvertex);
74 
75   myvertex.colour = c1;
76   myvertex.x = x2;
77   ta_commit_list(&myvertex);
78 
79   myvertex.colour = c2;
80   myvertex.x = x1;
81   myvertex.y = y2;
82   ta_commit_list(&myvertex);
83 
84   myvertex.colour = c3;
85   myvertex.x = x2;
86   myvertex.cmd |= TA_CMD_VERTEX_EOS;
87   ta_commit_list(&myvertex);
88 }
89 
draw_trans_quad(float x1,float y1,float x2,float y2,int c0,int c1,int c2,int c3)90 void draw_trans_quad(float x1, float y1, float x2, float y2,
91 		     int c0, int c1, int c2, int c3)
92 {
93   struct polygon_list mypoly;
94   struct packed_colour_vertex_list myvertex;
95 
96   mypoly.cmd =
97     TA_CMD_POLYGON|TA_CMD_POLYGON_TYPE_TRANSPARENT|TA_CMD_POLYGON_SUBLIST|
98     TA_CMD_POLYGON_STRIPLENGTH_2|TA_CMD_POLYGON_PACKED_COLOUR|
99     TA_CMD_POLYGON_GOURAUD_SHADING;
100   mypoly.mode1 = TA_POLYMODE1_Z_ALWAYS|TA_POLYMODE1_NO_Z_UPDATE;
101   mypoly.mode2 =
102     TA_POLYMODE2_BLEND_SRC_ALPHA|TA_POLYMODE2_BLEND_DST_INVALPHA|
103     TA_POLYMODE2_FOG_DISABLED|TA_POLYMODE2_ENABLE_ALPHA;
104   mypoly.texture = 0;
105 
106   mypoly.red = mypoly.green = mypoly.blue = mypoly.alpha = 0;
107 
108   ta_commit_list(&mypoly);
109 
110   myvertex.cmd = TA_CMD_VERTEX;
111   myvertex.ocolour = 0;
112   myvertex.z = 0.5;
113   myvertex.u = 0.0;
114   myvertex.v = 0.0;
115 
116   myvertex.colour = c0;
117   myvertex.x = x1;
118   myvertex.y = y1;
119   ta_commit_list(&myvertex);
120 
121   myvertex.colour = c1;
122   myvertex.x = x2;
123   ta_commit_list(&myvertex);
124 
125   myvertex.colour = c2;
126   myvertex.x = x1;
127   myvertex.y = y2;
128   ta_commit_list(&myvertex);
129 
130   myvertex.colour = c3;
131   myvertex.x = x2;
132   myvertex.cmd |= TA_CMD_VERTEX_EOS;
133   ta_commit_list(&myvertex);
134 }
135 
136 
137 struct Game
138 {
139   char dir[256];
140   char filename_base[256];
141   char text[256];
142   Common::Language language;
143   Common::Platform platform;
144   Icon icon;
145   Label label;
146 };
147 
148 struct Dir
149 {
150   char name[252];
151   char deficon[256];
152   Common::FSNode node;
153 };
154 
155 static Game the_game;
156 
isIcon(const Common::FSNode & entry)157 static bool isIcon(const Common::FSNode &entry)
158 {
159 	return entry.getDisplayName().hasSuffixIgnoreCase(".ICO");
160 }
161 
loadIcon(Game & game,Dir * dirs,int num_dirs)162 static bool loadIcon(Game &game, Dir *dirs, int num_dirs)
163 {
164   char icofn[520];
165   sprintf(icofn, "%s%s.ICO", game.dir, game.filename_base);
166   if (game.icon.load(icofn))
167     return true;
168   for (int i=0; i<num_dirs; i++)
169     if (!strcmp(dirs[i].name, game.dir) &&
170        dirs[i].deficon[0]) {
171       sprintf(icofn, "%s%s", game.dir, dirs[i].deficon);
172       if (game.icon.load(icofn))
173 	return true;
174       break;
175     }
176   return false;
177 }
178 
makeDefIcon(Icon & icon)179 static void makeDefIcon(Icon &icon)
180 {
181 #include "deficon.h"
182   icon.load(scummvm_icon, sizeof(scummvm_icon));
183 }
184 
sameOrSubdir(const char * dir1,const char * dir2)185 static bool sameOrSubdir(const char *dir1, const char *dir2)
186 {
187   int l1 = strlen(dir1), l2 = strlen(dir2);
188   if (l1<=l2)
189     return !strcmp(dir1, dir2);
190   else
191     return !memcmp(dir1, dir2, l2);
192 }
193 
uniqueGame(const char * base,const char * dir,Common::Language lang,Common::Platform plf,Game * games,int cnt)194 static bool uniqueGame(const char *base, const char *dir,
195 		       Common::Language lang, Common::Platform plf,
196 		       Game *games, int cnt)
197 {
198   while (cnt--)
199     if (/*Don't detect the same game in a subdir,
200 	  this is a workaround for the detector bug in toon... */
201 	sameOrSubdir(dir, games->dir) &&
202 	/*!strcmp(dir, games->dir) &&*/
203 	!scumm_stricmp(base, games->filename_base) &&
204 	lang == games->language &&
205 	plf == games->platform)
206       return false;
207     else
208       games++;
209   return true;
210 }
211 
findGames(Game * games,int max,bool use_ini)212 static int findGames(Game *games, int max, bool use_ini)
213 {
214   Dir *dirs = new Dir[MAX_DIR];
215   int curr_game = 0, curr_dir = 0, num_dirs = 0;
216 
217   if (use_ini) {
218     ConfMan.loadDefaultConfigFile();
219     const Common::ConfigManager::DomainMap &game_domains = ConfMan.getGameDomains();
220     for(Common::ConfigManager::DomainMap::const_iterator i =
221 	  game_domains.begin(); curr_game < max && i != game_domains.end(); i++) {
222       Common::String path = (*i)._value["path"];
223       if (path.size() && path.lastChar() != '/')
224 	path += "/";
225       int j;
226       for (j=0; j<num_dirs; j++)
227 	if (path.equals(dirs[j].node.getPath()))
228 	  break;
229       if (j >= num_dirs) {
230 	if (num_dirs >= MAX_DIR)
231 	  continue;
232 	dirs[j = num_dirs++].node = Common::FSNode(path);
233       }
234       if (curr_game < max) {
235 	strcpy(games[curr_game].filename_base, (*i)._key.c_str());
236 	strncpy(games[curr_game].dir, dirs[j].node.getPath().c_str(), 256);
237 	games[curr_game].dir[255] = '\0';
238 	games[curr_game].language = Common::UNK_LANG;
239 	games[curr_game].platform = Common::kPlatformUnknown;
240 	strcpy(games[curr_game].text, (*i)._value["description"].c_str());
241 	curr_game++;
242       }
243     }
244   } else {
245     dirs[num_dirs++].node = Common::FSNode("");
246   }
247 
248   while ((curr_game < max || use_ini) && curr_dir < num_dirs) {
249     strncpy(dirs[curr_dir].name, dirs[curr_dir].node.getPath().c_str(), 251);
250     dirs[curr_dir].name[250] = '\0';
251     if (!dirs[curr_dir].name[0] ||
252 	dirs[curr_dir].name[strlen(dirs[curr_dir].name)-1] != '/')
253       strcat(dirs[curr_dir].name, "/");
254     dirs[curr_dir].deficon[0] = '\0';
255     Common::FSList files, fslist;
256     dirs[curr_dir++].node.getChildren(fslist, Common::FSNode::kListAll);
257     for (Common::FSList::const_iterator entry = fslist.begin(); entry != fslist.end();
258 	 ++entry) {
259       if (entry->isDirectory()) {
260 	if (!use_ini && num_dirs < MAX_DIR) {
261 	  dirs[num_dirs].node = *entry;
262 	  num_dirs++;
263 	}
264 	/* Toonstruck detector needs directories to be present too */
265 	if(!use_ini)
266 	  files.push_back(*entry);
267       } else
268 	if (isIcon(*entry))
269 	  strcpy(dirs[curr_dir-1].deficon, entry->getDisplayName().c_str());
270 	else if(!use_ini)
271 	  files.push_back(*entry);
272     }
273 
274     if (!use_ini) {
275       DetectionResults detectionResults = EngineMan.detectGames(files);
276       DetectedGames candidates = detectionResults.listRecognizedGames();
277 
278       for (DetectedGames::const_iterator ge = candidates.begin();
279 	   ge != candidates.end(); ++ge)
280 	if (curr_game < max) {
281 	  strcpy(games[curr_game].filename_base, ge->gameId.c_str());
282 	  strcpy(games[curr_game].dir, dirs[curr_dir-1].name);
283 	  games[curr_game].language = ge->language;
284 	  games[curr_game].platform = ge->platform;
285 	  if (uniqueGame(games[curr_game].filename_base,
286 			 games[curr_game].dir,
287 			 games[curr_game].language,
288 			 games[curr_game].platform, games, curr_game)) {
289 
290 	    strcpy(games[curr_game].text, ge->description.c_str());
291 #if 0
292 	    printf("Registered game <%s> (l:%d p:%d) in <%s> <%s> because of <%s> <*>\n",
293 		   games[curr_game].text,
294 		   (int)games[curr_game].language,
295 		   (int)games[curr_game].platform,
296 		   games[curr_game].dir, games[curr_game].filename_base,
297 		   dirs[curr_dir-1].name);
298 #endif
299 	    curr_game++;
300 	  }
301 	}
302     }
303   }
304 
305   for (int i=0; i<curr_game; i++)
306     if (!loadIcon(games[i], dirs, num_dirs))
307       makeDefIcon(games[i].icon);
308   delete[] dirs;
309   return curr_game;
310 }
311 
getCdState()312 int getCdState()
313 {
314   unsigned int param[4];
315   gdGdcGetDrvStat(param);
316   return param[0];
317 }
318 
drawBackground()319 static void drawBackground()
320 {
321   draw_solid_quad(20.0, 20.0, 620.0, 460.0,
322 		  0xff0000, 0x00ff00, 0x0000ff, 0xffffff);
323 }
324 
waitForDisk()325 void waitForDisk()
326 {
327   Label lab;
328   int wasopen = 0;
329   ta_sync();
330   void *mark = ta_txmark();
331   lab.create_texture("Please insert game CD.");
332   //printf("waitForDisk, cdstate = %d\n", getCdState());
333   for (;;) {
334     int s = getCdState();
335     if (s >= 6)
336       wasopen = 1;
337     if (s > 0 && s < 6 && wasopen) {
338       cdfs_reinit();
339       chdir("/");
340       chdir("/");
341       ta_sync();
342       ta_txrelease(mark);
343       return;
344     }
345 
346     ta_begin_frame();
347 
348     drawBackground();
349 
350     ta_commit_end();
351 
352     lab.draw(166.0, 200.0, 0xffff2020);
353 
354     ta_commit_frame();
355 
356     int mousex = 0, mousey = 0;
357     byte shiftFlags;
358 
359     int mask = getimask();
360     setimask(15);
361     handleInput(locked_get_pads(), mousex, mousey, shiftFlags);
362     setimask(mask);
363   }
364 }
365 
drawGameLabel(Game & game,int pal,float x,float y,unsigned int argb,int fade=0,float scale=1.0)366 static void drawGameLabel(Game &game, int pal, float x, float y,
367 			  unsigned int argb, int fade = 0, float scale = 1.0)
368 {
369   unsigned int fade_alpha = (255-fade)<<24;
370 
371   game.icon.draw(x, y, x+32.0*scale, y+32.0*scale, pal, 0xffffff|fade_alpha);
372   game.label.draw(x+54.0*scale, y+4.0*scale, argb|fade_alpha, scale);
373 }
374 
gameMenu(Game * games,int num_games)375 int gameMenu(Game *games, int num_games)
376 {
377   int top_game = 0, selector_pos = 0;
378   int mousex = 0, mousey = 0;
379   float y;
380 
381   if (!num_games)
382     return -1;
383 
384   for (;;) {
385 
386     if (getCdState()>=6)
387       return -1;
388 
389     ta_begin_frame();
390 
391     drawBackground();
392 
393     ta_commit_end();
394 
395     y = 40.0;
396     for (int i=top_game, cnt=0; cnt<10 && i<num_games; i++, cnt++) {
397       int pal = 48+(i&15);
398 
399       if (cnt == selector_pos)
400 	draw_trans_quad(100.0, y, 590.0, y+32.0,
401 			0x7000ff00, 0x7000ff00, 0x7000ff00, 0x7000ff00);
402 
403       games[i].icon.setPalette(pal);
404       drawGameLabel(games[i], pal, 50.0, y, (cnt == selector_pos?
405 					     0xffff00 : 0xffffff));
406       y += 40.0;
407     }
408 
409     ta_commit_frame();
410 
411     byte shiftFlags;
412     int event;
413 
414     int mask = getimask();
415     setimask(15);
416     event = handleInput(locked_get_pads(), mousex, mousey, shiftFlags);
417     setimask(mask);
418 
419     if (event==-Common::EVENT_LBUTTONDOWN || event==Common::KEYCODE_RETURN || event==Common::KEYCODE_F5) {
420       int selected_game = top_game + selector_pos;
421 
422       for (int fade=0; fade<=256; fade+=4) {
423 
424 	ta_begin_frame();
425 
426 	drawBackground();
427 
428 	ta_commit_end();
429 
430 	y = 40.0;
431 
432 	if (fade < 256)
433 	  for (int i=top_game, cnt=0; cnt<10 && i<num_games;
434 	      i++, cnt++, y += 40.0)
435 	    if (cnt != selector_pos)
436 	      drawGameLabel(games[i], 48+(i&15), 50.0, y, 0xffffff, fade);
437 
438 	y = (40.0/256.0 * (selector_pos + 1))*(256-fade) + 80.0/256.0*fade;
439 	float x = 50.0/256.0*(256-fade) + 160.0/256.0*fade;
440 	float scale = 1.0+9.0/256.0*fade;
441 
442 	drawGameLabel(games[selected_game], 48+(selected_game&15), x, y,
443 		      0xffff00, 0, scale);
444 
445 	ta_commit_frame();
446       }
447       return selected_game;
448     }
449 
450     if (mousey>=16) {
451       if (selector_pos + top_game + 1 < num_games)
452 	if (++selector_pos >= 10) {
453 	  --selector_pos;
454 	  ++top_game;
455 	}
456       mousey -= 16;
457     } else if (mousey<=-16) {
458       if (selector_pos + top_game > 0)
459 	if (--selector_pos < 0) {
460 	  ++selector_pos;
461 	  --top_game;
462 	}
463       mousey += 16;
464     }
465   }
466 }
467 
selectGame(char * & ret,char * & dir_ret,Common::Language & lang_ret,Common::Platform & plf_ret,Icon & icon)468 bool selectGame(char *&ret, char *&dir_ret, Common::Language &lang_ret, Common::Platform &plf_ret, Icon &icon)
469 {
470   Game *games = new Game[MAX_GAMES];
471   int selected, num_games;
472 
473   ta_sync();
474   void *mark = ta_txmark();
475 
476   for (;;) {
477     num_games = findGames(games, MAX_GAMES, true);
478     if (!num_games)
479       num_games = findGames(games, MAX_GAMES, false);
480 
481     for (int i=0; i<num_games; i++) {
482       games[i].icon.create_texture();
483       games[i].label.create_texture(games[i].text);
484     }
485 
486     selected = gameMenu(games, num_games);
487 
488     ta_sync();
489     ta_txrelease(mark);
490 
491     if (selected == -1)
492       waitForDisk();
493     else
494       break;
495 
496   }
497 
498   if (selected >= num_games)
499     selected = -1;
500 
501   if (selected >= 0)
502     the_game = games[selected];
503 
504   delete[] games;
505 
506   if (selected>=0) {
507 #if 0
508     chdir(the_game.dir);
509 #else
510     chdir("/");
511     dir_ret = the_game.dir;
512 #endif
513     ret = the_game.filename_base;
514     lang_ret = the_game.language;
515     plf_ret = the_game.platform;
516     icon = the_game.icon;
517     return true;
518   } else
519     return false;
520 }
521 
522 #ifdef DYNAMIC_MODULES
findPluginDirs(Game * plugin_dirs,int max,const Common::FSNode & base)523 static int findPluginDirs(Game *plugin_dirs, int max, const Common::FSNode &base)
524 {
525   Common::FSList fslist;
526   int curr_dir = 0;
527   base.getChildren(fslist, Common::FSNode::kListDirectoriesOnly);
528   for (Common::FSList::const_iterator entry = fslist.begin(); entry != fslist.end();
529        ++entry) {
530     if (entry->isDirectory()) {
531       if (curr_dir >= max)
532 	break;
533       strncpy(plugin_dirs[curr_dir].dir, (*entry).getPath().c_str(), 256);
534       strncpy(plugin_dirs[curr_dir].text, (*entry).getDisplayName().c_str(), 256);
535       plugin_dirs[curr_dir].icon.load(NULL, 0, 0);
536       curr_dir++;
537     }
538   }
539   return curr_dir;
540 }
541 
selectPluginDir(Common::String & selection,const Common::FSNode & base)542 bool selectPluginDir(Common::String &selection, const Common::FSNode &base)
543 {
544   Game *plugin_dirs = new Game[MAX_PLUGIN_DIRS];
545   int selected, num_plugin_dirs;
546 
547   ta_sync();
548   void *mark = ta_txmark();
549 
550   num_plugin_dirs = findPluginDirs(plugin_dirs, MAX_PLUGIN_DIRS, base);
551 
552   for (int i=0; i<num_plugin_dirs; i++) {
553     plugin_dirs[i].icon.create_texture();
554     plugin_dirs[i].label.create_texture(plugin_dirs[i].text);
555   }
556 
557   selected = gameMenu(plugin_dirs, num_plugin_dirs);
558 
559   ta_sync();
560   ta_txrelease(mark);
561 
562   if (selected >= num_plugin_dirs)
563     selected = -1;
564 
565   if (selected >= 0)
566     selection = plugin_dirs[selected].dir;
567 
568   delete[] plugin_dirs;
569 
570   return selected >= 0;
571 }
572 #endif
573