SceneObject* ObjectManager::cloneObject(SceneObject* object, bool makeTransient) { ObjectOutputStream objectData(500); (cast<ManagedObject*>(object))->writeObject(&objectData); objectData.reset(); ObjectInputStream objectInput; objectData.copy(&objectInput, 0); objectInput.reset(); uint32 serverCRC = object->getServerObjectCRC(); SceneObject* clonedObject = NULL; ObjectDatabase* database = getTable(object->getObjectID()); String databaseName; uint64 oid; if (database != NULL) { database->getDatabaseName(databaseName); oid = getNextObjectID(databaseName); } else oid = getNextFreeObjectID(); clonedObject = instantiateSceneObject(serverCRC, oid, false); if (makeTransient || !object->isPersistent()) { //clonedObject = createObject(serverCRC, 0, databaseName); clonedObject->setPersistent(0); } else if (object->isPersistent()) { //clonedObject = createObject(serverCRC, object->getPersistenceLevel(), databaseName); clonedObject->setPersistent(object->getPersistenceLevel()); } Locker locker(clonedObject); clonedObject->readObject(&objectInput); clonedObject->createComponents(); clonedObject->setParent(NULL); if (clonedObject->isPersistent()) updatePersistentObject(clonedObject); return clonedObject; }
SceneObject* ObjectManager::cloneObject(SceneObject* object, bool makeTransient) { ObjectOutputStream objectData(500); (cast<ManagedObject*>(object))->writeObject(&objectData); objectData.reset(); ObjectInputStream objectInput; objectData.copy(&objectInput, 0); objectInput.reset(); uint32 serverCRC = object->getServerObjectCRC(); SceneObject* clonedObject = NULL; ObjectDatabase* database = getTable(object->getObjectID()); String databaseName; uint64 oid; if (database != NULL) { database->getDatabaseName(databaseName); oid = getNextObjectID(databaseName); } else oid = getNextFreeObjectID(); clonedObject = instantiateSceneObject(serverCRC, oid, false); if (makeTransient || !object->isPersistent()) { //clonedObject = createObject(serverCRC, 0, databaseName); clonedObject->setPersistent(0); } else if (object->isPersistent()) { //clonedObject = createObject(serverCRC, object->getPersistenceLevel(), databaseName); clonedObject->setPersistent(object->getPersistenceLevel()); } Locker locker(clonedObject); clonedObject->readObject(&objectInput); clonedObject->createComponents(); clonedObject->setParent(NULL); VectorMap<String, ManagedReference<SceneObject*> > slottedObjects; clonedObject->getSlottedObjects(slottedObjects); for (int i=slottedObjects.size()-1; i>=0; i--) { String key = slottedObjects.elementAt(i).getKey(); Reference<SceneObject*> obj = slottedObjects.elementAt(i).getValue(); clonedObject->removeSlottedObject(i); Reference<SceneObject*> clonedChild = cloneObject(obj, makeTransient); clonedChild->setParent(object); slottedObjects.put(key, clonedChild); } VectorMap<uint64, ManagedReference<SceneObject*> > objects; clonedObject->getContainerObjects(objects); for (int i=objects.size()-1; i>=0; i--) { uint64 key = objects.elementAt(i).getKey(); Reference<SceneObject*> obj = objects.elementAt(i).getValue(); objects.remove(i); Reference<SceneObject*> clonedChild = cloneObject(obj, makeTransient); clonedChild->setParent(object); objects.put(key, clonedChild); } clonedObject->onCloneObject(object); if (clonedObject->isPersistent()) updatePersistentObject(clonedObject); return clonedObject; }