ExtContentBrowserBlueprintLibrary.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #pragma once
  3. #include "ExtContentBrowserBlueprintLibrary.h"
  4. #include "ExtContentBrowserSingleton.h"
  5. #include "ExtPackageUtils.h"
  6. #include "HAL/FileManager.h"
  7. #include "HAL/PlatformProcess.h"
  8. #include "Misc/Paths.h"
  9. #include "Misc/FileHelper.h"
  10. void UExtContentBrowserBlueprintLibrary::ImportUAssets(const TArray<FString>& AssetFilePaths, const FUAssetImportSetting& ImportSetting, bool bOneByOne)
  11. {
  12. FUAssetImportSetting MyImportSetting = ImportSetting;
  13. MyImportSetting.ResolveTargetContentDir(); // Target Content Dir need to be resolved for manual constructed Import Settings
  14. if (bOneByOne)
  15. {
  16. for (auto& AssetFilePath : AssetFilePaths)
  17. {
  18. FExtAssetImporter::ImportAssetsByFilePaths({ AssetFilePath }, MyImportSetting);
  19. }
  20. }
  21. else
  22. {
  23. FExtAssetImporter::ImportAssetsByFilePaths(AssetFilePaths, MyImportSetting);
  24. }
  25. }
  26. void UExtContentBrowserBlueprintLibrary::ImportUAsset(const FString& AssetFilePath, const FUAssetImportSetting& ImportSetting)
  27. {
  28. FUAssetImportSetting MyImportSetting = ImportSetting;
  29. MyImportSetting.ResolveTargetContentDir(); // Target Content Dir need to be resolved for manual constructed Import Settings
  30. FExtAssetImporter::ImportAssetsByFilePaths({ AssetFilePath }, MyImportSetting);
  31. }
  32. void UExtContentBrowserBlueprintLibrary::ImportFromFileList(const FString& FilePath, const FUAssetImportSetting& ImportSetting, bool bOneByOne)
  33. {
  34. struct Local
  35. {
  36. static void GetAssetFileList(const TArray<FString>& InRawFilePaths, TArray<FString>& OutAsstFlieList)
  37. {
  38. int32 Index = 0;
  39. for (const FString& RawPath : InRawFilePaths)
  40. {
  41. ECB_LOG(Display, TEXT(" %d - %s"), ++Index, *RawPath);
  42. FString Path(RawPath);
  43. Path.TrimStartAndEndInline();
  44. // Is File
  45. if (FPaths::FileExists(*RawPath))
  46. {
  47. ECB_LOG(Display, TEXT(" => File, extension: %s"), *FPaths::GetExtension(Path));
  48. FPaths::NormalizeFilename(Path);
  49. if (FExtAssetSupport::IsSupportedPackageFilename(Path))
  50. {
  51. OutAsstFlieList.AddUnique(Path);
  52. }
  53. }
  54. // Is Potential Dir
  55. else
  56. {
  57. FPaths::NormalizeDirectoryName(Path);
  58. bool bIsDir = false;
  59. bool bRecursive = false;
  60. const FString RecursiveDirPattern(TEXT("/**"));
  61. const FString DirPattern(TEXT("/**"));
  62. if (Path.EndsWith(RecursiveDirPattern))
  63. {
  64. Path.RemoveFromEnd(RecursiveDirPattern);
  65. bRecursive = true;
  66. }
  67. else if (Path.EndsWith(DirPattern))
  68. {
  69. Path.RemoveFromEnd(DirPattern);
  70. }
  71. if (FPaths::DirectoryExists(Path))
  72. {
  73. bIsDir = true;
  74. }
  75. if (bIsDir)
  76. {
  77. FPaths::NormalizeDirectoryName(Path);
  78. TArray<FString> FilesInDir;
  79. FExtPackageUtils::GetAllPackages(Path, FilesInDir, bRecursive);
  80. OutAsstFlieList.Append(FilesInDir);
  81. ECB_LOG(Display, TEXT(" => Folder, normalized: %s , %d files"), *Path, FilesInDir.Num());
  82. }
  83. }
  84. }
  85. }
  86. };
  87. if (FPaths::FileExists(*FilePath))
  88. {
  89. TArray<FString> RawFilePaths;
  90. FFileHelper::LoadFileToStringArray(RawFilePaths, *FilePath);
  91. ECB_LOG(Display, TEXT("[ImportFromFileList] %d lines"), RawFilePaths.Num());
  92. if (RawFilePaths.Num() <= 0)
  93. {
  94. return;
  95. }
  96. TArray<FString> AssetFilePaths;
  97. Local::GetAssetFileList(RawFilePaths, AssetFilePaths);
  98. if (AssetFilePaths.Num() <= 0)
  99. {
  100. return;
  101. }
  102. FUAssetImportSetting MyImportSetting = ImportSetting;
  103. MyImportSetting.ResolveTargetContentDir(); // Target Content Dir need to be resolved for manual constructed Import Settings
  104. ImportUAssets(AssetFilePaths, MyImportSetting, bOneByOne);
  105. }
  106. }
  107. void UExtContentBrowserBlueprintLibrary::GetGlobalImportSetting(FUAssetImportSetting& ImportSetting)
  108. {
  109. ImportSetting = FUAssetImportSetting::GetSavedImportSetting();
  110. }
  111. void UExtContentBrowserBlueprintLibrary::GetSelectedUAssetFilePaths(TArray<FString>& AssetFilePaths)
  112. {
  113. TArray<FExtAssetData> SelectedAssets;
  114. FExtContentBrowserSingleton::Get().GetSelectedAssets(SelectedAssets);
  115. AssetFilePaths.Empty(SelectedAssets.Num());
  116. for (const FExtAssetData& AssetData : SelectedAssets)
  117. {
  118. AssetFilePaths.Add(AssetData.PackageFilePath.ToString());
  119. }
  120. }
  121. #if ECB_TODO
  122. void UExtContentBrowserBlueprintLibrary::GetUAssetFilePathsInFolder(FName FolderPath, TArray<FString>& AssetFilePaths, bool bRecursive/* = false*/);
  123. {
  124. }
  125. #endif