GETCD.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************
  19. ***** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *****
  20. ****************************************************************************
  21. * Project Name:: Command & Conquer *
  22. * $Archive:: /Renegade Setup/Autorun/GETCD.CPP $Author:: Steve_t *
  23. * $Modtime:: 1/28/02 10:54a $Revision:: 20 *
  24. *--------------------------------------------------------------------------*
  25. * Functions: *
  26. * GetCDClass::GetCDClass -- default constructor *
  27. * GetCDClass::~GetCDClass -- destructor *
  28. * GetCDClass::GetCDDrive -- returns the logical CD drive *
  29. * CD_Volume_Verification -- Check label of the CDRom. *
  30. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  31. #include <windows.h>
  32. #include <string.h>
  33. //#include "always.h"
  34. #include "getcd.h"
  35. //#include "timer.h"
  36. #include "wnd_file.h"
  37. //#include "missiondisk.h"
  38. #include "winfix.h"
  39. #ifndef ROR_NOT_READY
  40. #define ROR_NOT_READY 21
  41. #endif
  42. /**********************************************************************
  43. ** This macro serves as a general way to determine the number of elements
  44. ** within an array.
  45. */
  46. #define ARRAY_SIZE(x) int(sizeof(x)/sizeof(x[0]))
  47. #define size_of(typ,id) sizeof(((typ*)0)->id)
  48. //-----------------------------------------------------------------------------
  49. // Global Variables
  50. //-----------------------------------------------------------------------------
  51. #if ( MISSION_DISK )
  52. static char * _CD_Volume_Label[] = {
  53. "Renegade Mission", // Yuri's Revenge (mission disk)
  54. };
  55. #else
  56. static char * _CD_Volume_Label[] = {
  57. "Renegade Game", // Red Alert 2
  58. "Renegade Data", // Red Alert 2
  59. };
  60. #endif
  61. static int _Num_Volumes = ARRAY_SIZE( _CD_Volume_Label );
  62. GetCDClass CDList;
  63. /****************************************************************************
  64. * GetCDClass -- default constructor *
  65. * *
  66. * INPUT: *
  67. * none *
  68. * OUTPUT: *
  69. * none *
  70. * WARNINGS: *
  71. * *
  72. * HISTORY: *
  73. * 05/26/1994 SW : Created. *
  74. * 12/04/1995 ST : fixed for Win95 *
  75. *==========================================================================*/
  76. GetCDClass::GetCDClass( void )
  77. {
  78. char path[]={ "a:\\" };
  79. CDCount = 0;
  80. CDIndex = 0;
  81. Msg( __LINE__, __FILE__, "GetCDClass constructor\n" );
  82. /*--------------------------------------------------------------------------
  83. ** Set all CD drive placeholders to empty
  84. */
  85. for( int j = 0; j < MAX_CD_DRIVES; j++ ) {
  86. CDDrives[j] = NO_CD_DRIVE;
  87. }
  88. for( char i = 'c'; i <= 'z'; i++ ) {
  89. path[0] = i;
  90. if ( GetDriveType( path ) == DRIVE_CDROM ) {
  91. CDDrives[ CDCount++ ] = (int)( i-'a' );
  92. Msg( __LINE__, __FILE__, "CD drive found - %c:\n", i +'A'-'a' );
  93. }
  94. }
  95. /*--------------------------------------------------------------------------
  96. ** Catch the case when there are NO CD-ROM drives available
  97. */
  98. if ( CDCount == 0 ) {
  99. for ( char i = 'a'; i <= 'b'; i++ ) {
  100. path[0] = i;
  101. if ( GetDriveType( path ) == DRIVE_CDROM ) {
  102. CDDrives[ CDCount++ ] = (int)( i-'a' );
  103. }
  104. }
  105. }
  106. if ( CDCount == 0 ) {
  107. Msg( __LINE__, __FILE__, "No CD drives found\n");
  108. }
  109. }
  110. /********************************************************************************
  111. * GetCDClass -- destructor *
  112. * *
  113. * INPUT: none *
  114. * *
  115. * OUTPUT: none *
  116. * *
  117. * WARNINGS: *
  118. * *
  119. * HISTORY: *
  120. * 05/26/1994 SW: Created. *
  121. * 12/4/95 ST: fixed for Win95 *
  122. *==============================================================================*/
  123. GetCDClass::~GetCDClass(void)
  124. {
  125. // if(cdDrive_addrp.seg)
  126. // DPMI_real_free(cdDrive_addrp); // free up those conventional buffers
  127. }
  128. /********************************************************************************
  129. * GetCDClass -- Get_CD_Drive_For_This_Volume *
  130. * *
  131. * INPUT: char *volume_name *
  132. * *
  133. * OUTPUT: int *
  134. * *
  135. * WARNINGS: *
  136. * *
  137. * HISTORY: *
  138. * 10/18/2000 MML: Created. *
  139. *==============================================================================*/
  140. int GetCDClass::Get_CD_Drive_For_This_Volume ( char *volume_label )
  141. {
  142. char volume_name[128] = "";
  143. int count = 0;
  144. char buffer[128];
  145. unsigned misc_dword;
  146. unsigned filename_length;
  147. int cd_drive;
  148. CDIndex = 0;
  149. while( CDIndex < CDCount ) {
  150. //---------------------------------------------------------------------
  151. // NOTE: A=0, B=1, C=2, D=3, etc...
  152. //---------------------------------------------------------------------
  153. cd_drive = CDDrives[ CDIndex++ ];
  154. wsprintf( buffer, "%c:\\", 'A' + cd_drive );
  155. if ( GetVolumeInformation(
  156. (char const *)buffer,
  157. &volume_name[0],
  158. (unsigned long)sizeof(volume_name)-1,
  159. (unsigned long *)NULL,
  160. (unsigned long *)&filename_length,
  161. (unsigned long *)&misc_dword,
  162. (char *)NULL,
  163. (unsigned long)0 )) {
  164. //---------------------------------------------------------------------
  165. // Windows '95 appears to have a volume name limit of 11 characters.
  166. // I cannot find a Win32 call that will return the maximum volume name
  167. // length so the value '11' is hard-coded here and the assumption made
  168. // that all OS's have this length or better.
  169. //---------------------------------------------------------------------
  170. if( WinVersion.Is_Win95()) {
  171. volume_name[11] = '\0';
  172. }
  173. if ( !_stricmp( volume_label, volume_name )) {
  174. return( cd_drive );
  175. }
  176. } else {
  177. #if _DEBUG
  178. LPVOID lpMsgBuf;
  179. FormatMessage(
  180. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  181. FORMAT_MESSAGE_FROM_SYSTEM |
  182. FORMAT_MESSAGE_IGNORE_INSERTS,
  183. NULL,
  184. GetLastError(),
  185. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  186. (LPTSTR) &lpMsgBuf,
  187. 0,
  188. NULL
  189. );
  190. Msg( __LINE__, __FILE__, (LPTSTR)lpMsgBuf );
  191. LocalFree( lpMsgBuf );
  192. #endif
  193. }
  194. }
  195. return( Get_First_CD_Drive());
  196. }
  197. /********************************************************************************
  198. * GetCDClass -- Get_Volume_Label *
  199. * *
  200. * INPUT: int index *
  201. * *
  202. * OUTPUT: char * *
  203. * *
  204. * WARNINGS: *
  205. * *
  206. * HISTORY: *
  207. * 10/31/2000 MML: Created. * Happy Halloween! * *
  208. *==============================================================================*/
  209. char * GetCDClass::Get_Volume_Label ( int index )
  210. {
  211. if( index >= 0 && index < _Num_Volumes ) {
  212. return( _CD_Volume_Label[ index ]);
  213. }
  214. return( _CD_Volume_Label[0]);
  215. }
  216. /********************************************************************************
  217. * GetCDClass -- Get_Volume_For_This_CD_Drive *
  218. * *
  219. * INPUT: char *volume_name *
  220. * char *path *
  221. * *
  222. * OUTPUT: int *
  223. * *
  224. * WARNINGS: *
  225. * *
  226. * HISTORY: *
  227. * 10/31/2000 MML: Created. * Happy Halloween! * *
  228. *==============================================================================*/
  229. char *GetCDClass::Get_Volume_For_This_CD_Drive ( char *path, char *volume_name )
  230. {
  231. char buffer[128];
  232. unsigned misc_dword;
  233. unsigned filename_length;
  234. static char volume_label[ MAX_PATH ] = ""; // [OYO] add static
  235. if ( path == NULL || volume_name == NULL ) {
  236. return( NULL );
  237. }
  238. memset( volume_name, '\0', sizeof( volume_name ));
  239. wsprintf( buffer, "%c:\\", path[0] );
  240. if ( GetVolumeInformation(
  241. (char const *)buffer,
  242. &volume_label[0],
  243. (unsigned long)sizeof(volume_label)-1,
  244. (unsigned long *)NULL,
  245. (unsigned long *)&filename_length,
  246. (unsigned long *)&misc_dword,
  247. (char *)NULL,
  248. (unsigned long)0 )) {
  249. strcpy( volume_name, volume_label );
  250. } else {
  251. LPVOID lpMsgBuf;
  252. FormatMessage(
  253. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  254. FORMAT_MESSAGE_FROM_SYSTEM |
  255. FORMAT_MESSAGE_IGNORE_INSERTS,
  256. NULL,
  257. GetLastError(),
  258. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  259. (LPTSTR) &lpMsgBuf,
  260. 0,
  261. NULL
  262. );
  263. Msg( __LINE__, __FILE__, (LPTSTR)lpMsgBuf );
  264. LocalFree( lpMsgBuf );
  265. strcpy( volume_name, _CD_Volume_Label[0] );
  266. }
  267. //-------------------------------------------------------------------------
  268. // Windows '95 appears to have a volume name limit of 11 characters.
  269. // I cannot find a Win32 call that will return the maximum volume name
  270. // length so the value '11' is hard-coded here and the assumption made
  271. // that all OS's have this length or better.
  272. //-------------------------------------------------------------------------
  273. bool winversion = WinVersion.Is_Win95();
  274. if( winversion ) {
  275. volume_name[11] = '\0';
  276. }
  277. return( volume_name );
  278. }
  279. /********************************************************************************
  280. * CD_VOLUME_VERIFICATION -- Check volume label of the CD in the given drive. *
  281. * *
  282. * INPUT: int drive_number *
  283. * char *lable *
  284. * *
  285. * OUTPUT: TRUE/FALSE *
  286. * *
  287. * WARNINGS: None *
  288. * *
  289. * HISTORY: *
  290. * 01/11/99 4:20PM MML : Created *
  291. *==============================================================================*/
  292. bool CD_Volume_Verification ( int cd_drive, char *volume_label, char *volume_to_find )
  293. {
  294. char volume_name[128] = "";
  295. int count = 0;
  296. char buffer[128];
  297. unsigned misc_dword;
  298. unsigned filename_length;
  299. /***************************************************************************
  300. ** Get the volume label. If we get a 'not ready' error then retry for the
  301. ** timeout period.
  302. */
  303. for (;;) {
  304. //---------------------------------------------------------------------
  305. // NOTE: A=0, B=1, C=2, D=3, etc...
  306. //---------------------------------------------------------------------
  307. wsprintf( buffer, "%c:\\", 'A' + cd_drive );
  308. if ( GetVolumeInformation(
  309. (char const *)buffer,
  310. &volume_name[0],
  311. (unsigned long)sizeof(volume_name)-1,
  312. (unsigned long *)NULL,
  313. (unsigned long *)&filename_length,
  314. (unsigned long *)&misc_dword,
  315. (char *)NULL,
  316. (unsigned long)0 )) {
  317. /******************************************************************
  318. ** Match the volume label to the list of known volume labels.
  319. */
  320. //---------------------------------------------------------------------
  321. // Windows '95 appears to have a volume name limit of 11 characters.
  322. // I cannot find a Win32 call that will return the maximum volume name
  323. // length so the value '11' is hard-coded here and the assumption made
  324. // that all OS's have this length or better.
  325. //---------------------------------------------------------------------
  326. if( WinVersion.Is_Win95()) {
  327. volume_name[11] = '\0';
  328. }
  329. if ( volume_label != NULL ) {
  330. strncpy( volume_label, volume_name, 128 );
  331. }
  332. if ( !_stricmp( volume_to_find, volume_name )) {
  333. return TRUE;
  334. }
  335. if ( !count ) {
  336. count++;
  337. } else {
  338. return FALSE;
  339. }
  340. } else {
  341. /*********************************************************************
  342. ** Failed to get the volume label on a known CD drive. If this is a
  343. ** CD changer it may require time to swap the disks so don't return
  344. ** immediately if the error is ROR_NOT_READY
  345. */
  346. return FALSE;
  347. }
  348. }
  349. }
  350. /* ==================================================================== */