Skip to content

Commit c7ffff8

Browse files
authored
Merge pull request #267 from FreeZoneMods/xd_dev
CAI_Space improvement
2 parents 378e69f + 2af9583 commit c7ffff8

30 files changed

+337
-111
lines changed

src/Common/Common.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<ClInclude Include="..\xrCommon\xr_list.h" />
181181
<ClInclude Include="..\xrCommon\xr_map.h" />
182182
<ClInclude Include="..\xrCommon\xr_set.h" />
183+
<ClInclude Include="..\xrCommon\xr_smart_pointers.h" />
183184
<ClInclude Include="..\xrCommon\xr_stack.h" />
184185
<ClInclude Include="..\xrCommon\xr_string.h" />
185186
<ClInclude Include="..\xrCommon\xr_unordered_map.h" />

src/Common/Common.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
<ClInclude Include="..\xrCommon\xr_array.h">
8686
<Filter>xrCommon</Filter>
8787
</ClInclude>
88+
<ClInclude Include="..\xrCommon\xr_smart_pointers.h">
89+
<Filter>xrCommon</Filter>
90+
</ClInclude>
8891
</ItemGroup>
8992
<ItemGroup>
9093
<Filter Include="NvMender2003">

src/editors/ECore/stdafx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ typedef TMsgDlgBtn TMsgDlgButtons[mbHelp];
108108
#include "xrCore/xr_token.h"
109109
#include "xrCommon/xr_vector.h"
110110
#include "xrCommon/xr_string.h"
111+
#include "xrCommon/xr_smart_pointers.h"
111112
#include "xrCore/Animation/Bone.hpp"
112113
#include "xrCore/Animation/Motion.hpp"
113114

src/utils/xrSE_Factory/ai_space.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111
#include "xrScriptEngine/script_engine.hpp"
1212
#include "xrServerEntities/object_factory.h"
1313

14-
CAI_Space* g_ai_space = nullptr;
14+
static CAI_Space g_ai_space;
1515

16-
CAI_Space::CAI_Space() { m_script_engine = nullptr; }
16+
CAI_Space& CAI_Space::GetInstance()
17+
{
18+
auto& instance = g_ai_space;
19+
if (!instance.m_inited)
20+
{
21+
instance.init();
22+
}
23+
return instance;
24+
}
1725

1826
void CAI_Space::RegisterScriptClasses()
1927
{
@@ -49,13 +57,16 @@ void CAI_Space::RegisterScriptClasses()
4957

5058
void CAI_Space::init()
5159
{
52-
VERIFY(!m_script_engine);
60+
R_ASSERT(!m_inited);
61+
5362
VERIFY(!GEnv.ScriptEngine);
54-
GEnv.ScriptEngine = m_script_engine = new CScriptEngine(true);
63+
GEnv.ScriptEngine = new CScriptEngine(true);
5564
XRay::ScriptExporter::Reset(); // mark all nodes as undone
56-
m_script_engine->init(XRay::ScriptExporter::Export, true);
65+
GEnv.ScriptEngine->init(XRay::ScriptExporter::Export, true);
5766
RegisterScriptClasses();
5867
object_factory().register_script();
68+
69+
m_inited = true;
5970
}
6071

61-
CAI_Space::~CAI_Space() { xr_delete(m_script_engine); }
72+
CAI_Space::~CAI_Space() { xr_delete(GEnv.ScriptEngine); }

src/utils/xrSE_Factory/ai_space.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@
77
////////////////////////////////////////////////////////////////////////////
88

99
#pragma once
10+
#include "xrCore/Events/Notifier.h"
1011

1112
class CScriptEngine;
1213

1314
class CAI_Space
1415
{
1516
private:
16-
CScriptEngine* m_script_engine;
17+
bool m_inited = false;
1718

19+
void init();
1820
void RegisterScriptClasses();
1921

2022
public:
21-
CAI_Space();
23+
CAI_Space() = default;
24+
CAI_Space(const CAI_Space&) = delete;
25+
CAI_Space& operator=(const CAI_Space&) = delete;
2226
virtual ~CAI_Space();
23-
void init();
27+
static CAI_Space& GetInstance();
28+
2429
IC CScriptEngine& script_engine() const;
25-
};
2630

27-
extern CAI_Space* g_ai_space;
31+
enum EEventID
32+
{
33+
EVENT_SCRIPT_ENGINE_STARTED,
34+
EVENT_SCRIPT_ENGINE_RESET,
35+
EVENT_COUNT,
36+
};
37+
CEventNotifierCallback::CID Subscribe(CEventNotifierCallback* cb, EEventID event_id) { return 0; }
38+
bool Unsubscribe(CEventNotifierCallback::CID cid, EEventID event_id) { return true; }
39+
};
2840

2941
IC CAI_Space& ai();
3042

src/utils/xrSE_Factory/ai_space_inline.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@
1010

1111
IC CScriptEngine& CAI_Space::script_engine() const
1212
{
13-
VERIFY(m_script_engine);
14-
return *m_script_engine;
13+
VERIFY(GEnv.ScriptEngine);
14+
return *GEnv.ScriptEngine;
1515
}
1616

17-
IC CAI_Space& ai()
18-
{
19-
if (!g_ai_space)
20-
{
21-
g_ai_space = new CAI_Space();
22-
g_ai_space->init();
23-
}
24-
return *g_ai_space;
25-
}
17+
IC CAI_Space& ai() { return CAI_Space::GetInstance(); }

