From 87e11231e97edd31a12cd790df08c6d9bfbd894d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabri=C3=ABl=20Konat?= Date: Wed, 20 Jul 2011 18:30:11 +0200 Subject: [PATCH] Changed lua code generation so that it gets a child object instead of creating one, since this is now done by the engine using the XML code generation. --- .../trans/codegen/lua/generate.str | 9 ++++++- DSL/DiversiaScript/trans/desugar.str | 4 +-- DSL/DiversiaScript/trans/projection/type.str | 1 + DSL/DiversiaScript/trans/signatures.str | 7 +++-- Framework/Client/Camp/CampBindings.cpp | 4 ++- .../Object/ClientObjectTemplateManager.cpp | 8 ++++++ .../Object/ClientObjectTemplateManager.h | 12 +++++++++ Framework/Object/Camp/CampBindings.cpp | 1 + Framework/Object/Object.cpp | 26 ++++++++++++++----- Framework/Object/Object.h | 13 +++++++--- Framework/Object/ObjectManager.cpp | 8 +++--- Framework/Object/ObjectTemplate.cpp | 4 +-- 12 files changed, 76 insertions(+), 21 deletions(-) diff --git a/DSL/DiversiaScript/trans/codegen/lua/generate.str b/DSL/DiversiaScript/trans/codegen/lua/generate.str index a677ff7..96a56b0 100644 --- a/DSL/DiversiaScript/trans/codegen/lua/generate.str +++ b/DSL/DiversiaScript/trans/codegen/lua/generate.str @@ -350,7 +350,14 @@ rules // Statements to-lua: Expression(n@ObjectNew(ObjectRef(name), _)) -> name]])> n to-lua: VarDef(_, _{varName}, _, n@ObjectNew(_, _)) -> n to-lua: PropDef(_, varName, _, n@ObjectNew(_, _)) -> n - to-lua: Assign(VarRef(_{varName}), n@ObjectNew(_, _)) -> n + to-lua: Assign(VarRef(_{varName}), n@ObjectNew(_, _)) -> n + to-lua: PropDef(_, objectName, _, ObjectGet(ObjectRef(name))) -> [ + Assignment([VarRef(objectName)], [Call(VarRef("Object"), "ChildObjectByDisplayName", Args([String(name)]))]), + Assignment([VarRef(luaScriptName)], [Call(VarRef(name), "GetComponent", Args([String("LuaObjectScript")]))]), + Call(VarRef(luaScriptName), "CreateEnv", Args([])), + Call(VarRef(luaScriptName), "ReplaceVarWithThisEnv", Args([Access(VarRef("Script"), "ClientEnvironmentName"), String(objectName)])) + ] + where luaScriptName := $[[objectName]__LuaObjectScript] to-lua: StateChange(StateRef(name)) -> [exitCall, change, enterCall] where exitCall := Call(TableVarRef(VarRef("_G"), Concat(String("StateExit_"), )), Args([])) where change := Assignment([], [String(name)]) diff --git a/DSL/DiversiaScript/trans/desugar.str b/DSL/DiversiaScript/trans/desugar.str index 9b3b9cc..59b8ec6 100644 --- a/DSL/DiversiaScript/trans/desugar.str +++ b/DSL/DiversiaScript/trans/desugar.str @@ -121,10 +121,10 @@ rules // Child object where ( objectDefs, varDefs) => varDefs' object-to-var: ObjectDef(name, propAssigns, propDefs, componentDefs, objectDefs , varDefs, defaultStateDef, stateDefs, eventDefs) -> - PropDef(name, CustomType(name), ObjectNew(ObjectRef(name), [BuiltinObjectProp(Parent(), This())])) + PropDef(name, CustomType(name), ObjectGet(ObjectRef(name))) object-to-var: ExternalObjectDef(name) -> - PropDef(name, CustomType(name), ObjectNew(ObjectRef(name), [BuiltinObjectProp(Parent(), This())])) + PropDef(name, CustomType(name), ObjectGet(ObjectRef(name))) /* // TODO need to following type rules to do type inf. diff --git a/DSL/DiversiaScript/trans/projection/type.str b/DSL/DiversiaScript/trans/projection/type.str index cf4508d..207fa0d 100644 --- a/DSL/DiversiaScript/trans/projection/type.str +++ b/DSL/DiversiaScript/trans/projection/type.str @@ -128,6 +128,7 @@ rules // Expressions type-of: ObjectBuiltinCall(objectProp, func, _) -> type where type := ClientObject() type-of: ObjectNew(objectRef, _) -> objectRef + type-of: ObjectGet(objectRef) -> objectRef type-of: ObjectRef(name) -> name type-of: ComponentRef(name) -> name type-of: PluginRef(name) -> name diff --git a/DSL/DiversiaScript/trans/signatures.str b/DSL/DiversiaScript/trans/signatures.str index c1f6e2a..0863cee 100644 --- a/DSL/DiversiaScript/trans/signatures.str +++ b/DSL/DiversiaScript/trans/signatures.str @@ -35,10 +35,13 @@ signature constructors // Structure StateDef: Id * List(ObjectProp) * List(ComponentDef) * List(VarDef) * List(EventDef) -> StateDef DefaultStateDef: Id * List(ObjectProp) * List(ComponentDef) * List(VarDef) * List(EventDef) -> StateDef + +signature constructors // Object get instruction + + ObjectGet: ObjectRef -> ObjectGet signature constructors // Types - - //EnumType: Id -> Type + NullType: Type signature constructors // Variables & properties diff --git a/Framework/Client/Camp/CampBindings.cpp b/Framework/Client/Camp/CampBindings.cpp index 0c95ca9..c13a297 100644 --- a/Framework/Client/Camp/CampBindings.cpp +++ b/Framework/Client/Camp/CampBindings.cpp @@ -216,11 +216,13 @@ void CampBindings::bindClientObjectTemplateManager() camp::Class::declare( "ClientObjectTemplateManager" ) .tag( "NoLevelSerialization", true ) .base() - .base(); + .base() // Constructors // Properties (read-only) // Properties (read/write) // Functions + .function( "CreateLocalObject", boost::function( boost::bind( &ClientObjectTemplateManager::createObject, _1, _2, _3, LOCAL ) ) ) + .function( "CreateRemoteObject", boost::function( boost::bind( &ClientObjectTemplateManager::createObject, _1, _2, _3, REMOTE ) ) ); // Static functions // Operators } diff --git a/Framework/Client/Object/ClientObjectTemplateManager.cpp b/Framework/Client/Object/ClientObjectTemplateManager.cpp index 4fd65b3..c7678b3 100644 --- a/Framework/Client/Object/ClientObjectTemplateManager.cpp +++ b/Framework/Client/Object/ClientObjectTemplateManager.cpp @@ -28,6 +28,7 @@ THE SOFTWARE. #include "Client/Plugin/ClientPluginManager.h" #include "Client/Lua/LuaPlugin.h" +#include "Client/Object/ClientObjectManager.h" #include "Client/Object/ClientObjectTemplate.h" #include "Client/Object/ClientObjectTemplateManager.h" #include "Client/Permission/PermissionManager.h" @@ -64,6 +65,13 @@ ClientObjectTemplateManager::~ClientObjectTemplateManager() } +Object& ClientObjectTemplateManager::createObject( const String& rTemplateName, + const String& rObjectName, NetworkingType type ) +{ + return ObjectTemplateManager::getObjectTemplate( rTemplateName ).createObject( + Plugin::getPluginManager().getPlugin(), rObjectName, type ); +} + void ClientObjectTemplateManager::load() { // TODO: Implement real loading completed signal diff --git a/Framework/Client/Object/ClientObjectTemplateManager.h b/Framework/Client/Object/ClientObjectTemplateManager.h index 32748ad..a695fce 100644 --- a/Framework/Client/Object/ClientObjectTemplateManager.h +++ b/Framework/Client/Object/ClientObjectTemplateManager.h @@ -53,6 +53,18 @@ class DIVERSIA_CLIENT_API ClientObjectTemplateManager : public ObjectTemplateMan **/ inline String getTypeName() const { return PLUGINNAME_OBJECTTEMPLATEMANAGER; } static inline String getTypeNameStatic() { return PLUGINNAME_OBJECTTEMPLATEMANAGER; } + + /** + Creates an object with the parameters and component templates from given object template name. + + @param rTemplateName The name of the object template to instantiate. + @param rObjectName The name of the object. + @param type If the object should be a remote or local object. + + @return The created object. + **/ + Object& createObject( const String& rTemplateName, const String& rObjectName, + NetworkingType type ); private: friend class TemplatePluginFactory; diff --git a/Framework/Object/Camp/CampBindings.cpp b/Framework/Object/Camp/CampBindings.cpp index a186233..c6d9fcc 100644 --- a/Framework/Object/Camp/CampBindings.cpp +++ b/Framework/Object/Camp/CampBindings.cpp @@ -176,6 +176,7 @@ void CampBindings::bindObject() .function( "IsCreatedBy", &Object::isCreatedBy ) .function( "IsCreatedBySource", &Object::isCreatedBySource ) .function( "CreateChildObject", &Object::createChildObject ) + .function( "ChildObjectByDisplayName", &Object::childObjectByDisplayName ) .function( "CreateComponent", boost::function( boost::bind( &Object::createComponent, _1, _2, _3, _4, RakNet::RakNetGUID( 0 ) ) ) ) .function( "CreateComponentByHandle", (Component&(Object::*)(const ComponentHandle&))&Object::createComponent ) .function( "GetComponent", (Component&(Object::*)(const String&) const)&Object::getComponent ) diff --git a/Framework/Object/Object.cpp b/Framework/Object/Object.cpp index 7d31def..0c2a23a 100644 --- a/Framework/Object/Object.cpp +++ b/Framework/Object/Object.cpp @@ -158,8 +158,8 @@ void Object::setNetworkingType( NetworkingType type, bool redirectToChilds /*=fa // type for all childs. if( !Node::hasParent() || redirectToChilds ) { - ObjectChilds childs = Object::getChildObjects(); - for( ObjectChilds::iterator i = childs.begin(); i != childs.end(); ++i ) + ObjectHashMap childs = Object::getChildObjects(); + for( ObjectHashMap::iterator i = childs.begin(); i != childs.end(); ++i ) { // Set redirectToChilds to true so that the networking type will be set across the // whole tree of objects. @@ -212,8 +212,8 @@ void Object::querySetNetworkingType( NetworkingType type, bool redirectToChilds // type can be changed for all childs. if( !Node::hasParent() || redirectToChilds ) { - ObjectChilds childs = Object::getChildObjects(); - for( ObjectChilds::iterator j = childs.begin(); j != childs.end(); ++j ) + ObjectHashMap childs = Object::getChildObjects(); + for( ObjectHashMap::iterator j = childs.begin(); j != childs.end(); ++j ) { // Set redirectToChilds to true so that the networking type will be set across the // whole tree of objects. @@ -260,8 +260,8 @@ Object& Object::duplicate( const String& rName /*= ""*/ ) object.setScale( Node::getScale() ); // Duplicate child objects - ObjectChilds childs = Object::getChildObjects(); - for( ObjectChilds::iterator i = childs.begin(); i != childs.end(); ++i ) + ObjectHashMap childs = Object::getChildObjects(); + for( ObjectHashMap::iterator i = childs.begin(); i != childs.end(); ++i ) { i->second->duplicate().parent( &object ); } @@ -532,6 +532,20 @@ String Object::getParentName() const return String(); } +Object& Object::childObjectByDisplayName( const String& rDisplayName ) +{ + ChildNodeMap childs = Node::getChildren(); + for( ChildNodeMap::iterator i = childs.begin(); i != childs.end(); ++i ) + { + Object* object = static_cast( i->second ); + if( object->getDisplayName() == rDisplayName ) return *object; + } + + DIVERSIA_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, + "Child object with display name " + rDisplayName + " does not exist in object " + mName, + "Object::childObjectByDisplayName" ); +} + void Object::setTemplate( ObjectTemplate* pTemplate ) { mTemplate = pTemplate; diff --git a/Framework/Object/Object.h b/Framework/Object/Object.h index acf7047..9e55eab 100644 --- a/Framework/Object/Object.h +++ b/Framework/Object/Object.h @@ -36,7 +36,7 @@ namespace ObjectSystem { //------------------------------------------------------------------------------ -typedef DiversiaHashMap ObjectChilds; +typedef DiversiaHashMap ObjectHashMap; typedef std::multimap ComponentsByType; typedef std::map ComponentsByName; @@ -456,10 +456,10 @@ class DIVERSIA_OBJECT_API Object : public RakNet::Replica3, public Node, public /** Gets a copy of the childs map. **/ - inline ObjectChilds getChildObjects() const + inline ObjectHashMap getChildObjects() const { ChildNodeMap childs = Node::getChildren(); - ObjectChilds objectChilds; + ObjectHashMap objectChilds; for( ChildNodeMap::iterator i = childs.begin(); i != childs.end(); ++i ) { objectChilds.insert( std::make_pair( i->first, static_cast( i->second ) ) ); @@ -467,6 +467,13 @@ class DIVERSIA_OBJECT_API Object : public RakNet::Replica3, public Node, public return objectChilds; } /** + Gets a child object with given display name. If multiple childs have the same display name the + first one is returned. + + @param rDisplayName Display name of the object to get. + **/ + Object& childObjectByDisplayName( const String& rDisplayName ); + /** Sets the parent object for this object using a pointer to the parent object. @param pObject The new parent object, or 0 to unparent. diff --git a/Framework/Object/ObjectManager.cpp b/Framework/Object/ObjectManager.cpp index 4dfaf28..8b595ec 100644 --- a/Framework/Object/ObjectManager.cpp +++ b/Framework/Object/ObjectManager.cpp @@ -135,8 +135,8 @@ void ObjectManager::destroyObjectTree( Object& rObject, // Destroy all childs. if( !rObject.hasParent() || redirectToChilds ) { - ObjectChilds childs = rObject.getChildObjects(); - for( ObjectChilds::iterator i = childs.begin(); i != childs.end(); ++i ) + ObjectHashMap childs = rObject.getChildObjects(); + for( ObjectHashMap::iterator i = childs.begin(); i != childs.end(); ++i ) { // Set redirectToChilds to true so that the call is forwarded to all childs. ObjectManager::destroyWholeObjectTree( *i->second, source, true ); @@ -160,8 +160,8 @@ void ObjectManager::destroyWholeObjectTree( Object& rObject, // Destroy all childs. if( !rObject.hasParent() || redirectToChilds ) { - ObjectChilds childs = rObject.getChildObjects(); - for( ObjectChilds::iterator i = childs.begin(); i != childs.end(); ++i ) + ObjectHashMap childs = rObject.getChildObjects(); + for( ObjectHashMap::iterator i = childs.begin(); i != childs.end(); ++i ) { // Set redirectToChilds to true so that the call is forwarded to all childs. ObjectManager::destroyWholeObjectTree( *i->second, source, true ); diff --git a/Framework/Object/ObjectTemplate.cpp b/Framework/Object/ObjectTemplate.cpp index 4cc4ccc..9dc509a 100644 --- a/Framework/Object/ObjectTemplate.cpp +++ b/Framework/Object/ObjectTemplate.cpp @@ -268,8 +268,8 @@ void ObjectTemplate::createComponentTemplates( const Object& rObject ) Node::setScale( rObject.getScale() ); // Recursively go trough all child objects. - ObjectChilds childs = rObject.getChildObjects(); - for( ObjectChilds::iterator i = childs.begin(); i != childs.end(); ++i ) + ObjectHashMap childs = rObject.getChildObjects(); + for( ObjectHashMap::iterator i = childs.begin(); i != childs.end(); ++i ) { mObjectTemplateManager.createObjectTemplate( *i->second, mObjectTemplateManager.generateName(), mType ).parent( this );