1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Reflection/BsIReflectable.h"
7#include "CoreThread/BsCoreObject.h"
8
9namespace bs
10{
11 /** @addtogroup Resources
12 * @{
13 */
14
15 /** Base class for all resources. */
16 class BS_CORE_EXPORT Resource : public IReflectable, public CoreObject
17 {
18 public:
19 Resource(bool requiresGpuInitialization = true);
20 virtual ~Resource() = default;
21
22 /** Returns the name of the resource. */
23 const String& getName() const;
24
25 /** Sets the name of the resource. */
26 void setName(const String& name);
27
28 /** Retrieves meta-data containing various information describing a resource. */
29 SPtr<ResourceMetaData> getMetaData() const { return mMetaData; }
30
31 /** Returns whether or not this resource is allowed to be asynchronously loaded. */
32 virtual bool allowAsyncLoading() const { return true; }
33
34 protected:
35 friend class Resources;
36 friend class ResourceHandleBase;
37
38 /**
39 * Retrieves a list of all resources that this resource depends on.
40 *
41 * @note Thread safe.
42 */
43 void getResourceDependencies(FrameVector<HResource>& dependencies) const;
44
45 /** Checks if all the resources this object is dependent on are fully loaded. */
46 bool areDependenciesLoaded() const;
47
48 /**
49 * Registers a new resource that this resource is dependent on.
50 *
51 * @note Thread safe.
52 */
53 void addResourceDependency(const HResource& resource);
54
55 /**
56 * Unregisters a previously registered dependency.
57 *
58 * @note Thread safe.
59 */
60 void removeResourceDependency(const HResource& resource);
61
62 /**
63 * Returns true if the resource can be compressed using a generic compression when saved on a storage device.
64 * Certain resources already have their contents compressed (like audio files) and will not benefit from further
65 * compression. Resources supporting streaming should never be compressed, instead such resources can handle
66 * compression/decompression locally through their streams.
67 */
68 virtual bool isCompressible() const { return true; }
69
70 UINT32 mSize;
71 SPtr<ResourceMetaData> mMetaData;
72
73 /**
74 * Signal to the resource implementation if original data should be kept in memory. This is sometimes needed if
75 * the resource destroys original data during normal usage, but it might still be required for special purposes
76 * (like saving in the editor).
77 */
78 bool mKeepSourceData;
79
80 /** A list of all other resources this resource depends on. */
81 Vector<WeakResourceHandle<Resource>> mDependencies;
82
83 /** Mutex ensuring dependencies list updates and queries are thread safe. */
84 mutable Mutex mDependenciesMutex;
85
86 /************************************************************************/
87 /* SERIALIZATION */
88 /************************************************************************/
89 public:
90 friend class ResourceRTTI;
91 static RTTITypeBase* getRTTIStatic();
92 RTTITypeBase* getRTTI() const override;
93 };
94
95 /** @} */
96}