src/utils/xrSE_Factory/xrSE_Factory.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ BOOL APIENTRY DllMain(HANDLE module_handle, DWORD call_reason, LPVOID reserved)
6060
auto s = (CInifile**)&pSettings;
6161
xr_delete(*s);
6262
xr_delete(g_property_list_helper);
63-
xr_delete(g_ai_space);
6463
xr_delete(g_object_factory);
6564
if (prop_helper_module)
6665
prop_helper_module = nullptr;

src/xrCommon/xr_smart_pointers.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
template <typename T>
6+
using xr_unique_ptr = std::unique_ptr<T>;
7+
8+
template <typename T>
9+
using xr_shared_ptr = std::shared_ptr<T>;
10+
11+
template <class T, class... Args>
12+
inline xr_unique_ptr<T> xr_make_unique(Args&&... args)
13+
{
14+
return std::make_unique<T>(args...);
15+
}
16+
17+
template <class T, class... Args>
18+
inline xr_shared_ptr<T> xr_make_shared(Args&&... args)
19+
{
20+
return std::make_shared<T>(args...);
21+
}

src/xrCore/Events/Notifier.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
3+
#include "xrCommon/xr_smart_pointers.h"
4+
#include "xrCommon/xr_vector.h"
5+
#include "xrCommon/xr_array.h"
6+
#include "xrCore/Threading/Lock.hpp"
7+
#include "xrCore/Threading/ScopeLock.hpp"
8+
9+
class CEventNotifierCallback
10+
{
11+
public:
12+
using CID = size_t;
13+
static const CID INVALID_CID = std::numeric_limits<CID>::max();
14+
15+
virtual void ProcessEvent() = 0;
16+
virtual ~CEventNotifierCallback(){};
17+
};
18+
19+
template <unsigned int CNT>
20+
class CEventNotifier
21+
{
22+
private:
23+
class CCallbackStorage
24+
{
25+
class CCallbackWrapper
26+
{
27+
public:
28+
xr_unique_ptr<CEventNotifierCallback> callback;
29+
bool destroying = false;
30+
bool executing = false;
31+
32+
CCallbackWrapper(CEventNotifierCallback* cb) : callback(cb){};
33+
bool operator==(const CEventNotifierCallback* cb) { return cb == callback.get(); }
34+
void Reset()
35+
{
36+
callback.reset(nullptr);
37+
destroying = false;
38+
executing = false;
39+
}
40+
};
41+
xr_vector<CCallbackWrapper> m_callbacks;
42+
Lock m_lock;
43+
44+
public:
45+
CEventNotifierCallback::CID RegisterCallback(CEventNotifierCallback* cb)
46+
{
47+
ScopeLock lock(&m_lock);
48+
auto it = std::find(m_callbacks.begin(), m_callbacks.end(), nullptr);
49+
return (it == m_callbacks.end()) ? (m_callbacks.emplace_back(cb), m_callbacks.size() - 1) :
50+
(it->callback.reset(cb), std::distance(m_callbacks.begin(), it));
51+
}
52+
53+
bool UnregisterCallback(CEventNotifierCallback::CID cid)
54+
{
55+
bool result = false;
56+
ScopeLock lock(&m_lock);
57+
if (cid < m_callbacks.size() && m_callbacks[cid].callback != nullptr)
58+
{
59+
if (!m_callbacks[cid].destroying)
60+
{
61+
result = true;
62+
m_callbacks[cid].destroying = true;
63+
}
64+
65+
if (!m_callbacks[cid].executing)
66+
{
67+
m_callbacks[cid].Reset();
68+
}
69+
}
70+
return result;
71+
}
72+
73+
void ExecuteCallbacks()
74+
{
75+
ScopeLock lock(&m_lock);
76+
for (CEventNotifierCallback::CID i = 0; i < m_callbacks.size(); ++i)
77+
{
78+
auto& cb = m_callbacks[i];
79+
if (cb.callback != nullptr && !cb.destroying)
80+
{
81+
cb.executing = true;
82+
cb.callback->ProcessEvent();
83+
cb.executing = false;
84+
85+
if (cb.destroying)
86+
{
87+
UnregisterCallback(i);
88+
}
89+
}
90+
}
91+
}
92+
};
93+
94+
xr_array<CCallbackStorage, CNT> m_callbacks;
95+
96+
public:
97+
CEventNotifierCallback::CID RegisterCallback(CEventNotifierCallback* cb, unsigned int event_id)
98+
{
99+
R_ASSERT(event_id < CNT);
100+
return m_callbacks[event_id].RegisterCallback(cb);
101+
}
102+
103+
bool UnregisterCallback(CEventNotifierCallback::CID cid, unsigned int event_id)
104+
{
105+
R_ASSERT(event_id < CNT);
106+
return m_callbacks[event_id].UnregisterCallback(cid);
107+
}
108+
109+
void FireEvent(unsigned int event_id)
110+
{
111+
R_ASSERT(event_id < CNT);
112+
m_callbacks[event_id].ExecuteCallbacks();
113+
}
114+
};

src/xrCore/FileSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "vfw.h"
1212
#endif
1313

14-
std::unique_ptr<EFS_Utils> xr_EFS;
14+
xr_unique_ptr<EFS_Utils> xr_EFS;
1515
//----------------------------------------------------
1616
EFS_Utils::EFS_Utils() {}
1717
EFS_Utils::~EFS_Utils() {}

0 commit comments

Comments
 (0)