CUBlueprintLibrary.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2018-current Getnamo. All Rights Reserved
  2. #pragma once
  3. #include "Kismet/BlueprintFunctionLibrary.h"
  4. #include "Async/Future.h"
  5. #include "Sound/SoundWaveProcedural.h"
  6. #include "CUBlueprintLibrary.generated.h"
  7. /** Wrapper for EImageFormat::Type for BP */
  8. UENUM()
  9. enum class EImageFormatBPType : uint8
  10. {
  11. /** Invalid or unrecognized format. */
  12. Invalid = 254,
  13. /** Portable Network Graphics. */
  14. PNG = 0,
  15. /** Joint Photographic Experts Group. */
  16. JPEG,
  17. /** Single channel JPEG. */
  18. GrayscaleJPEG,
  19. /** Windows Bitmap. */
  20. BMP,
  21. /** Windows Icon resource. */
  22. ICO,
  23. /** OpenEXR (HDR) image file format. */
  24. EXR,
  25. /** Mac icon. */
  26. ICNS
  27. };
  28. /** Callback threading option */
  29. UENUM(BlueprintType)
  30. enum ESIOCallbackType
  31. {
  32. CALLBACK_GAME_THREAD,
  33. CALLBACK_BACKGROUND_THREADPOOL,
  34. CALLBACK_BACKGROUND_TASKGRAPH
  35. };
  36. /**
  37. * Useful generic blueprint functions, mostly conversion
  38. */
  39. UCLASS()
  40. class COREUTILITY_API UCUBlueprintLibrary : public UBlueprintFunctionLibrary
  41. {
  42. GENERATED_BODY()
  43. public:
  44. //Conversion Nodes
  45. /**
  46. * Convert any unicode bytes to string
  47. */
  48. UFUNCTION(BlueprintPure, meta = (DisplayName = "To String (Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  49. static FString Conv_BytesToString(const TArray<uint8>& InBytes);
  50. /**
  51. * Convert string to UTF8 bytes
  52. */
  53. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Bytes (String)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  54. static TArray<uint8> Conv_StringToBytes(FString InString);
  55. /**
  56. * Convert bytes to UTexture2D using auto-detection - optimized, but can still have performance implication
  57. */
  58. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Texture2D (Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  59. static UTexture2D* Conv_BytesToTexture(const TArray<uint8>& InBytes);
  60. /**
  61. * Audio decompression - Convert opus (currently raw serialized) to wav
  62. */
  63. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Wav Bytes (Opus Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  64. static TArray<uint8> Conv_OpusBytesToWav(const TArray<uint8>& InBytes);
  65. /**
  66. * Audio compression - Convert wav to opus (currently raw serialized)
  67. */
  68. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Opus Bytes (Wav Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  69. static TArray<uint8> Conv_WavBytesToOpus(const TArray<uint8>& InBytes);
  70. /**
  71. * Assumes .wav chunks - handles async alloc, callable from any thread
  72. */
  73. UFUNCTION(BlueprintPure, meta = (DisplayName = "To SoundWave (Wav Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  74. static USoundWave* Conv_WavBytesToSoundWave(const TArray<uint8>& InBytes);
  75. /**
  76. * convert a soundwave into wav bytes
  77. */
  78. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Bytes (SoundWave)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  79. static TArray<uint8> Conv_SoundWaveToWavBytes(USoundWave* SoundWave);
  80. /**
  81. * Compact Transform bytes are [[pitch,yaw,roll,x,y,z,sx,sy,sz],...]
  82. */
  83. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Transforms (Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  84. static void Conv_CompactBytesToTransforms(const TArray<uint8>& InCompactBytes, TArray<FTransform>& OutTransforms);
  85. /**
  86. * Compact Position bytes are [[x,y,z],...]
  87. */
  88. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Transforms (Location Bytes)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  89. static void Conv_CompactPositionBytesToTransforms(const TArray<uint8>& InCompactBytes, TArray<FTransform>& OutTransforms);
  90. /**
  91. * Sets and updates soundwave if needed from incoming bytes. Callable on background threads
  92. */
  93. UFUNCTION(BlueprintCallable, Category = "CoreUtility|Conversion")
  94. static void SetSoundWaveFromWavBytes(USoundWaveProcedural* InSoundWave, const TArray<uint8>& InBytes);
  95. /**
  96. * Fully Async texture conversion from bytes will auto-detect format, depends on TFuture, cannot be called in blueprint. Latent graph callback may be used
  97. */
  98. static TFuture<UTexture2D*> Conv_BytesToTexture_Async(const TArray<uint8>& InBytes);
  99. /**
  100. * Convert UTexture2D to bytes in given format - can have performance implication
  101. */
  102. UFUNCTION(BlueprintPure, meta = (DisplayName = "To Bytes (Texture2D)", BlueprintAutocast), Category = "CoreUtility|Conversion")
  103. static bool Conv_TextureToBytes(UTexture2D* Texture, TArray<uint8>& OutBuffer, EImageFormatBPType Format = EImageFormatBPType::PNG);
  104. /**
  105. * Current UTC time in string format
  106. */
  107. UFUNCTION(BlueprintPure, Category = "CoreUtility|Misc")
  108. static FString NowUTCString();
  109. /**
  110. * Returns a type of Unique Hardware ID
  111. */
  112. UFUNCTION(BlueprintPure, Category = "CoreUtility|Misc")
  113. static FString GetLoginId();
  114. /**
  115. * Return a somewhat unique int for given string
  116. */
  117. UFUNCTION(BlueprintPure, Category = "CoreUtility|Misc")
  118. static int32 ToHashCode(const FString& String);
  119. /**
  120. * Time inter-tick durations for simple
  121. */
  122. UFUNCTION(BlueprintCallable, Category = "CoreUtility|Misc")
  123. static void MeasureTimerStart(const FString& Category = TEXT("TimeTaken"));
  124. /**
  125. * Stops the timer started for this category and returns the duration taken in milliseconds
  126. */
  127. UFUNCTION(BlueprintCallable, Category = "CoreUtility|Misc")
  128. static float MeasureTimerStop(const FString& Category = TEXT("TimeTaken"), bool bShouldLogResult = true);
  129. /**
  130. * Calls function by name given calling context on thread specified. Use e.g. delay (0) to return to game thread
  131. * or use game thread callback for threadtype. This allows you to run certain functions on a background thread or
  132. * taskgraph in blueprints. Keep in mind that you should not create or destroy UObjects non-game threads.
  133. */
  134. UFUNCTION(BlueprintCallable, Category = "CoreUtility|Threading", meta = (WorldContext = "WorldContextObject"))
  135. static void CallFunctionOnThread(const FString& Function, ESIOCallbackType ThreadType, UObject* WorldContextObject = nullptr);
  136. /**
  137. * Calls specified function on thread type with a latent graph return. This allows you to run certain functions on a background thread or
  138. * taskgraph in blueprints. Keep in mind that you should not create or destroy UObjects non-game threads.
  139. */
  140. UFUNCTION(BlueprintCallable, Category = "CoreUtility|Threading", meta = (Latent, LatentInfo = "LatentInfo", WorldContext = "WorldContextObject"))
  141. static void CallFunctionOnThreadGraphReturn(const FString& Function, ESIOCallbackType ThreadType, struct FLatentActionInfo LatentInfo, UObject* WorldContextObject = nullptr);
  142. };