1 /* KJL 15:17:31 10/12/98 - user profile stuff */
2 #include "list_tem.hpp"
3 extern "C"
4 {
5 #include "3dc.h"
6 #include "inline.h"
7 #include "module.h"
8 #include "stratdef.h"
9 
10 #include "avp_userprofile.h"
11 #include "language.h"
12 #include "gammacontrol.h"
13 #include "psnd.h"
14 #include "cd_player.h"
15 
16 #define UseLocalAssert Yes
17 #include "ourasert.h"
18 
19  // Edmond
20 #include "pldnet.h"
21 #include <time.h>
22 
23 static int LoadUserProfiles(void);
24 
25 static void EmptyUserProfilesList(void);
26 static void InsertProfileIntoList(AVP_USER_PROFILE *profilePtr);
27 static int ProfileIsMoreRecent(AVP_USER_PROFILE *profilePtr, AVP_USER_PROFILE *profileToTestAgainstPtr);
28 static void SetDefaultProfileOptions(AVP_USER_PROFILE *profilePtr);
29 
30 extern int SmackerSoundVolume;
31 extern int EffectsSoundVolume;
32 extern int MoviesAreActive;
33 extern int IntroOutroMoviesAreActive;
34 extern char MP_PlayerName[];
35 extern int AutoWeaponChangeOn;
36 
37 
38 List<AVP_USER_PROFILE *> UserProfilesList;
39 static AVP_USER_PROFILE DefaultUserProfile =
40 {
41 	"",
42 };
43 static AVP_USER_PROFILE *CurrentUserProfilePtr;
44 
ExamineSavedUserProfiles(void)45 extern void ExamineSavedUserProfiles(void)
46 {
47 	// delete any existing profiles
48 	EmptyUserProfilesList();
49 
50 	// load any available user profiles
51 	LoadUserProfiles();
52 
53 	// make a fake entry to allow creating new user profiles
54 	AVP_USER_PROFILE *profilePtr = new AVP_USER_PROFILE;
55 	*profilePtr = DefaultUserProfile;
56 
57 	profilePtr->FileTime = time(NULL);
58 
59 	strncpy(profilePtr->Name,GetTextString(TEXTSTRING_USERPROFILE_NEW),MAX_SIZE_OF_USERS_NAME);
60 	profilePtr->Name[MAX_SIZE_OF_USERS_NAME]=0;
61 	SetDefaultProfileOptions(profilePtr);
62 
63 	InsertProfileIntoList(profilePtr);
64 }
65 
NumberOfUserProfiles(void)66 extern int NumberOfUserProfiles(void)
67 {
68 	int n = UserProfilesList.size();
69 
70 	LOCALASSERT(n>0);
71 
72 	return n-1;
73 }
74 
GetFirstUserProfile(void)75 extern AVP_USER_PROFILE *GetFirstUserProfile(void)
76 {
77 	CurrentUserProfilePtr=UserProfilesList.first_entry();
78 	return CurrentUserProfilePtr;
79 }
80 
GetNextUserProfile(void)81 extern AVP_USER_PROFILE *GetNextUserProfile(void)
82 {
83 	if (CurrentUserProfilePtr == UserProfilesList.last_entry())
84 	{
85 		CurrentUserProfilePtr = UserProfilesList.first_entry();
86 	}
87 	else
88 	{
89 		CurrentUserProfilePtr = UserProfilesList.next_entry(CurrentUserProfilePtr);
90 	}
91 	return CurrentUserProfilePtr;
92 }
93 
EmptyUserProfilesList(void)94 static void EmptyUserProfilesList(void)
95 {
96 	while (UserProfilesList.size())
97 	{
98 		delete UserProfilesList.first_entry();
99 		UserProfilesList.delete_first_entry();
100 	}
101 }
102 
SaveUserProfile(AVP_USER_PROFILE * profilePtr)103 extern int SaveUserProfile(AVP_USER_PROFILE *profilePtr)
104 {
105 	char *filename = new char [strlen(USER_PROFILES_PATH)+strlen(profilePtr->Name)+strlen(USER_PROFILES_SUFFIX)+1];
106 	strcpy(filename,USER_PROFILES_PATH);
107 	strcat(filename,profilePtr->Name);
108 	strcat(filename,USER_PROFILES_SUFFIX);
109 
110 	FILE* file=OpenGameFile(filename, FILEMODE_WRITEONLY, FILETYPE_CONFIG);
111 	delete [] filename;
112 	if (!file) return 0;
113 
114 	SaveSettingsToUserProfile(profilePtr);
115 
116 	fwrite(profilePtr,sizeof(AVP_USER_PROFILE),1,file);
117 	fclose(file);
118 
119 	return 1;
120 }
121 
DeleteUserProfile(int number)122 extern void DeleteUserProfile(int number)
123 {
124 	AVP_USER_PROFILE *profilePtr = GetFirstUserProfile();
125 
126 	for (int i=0; i<number; i++) profilePtr = GetNextUserProfile();
127 
128 	char *filename = new char [strlen(USER_PROFILES_PATH)+strlen(profilePtr->Name)+strlen(USER_PROFILES_SUFFIX)+1];
129 	strcpy(filename,USER_PROFILES_PATH);
130 	strcat(filename,profilePtr->Name);
131 	strcat(filename,USER_PROFILES_SUFFIX);
132 
133 	DeleteGameFile(filename);
134 
135 	delete [] filename;
136 	{
137 		int i;
138 		filename = new char [100];
139 
140 		for (i=0; i<NUMBER_OF_SAVE_SLOTS; i++)
141 		{
142 			sprintf(filename,"%s%s_%d.sav",USER_PROFILES_PATH,profilePtr->Name,i+1);
143 			DeleteGameFile(filename);
144 		}
145 		delete [] filename;
146 	}
147 }
148 
InsertProfileIntoList(AVP_USER_PROFILE * profilePtr)149 static void InsertProfileIntoList(AVP_USER_PROFILE *profilePtr)
150 {
151 	if (UserProfilesList.size())
152 	{
153 		AVP_USER_PROFILE *profileInListPtr = GetFirstUserProfile();
154 
155 		for (int i=0; i<UserProfilesList.size(); i++, profileInListPtr = GetNextUserProfile())
156 		{
157 			if (ProfileIsMoreRecent(profilePtr,profileInListPtr))
158 			{
159 				UserProfilesList.add_entry_before(profilePtr,profileInListPtr);
160 				return;
161 			}
162 		}
163 	}
164 	UserProfilesList.add_entry(profilePtr);
165 }
166 
ProfileIsMoreRecent(AVP_USER_PROFILE * profilePtr,AVP_USER_PROFILE * profileToTestAgainstPtr)167 static int ProfileIsMoreRecent(AVP_USER_PROFILE *profilePtr, AVP_USER_PROFILE *profileToTestAgainstPtr)
168 {
169 	if (difftime(profilePtr->FileTime, profileToTestAgainstPtr->FileTime) > 0.0) {
170 		return 1; /* first file newer than file to test */
171 	} else {
172 		return 0;
173 	}
174 }
175 
LoadUserProfiles(void)176 static int LoadUserProfiles(void)
177 {
178 	void *gd;
179 	GameDirectoryFile *gdf;
180 
181 	gd = OpenGameDirectory(USER_PROFILES_PATH, USER_PROFILES_WILDCARD_NAME, FILETYPE_CONFIG);
182 	if (gd == NULL) {
183 		CreateGameDirectory(USER_PROFILES_PATH); /* maybe it didn't exist.. */
184 		return 0;
185 	}
186 
187 	int nPathLen = strlen(USER_PROFILES_PATH);
188 
189 	while ((gdf = ScanGameDirectory(gd)) != NULL) {
190 		if ((gdf->attr & FILEATTR_DIRECTORY) != 0)
191 			continue;
192 		if ((gdf->attr & FILEATTR_READABLE) == 0)
193 			continue;
194 
195 		char * pszFullPath = new char [nPathLen+strlen(gdf->filename)+1];
196 		strcpy(pszFullPath, USER_PROFILES_PATH);
197 		strcat(pszFullPath, gdf->filename);
198 
199 		FILE *rif_file;
200 		rif_file = OpenGameFile(pszFullPath, FILEMODE_READONLY, FILETYPE_CONFIG);
201 		if(rif_file==NULL)
202 		{
203 			delete[] pszFullPath;
204 			continue;
205 		}
206 
207 		AVP_USER_PROFILE *profilePtr = new AVP_USER_PROFILE;
208 
209 		if (fread(profilePtr, 1, sizeof(AVP_USER_PROFILE), rif_file) != sizeof(AVP_USER_PROFILE))
210 		{
211 	       		fclose(rif_file);
212 			delete[] pszFullPath;
213 			delete profilePtr;
214 			continue;
215 		}
216 
217 		profilePtr->FileTime = gdf->timestamp;
218 
219 		InsertProfileIntoList(profilePtr);
220 		fclose(rif_file);
221 		delete[] pszFullPath;
222 	}
223 
224 	CloseGameDirectory(gd);
225 
226 	return 1;
227 }
228 
SetDefaultProfileOptions(AVP_USER_PROFILE * profilePtr)229 static void SetDefaultProfileOptions(AVP_USER_PROFILE *profilePtr)
230 {
231 	// set Gamma
232 	RequestedGammaSetting = 128;
233 
234 	// controls
235 	MarineInputPrimaryConfig = DefaultMarineInputPrimaryConfig;
236 	MarineInputSecondaryConfig = DefaultMarineInputSecondaryConfig;
237 	PredatorInputPrimaryConfig = DefaultPredatorInputPrimaryConfig;
238 	PredatorInputSecondaryConfig = DefaultPredatorInputSecondaryConfig;
239 	AlienInputPrimaryConfig = DefaultAlienInputPrimaryConfig;
240 	AlienInputSecondaryConfig = DefaultAlienInputSecondaryConfig;
241 	ControlMethods = DefaultControlMethods;
242 	JoystickControlMethods = DefaultJoystickControlMethods;
243 
244 	SmackerSoundVolume = ONE_FIXED/512;
245 	EffectsSoundVolume = VOLUME_DEFAULT;
246 	CDPlayerVolume = CDDA_VOLUME_DEFAULT;
247 	MoviesAreActive = 1;
248 	IntroOutroMoviesAreActive = 1;
249 	AutoWeaponChangeOn = TRUE;
250 
251 	strcpy(MP_PlayerName, "DeadMeat");
252 
253 	SetToDefaultDetailLevels();
254 
255 	{
256 		int a,b;
257 
258 		for (a=0; a<I_MaxDifficulties; a++) {
259 			for (b=0; b<AVP_ENVIRONMENT_END_OF_LIST; b++) {
260 				profilePtr->PersonalBests[a][b]=DefaultLevelGameStats;
261 			}
262 		}
263 	}
264 
265 	SaveSettingsToUserProfile(profilePtr);
266 }
267 
GetSettingsFromUserProfile(void)268 extern void GetSettingsFromUserProfile(void)
269 {
270 	RequestedGammaSetting = UserProfilePtr->GammaSetting;
271 
272 	MarineInputPrimaryConfig = 		UserProfilePtr->MarineInputPrimaryConfig;
273 	MarineInputSecondaryConfig = 	UserProfilePtr->MarineInputSecondaryConfig;
274 	PredatorInputPrimaryConfig = 	UserProfilePtr->PredatorInputPrimaryConfig;
275 	PredatorInputSecondaryConfig = 	UserProfilePtr->PredatorInputSecondaryConfig;
276 	AlienInputPrimaryConfig = 		UserProfilePtr->AlienInputPrimaryConfig;
277 	AlienInputSecondaryConfig = 	UserProfilePtr->AlienInputSecondaryConfig;
278 	ControlMethods = 				UserProfilePtr->ControlMethods;
279 	JoystickControlMethods = 		UserProfilePtr->JoystickControlMethods;
280 	MenuDetailLevelOptions = 		UserProfilePtr->DetailLevelSettings;
281 	SmackerSoundVolume =			UserProfilePtr->SmackerSoundVolume;
282 	EffectsSoundVolume =			UserProfilePtr->EffectsSoundVolume;
283 	CDPlayerVolume = 				UserProfilePtr->CDPlayerVolume;
284 	MoviesAreActive =				UserProfilePtr->MoviesAreActive;
285 	IntroOutroMoviesAreActive =		UserProfilePtr->IntroOutroMoviesAreActive;
286 	AutoWeaponChangeOn = 			!UserProfilePtr->AutoWeaponChangeDisabled;
287    	strncpy(MP_PlayerName,UserProfilePtr->MultiplayerCallsign,15);
288 
289 	SetDetailLevelsFromMenu();
290 }
291 
SaveSettingsToUserProfile(AVP_USER_PROFILE * profilePtr)292 extern void SaveSettingsToUserProfile(AVP_USER_PROFILE *profilePtr)
293 {
294 	profilePtr->GammaSetting = RequestedGammaSetting;
295 
296 	profilePtr->MarineInputPrimaryConfig =		MarineInputPrimaryConfig;
297 	profilePtr->MarineInputSecondaryConfig =	MarineInputSecondaryConfig;
298 	profilePtr->PredatorInputPrimaryConfig =	PredatorInputPrimaryConfig;
299 	profilePtr->PredatorInputSecondaryConfig =	PredatorInputSecondaryConfig;
300 	profilePtr->AlienInputPrimaryConfig =		AlienInputPrimaryConfig;
301 	profilePtr->AlienInputSecondaryConfig =		AlienInputSecondaryConfig;
302 	profilePtr->ControlMethods =				ControlMethods;
303 	profilePtr->JoystickControlMethods =		JoystickControlMethods;
304 	profilePtr->DetailLevelSettings =			MenuDetailLevelOptions;
305 	profilePtr->SmackerSoundVolume =			SmackerSoundVolume;
306 	profilePtr->EffectsSoundVolume =			EffectsSoundVolume;
307 	profilePtr->CDPlayerVolume = 				CDPlayerVolume;
308 	profilePtr->MoviesAreActive =				MoviesAreActive;
309 	profilePtr->IntroOutroMoviesAreActive =		IntroOutroMoviesAreActive;
310 	profilePtr->AutoWeaponChangeDisabled=   !AutoWeaponChangeOn;
311    	strncpy(profilePtr->MultiplayerCallsign,MP_PlayerName,15);
312 }
313 
FixCheatModesInUserProfile(AVP_USER_PROFILE * profilePtr)314 extern void FixCheatModesInUserProfile(AVP_USER_PROFILE *profilePtr)
315 {
316 	int a;
317 
318 	for (a=0; a<MAX_NUMBER_OF_CHEATMODES; a++) {
319 		if (profilePtr->CheatMode[a]==2) {
320 			profilePtr->CheatMode[a]=1;
321 		}
322 	}
323 
324 }
325 
326 }; // extern "C"
327