diff src/win32/WinMiscUtil.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/WinMiscUtil.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,497 @@
     1.4 +#include "stdafx.h"
     1.5 +#include "WinMiscUtil.h"
     1.6 +#include "WinResUtil.h"
     1.7 +#include "resource.h"
     1.8 +#include "../NLS.h"
     1.9 +#include "VBA.h"
    1.10 +#include "Reg.h"
    1.11 +#include "../common/movie.h"
    1.12 +#include <direct.h>
    1.13 +
    1.14 +#include "GSACodeSelect.h"
    1.15 +#include "../gba/GBACheats.h"
    1.16 +#include "../gb/gbCheats.h"
    1.17 +
    1.18 +// #undef WinDef macro garbage
    1.19 +#ifdef max
    1.20 +#undef max
    1.21 +#endif
    1.22 +
    1.23 +#ifdef min
    1.24 +#undef min
    1.25 +#endif
    1.26 +
    1.27 +using std::max;
    1.28 +using std::min;
    1.29 +
    1.30 +extern int emulating;
    1.31 +
    1.32 +extern const char IDS_ROM_DIR[]		= "romDir";
    1.33 +extern const char IDS_GBXROM_DIR[]	= "gbromDir";
    1.34 +extern const char IDS_BATTERY_DIR[]	= "batteryDir";
    1.35 +extern const char IDS_SAVE_DIR[]	= "saveDir";
    1.36 +extern const char IDS_MOVIE_DIR[]	= "moviesDir";
    1.37 +extern const char IDS_CHEAT_DIR[]	= "cheatsDir";
    1.38 +extern const char IDS_LUA_DIR[]		= "luaDir";
    1.39 +extern const char IDS_IPS_DIR[]		= "ipsDir";
    1.40 +extern const char IDS_AVI_DIR[]		= "aviRecordDir";
    1.41 +extern const char IDS_WAV_DIR[]		= "soundRecordDir";
    1.42 +extern const char IDS_CAPTURE_DIR[] = "captureDir";
    1.43 +extern const char IDS_WATCH_DIR[]	= "watchDir";
    1.44 +
    1.45 +extern const char IDS_ROM_DEFAULT_DIR[]		= "\\roms";
    1.46 +extern const char IDS_GBXROM_DEFAULT_DIR[]	= "\\gbroms";
    1.47 +extern const char IDS_BATTERY_DEFAULT_DIR[]	= "\\battery";
    1.48 +extern const char IDS_SAVE_DEFAULT_DIR[]	= "\\save";
    1.49 +extern const char IDS_MOVIE_DEFAULT_DIR[]	= "\\movies";
    1.50 +extern const char IDS_CHEAT_DEFAULT_DIR[]	= "\\cheats";
    1.51 +extern const char IDS_LUA_DEFAULT_DIR[]		= "\\lua";
    1.52 +extern const char IDS_IPS_DEFAULT_DIR[]		= "\\ips";
    1.53 +extern const char IDS_AVI_DEFAULT_DIR[]		= "\\avi";
    1.54 +extern const char IDS_WAV_DEFAULT_DIR[]		= "\\wav";
    1.55 +extern const char IDS_CAPTURE_DEFAULT_DIR[] = "\\screen";
    1.56 +extern const char IDS_WATCH_DEFAULT_DIR[]	= "\\watches";
    1.57 +
    1.58 +extern const char *IDS_tbl[] = {
    1.59 +	IDS_ROM_DIR,   IDS_GBXROM_DIR, IDS_BATTERY_DIR, IDS_SAVE_DIR, 
    1.60 +	IDS_MOVIE_DIR, IDS_CHEAT_DIR,  IDS_LUA_DIR,     IDS_IPS_DIR, 
    1.61 +	IDS_AVI_DIR,   IDS_WAV_DIR,    IDS_CAPTURE_DIR, IDS_WATCH_DIR
    1.62 +};
    1.63 +
    1.64 +extern const char *IDS_def_tbl[] = {
    1.65 +	IDS_ROM_DEFAULT_DIR,   IDS_GBXROM_DEFAULT_DIR, IDS_BATTERY_DEFAULT_DIR, IDS_SAVE_DEFAULT_DIR, 
    1.66 +	IDS_MOVIE_DEFAULT_DIR, IDS_CHEAT_DEFAULT_DIR,  IDS_LUA_DEFAULT_DIR,     IDS_IPS_DEFAULT_DIR, 
    1.67 +	IDS_AVI_DEFAULT_DIR,   IDS_WAV_DEFAULT_DIR,    IDS_CAPTURE_DEFAULT_DIR, IDS_WATCH_DEFAULT_DIR
    1.68 +};
    1.69 +
    1.70 +// these could be made VBA members, but  the VBA class is already oversized too much
    1.71 +//
    1.72 +
    1.73 +bool winFileExists(const CString &filename)
    1.74 +{
    1.75 +	FILE *f = fopen(filename, "rb");
    1.76 +	if (f)
    1.77 +	{
    1.78 +		fclose(f);
    1.79 +		return true;
    1.80 +	}
    1.81 +	return false;
    1.82 +}
    1.83 +
    1.84 +bool winIsDriveRoot(const CString &file)
    1.85 +{
    1.86 +	if (file.GetLength() == 3)
    1.87 +	{
    1.88 +		if (file[1] == ':' && file[2] == '\\')
    1.89 +			return true;
    1.90 +	}
    1.91 +	return false;
    1.92 +}
    1.93 +
    1.94 +CString winGetOriginalFilename(const CString &file)
    1.95 +{
    1.96 +	int index = file.Find('|');
    1.97 +
    1.98 +	if (index != -1)
    1.99 +		return file.Left(index);
   1.100 +	else
   1.101 +		return file;
   1.102 +}
   1.103 +
   1.104 +CString winGetDirFromFilename(const CString &file)
   1.105 +{
   1.106 +	CString temp  = winGetOriginalFilename(file);
   1.107 +	int		index = max(temp.ReverseFind('/'), temp.ReverseFind('\\'));
   1.108 +	if (index != -1)
   1.109 +	{
   1.110 +		temp = temp.Left(index);
   1.111 +		if (temp.GetLength() == 2 && temp[1] == ':')
   1.112 +			temp += "\\";
   1.113 +	}
   1.114 +
   1.115 +	return temp;
   1.116 +}
   1.117 +
   1.118 +CString winGetDestDir(const CString &TargetDirReg)
   1.119 +{
   1.120 +	CString targetDir = regQueryStringValue(TargetDirReg, NULL);
   1.121 +	int pos = targetDir.ReverseFind('\\');
   1.122 +	if (pos > 0 && pos == targetDir.GetLength() - 1)
   1.123 +		targetDir.Delete(pos);
   1.124 +
   1.125 +	// it makes no sense to create rom directories
   1.126 +	// see MainWnd::winFileOpenSelect for more info
   1.127 +	if (!TargetDirReg.Compare(IDS_ROM_DIR) || !TargetDirReg.Compare(IDS_GBXROM_DIR))
   1.128 +		return targetDir;
   1.129 +
   1.130 +	if (targetDir.IsEmpty())
   1.131 +	{
   1.132 +		targetDir = theApp.exeDir;		// reset the targetDir to the application's path
   1.133 +		if (!TargetDirReg.Compare(IDS_BATTERY_DIR))
   1.134 +		{
   1.135 +			targetDir += IDS_BATTERY_DEFAULT_DIR;
   1.136 +		}
   1.137 +		else if (!TargetDirReg.Compare(IDS_SAVE_DIR))
   1.138 +		{
   1.139 +			targetDir += IDS_SAVE_DEFAULT_DIR;
   1.140 +		}
   1.141 +		else if (!TargetDirReg.Compare(IDS_MOVIE_DIR))
   1.142 +		{
   1.143 +			targetDir += IDS_MOVIE_DEFAULT_DIR;
   1.144 +		}
   1.145 +		else if (!TargetDirReg.Compare(IDS_CHEAT_DIR))
   1.146 +		{
   1.147 +			targetDir += IDS_CHEAT_DEFAULT_DIR;
   1.148 +		}
   1.149 +		else if (!TargetDirReg.Compare(IDS_LUA_DIR))
   1.150 +		{
   1.151 +			targetDir += IDS_LUA_DEFAULT_DIR;
   1.152 +		}
   1.153 +		else if (!TargetDirReg.Compare(IDS_IPS_DIR))
   1.154 +		{
   1.155 +			targetDir += IDS_IPS_DEFAULT_DIR;
   1.156 +		}
   1.157 +		else if (!TargetDirReg.Compare(IDS_AVI_DIR))
   1.158 +		{
   1.159 +			targetDir += IDS_AVI_DEFAULT_DIR;
   1.160 +		}
   1.161 +		else if (!TargetDirReg.Compare(IDS_WAV_DIR))
   1.162 +		{
   1.163 +			targetDir += IDS_WAV_DEFAULT_DIR;
   1.164 +		}
   1.165 +		else if (!TargetDirReg.Compare(IDS_CAPTURE_DIR))
   1.166 +		{
   1.167 +			targetDir += IDS_CAPTURE_DEFAULT_DIR;
   1.168 +		}
   1.169 +		else if (!TargetDirReg.Compare(IDS_WATCH_DIR))
   1.170 +		{
   1.171 +			targetDir += IDS_WATCH_DEFAULT_DIR;
   1.172 +		}
   1.173 +		regSetStringValue(TargetDirReg, targetDir);	// Add the directory to the INI file
   1.174 +	}
   1.175 +
   1.176 +	_mkdir(targetDir);			// make the directory
   1.177 +
   1.178 +	return targetDir;
   1.179 +}
   1.180 +
   1.181 +CString winGetDestFilename(const CString &LogicalRomName, const CString &TargetDirReg, const CString &ext)
   1.182 +{
   1.183 +	if (LogicalRomName.GetLength() == 0)
   1.184 +		return CString();
   1.185 +
   1.186 +	CString targetDir = winGetDestDir(TargetDirReg);
   1.187 +	targetDir += '\\';
   1.188 +
   1.189 +	CString buffer = LogicalRomName;
   1.190 +
   1.191 +	int index = max(buffer.ReverseFind('/'), max(buffer.ReverseFind('\\'), buffer.ReverseFind('|')));
   1.192 +	if (index != -1)
   1.193 +		buffer = buffer.Right(buffer.GetLength() - index - 1);
   1.194 +
   1.195 +	index = buffer.ReverseFind('.');
   1.196 +	if (index != -1)
   1.197 +		buffer = buffer.Left(index);
   1.198 +
   1.199 +	CString filename;
   1.200 +	filename.Format("%s%s%s", targetDir, buffer, ext);
   1.201 +	bool fileExists = winFileExists(filename);
   1.202 +
   1.203 +	// check for old style of naming, for better backward compatibility
   1.204 +	if (!fileExists || theApp.filenamePreference == 0)
   1.205 +	{
   1.206 +		index = LogicalRomName.Find('|');
   1.207 +		if (index != -1)
   1.208 +		{
   1.209 +			buffer = LogicalRomName.Left(index);
   1.210 +			index  = max(buffer.ReverseFind('/'), buffer.ReverseFind('\\'));
   1.211 +
   1.212 +			int dotIndex = buffer.ReverseFind('.');
   1.213 +			if (dotIndex > index)
   1.214 +				buffer = buffer.Left(dotIndex);
   1.215 +
   1.216 +			if (index != -1)
   1.217 +				buffer = buffer.Right(buffer.GetLength() - index - 1);
   1.218 +
   1.219 +			CString filename2;
   1.220 +			filename2.Format("%s%s%s", targetDir, buffer, ext);
   1.221 +			bool file2Exists = winFileExists(filename2);
   1.222 +
   1.223 +			if ((file2Exists && !fileExists) || (theApp.filenamePreference == 0 && (file2Exists || !fileExists)))
   1.224 +				return filename2;
   1.225 +		}
   1.226 +	}
   1.227 +
   1.228 +	return filename;
   1.229 +}
   1.230 +
   1.231 +CString winGetSavestateFilename(const CString &LogicalRomName, int nID)
   1.232 +{
   1.233 +	CString		ext;
   1.234 +//	size_t		startindex;	// forget about C89/ANSI-C
   1.235 +//	size_t		endindex;
   1.236 +	if (VBAMovieActive() && theApp.AsscWithSaveState)
   1.237 +	{
   1.238 +		std::string fs(VBAMovieGetFilename());	// RVO tip
   1.239 +		size_t startindex = fs.find_last_of("/\\") ;
   1.240 +		if (startindex < fs.length())
   1.241 +			++startindex;	// luckily the found character can't be at the end of fs
   1.242 +		else
   1.243 +			startindex = 0;
   1.244 +		size_t endindex = fs.find_last_of(".");
   1.245 +		if (endindex < fs.length() && endindex > startindex)
   1.246 +			endindex;	//??
   1.247 +		else
   1.248 +			endindex = fs.length();
   1.249 +		fs = fs.substr(startindex, endindex - startindex);
   1.250 +		ext.Format("-%s-%d.sgm", fs.c_str(), nID);
   1.251 +	}
   1.252 +	else
   1.253 +	{
   1.254 +		ext.Format("%d.sgm", nID);
   1.255 +	}
   1.256 +	return winGetDestFilename(LogicalRomName, IDS_SAVE_DIR, ext);
   1.257 +}
   1.258 +
   1.259 +CString winGetSavestateMenuString(const CString &LogicalRomName, int nID)
   1.260 +{
   1.261 +	CString str;
   1.262 +	if (theApp.showSlotTime)
   1.263 +	{
   1.264 +		CFileStatus status;
   1.265 +		if (emulating && CFile::GetStatus(winGetSavestateFilename(LogicalRomName, nID), status))
   1.266 +		{
   1.267 +			str.Format("#&%d %s", nID, status.m_mtime.Format("%Y/%m/%d %H:%M:%S"));
   1.268 +		}
   1.269 +		else
   1.270 +		{
   1.271 +			str.Format("#&%d ----/--/-- --:--:--", nID);
   1.272 +		}
   1.273 +	}
   1.274 +	else
   1.275 +	{
   1.276 +		str.Format("Slot #&%d", nID);
   1.277 +	}
   1.278 +
   1.279 +	return str;
   1.280 +}
   1.281 +
   1.282 +void winCorrectPath(CString &path)
   1.283 +{
   1.284 +	if (winFileExists(path))
   1.285 +	{
   1.286 +		return;
   1.287 +	}
   1.288 +
   1.289 +	CString tempStr = theApp.exeDir;
   1.290 +	tempStr += "\\";
   1.291 +	tempStr += path;
   1.292 +
   1.293 +	if (winFileExists(tempStr))
   1.294 +	{
   1.295 +		path = tempStr;
   1.296 +		return;
   1.297 +	}
   1.298 +
   1.299 +	for (int i = 0; i < _countof(IDS_tbl); ++i)
   1.300 +	{
   1.301 +		tempStr = winGetDestDir(IDS_tbl[i]);
   1.302 +		tempStr += "\\";
   1.303 +		tempStr += path;
   1.304 +
   1.305 +		if (winFileExists(tempStr))
   1.306 +		{
   1.307 +			path = tempStr;
   1.308 +			return;
   1.309 +		}
   1.310 +	}
   1.311 +}
   1.312 +
   1.313 +void winCorrectPath(char *path)
   1.314 +{
   1.315 +	CString pathCStr(path);
   1.316 +	winCorrectPath(pathCStr);
   1.317 +	strcpy(path, pathCStr);
   1.318 +}
   1.319 +
   1.320 +// some file I/O
   1.321 +
   1.322 +int winScreenCapture(int captureNumber)
   1.323 +{
   1.324 +	CString ext;
   1.325 +	CString captureName;
   1.326 +
   1.327 +	do
   1.328 +	{
   1.329 +		if (theApp.captureFormat == 0)
   1.330 +			ext.Format("_%03d.png", captureNumber);
   1.331 +		else
   1.332 +			ext.Format("_%03d.bmp", captureNumber);
   1.333 +
   1.334 +		captureName = winGetDestFilename(theApp.gameFilename, IDS_CAPTURE_DIR, ext);
   1.335 +		++captureNumber;
   1.336 +	} while (winFileExists(captureName) && captureNumber > 0);
   1.337 +
   1.338 +	if (captureNumber < 0)
   1.339 +	{
   1.340 +		systemMessage(0, "Too many existing files (not less than %d)! Screen capture failed!", captureNumber - 1);
   1.341 +		return 0;
   1.342 +	}
   1.343 +
   1.344 +	if (theApp.captureFormat == 0)
   1.345 +		theApp.emulator.emuWritePNG(captureName);
   1.346 +	else
   1.347 +		theApp.emulator.emuWriteBMP(captureName);
   1.348 +
   1.349 +	systemScreenMessage(winResLoadString(IDS_SCREEN_CAPTURE));
   1.350 +
   1.351 +	return captureNumber;
   1.352 +}
   1.353 +
   1.354 +bool winImportGSACodeFile(CString &fileName)
   1.355 +{
   1.356 +	FILE *f = fopen(fileName, "rb");
   1.357 +
   1.358 +	if (f == NULL)
   1.359 +	{
   1.360 +		systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", fileName);
   1.361 +		return false;
   1.362 +	}
   1.363 +
   1.364 +	if (systemCartridgeType == 1)
   1.365 +	{
   1.366 +		fclose(f);
   1.367 +		return gbCheatReadGSCodeFile(fileName);
   1.368 +	}
   1.369 +
   1.370 +	u32 len;
   1.371 +	fread(&len, 1, 4, f);
   1.372 +	if (len != 14)
   1.373 +	{
   1.374 +		fclose(f);
   1.375 +		systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s",
   1.376 +		              fileName);
   1.377 +		return false;
   1.378 +	}
   1.379 +	char buffer[16];
   1.380 +	fread(buffer, 1, 14, f);
   1.381 +	buffer[14] = 0;
   1.382 +	if (memcmp(buffer, "SharkPortCODES", 14))
   1.383 +	{
   1.384 +		fclose(f);
   1.385 +		systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s",
   1.386 +		              fileName);
   1.387 +		return false;
   1.388 +	}
   1.389 +	fseek(f, 0x1e, SEEK_SET);
   1.390 +	fread(&len, 1, 4, f);
   1.391 +	int game = 0;
   1.392 +	if (len > 1)
   1.393 +	{
   1.394 +		GSACodeSelect dlg(f);
   1.395 +		game = dlg.DoModal();
   1.396 +	}
   1.397 +	fclose(f);
   1.398 +
   1.399 +	bool v3 = false;
   1.400 +
   1.401 +	int index = fileName.ReverseFind('.');
   1.402 +
   1.403 +	if (index != -1)
   1.404 +	{
   1.405 +		if (fileName.Right(3).CompareNoCase("XPC") == 0)
   1.406 +			v3 = true;
   1.407 +	}
   1.408 +
   1.409 +	if (game != -1)
   1.410 +	{
   1.411 +		return cheatsImportGSACodeFile(fileName, game, v3);
   1.412 +	}
   1.413 +
   1.414 +	return true;
   1.415 +}
   1.416 +
   1.417 +void winLoadCheatList(const char *name)
   1.418 +{
   1.419 +	bool res = false;
   1.420 +
   1.421 +	if (systemCartridgeType == 0)
   1.422 +		res = cheatsLoadCheatList(name);
   1.423 +	else
   1.424 +		res = gbCheatsLoadCheatList(name);
   1.425 +
   1.426 +	if (res)
   1.427 +		systemScreenMessage(winResLoadString(IDS_LOADED_CHEATS));
   1.428 +}
   1.429 +
   1.430 +void winSaveCheatList(const char *name)
   1.431 +{
   1.432 +	if (systemCartridgeType == 0)
   1.433 +		cheatsSaveCheatList(name);
   1.434 +	else
   1.435 +		gbCheatsSaveCheatList(name);
   1.436 +}
   1.437 +
   1.438 +void winLoadCheatListDefault()
   1.439 +{
   1.440 +	CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, ".clt");
   1.441 +
   1.442 +	winLoadCheatList(cheatName);
   1.443 +}
   1.444 +
   1.445 +void winSaveCheatListDefault()
   1.446 +{
   1.447 +	CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, ".clt");
   1.448 +
   1.449 +	winSaveCheatList(cheatName);
   1.450 +}
   1.451 +
   1.452 +bool winReadBatteryFile()
   1.453 +{
   1.454 +	CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav");
   1.455 +
   1.456 +	bool res = false;
   1.457 +
   1.458 +	if (theApp.emulator.emuReadBattery)
   1.459 +		res = theApp.emulator.emuReadBattery(batteryName);
   1.460 +
   1.461 +	if (res)
   1.462 +		systemScreenMessage(winResLoadString(IDS_LOADED_BATTERY));
   1.463 +
   1.464 +	return res;
   1.465 +}
   1.466 +
   1.467 +bool winWriteBatteryFile()
   1.468 +{
   1.469 +	CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav");
   1.470 +
   1.471 +	if (theApp.emulator.emuWriteBattery)
   1.472 +		return theApp.emulator.emuWriteBattery(batteryName);
   1.473 +
   1.474 +	return false;
   1.475 +}
   1.476 +
   1.477 +bool winEraseBatteryFile()
   1.478 +{
   1.479 +	CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav");
   1.480 +	return !remove(batteryName);
   1.481 +}
   1.482 +
   1.483 +bool winReadSaveGame(const char *name)
   1.484 +{
   1.485 +	if (theApp.emulator.emuReadState)
   1.486 +		return theApp.emulator.emuReadState(name);
   1.487 +	return false;
   1.488 +}
   1.489 +
   1.490 +bool winWriteSaveGame(const char *name)
   1.491 +{
   1.492 +	if (theApp.emulator.emuWriteState)
   1.493 +		return theApp.emulator.emuWriteState(name);
   1.494 +	return false;
   1.495 +}
   1.496 +
   1.497 +bool winEraseSaveGame(const char *name)
   1.498 +{
   1.499 +	return !remove(name);
   1.500 +}