FReply UConfigImportExport::OnReadFromS3Bucket()

in Ambit/Source/Ambit/Mode/ConfigImportExport.cpp [321:416]


FReply UConfigImportExport::OnReadFromS3Bucket()
{
    FAmbitMode* AmbitMode = FAmbitMode::GetEditorMode();
    check(AmbitMode);

    FString BucketName;
    FString AwsRegion;
    GetAwsSettings(AwsRegion, BucketName, false);

    FString ConfigurationName = GetDefaultConfigurationName();
    if (!AmbitMode->UISettings->ConfigurationName.IsEmpty())
    {
        ConfigurationName = AmbitMode->UISettings->ConfigurationName;
    }

    FString BscPath = ConfigurationName + FileExtensions::KBSCExtension;

    TSet<FString> ObjectsSet;
    try
    {
        ObjectsSet = AWSWrapper::ListObjects(AwsRegion, BucketName);
    }
    catch (const std::invalid_argument& Ia)
    {
        FMenuHelpers::LogErrorAndPopup(Ia.what());
        return FReply::Handled();
    }
    catch (const std::runtime_error& Re)
    {
        FMenuHelpers::LogErrorAndPopup(Re.what());
        return FReply::Handled();
    }

    // If the bucket is empty
    if (ObjectsSet.Num() == 0)
    {
        FMenuHelpers::LogErrorAndPopup("There is no object in this bucket. Please check the input information again.");
        return FReply::Handled();
    }

    // If the bucket doesn't have this file
    if (!ObjectsSet.Contains(BscPath))
    {
        FMenuHelpers::LogErrorAndPopup(
            "Do not have this object. Please check the bucket name, object name and region again.");
        return FReply::Handled();
    }

    FString FullContents;

    try
    {
        FullContents = AWSWrapper::GetObject(AwsRegion, BucketName, BscPath);
    }
    catch (const std::invalid_argument& Ia)
    {
        FMenuHelpers::LogErrorAndPopup(Ia.what());
        return FReply::Handled();
    }
    catch (const std::runtime_error& Re)
    {
        FMenuHelpers::LogErrorAndPopup(Re.what());
        return FReply::Handled();
    }

    // Parse JSON file
    TSharedPtr<FJsonObject> DeserializedBsc = FJsonHelpers::DeserializeJson(FullContents);

    if (!DeserializedBsc)
    {
        FMenuHelpers::LogErrorAndPopup("Error Parsing Bulk Scenario Configuration.");
        return FReply::Handled();
    }

    FBulkScenarioConfiguration BscScenario;
    BscScenario.DeserializeFromJson(DeserializedBsc);

    if (BscScenario.ConfigurationName.IsEmpty())
    {
        FMenuHelpers::LogErrorAndPopup("Unable to Load Bulk Scenario Configuration. Please Check File and Try Again.");
        return FReply::Handled();
    }

    AmbitMode->UISettings->ConfigurationName = BscScenario.ConfigurationName;
    AmbitMode->UISettings->BatchName = BscScenario.BatchName;
    AmbitMode->UISettings->TimeOfDayTypes = BscScenario.TimeOfDayTypes;
    AmbitMode->UISettings->WeatherTypes = BscScenario.WeatherTypes;
    AmbitMode->UISettings->BulkPedestrianTraffic = BscScenario.PedestrianDensity;
    AmbitMode->UISettings->BulkVehicleTraffic = BscScenario.VehicleDensity;

    FAmbitDetailCustomization::UpdateNumberOfPermutations();

    CreateAmbitSpawnersFromJson(DeserializedBsc);

    return FReply::Handled();
}