|
|
@@ -166,52 +166,80 @@ bool ScriptFile::Load(Deserializer& source)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-void ScriptFile::AddEventHandler(StringHash eventType, const String& handlerName)
|
|
|
-{
|
|
|
- if (!compiled_)
|
|
|
- return;
|
|
|
-
|
|
|
- String declaration = "void " + handlerName + "(StringHash, VariantMap&)";
|
|
|
- asIScriptFunction* function = GetFunction(declaration);
|
|
|
- if (!function)
|
|
|
- {
|
|
|
- declaration = "void " + handlerName + "()";
|
|
|
- function = GetFunction(declaration);
|
|
|
- if (!function)
|
|
|
- {
|
|
|
- LOGERROR("Event handler function " + handlerName + " not found in " + GetName());
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- SubscribeToEvent(eventType, HANDLER_USERDATA(ScriptFile, HandleScriptEvent, (void*)function));
|
|
|
-}
|
|
|
-
|
|
|
-void ScriptFile::AddEventHandler(Object* sender, StringHash eventType, const String& handlerName)
|
|
|
-{
|
|
|
- if (!compiled_)
|
|
|
- return;
|
|
|
-
|
|
|
- if (!sender)
|
|
|
- {
|
|
|
- LOGERROR("Null event sender for event " + String(eventType) + ", handler " + handlerName);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String declaration = "void " + handlerName + "(StringHash, VariantMap&)";
|
|
|
- asIScriptFunction* function = GetFunction(declaration);
|
|
|
- if (!function)
|
|
|
- {
|
|
|
- declaration = "void " + handlerName + "()";
|
|
|
- function = GetFunction(declaration);
|
|
|
- if (!function)
|
|
|
- {
|
|
|
- LOGERROR("Event handler function " + handlerName + " not found in " + GetName());
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- SubscribeToEvent(sender, eventType, HANDLER_USERDATA(ScriptFile, HandleScriptEvent, (void*)function));
|
|
|
+void ScriptFile::AddEventHandler(StringHash eventType, const String& handlerName)
|
|
|
+{
|
|
|
+ if (!compiled_)
|
|
|
+ return;
|
|
|
+
|
|
|
+ String declaration = "void " + handlerName + "(StringHash, VariantMap&)";
|
|
|
+ asIScriptFunction* function = 0;
|
|
|
+ asIScriptObject* reciever = static_cast<asIScriptObject*>(asGetActiveContext()->GetThisPointer());
|
|
|
+
|
|
|
+ if (reciever)
|
|
|
+ function = GetMethod(reciever, declaration);
|
|
|
+ else
|
|
|
+ function = GetFunction(declaration);
|
|
|
+
|
|
|
+ if (!function)
|
|
|
+ {
|
|
|
+ declaration = "void " + handlerName + "()";
|
|
|
+
|
|
|
+ if (reciever)
|
|
|
+ function = GetMethod(reciever, declaration);
|
|
|
+ else
|
|
|
+ function = GetFunction(declaration);
|
|
|
+
|
|
|
+ if (!function)
|
|
|
+ {
|
|
|
+ LOGERROR("Event handler function " + handlerName + " not found in " + GetName());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SharedPtr<ScriptEventData> data(new ScriptEventData(function, reciever));
|
|
|
+ scriptEventData_.Push(data);
|
|
|
+ SubscribeToEvent(eventType, HANDLER_USERDATA(ScriptFile, HandleScriptEvent, data.Get()));
|
|
|
+}
|
|
|
+
|
|
|
+void ScriptFile::AddEventHandler(Object* sender, StringHash eventType, const String& handlerName)
|
|
|
+{
|
|
|
+ if (!compiled_)
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (!sender)
|
|
|
+ {
|
|
|
+ LOGERROR("Null event sender for event " + String(eventType) + ", handler " + handlerName);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String declaration = "void " + handlerName + "(StringHash, VariantMap&)";
|
|
|
+ asIScriptFunction* function = 0;
|
|
|
+ asIScriptObject* reciever = static_cast<asIScriptObject*>(asGetActiveContext()->GetThisPointer());
|
|
|
+
|
|
|
+ if (reciever)
|
|
|
+ function = GetMethod(reciever, declaration);
|
|
|
+ else
|
|
|
+ function = GetFunction(declaration);
|
|
|
+
|
|
|
+ if (!function)
|
|
|
+ {
|
|
|
+ declaration = "void " + handlerName + "()";
|
|
|
+
|
|
|
+ if (reciever)
|
|
|
+ function = GetMethod(reciever, declaration);
|
|
|
+ else
|
|
|
+ function = GetFunction(declaration);
|
|
|
+
|
|
|
+ if (!function)
|
|
|
+ {
|
|
|
+ LOGERROR("Event handler function " + handlerName + " not found in " + GetName());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SharedPtr<ScriptEventData> data(new ScriptEventData(function, reciever));
|
|
|
+ scriptEventData_.Push(data);
|
|
|
+ SubscribeToEvent(sender, eventType, HANDLER_USERDATA(ScriptFile, HandleScriptEvent, data.Get()));
|
|
|
}
|
|
|
|
|
|
bool ScriptFile::Execute(const String& declaration, const VariantVector& parameters, bool unprepare)
|
|
|
@@ -651,19 +679,36 @@ void ScriptFile::ReleaseModule()
|
|
|
|
|
|
void ScriptFile::HandleScriptEvent(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
- if (!compiled_)
|
|
|
- return;
|
|
|
-
|
|
|
- asIScriptFunction* function = static_cast<asIScriptFunction*>(GetEventHandler()->GetUserData());
|
|
|
-
|
|
|
- VariantVector parameters;
|
|
|
- if (function->GetParamCount() > 0)
|
|
|
- {
|
|
|
- parameters.Push(Variant((void*)&eventType));
|
|
|
- parameters.Push(Variant((void*)&eventData));
|
|
|
+ if (!compiled_)
|
|
|
+ return;
|
|
|
+
|
|
|
+ ScriptEventData* data = static_cast<ScriptEventData*>(GetEventHandler()->GetUserData());
|
|
|
+
|
|
|
+ asIScriptObject* object = data->GetObject();
|
|
|
+ asIScriptFunction* method = data->GetFunction();
|
|
|
+
|
|
|
+ if (object && !data->IsObjectAlive())
|
|
|
+ {
|
|
|
+ scriptEventData_.Remove(SharedPtr<ScriptEventData>(data));
|
|
|
+ UnsubscribeFromEvent(eventType);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ VariantVector parameters;
|
|
|
+ if (method->GetParamCount() > 0)
|
|
|
+ {
|
|
|
+ parameters.Push(Variant((void*) &eventType));
|
|
|
+ parameters.Push(Variant((void*) &eventData));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (object)
|
|
|
+ {
|
|
|
+ Execute(object, method, parameters);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Execute(method, parameters);
|
|
|
}
|
|
|
-
|
|
|
- Execute(function, parameters);
|
|
|
}
|
|
|
|
|
|
void ScriptFile::HandleUpdate(StringHash eventType, VariantMap& eventData)
|