BOOL LoadApi()

in XInput_Scp/XInput_SCP.cpp [7:103]


BOOL LoadApi(BOOL bEnable)
{
	if (bEnable) l_nAttached++; else l_nAttached--;

	if (bEnable && !l_bStarted && !l_bUnloaded)
	{
		load_lib_usb();
		WRAP_LoadXInput(true);

		// Load Bluetooth DS3s
		CBTConnection* BtPad = new CBTConnection();

		if (BtPad->Open())
		{
			l_nBtPads = BtPad->CollectionSize;

			for(DWORD Index = 0; Index < BtPad->CollectionSize; Index++)
			{
				l_Pad[l_nPads++] = BtPad;
			}
		}
		else delete BtPad;

		CSCPController*	Pad;

		// Load DS3s
		if (l_nBtPads == 0) // Only if not using BTH Server
		{
			for (DWORD Index = 0; Index < XUSER_MAX_COUNT * CDS3Controller::CollectionSize && l_nPads < XUSER_MAX_COUNT; Index++)
			{
				Pad = new CDS3Controller(Index);

				if (Pad->Open()) l_Pad[l_nPads++] = Pad;
				else { delete Pad; break; }
			}
		}

		// Load SL3s
		for (DWORD Index = 0; Index < XUSER_MAX_COUNT * CSL3Controller::CollectionSize && l_nPads < XUSER_MAX_COUNT; Index++)
		{
			Pad = new CSL3Controller(Index);

			if (Pad->Open()) l_Pad[l_nPads++] = Pad;
			else { delete Pad; break; }
		}

		// Load DS2s
		for (DWORD Index = 0; Index < XUSER_MAX_COUNT * CDS2Controller::CollectionSize && l_nPads < XUSER_MAX_COUNT; Index++)
		{
			Pad = new CDS2Controller(Index);

			if (Pad->Open()) l_Pad[l_nPads++] = Pad;
			else delete Pad;
		}
		
		// Load X360s
		if (l_nBtPads == 0) // Only if not using BTH Server
		{
			for (DWORD Index = 0; Index < XUSER_MAX_COUNT * CX360Controller::CollectionSize && l_nPads < XUSER_MAX_COUNT; Index++)
			{
				Pad = new CX360Controller(Index);

				if (Pad->Open()) l_Pad[l_nPads++] = Pad;
				else delete Pad;
			}
		}

		l_bStarted = true;

		if (l_nPads == 0)
		{
			// No Devices found, PassThrough only
			l_bPassThrough = true;
		}
	}
	else if (!bEnable && l_bStarted && !l_bUnloaded && l_nAttached == 0)
	{
		l_bStarted = false;
		l_bUnloaded = true;

		if (l_nBtPads > 0)
		{
			l_Pad[0]->Close();
			delete l_Pad[0];
		}

		for (DWORD Index = l_nBtPads; Index < l_nPads; Index++)
		{
			l_Pad[Index]->Close();
			delete l_Pad[Index];
		}

		return WRAP_LoadXInput(bEnable);
	}

	return true;
}