| 166 | #endif |
| 167 | |
| 168 | EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode) |
| 169 | { |
| 170 | EventHook *pHook; |
| 171 | |
| 172 | /* If we aren't listening to this event... */ |
| 173 | if (!gameevents->FindListener(this, name)) |
| 174 | { |
| 175 | /* Then add ourselves */ |
| 176 | if (!gameevents->AddListener(this, name, true)) |
| 177 | { |
| 178 | /* If event doesn't exist... */ |
| 179 | return EventHookErr_InvalidEvent; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* If a hook structure does not exist... */ |
| 184 | if (!m_EventHooks.retrieve(name, &pHook)) |
| 185 | { |
| 186 | EventHookList *pHookList; |
| 187 | IPlugin *plugin = scripts->FindPluginByContext(pFunction->GetParentContext()); |
| 188 | |
| 189 | /* Check plugin for an existing EventHook list */ |
| 190 | if (!plugin->GetProperty("EventHooks", (void **)&pHookList)) |
| 191 | { |
| 192 | pHookList = new EventHookList(); |
| 193 | plugin->SetProperty("EventHooks", pHookList); |
| 194 | } |
| 195 | |
| 196 | /* Create new GameEventHook structure */ |
| 197 | pHook = new EventHook(); |
| 198 | |
| 199 | if (mode == EventHookMode_Pre) |
| 200 | { |
| 201 | /* Create forward for a pre hook */ |
| 202 | pHook->pPreHook = forwardsys->CreateForwardEx(NULL, ET_Hook, 3, GAMEEVENT_PARAMS); |
| 203 | /* Add to forward list */ |
| 204 | pHook->pPreHook->AddFunction(pFunction); |
| 205 | } else { |
| 206 | /* Create forward for a post hook */ |
| 207 | pHook->pPostHook = forwardsys->CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS); |
| 208 | /* Should we copy data from a pre hook to the post hook? */ |
| 209 | pHook->postCopy = (mode == EventHookMode_Post); |
| 210 | /* Add to forward list */ |
| 211 | pHook->pPostHook->AddFunction(pFunction); |
| 212 | } |
| 213 | |
| 214 | /* Cache the name for post hooks */ |
| 215 | pHook->name = name; |
| 216 | |
| 217 | /* Increase reference count */ |
| 218 | pHook->refCount++; |
| 219 | |
| 220 | /* Add hook structure to hook lists */ |
| 221 | pHookList->push_back(pHook); |
| 222 | m_EventHooks.insert(name, pHook); |
| 223 | |
| 224 | return EventHookErr_Okay; |
| 225 | } |
no test coverage detected