convert line ends
[canaan.git] / prj / cam / src / shock / shkchkcd.cpp
blob6c04836c0a0d11efdb146858ebf89d6b796d5f34
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/src/shock/shkchkcd.cpp,v 1.4 2000/02/19 12:36:41 toml Exp $
8 #include <windows.h>
9 #include <config.h>
10 #include <string.h>
11 #include <shkchkcd.h>
12 #include <mprintf.h>
13 #include <memall.h>
14 #include <dbmem.h> // must be last header!
17 // Shock CD and protection stuff.
19 // 1) We use DiscSafe copy protection, which wraps our executable in
20 // encrypted form. CD authentication occurs before our WinMain()
21 // even begins.
23 // 2) So, why this code? It's mostly here to locate the CD for runtime
24 // access. The cd_path config variable is only a default value to
25 // check. The user might have 2 CD drives, or a removable-medium
26 // drive which causes the CD to show up at a different letter.
29 #ifndef SHIP
30 #define CHKCD_SPEW(a) \
31 if (config_is_defined("chkcd_spew")) {mprintf a;}
32 #define CHKCD_BOOLSPEW(a,b,c)\
33 if (config_is_defined("chkcd_spew")) {if((a)){mprintf b;} else {mprintf c;}}
34 #else
35 #define CHKCD_SPEW(a)
36 #define CHKCD_BOOLSPEW(a,b,c)
37 #endif
40 static char *path_configs[]=
41 { "cd_path", "resname_base", "load_path", "script_module_path", "movie_path" };
43 // gonna replace " X:\" and "+X:\"
44 void _FixupCDConfigVars( char *drivePath )
46 char try_paths[MAX_PATH], old_path[MAX_PATH];
47 int i;
49 if ( !config_get_raw( path_configs[0], old_path, sizeof( old_path ) ) )
50 strcpy( old_path, "D:\\" );
51 strlwr( old_path );
52 strlwr( drivePath );
53 for( i = 0; i < sizeof( path_configs ) / sizeof( path_configs[0] ); i++ )
55 if( config_get_raw( path_configs[i], try_paths, sizeof( try_paths ) ) )
57 char *p;
58 strlwr( try_paths );
59 p=try_paths;
60 while( ( p = strstr( p, old_path ) ) != NULL)
62 CHKCD_SPEW(( "CHKCD : Fixing up config var %s\n", path_configs[i] ));
63 CHKCD_SPEW(( "CHKCD : Was: \"%s\"\n", try_paths ));
64 memcpy( p, drivePath, strlen( drivePath ) );
65 CHKCD_SPEW(( "CHKCD : Now: \"%s\"\n", try_paths ));
66 p++; // lets not fix ourselves up again
68 config_set_string( path_configs[i], try_paths );
69 config_set_priority( path_configs[i], PRIORITY_TRANSIENT );
74 static char* _GetNoCDString( char *fill_in, char *type, int fill_in_len )
76 char cfg_var[128];
77 char lang[32];
79 // Get language from config.
80 if ( !config_get_raw( "language", lang, sizeof( lang ) ) )
81 strcpy( lang, "english" ); // yes, gringo bastards, yes
83 // Use config variable of form no_cd_XXX_YYY, e.g. no_cd_title_english.
84 sprintf( cfg_var, "no_cd_%s_%s", type, lang);
85 if ( !config_get_raw( cfg_var, fill_in, fill_in_len) )
86 strcpy( fill_in, "NO CD" );
88 return fill_in;
91 static BOOL _ShowCDMessage( BOOL copy_prot )
93 char title[128];
94 char message[256];
96 _GetNoCDString( title, "title", sizeof( title ) );
97 _GetNoCDString( message, "msg", sizeof( message) );
98 if ( copy_prot )
99 strcat( message," ." );
100 if ( MessageBox( NULL, message, title, MB_OKCANCEL | MB_ICONEXCLAMATION ) == IDCANCEL )
101 return FALSE;
102 return TRUE;
105 static BOOL _CheckIsACD( char* drivePath )
107 return GetDriveType( drivePath ) == DRIVE_CDROM;
110 static BOOL _CheckFile( char* drivePath )
112 BOOL wasFound;
113 char path[MAX_PATH];
114 FILE *fp=NULL;
116 // Set up search path.
117 path[0] = '\0';
118 strcat( path, drivePath );
119 strcat( path, "shock\\snd.crf" );
121 // Attempt to open known file.
122 fp = fopen( path, "rb" );
123 if( fp )
125 fclose( fp );
126 wasFound = TRUE;
128 else
130 wasFound = FALSE;
133 return wasFound;
136 static BOOL _CheckDrive( char* drivePath )
138 return( _CheckIsACD( drivePath ) && _CheckFile( drivePath ) );
142 static BOOL _CheckDrives( BOOL onlyCheckPath )
144 char path[MAX_PATH];
145 BOOL wasFound = FALSE;
147 // Check config path setting.
148 if( config_get_raw( "cd_path", path, sizeof( path ) ) )
150 wasFound = _CheckDrive( path );
151 CHKCD_SPEW(("CHKCD : Checking CD at cd_path (%s)... ", path));
152 CHKCD_BOOLSPEW( wasFound, ( "Found.\n" ), ( "Not found.\n") );
155 // We don't check other drives if onlyCheckPath is flagged or if
156 // we already found it.
157 if( !onlyCheckPath && !wasFound )
159 CHKCD_SPEW(( "CHKCD : Checking other drives... " ));
160 strcpy( path, "A:\\" );
161 while( !wasFound && path[0] <= 'Z' )
163 CHKCD_SPEW(( "%c", path[0] ));
164 wasFound = _CheckDrive( path );
165 if ( !wasFound )
166 path[0]++;
169 CHKCD_BOOLSPEW( wasFound, ( "\nCHKCD : Found CD at %s\n", path ), ("\nCHKCD : Not found.\n") );
170 if( wasFound )
172 _FixupCDConfigVars( path );
176 return wasFound;
179 // so defdep sees it
180 #ifdef THIEF
181 #endif
182 #ifdef SHOCK
183 #endif
185 // note that we are the default provider of this function
186 #if !defined(THIEF)
187 BOOL CheckForCD(void)
189 BOOL wasFound = FALSE;
190 BOOL tryAgain = TRUE;
192 #ifndef COPY_PROTECTION
193 // Don't complain about copy protection.
194 if( !config_is_defined( "test_copy_protect" ) )
196 CHKCD_SPEW(( "CHKCD : Skipping copy protection.\n"));
197 // We still do _CheckDrives() so that any FixupConfigVars can happen.
198 wasFound = _CheckDrives( config_is_defined( "only_check_path" ) );
199 // Force true result.
200 wasFound = TRUE;
202 else
203 #endif
205 while ( !wasFound && tryAgain )
207 // For testing purposes, we can check just one drive.
208 wasFound = _CheckDrives( config_is_defined( "only_check_path" ) );
210 if ( !wasFound )
212 tryAgain = _ShowCDMessage( TRUE );
217 return wasFound;
219 #endif