main.cpp 26.3 KB
Newer Older
aliaspider's avatar
aliaspider committed
1
2

#include "PrecompiledHeader.h"
3

4
5
6
7
8
#ifdef WIN32
#include <windows.h>
#undef Yield
#endif

aliaspider's avatar
aliaspider committed
9
10
11
12
13
#include "AppCommon.h"
#include "App.h"

#include <cstdint>
#include <libretro.h>
14
#include <libretro_core_options.h>
aliaspider's avatar
aliaspider committed
15
16
#include <string>
#include <thread>
Libretro-Admin's avatar
Libretro-Admin committed
17
#include <wx/textfile.h>
18
#include <wx/stdpaths.h>
19
20
#include <wx/dir.h>
#include <wx/evtloop.h>
aliaspider's avatar
aliaspider committed
21
22

#include "GS.h"
23
#include "options_tools.h"
24
#include "retro_messager.h"
25
#include "language_injector.h"
aliaspider's avatar
aliaspider committed
26
27
#include "input.h"
#include "svnrev.h"
28
#include "disk_control.h"
aliaspider's avatar
aliaspider committed
29
#include "SPU2/Global.h"
30
#include "ps2/BiosTools.h"
31
#include "memcard_retro.h"
32

33
34


35
#include "MTVU.h"
aliaspider's avatar
aliaspider committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#ifdef PERF_TEST
static struct retro_perf_callback perf_cb;

#define RETRO_PERFORMANCE_INIT(name)                 \
	retro_perf_tick_t current_ticks;                 \
	static struct retro_perf_counter name = {#name}; \
	if (!name.registered)                            \
		perf_cb.perf_register(&(name));              \
	current_ticks = name.total

#define RETRO_PERFORMANCE_START(name) perf_cb.perf_start(&(name))
#define RETRO_PERFORMANCE_STOP(name) \
	perf_cb.perf_stop(&(name));      \
	current_ticks = name.total - current_ticks;
#else
#define RETRO_PERFORMANCE_INIT(name)
#define RETRO_PERFORMANCE_START(name)
#define RETRO_PERFORMANCE_STOP(name)
#endif

57
static bool init_failed = false;
58
int option_upscale_mult = 1;
Mauri-76's avatar
Mauri-76 committed
59
std::string sel_bios_path = "";
aliaspider's avatar
aliaspider committed
60
61
62
retro_environment_t environ_cb;
retro_video_refresh_t video_cb;
struct retro_hw_render_callback hw_render;
63
unsigned libretro_msg_interface_version = 0;
64
retro_log_printf_t log_cb;
aliaspider's avatar
aliaspider committed
65

Mauri-76's avatar
Mauri-76 committed
66
67
std::string retroarch_system_path;

aliaspider's avatar
aliaspider committed
68
69
// renderswitch - tells GSdx to go into dx9 sw if "renderswitch" is set.
bool renderswitch = false;
70
Pcsx2App* pcsx2;
aliaspider's avatar
misc.    
aliaspider committed
71
static wxFileName bios_dir;
aliaspider's avatar
aliaspider committed
72

Mauri-76's avatar
Mauri-76 committed
73
74
static const char* FILENAME_SHARED_MEMCARD_8 = "Shared Memory Card (8 MB)";
static const char* FILENAME_SHARED_MEMCARD_32 = "Shared Memory Card (32 MB)";
75

Mauri-76's avatar
Mauri-76 committed
76
77
78
79
80
81
82
83
84
85
wxFileName save_dir_root;

wxFileName slot1_file;
wxFileName slot2_file;

wxFileName legacy_memcard1;
wxFileName legacy_memcard2;

wxFileName save_game_folder;

86
87
88
89
static std::vector<std::string> bios_files;
static std::vector<std::string> custom_memcard_list_slot1;
static std::vector<std::string> custom_memcard_list_slot2;

aliaspider's avatar
aliaspider committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
void retro_set_video_refresh(retro_video_refresh_t cb)
{
	video_cb = cb;
}

void retro_set_environment(retro_environment_t cb)
{
	environ_cb = cb;
	bool no_game = true;
	environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_game);
#ifdef PERF_TEST
	environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb);
#endif
}

void retro_init(void)
{
	enum retro_pixel_format xrgb888 = RETRO_PIXEL_FORMAT_XRGB8888;
	environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &xrgb888);
	struct retro_log_callback log;
	if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
	{
		log_cb = log.log;
	}
114
	environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &libretro_msg_interface_version);
aliaspider's avatar
aliaspider committed
115

116
117
	const char* system = nullptr;
	environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system);
118
	
119
120
121
	const char* save_dir = nullptr;
	environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir);

122

Mauri-76's avatar
Mauri-76 committed
123
124
	retroarch_system_path = system;

125
	// checks and create save folders
126

Mauri-76's avatar
Mauri-76 committed
127
128
	save_dir_root = wxFileName(wxString(save_dir), "");
	save_dir_root.AppendDir("pcsx2");
129

