Asset.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Atomic/Core/Object.h>
  24. #include <Atomic/Resource/Resource.h>
  25. #include <Atomic/Scene/Node.h>
  26. #include "AssetImporter.h"
  27. using namespace Atomic;
  28. namespace ToolCore
  29. {
  30. #define ASSET_VERSION 1
  31. class Asset : public Object
  32. {
  33. friend class AssetDatabase;
  34. friend class AssetImporter;
  35. ATOMIC_OBJECT(Asset, Object);
  36. public:
  37. /// Construct.
  38. Asset(Context* context);
  39. virtual ~Asset();
  40. bool Import();
  41. bool Preload();
  42. // the .fbx, .png, etc path, attempts to load .asset, creates missing .asset
  43. bool SetPath(const String& path);
  44. const String& GetGUID() const { return guid_; }
  45. const String& GetName() const { return name_; }
  46. // Get absolute path to asset file
  47. const String& GetPath() const { return path_; }
  48. String GetExtension() const;
  49. /// Get the path relative to project
  50. String GetRelativePath();
  51. String GetCachePath() const;
  52. Resource* GetResource(const String& typeName = String::EMPTY);
  53. const StringHash GetImporterType() { return importer_.Null() ? String::EMPTY : importer_->GetType(); }
  54. const String& GetImporterTypeName() { return importer_.Null() ? String::EMPTY : importer_->GetTypeName(); }
  55. AssetImporter* GetImporter() { return importer_; }
  56. void PostImportError(const String& message);
  57. Asset* GetParent();
  58. void SetDirty(bool dirty) { dirty_ = dirty; }
  59. bool IsDirty() const { return dirty_; }
  60. /// Get the last timestamp as seen by the AssetDatabase
  61. unsigned GetFileTimestamp() { return fileTimestamp_; }
  62. /// Sets the time stamp to the asset files current time
  63. void UpdateFileTimestamp();
  64. // get the .asset filename
  65. String GetDotAssetFilename();
  66. /// Rename the asset, which depending on the asset type may be nontrivial
  67. bool Rename(const String& newName);
  68. /// Move the asset, which depending on the asset type may be nontrivial
  69. bool Move(const String& newPath);
  70. bool IsFolder() const { return isFolder_; }
  71. // load .asset
  72. bool Load();
  73. // save .asset
  74. bool Save();
  75. /// Instantiate a node from the asset
  76. Node* InstantiateNode(Node* parent, const String& name);
  77. // Get a mapping of the assets path to cache file representations, by type
  78. void GetAssetCacheMap(HashMap<String, String>& assetMap) { if (importer_.NotNull()) importer_->GetAssetCacheMap(assetMap); }
  79. private:
  80. bool CreateImporter();
  81. bool CheckCacheFile();
  82. String guid_;
  83. // can change
  84. String path_;
  85. String name_;
  86. bool dirty_;
  87. bool isFolder_;
  88. // the current timestamp of the asset as seen by the asset database
  89. // used to catch when the asset needs to be marked dirty when notified
  90. // that the file has changed (the resource system will send a changed file
  91. // event when the resource is first added)
  92. unsigned fileTimestamp_;
  93. SharedPtr<JSONFile> json_;
  94. SharedPtr<AssetImporter> importer_;
  95. };
  96. }