Main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <direct.h>
  4. #include <process.h>
  5. #include "include.hpp"
  6. #include <x32.h>
  7. void check_disk_drive(void);
  8. void decode_argv_1(int args,char **arglist);
  9. void decode_argv_2(int args,char **arglist);
  10. void _cdecl handler(struct FAULT_STRUC *fault);
  11. void kill_mouse(void);
  12. void initialise(void);
  13. void intro(void);
  14. void mainloop(void);
  15. void load_config(void);
  16. int force_roland = 0; // set this to force roland
  17. int do_a_replay=0;
  18. int force_sounds_off=0; // Set this to prevent music drivers from being loaded
  19. int cd_version = 0;
  20. int skip_intro=0;
  21. int start_flag2=0;
  22. //extern char * _envptr;
  23. #ifdef CLICKING_OPTIONAL
  24. int allow_clicking=0;
  25. #endif
  26. int language = START_UP_LANGUAGE;
  27. #ifdef CMD_OPTIONS
  28. int start_flagznotused=0;
  29. int ignore_saved_game_version=0;
  30. #endif
  31. main(int argc,char **argv)
  32. {
  33. #ifndef DONT_DISABLE_FAULTS
  34. _x32_fault_intercept(handler);
  35. #endif
  36. decode_argv_1(argc-1,argv+1);
  37. load_config();
  38. #ifdef DEBUG_C
  39. switch(language)
  40. { case ENGLISH_CODE:
  41. printf("English\n");
  42. break;
  43. case GERMAN_CODE:
  44. printf("German\n");
  45. break;
  46. case FRENCH_CODE:
  47. printf("French\n");
  48. break;
  49. case USA_CODE:
  50. printf("American\n");
  51. break;
  52. case SWEDISH_CODE:
  53. printf("Swedish\n");
  54. break;
  55. case ITALIAN_CODE:
  56. printf("Italian\n");
  57. break;
  58. case PORTUGUESE_CODE:
  59. printf("Portuguese\n");
  60. break;
  61. case SPANISH_CODE:
  62. printf("Spanish\n");
  63. break;
  64. default:
  65. printf("Invalid language\n");
  66. exit(1);
  67. }
  68. #endif
  69. decode_argv_2(argc-1,argv+1);
  70. check_disk_drive(); // This comes before initialise in case there is an error in initialise
  71. initialise();
  72. #ifdef CMD_OPTIONS
  73. if (start_flag == 0)
  74. #endif
  75. intro();
  76. mainloop();
  77. return(0);
  78. }
  79. void _cdecl handler(struct FAULT_STRUC *fault)
  80. {
  81. pc_restore();
  82. switch(language)
  83. { case ENGLISH_CODE:
  84. case USA_CODE:
  85. printf("Fatal System Error\n");
  86. break;
  87. case GERMAN_CODE:
  88. printf("Fatal System Error\n");
  89. break;
  90. case FRENCH_CODE:
  91. printf("Erreur systŠme fatale\n");
  92. break;
  93. case SWEDISH_CODE:
  94. printf("Erreur systŠme fatale\n");
  95. break;
  96. case ITALIAN_CODE:
  97. printf("Erreur systŠme fatale\n");
  98. break;
  99. case PORTUGUESE_CODE:
  100. printf("Erro Fatal do Sistema\n");
  101. break;
  102. case SPANISH_CODE:
  103. printf("Erreur systŠme fatale\n");
  104. break;
  105. }
  106. printf("cs:eip = %x:%x\n",(int)(*fault).cs,(*fault).eip);
  107. printf("Sorry!\n");
  108. // pzrintf("\nFault # %d has been intercepted.",(int)(*fault).fault_num);
  109. // pzrintf("\n\neax = %.8xH ebx = %.8xH ecx = %.8xH edx = %.8xH",
  110. // (*fault).eax,(*fault).ebx,(*fault).ecx,(*fault).edx);
  111. // pzrintf("\n\nedi = %.8xH esi = %.8xH ebp = %.8xH",
  112. // (*fault).edi,(*fault).esi,(*fault).ebp);
  113. // pzrintf("\n\nds = %.4xH es = %.4xH fs = %.4xH gs = %.4xH",
  114. // (int)(*fault).ds,(int)(*fault).es,(int)(*fault).fs,(int)(*fault).gs);
  115. // pzrintf("\n\nss:esp = %.4x:%.8xH",(int)(*fault).ss,(*fault).esp);
  116. // pzrintf("\n\ncs:eip = %.4x:%.8xH",(int)(*fault).cs,(*fault).eip);
  117. exit(1); /* Exit with error code. */
  118. }
  119. char *file_start="";
  120. char *file_name;
  121. void decode_argv_1(int args,char **arglist)
  122. {
  123. static int helpy=0;
  124. file_name = malloc(30);
  125. while (args)
  126. { char *line = *arglist;
  127. if (*line == 'z')
  128. helpy = 1;
  129. if (strncmp(line,"CFG=",4)==0)
  130. { file_start = malloc(strlen(line));
  131. strcpy(file_start,line+4);
  132. strcpy(file_start+strlen(file_start),"\\");
  133. if (helpy)
  134. { printf("\r\ncfg := %s\r\n\r\n",file_start);
  135. exit(1);
  136. }
  137. }
  138. args--;
  139. arglist++;
  140. }
  141. }
  142. void decode_argv_2(int args,char **arglist)
  143. {
  144. while (args)
  145. { char *line = *arglist;
  146. if (strncmp(line,"start",5)==0)
  147. { // We want to start on a new section
  148. start_flag2 = line[5] - '0';
  149. if ( (start_flag2 < 2) || (start_flag2 > 9) )
  150. exit(1);
  151. skip_intro = 1;
  152. }
  153. else if (strncmp(line,"CFG=",4)==0) {}
  154. while (*line)
  155. {
  156. #ifdef CMD_OPTIONS
  157. if ( (*line >= '1') && (*line <= '9'))
  158. start_flag = *line - '0';
  159. else
  160. #endif
  161. switch(*line)
  162. { case '?':
  163. printf("R\tUse Roland Board (Default SoundBlaster/Adlib)\n");
  164. printf("m\tForce music/fx off\n");
  165. #ifdef WITH_REPLAY
  166. printf("r\tUse replay file\n");
  167. #endif
  168. #ifdef CMD_OPTIONS
  169. printf("1-7\tStart sections 1-6 + linc terminal\n");
  170. printf("i\tMake restart file\n");
  171. printf("t\tDont play vocs\n");
  172. #endif
  173. #ifdef CLICKING_OPTIONAL
  174. printf("c\tAllow text clicking\n");
  175. #endif
  176. exit(1);
  177. #ifdef CLICKING_OPTIONAL
  178. case 'c':
  179. allow_clicking = 1;
  180. break;
  181. #endif
  182. case 'd':
  183. cd_version = 1;
  184. break;
  185. case 'l': // specify language
  186. { switch(line[1])
  187. { case 'e': // English
  188. language = SPANISH_CODE;
  189. break;
  190. case 'g': // German
  191. language = GERMAN_CODE;
  192. break;
  193. case 'f': // French
  194. language = FRENCH_CODE;
  195. break;
  196. case 'c': // American
  197. language = USA_CODE;
  198. break;
  199. case 's': // Swedish
  200. language = SWEDISH_CODE;
  201. break;
  202. case 'i': // Italian
  203. language = ITALIAN_CODE;
  204. break;
  205. case 'p': // Portuguese
  206. language = PORTUGUESE_CODE;
  207. break;
  208. default:
  209. printf("Invalid language\n");
  210. exit(1);
  211. }
  212. line++;
  213. }
  214. break;
  215. case 'm':
  216. force_sounds_off = 1;
  217. break;
  218. case 'i':
  219. skip_intro = 1;
  220. break;
  221. #ifdef CMD_OPTIONS
  222. case 'v':
  223. ignore_saved_game_version = 1;
  224. break;
  225. #endif
  226. #ifdef WITH_REPLAY
  227. case 'r':
  228. do_a_replay = 1;
  229. break;
  230. #endif
  231. case 'R':
  232. force_roland = 1;
  233. break;
  234. }
  235. line++;
  236. }
  237. args--;
  238. arglist++;
  239. }
  240. }
  241. //;--------------------------------------------------------------------------------------------------
  242. // File stuff for cd version
  243. // Redirect files to c:\steelsky\... if current drive is write protected
  244. void check_disk_drive()
  245. {
  246. int file;
  247. char *test_base = "sky.dms";
  248. // If we can open a file in the current directory then we are not on cd
  249. file = open(test_base,O_WRONLY|O_CREAT,0666);
  250. if (file<0) //Hard disk is write protected, most likely to be a cd
  251. { cd_version = 1;
  252. // Make sure there is a directory on c: called steelsky
  253. if ( *file_start == 0)
  254. { file_start = malloc(20);
  255. strcpy(file_start , "c:\\steelsky");
  256. }
  257. sprintf(file_name,"%s\\%s",file_start,test_base); // Check we can write to this
  258. file = open(file_name,O_WRONLY|O_CREAT,0666);
  259. if (file < 0) // Maybe directory needs creating
  260. { if (mkdir(file_start)<0)
  261. { sprintf(file_name,"Could not create directory %s\n",file_start);
  262. pc_restore();
  263. perror(file_name);
  264. exit(1);
  265. }
  266. // Check we can now open files here
  267. file = open(file_name,O_WRONLY|O_CREAT,0666);
  268. if (file<0)
  269. { sprintf(file_name,"Error writing to %s",file_start);
  270. pc_restore();
  271. perror(file_name);
  272. exit(1);
  273. }
  274. else
  275. { close(file);
  276. unlink(file_name);
  277. }
  278. }
  279. else
  280. { close(file);
  281. unlink(file_name);
  282. }
  283. strcpy(file_start + strlen(file_start) , "\\");
  284. //file_start = "c:\\steelsky\\";
  285. }
  286. else
  287. { close(file);
  288. unlink(test_base);
  289. file_start = "";
  290. }
  291. }
  292. int open_for_write(char *name)
  293. {
  294. int file;
  295. sprintf(file_name,"%s%s",file_start,name);
  296. file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC,0666);
  297. return(file);
  298. }
  299. int open_for_read(char *name)
  300. {
  301. int file;
  302. file = open(name,O_RDONLY,0);
  303. if (file > 0)
  304. return(file);
  305. sprintf(file_name,"%s%s",file_start,name);
  306. file = open(file_name,O_RDONLY,0);
  307. return(file);
  308. }
  309. //;-------------------------------------
  310. // old envoirenment stuff
  311. //char *eptr,*tptr = NULL;
  312. //int temp_drive;
  313. //
  314. //// First Look for a tmp or temp variable to see where the swap stuff goes
  315. //
  316. //eptr = _envptr;
  317. //
  318. //while ((*eptr) && (tptr==NULL))
  319. //{ if (strncmp(eptr, "TMP", 3) == 0)
  320. // tptr = eptr + 4;
  321. // else if (strncmp(eptr, "TEMP", 4) == 0)
  322. // tptr = eptr + 5;
  323. // else eptr += (strlen(eptr)+1);
  324. //}
  325. //
  326. //if (tptr) // We got a variable
  327. //{ printf("\nGot '%s'\n\n",tptr);
  328. // if (tptr[1] == ':')
  329. // { // I think we got ourselves a drive
  330. // if (*tptr > 'Z')
  331. // temp_drive = tptr[0] - 'a';
  332. // else temp_drive = tptr[0] - 'A';
  333. // }
  334. // else temp_drive = get_current_disk_drive();
  335. //}
  336. //else temp_drive = get_current_disk_drive();
  337. //
  338. //printf("\nCurrent drive %d\n",get_current_disk_drive());
  339. //printf("Temp drive %d\n",temp_drive);
  340. //printf("Space left %d\n",get_free_disk_space(temp_drive));
  341. //
  342. //printf("\n");
  343.