malice936

Plugin.h

Apr 13th, 2025 (edited)
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | Gaming | 0 0
  1. // Plugin.h (Updated April 19, 2025)
  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.     // Array of records parsed from the plugin
  44.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Plugin", meta = (ToolTip = "Records parsed from the plugin."))
  45.     TArray<FRecord> Records;
  46.  
  47.     // Default constructor initializing to safe defaults
  48.     FPlugin()
  49.         : GameId(EGameID::Skyrim) // Default to Skyrim for safety
  50.         , Name(TEXT(""))          // Empty name
  51.         , HeaderRecord()          // Default header record
  52.         , FormIds()               // Empty FormID set
  53.         , Records()               // Empty records array
  54.     {
  55.     }
  56.  
  57.     // Parameterized constructor for initializing with specific values
  58.     FPlugin(EGameID InGameId, const FString& InName, const FRecord& InHeaderRecord, const TSet<FFormID>& InFormIds)
  59.         : GameId(InGameId)        // Set game identifier
  60.         , Name(InName)            // Set plugin filename
  61.         , HeaderRecord(InHeaderRecord) // Set header record
  62.         , FormIds()               // Initialize empty FormIDs
  63.         , Records()               // Initialize empty records array
  64.     {
  65.         FormIds = InFormIds;      // Assign FormIDs
  66.     }
  67.  
  68.     // Equality operator to compare two plugins
  69.     bool operator==(const FPlugin& Other) const
  70.     {
  71.         // Compare GameId, Name, HeaderRecord, FormIds, and Records
  72.         return GameId == Other.GameId &&
  73.                Name == Other.Name &&
  74.                HeaderRecord == Other.HeaderRecord &&
  75.                FormIds.Num() == Other.FormIds.Num() &&
  76.                FormIds.Difference(Other.FormIds).IsEmpty() &&
  77.                Records == Other.Records;
  78.     }
  79.  
  80.     // Inequality operator derived from equality
  81.     bool operator!=(const FPlugin& Other) const
  82.     {
  83.         return !(*this == Other); // Negate result of operator==
  84.     }
  85. };
  86.  
  87. // Hash function for using FPlugin in TMap/TSet containers
  88. FORCEINLINE uint32 GetTypeHash(const FPlugin& Plugin)
  89. {
  90.     uint32 Hash = 0; // Initialize hash value
  91.     // Combine hashes of GameId (cast to uint8 for enum), Name, and HeaderRecord
  92.     Hash = HashCombine(Hash, GetTypeHash(static_cast<uint8>(Plugin.GameId)));
  93.     Hash = HashCombine(Hash, GetTypeHash(Plugin.Name));
  94.     Hash = HashCombine(Hash, GetTypeHash(Plugin.HeaderRecord));
  95.  
  96.     // Iterate over FormIds and combine their hashes
  97.     for (const FFormID& FormId : Plugin.FormIds)
  98.     {
  99.         Hash = HashCombine(Hash, GetTypeHash(FormId));
  100.     }
  101.  
  102.     // Iterate over Records and combine their hashes
  103.     for (const FRecord& Record : Plugin.Records)
  104.     {
  105.         Hash = HashCombine(Hash, GetTypeHash(Record));
  106.     }
  107.  
  108.     return Hash; // Return final hash value
  109. }
  110.  
  111. // Blueprint function library for plugin-related operations
  112. UCLASS()
  113. class UNREALCREATIONENGINE_API UPluginHelper : public UBlueprintFunctionLibrary
  114. {
  115.     GENERATED_BODY()
  116.  
  117. public:
  118.     // Loads a plugin from a file, optionally reading only the header
  119.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Loads a plugin from a file, optionally header-only."))
  120.     static bool LoadPlugin(FPlugin& OutPlugin, const FString& FilePath, EGameID GameID, bool bLoadHeaderOnly = false);
  121.  
  122.     // Loads a plugin from a byte array, optionally reading only the header
  123.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Loads a plugin from a byte array, optionally header-only."))
  124.     static bool LoadPluginFromBytes(FPlugin& OutPlugin, TArray<FFormID>& OutFormIds, const TArray<uint8>& ByteArray, EGameID GameID, bool bLoadHeaderOnly = false);
  125.  
  126.     // Checks if a plugin file is valid by attempting to load it
  127.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin file is valid."))
  128.     static bool IsValidPlugin(const FString& FilePath, EGameID GameID, bool bLoadHeaderOnly = false);
  129.  
  130.     // Gets the plugin's filename
  131.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the plugin filename."))
  132.     static FString GetName(const FPlugin& Plugin);
  133.  
  134.     // Checks if the plugin is a master file (.esm or flagged)
  135.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin is a master file (.esm or flagged)."))
  136.     static bool IsMasterFile(const FPlugin& Plugin);
  137.  
  138.     // Gets the list of master plugin filenames
  139.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the list of master plugins."))
  140.     static TArray<FString> GetMasters(const FPlugin& Plugin);
  141.  
  142.     // Gets the plugin description from HEDR (Morrowind) or SNAM (others)
  143.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the plugin description from HEDR/SNAM."))
  144.     static FString GetDescription(const FPlugin& Plugin);
  145.  
  146.     // Gets the unique FormIDs (non-Morrowind plugins only)
  147.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the unique FormIDs (non-Morrowind only)."))
  148.     static TArray<FFormID> GetFormIds(const FPlugin& Plugin);
  149.  
  150.     // Gets the total number of records and groups from HEDR
  151.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the total number of records and groups."))
  152.     static int32 GetRecordAndGroupCount(const FPlugin& Plugin);
  153.  
  154.     // Checks if a specific master plugin is listed as a dependency
  155.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Returns true if the plugin is a master dependency."))
  156.     static bool HasMaster(const FPlugin& Plugin, const FString& MasterName);
  157.  
  158.     // Gets the number of master plugins
  159.     UFUNCTION(BlueprintPure, Category = "Plugin", meta = (ToolTip = "Gets the number of master plugins."))
  160.     static int32 GetMasterCount(const FPlugin& Plugin);
  161.  
  162.     // Gets the records parsed from the plugin
  163.     UFUNCTION(BlueprintCallable, Category = "Plugin", meta = (ToolTip = "Gets the records parsed from the plugin."))
  164.     static TArray<FRecord> GetPluginRecords(const FPlugin& Plugin);
  165. };
Advertisement
Add Comment
Please, Sign In to add comment