Mauri-76's avatar
Mauri-76 committed
130
131
	slot1_file = wxFileName(save_dir_root.GetPath(), "");
	slot1_file.AppendDir("Slot 1");
132

Mauri-76's avatar
Mauri-76 committed
133
134
	slot2_file = wxFileName(save_dir_root.GetPath(), "");
	slot2_file.AppendDir("Slot 2");
135

Mauri-76's avatar
Mauri-76 committed
136
137
138
139
140
	
	if (! save_dir_root.DirExists()) save_dir_root.Mkdir();
	if (!slot1_file.DirExists()) slot1_file.Mkdir();
	if (!slot2_file.DirExists()) slot2_file.Mkdir();
	
141

142
143
	// check if legacy memcards exists

Mauri-76's avatar
Mauri-76 committed
144
145
146
	wxFileName legacy_dir(wxString(system), "");
	legacy_dir.AppendDir("pcsx2");
	legacy_dir.AppendDir("memcards");
147

Mauri-76's avatar
Mauri-76 committed
148
149
150
	legacy_memcard1 = wxFileName(legacy_dir.GetPath(), "");
	legacy_memcard1.SetName("Mcd001");
	legacy_memcard1.SetExt("ps2");
151

Mauri-76's avatar
Mauri-76 committed
152
153
154
	legacy_memcard2 = wxFileName(legacy_dir.GetPath(), "");
	legacy_memcard2.SetName("Mcd002");
	legacy_memcard2.SetExt("ps2");
155

156

157
158
159
	// get other 'custom' memcards put by the user in the slot 1 folder, if any

	wxArrayString memcard_files_slot1;
160

161
162
163
164
165
166
167
168
169
170
171

	wxDir::GetAllFiles(slot1_file.GetPath(), &memcard_files_slot1, L"*.*", wxDIR_FILES);
	for (wxString memcard_file : memcard_files_slot1)
	{
		wxFileName found_file;
		found_file.Assign(memcard_file);
		if (!found_file.GetName().IsSameAs(FILENAME_SHARED_MEMCARD_8) && !found_file.GetName().IsSameAs(FILENAME_SHARED_MEMCARD_8))
		{
			if (found_file.GetExt().IsSameAs("ps2"))
			{
				custom_memcard_list_slot1.push_back((std::string)found_file.GetFullPath());
172
173
174
175

				std::string name = (std::string)found_file.GetName();
				name.insert(0, "[USER] ");
				custom_memcard_list_slot1.push_back(name);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
			}
		}
		
	}

	// get other 'custom' memcards put by the user in the slot 2 folder, if any

	wxArrayString memcard_files_slot2;
	//wxString shared_path = wxString(shared_memcards_dir);

	wxDir::GetAllFiles(slot2_file.GetPath(), &memcard_files_slot2, L"*.*", wxDIR_FILES);
	for (wxString memcard_file : memcard_files_slot2)
	{
		wxFileName found_file;
		found_file.Assign(memcard_file);
		if (!found_file.GetName().IsSameAs(FILENAME_SHARED_MEMCARD_8) && !found_file.GetName().IsSameAs(FILENAME_SHARED_MEMCARD_8))
		{
			if (found_file.GetExt().IsSameAs("ps2"))
			{
				custom_memcard_list_slot2.push_back((std::string)found_file.GetFullPath());
196
197
198
199

				std::string name = (std::string)found_file.GetName();
				name.insert(0, "[USER] ");
				custom_memcard_list_slot2.push_back(name);
200
201
202
203
204
			}
		}
	}


205
206
207
	// breaking after filled should be more quick than looping throught all options
	// so we do this 2 times for both memcard options slot.
	// the max number of shared memcard is limited to 20
208
209
210
	//
	// Per game folders saves has been disabled because hangs while writing on disk
	// and seems not working well on some games
211
212
	for (retro_core_option_definition& def : option_defs)
	{								
Mauri-76's avatar
Mauri-76 committed
213
214
		if (!def.key || strcmp(def.key, "pcsx2_memcard_slot_1")) continue; 
		size_t i = 0;
215
		def.values[i++] = { "empty", "Empty" };
216
217
218
219
		if (legacy_memcard1.FileExists())
		{
			def.values[i++] = {"legacy", "Legacy"};
		}
220
221
		def.values[i++] = { "shared8", "Shared Memory Card (8 MB)" };
		def.values[i++] = { "shared32", "Shared Memory Card (32 MB)" };
222
		for (size_t j = 0; j < custom_memcard_list_slot1.size(); j += 2)
223
224
		{
			if (j >= 40) break;
225
			def.values[i++] = { custom_memcard_list_slot1[j].c_str(), custom_memcard_list_slot1[j + 1].c_str()};
226
		}
Mauri-76's avatar
Mauri-76 committed
227

228
		def.values[i++] = { NULL, NULL };
229
		if (i > 1)
230
		def.default_value = def.values[1].value;
231
232
233
234
235
236
		break;
	}

	for (retro_core_option_definition& def : option_defs)
	{
		if (!def.key || strcmp(def.key, "pcsx2_memcard_slot_2")) continue; 
Mauri-76's avatar
Mauri-76 committed
237
		size_t i = 0;
238
239
240
241
242
		def.values[i++] = {"empty", "Empty"};
		if (legacy_memcard2.FileExists())
		{
			def.values[i++] = {"legacy", "Legacy"};
		}
243
244
		def.values[i++] = { "shared8", "Shared Memory Card (8 MB)" };
		def.values[i++] = { "shared32", "Shared Memory Card (32 MB)" };
245
		for (size_t j = 0; j < custom_memcard_list_slot2.size(); j += 2)
246
247
		{
			if (j >= 40) break;
248
			def.values[i++] = { custom_memcard_list_slot2[j].c_str(), custom_memcard_list_slot2[j + 1].c_str()};
249
		}
Mauri-76's avatar
Mauri-76 committed
250

251
252
253
254
255
256
257
258
		def.values[i++] = { NULL, NULL };
		def.default_value = def.values[0].value;
		break;
	}


	// get the BIOS available and fill the option

259
260
261
262
263
264
	bios_dir = Path::Combine(system, "pcsx2/bios");

	wxArrayString bios_list;
	wxDir::GetAllFiles(bios_dir.GetFullPath(), &bios_list, L"*.*", wxDIR_FILES);
	for (wxString bios_file : bios_list)
	{
265
			wxString description;
266
			if (IsBIOSlite(bios_file, description)) {
267
268
269
270
				std::string log_bios = (std::string)description;
				wxFileName f;
				f.Assign(bios_file);
				bios_files.push_back((std::string)f.GetFullName());
271
272
				bios_files.push_back((std::string)description);
			}
273
	}
274
275
276
277
278
279
280
281
282
283

	// check if there are bios file in the bios folders, otherwise terminates with a explicit log message

	if (bios_files.size() == 0) {
		std::string checked_path = bios_dir.GetFullPath().ToStdString();
		log_cb(RETRO_LOG_ERROR, "Could not find valid BIOS files! \n");
		log_cb(RETRO_LOG_ERROR, "Please provide required BIOS file in %s folder \n", checked_path.c_str());
		return;
	}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

	for (retro_core_option_definition& def : option_defs)
	{
		if (!def.key || strcmp(def.key, "pcsx2_bios")) continue;
		size_t i = 0, numfiles = bios_files.size();
		int cont = 0;
		for (size_t f = 0; f != numfiles; f += 2)
		{
			def.values[i++] = { bios_files[f].c_str(), bios_files[f + 1].c_str() };
			cont++;
		}
		def.values[cont] = { NULL, NULL };
		def.default_value = def.values[0].value;
		break;
	}

300

301
302
	// loads the options structure to the frontend

303
	libretro_set_core_options(environ_cb);
304
305
306

	// start init some core settings

307
	option_upscale_mult = option_value(INT_PCSX2_OPT_UPSCALE_MULTIPLIER, KeyOptionInt::return_type);
308

309
310
311
312
313
314
	wxFileName f_bios;
	f_bios.Assign(option_value(STRING_PCSX2_OPT_BIOS, KeyOptionString::return_type));

	f_bios = wxFileName(bios_dir.GetFullPath(), option_value(STRING_PCSX2_OPT_BIOS, KeyOptionString::return_type));
	sel_bios_path = f_bios.GetFullPath().ToStdString();
	
315
316
317
	// instantiate the pcsx2 app and so some things on it
	pcsx2 = &wxGetApp();
	pxDoOutOfMemory = SysOutOfMemory_EmergencyResponse;
Mauri-76's avatar
Mauri-76 committed
318
	
319
	g_Conf = std::make_unique<AppConfig>();
320
321
	g_Conf->EmuOptions.BiosFilename.Assign(sel_bios_path);
	
322
	// some other stuffs about pcsx2
aliaspider's avatar
aliaspider committed
323

324
325
326
	if (pcsx2->DetectCpuAndUserMode())
	{
		pcsx2->AllocateCoreStuffs();
327

328
329
		// checks and creates the shared memory cards based on user option if
		// already set (e.g. global config + new content)
330

331
332
333
334
335
		if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "shared8") == 0)
		{
			slot1_file.SetName(FILENAME_SHARED_MEMCARD_8);
			slot1_file.SetExt("ps2");
			MemCardRetro::CreateSharedMemCardIfNotExisting(slot1_file, 8);
336

337
338
339
340
341
342
343
		}
		else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "shared32") == 0)
		{
			slot1_file.SetName(FILENAME_SHARED_MEMCARD_32);
			slot1_file.SetExt("ps2");
			MemCardRetro::CreateSharedMemCardIfNotExisting(slot1_file, 32);
		}
344
345


