1616//
1717// =============================================================================
1818
19+ #include " game/scripteventtable.h"
1920#include " util/error.h"
2021#include " util/stream.h"
2122#include " util/string.h"
@@ -33,5 +34,59 @@ inline bool ReadAndAssertCount(Stream *in, const char *objname, uint32_t expecte
3334 return !err.HasError ();
3435}
3536
37+ // Reads ScriptEventSchema and ScriptEventsTables for an object list.
38+ // The object list is assumed to be already precreated.
39+ // NOTE: if there will be an issue with the Events member (diff name, protected),
40+ // then we would require a method that sets handlers into object's event table.
41+ // NOTE: made this a template function, because majority of objects in the engine
42+ // do not have a shared parent class (also we work with a vector of them here...).
43+ // Revise this later?
44+ template <typename TObj, typename TObjIter>
45+ HError ReadScriptEventsTablesForObjects (TObjIter begin, TObjIter end, const char *objname, Stream *in)
46+ {
47+ // TODO: figure out a more optimal way for handling all the operations here,
48+ // perhaps join schema read and remap into the member of the ScriptEventSchema class?
49+ // join Handlers read and remap? anything else that may be improved?
50+ ScriptEventSchema schema;
51+ HError err = schema.Read (in);
52+ if (!err)
53+ return err;
54+ std::vector<uint32_t > remap;
55+ const bool must_remap = schema.CreateRemap (TObj::GetEventSchema (), remap);
56+
57+ size_t obj_count = end - begin;
58+ if (!ReadAndAssertCount (in, objname, static_cast <uint32_t >(obj_count), err))
59+ return err;
60+
61+ ScriptEventHandlers handlers;
62+ for (size_t i = 0 ; i < obj_count; ++i)
63+ {
64+ err = handlers.Read (in);
65+ if (!err)
66+ return err;
67+ if (must_remap)
68+ handlers.Remap (remap);
69+ (*(begin + i)).GetEvents ().SetHandlers (handlers);
70+ }
71+ return HError::None ();
72+ }
73+
74+ // Reads ScriptEventSchema and ScriptEventsTables for an object list.
75+ // This is a variant for the objects stored in a std::vector
76+ template <typename TObj>
77+ HError ReadScriptEventsTablesForObjects (std::vector<TObj> &objs, const char *objname, Stream *in)
78+ {
79+ return ReadScriptEventsTablesForObjects<TObj, typename std::vector<TObj>::iterator>(objs.begin (), objs.end (), objname, in);
80+ }
81+
82+ // Reads ScriptEventSchema and ScriptEventsTables for an object list.
83+ // This is a variant for the objects stored in a C-style array.
84+ // TODO: store these in a vector too at some point, and remove this variant.
85+ template <typename TObj, size_t ObjListSize>
86+ HError ReadScriptEventsTablesForObjects (TObj (&objs)[ObjListSize], size_t obj_count, const char *objname, Stream *in)
87+ {
88+ return ReadScriptEventsTablesForObjects<TObj, TObj*>(objs, objs + obj_count, objname, in);
89+ }
90+
3691} // namespace Common
3792} // namespace AGS
0 commit comments