|
@@ -21,13 +21,17 @@
|
|
|
//
|
|
//
|
|
|
|
|
|
|
|
#include "Precompiled.h"
|
|
#include "Precompiled.h"
|
|
|
|
|
+#include "CoreEvents.h"
|
|
|
#include "Context.h"
|
|
#include "Context.h"
|
|
|
#include "Log.h"
|
|
#include "Log.h"
|
|
|
#include "LuaFile.h"
|
|
#include "LuaFile.h"
|
|
|
#include "LuaScript.h"
|
|
#include "LuaScript.h"
|
|
|
#include "LuaScriptInstance.h"
|
|
#include "LuaScriptInstance.h"
|
|
|
|
|
+#include "MemoryBuffer.h"
|
|
|
|
|
+#include "PhysicsEvents.h"
|
|
|
#include "ResourceCache.h"
|
|
#include "ResourceCache.h"
|
|
|
#include "ProcessUtils.h"
|
|
#include "ProcessUtils.h"
|
|
|
|
|
+#include "VectorBuffer.h"
|
|
|
|
|
|
|
|
extern "C"
|
|
extern "C"
|
|
|
{
|
|
{
|
|
@@ -40,12 +44,30 @@ extern "C"
|
|
|
namespace Urho3D
|
|
namespace Urho3D
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
|
|
+static const char* scriptObjectMethodNames[] = {
|
|
|
|
|
+ ".Start",
|
|
|
|
|
+ ".Stop",
|
|
|
|
|
+ // ".DelayedStart",
|
|
|
|
|
+ ".Update",
|
|
|
|
|
+ ".PostUpdate",
|
|
|
|
|
+ ".FixedUpdate",
|
|
|
|
|
+ ".FixedPostUpdate",
|
|
|
|
|
+ ".Load",
|
|
|
|
|
+ ".Save",
|
|
|
|
|
+ ".ReadNetworkUpdate",
|
|
|
|
|
+ ".WriteNetworkUpdate",
|
|
|
|
|
+ ".ApplyAttributes"
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
LuaScriptInstance::LuaScriptInstance(Context* context) :
|
|
LuaScriptInstance::LuaScriptInstance(Context* context) :
|
|
|
Component(context),
|
|
Component(context),
|
|
|
scriptObjectRef_(LUA_REFNIL)
|
|
scriptObjectRef_(LUA_REFNIL)
|
|
|
{
|
|
{
|
|
|
luaScript_ = GetSubsystem<LuaScript>();
|
|
luaScript_ = GetSubsystem<LuaScript>();
|
|
|
luaState_ = luaScript_->GetLuaState();
|
|
luaState_ = luaScript_->GetLuaState();
|
|
|
|
|
+
|
|
|
|
|
+ for (unsigned i = 0; i < MAX_LUA_SCRIPT_OBJECT_METHODS; ++i)
|
|
|
|
|
+ scriptObjectMethodRefs_[i] = LUA_REFNIL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
LuaScriptInstance::~LuaScriptInstance()
|
|
LuaScriptInstance::~LuaScriptInstance()
|
|
@@ -55,13 +77,61 @@ LuaScriptInstance::~LuaScriptInstance()
|
|
|
|
|
|
|
|
void LuaScriptInstance::RegisterObject(Context* context)
|
|
void LuaScriptInstance::RegisterObject(Context* context)
|
|
|
{
|
|
{
|
|
|
- context->RegisterFactory<LuaScriptInstance>();
|
|
|
|
|
|
|
+ context->RegisterFactory<LuaScriptInstance>(LOGIC_CATEGORY);
|
|
|
|
|
+
|
|
|
|
|
+ // ACCESSOR_ATTRIBUTE(LuaScriptInstance, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
|
|
|
|
|
+ REF_ACCESSOR_ATTRIBUTE(LuaScriptInstance, VAR_STRING, "Script File Name", GetScriptFileName, SetScriptFileName, String, String::EMPTY, AM_DEFAULT);
|
|
|
|
|
+ REF_ACCESSOR_ATTRIBUTE(LuaScriptInstance, VAR_STRING, "Script Object Type", GetScriptObjectType, SetScriptObjectType, String, String::EMPTY, AM_DEFAULT);
|
|
|
|
|
+ ACCESSOR_ATTRIBUTE(LuaScriptInstance, VAR_BUFFER, "Script Data", GetScriptDataAttr, SetScriptDataAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE | AM_NOEDIT);
|
|
|
|
|
+ ACCESSOR_ATTRIBUTE(LuaScriptInstance, VAR_BUFFER, "Script Network Data", GetScriptNetworkDataAttr, SetScriptNetworkDataAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_NOEDIT);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::ApplyAttributes()
|
|
|
|
|
+{
|
|
|
|
|
+ CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_APPLYATTRIBUTES]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool LuaScriptInstance::CreateObject(const String& scriptObjectType)
|
|
bool LuaScriptInstance::CreateObject(const String& scriptObjectType)
|
|
|
|
|
+{
|
|
|
|
|
+ SetScriptFileName(String::EMPTY);
|
|
|
|
|
+ SetScriptObjectType(scriptObjectType);
|
|
|
|
|
+ return scriptObjectRef_ != LUA_REFNIL;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool LuaScriptInstance::CreateObject(const String& scriptFileName, const String& scriptObjectType)
|
|
|
|
|
+{
|
|
|
|
|
+ SetScriptFileName(scriptFileName);
|
|
|
|
|
+ SetScriptObjectType(scriptObjectType);
|
|
|
|
|
+ return scriptObjectRef_ != LUA_REFNIL;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::SetScriptFileName(const String& scriptFileName)
|
|
|
|
|
+{
|
|
|
|
|
+ if (scriptFileName_ == scriptFileName)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ scriptFileName_ = scriptFileName;
|
|
|
|
|
+
|
|
|
|
|
+ if (!scriptFileName_.Empty())
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
|
|
+ LuaFile* luaFile = cache->GetResource<LuaFile>(scriptFileName_);
|
|
|
|
|
+ if (!luaFile)
|
|
|
|
|
+ {
|
|
|
|
|
+ LOGERROR("Get Lua file failed: " + scriptFileName_);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!luaFile->LoadAndExecute(luaState_))
|
|
|
|
|
+ {
|
|
|
|
|
+ LOGERROR("Execute Lua file failed: " + scriptFileName_);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::SetScriptObjectType(const String& scriptObjectType)
|
|
|
{
|
|
{
|
|
|
if (scriptObjectType_ == scriptObjectType)
|
|
if (scriptObjectType_ == scriptObjectType)
|
|
|
- return true;
|
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
ReleaseObject();
|
|
ReleaseObject();
|
|
|
|
|
|
|
@@ -72,7 +142,7 @@ bool LuaScriptInstance::CreateObject(const String& scriptObjectType)
|
|
|
{
|
|
{
|
|
|
LOGERROR("Could not find lua function CreateScriptObjectInstance");
|
|
LOGERROR("Could not find lua function CreateScriptObjectInstance");
|
|
|
lua_settop(luaState_, top);
|
|
lua_settop(luaState_, top);
|
|
|
- return false;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Get table as first paramter.
|
|
// Get table as first paramter.
|
|
@@ -81,52 +151,56 @@ bool LuaScriptInstance::CreateObject(const String& scriptObjectType)
|
|
|
{
|
|
{
|
|
|
LOGERROR("Could not find lua table " + scriptObjectType);
|
|
LOGERROR("Could not find lua table " + scriptObjectType);
|
|
|
lua_settop(luaState_, top);
|
|
lua_settop(luaState_, top);
|
|
|
- return false;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Push this as second parameter.
|
|
// Push this as second parameter.
|
|
|
tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Call ObjectType:new function.
|
|
// Call ObjectType:new function.
|
|
|
if (lua_pcall(luaState_, 2, 1, 0) != 0)
|
|
if (lua_pcall(luaState_, 2, 1, 0) != 0)
|
|
|
{
|
|
{
|
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
lua_settop(luaState_, top);
|
|
lua_settop(luaState_, top);
|
|
|
- return false;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
scriptObjectType_ = scriptObjectType;
|
|
scriptObjectType_ = scriptObjectType;
|
|
|
scriptObjectRef_ = luaL_ref(luaState_, LUA_REGISTRYINDEX);
|
|
scriptObjectRef_ = luaL_ref(luaState_, LUA_REGISTRYINDEX);
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ // Find script object method refs.
|
|
|
|
|
+ FindScriptObjectMethodRefs();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool LuaScriptInstance::CreateObject(const String& fileName, const String& scriptObjectType)
|
|
|
|
|
|
|
+void LuaScriptInstance::SetScriptDataAttr(PODVector<unsigned char> data)
|
|
|
{
|
|
{
|
|
|
- ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
|
|
-
|
|
|
|
|
- LuaFile* luaFile = cache->GetResource<LuaFile>(fileName);
|
|
|
|
|
- if (!luaFile)
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ int functionRef = scriptObjectMethodRefs_[LSOM_LOAD];
|
|
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
- if (!luaFile->LoadAndExecute(luaState_))
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ MemoryBuffer buf(data);
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, (Deserializer&)buf);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::SetScriptNetworkDataAttr(PODVector<unsigned char> data)
|
|
|
|
|
+{
|
|
|
|
|
+ int functionRef = scriptObjectMethodRefs_[LSOM_READNETWORKUPDATE];
|
|
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
- return CreateObject(scriptObjectType);
|
|
|
|
|
|
|
+ MemoryBuffer buf(data);
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, (Deserializer&)buf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
|
|
void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
|
|
|
{
|
|
{
|
|
|
- StringHash eventType(eventName);
|
|
|
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
|
|
|
|
|
|
- int functionRef = LUA_REFNIL;
|
|
|
|
|
- if (luaScript_->FindFunction(realFunctionName))
|
|
|
|
|
- functionRef = luaL_ref(luaState_, LUA_REGISTRYINDEX);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ int functionRef = luaScript_->GetScriptFunctionRef(realFunctionName);
|
|
|
if (functionRef != LUA_REFNIL)
|
|
if (functionRef != LUA_REFNIL)
|
|
|
{
|
|
{
|
|
|
|
|
+ StringHash eventType(eventName);
|
|
|
SubscribeToEvent(eventType, HANDLER(LuaScriptInstance, HandleEvent));
|
|
SubscribeToEvent(eventType, HANDLER(LuaScriptInstance, HandleEvent));
|
|
|
eventTypeToFunctionRefMap_[eventType] = functionRef;
|
|
eventTypeToFunctionRefMap_[eventType] = functionRef;
|
|
|
}
|
|
}
|
|
@@ -140,10 +214,6 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvent(const String& eventName)
|
|
|
if (i != eventTypeToFunctionRefMap_.End())
|
|
if (i != eventTypeToFunctionRefMap_.End())
|
|
|
{
|
|
{
|
|
|
UnsubscribeFromEvent(eventType);
|
|
UnsubscribeFromEvent(eventType);
|
|
|
-
|
|
|
|
|
- if (i->second_ != LUA_REFNIL)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
|
|
|
|
|
-
|
|
|
|
|
eventTypeToFunctionRefMap_.Erase(i);
|
|
eventTypeToFunctionRefMap_.Erase(i);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -154,66 +224,117 @@ void LuaScriptInstance::ScriptUnsubscribeFromAllEvents()
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
|
UnsubscribeFromAllEvents();
|
|
UnsubscribeFromAllEvents();
|
|
|
-
|
|
|
|
|
- for (HashMap<StringHash, int>::Iterator i = eventTypeToFunctionRefMap_.Begin(); i != eventTypeToFunctionRefMap_.End(); ++i)
|
|
|
|
|
- if (i->second_ != LUA_REFNIL)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
|
|
|
|
|
-
|
|
|
|
|
eventTypeToFunctionRefMap_.Clear();
|
|
eventTypeToFunctionRefMap_.Clear();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void LuaScriptInstance::ScriptSubscribeToEvent(void* object, const String& eventName, const String& functionName)
|
|
|
|
|
|
|
+void LuaScriptInstance::ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName)
|
|
|
{
|
|
{
|
|
|
- StringHash eventType(eventName);
|
|
|
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
|
- Object* sender = (Object*)object;
|
|
|
|
|
-
|
|
|
|
|
- int functionRef = LUA_REFNIL;
|
|
|
|
|
- if (luaScript_->FindFunction(realFunctionName))
|
|
|
|
|
- functionRef = luaL_ref(luaState_, LUA_REGISTRYINDEX);
|
|
|
|
|
|
|
|
|
|
|
|
+ int functionRef = luaScript_->GetScriptFunctionRef(realFunctionName);
|
|
|
if (functionRef != LUA_REFNIL)
|
|
if (functionRef != LUA_REFNIL)
|
|
|
{
|
|
{
|
|
|
- SubscribeToEvent(sender, eventType, HANDLER(LuaScriptInstance, HandleObjectEvent));
|
|
|
|
|
- objectToEventTypeToFunctionRefMap_[sender][eventType] = functionRef;
|
|
|
|
|
|
|
+ Object* object = (Object*)sender;
|
|
|
|
|
+ StringHash eventType(eventName);
|
|
|
|
|
+ SubscribeToEvent(object, eventType, HANDLER(LuaScriptInstance, HandleObjectEvent));
|
|
|
|
|
+ objectToEventTypeToFunctionRefMap_[object][eventType] = functionRef;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void LuaScriptInstance::ScriptUnsubscribeFromEvent(void* object, const String& eventName)
|
|
|
|
|
|
|
+void LuaScriptInstance::ScriptUnsubscribeFromEvent(void* sender, const String& eventName)
|
|
|
{
|
|
{
|
|
|
StringHash eventType(eventName);
|
|
StringHash eventType(eventName);
|
|
|
- Object* sender = (Object*)object;
|
|
|
|
|
|
|
+ Object* object = (Object*)sender ;
|
|
|
|
|
|
|
|
- HashMap<StringHash, int>::Iterator i = objectToEventTypeToFunctionRefMap_[sender].Find(eventType);
|
|
|
|
|
- if (i != objectToEventTypeToFunctionRefMap_[sender].End())
|
|
|
|
|
|
|
+ HashMap<StringHash, int>::Iterator i = objectToEventTypeToFunctionRefMap_[object].Find(eventType);
|
|
|
|
|
+ if (i != objectToEventTypeToFunctionRefMap_[object].End())
|
|
|
{
|
|
{
|
|
|
- UnsubscribeFromEvent(sender, eventType);
|
|
|
|
|
-
|
|
|
|
|
- if (i->second_ != LUA_REFNIL)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
|
|
|
|
|
-
|
|
|
|
|
- objectToEventTypeToFunctionRefMap_[sender].Erase(i);
|
|
|
|
|
|
|
+ UnsubscribeFromEvent(object, eventType);
|
|
|
|
|
+ objectToEventTypeToFunctionRefMap_[object].Erase(i);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void LuaScriptInstance::ScriptUnsubscribeFromEvents(void* object)
|
|
|
|
|
|
|
+void LuaScriptInstance::ScriptUnsubscribeFromEvents(void* sender)
|
|
|
{
|
|
{
|
|
|
- Object* sender = (Object*)object;
|
|
|
|
|
|
|
+ Object* object = (Object*)sender;
|
|
|
|
|
|
|
|
- HashMap<Object*, HashMap<StringHash, int> >::Iterator it = objectToEventTypeToFunctionRefMap_.Find(sender);
|
|
|
|
|
|
|
+ HashMap<Object*, HashMap<StringHash, int> >::Iterator it = objectToEventTypeToFunctionRefMap_.Find(object);
|
|
|
if (it == objectToEventTypeToFunctionRefMap_.End())
|
|
if (it == objectToEventTypeToFunctionRefMap_.End())
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
|
- UnsubscribeFromEvents(sender);
|
|
|
|
|
|
|
+ UnsubscribeFromEvents(object);
|
|
|
|
|
+ objectToEventTypeToFunctionRefMap_.Erase(it);
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- HashMap<StringHash, int>& eventTypeToFunctionRefMap = it->second_;
|
|
|
|
|
- for (HashMap<StringHash, int>::Iterator i = eventTypeToFunctionRefMap.Begin(); i != eventTypeToFunctionRefMap.End(); ++i)
|
|
|
|
|
- {
|
|
|
|
|
- if (i->second_ != LUA_REFNIL)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+PODVector<unsigned char> LuaScriptInstance::GetScriptDataAttr() const
|
|
|
|
|
+{
|
|
|
|
|
+ int functionRef = scriptObjectMethodRefs_[LSOM_SAVE];
|
|
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
|
|
+ return PODVector<unsigned char>();
|
|
|
|
|
|
|
|
- objectToEventTypeToFunctionRefMap_.Erase(it);
|
|
|
|
|
|
|
+ VectorBuffer buf;
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, (Serializer&)buf);
|
|
|
|
|
+
|
|
|
|
|
+ return buf.GetBuffer();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+PODVector<unsigned char> LuaScriptInstance::GetScriptNetworkDataAttr() const
|
|
|
|
|
+{
|
|
|
|
|
+ int functionRef = scriptObjectMethodRefs_[LSOM_WRITENETWORKUPDATE];
|
|
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
|
|
+ return PODVector<unsigned char>();
|
|
|
|
|
+
|
|
|
|
|
+ VectorBuffer buf;
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, (Serializer&)buf);
|
|
|
|
|
+
|
|
|
|
|
+ return buf.GetBuffer();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::FindScriptObjectMethodRefs()
|
|
|
|
|
+{
|
|
|
|
|
+ for (unsigned i = 0; i < MAX_LUA_SCRIPT_OBJECT_METHODS; ++i)
|
|
|
|
|
+ scriptObjectMethodRefs_[i] = luaScript_->GetScriptFunctionRef(scriptObjectType_ + scriptObjectMethodNames[i]);
|
|
|
|
|
+
|
|
|
|
|
+ if (scriptObjectMethodRefs_[LSOM_UPDATE] != LUA_REFNIL)
|
|
|
|
|
+ SubscribeToEvent(E_UPDATE, HANDLER(LuaScriptInstance, HandleUpdate));
|
|
|
|
|
+
|
|
|
|
|
+ if (scriptObjectMethodRefs_[LSOM_POSTUPDATE] != LUA_REFNIL)
|
|
|
|
|
+ SubscribeToEvent(E_POSTUPDATE, HANDLER(LuaScriptInstance, HandlePostUpdate));
|
|
|
|
|
+
|
|
|
|
|
+ if (scriptObjectMethodRefs_[LSOM_FIXEDUPDATE] != LUA_REFNIL)
|
|
|
|
|
+ SubscribeToEvent(E_PHYSICSPRESTEP, HANDLER(LuaScriptInstance, HandleFixedUpdate));
|
|
|
|
|
+
|
|
|
|
|
+ if (scriptObjectMethodRefs_[LSOM_FIXEDPOSTUPDATE] != LUA_REFNIL)
|
|
|
|
|
+ SubscribeToEvent(E_PHYSICSPOSTSTEP, HANDLER(LuaScriptInstance, HandlePostFixedUpdate));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
|
|
+{
|
|
|
|
|
+ using namespace Update;
|
|
|
|
|
+ float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
|
|
+ CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_UPDATE], timeStep);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
|
|
+{
|
|
|
|
|
+ using namespace PostUpdate;
|
|
|
|
|
+ float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
|
|
+ CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_POSTUPDATE], timeStep);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::HandleFixedUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
|
|
+{
|
|
|
|
|
+ using namespace PhysicsPreStep;
|
|
|
|
|
+ float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
|
|
+ CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_FIXEDUPDATE], timeStep);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
|
|
+{
|
|
|
|
|
+ using namespace PhysicsPostStep;
|
|
|
|
|
+ float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
|
|
+ CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_FIXEDPOSTUPDATE], timeStep);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
|
|
void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
|
|
@@ -225,7 +346,7 @@ void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
|
|
|
if (functionRef == LUA_REFNIL)
|
|
if (functionRef == LUA_REFNIL)
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
|
- CallEventHandler(functionRef, eventType, eventData);
|
|
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, eventType, eventData);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void LuaScriptInstance::HandleObjectEvent(StringHash eventType, VariantMap& eventData)
|
|
void LuaScriptInstance::HandleObjectEvent(StringHash eventType, VariantMap& eventData)
|
|
@@ -238,11 +359,43 @@ void LuaScriptInstance::HandleObjectEvent(StringHash eventType, VariantMap& even
|
|
|
if (functionRef == LUA_REFNIL)
|
|
if (functionRef == LUA_REFNIL)
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
|
- CallEventHandler(functionRef, eventType, eventData);
|
|
|
|
|
|
|
+ CallScriptObjectFunction(functionRef, eventType, eventData);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void LuaScriptInstance::CallEventHandler(int functionRef, StringHash eventType, VariantMap& eventData )
|
|
|
|
|
|
|
+void LuaScriptInstance::ReleaseObject()
|
|
|
{
|
|
{
|
|
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ // Unref script object.
|
|
|
|
|
+ luaL_unref(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
+ scriptObjectRef_ = LUA_REFNIL;
|
|
|
|
|
+
|
|
|
|
|
+ int top = lua_gettop(luaState_);
|
|
|
|
|
+ lua_getglobal(luaState_, "DestroyScriptObjectInstance");
|
|
|
|
|
+ if (!lua_isfunction(luaState_, -1))
|
|
|
|
|
+ {
|
|
|
|
|
+ LOGERROR("Could not find lua function DestroyScriptObjectInstance");
|
|
|
|
|
+ lua_settop(luaState_, top);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Push this as second parameter.
|
|
|
|
|
+ tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
|
|
|
+ if (lua_pcall(luaState_, 1, 0, 0) != 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ const char* message = lua_tostring(luaState_, -1);
|
|
|
|
|
+ LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
|
|
+ lua_settop(luaState_, top);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef)
|
|
|
|
|
+{
|
|
|
|
|
+ if (functionRef == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
int top = lua_gettop(luaState_);
|
|
int top = lua_gettop(luaState_);
|
|
|
|
|
|
|
|
// Push function.
|
|
// Push function.
|
|
@@ -251,13 +404,34 @@ void LuaScriptInstance::CallEventHandler(int functionRef, StringHash eventType,
|
|
|
// Push script object.
|
|
// Push script object.
|
|
|
lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
|
|
|
- // Push event type.
|
|
|
|
|
- tolua_pushusertype(luaState_, (void*)&eventType, "StringHash");
|
|
|
|
|
|
|
+ // Call update function.
|
|
|
|
|
+ if (lua_pcall(luaState_, 1, 0, 0) != 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ const char* message = lua_tostring(luaState_, -1);
|
|
|
|
|
+ LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
|
|
+ lua_settop(luaState_, top);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // Push event data.
|
|
|
|
|
- tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
|
|
|
|
|
|
|
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef, float timeStep)
|
|
|
|
|
+{
|
|
|
|
|
+ if (functionRef == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
|
|
|
- if (lua_pcall(luaState_, 3, 0, 0) != 0)
|
|
|
|
|
|
|
+ int top = lua_gettop(luaState_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push function.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
|
|
+
|
|
|
|
|
+ // Push script object.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push time step.
|
|
|
|
|
+ tolua_pushnumber(luaState_, timeStep);
|
|
|
|
|
+
|
|
|
|
|
+ // Call update function.
|
|
|
|
|
+ if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
{
|
|
{
|
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
@@ -266,39 +440,74 @@ void LuaScriptInstance::CallEventHandler(int functionRef, StringHash eventType,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void LuaScriptInstance::ReleaseObject()
|
|
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Deserializer& deserializer)
|
|
|
{
|
|
{
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
|
|
|
|
+ if (functionRef == LUA_REFNIL)
|
|
|
return;
|
|
return;
|
|
|
-
|
|
|
|
|
- // Unref script object.
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
- scriptObjectRef_ = LUA_REFNIL;
|
|
|
|
|
-
|
|
|
|
|
- // Unref Lua function.
|
|
|
|
|
- for (HashMap<StringHash, int>::Iterator i = eventTypeToFunctionRefMap_.Begin(); i != eventTypeToFunctionRefMap_.End(); ++i)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, i->second_);
|
|
|
|
|
- for (HashMap<Object*, HashMap<StringHash, int> >::Iterator i = objectToEventTypeToFunctionRefMap_.Begin();
|
|
|
|
|
- i != objectToEventTypeToFunctionRefMap_.End(); ++i)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ int top = lua_gettop(luaState_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push function.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
|
|
+
|
|
|
|
|
+ // Push script object.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push Deserializer.
|
|
|
|
|
+ tolua_pushusertype(luaState_, (void*)&deserializer, "Deserializer");
|
|
|
|
|
+
|
|
|
|
|
+ // Call update function.
|
|
|
|
|
+ if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
{
|
|
{
|
|
|
- for (HashMap<StringHash, int>::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
|
|
|
|
|
- luaL_unref(luaState_, LUA_REGISTRYINDEX, j->second_);
|
|
|
|
|
|
|
+ const char* message = lua_tostring(luaState_, -1);
|
|
|
|
|
+ LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
|
|
+ lua_settop(luaState_, top);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Serializer& serializer) const
|
|
|
|
|
+{
|
|
|
|
|
+ if (functionRef == LUA_REFNIL)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
int top = lua_gettop(luaState_);
|
|
int top = lua_gettop(luaState_);
|
|
|
|
|
|
|
|
- lua_getglobal(luaState_, "DestroyScriptObjectInstance");
|
|
|
|
|
- if (!lua_isfunction(luaState_, -1))
|
|
|
|
|
|
|
+ // Push function.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
|
|
+
|
|
|
|
|
+ // Push script object.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push Deserializer.
|
|
|
|
|
+ tolua_pushusertype(luaState_, (void*)&serializer, "Serializer");
|
|
|
|
|
+
|
|
|
|
|
+ // Call update function.
|
|
|
|
|
+ if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
{
|
|
{
|
|
|
- LOGERROR("Could not find lua function DestroyScriptObjectInstance");
|
|
|
|
|
|
|
+ const char* message = lua_tostring(luaState_, -1);
|
|
|
|
|
+ LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
lua_settop(luaState_, top);
|
|
lua_settop(luaState_, top);
|
|
|
- return;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // Push this as second parameter.
|
|
|
|
|
- tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
|
|
|
|
|
+void LuaScriptInstance::CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData )
|
|
|
|
|
+{
|
|
|
|
|
+ int top = lua_gettop(luaState_);
|
|
|
|
|
|
|
|
- if (lua_pcall(luaState_, 1, 0, 0) != 0)
|
|
|
|
|
|
|
+ // Push function.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
|
|
+
|
|
|
|
|
+ // Push script object.
|
|
|
|
|
+ lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
|
|
+
|
|
|
|
|
+ // Push event type.
|
|
|
|
|
+ tolua_pushusertype(luaState_, (void*)&eventType, "StringHash");
|
|
|
|
|
+
|
|
|
|
|
+ // Push event data.
|
|
|
|
|
+ tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
|
|
|
|
|
+
|
|
|
|
|
+ if (lua_pcall(luaState_, 3, 0, 0) != 0)
|
|
|
{
|
|
{
|
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
const char* message = lua_tostring(luaState_, -1);
|
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
LOGERROR("Execute Lua function failed: " + String(message));
|
|
@@ -306,5 +515,4 @@ void LuaScriptInstance::ReleaseObject()
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|