346
347
348
349
350
351
352
353
354
355
356
357
358
		if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "shared8") == 0)
		{
			slot2_file.SetName(FILENAME_SHARED_MEMCARD_8);
			slot2_file.SetExt("ps2");
			MemCardRetro::CreateSharedMemCardIfNotExisting(slot2_file, 8);

		}
		else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "shared32") == 0)
		{
			slot2_file.SetName(FILENAME_SHARED_MEMCARD_32);
			slot2_file.SetExt("ps2");
			MemCardRetro::CreateSharedMemCardIfNotExisting(slot2_file, 32);
		}
359
360


361
		// apply options to pcsx2
Mauri-76's avatar
Mauri-76 committed
362

363
364
365
		g_Conf->EnablePresets = true;
		g_Conf->EmuOptions.EnableIPC = false;
		g_Conf->EmuOptions.Speedhacks.fastCDVD  = option_value(BOOL_PCSX2_OPT_FASTCDVD, KeyOptionBool::return_type);
366

367
		g_Conf->EmuOptions.EnableNointerlacingPatches = (option_value(INT_PCSX2_OPT_DEINTERLACING_MODE, KeyOptionInt::return_type) == -1);
368
		g_Conf->EmuOptions.Enable60fpsPatches = (option_value(BOOL_PCSX2_OPT_ENABLE_60FPS_PATCHES, KeyOptionBool::return_type));
369
370
371
372
373
374
		g_Conf->EmuOptions.EnableWideScreenPatches = option_value(BOOL_PCSX2_OPT_ENABLE_WIDESCREEN_PATCHES, KeyOptionBool::return_type);
		g_Conf->EmuOptions.GS.FrameSkipEnable = option_value(BOOL_PCSX2_OPT_FRAMESKIP, KeyOptionBool::return_type);
		g_Conf->EmuOptions.GS.FramesToDraw = option_value(INT_PCSX2_OPT_FRAMES_TO_DRAW, KeyOptionInt::return_type);
		g_Conf->EmuOptions.GS.FramesToSkip = option_value(INT_PCSX2_OPT_FRAMES_TO_SKIP, KeyOptionInt::return_type);
		g_Conf->EmuOptions.GS.VsyncQueueSize = option_value(INT_PCSX2_OPT_VSYNC_MTGS_QUEUE, KeyOptionInt::return_type);
		g_Conf->EmuOptions.EnableCheats = option_value(BOOL_PCSX2_OPT_ENABLE_CHEATS, KeyOptionBool::return_type);
375
376


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
		int EE_clampMode = option_value(INT_PCSX2_OPT_EE_CLAMPING_MODE, KeyOptionInt::return_type);
		g_Conf->EmuOptions.Cpu.Recompiler.fpuOverflow = (EE_clampMode >= 1);
		g_Conf->EmuOptions.Cpu.Recompiler.fpuExtraOverflow = (EE_clampMode >= 2);
		g_Conf->EmuOptions.Cpu.Recompiler.fpuFullMode = (EE_clampMode >= 3);

		SSE_RoundMode EE_roundMode = (SSE_RoundMode)option_value(INT_PCSX2_OPT_EE_ROUND_MODE, KeyOptionInt::return_type);
		g_Conf->EmuOptions.Cpu.sseMXCSR.SetRoundMode(EE_roundMode);

		int VUs_clampMode = option_value(INT_PCSX2_OPT_VU_CLAMPING_MODE, KeyOptionInt::return_type);
		g_Conf->EmuOptions.Cpu.Recompiler.vuOverflow = (VUs_clampMode >= 1);
		g_Conf->EmuOptions.Cpu.Recompiler.vuExtraOverflow = (VUs_clampMode >= 2);
		g_Conf->EmuOptions.Cpu.Recompiler.vuSignOverflow = (VUs_clampMode >= 3);

		SSE_RoundMode VUs_roundMode = (SSE_RoundMode)option_value(INT_PCSX2_OPT_VU_ROUND_MODE, KeyOptionInt::return_type);
		g_Conf->EmuOptions.Cpu.sseVUMXCSR.SetRoundMode(VUs_roundMode);
392

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
		static retro_disk_control_ext_callback disk_control = {
			DiskControl::set_eject_state,
			DiskControl::get_eject_state,
			DiskControl::get_image_index,
			DiskControl::set_image_index,
			DiskControl::get_num_images,
			DiskControl::replace_image_index,
			DiskControl::add_image_index,
			DiskControl::set_initial_image,
			DiskControl::get_image_path,
			DiskControl::get_image_label,
		};

		environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control);

	}
	else
		init_failed = true;
aliaspider's avatar
aliaspider committed
411
412
413
414
}

void retro_deinit(void)
{
415
416
417
418
	/* FIXME: This is a workaround that resolves crashes on close content.
	When closing the frontend, we end up with a zombie process because the
	main thread tries to call vu1Thread.Cancel() within pcsx2's destructor
	and it gets stuck waiting for a mutex that will never unlock */
419
	vu1Thread.WaitVU();
420
	//vu1Thread.Cancel();
421

422
	pcsx2->CleanupOnExit();
423
	pcsx2->OnExit();
424
425
426
427
428

	bios_files.clear();
	custom_memcard_list_slot1.clear();
	custom_memcard_list_slot2.clear();

aliaspider's avatar
aliaspider committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifdef PERF_TEST
	perf_cb.perf_log();
#endif
}

