CUFileComponent.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018-current Getnamo. All Rights Reserved
  2. #include "CUFileComponent.h"
  3. #include "HAL/PlatformFileManager.h"
  4. #include "Misc/FileHelper.h"
  5. #include "Misc/Paths.h"
  6. #if PLATFORM_ANDROID
  7. #include "Android/AndroidPlatformMisc.h"
  8. #endif
  9. UCUFileComponent::UCUFileComponent(const FObjectInitializer &init) : UActorComponent(init)
  10. {
  11. bWantsInitializeComponent = true;
  12. bAutoActivate = true;
  13. }
  14. FString UCUFileComponent::ProjectContentsDirectory()
  15. {
  16. return FPaths::ProjectContentDir();
  17. }
  18. FString UCUFileComponent::ProjectDirectory()
  19. {
  20. return FPaths::ProjectDir();
  21. }
  22. FString UCUFileComponent::ProjectSavedDirectory()
  23. {
  24. return FPaths::ProjectSavedDir();
  25. }
  26. FString UCUFileComponent::ExternalSaveDirectory()
  27. {
  28. #if PLATFORM_ANDROID
  29. return FString(FAndroidMisc::GamePersistentDownloadDir());
  30. #else
  31. return FPaths::ProjectSavedDir();
  32. #endif
  33. }
  34. void UCUFileComponent::SplitFullPath(const FString& InFullPath, FString& OutDirectory, FString& OutFileName)
  35. {
  36. bool bDidSplit = InFullPath.Split(TEXT("/"), &OutDirectory, &OutFileName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
  37. if (!bDidSplit)
  38. {
  39. //search by backslash
  40. InFullPath.Split(TEXT("\\\\"), &OutDirectory, &OutFileName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
  41. }
  42. }
  43. void UCUFileComponent::ProjectRelativePath(const FString& InFullPath, FString& OutProjectRelativePath)
  44. {
  45. const FString PathToProject = ProjectDirectory();
  46. FString Before, After;
  47. InFullPath.Split(PathToProject, &Before, &After);
  48. OutProjectRelativePath = After;
  49. }
  50. bool UCUFileComponent::SaveBytesToFile(const TArray<uint8>& Bytes, const FString& Directory, const FString& FileName, bool bLogSave)
  51. {
  52. //bool AllowOverwriting = false;
  53. IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
  54. if (PlatformFile.CreateDirectoryTree(*Directory))
  55. {
  56. FString AbsoluteFilePath;
  57. // Get absolute file path
  58. if (Directory.EndsWith(TEXT("/")))
  59. {
  60. AbsoluteFilePath = FPaths::ConvertRelativePathToFull(Directory + FileName);
  61. }
  62. else
  63. {
  64. AbsoluteFilePath = FPaths::ConvertRelativePathToFull(Directory + "/" + FileName);
  65. }
  66. // Allow overwriting or file doesn't already exist
  67. bool bSaveSuccesful = FFileHelper::SaveArrayToFile(Bytes, *AbsoluteFilePath);
  68. if (bLogSave)
  69. {
  70. if (bSaveSuccesful)
  71. {
  72. UE_LOG(LogTemp, Log, TEXT("Saved: %s with %d bytes"), *AbsoluteFilePath, Bytes.Num());
  73. }
  74. else
  75. {
  76. UE_LOG(LogTemp, Log, TEXT("Failed to save: %s"), *AbsoluteFilePath);
  77. }
  78. }
  79. return bSaveSuccesful;
  80. }
  81. return false;
  82. }
  83. bool UCUFileComponent::ReadBytesFromFile(const FString& Directory, const FString& FileName, TArray<uint8>& OutBytes)
  84. {
  85. // Get absolute file path
  86. FString AbsoluteFilePath = Directory + "/" + FileName;
  87. // Allow overwriting or file doesn't already exist
  88. return FFileHelper::LoadFileToArray(OutBytes, *AbsoluteFilePath);
  89. }