Skip to content

Commit 675216e

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

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-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()} -> std::convertible_to<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: 35 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,29 +94,35 @@ 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
{
120128
return;
@@ -127,6 +135,11 @@ void CScriptStoryIDManager::Register(ALife::_OBJECT_ID obj_id, shared_str script
127135
void CScriptStoryIDManager::Unregister(ALife::_OBJECT_ID obj_id)
128136
{
129137
xrSRWLockGuard guard(m_containers_lock);
138+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
139+
auto Finally = xr_scope_exit([this]()
140+
{
141+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
142+
});
130143
if (m_containers_by_id.contains(obj_id)){
131144
auto elem = *m_containers_by_id.find(obj_id);
132145
m_containers_by_id.erase(elem.first);
@@ -137,6 +150,11 @@ void CScriptStoryIDManager::Unregister(ALife::_OBJECT_ID obj_id)
137150
void CScriptStoryIDManager::Unregister(LPCSTR script_story_id)
138151
{
139152
xrSRWLockGuard guard(m_containers_lock);
153+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
154+
auto Finally = xr_scope_exit([this]()
155+
{
156+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
157+
});
140158
if (m_containers_by_script_story_id.contains(script_story_id)){
141159
auto elem = *m_containers_by_script_story_id.find(script_story_id);
142160
m_containers_by_id.erase(elem.second);
@@ -147,13 +165,15 @@ void CScriptStoryIDManager::Unregister(LPCSTR script_story_id)
147165
ALife::_OBJECT_ID CScriptStoryIDManager::GetID(LPCSTR script_story_id) const
148166
{
149167
xrSRWLockGuard guard(m_containers_lock, true);
168+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
150169
auto it = m_containers_by_script_story_id.find(script_story_id);
151170
return it != m_containers_by_script_story_id.end() ? it->second : ALife::_OBJECT_ID(-1);
152171
}
153172

154173
LPCSTR CScriptStoryIDManager::GetID(ALife::_OBJECT_ID obj_id) const
155174
{
156175
xrSRWLockGuard guard(m_containers_lock, true);
176+
VERIFY(m_containers_by_id.size() == m_containers_by_script_story_id.size());
157177
auto it = m_containers_by_id.find(obj_id);
158178
return it != m_containers_by_id.end() ? it->second.c_str() : nullptr;
159179
}

0 commit comments

Comments
 (0)