ezEngine  Milestone 7
Resource.h
1 #pragma once
2 
3 #include <Core/Basics.h>
4 #include <Foundation/Strings/HashedString.h>
5 #include <Foundation/IO/Stream.h>
7 #include <Foundation/Time/Time.h>
8 #include <Foundation/Time/Timestamp.h>
9 #include <Core/ResourceManager/Implementation/Declarations.h>
10 #include <Core/ResourceManager/ResourceHandle.h>
11 #include <Core/ResourceManager/ResourceManager.h>
12 
18 template<typename SELF, typename SELF_DESCRIPTOR>
19 class ezResource : public ezResourceBase
20 {
21 public:
22  typedef SELF_DESCRIPTOR DescriptorType;
23 
28  {
29  m_hFallback = hResource;
30  m_Flags.AddOrRemove(ezResourceFlags::ResourceHasFallback, m_hFallback.IsValid());
31  }
32 
33  static void SetTypeFallbackResource(const ezResourceHandle<SELF>& hResource)
34  {
35  if (!s_bAddedManagerEventHandler && !s_TypeFallbackResource.IsValid() && !s_TypeMissingResource.IsValid() && hResource.IsValid())
36  {
37  s_bAddedManagerEventHandler = true;
38  ezResourceManager::s_ManagerEvents.AddEventHandler(ManagerEventHandler);
39  }
40 
41  s_TypeFallbackResource = hResource;
42  }
43 
44  static void SetTypeMissingResource(const ezResourceHandle<SELF>& hResource)
45  {
46  if (!s_bAddedManagerEventHandler && !s_TypeFallbackResource.IsValid() && !s_TypeMissingResource.IsValid() && hResource.IsValid())
47  {
48  s_bAddedManagerEventHandler = true;
49  ezResourceManager::s_ManagerEvents.AddEventHandler(ManagerEventHandler);
50  }
51 
52  s_TypeMissingResource = hResource;
53  }
54 
55  static const ezResourceHandle<SELF>& GetTypeFallbackResource() { return s_TypeFallbackResource; }
56  static const ezResourceHandle<SELF>& GetTypeMissingResource() { return s_TypeMissingResource; }
57 
58 protected:
59 
60  friend class ezResourceManager;
61 
62  ezResource(DoUpdate ResourceUpdateThread, ezUInt8 uiQualityLevelsLoadable) : ezResourceBase(ResourceUpdateThread, uiQualityLevelsLoadable)
63  {
64  }
65 
66  ~ezResource() { }
67 
68 private:
69  static void ManagerEventHandler(const ezResourceManager::ManagerEvent& e)
70  {
71  if (e.m_EventType == ezResourceManager::ManagerEventType::ManagerShuttingDown)
72  {
73  s_TypeFallbackResource.Invalidate();
74  s_TypeMissingResource.Invalidate();
75  }
76  }
77 
78  static bool s_bAddedManagerEventHandler;
79  static ezResourceHandle<SELF> s_TypeFallbackResource;
80  static ezResourceHandle<SELF> s_TypeMissingResource;
81 
82  void CallCreateResource(const SELF_DESCRIPTOR& descriptor)
83  {
84  ezResourceLoadDesc ld = CreateResource(descriptor);
85 
86  EZ_ASSERT_DEV(ld.m_State != ezResourceState::Invalid, "CreateResource() did not return a valid resource load state");
87  EZ_ASSERT_DEV(ld.m_uiQualityLevelsDiscardable != 0xFF, "CreateResource() did not fill out m_uiQualityLevelsDiscardable correctly");
88  EZ_ASSERT_DEV(ld.m_uiQualityLevelsLoadable != 0xFF, "CreateResource() did not fill out m_uiQualityLevelsLoadable correctly");
89 
90  m_LoadingState = ld.m_State;
91  m_uiQualityLevelsDiscardable = ld.m_uiQualityLevelsDiscardable;
92  m_uiQualityLevelsLoadable = ld.m_uiQualityLevelsLoadable;
93 
94  // Update Memory Usage
95  {
97  MemUsage.m_uiMemoryCPU = 0xFFFFFFFF;
98  MemUsage.m_uiMemoryGPU = 0xFFFFFFFF;
99  UpdateMemoryUsage(MemUsage);
100 
101  EZ_ASSERT_DEV(MemUsage.m_uiMemoryCPU != 0xFFFFFFFF, "Resource '%s' did not properly update its CPU memory usage", GetResourceID().GetData());
102  EZ_ASSERT_DEV(MemUsage.m_uiMemoryGPU != 0xFFFFFFFF, "Resource '%s' did not properly update its GPU memory usage", GetResourceID().GetData());
103 
104  m_MemoryUsage = MemUsage;
105  }
106 
108  e.m_pResource = this;
109  e.m_EventType = ezResourceManager::ResourceEventType::ResourceContentUpdated;
110  ezResourceManager::BroadcastResourceEvent(e);
111  }
112 
125  virtual ezResourceLoadDesc CreateResource(const SELF_DESCRIPTOR& descriptor)
126  {
127  EZ_REPORT_FAILURE("The resource type '%s' does not support resource creation", GetDynamicRTTI()->GetTypeName());
128 
129  return ezResourceLoadDesc();
130  }
131 
132  ezResourceHandle<SELF> m_hFallback;
133 };
134 
135 template<typename SELF, typename SELF_DESCRIPTOR>
137 
138 template<typename SELF, typename SELF_DESCRIPTOR>
140 
141 template<typename SELF, typename SELF_DESCRIPTOR>
143 
144