| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- //
- // 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 <Atomic/Core/Context.h>
- #include <Atomic/IO/Deserializer.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Core/Profiler.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/IO/Serializer.h>
- #include <Atomic/Script/ScriptSystem.h>
- #include "NETScriptEvents.h"
- #include "CSComponentAssembly.h"
- namespace Atomic
- {
- HashMap<StringHash, VariantType> CSComponentAssembly::typeMap_;
- CSComponentAssembly::CSComponentAssembly(Context* context) :
- ScriptComponentFile(context)
- {
- }
- CSComponentAssembly::~CSComponentAssembly()
- {
- }
- void CSComponentAssembly::InitTypeMap()
- {
- typeMap_["Boolean"] = VAR_BOOL;
- typeMap_["Int32"] = VAR_INT;
- typeMap_["UInt32"] = VAR_INT;
- typeMap_["Single"] = VAR_FLOAT;
- typeMap_["Double"] = VAR_DOUBLE;
- typeMap_["String"] = VAR_STRING;
- typeMap_["Vector2"] = VAR_VECTOR2;
- typeMap_["Vector3"] = VAR_VECTOR3;
- typeMap_["Vector4"] = VAR_VECTOR4;
- typeMap_["Quaternion"] = VAR_QUATERNION;
- typeMap_["IntVector2"] = VAR_INTVECTOR2;
- }
- CSComponent* CSComponentAssembly::CreateCSComponent(const String& classname)
- {
- return nullptr;
- }
- bool CSComponentAssembly::ParseComponentClassJSON(const JSONValue& json)
- {
- if (!typeMap_.Size())
- InitTypeMap();
- String className = json.Get("name").GetString();
- classNames_.Push(className);
- const JSONValue& jfields = json.Get("fields");
- PODVector<StringHash> enumsAdded;
- if (jfields.IsArray())
- {
- for (unsigned i = 0; i < jfields.GetArray().Size(); i++)
- {
- const JSONValue& jfield = jfields.GetArray().At(i);
- VariantType varType = VAR_NONE;
- bool isEnum = jfield.Get("isEnum").GetBool();
- String typeName = jfield.Get("typeName").GetString();
- String fieldName = jfield.Get("name").GetString();
- String defaultValue = jfield.Get("defaultValue").GetString();
- String tooltip;
- if (!defaultValue.Length())
- {
- JSONArray caPos = jfield.Get("caPos").GetArray();
- if (caPos.Size())
- defaultValue = caPos[0].GetString();
- }
- JSONObject caNamed = jfield.Get("caNamed").GetObject();
- if (!defaultValue.Length())
- {
- if (caNamed.Contains("DefaultValue"))
- defaultValue = caNamed["DefaultValue"].GetString();
- }
- // tooltip
- if (caNamed.Contains("Tooltip"))
- {
- tooltip = caNamed["Tooltip"].GetString();
- }
- if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
- {
- varType = VAR_INT;
- enumsAdded.Push(fieldName);
- const Vector<EnumInfo>& einfos = assemblyEnums_[typeName];
- for (unsigned i = 0; i < einfos.Size(); i++)
- AddEnum(/*typeName*/fieldName, einfos[i], className);
- }
- if (varType == VAR_NONE && typeMap_.Contains(typeName))
- varType = typeMap_[typeName];
- if (varType == VAR_NONE)
- {
- // FIXME: We need to be able to test if a type is a ResourceRef, this isn't really the way to achieve that
- const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context_->GetObjectFactories();
- HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();
- while (itr != factories.End())
- {
- if (itr->second_->GetFactoryTypeName() == typeName)
- {
- varType = VAR_RESOURCEREF;
- break;
- }
- itr++;
- }
- if (varType == VAR_NONE)
- {
- ATOMIC_LOGERRORF("Component Class %s contains unmappable type %s in field %s",
- className.CString(), typeName.CString(), fieldName.CString());
- continue;
- }
- }
- if (!defaultValue.Length() && varType == VAR_RESOURCEREF)
- {
- // We still need a default value for ResourceRef's so we know the classtype
- AddDefaultValue(fieldName, ResourceRef(typeName), className);
- }
- else
- {
- Variant value;
- if (varType == VAR_RESOURCEREF)
- {
- ResourceRef rref(typeName);
- rref.name_ = defaultValue;
- value = rref;
- }
- else
- {
- value.FromString(varType, defaultValue);
- }
- AddDefaultValue(fieldName, value, className);
- }
- AddField(fieldName, varType, className, tooltip);
- }
- }
- return true;
- }
- bool CSComponentAssembly::ParseAssemblyJSON(const JSONValue& json)
- {
- Clear();
- assemblyEnums_.Clear();
- classNames_.Clear();
- const JSONArray& enums = json.Get("enums").GetArray();
- // parse to all enums hash
- for (unsigned i = 0; i < enums.Size(); i++)
- {
- const JSONValue& ejson = enums.At(i);
- String enumName = ejson.Get("name").GetString();
- const JSONObject& evalues = ejson.Get("values").GetObject();
- JSONObject::ConstIterator itr = evalues.Begin();
- Vector<EnumInfo> values;
- while (itr != evalues.End())
- {
- EnumInfo info;
- info.name_ = itr->first_;
- info.value_ = itr->second_.GetInt();
- values.Push(info);
- itr++;
- }
- assemblyEnums_[enumName] = values;
- }
- const JSONArray& components = json.Get("components").GetArray();
- for (unsigned i = 0; i < components.Size(); i++)
- {
- const JSONValue& cjson = components.At(i);
- ParseComponentClassJSON(cjson);
- }
- return true;
- }
- void CSComponentAssembly::RegisterObject(Context* context)
- {
- context->RegisterFactory<CSComponentAssembly>();
- }
- bool CSComponentAssembly::BeginLoad(Deserializer& source)
- {
- // TODO: Assemblies in packages?
- File* sourceFile = (File*) &source;
- fullAssemblyPath_ = sourceFile->GetFullPath();
- VariantMap eventData;
- using namespace CSComponentAssemblyReference;
- eventData[P_ASSEMBLYPATH] = fullAssemblyPath_;
- SendEvent(E_CSCOMPONENTASSEMBLYREFERENCE, eventData);
- return true;
- }
- bool CSComponentAssembly::Save(Serializer& dest) const
- {
- return true;
- }
- CSComponentAssembly* CSComponentAssembly::ResolveClassAssembly(const String& fullClassName)
- {
- Context* context = ScriptSystem::GetContext();
- assert(context);
- String classname = fullClassName;
- String csnamespace;
- // Handle namespaces
- if (fullClassName.Contains('.'))
- {
- StringVector elements = fullClassName.Split('.');
- if (elements.Size() <= 1)
- return 0;
- classname = elements.Back();
- elements.Pop();
- csnamespace = String::Joined(elements, ".");
- }
- ResourceCache* cache = context->GetSubsystem<ResourceCache>();
- PODVector<CSComponentAssembly*> assemblies;
- cache->GetResources<CSComponentAssembly>(assemblies);
- for (unsigned i = 0; i < assemblies.Size(); i++)
- {
- CSComponentAssembly* assembly = assemblies[i];
- // TODO: support namespaces
- const StringVector& classNames = assembly->GetClassNames();
- if (classNames.Contains(classname))
- {
- return assembly;
- }
- }
- return 0;
- }
- bool CSComponentAssembly::PreloadClassAssemblies()
- {
- // TEMPORARY SOLUTION, Desktop only
- ATOMIC_LOGINFO("Preloading Class Assemblies");
- Context* context = ScriptSystem::GetContext();
- assert(context);
- ResourceCache* cache = context->GetSubsystem<ResourceCache>();
- FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
- const StringVector& resourceDirs = cache->GetResourceDirs();
- for (unsigned i = 0; i < resourceDirs.Size(); i++)
- {
- const String& resourceDir = resourceDirs[i];
- ATOMIC_LOGINFOF("Scanning: %s", resourceDir.CString());
- StringVector results;
- fileSystem->ScanDir(results, resourceDir, "*.dll", SCAN_FILES, true);
- for (unsigned j = 0; j < results.Size(); j++)
- {
- // FIXME: This filtering is necessary as we're loading setting project root folder as a resource dir
- // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1037
- String filter = results[j].ToLower();
- if (filter.StartsWith("atomicnet/") || filter.StartsWith("resources/"))
- {
- ATOMIC_LOGINFOF("Skipping Assembly: %s (https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1037)", results[j].CString());
- continue;
- }
- ATOMIC_LOGINFOF("Loading Assembly: %s", results[j].CString());
- cache->GetResource<CSComponentAssembly>(results[j]);
- }
- }
- return true;
- }
- }
|