SpineAtlasImportFactory.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "SpineAtlasImportFactory.h"
  30. #include "AssetToolsModule.h"
  31. #include "SpineAtlasAsset.h"
  32. #include "Editor.h"
  33. #define LOCTEXT_NAMESPACE "Spine"
  34. using namespace spine;
  35. USpineAtlasAssetFactory::USpineAtlasAssetFactory(const FObjectInitializer &objectInitializer) : Super(objectInitializer) {
  36. bCreateNew = false;
  37. bEditAfterNew = true;
  38. bEditorImport = true;
  39. SupportedClass = USpineAtlasAsset::StaticClass();
  40. Formats.Add(TEXT("atlas;Spine Atlas file"));
  41. }
  42. FText USpineAtlasAssetFactory::GetToolTip() const {
  43. return LOCTEXT("SpineAtlasAssetFactory", "Animations exported from Spine");
  44. }
  45. bool USpineAtlasAssetFactory::FactoryCanImport(const FString &Filename) {
  46. return true;
  47. }
  48. UObject *USpineAtlasAssetFactory::FactoryCreateFile(UClass *InClass, UObject *InParent, FName InName, EObjectFlags Flags, const FString &Filename, const TCHAR *Parms, FFeedbackContext *Warn, bool &bOutOperationCanceled) {
  49. FString FileExtension = FPaths::GetExtension(Filename);
  50. GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPreImport(this, InClass, InParent, InName, *FileExtension);
  51. FString rawString;
  52. if (!FFileHelper::LoadFileToString(rawString, *Filename)) {
  53. return nullptr;
  54. }
  55. FString currentSourcePath, filenameNoExtension, unusedExtension;
  56. const FString longPackagePath = FPackageName::GetLongPackagePath(InParent->GetOutermost()->GetPathName());
  57. FPaths::Split(UFactory::GetCurrentFilename(), currentSourcePath, filenameNoExtension, unusedExtension);
  58. USpineAtlasAsset *asset = NewObject<USpineAtlasAsset>(InParent, InClass, InName, Flags);
  59. asset->SetRawData(rawString);
  60. asset->SetAtlasFileName(FName(*Filename));
  61. LoadAtlas(asset, currentSourcePath, longPackagePath);
  62. GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetPostImport(this, asset);
  63. return asset;
  64. }
  65. bool USpineAtlasAssetFactory::CanReimport(UObject *Obj, TArray<FString> &OutFilenames) {
  66. USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj);
  67. if (!asset) return false;
  68. FString filename = asset->GetAtlasFileName().ToString();
  69. if (!filename.IsEmpty())
  70. OutFilenames.Add(filename);
  71. return true;
  72. }
  73. void USpineAtlasAssetFactory::SetReimportPaths(UObject *Obj, const TArray<FString> &NewReimportPaths) {
  74. USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj);
  75. if (asset && ensure(NewReimportPaths.Num() == 1))
  76. asset->SetAtlasFileName(FName(*NewReimportPaths[0]));
  77. }
  78. EReimportResult::Type USpineAtlasAssetFactory::Reimport(UObject *Obj) {
  79. USpineAtlasAsset *asset = Cast<USpineAtlasAsset>(Obj);
  80. FString rawString;
  81. if (!FFileHelper::LoadFileToString(rawString, *asset->GetAtlasFileName().ToString())) return EReimportResult::Failed;
  82. asset->SetRawData(rawString);
  83. FString currentSourcePath, filenameNoExtension, unusedExtension;
  84. const FString longPackagePath = FPackageName::GetLongPackagePath(asset->GetOutermost()->GetPathName());
  85. FString currentFileName = asset->GetAtlasFileName().ToString();
  86. FPaths::Split(currentFileName, currentSourcePath, filenameNoExtension, unusedExtension);
  87. LoadAtlas(asset, currentSourcePath, longPackagePath);
  88. if (Obj->GetOuter()) Obj->GetOuter()->MarkPackageDirty();
  89. else
  90. Obj->MarkPackageDirty();
  91. GEditor->GetEditorSubsystem<UImportSubsystem>()->BroadcastAssetReimport(asset);
  92. return EReimportResult::Succeeded;
  93. }
  94. UTexture2D *resolveTexture(USpineAtlasAsset *Asset, const FString &PageFileName, const FString &TargetSubPath) {
  95. FAssetToolsModule &AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
  96. TArray<FString> fileNames;
  97. fileNames.Add(PageFileName);
  98. TArray<UObject *> importedAsset = AssetToolsModule.Get().ImportAssets(fileNames, TargetSubPath);
  99. UTexture2D *texture = (importedAsset.Num() > 0) ? Cast<UTexture2D>(importedAsset[0]) : nullptr;
  100. return texture;
  101. }
  102. void USpineAtlasAssetFactory::LoadAtlas(USpineAtlasAsset *Asset, const FString &CurrentSourcePath, const FString &LongPackagePath) {
  103. Atlas *atlas = Asset->GetAtlas();
  104. Asset->atlasPages.Empty();
  105. const FString targetTexturePath = LongPackagePath / TEXT("Textures");
  106. Vector<AtlasPage *> &pages = atlas->getPages();
  107. for (size_t i = 0, n = pages.size(); i < n; i++) {
  108. AtlasPage *page = pages[i];
  109. const FString sourceTextureFilename = FPaths::Combine(*CurrentSourcePath, UTF8_TO_TCHAR(page->name.buffer()));
  110. UTexture2D *texture = resolveTexture(Asset, sourceTextureFilename, targetTexturePath);
  111. Asset->atlasPages.Add(texture);
  112. }
  113. }
  114. #undef LOCTEXT_NAMESPACE