void retro_get_system_info(retro_system_info* info)
{
#ifdef GIT_REV
	info->library_version = GIT_REV;
#else
	static char version[] = "#.#.#";
	version[0] = '0' + PCSX2_VersionHi;
	version[2] = '0' + PCSX2_VersionMid;
	version[4] = '0' + PCSX2_VersionLo;
	info->library_version = version;
#endif

446
	info->library_name = "pcsx2 (alpha)";
crumpmasterjc's avatar
crumpmasterjc committed
447
	info->valid_extensions = "elf|iso|ciso|chd|cso|cue|bin|m3u";
aliaspider's avatar
aliaspider committed
448
449
450
451
452
453
	info->need_fullpath = true;
	info->block_extract = true;
}

void retro_get_system_av_info(retro_system_av_info* info)
{
454
	if ( !std::strcmp(option_value(STRING_PCSX2_OPT_RENDERER, KeyOptionString::return_type), "Software") || !std::strcmp(option_value(STRING_PCSX2_OPT_RENDERER, KeyOptionString::return_type), "Null"))
aliaspider's avatar
misc.    
aliaspider committed
455
	{
456
457
		info->geometry.base_width = 640;
		info->geometry.base_height = 448;
aliaspider's avatar
misc.    
aliaspider committed
458
459
460
	}
	else
	{
461
462
		info->geometry.base_width = 640 * option_value(INT_PCSX2_OPT_UPSCALE_MULTIPLIER, KeyOptionInt::return_type);
		info->geometry.base_height = 448 * option_value(INT_PCSX2_OPT_UPSCALE_MULTIPLIER, KeyOptionInt::return_type);
aliaspider's avatar
misc.    
aliaspider committed
463
	}
aliaspider's avatar
aliaspider committed
464

465
466
467
	info->geometry.max_width = info->geometry.base_width;
	info->geometry.max_height = info->geometry.base_height;

468
	if (option_value(INT_PCSX2_OPT_ASPECT_RATIO, KeyOptionInt::return_type) == 0)
469
470
471
		info->geometry.aspect_ratio = 4.0f / 3.0f;
	else
		info->geometry.aspect_ratio = 16.0f / 9.0f;
aliaspider's avatar
aliaspider committed
472
473
474
475
476
477
	info->timing.fps = (retro_get_region() == RETRO_REGION_NTSC) ? (60.0f / 1.001f) : 50.0f;
	info->timing.sample_rate = 48000;
}

void retro_reset(void)
{
478
479
	GetMTGS().FinishTaskInThread();
	GetCoreThread().ResetQuick();
480
	DiskControl::eject_state = false;
aliaspider's avatar
aliaspider committed
481
482
}

483
static void context_reset(void)
aliaspider's avatar
aliaspider committed
484
{
485
	GetMTGS().OpenGS();
aliaspider's avatar
aliaspider committed
486
}
487
488

static void context_destroy(void)
aliaspider's avatar
aliaspider committed
489
490
{
	GetMTGS().FinishTaskInThread();
491
492
493

	while (pcsx2->HasPendingEvents())
		pcsx2->ProcessPendingEvents();
494
	GetMTGS().CloseGS();
495
496
	while (pcsx2->HasPendingEvents())
		pcsx2->ProcessPendingEvents();
aliaspider's avatar
aliaspider committed
497
498
}

