Przeglądaj źródła

Remove code duplication and avoid unnecessary AttributeInfo search.

Yao Wei Tjong 姚伟忠 11 lat temu
rodzic
commit
1b49f15ca5
1 zmienionych plików z 14 dodań i 21 usunięć
  1. 14 21
      Source/Engine/Scene/Node.cpp

+ 14 - 21
Source/Engine/Scene/Node.cpp

@@ -748,7 +748,7 @@ Component* Node::CloneComponent(Component* component, CreateMode mode, unsigned
         return 0;
     }
 
-    Component* cloneComponent = CreateComponent(component->GetType(), mode, 0);
+    Component* cloneComponent = SafeCreateComponent(component->GetTypeName(), component->GetType(), mode, 0);
     if (!cloneComponent)
     {
         LOGERROR("Could not clone component " + component->GetTypeName());
@@ -762,7 +762,11 @@ Component* Node::CloneComponent(Component* component, CreateMode mode, unsigned
         {
             const AttributeInfo& attr = compAttributes->At(j);
             if (attr.mode_ & AM_FILE)
-                cloneComponent->SetAttribute(j, component->GetAttribute(j));
+            {
+                Variant value;
+                component->OnGetAttribute(attr, value);
+                cloneComponent->OnSetAttribute(attr, value);
+            }
         }
     }
     
@@ -1612,32 +1616,21 @@ Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mod
         const AttributeInfo& attr = attributes->At(j);
         // Do not copy network-only attributes, as they may have unintended side effects
         if (attr.mode_ & AM_FILE)
-            cloneNode->SetAttribute(j, GetAttribute(j));
+        {
+            Variant value;
+            OnGetAttribute(attr, value);
+            cloneNode->OnSetAttribute(attr, value);
+        }
     }
 
     // Clone components
     for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
     {
         Component* component = *i;
-        Component* cloneComponent = cloneNode->SafeCreateComponent(component->GetTypeName(), component->GetType(),
+        Component* cloneComponent = cloneNode->CloneComponent(component,
             (mode == REPLICATED && component->GetID() < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, 0);
-        if (!cloneComponent)
-        {
-            LOGERROR("Could not clone component " + component->GetTypeName());
-            continue;
-        }
-        resolver.AddComponent(component->GetID(), cloneComponent);
-
-        const Vector<AttributeInfo>* compAttributes = component->GetAttributes();
-        if (compAttributes)
-        {
-            for (unsigned j = 0; j < compAttributes->Size(); ++j)
-            {
-                const AttributeInfo& attr = compAttributes->At(j);
-                if (attr.mode_ & AM_FILE)
-                    cloneComponent->SetAttribute(j, component->GetAttribute(j));
-            }
-        }
+        if (cloneComponent)
+            resolver.AddComponent(component->GetID(), cloneComponent);
     }
 
     // Clone child nodes recursively