Skip to content

Commit 3dab6b4

Browse files
More info about script story ID collisions-> "Move engine script story ID under EngineExternal (default is "off")"
1 parent 3b4225f commit 3dab6b4

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

src/xrCore/_stl_extensions.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ using xr_hash_map = std::unordered_map<K, V, std::hash<K>, _Traits, allocator>;
114114
template <typename K, typename H = std::hash<K>, class _Traits = std::equal_to<K>, typename allocator = xalloc<K> >
115115
using xr_hash_set = std::unordered_set<K, H, _Traits, allocator>;
116116

117+
#if __cplusplus > 202002L
118+
#include <scope>
119+
using xr_scope_exit = std::scope_exit;
120+
#else
121+
template<typename T>
122+
requires requires(T a) { {a()} -> void; }
123+
struct xr_scope_exit
124+
{
125+
T Func;
126+
bool IsActive = true;
127+
xr_scope_exit(T &&t) : Func(t) {}
128+
~xr_scope_exit(){ if (IsActive) { Func(); }}
129+
void release() { IsActive = false; }
130+
131+
xr_scope_exit(const xr_scope_exit&) = delete;
132+
xr_scope_exit& operator=(const xr_scope_exit&) = delete;
133+
xr_scope_exit(xr_scope_exit&&) noexcept = default;
134+
xr_scope_exit& operator=(xr_scope_exit&&) noexcept = default;
135+
};
136+
#endif
137+
117138
struct pred_str {
118139
IC bool operator()(const char* x, const char* y) const { return xr_strcmp(x,y)<0; }
119140
};

src/xrGame/ScriptsSubsystems/StoryID/StoryIDManager.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ void CScriptStoryIDManager::VerifiedRegisterObject(CSE_Abstract* se_obj)
3030
R_ASSERT3(false, "There is no 'story_id' field in [story_object] section :object", se_obj->name());
3131
}
3232
if (value)
33+
{
3334
self.Register(se_obj->ID, value);
35+
}
3436
return;
3537
}
3638
auto story_id = READ_IF_EXISTS(pSettings, r_string, se_obj->name(), "story_id", nullptr);
@@ -92,31 +94,38 @@ void CScriptStoryIDManager::script_register(lua_State* L)
9294
void CScriptStoryIDManager::Register(ALife::_OBJECT_ID obj_id, shared_str script_story_id)
9395
{
9496
xrSRWLockGuard guard(m_containers_lock);
97+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
98+
auto Finally = xr_scope_exit([this]()
99+
{
100+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
101+
});
95102
auto ByIDIt = m_containers_by_id.find(obj_id);
96103
auto ByScriptStoryIDIt = m_containers_by_script_story_id.find(script_story_id);
97104
if (ByScriptStoryIDIt != m_containers_by_script_story_id.end() && ByScriptStoryIDIt->second != obj_id)
98105
{
99-
xr_string message = "You are trying to spawn two or more objects with the same story_id:[";
100-
message.append(script_story_id.c_str());
101-
message.append("] --> [");
102-
auto ExistName = ai().alife().objects().object(ByScriptStoryIDIt->second)->name();
103-
message.append(ExistName);
104-
message.append("] try to add:[");
105-
auto NewName = ai().alife().objects().object(obj_id)->name();
106-
message.append(NewName);
107-
message.append("]");
108-
R_ASSERT2(ByScriptStoryIDIt == m_containers_by_script_story_id.end(), message.c_str());
106+
auto ObjExist = ai().alife().objects().object(ByScriptStoryIDIt->second);
107+
auto ObjNew = ai().alife().objects().object(obj_id);
108+
I_ASSERT_M(false,
109+
"You are trying to spawn two or more objects with the same story_id [%s]: Old Obj [%d][%s], New obj [%d][%s]",
110+
script_story_id.c_str(),
111+
ObjExist->ID,
112+
ObjExist->name(),
113+
std::to_string(ObjNew->ID),
114+
ObjNew->name()
115+
);
109116
}
110117
if (ByIDIt != m_containers_by_id.end()){
111118
VERIFY(ByScriptStoryIDIt != m_containers_by_script_story_id.end());
112119
if(ByScriptStoryIDIt->first != script_story_id){
113-
xr_string message = "Object [";
114-
message.append(script_story_id.c_str());
115-
message.append("] is already in story_objects_registry with story_id[");
116-
message.append(ByIDIt->second.c_str());
117-
R_ASSERT2(ByScriptStoryIDIt != m_containers_by_script_story_id.end(), message.c_str());
120+
I_ASSERT_M(false,
121+
"Object [%d][%s] is already in story_objects_registry with story_id [%s]",
122+
ByIDIt->first,
123+
script_story_id.c_str(),
124+
ByIDIt->second.c_str()
125+
);
118126
}else
119127
{
128+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
120129
return;
121130
}
122131
}
@@ -127,6 +136,11 @@ void CScriptStoryIDManager::Register(ALife::_OBJECT_ID obj_id, shared_str script
127136
void CScriptStoryIDManager::Unregister(ALife::_OBJECT_ID obj_id)
128137
{
129138
xrSRWLockGuard guard(m_containers_lock);
139+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
140+
auto Finally = xr_scope_exit([this]()
141+
{
142+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
143+
});
130144
if (m_containers_by_id.contains(obj_id)){
131145
auto elem = *m_containers_by_id.find(obj_id);
132146
m_containers_by_id.erase(elem.first);
@@ -137,6 +151,11 @@ void CScriptStoryIDManager::Unregister(ALife::_OBJECT_ID obj_id)
137151
void CScriptStoryIDManager::Unregister(LPCSTR script_story_id)
138152
{
139153
xrSRWLockGuard guard(m_containers_lock);
154+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
155+
auto Finally = xr_scope_exit([this]()
156+
{
157+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
158+
});
140159
if (m_containers_by_script_story_id.contains(script_story_id)){
141160
auto elem = *m_containers_by_script_story_id.find(script_story_id);
142161
m_containers_by_id.erase(elem.second);
@@ -147,13 +166,15 @@ void CScriptStoryIDManager::Unregister(LPCSTR script_story_id)
147166
ALife::_OBJECT_ID CScriptStoryIDManager::GetID(LPCSTR script_story_id) const
148167
{
149168
xrSRWLockGuard guard(m_containers_lock, true);
169+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
150170
auto it = m_containers_by_script_story_id.find(script_story_id);
151171
return it != m_containers_by_script_story_id.end() ? it->second : ALife::_OBJECT_ID(-1);
152172
}
153173

154174
LPCSTR CScriptStoryIDManager::GetID(ALife::_OBJECT_ID obj_id) const
155175
{
156176
xrSRWLockGuard guard(m_containers_lock, true);
177+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
157178
auto it = m_containers_by_id.find(obj_id);
158179
return it != m_containers_by_id.end() ? it->second.c_str() : nullptr;
159180
}

0 commit comments

Comments
 (0)