499
static bool set_hw_render(retro_hw_context_type type)
aliaspider's avatar
aliaspider committed
500
{
501

aliaspider's avatar
aliaspider committed
502
503
504
505
506
507
	hw_render.context_type = type;
	hw_render.context_reset = context_reset;
	hw_render.context_destroy = context_destroy;
	hw_render.bottom_left_origin = true;
	hw_render.depth = true;
	hw_render.cache_context = false;
508

aliaspider's avatar
aliaspider committed
509
510
511
512
513
514
515
516
517
518
519
520
	switch (type)
	{
		case RETRO_HW_CONTEXT_DIRECT3D:
			hw_render.version_major = 11;
			hw_render.version_minor = 0;
			break;

		case RETRO_HW_CONTEXT_OPENGL_CORE:
			hw_render.version_major = 3;
			hw_render.version_minor = 3;
			break;

521
		case RETRO_HW_CONTEXT_OPENGL:
522
			if (set_hw_render(RETRO_HW_CONTEXT_OPENGL_CORE))
523
524
525
526
527
528
529
				return true;

			hw_render.version_major = 3;
			hw_render.version_minor = 0;
			break;

		case RETRO_HW_CONTEXT_OPENGLES3:
530
			if (set_hw_render(RETRO_HW_CONTEXT_OPENGL))
531
532
533
534
535
536
				return true;

			hw_render.version_major = 3;
			hw_render.version_minor = 0;
			break;

aliaspider's avatar
aliaspider committed
537
		case RETRO_HW_CONTEXT_NONE:
538
539
540
			hw_render.version_major = 3;
			hw_render.version_minor = 0;
			break;
541
542

		default:
aliaspider's avatar
aliaspider committed
543
544
			return false;
	}
545

aliaspider's avatar
aliaspider committed
546
547
548
	return environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render);
}

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
static wxVector<wxString>
read_m3u_file(const wxFileName& m3u_file)
{
	log_cb(RETRO_LOG_DEBUG, "Reading M3U file");

	wxVector<wxString> result;

	wxTextFile m3u_data;
	if (!m3u_data.Open(m3u_file.GetFullPath()))
	{
		log_cb(RETRO_LOG_ERROR, "M3U file \"%s\" cannot be read", m3u_file.GetFullPath().c_str());
		return result;
	}

	wxString base_path = m3u_file.GetPath();

	// This is the UTF-8 representation of U+FEFF.
	const wxString utf8_bom = "\xEF\xBB\xBF";

	wxString line = m3u_data.GetFirstLine();
	if (line.StartsWith(utf8_bom))
	{
		log_cb(RETRO_LOG_WARN, "M3U file \"%s\" contains UTF-8 BOM", m3u_file.GetFullPath().c_str());
		line.erase(0, utf8_bom.length());
	}

	for (line; !m3u_data.Eof(); line = m3u_data.GetNextLine())
	{
		if (!line.StartsWith('#')) // Comments start with #
		{
			wxFileName discFile(base_path, line);
			discFile.Normalize();
			if (discFile.Exists())
			{
				log_cb(RETRO_LOG_DEBUG, "Found disc image in M3U file, %s", discFile.GetFullPath());
				result.push_back(discFile.GetFullPath());
			}
			else
			{
				wxString full_path = discFile.GetFullPath();
				log_cb(RETRO_LOG_WARN, "File specified in the M3U file \"%s\" was not found:\n%s", m3u_file.GetFullPath().c_str(), full_path.c_str());
			}
		}
	}

	if (result.empty())
		log_cb(RETRO_LOG_ERROR, "No paths found in the M3U file \"%s\"", m3u_file.GetFullPath().c_str());

	return result;
}

aliaspider's avatar
aliaspider committed
600
601
bool retro_load_game(const struct retro_game_info* game)
{
602
603
604
605
606
	if (init_failed)
	{
		init_failed = false;
		return false;
	}
607
608

	ResetContentStuffs();
609

610
	const char* selected_bios = sel_bios_path.c_str();
611
	if (selected_bios == NULL)
612
	{
aliaspider's avatar
misc.    
aliaspider committed
613
		log_cb(RETRO_LOG_ERROR, "Could not find any valid PS2 Bios File in %s\n", (const char*)bios_dir.GetFullPath());
614
615
		return false;
	}
616
617
	else
		log_cb(RETRO_LOG_INFO, "Loading selected BIOS:  %s\n", selected_bios);
618

619

620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
	// we check if nvm file exists, if not it means that it's a new bios.
	// in this case whe bypass the fastboot option, forcing to enter the bios first run setup

	wxFileName nvmFileCheck;
	nvmFileCheck.Assign(sel_bios_path);
	nvmFileCheck.SetExt("nvm");
	
	bool fastboot_option = option_value(BOOL_PCSX2_OPT_FASTBOOT, KeyOptionBool::return_type);

	if (! nvmFileCheck.FileExists()) {
		fastboot_option = false;
	}




aliaspider's avatar
aliaspider committed
636
637
638
	const char* system = nullptr;
	environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system);

aliaspider's avatar
aliaspider committed
639
640
	//	pcsx2->Overrides.Gamefixes.Set( id, true);

aliaspider's avatar
aliaspider committed
641
642
	// By default no IRX injection
	g_Conf->CurrentIRX = "";
643
	g_Conf->BaseFilenames.Bios = selected_bios;
aliaspider's avatar
aliaspider committed
644

645
	DiskControl::eject_state = false;
aliaspider's avatar
misc.    
aliaspider committed
646

647

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
	// set up memcard on slot 1

	if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "empty") == 0)
	{
		// slot empty
		g_Conf->Mcd[0].Type = MemoryCardType::MemoryCard_None;
		g_Conf->Mcd[0].Enabled = false;
	}
	else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "shared8") == 0
		|| strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "shared32") == 0)
	{
		// Shared Memcards
		g_Conf->Mcd[0].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[0].Enabled = true;
		g_Conf->Mcd[0].Filename = slot1_file;

	}
	else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type), "legacy") == 0)
	{
		// legacy
		g_Conf->Mcd[0].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[0].Enabled = true;
		g_Conf->Mcd[0].Filename = legacy_memcard1;
	}
	else
	{
		// User imported memcards
		wxFileName user_memcard;
		user_memcard.Assign(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_1, KeyOptionString::return_type));

		g_Conf->Mcd[0].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[0].Enabled = true;
		g_Conf->Mcd[0].Filename = user_memcard;
	}


	// set up memcard on slot 2

	if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "empty") == 0)
	{
		// slot empty
		g_Conf->Mcd[1].Type = MemoryCardType::MemoryCard_None;
		g_Conf->Mcd[1].Enabled = false;
	}
	else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "shared8") == 0
		|| strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "shared32") == 0)
	{
		// Shared Memcards
		g_Conf->Mcd[1].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[1].Enabled = true;
		g_Conf->Mcd[1].Filename = slot2_file;
	}
	else if (strcmp(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type), "legacy") == 0)
	{
		// legacy
		g_Conf->Mcd[1].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[1].Enabled = true;
		g_Conf->Mcd[1].Filename = legacy_memcard2;
	}
	else
	{
		// User imported memcards
		wxFileName user_memcard;
		user_memcard.Assign(option_value(STRING_PCSX2_OPT_MEMCARD_SLOT_2, KeyOptionString::return_type));

		g_Conf->Mcd[1].Type = MemoryCardType::MemoryCard_File;
		g_Conf->Mcd[1].Enabled = true;
		g_Conf->Mcd[1].Filename = user_memcard;

	}


aliaspider's avatar
aliaspider committed
720
721
	if (game)
	{
722

723
		LanguageInjector::Inject(
724
				sel_bios_path,
725
726
727
				option_value(STRING_PCSX2_OPT_SYSTEM_LANGUAGE, KeyOptionString::return_type)
				);

728

729

730
731
732
733
734
735
736
737
738
739
740
741
742
		wxVector<wxString> game_paths;

		wxFileName file_name(game->path);
		if (file_name.GetExt() == "m3u")
		{
			game_paths = read_m3u_file(file_name);
			log_cb(RETRO_LOG_DEBUG, "Found %u game images in M3U file.", game_paths.size());
		}
		else
		{
			game_paths.push_back(game->path);
		}

743
		DiskControl::disk_images.assign(game_paths.begin(), game_paths.end());
744
745
746
747

		u32 magic = 0;
		FILE* fp = fopen(game_paths[0], "rb");

aliaspider's avatar
aliaspider committed
748
		if (!fp)
749
750
		{
			log_cb(RETRO_LOG_ERROR, "Could not open File: %s\n", game_paths[0]);
aliaspider's avatar
aliaspider committed
751
			return false;
752
		}
aliaspider's avatar
aliaspider committed
753
754
755

		fread(&magic, 4, 1, fp);
		fclose(fp);
aliaspider's avatar
aliaspider committed
756

757
758
759
		if (magic == 0x464C457F) // elf
		{
			// g_Conf->CurrentIRX = "";
760
			log_cb(RETRO_LOG_INFO, "ELF file detected, loading content....\n");
761
762
763
764
			g_Conf->EmuOptions.UseBOOT2Injection = true;
			pcsx2->SysExecute(CDVD_SourceType::NoDisc, game_paths[0]);
		}
		else
765
		{	
766

767
			g_Conf->EmuOptions.UseBOOT2Injection = fastboot_option;
768
769
			g_Conf->CdvdSource = CDVD_SourceType::Iso;
			g_Conf->CurrentIso = game_paths[0];
770
771


772
773
774
775
776
777
778
779
780
			if (!option_value(BOOL_PCSX2_OPT_BOOT_TO_BIOS, KeyOptionBool::return_type))
			{
				pcsx2->SysExecute(g_Conf->CdvdSource);
				log_cb(RETRO_LOG_INFO, "Game Loaded\n");
			}
			else
			{
				// we enter here in the bios, so we have the correct memcards loaded
				log_cb(RETRO_LOG_INFO, "Entrerning BIOS Menu.....\n");
Mauri-76's avatar
Mauri-76 committed
781
				RetroMessager::Notification("Boot to BIOS enabled", true);
782
				g_Conf->EmuOptions.UseBOOT2Injection = false;
783
784
785
786
787
				g_Conf->CdvdSource = CDVD_SourceType::NoDisc;
				g_Conf->CurrentIso = "";
				pcsx2->SysExecute(g_Conf->CdvdSource);

			}
788
		}
789

aliaspider's avatar
aliaspider committed
790
	}
791
	else
aliaspider's avatar
aliaspider committed
792
	{
793
		log_cb(RETRO_LOG_INFO, "Enterning BIOS Menu.....\n");
794
		g_Conf->EmuOptions.UseBOOT2Injection = false;
795
796
		g_Conf->CdvdSource = CDVD_SourceType::NoDisc;
		g_Conf->CurrentIso = "";
797
		pcsx2->SysExecute(g_Conf->CdvdSource);
aliaspider's avatar
aliaspider committed
798
	}
aliaspider's avatar
aliaspider committed
799

Libretro-Admin's avatar
Libretro-Admin committed
800
	g_Conf->EmuOptions.GS.FramesToDraw = 1;
801
	//	g_Conf->CurrentGameArgs = "";
aliaspider's avatar
aliaspider committed
802
803

	Input::Init();
