BsBinaryDiff.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsBinaryDiff.h"
  4. #include "BsSerializedObject.h"
  5. #include "BsBinarySerializer.h"
  6. #include "BsBinaryCloner.h"
  7. #include "BsRTTIType.h"
  8. namespace BansheeEngine
  9. {
  10. SPtr<SerializedObject> IDiff::generateDiff(const SPtr<SerializedObject>& orgObj,
  11. const SPtr<SerializedObject>& newObj)
  12. {
  13. ObjectMap objectMap;
  14. return generateDiff(orgObj, newObj, objectMap);
  15. }
  16. SPtr<SerializedInstance> IDiff::generateDiff(RTTITypeBase* rtti, UINT32 fieldType, const SPtr<SerializedInstance>& orgData,
  17. const SPtr<SerializedInstance>& newData, ObjectMap& objectMap)
  18. {
  19. SPtr<SerializedInstance> modification;
  20. switch (fieldType)
  21. {
  22. case SerializableFT_ReflectablePtr:
  23. case SerializableFT_Reflectable:
  24. {
  25. SPtr<SerializedObject> orgObjData = std::static_pointer_cast<SerializedObject>(orgData);
  26. SPtr<SerializedObject> newObjData = std::static_pointer_cast<SerializedObject>(newData);
  27. auto iterFind = objectMap.find(newObjData);
  28. if (iterFind != objectMap.end())
  29. modification = iterFind->second;
  30. else
  31. {
  32. RTTITypeBase* childRtti = nullptr;
  33. if (orgObjData->getRootTypeId() == newObjData->getRootTypeId())
  34. childRtti = IReflectable::_getRTTIfromTypeId(newObjData->getRootTypeId());
  35. SPtr<SerializedObject> objectDiff;
  36. if (childRtti != nullptr)
  37. {
  38. IDiff& handler = childRtti->getDiffHandler();
  39. objectDiff = handler.generateDiff(orgObjData, newObjData, objectMap);
  40. }
  41. if (objectDiff != nullptr)
  42. objectMap[newObjData] = objectDiff;
  43. modification = objectDiff;
  44. }
  45. }
  46. break;
  47. case SerializableFT_Plain:
  48. case SerializableFT_DataBlock:
  49. {
  50. SPtr<SerializedField> orgFieldData = std::static_pointer_cast<SerializedField>(orgData);
  51. SPtr<SerializedField> newFieldData = std::static_pointer_cast<SerializedField>(newData);
  52. bool isModified = orgFieldData->size != newFieldData->size;
  53. if (!isModified)
  54. isModified = memcmp(orgFieldData->value, newFieldData->value, newFieldData->size) != 0;
  55. if (isModified)
  56. modification = newFieldData->clone();
  57. }
  58. break;
  59. }
  60. return modification;
  61. }
  62. void IDiff::applyDiff(const SPtr<IReflectable>& object, const SPtr<SerializedObject>& diff)
  63. {
  64. Vector<DiffCommand> commands;
  65. DiffObjectMap objectMap;
  66. applyDiff(object, diff, objectMap, commands);
  67. IReflectable* destObject = nullptr;
  68. Stack<IReflectable*> objectStack;
  69. for (auto& command : commands)
  70. {
  71. bool isArray = (command.type & Diff_ArrayFlag) != 0;
  72. DiffCommandType type = (DiffCommandType)(command.type & 0xF);
  73. switch (type)
  74. {
  75. case Diff_ArraySize:
  76. command.field->setArraySize(destObject, command.arraySize);
  77. break;
  78. case Diff_ObjectStart:
  79. {
  80. destObject = command.object.get();
  81. objectStack.push(destObject);
  82. RTTITypeBase* curRtti = destObject->getRTTI();
  83. while (curRtti != nullptr)
  84. {
  85. curRtti->onDeserializationStarted(destObject);
  86. curRtti = curRtti->getBaseClass();
  87. }
  88. }
  89. break;
  90. case Diff_ObjectEnd:
  91. {
  92. Stack<RTTITypeBase*> rttiTypes;
  93. RTTITypeBase* curRtti = destObject->getRTTI();
  94. while (curRtti != nullptr)
  95. {
  96. rttiTypes.push(curRtti);
  97. curRtti = curRtti->getBaseClass();
  98. }
  99. while (!rttiTypes.empty())
  100. {
  101. rttiTypes.top()->onDeserializationEnded(destObject);
  102. rttiTypes.pop();
  103. }
  104. objectStack.pop();
  105. if (!objectStack.empty())
  106. destObject = objectStack.top();
  107. else
  108. destObject = nullptr;
  109. }
  110. break;
  111. default:
  112. break;
  113. }
  114. if (isArray)
  115. {
  116. switch (type)
  117. {
  118. case Diff_ReflectablePtr:
  119. {
  120. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(command.field);
  121. field->setArrayValue(destObject, command.arrayIdx, command.object);
  122. }
  123. break;
  124. case Diff_Reflectable:
  125. {
  126. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(command.field);
  127. field->setArrayValue(destObject, command.arrayIdx, *command.object);
  128. }
  129. break;
  130. case Diff_Plain:
  131. {
  132. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(command.field);
  133. field->arrayElemFromBuffer(destObject, command.arrayIdx, command.value);
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. else
  141. {
  142. switch (type)
  143. {
  144. case Diff_ReflectablePtr:
  145. {
  146. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(command.field);
  147. field->setValue(destObject, command.object);
  148. }
  149. break;
  150. case Diff_Reflectable:
  151. {
  152. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(command.field);
  153. field->setValue(destObject, *command.object);
  154. }
  155. break;
  156. case Diff_Plain:
  157. {
  158. RTTIPlainFieldBase* field = static_cast<RTTIPlainFieldBase*>(command.field);
  159. field->fromBuffer(destObject, command.value);
  160. }
  161. break;
  162. case Diff_DataBlock:
  163. {
  164. RTTIManagedDataBlockFieldBase* field = static_cast<RTTIManagedDataBlockFieldBase*>(command.field);
  165. UINT8* dataCopy = field->allocate(destObject, command.size);
  166. memcpy(dataCopy, command.value, command.size);
  167. ManagedDataBlock value(dataCopy, command.size); // Not managed because I assume the owner class will decide whether to delete the data or keep it
  168. field->setValue(destObject, value);
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. void IDiff::applyDiff(RTTITypeBase* rtti, const SPtr<IReflectable>& object, const SPtr<SerializedObject>& diff,
  178. DiffObjectMap& objectMap, Vector<DiffCommand>& diffCommands)
  179. {
  180. IDiff& diffHandler = rtti->getDiffHandler();
  181. diffHandler.applyDiff(object, diff, objectMap, diffCommands);
  182. }
  183. SPtr<SerializedObject> BinaryDiff::generateDiff(const SPtr<SerializedObject>& orgObj,
  184. const SPtr<SerializedObject>& newObj, ObjectMap& objectMap)
  185. {
  186. SPtr<SerializedObject> output;
  187. for (auto& subObject : newObj->subObjects)
  188. {
  189. RTTITypeBase* rtti = IReflectable::_getRTTIfromTypeId(subObject.typeId);
  190. if (rtti == nullptr)
  191. continue;
  192. SerializedSubObject* orgSubObject = nullptr;
  193. for (auto& curSubObject : orgObj->subObjects)
  194. {
  195. if (curSubObject.typeId == subObject.typeId)
  196. {
  197. orgSubObject = &curSubObject;
  198. break;
  199. }
  200. }
  201. SerializedSubObject* diffSubObject = nullptr;
  202. for (auto& newEntry : subObject.entries)
  203. {
  204. RTTIField* genericField = rtti->findField(newEntry.first);
  205. if (genericField == nullptr)
  206. continue;
  207. SPtr<SerializedInstance> newEntryData = newEntry.second.serialized;
  208. SPtr<SerializedInstance> orgEntryData;
  209. if (orgSubObject != nullptr)
  210. {
  211. auto orgEntryFind = orgSubObject->entries.find(newEntry.first);
  212. if (orgEntryFind != orgSubObject->entries.end())
  213. orgEntryData = orgEntryFind->second.serialized;
  214. }
  215. SPtr<SerializedInstance> modification;
  216. bool hasModification = false;
  217. if (genericField->isArray())
  218. {
  219. SPtr<SerializedArray> orgArrayData = std::static_pointer_cast<SerializedArray>(orgEntryData);
  220. SPtr<SerializedArray> newArrayData = std::static_pointer_cast<SerializedArray>(newEntryData);
  221. SPtr<SerializedArray> serializedArray;
  222. if (newEntryData != nullptr && orgEntryData != nullptr)
  223. {
  224. for (auto& arrayEntryPair : newArrayData->entries)
  225. {
  226. SPtr<SerializedInstance> arrayModification;
  227. auto iterFind = orgArrayData->entries.find(arrayEntryPair.first);
  228. if (iterFind == orgArrayData->entries.end())
  229. arrayModification = arrayEntryPair.second.serialized->clone();
  230. else
  231. {
  232. arrayModification = IDiff::generateDiff(rtti, genericField->mType, iterFind->second.serialized,
  233. arrayEntryPair.second.serialized, objectMap);
  234. }
  235. if (arrayModification != nullptr)
  236. {
  237. if (serializedArray == nullptr)
  238. {
  239. serializedArray = bs_shared_ptr_new<SerializedArray>();
  240. serializedArray->numElements = newArrayData->numElements;
  241. }
  242. SerializedArrayEntry arrayEntry;
  243. arrayEntry.index = arrayEntryPair.first;
  244. arrayEntry.serialized = arrayModification;
  245. serializedArray->entries[arrayEntryPair.first] = arrayEntry;
  246. }
  247. }
  248. }
  249. else if (newEntryData == nullptr)
  250. {
  251. serializedArray = bs_shared_ptr_new<SerializedArray>();
  252. }
  253. else if (orgEntryData == nullptr)
  254. {
  255. serializedArray = std::static_pointer_cast<SerializedArray>(newArrayData->clone());
  256. }
  257. modification = serializedArray;
  258. hasModification = modification != nullptr;
  259. }
  260. else
  261. {
  262. if (newEntryData != nullptr && orgEntryData != nullptr)
  263. {
  264. modification = IDiff::generateDiff(rtti, genericField->mType, orgEntryData, newEntryData, objectMap);
  265. hasModification = modification != nullptr;
  266. }
  267. else if (newEntryData == nullptr)
  268. {
  269. switch (genericField->mType)
  270. {
  271. case SerializableFT_Plain:
  272. case SerializableFT_DataBlock:
  273. modification = bs_shared_ptr_new<SerializedField>();
  274. break;
  275. default:
  276. break;
  277. }
  278. hasModification = true;
  279. }
  280. else if (orgEntryData == nullptr)
  281. {
  282. modification = newEntryData->clone();
  283. hasModification = modification != nullptr;
  284. }
  285. }
  286. if (hasModification)
  287. {
  288. if (output == nullptr)
  289. output = bs_shared_ptr_new<SerializedObject>();
  290. if (diffSubObject == nullptr)
  291. {
  292. output->subObjects.push_back(SerializedSubObject());
  293. diffSubObject = &output->subObjects.back();
  294. diffSubObject->typeId = rtti->getRTTIId();
  295. }
  296. SerializedEntry modificationEntry;
  297. modificationEntry.fieldId = genericField->mUniqueId;
  298. modificationEntry.serialized = modification;
  299. diffSubObject->entries[genericField->mUniqueId] = modificationEntry;
  300. }
  301. }
  302. }
  303. return output;
  304. }
  305. void BinaryDiff::applyDiff(const SPtr<IReflectable>& object, const SPtr<SerializedObject>& diff,
  306. DiffObjectMap& objectMap, Vector<DiffCommand>& diffCommands)
  307. {
  308. if (object == nullptr || diff == nullptr || object->getTypeId() != diff->getRootTypeId())
  309. return;
  310. DiffCommand objStartCommand;
  311. objStartCommand.field = nullptr;
  312. objStartCommand.type = Diff_ObjectStart;
  313. objStartCommand.object = object;
  314. diffCommands.push_back(objStartCommand);
  315. Stack<RTTITypeBase*> rttiTypes;
  316. for (auto& subObject : diff->subObjects)
  317. {
  318. for (auto& diffEntry : subObject.entries)
  319. {
  320. RTTITypeBase* rtti = IReflectable::_getRTTIfromTypeId(subObject.typeId);
  321. if (rtti == nullptr)
  322. continue;
  323. if (!object->isDerivedFrom(rtti))
  324. continue;
  325. rtti->onSerializationStarted(object.get());
  326. rttiTypes.push(rtti);
  327. RTTIField* genericField = rtti->findField(diffEntry.first);
  328. if (genericField == nullptr)
  329. continue;
  330. SPtr<SerializedInstance> diffData = diffEntry.second.serialized;
  331. if (genericField->isArray())
  332. {
  333. SPtr<SerializedArray> diffArray = std::static_pointer_cast<SerializedArray>(diffData);
  334. UINT32 numArrayElements = (UINT32)diffArray->numElements;
  335. DiffCommand arraySizeCommand;
  336. arraySizeCommand.field = genericField;
  337. arraySizeCommand.type = Diff_ArraySize | Diff_ArrayFlag;
  338. arraySizeCommand.arraySize = numArrayElements;
  339. diffCommands.push_back(arraySizeCommand);
  340. switch (genericField->mType)
  341. {
  342. case SerializableFT_ReflectablePtr:
  343. {
  344. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  345. UINT32 orgArraySize = genericField->getArraySize(object.get());
  346. for (auto& arrayElem : diffArray->entries)
  347. {
  348. SPtr<SerializedObject> arrayElemData = std::static_pointer_cast<SerializedObject>(arrayElem.second.serialized);
  349. DiffCommand command;
  350. command.field = genericField;
  351. command.type = Diff_ReflectablePtr | Diff_ArrayFlag;
  352. command.arrayIdx = arrayElem.first;
  353. if (arrayElemData == nullptr)
  354. {
  355. command.object = nullptr;
  356. diffCommands.push_back(command);
  357. }
  358. else
  359. {
  360. bool needsNewObject = arrayElem.first >= orgArraySize;
  361. if (!needsNewObject)
  362. {
  363. SPtr<IReflectable> childObj = field->getArrayValue(object.get(), arrayElem.first);
  364. if (childObj != nullptr)
  365. {
  366. IDiff::applyDiff(childObj->getRTTI(), childObj, arrayElemData, objectMap, diffCommands);
  367. command.object = childObj;
  368. }
  369. else
  370. needsNewObject = true;
  371. }
  372. if (needsNewObject)
  373. {
  374. RTTITypeBase* childRtti = IReflectable::_getRTTIfromTypeId(arrayElemData->getRootTypeId());
  375. if (childRtti != nullptr)
  376. {
  377. auto findObj = objectMap.find(arrayElemData);
  378. if (findObj == objectMap.end())
  379. {
  380. SPtr<IReflectable> newObject = childRtti->newRTTIObject();
  381. findObj = objectMap.insert(std::make_pair(arrayElemData, newObject)).first;
  382. }
  383. IDiff::applyDiff(childRtti, findObj->second, arrayElemData, objectMap, diffCommands);
  384. command.object = findObj->second;
  385. diffCommands.push_back(command);
  386. }
  387. else
  388. {
  389. command.object = nullptr;
  390. diffCommands.push_back(command);
  391. }
  392. }
  393. }
  394. }
  395. }
  396. break;
  397. case SerializableFT_Reflectable:
  398. {
  399. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  400. UINT32 orgArraySize = genericField->getArraySize(object.get());
  401. Vector<SPtr<IReflectable>> newArrayElements(numArrayElements);
  402. UINT32 minArrayLength = std::min(orgArraySize, numArrayElements);
  403. for (UINT32 i = 0; i < minArrayLength; i++)
  404. {
  405. IReflectable& childObj = field->getArrayValue(object.get(), i);
  406. newArrayElements[i] = BinaryCloner::clone(&childObj, true);
  407. }
  408. for (auto& arrayElem : diffArray->entries)
  409. {
  410. SPtr<SerializedObject> arrayElemData = std::static_pointer_cast<SerializedObject>(arrayElem.second.serialized);
  411. if (arrayElem.first < orgArraySize)
  412. {
  413. SPtr<IReflectable> childObj = newArrayElements[arrayElem.first];
  414. IDiff::applyDiff(childObj->getRTTI(), childObj, arrayElemData, objectMap, diffCommands);
  415. }
  416. else
  417. {
  418. RTTITypeBase* childRtti = IReflectable::_getRTTIfromTypeId(arrayElemData->getRootTypeId());
  419. if (childRtti != nullptr)
  420. {
  421. SPtr<IReflectable> newObject = childRtti->newRTTIObject();
  422. IDiff::applyDiff(childRtti, newObject, arrayElemData, objectMap, diffCommands);
  423. newArrayElements[arrayElem.first] = newObject;
  424. }
  425. }
  426. }
  427. for (UINT32 i = 0; i < numArrayElements; i++)
  428. {
  429. DiffCommand command;
  430. command.field = genericField;
  431. command.type = Diff_Reflectable | Diff_ArrayFlag;
  432. command.arrayIdx = i;
  433. command.object = newArrayElements[i];
  434. diffCommands.push_back(command);
  435. }
  436. }
  437. break;
  438. case SerializableFT_Plain:
  439. {
  440. for (auto& arrayElem : diffArray->entries)
  441. {
  442. SPtr<SerializedField> fieldData = std::static_pointer_cast<SerializedField>(arrayElem.second.serialized);
  443. if (fieldData != nullptr)
  444. {
  445. DiffCommand command;
  446. command.field = genericField;
  447. command.type = Diff_Plain | Diff_ArrayFlag;
  448. command.value = fieldData->value;
  449. command.size = fieldData->size;
  450. command.arrayIdx = arrayElem.first;
  451. diffCommands.push_back(command);
  452. }
  453. }
  454. }
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. else
  461. {
  462. switch (genericField->mType)
  463. {
  464. case SerializableFT_ReflectablePtr:
  465. {
  466. RTTIReflectablePtrFieldBase* field = static_cast<RTTIReflectablePtrFieldBase*>(genericField);
  467. SPtr<SerializedObject> fieldObjectData = std::static_pointer_cast<SerializedObject>(diffData);
  468. DiffCommand command;
  469. command.field = genericField;
  470. command.type = Diff_ReflectablePtr;
  471. if (fieldObjectData == nullptr)
  472. command.object = nullptr;
  473. else
  474. {
  475. SPtr<IReflectable> childObj = field->getValue(object.get());
  476. if (childObj == nullptr)
  477. {
  478. RTTITypeBase* childRtti = IReflectable::_getRTTIfromTypeId(fieldObjectData->getRootTypeId());
  479. if (childRtti != nullptr)
  480. {
  481. auto findObj = objectMap.find(fieldObjectData);
  482. if (findObj == objectMap.end())
  483. {
  484. SPtr<IReflectable> newObject = childRtti->newRTTIObject();
  485. findObj = objectMap.insert(std::make_pair(fieldObjectData, newObject)).first;
  486. }
  487. IDiff::applyDiff(childRtti, findObj->second, fieldObjectData, objectMap, diffCommands);
  488. command.object = findObj->second;
  489. }
  490. else
  491. {
  492. command.object = nullptr;
  493. }
  494. }
  495. else
  496. {
  497. IDiff::applyDiff(childObj->getRTTI(), childObj, fieldObjectData, objectMap, diffCommands);
  498. command.object = childObj;
  499. }
  500. }
  501. diffCommands.push_back(command);
  502. }
  503. break;
  504. case SerializableFT_Reflectable:
  505. {
  506. RTTIReflectableFieldBase* field = static_cast<RTTIReflectableFieldBase*>(genericField);
  507. SPtr<SerializedObject> fieldObjectData = std::static_pointer_cast<SerializedObject>(diffData);
  508. IReflectable& childObj = field->getValue(object.get());
  509. std::shared_ptr<IReflectable> clonedObj = BinaryCloner::clone(&childObj, true);
  510. IDiff::applyDiff(clonedObj->getRTTI(), clonedObj, fieldObjectData, objectMap, diffCommands);
  511. DiffCommand command;
  512. command.field = genericField;
  513. command.type = Diff_Reflectable;
  514. command.object = clonedObj;
  515. diffCommands.push_back(command);
  516. }
  517. break;
  518. case SerializableFT_Plain:
  519. {
  520. SPtr<SerializedField> diffFieldData = std::static_pointer_cast<SerializedField>(diffData);
  521. if (diffFieldData->size > 0)
  522. {
  523. DiffCommand command;
  524. command.field = genericField;
  525. command.type = Diff_Plain;
  526. command.value = diffFieldData->value;
  527. command.size = diffFieldData->size;
  528. diffCommands.push_back(command);
  529. }
  530. }
  531. break;
  532. case SerializableFT_DataBlock:
  533. {
  534. SPtr<SerializedField> diffFieldData = std::static_pointer_cast<SerializedField>(diffData);
  535. DiffCommand command;
  536. command.field = genericField;
  537. command.type = Diff_DataBlock;
  538. command.value = diffFieldData->value;
  539. command.size = diffFieldData->size;
  540. diffCommands.push_back(command);
  541. }
  542. break;
  543. }
  544. }
  545. }
  546. }
  547. DiffCommand objEndCommand;
  548. objEndCommand.field = nullptr;
  549. objEndCommand.type = Diff_ObjectEnd;
  550. objEndCommand.object = object;
  551. diffCommands.push_back(objEndCommand);
  552. while (!rttiTypes.empty())
  553. {
  554. rttiTypes.top()->onSerializationEnded(object.get());
  555. rttiTypes.pop();
  556. }
  557. }
  558. }