MarcusBuer

Register Subsystem when plugin loads

Sep 18th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | Source Code | 0 0
  1. // Copyright Luzera Studio. All Rights Reserved.
  2.  
  3. #include "LuzeraBenchmarkTool.h"
  4.  
  5. #include "AssetRegistry/AssetRegistryModule.h"
  6. #include "Engine/AssetManagerSettings.h"
  7. #include "Subsystems/BenchmarkSessionSubsystem.h"
  8.  
  9. #define LOCTEXT_NAMESPACE "FLuzeraBenchmarkToolModule"
  10.  
  11. /**
  12.  * @brief Called when the module is loaded into memory.
  13.  */
  14. void FLuzeraBenchmarkToolModule::StartupModule()
  15. {
  16.     // --- Find and Register the user's Benchmark Subsystem Blueprint ---
  17.     UAssetManagerSettings* Settings = GetMutableDefault<UAssetManagerSettings>();
  18.     if (!Settings) return;
  19.  
  20.     // 1. Find the user's Blueprint asset that inherits from our C++ base class.
  21.     const FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
  22.     TArray<FAssetData> SubsystemAssets;
  23.     FARFilter Filter;
  24.     Filter.ClassPaths.Add(UBlueprint::StaticClass()->GetClassPathName());
  25.     // We filter for Blueprints whose C++ parent is our abstract subsystem class.
  26.     Filter.TagsAndValues.Add(FBlueprintTags::NativeParentClassPath, FObjectPropertyBase::GetExportPath(UBenchmarkSessionSubsystem::StaticClass()));
  27.     AssetRegistryModule.Get().GetAssets(Filter, SubsystemAssets);
  28.  
  29.     if (SubsystemAssets.Num() == 0)
  30.     {
  31.         UE_LOG(LogTemp, Warning, TEXT("LuzeraBenchmarkTool: No Blueprint based on UBenchmarkSessionSubsystem found. It will not be available in packaged builds until one is created."));
  32.         return;
  33.     }
  34.    
  35.     // 2. Load the Blueprint asset and get its generated class.
  36.     UBlueprint* BlueprintAsset = Cast<UBlueprint>(SubsystemAssets[0].GetAsset());
  37.     if (!BlueprintAsset || !BlueprintAsset->GeneratedClass)
  38.     {
  39.         UE_LOG(LogTemp, Error, TEXT("LuzeraBenchmarkTool: Found a subsystem Blueprint, but it failed to load or has no generated class."));
  40.         return;
  41.     }
  42.     UClass* BlueprintClass = BlueprintAsset->GeneratedClass;
  43.    
  44.     // 3. The Primary Asset Type will be the name of the Blueprint class (e.g., BP_Benchmark_Subsystem_C).
  45.     const FName BlueprintAssetType = BlueprintClass->GetFName();
  46.  
  47.     // 4. Check if this specific Blueprint is already registered.
  48.     bool bIsAlreadyRegistered = false;
  49.     for (const FPrimaryAssetTypeInfo& AssetTypeInfo : Settings->PrimaryAssetTypesToScan)
  50.     {
  51.         if (AssetTypeInfo.PrimaryAssetType == BlueprintAssetType)
  52.         {
  53.             bIsAlreadyRegistered = true;
  54.             break;
  55.         }
  56.     }
  57.  
  58.     // 5. If it's not registered, add it now.
  59.     if (!bIsAlreadyRegistered)
  60.     {
  61.         // The AssetBaseClass is now the specific Blueprint class found above.
  62.         FPrimaryAssetTypeInfo NewAssetTypeInfo(
  63.             BlueprintAssetType,
  64.             BlueprintClass, // Use the actual Blueprint class here
  65.             true,           // It has Blueprint classes
  66.             false
  67.         );
  68.         NewAssetTypeInfo.Rules.CookRule = EPrimaryAssetCookRule::AlwaysCook;
  69.  
  70.         Settings->PrimaryAssetTypesToScan.Add(NewAssetTypeInfo);
  71.         Settings->SaveConfig();
  72.  
  73.         UE_LOG(LogTemp, Log, TEXT("LuzeraBenchmarkTool: Automatically registered '%s' with the Asset Manager."), *BlueprintAssetType.ToString());
  74.     }
  75. }
  76.  
  77. /**
  78.  * @brief Called before the module is unloaded from memory.
  79.  */
  80. void FLuzeraBenchmarkToolModule::ShutdownModule()
  81. {
  82.     // This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
  83.     // we call this function before unloading the module.
  84. }
  85.  
  86. #undef LOCTEXT_NAMESPACE
  87.  
  88. IMPLEMENT_MODULE(FLuzeraBenchmarkToolModule, LuzeraBenchmarkTool)
  89.  
Advertisement
Add Comment
Please, Sign In to add comment