Advertisement
malice936

Record.h

Apr 13th, 2025
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.28 KB | Gaming | 0 0
  1. // Ensures this header is only included once during compilation to prevent redefinition errors.
  2. #pragma once
  3.  
  4. // Includes core Unreal Engine types and macros (e.g., FString, TArray, UPROPERTY) required for the struct and class definitions.
  5. #include "CoreMinimal.h"
  6.  
  7. // Includes the base class for Blueprint Function Libraries, allowing the creation of Blueprint-callable functions.
  8. #include "Kismet/BlueprintFunctionLibrary.h"
  9.  
  10. // Includes the GameID enum, used to specify game-specific parsing logic for records.
  11. #include "GameID.h"
  12.  
  13. // Includes the FormID struct, representing unique identifiers for records in non-Morrowind games.
  14. #include "FormID.h"
  15.  
  16. // Includes the Subrecord struct, used to store subrecords within a record.
  17. #include "Subrecord.h"
  18.  
  19. // Includes the generated header for this file, created by Unreal Engine for USTRUCT and UCLASS definitions.
  20. #include "Record.generated.h"
  21.  
  22. // Defines a struct representing a record, a higher-level data structure in Bethesda game file formats (e.g., .ESM, .ESP).
  23. // Marked as BlueprintType to allow use in Blueprints, enabling modders and designers to interact with records.
  24. USTRUCT(BlueprintType)
  25. struct FRecord
  26. {
  27.     GENERATED_BODY()
  28.  
  29.     // The 4-byte type identifier of the record (e.g., "NPC_", "WEAP"), stored as an FString for Blueprint compatibility.
  30.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Record", meta=(ToolTip="4-byte type identifier (e.g., NPC_)."))
  31.     FString Type;
  32.  
  33.     // Flags indicating record properties, such as compression status (e.g., bit 0x00040000 for compressed fields).
  34.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Record", meta=(ToolTip="Record flags (e.g., compression status)."))
  35.     int32 Flags;
  36.  
  37.     // FormID uniquely identifying the record, used in non-Morrowind games (e.g., Skyrim, Oblivion).
  38.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Record", meta=(ToolTip="FormID identifying the record."))
  39.     FFormID FormId;
  40.  
  41.     // Array of subrecords containing the record's detailed data.
  42.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Record", meta=(ToolTip="Subrecords containing record data."))
  43.     TArray<FSubrecord> Subrecords;
  44.  
  45.     // Default constructor, initializing fields to safe defaults.
  46.     FRecord()
  47.         : Type(TEXT(""))
  48.         , Flags(0)
  49.         , FormId(0)
  50.         , Subrecords()
  51.     {
  52.     }
  53.  
  54.     // Parameterized constructor to initialize a record with specific values.
  55.     FRecord(const FString& InType, int32 InFlags, const FFormID& InFormId, const TArray<FSubrecord>& InSubrecords)
  56.         : Type(InType)
  57.         , Flags(InFlags)
  58.         , FormId(InFormId)
  59.         , Subrecords(InSubrecords)
  60.     {
  61.     }
  62.  
  63.     // Equality operator to compare two records based on all fields.
  64.     bool operator==(const FRecord& Other) const
  65.     {
  66.         return Type == Other.Type && Flags == Other.Flags && FormId == Other.FormId && Subrecords == Other.Subrecords;
  67.     }
  68.  
  69.     // Inequality operator, defined using the equality operator for consistency.
  70.     bool operator!=(const FRecord& Other) const
  71.     {
  72.         return !(*this == Other);
  73.     }
  74. };
  75.  
  76. // Defines a hash function for FRecord, enabling use in hash-based containers like TMap or TSet.
  77. // Combines hashes of all fields using XOR for a unique value.
  78. FORCEINLINE uint32 GetTypeHash(const FRecord& Record)
  79. {
  80.     return GetTypeHash(Record.Type) ^ GetTypeHash(Record.Flags) ^ GetTypeHash(Record.FormId) ^ GetTypeHash(Record.Subrecords);
  81. }
  82.  
  83. // Declares a Blueprint Function Library for record-related utilities.
  84. // Provides parsing, file/byte reading, writing, and data access functions, all Blueprint-accessible.
  85. UCLASS()
  86. class UNREALCREATIONENGINE_API URecordHelper : public UBlueprintFunctionLibrary
  87. {
  88.     GENERATED_BODY()
  89.  
  90. public:
  91.     // Core parsing function to read a record from an FArchive.
  92.     // Not Blueprint-exposed due to FArchive, but used internally by other functions.
  93.     // Parameters:
  94.     // - OutRecord: The FRecord struct to populate with parsed data.
  95.     // - Archive: The FArchive to read from.
  96.     // - GameID: Specifies the game format for parsing logic.
  97.     // - bSkipSubrecords: If true, skips parsing subrecords.
  98.     // Returns: True if parsing was successful, false otherwise.
  99.     static bool ReadRecord(FRecord& OutRecord, FArchive& Archive, EGameID GameID, bool bSkipSubrecords);
  100.  
  101.     // Reads a record from a file, handling game-specific logic and optional subrecord skipping.
  102.     // Blueprint-callable for easy use in modding workflows.
  103.     // Parameters:
  104.     // - OutRecord: The FRecord to populate.
  105.     // - FilePath: Path to the file to read from.
  106.     // - GameID: Specifies the game format.
  107.     // - bSkipSubrecords: If true, skips subrecords.
  108.     // Returns: True if the record was successfully read, false otherwise.
  109.     UFUNCTION(BlueprintCallable, Category = "Record", meta=(ToolTip="Reads a record from a file with game-specific logic."))
  110.     static bool ReadRecordFromFile(FRecord& OutRecord, const FString& FilePath, EGameID GameID, bool bSkipSubrecords);
  111.  
  112.     // Reads a record from a byte array, useful for in-memory data or network transmission.
  113.     // Blueprint-callable for flexibility in data sources.
  114.     // Parameters:
  115.     // - OutRecord: The FRecord to populate.
  116.     // - ByteArray: The byte array to read from.
  117.     // - GameID: Specifies the game format.
  118.     // - bSkipSubrecords: If true, skips subrecords.
  119.     // Returns: True if the record was successfully read, false otherwise.
  120.     UFUNCTION(BlueprintCallable, Category = "Record", meta=(ToolTip="Reads a record from a byte array with game-specific logic."))
  121.     static bool ReadRecordFromBytes(FRecord& OutRecord, const TArray<uint8>& ByteArray, EGameID GameID, bool bSkipSubrecords);
  122.  
  123.     // Writes a record to a file, preserving its structure and game-specific format.
  124.     // Blueprint-callable for saving records to disk.
  125.     // Parameters:
  126.     // - Record: The FRecord to write.
  127.     // - FilePath: Path to the file to write to.
  128.     // - GameID: Specifies the game format for serialization.
  129.     // Returns: True if the record was successfully written, false otherwise.
  130.     UFUNCTION(BlueprintCallable, Category = "Record", meta=(ToolTip="Writes a record to a file with game-specific logic."))
  131.     static bool WriteRecordToFile(const FRecord& Record, const FString& FilePath, EGameID GameID);
  132.  
  133.     // Serializes a record to a byte array, useful for network transmission or storage.
  134.     // Blueprint-callable for flexible data handling.
  135.     // Parameters:
  136.     // - Record: The FRecord to serialize.
  137.     // - OutByteArray: The byte array to write the serialized data to.
  138.     // - GameID: Specifies the game format for serialization.
  139.     // Returns: True if the record was successfully serialized, false otherwise.
  140.     UFUNCTION(BlueprintCallable, Category = "Record", meta=(ToolTip="Serializes a record to a byte array with game-specific logic."))
  141.     static bool WriteRecordToBytes(const FRecord& Record, TArray<uint8>& OutByteArray, EGameID GameID);
  142.  
  143.     // Gets the record's 4-byte type identifier.
  144.     // BlueprintPure indicates this function has no side effects and can be used in Blueprint graphs.
  145.     // Parameters:
  146.     // - Record: The FRecord to get the type from.
  147.     // Returns: The 4-byte type identifier as an FString.
  148.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the 4-byte type identifier."))
  149.     static FString GetType(const FRecord& Record);
  150.  
  151.     // Gets the record's flags.
  152.     // BlueprintPure for use in Blueprint logic without side effects.
  153.     // Parameters:
  154.     // - Record: The FRecord to get the flags from.
  155.     // Returns: The flags as an int32.
  156.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the record flags."))
  157.     static int32 GetFlags(const FRecord& Record);
  158.  
  159.     // Gets the record's FormID.
  160.     // BlueprintPure for Blueprint accessibility.
  161.     // Parameters:
  162.     // - Record: The FRecord to get the FormID from.
  163.     // Returns: The FormID as an FFormID struct.
  164.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the FormID."))
  165.     static FFormID GetFormId(const FRecord& Record);
  166.  
  167.     // Gets the array of subrecords.
  168.     // BlueprintPure for Blueprint use.
  169.     // Parameters:
  170.     // - Record: The FRecord to get the subrecords from.
  171.     // Returns: The array of FSubrecord structs.
  172.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the subrecords."))
  173.     static TArray<FSubrecord> GetSubrecords(const FRecord& Record);
  174.  
  175.     // Checks if the record is compressed based on its flags.
  176.     // BlueprintPure for use in conditional logic in Blueprints.
  177.     // Parameters:
  178.     // - Record: The FRecord to check.
  179.     // Returns: True if the record is compressed, false otherwise.
  180.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Returns true if the record is compressed."))
  181.     static bool AreFieldsCompressed(const FRecord& Record);
  182.  
  183.     // Retrieves the first subrecord matching the specified type (e.g., "NAME").
  184.     // BlueprintPure for Blueprint-friendly data access.
  185.     // Parameters:
  186.     // - Record: The FRecord to search.
  187.     // - SubrecordType: The type of subrecord to find.
  188.     // - OutSubrecord: The found FSubrecord, if any.
  189.     // Returns: True if a matching subrecord was found, false otherwise.
  190.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the first subrecord with the specified type, if it exists."))
  191.     static bool GetSubrecordByType(const FRecord& Record, const FString& SubrecordType, FSubrecord& OutSubrecord);
  192.  
  193.     // Checks if a subrecord with the specified type exists.
  194.     // BlueprintPure for conditional checks in Blueprints.
  195.     // Parameters:
  196.     // - Record: The FRecord to check.
  197.     // - SubrecordType: The type of subrecord to look for.
  198.     // Returns: True if a subrecord with the specified type exists, false otherwise.
  199.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Returns true if a subrecord with the specified type exists."))
  200.     static bool HasSubrecord(const FRecord& Record, const FString& SubrecordType);
  201.  
  202.     // Returns the number of subrecords in the record.
  203.     // BlueprintPure for use in Blueprint logic.
  204.     // Parameters:
  205.     // - Record: The FRecord to get the subrecord count from.
  206.     // Returns: The number of subrecords as an int32.
  207.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Gets the number of subrecords in the record."))
  208.     static int32 GetSubrecordCount(const FRecord& Record);
  209.  
  210.     // Validates the record (non-empty type, valid FormID for non-Morrowind).
  211.     // BlueprintPure for validation in Blueprints.
  212.     // Parameters:
  213.     // - Record: The FRecord to validate.
  214.     // - GameID: The game format to determine FormID validity.
  215.     // Returns: True if the record is valid, false otherwise.
  216.     UFUNCTION(BlueprintPure, Category = "Record", meta=(ToolTip="Returns true if the record is valid (non-empty type, valid FormID)."))
  217.     static bool IsValidRecord(const FRecord& Record, EGameID GameID);
  218.  
  219.     // Clears all subrecords from the record.
  220.     // BlueprintCallable for modifying records in Blueprints.
  221.     // Parameters:
  222.     // - Record: The FRecord to clear subrecords from.
  223.     UFUNCTION(BlueprintCallable, Category = "Record", meta=(ToolTip="Clears all subrecords from the record."))
  224.     static void ClearSubrecords(FRecord& Record);
  225. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement