void UDeviceProfileManager::SaveProfiles(bool bSaveToDefaults)
{
	if( !HasAnyFlags( RF_ClassDefaultObject ) )
	{
		if(bSaveToDefaults)
		{
			for (int32 DeviceProfileIndex = 0; DeviceProfileIndex < Profiles.Num(); ++DeviceProfileIndex)
			{
				UDeviceProfile* CurrentProfile = CastChecked<UDeviceProfile>(Profiles[DeviceProfileIndex]);
				CurrentProfile->UpdateDefaultConfigFile();
			}
		}
		else
		{
			TArray< FString > DeviceProfileMapArray;

			for (int32 DeviceProfileIndex = 0; DeviceProfileIndex < Profiles.Num(); ++DeviceProfileIndex)
			{
				UDeviceProfile* CurrentProfile = CastChecked<UDeviceProfile>(Profiles[DeviceProfileIndex]);
				FString DeviceProfileTypeNameCombo = FString::Printf(TEXT("%s,%s"), *CurrentProfile->GetName(), *CurrentProfile->DeviceType);
				DeviceProfileMapArray.Add(DeviceProfileTypeNameCombo);

				CurrentProfile->SaveConfig(CPF_Config, *DeviceProfileFileName);
			}

			GConfig->SetArray(TEXT("DeviceProfiles"), TEXT("DeviceProfileNameAndTypes"), DeviceProfileMapArray, DeviceProfileFileName);
			GConfig->Flush(false, DeviceProfileFileName);
		}

		ManagerUpdatedDelegate.Broadcast();
	}
}
UDeviceProfile& UDeviceProfileManager::FindProfile( const FString& ProfileName )
{
	UDeviceProfile* FoundProfile = nullptr;

	for( int32 Idx = 0; Idx < Profiles.Num(); Idx++ )
	{
		UDeviceProfile* CurrentDevice = CastChecked<UDeviceProfile>( Profiles[Idx] );
		if( CurrentDevice->GetName() == ProfileName )
		{
			FoundProfile = CurrentDevice;
			break;
		}
	}

	return FoundProfile != nullptr ? *FoundProfile : CreateProfile(ProfileName, FPlatformProperties::PlatformName());
}
UDeviceProfile* UDeviceProfileManager::FindProfile( const FString& ProfileName )
{
	UDeviceProfile* FoundProfile = NULL;

	for( int32 Idx = 0; Idx < Profiles.Num(); Idx++ )
	{
		UDeviceProfile* CurrentDevice = CastChecked<UDeviceProfile>( Profiles[Idx] );
		if( CurrentDevice->GetName() == ProfileName )
		{
			FoundProfile = CurrentDevice;
			break;
		}
	}

	return FoundProfile;
}
void UDeviceProfileManager::InternalSaveProfiles()
{
	TArray< FString > DeviceProfileMapArray;
	
	for(int32 DeviceProfileIndex = 0; DeviceProfileIndex < Profiles.Num(); ++DeviceProfileIndex)
	{
		UDeviceProfile* CurrentProfile = CastChecked<UDeviceProfile>( Profiles[DeviceProfileIndex] );
		FString DeviceProfileTypeNameCombo = FString::Printf( TEXT("%s,%s"), *CurrentProfile->GetName(), *CurrentProfile->DeviceType );
		DeviceProfileMapArray.Add( DeviceProfileTypeNameCombo );

		CurrentProfile->SaveConfig(CPF_Config, *DeviceProfileFileName);
	}

	GConfig->SetArray( TEXT( "DeviceProfiles" ), TEXT( "DeviceProfileNameAndTypes" ), DeviceProfileMapArray, DeviceProfileFileName );
	GConfig->Flush( false, DeviceProfileFileName );
}
UDeviceProfile* UDeviceProfileManager::CreateProfile( const FString& ProfileName, const FString& ProfileType, const FString& ParentName )
{
	UDeviceProfile* DeviceProfile = FindObject<UDeviceProfile>( GetTransientPackage(), *ProfileName );
	if( DeviceProfile == NULL )
	{
		DeviceProfile = ConstructObject<UDeviceProfile>( UDeviceProfile::StaticClass(), GetTransientPackage(), *ProfileName, RF_Transient|RF_Public);
		DeviceProfile->LoadConfig( UDeviceProfile::StaticClass(), *DeviceProfileFileName );
		DeviceProfile->BaseProfileName = ParentName != TEXT("") ? ParentName : DeviceProfile->BaseProfileName;
		DeviceProfile->DeviceType = ProfileType;

		UDeviceProfile* ObjectTemplate = NULL;

		// Recursively build the parent tree
		if( DeviceProfile->BaseProfileName != TEXT("") )
		{
			DeviceProfile->Parent = FindObject<UDeviceProfile>( GetTransientPackage(), *DeviceProfile->BaseProfileName );
			if( DeviceProfile->Parent == NULL )
			{
				DeviceProfile->Parent = CreateProfile( DeviceProfile->BaseProfileName, ProfileType );
			}
			ObjectTemplate = CastChecked<UDeviceProfile>(DeviceProfile->Parent);
		}

		if( ObjectTemplate )
		{
			DeviceProfile->ClearFlags( RF_AllFlags );
			DeviceProfile->Rename( *FString::Printf(TEXT("RedundantDeviceProfile_%d"), RenameIndex++), GetTransientPackage());
			DeviceProfile->SetFlags( RF_PendingKill );

			DeviceProfile = ConstructObject<UDeviceProfile>( UDeviceProfile::StaticClass(), GetTransientPackage(), *ProfileName, RF_Transient|RF_Public, ObjectTemplate);
			DeviceProfile->BaseProfileName = ObjectTemplate->GetName();
			DeviceProfile->DeviceType = ProfileType;
		}

		Profiles.Add( DeviceProfile );

		// Inform the UI that the device list has changed
		ManagerUpdatedDelegate.Broadcast(); 
	}

	return DeviceProfile;
}