Advertisement
malice936

Plugin.h

Apr 13th, 2025
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.62 KB | Gaming | 0 0
  1. // Plugin.h
  2. #pragma once
  3.  
  4. // Core Unreal Engine types and macros
  5. #include "CoreMinimal.h"
  6. // Required for TSet operations like Difference()
  7. #include "Containers/Set.h"
  8. // Blueprint function library base class
  9. #include "Kismet/BlueprintFunctionLibrary.h"
  10. // Custom enum for game identification (e.g., Skyrim, Morrowind)
  11. #include "GameID.h"
  12. // Struct for unique FormIDs
  13. #include "FormID.h"
  14. // Struct for plugin header records
  15. #include "Record.h"
  16. // Struct for group data in plugins
  17. #include "Group.h"
  18. // Generated code for USTRUCT and UCLASS
  19. #include "Plugin.generated.h"
  20.  
  21. // Represents a Bethesda plugin file (.ESM, .ESP, .ESL) with metadata and FormIDs
  22. USTRUCT(BlueprintType)
  23. struct FPlugin
  24. {
  25.     GENERATED_BODY()
  26.  
  27.     // Game identifier (e.g., Skyrim, Morrowind) for plugin context
  28.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Plugin", meta = (ToolTip = "Game identifier (e.g., Skyrim, Morrowind)."))
  29.     EGameID GameId;
  30.  
  31.     // Plugin filename (e.g., "MyPlugin.esp") without path
  32.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Plugin", meta = (ToolTip = "Plugin filename."))
  33.     FString Name;
  34.  
  35.     // Header record (TES3 for Morrowind, TES4 for others) containing metadata
  36.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Plugin", meta = (ToolTip = "Header record of the plugin."))
  37.     FRecord HeaderRecord;
  38.  
  39.     // Set of unique FormIDs from records or groups in the plugin
  40.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Plugin", meta = (ToolTip = "Unique FormIDs in the plugin."))
  41.     TSet<FFormID> FormIds;
  42.  
  43.     // Default constructor initializing to safe defaults
  44.     FPlugin()
  45.         : GameId(EGameID::Skyrim) // Default to Skyrim for safety
  46.         , Name(TEXT(""))          // Empty name
  47.         , HeaderRecord()          // Default header record
  48.         , FormIds()               // Empty FormID set
  49.     {
  50.     }
  51.  
  52.     // Parameterized constructor for initializing with specific values
  53.     FPlugin(EGameID InGameId, const FString& InName, const FRecord& InHeaderRecord, const TSet<FFormID>& InFormIds)
  54.         : GameId(InGameId)        // Set game identifier
  55.         , Name(InName)            // Set plugin filename
  56.         , HeaderRecord(InHeaderRecord) // Set header record
  57.         , FormIds(InFormIds)      // Set FormIDs
  58.     {
  59.     }
  60.  
  61.     // Equality operator to compare two plugins
  62.     bool operator==(const FPlugin& Other) const
  63.     {
  64.         // Compare GameId, Name, HeaderRecord, and FormIds
  65.         // FormIds comparison uses Num() for size and Difference() for content equality
  66.         return GameId == Other.GameId &&
  67.                Name == Other.Name &&
  68.                HeaderRecord == Other.HeaderRecord &&
  69.                FormIds.Num() == Other.FormIds.Num() &&
  70.                FormIds.Difference(Other.FormIds).IsEmpty();
  71.     }
  72.  
  73.     // Inequality operator derived from equality
  74.     bool operator!=(const FPlugin& Other) const
  75.     {
  76.         return !(*this == Other); // Negate result of operator==
  77.     }
  78. };
  79.  
  80. // Hash function for using FPlugin in TMap/TSet containers
  81. FORCEINLINE uint32 GetTypeHash(const FPlugin& Plugin)
  82. {
  83.     uint32 Hash = 0; // Initialize hash value
  84.     // Combine hashes of GameId (cast to uint8 for enum), Name, and HeaderRecord
  85.     Hash = HashCombine(Hash, GetTypeHash(static_cast<uint8>(Plugin.GameId)));
  86.     Hash = HashCombine(Hash, GetTypeHash(Plugin.Name));
  87.     Hash = HashCombine(Hash, GetTypeHash(Plugin.HeaderRecord));
  88.  
  89.     // Iterate over FormIds and combine their hashes
  90.     for (const FFormID& FormId : Plugin.FormIds)
  91.     {
  92.         Hash = HashCombine(Hash, GetTypeHash(FormId));
  93.     }
  94.  
  95.     return Hash; // Return final hash value
  96. }
  97.  
  98. // Blueprint function library for plugin-related operations
  99. UCLASS()
  100. class UNREALCREATIONENGINE_API UPluginHelper : public UBlueprintFunctionLibrary
  101. {
  102.     GENERATED_BODY()
  103.  
  104. public:
  105.     // Loads a plugin from a file, optionally reading only the header
  106.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Loads a plugin from a file, optionally header-only."))
  107.     static bool LoadPlugin(FPlugin& OutPlugin, const FString& FilePath, EGameID GameID, bool bLoadHeaderOnly = false);
  108.  
  109.     // Loads a plugin from a byte array, optionally reading only the header
  110.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Loads a plugin from a byte array, optionally header-only."))
  111.     static bool LoadPluginFromBytes(FPlugin& OutPlugin, const TArray<uint8>& ByteArray, EGameID GameID, bool bLoadHeaderOnly = false);
  112.  
  113.     // Checks if a plugin file is valid by attempting to load it
  114.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin file is valid."))
  115.     static bool IsValidPlugin(const FString& FilePath, EGameID GameID, bool bLoadHeaderOnly = false);
  116.  
  117.     // Gets the plugin's filename
  118.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the plugin filename."))
  119.     static FString GetName(const FPlugin& Plugin);
  120.  
  121.     // Checks if the plugin is a master file (.esm or flagged)
  122.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin is a master file (.esm or flagged)."))
  123.     static bool IsMasterFile(const FPlugin& Plugin);
  124.  
  125.     // Gets the list of master plugin filenames
  126.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the list of master plugins."))
  127.     static TArray<FString> GetMasters(const FPlugin& Plugin);
  128.  
  129.     // Gets the plugin description from HEDR (Morrowind) or SNAM (others)
  130.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the plugin description from HEDR/SNAM."))
  131.     static FString GetDescription(const FPlugin& Plugin);
  132.  
  133.     // Gets the unique FormIDs (non-Morrowind plugins only)
  134.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the unique FormIDs (non-Morrowind only)."))
  135.     static TArray<FFormID> GetFormIds(const FPlugin& Plugin);
  136.  
  137.     // Gets the total number of records and groups from HEDR
  138.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the total number of records and groups."))
  139.     static int32 GetRecordAndGroupCount(const FPlugin& Plugin);
  140.  
  141.     // Checks if a specific master plugin is listed as a dependency
  142.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin is a master dependency."))
  143.     static bool HasMaster(const FPlugin& Plugin, const FString& MasterName);
  144.  
  145.     // Gets the number of master plugins
  146.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the number of master plugins."))
  147.     static int32 GetMasterCount(const FPlugin& Plugin);
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement