| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Context.h"
- #include "FileSystem.h"
- #include "Log.h"
- #include "Profiler.h"
- #include "ResourceCache.h"
- #include "Script.h"
- #include "ScriptFile.h"
- #include "ScriptInstance.h"
- #include "ArrayPtr.h"
- #include <angelscript.h>
- #include <cstring>
- #include "DebugNew.h"
- OBJECTTYPESTATIC(ScriptFile);
- ScriptFile::ScriptFile(Context* context) :
- Resource(context),
- script_(GetSubsystem<Script>()),
- scriptModule_(0),
- compiled_(false)
- {
- }
- ScriptFile::~ScriptFile()
- {
- ReleaseModule();
- }
- void ScriptFile::RegisterObject(Context* context)
- {
- context->RegisterFactory<ScriptFile>();
- }
- bool ScriptFile::Load(Deserializer& source)
- {
- PROFILE(LoadScript);
-
- ReleaseModule();
-
- // Create the module. Discard previous module if there was one
- if (scriptModule_)
- script_->GetModuleMap().Erase(scriptModule_);
- asIScriptEngine* engine = script_->GetScriptEngine();
- scriptModule_ = engine->GetModule(GetName().CString(), asGM_ALWAYS_CREATE);
- if (!scriptModule_)
- {
- LOGERROR("Failed to create script module " + GetName());
- return false;
- }
-
- // Add the initial section and check for includes
- if (!AddScriptSection(engine, source))
- return false;
-
- // Compile. Set script engine logging to retained mode so that potential exceptions can show all error info
- script_->SetLogMode(LOGMODE_RETAINED);
- script_->ClearLogMessages();
- int result = scriptModule_->Build();
- String errors = script_->GetLogMessages();
- script_->SetLogMode(LOGMODE_IMMEDIATE);
- if (result < 0)
- {
- LOGERROR("Failed to compile script module " + GetName() + ":\n" + errors);
- return false;
- }
- if (!errors.Empty())
- LOGWARNING(errors);
-
- LOGINFO("Compiled script module " + GetName());
- compiled_ = true;
- script_->GetModuleMap()[scriptModule_] = this;
-
- 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));
- }
- bool ScriptFile::Execute(const String& declaration, const VariantVector& parameters, bool unprepare)
- {
- asIScriptFunction* function = GetFunction(declaration);
- if (!function)
- {
- LOGERROR("Function " + declaration + " not found in " + GetName());
- return false;
- }
-
- return Execute(GetFunction(declaration), parameters, unprepare);
- }
- bool ScriptFile::Execute(asIScriptFunction* function, const VariantVector& parameters, bool unprepare)
- {
- PROFILE(ExecuteFunction);
-
- if (!compiled_ || !function)
- return false;
-
- // It is possible that executing the function causes us to unload. Therefore do not rely on member variables
- // However, we are not prepared for the whole script system getting destroyed during execution (should never happen)
- Script* scriptSystem = script_;
-
- asIScriptContext* context = scriptSystem->GetScriptFileContext();
- if (!context)
- {
- LOGERROR("Maximum script execution nesting level exceeded");
- return false;
- }
-
- if (context->Prepare(function->GetId()) < 0)
- return false;
-
- SetParameters(context, function, parameters);
-
- scriptSystem->IncScriptNestingLevel();
- bool success = context->Execute() >= 0;
- if (unprepare)
- context->Unprepare();
- scriptSystem->DecScriptNestingLevel();
-
- return success;
- }
- bool ScriptFile::Execute(asIScriptObject* object, const String& declaration, const VariantVector& parameters, bool unprepare)
- {
- asIScriptFunction* method = GetMethod(object, declaration);
- if (!method)
- {
- LOGERROR("Method " + declaration + " not found in " + GetName());
- return false;
- }
-
- return Execute(object, method, parameters, unprepare);
- }
- bool ScriptFile::Execute(asIScriptObject* object, asIScriptFunction* method, const VariantVector& parameters, bool unprepare)
- {
- PROFILE(ExecuteMethod);
-
- if (!compiled_ || !object || !method)
- return false;
-
- // It is possible that executing the method causes us to unload. Therefore do not rely on member variables
- // However, we are not prepared for the whole script system getting destroyed during execution (should never happen)
- Script* scriptSystem = script_;
-
- asIScriptContext* context = scriptSystem->GetScriptFileContext();
- if (!context)
- {
- LOGERROR("Maximum script execution nesting level exceeded");
- return false;
- }
-
- if (context->Prepare(method->GetId()) < 0)
- return false;
-
- context->SetObject(object);
- SetParameters(context, method, parameters);
-
- scriptSystem->IncScriptNestingLevel();
- bool success = context->Execute() >= 0;
- if (unprepare)
- context->Unprepare();
- scriptSystem->DecScriptNestingLevel();
-
- return success;
- }
- asIScriptObject* ScriptFile::CreateObject(const String& className)
- {
- PROFILE(CreateObject);
-
- if (!IsCompiled())
- return 0;
-
- asIScriptContext* context = script_->GetScriptFileContext();
- if (!context)
- {
- LOGERROR("Maximum script execution nesting level exceeded, can not create object");
- return 0;
- }
-
- asIScriptEngine* engine = script_->GetScriptEngine();
- asIObjectType *type = engine->GetObjectTypeById(scriptModule_->GetTypeIdByDecl(className.CString()));
- if (!type)
- return 0;
-
- // Ensure that the type implements the "ScriptObject" interface, so it can be returned to script properly
- bool found = false;
- Map<asIObjectType*, bool>::ConstIterator i = checkedClasses_.Find(type);
- if (i != checkedClasses_.End())
- found = i->second_;
- else
- {
- unsigned numInterfaces = type->GetInterfaceCount();
- for (unsigned j = 0; j < numInterfaces; ++j)
- {
- asIObjectType* interfaceType = type->GetInterface(j);
- if (!strcmp(interfaceType->GetName(), "ScriptObject"))
- {
- found = true;
- break;
- }
- }
- checkedClasses_[type] = found;
- }
- if (!found)
- {
- LOGERROR("Script class " + className + " does not implement the ScriptObject interface");
- return 0;
- }
-
- // Get the factory function id from the object type
- String factoryName = className + "@ " + className + "()";
- int factoryId = type->GetFactoryIdByDecl(factoryName.CString());
- if (factoryId < 0 || context->Prepare(factoryId) < 0 || context->Execute() < 0)
- return 0;
-
- asIScriptObject* obj = *(static_cast<asIScriptObject**>(context->GetAddressOfReturnValue()));
- if (obj)
- obj->AddRef();
-
- return obj;
- }
- asIScriptFunction* ScriptFile::GetFunction(const String& declaration)
- {
- if (!compiled_)
- return 0;
-
- Map<String, asIScriptFunction*>::ConstIterator i = functions_.Find(declaration);
- if (i != functions_.End())
- return i->second_;
-
- int id = scriptModule_->GetFunctionIdByDecl(declaration.CString());
- asIScriptFunction* function = scriptModule_->GetFunctionDescriptorById(id);
- functions_[declaration] = function;
- return function;
- }
- asIScriptFunction* ScriptFile::GetMethod(asIScriptObject* object, const String& declaration)
- {
- if (!compiled_ || !object)
- return 0;
-
- asIObjectType* type = object->GetObjectType();
- if (!type)
- return 0;
- Map<asIObjectType*, Map<String, asIScriptFunction*> >::ConstIterator i = methods_.Find(type);
- if (i != methods_.End())
- {
- Map<String, asIScriptFunction*>::ConstIterator j = i->second_.Find(declaration);
- if (j != i->second_.End())
- return j->second_;
- }
-
- int id = type->GetMethodIdByDecl(declaration.CString());
- asIScriptFunction* function = scriptModule_->GetFunctionDescriptorById(id);
- methods_[type][declaration] = function;
- return function;
- }
- bool ScriptFile::AddScriptSection(asIScriptEngine* engine, Deserializer& source)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
-
- unsigned dataSize = source.GetSize();
- SharedArrayPtr<char> buffer(new char[dataSize]);
- source.Read((void*)buffer.RawPtr(), dataSize);
-
- // Pre-parse for includes
- // Adapted from Angelscript's scriptbuilder add-on
- Vector<String> includeFiles;
- unsigned pos = 0;
- while(pos < dataSize)
- {
- int len;
- asETokenClass t = engine->ParseToken(&buffer[pos], dataSize - pos, &len);
- if (t == asTC_COMMENT || t == asTC_WHITESPACE)
- {
- pos += len;
- continue;
- }
- // Is this a preprocessor directive?
- if (buffer[pos] == '#')
- {
- int start = pos++;
- asETokenClass t = engine->ParseToken(&buffer[pos], dataSize - pos, &len);
- if (t == asTC_IDENTIFIER)
- {
- String token(&buffer[pos], len);
- if (token == "include")
- {
- pos += len;
- t = engine->ParseToken(&buffer[pos], dataSize - pos, &len);
- if (t == asTC_WHITESPACE)
- {
- pos += len;
- t = engine->ParseToken(&buffer[pos], dataSize - pos, &len);
- }
-
- if (t == asTC_VALUE && len > 2 && buffer[pos] == '"')
- {
- // Get the include file
- String includeFile(&buffer[pos+1], len - 2);
- pos += len;
-
- // If the file is not found as it is, add the path of current file
- if (!cache->Exists(includeFile))
- includeFile = GetPath(GetName()) + includeFile;
-
- String includeFileLower = includeFile.ToLower();
-
- // If not included yet, store it for later processing
- if (includeFiles_.Find(includeFileLower) == includeFiles_.End())
- {
- includeFiles_.Insert(includeFileLower);
- includeFiles.Push(includeFile);
- }
-
- // Overwrite the include directive with space characters to avoid compiler error
- memset(&buffer[start], ' ', pos - start);
- }
- }
- }
- }
- // Don't search includes within statement blocks or between tokens in statements
- else
- {
- int len;
- // Skip until ; or { whichever comes first
- while (pos < dataSize && buffer[pos] != ';' && buffer[pos] != '{')
- {
- engine->ParseToken(&buffer[pos], 0, &len);
- pos += len;
- }
- // Skip entire statement block
- if (pos < dataSize && buffer[pos] == '{')
- {
- ++pos;
- // Find the end of the statement block
- int level = 1;
- while (level > 0 && pos < dataSize)
- {
- asETokenClass t = engine->ParseToken(&buffer[pos], 0, &len);
- if (t == asTC_KEYWORD)
- {
- if (buffer[pos] == '{')
- level++;
- else if(buffer[pos] == '}')
- level--;
- }
- pos += len;
- }
- }
- else
- ++pos;
- }
- }
-
- // Process includes first
- for (unsigned i = 0; i < includeFiles.Size(); ++i)
- {
- SharedPtr<File> file = cache->GetFile(includeFiles[i]);
- if (!AddScriptSection(engine, *file))
- return false;
- }
-
- // Then add this section
- if (scriptModule_->AddScriptSection(source.GetName().CString(), (const char*)buffer.RawPtr(), dataSize) < 0)
- {
- LOGERROR("Failed to add script section " + source.GetName());
- return false;
- }
-
- SetMemoryUse(GetMemoryUse() + dataSize);
- return true;
- }
- void ScriptFile::SetParameters(asIScriptContext* context, asIScriptFunction* function, const VariantVector& parameters)
- {
- unsigned paramCount = function->GetParamCount();
- for (unsigned i = 0; i < parameters.Size() && i < paramCount; ++i)
- {
- int paramType = function->GetParamTypeId(i);
-
- switch (paramType)
- {
- case asTYPEID_BOOL:
- context->SetArgByte(i, (unsigned char)parameters[i].GetBool());
- break;
-
- case asTYPEID_INT8:
- case asTYPEID_UINT8:
- context->SetArgByte(i, parameters[i].GetInt());
- break;
-
- case asTYPEID_INT16:
- case asTYPEID_UINT16:
- context->SetArgWord(i, parameters[i].GetInt());
- break;
-
- case asTYPEID_INT32:
- case asTYPEID_UINT32:
- context->SetArgDWord(i, parameters[i].GetInt());
- break;
-
- case asTYPEID_FLOAT:
- context->SetArgFloat(i, parameters[i].GetFloat());
- break;
-
- default:
- if (paramType & asTYPEID_APPOBJECT)
- {
- switch (parameters[i].GetType())
- {
- case VAR_VECTOR2:
- context->SetArgObject(i, (void *)¶meters[i].GetVector2());
- break;
-
- case VAR_VECTOR3:
- context->SetArgObject(i, (void *)¶meters[i].GetVector3());
- break;
-
- case VAR_VECTOR4:
- context->SetArgObject(i, (void *)¶meters[i].GetVector4());
- break;
-
- case VAR_QUATERNION:
- context->SetArgObject(i, (void *)¶meters[i].GetQuaternion());
- break;
-
- case VAR_STRING:
- context->SetArgObject(i, (void *)¶meters[i].GetString());
- break;
-
- case VAR_PTR:
- context->SetArgObject(i, (void *)parameters[i].GetPtr());
- break;
-
- default:
- break;
- }
- }
- break;
- }
- }
- }
- void ScriptFile::ReleaseModule()
- {
- if (scriptModule_)
- {
- // Clear search caches, event handlers and function-to-file mappings
- includeFiles_.Clear();
- checkedClasses_.Clear();
- functions_.Clear();
- methods_.Clear();
- UnsubscribeFromAllEventsWithUserData();
-
- // Remove the module
- script_->GetModuleMap().Erase(scriptModule_);
- asIScriptEngine* engine = script_->GetScriptEngine();
- engine->DiscardModule(GetName().CString());
- scriptModule_ = 0;
- compiled_ = false;
- SetMemoryUse(0);
- }
- }
- void ScriptFile::HandleScriptEvent(StringHash eventType, VariantMap& eventData)
- {
- if (!compiled_)
- return;
-
- asIScriptFunction* function = static_cast<asIScriptFunction*>(context_->GetHandler()->GetUserData());
-
- VariantVector parameters;
- if (function->GetParamCount() > 0)
- {
- parameters.Push(Variant((void*)&eventType));
- parameters.Push(Variant((void*)&eventData));
- }
-
- Execute(function, parameters);
- }
- ScriptFile* GetScriptContextFile()
- {
- asIScriptContext* context = asGetActiveContext();
- asIScriptFunction* function = context->GetFunction();
- asIScriptModule* module = function->GetEngine()->GetModule(function->GetModuleName());
- Map<asIScriptModule*, ScriptFile*>& moduleMap = static_cast<Script*>(context->GetEngine()->GetUserData())->GetModuleMap();
- Map<asIScriptModule*, ScriptFile*>::ConstIterator i = moduleMap.Find(module);
- if (i != moduleMap.End())
- return i->second_;
- else
- return 0;
- }
|