Advertisement
malice936

Subrecord.h

Apr 13th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 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, which is used to specify game-specific parsing logic for subrecords.
  11. #include "GameID.h"
  12.  
  13. // Includes the generated header for this file, which is automatically created by Unreal Engine for USTRUCT and UCLASS definitions, ensuring proper reflection and serialization.
  14. #include "Subrecord.generated.h"
  15.  
  16. // Defines a struct that represents a subrecord, a common data structure in Bethesda game file formats (e.g., .ESM, .ESP, .ESL).
  17. // Marked as BlueprintType to allow its use in Blueprints, making it accessible to designers and modders.
  18. USTRUCT(BlueprintType)
  19. struct FSubrecord
  20. {
  21.     GENERATED_BODY()
  22.  
  23.     // The 4-byte type identifier of the subrecord (e.g., "NAME", "DATA"), stored as an FString for easy manipulation in Blueprints.
  24.     // BlueprintReadOnly and EditAnywhere allow it to be read in Blueprints and edited in the editor.
  25.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Subrecord", meta=(ToolTip="4-byte type identifier (e.g., NAME)."))
  26.     FString Type;
  27.  
  28.     // The raw binary data of the subrecord, stored as a TArray of uint8 (bytes), which can be interpreted based on the subrecord type.
  29.     // Also BlueprintReadOnly and EditAnywhere for accessibility in Blueprints and the editor.
  30.     UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Subrecord", meta=(ToolTip="Raw binary data of the subrecord."))
  31.     TArray<uint8> RawData;
  32.  
  33.     // Default constructor, initializing Type to an empty string and RawData to an empty array.
  34.     FSubrecord()
  35.         : Type(TEXT(""))
  36.         , RawData()
  37.     {
  38.     }
  39.  
  40.     // Parameterized constructor to initialize the subrecord with a given type and raw data.
  41.     FSubrecord(const FString& InType, const TArray<uint8>& InRawData)
  42.         : Type(InType)
  43.         , RawData(InRawData)
  44.     {
  45.     }
  46.  
  47.     // Equality operator to compare two subrecords based on their Type and RawData.
  48.     // Useful for checking if two subrecords are identical, which can be important for data validation or comparison in Blueprints or C++.
  49.     bool operator==(const FSubrecord& Other) const
  50.     {
  51.         return Type == Other.Type && RawData == Other.RawData;
  52.     }
  53.  
  54.     // Inequality operator, defined in terms of the equality operator for consistency.
  55.     bool operator!=(const FSubrecord& Other) const
  56.     {
  57.         return !(*this == Other);
  58.     }
  59. };
  60.  
  61. // Defines a hash function for FSubrecord, allowing it to be used in Unreal Engine's hash-based containers like TMap or TSet.
  62. // Combines the hashes of Type and RawData using XOR to create a unique hash value.
  63. // FORCEINLINE improves performance by suggesting the compiler inline this function.
  64. FORCEINLINE uint32 GetTypeHash(const FSubrecord& Subrecord)
  65. {
  66.     return GetTypeHash(Subrecord.Type) ^ GetTypeHash(Subrecord.RawData);
  67. }
  68.  
  69. // Declares a Blueprint Function Library class that provides utility functions for working with subrecords.
  70. // This class is designed to be used in Blueprints, offering functions to read and manipulate subrecords.
  71. // UNREALCREATIONENGINE_API ensures the class is exported for use across modules if needed.
  72. UCLASS()
  73. class UNREALCREATIONENGINE_API USubrecordHelper : public UBlueprintFunctionLibrary
  74. {
  75.     GENERATED_BODY()
  76.  
  77. public:
  78.     // Static function to read a subrecord from an FArchive, which is an Unreal Engine serialization object.
  79.     // This function is not exposed to Blueprints due to the use of FArchive, a C++-only type.
  80.     // It serves as the core parsing logic, handling game-specific differences based on EGameID.
  81.     // DataLengthOverride allows optional specification of data length, defaulting to 0 if unused.
  82.     static bool ReadSubrecord(FSubrecord& OutSubrecord, FArchive& Archive, EGameID GameID, int32 DataLengthOverride = 0);
  83.  
  84.     // Blueprint-callable function to read a subrecord from a file specified by its path.
  85.     // Abstracts the file reading process, creating an FArchive internally and calling ReadSubrecord.
  86.     // Allows modders to read subrecords directly from files in Blueprints without dealing with low-level details.
  87.     UFUNCTION(BlueprintCallable, Category = "Subrecord", meta=(ToolTip="Reads a subrecord from a file with game-specific logic."))
  88.     static bool ReadSubrecordFromFile(FSubrecord& OutSubrecord, const FString& FilePath, EGameID GameID, int32 DataLengthOverride = 0);
  89.  
  90.     // Blueprint-callable function to read a subrecord from a byte array.
  91.     // Useful for reading subrecords from in-memory data, such as data received over a network or pre-loaded assets.
  92.     // Creates an FArchive from the byte array and calls ReadSubrecord.
  93.     UFUNCTION(BlueprintCallable, Category = "Subrecord", meta=(ToolTip="Reads a subrecord from a byte array with game-specific logic."))
  94.     static bool ReadSubrecordFromBytes(FSubrecord& OutSubrecord, const TArray<uint8>& ByteArray, EGameID GameID, int32 DataLengthOverride = 0);
  95.  
  96.     // Pure Blueprint function to get the Type of a subrecord.
  97.     // Marked as BlueprintPure, indicating it has no side effects and can be used in Blueprint graphs for data access.
  98.     UFUNCTION(BlueprintPure, Category = "Subrecord", meta=(ToolTip="Gets the 4-byte type identifier."))
  99.     static FString GetType(const FSubrecord& Subrecord);
  100.  
  101.     // Pure Blueprint function to get the RawData of a subrecord.
  102.     // Also BlueprintPure, allowing modders to access the binary data in Blueprints for further processing or display.
  103.     UFUNCTION(BlueprintPure, Category = "Subrecord", meta=(ToolTip="Gets the raw binary data."))
  104.     static TArray<uint8> GetRawData(const FSubrecord& Subrecord);
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement