Skip to content

Commit

Permalink
Fix crash that happens when loading cooked content with missing Car a…
Browse files Browse the repository at this point in the history
…ssets by adding more logging information to help user understand what happened, and moving the asset loading to later in the process (non-static).
  • Loading branch information
lovettchris committed Apr 5, 2018
1 parent 1c7b250 commit 87fcf32
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 49 deletions.
39 changes: 33 additions & 6 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Methods -> CamelCase
parameters -> camel_case
*/


bool UAirBlueprintLib::log_messages_hidden = false;
uint32_t UAirBlueprintLib::FlushOnDrawCount = 0;
msr::airlib::AirSimSettings::SegmentationSettings::MeshNamingMethodType UAirBlueprintLib::mesh_naming_method =
Expand Down Expand Up @@ -150,6 +149,7 @@ void UAirBlueprintLib::LogMessage(const FString &prefix, const FString &suffix,
if (log_messages_hidden)
return;


static TMap<FString, int> loggingKeys;
static int counter = 1;

Expand All @@ -161,13 +161,27 @@ void UAirBlueprintLib::LogMessage(const FString &prefix, const FString &suffix,

FColor color;
switch (level) {
case LogDebugLevel::Informational: color = FColor(147, 231, 237); break;
case LogDebugLevel::Success: color = FColor(156, 237, 147); break;
case LogDebugLevel::Failure: color = FColor(237, 147, 168); break;
case LogDebugLevel::Unimportant: color = FColor(237, 228, 147); break;
case LogDebugLevel::Informational:
color = FColor(147, 231, 237);
UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Success:
color = FColor(156, 237, 147);
UE_LOG(LogAirSim, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Failure:
color = FColor(237, 147, 168);
UE_LOG(LogAirSim, Error, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Unimportant:
color = FColor(237, 228, 147);
UE_LOG(LogAirSim, Verbose, TEXT("%s%s"), *prefix, *suffix);
break;
default: color = FColor::Black; break;
}
GEngine->AddOnScreenDebugMessage(key, persist_sec, color, prefix + suffix);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(key, persist_sec, color, prefix + suffix);
}
//GEngine->AddOnScreenDebugMessage(key + 10, 60.0f, color, FString::FromInt(key));
}

Expand Down Expand Up @@ -584,3 +598,16 @@ void UAirBlueprintLib::EnableInput(AActor* actor)
{
actor->EnableInput(actor->GetWorld()->GetFirstPlayerController());
}

UObject* UAirBlueprintLib::LoadObject(const std::string& name)
{
FString str(name.c_str());
UObject *obj = StaticLoadObject(UObject::StaticClass(), nullptr, *str);
if (obj == nullptr) {
std::string msg = "Failed to load asset - " + name;
FString fmsg(msg.c_str());
LogMessage(TEXT("Load: "), fmsg, LogDebugLevel::Failure);
throw std::invalid_argument(msg);
}
return obj;
}
3 changes: 3 additions & 0 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Kismet/KismetMathLibrary.h"
#include "Components/MeshComponent.h"
#include "LandscapeProxy.h"
#include "AirSim.h"
#include "common/AirSimSettings.hpp"
#include "AirBlueprintLib.generated.h"

Expand Down Expand Up @@ -105,6 +106,8 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
static void resetSimulatePhysics(AActor* actor);
static std::vector<UPrimitiveComponent*> getPhysicsComponents(AActor* actor);

static UObject* LoadObject(const std::string& name);

private:
template<typename T>
static void InitializeObjectStencilID(T* obj, bool ignore_existing = true);
Expand Down
47 changes: 24 additions & 23 deletions Unreal/Plugins/AirSim/Source/AirSim.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "AirSim.h"
#include "Misc/Paths.h"

DEFINE_LOG_CATEGORY(LogAirSim);

class FAirSim : public IModuleInterface
{
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};

IMPLEMENT_MODULE(FAirSim, AirSim)

void FAirSim::StartupModule()
{
//plugin startup
}

void FAirSim::ShutdownModule()
{
//plugin shutdown
// Licensed under the MIT License.

#include "AirSim.h"
#include "Misc/Paths.h"

DEFINE_LOG_CATEGORY(LogAirSim);

class FAirSim : public IModuleInterface
{
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};

IMPLEMENT_MODULE(FAirSim, AirSim)

void FAirSim::StartupModule()
{
//plugin startup
UE_LOG(LogAirSim, Log, TEXT("StartupModule: AirSim plugin"));
}

void FAirSim::ShutdownModule()
{
//plugin shutdown
}
24 changes: 12 additions & 12 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ ACarPawn::ACarPawn()
this->AutoPossessPlayer = EAutoReceiveInput::Player0;
//this->AutoReceiveInput = EAutoReceiveInput::Player0;

static MeshContructionHelpers helpers(AirSimSettings::singleton().car_mesh_paths);

GetMesh()->SetSkeletalMesh(helpers.skeleton);
GetMesh()->SetAnimationMode(EAnimationMode::AnimationBlueprint);
GetMesh()->SetAnimInstanceClass(helpers.bp->GeneratedClass);
SlipperyMaterial = helpers.slippery_mat;
NonSlipperyMaterial = helpers.non_slippery_mat;

static ConstructorHelpers::FClassFinder<APIPCamera> pip_camera_class(TEXT("Blueprint'/AirSim/Blueprints/BP_PIPCamera'"));
pip_camera_class_ = pip_camera_class.Succeeded() ? pip_camera_class.Class : nullptr;

UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());

check(Vehicle4W->WheelSetups.Num() == 4);
Expand Down Expand Up @@ -148,7 +137,7 @@ ACarPawn::ACarPawn()
InCarGear->SetVisibility(true);

// Setup the audio component and allocate it a sound cue
static ConstructorHelpers::FObjectFinder<USoundCue> SoundCue(TEXT("/AirSim/VehicleAdv/Sound/Engine_Loop_Cue.Engine_Loop_Cue"));
ConstructorHelpers::FObjectFinder<USoundCue> SoundCue(TEXT("/AirSim/VehicleAdv/Sound/Engine_Loop_Cue.Engine_Loop_Cue"));
EngineSoundComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("EngineSound"));
EngineSoundComponent->SetSound(SoundCue.Object);
EngineSoundComponent->SetupAttachment(GetMesh());
Expand Down Expand Up @@ -176,6 +165,17 @@ void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_se
else
EngineSoundComponent->Deactivate();

// load assets
MeshContructionHelpers helpers(AirSimSettings::singleton().car_mesh_paths);
GetMesh()->SetSkeletalMesh(helpers.skeleton);
GetMesh()->SetAnimationMode(EAnimationMode::AnimationBlueprint);
GetMesh()->SetAnimInstanceClass(helpers.bp->GeneratedClass);
SlipperyMaterial = helpers.slippery_mat;
NonSlipperyMaterial = helpers.non_slippery_mat;

ConstructorHelpers::FClassFinder<APIPCamera> pip_camera_class(TEXT("Blueprint'/AirSim/Blueprints/BP_PIPCamera'"));
pip_camera_class_ = pip_camera_class.Succeeded() ? pip_camera_class.Class : nullptr;

//put camera little bit above vehicle
FTransform camera_transform(FVector::ZeroVector);
FActorSpawnParameters camera_spawn_params;
Expand Down
14 changes: 6 additions & 8 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Components/SkeletalMeshComponent.h"
#include "PhysicalMaterials/PhysicalMaterial.h"
#include "common/AirSimSettings.hpp"
#include "AirBlueprintLib.h"
#include "CarPawn.generated.h"

class UPhysicalMaterial;
Expand Down Expand Up @@ -165,15 +166,12 @@ class ACarPawn : public AWheeledVehicle

MeshContructionHelpers(const msr::airlib::AirSimSettings::CarMeshPaths& paths)
{
skeleton = Cast<USkeletalMesh>(StaticLoadObject(UObject::StaticClass(),
nullptr, * (FString(paths.skeletal.c_str()))));
bp = Cast<UBlueprint>(StaticLoadObject(UObject::StaticClass(),
nullptr, * (FString(paths.bp.c_str()))));
slippery_mat = Cast<UPhysicalMaterial>(StaticLoadObject(UObject::StaticClass(),
nullptr, * (FString(paths.slippery_mat.c_str()))));
non_slippery_mat = Cast<UPhysicalMaterial>(StaticLoadObject(UObject::StaticClass(),
nullptr, * (FString(paths.non_slippery_mat.c_str()))));
skeleton = Cast<USkeletalMesh>(UAirBlueprintLib::LoadObject(paths.skeletal));
bp = Cast<UBlueprint>(UAirBlueprintLib::LoadObject(paths.bp));
slippery_mat = Cast<UPhysicalMaterial>(UAirBlueprintLib::LoadObject(paths.slippery_mat));
non_slippery_mat = Cast<UPhysicalMaterial>(UAirBlueprintLib::LoadObject(paths.non_slippery_mat));
}

};


Expand Down

0 comments on commit 87fcf32

Please sign in to comment.