804
	Input::RumbleEnabled(
805
806
807
			option_value(BOOL_PCSX2_OPT_GAMEPAD_RUMBLE_ENABLE, KeyOptionBool::return_type),
			option_value(INT_PCSX2_OPT_GAMEPAD_RUMBLE_FORCE, KeyOptionInt::return_type)
			);
aliaspider's avatar
aliaspider committed
808

809
	retro_hw_context_type context_type = RETRO_HW_CONTEXT_OPENGL;
810
811
812
813
	const char* option_renderer = option_value(STRING_PCSX2_OPT_RENDERER, KeyOptionString::return_type);
	log_cb(RETRO_LOG_INFO, "options renderer: %s\n", option_renderer);

	if (! std::strcmp(option_renderer,"Auto"))
814
		environ_cb(RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER, &context_type);
aliaspider's avatar
aliaspider committed
815
#ifdef _WIN32
816
	else if (!std::strcmp(option_renderer, "D3D11"))
817
		context_type = RETRO_HW_CONTEXT_DIRECT3D;
aliaspider's avatar
aliaspider committed
818
#endif
819
	else if (!std::strcmp(option_renderer, "Null"))
820
		context_type = RETRO_HW_CONTEXT_NONE;
aliaspider's avatar
aliaspider committed
821

822
	return set_hw_render(context_type);
aliaspider's avatar
aliaspider committed
823
824
825
826
827
828
829
830
831
832
833
}

bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info,
							 size_t num_info)
{
	return false;
}

void retro_unload_game(void)
{
	//	GetMTGS().FinishTaskInThread();
834
	//		GetMTGS().CloseGS();
835
836
837
838
	GetMTGS().FinishTaskInThread();

	while (pcsx2->HasPendingEvents())
		pcsx2->ProcessPendingEvents();
839
	GetMTGS().CloseGS();
840
841
842

	while (pcsx2->HasPendingEvents())
		pcsx2->ProcessPendingEvents();
aliaspider's avatar
aliaspider committed
843
844
845
846
847
}


void retro_run(void)
{
848
849
850
851
852
853
854
	bool updated = false;
	if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
	{
		log_cb(RETRO_LOG_INFO, "Options Change detected...\n");
		SetGSConfig().FrameSkipEnable = option_value(BOOL_PCSX2_OPT_FRAMESKIP, KeyOptionBool::return_type);
		SetGSConfig().FramesToDraw = option_value(INT_PCSX2_OPT_FRAMES_TO_DRAW, KeyOptionInt::return_type);
		SetGSConfig().FramesToSkip = option_value(INT_PCSX2_OPT_FRAMES_TO_SKIP, KeyOptionInt::return_type);
Mauri-76's avatar
Mauri-76 committed
855
		SetGSConfig().VsyncQueueSize = option_value(INT_PCSX2_OPT_VSYNC_MTGS_QUEUE, KeyOptionInt::return_type);
856
		//GSUpdateOptions();
857
858
859
860
		Input::RumbleEnabled(
			option_value(BOOL_PCSX2_OPT_GAMEPAD_RUMBLE_ENABLE, KeyOptionBool::return_type),
			option_value(INT_PCSX2_OPT_GAMEPAD_RUMBLE_FORCE, KeyOptionInt::return_type)
		);
861

862
	}
863

aliaspider's avatar
aliaspider committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
	Input::Update();

	RETRO_PERFORMANCE_INIT(pcsx2_run);
	RETRO_PERFORMANCE_START(pcsx2_run);

	GetMTGS().ExecuteTaskInThread();

	RETRO_PERFORMANCE_STOP(pcsx2_run);
}

size_t retro_serialize_size(void)
{
	return 0;
}

bool retro_serialize(void* data, size_t size)
{
	return false;
}
bool retro_unserialize(const void* data, size_t size)
{
	return false;
}

unsigned retro_get_region(void)
{
	return RETRO_REGION_NTSC;
}

unsigned retro_api_version()
{
	return RETRO_API_VERSION;
}

size_t retro_get_memory_size(unsigned id)
{
	return 0;
}

void* retro_get_memory_data(unsigned id)
{
	return NULL;
}

void retro_cheat_reset(void)
{
}

void retro_cheat_set(unsigned index, bool enabled, const char* code)
{
}

916
retro_audio_sample_t sample_cb;
aliaspider's avatar
aliaspider committed
917

aliaspider's avatar
aliaspider committed
918
919
920
921
922
923
924
925
926
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
}

void retro_set_audio_sample(retro_audio_sample_t cb)
{
	sample_cb = cb;
}

aliaspider's avatar
aliaspider committed
927
928
929
930
931
932
933
934
void DspUpdate()
{
}

s32 DspLoadLibrary(wchar_t* fileName, int modnum)
{
	return 0;
}
935

936
937
938
939
940
wxEventLoopBase* Pcsx2AppTraits::CreateEventLoop()
{
	return new wxEventLoop();
}

Libretro-Admin's avatar
Libretro-Admin committed
941
942
943
944
945
946
wxString GetExecutablePath()
{
	const char* system = nullptr;
	environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system);
	return Path::Combine(system, "pcsx2/PCSX2");
}