ObjectStreamIn.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Core/Factory.h>
  5. #include <Jolt/ObjectStream/SerializableAttribute.h>
  6. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  7. #include <Jolt/ObjectStream/ObjectStreamTextIn.h>
  8. #include <Jolt/ObjectStream/ObjectStreamBinaryIn.h>
  9. #include <Jolt/ObjectStream/SerializableObject.h>
  10. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  11. #include <unordered_set>
  12. JPH_SUPPRESS_WARNINGS_STD_END
  13. JPH_NAMESPACE_BEGIN
  14. ObjectStreamIn::ObjectStreamIn(istream &inStream) :
  15. mStream(inStream)
  16. {
  17. }
  18. bool ObjectStreamIn::GetInfo(istream &inStream, EStreamType &outType, int &outVersion, int &outRevision)
  19. {
  20. // Read header and check if it is the correct format, e.g. "TOS 1.00"
  21. char header[9];
  22. memset(header, 0, 9);
  23. inStream.read(header, 8);
  24. if ((header[0] == 'B' || header[0] == 'T') && header[1] == 'O' && header[2] == 'S'
  25. && (header[3] == ' ' || isdigit(header[3])) && isdigit(header[4])
  26. && header[5] == '.' && isdigit(header[6]) && isdigit(header[7]))
  27. {
  28. // Check if this is a binary or text objectfile
  29. switch (header[0])
  30. {
  31. case 'T': outType = ObjectStream::EStreamType::Text; break;
  32. case 'B': outType = ObjectStream::EStreamType::Binary; break;
  33. default: JPH_ASSERT(false); break;
  34. }
  35. // Extract version and revision
  36. header[5] = '\0';
  37. outVersion = atoi(&header[3]);
  38. outRevision = atoi(&header[6]);
  39. return true;
  40. }
  41. Trace("ObjectStreamIn: Not a valid object stream.");
  42. return false;
  43. }
  44. ObjectStreamIn *ObjectStreamIn::Open(istream &inStream)
  45. {
  46. // Check if file is an ObjectStream of the correct version and revision
  47. EStreamType type;
  48. int version;
  49. int revision;
  50. if (GetInfo(inStream, type, version, revision))
  51. {
  52. if (version == sVersion && revision == sRevision)
  53. {
  54. // Create an input stream of the correct type
  55. switch (type)
  56. {
  57. case EStreamType::Text: return new ObjectStreamTextIn(inStream);
  58. case EStreamType::Binary: return new ObjectStreamBinaryIn(inStream);
  59. default: JPH_ASSERT(false);
  60. }
  61. }
  62. else
  63. {
  64. Trace("ObjectStreamIn: Different version stream (%d.%02d, expected %d.%02d).", version, revision, sVersion, sRevision);
  65. }
  66. }
  67. return nullptr;
  68. }
  69. void *ObjectStreamIn::Read(const RTTI *inRTTI)
  70. {
  71. using ObjectSet = unordered_set<void *>;
  72. // Read all information on the stream
  73. void *main_object = nullptr;
  74. bool continue_reading = true;
  75. for (;;)
  76. {
  77. // Get type of next operation
  78. EDataType data_type;
  79. if (!ReadDataType(data_type))
  80. break;
  81. if (data_type == EDataType::Declare)
  82. {
  83. // Read type declaration
  84. if (!ReadRTTI())
  85. {
  86. Trace("ObjectStreamIn: Fatal error while reading class description for class %s.", inRTTI->GetName());
  87. continue_reading = false;
  88. break;
  89. }
  90. }
  91. else if (data_type == EDataType::Object)
  92. {
  93. const RTTI *rtti;
  94. void *object = ReadObject(rtti);
  95. if (!main_object && object)
  96. {
  97. // This is the first and thus main object of the file.
  98. if (rtti->IsKindOf(inRTTI))
  99. {
  100. // Object is of correct type
  101. main_object = object;
  102. }
  103. else
  104. {
  105. Trace("ObjectStreamIn: Main object of different type. Expected %s, but found %s.", inRTTI->GetName(), rtti->GetName());
  106. continue_reading = false;
  107. break;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. // Invalid or out of place token found
  114. Trace("ObjectStreamIn: Invalid or out of place token found.");
  115. continue_reading = false;
  116. break;
  117. }
  118. }
  119. // Resolve links (pointer, references)
  120. if (continue_reading)
  121. {
  122. // Resolve links
  123. ObjectSet referenced_objects;
  124. for (Link &link : mUnresolvedLinks)
  125. {
  126. IdentifierMap::const_iterator j = mIdentifierMap.find(link.mIdentifier);
  127. if (j != mIdentifierMap.end() && j->second.mRTTI->IsKindOf(link.mRTTI))
  128. {
  129. const ObjectInfo &obj_info = j->second;
  130. // Set pointer
  131. *link.mPointer = obj_info.mInstance;
  132. // Increment refcount if it was a referencing pointer
  133. if (link.mRefCountOffset != -1)
  134. ++(*(uint32 *)(((uint8 *)obj_info.mInstance) + link.mRefCountOffset));
  135. // Add referenced object to the list
  136. if (referenced_objects.find(obj_info.mInstance) == referenced_objects.end())
  137. referenced_objects.insert(obj_info.mInstance);
  138. }
  139. else
  140. {
  141. // Referenced object not found, set pointer to nullptr
  142. Trace("ObjectStreamIn: Setting incorrect pointer to class of type %s to nullptr.", link.mRTTI->GetName());
  143. *link.mPointer = nullptr;
  144. }
  145. }
  146. // Release unreferenced objects except the main object
  147. for (const IdentifierMap::value_type &j : mIdentifierMap)
  148. {
  149. const ObjectInfo &obj_info = j.second;
  150. if (obj_info.mInstance != main_object)
  151. {
  152. ObjectSet::const_iterator k = referenced_objects.find(obj_info.mInstance);
  153. if (k == referenced_objects.end())
  154. {
  155. Trace("ObjectStreamIn: Releasing unreferenced object of type %s.", obj_info.mRTTI->GetName());
  156. obj_info.mRTTI->DestructObject(obj_info.mInstance);
  157. }
  158. }
  159. }
  160. // Call OnLoaded callback
  161. unordered_set<SerializableObject *> visited_set;
  162. OSVisitCompounds(main_object, inRTTI, [&visited_set](const void *inObject, const RTTI *inType)
  163. {
  164. SerializableObject *so = const_cast<SerializableObject *>(reinterpret_cast<const SerializableObject *>(inType->CastTo(inObject, JPH_RTTI(SerializableObject))));
  165. if (so != nullptr && visited_set.insert(so).second)
  166. so->OnLoaded();
  167. });
  168. return main_object;
  169. }
  170. else
  171. {
  172. // Release all objects if a fatal error occurred
  173. for (const IdentifierMap::value_type &i : mIdentifierMap)
  174. {
  175. const ObjectInfo &obj_info = i.second;
  176. obj_info.mRTTI->DestructObject(obj_info.mInstance);
  177. }
  178. return nullptr;
  179. }
  180. }
  181. void *ObjectStreamIn::ReadObject(const RTTI *& outRTTI)
  182. {
  183. // Read the object class
  184. void *object = nullptr;
  185. string class_name;
  186. if (ReadName(class_name))
  187. {
  188. // Get class description
  189. ClassDescriptionMap::iterator i = mClassDescriptionMap.find(class_name);
  190. if (i != mClassDescriptionMap.end())
  191. {
  192. const ClassDescription &class_desc = i->second;
  193. // Read object identifier
  194. Identifier identifier;
  195. if (ReadIdentifier(identifier))
  196. {
  197. // Check if this object can be read or must be skipped
  198. if (identifier != sNullIdentifier
  199. && class_desc.mRTTI
  200. && !class_desc.mRTTI->IsAbstract())
  201. {
  202. // Create object instance
  203. outRTTI = class_desc.mRTTI;
  204. object = outRTTI->CreateObject();
  205. // Read object attributes
  206. if (ReadClassData(class_desc, object))
  207. {
  208. // Add object to identifier map
  209. mIdentifierMap.try_emplace(identifier, object, outRTTI);
  210. }
  211. else
  212. {
  213. // Fatal error while reading attributes, release object
  214. outRTTI->DestructObject(object);
  215. object = nullptr;
  216. }
  217. }
  218. else
  219. {
  220. // Skip this object
  221. // TODO: This operation can fail, but there is no check yet
  222. Trace("ObjectStreamIn: Found uncreatable object %s.", class_name.c_str());
  223. ReadClassData(class_desc, nullptr);
  224. }
  225. }
  226. }
  227. else
  228. {
  229. // TODO: This is a fatal error, but this function has no way of indicating this
  230. Trace("ObjectStreamIn: Found object of unknown class %s.", class_name.c_str());
  231. }
  232. }
  233. return object;
  234. }
  235. bool ObjectStreamIn::ReadRTTI()
  236. {
  237. // Read class name and find it's attribute info
  238. string class_name;
  239. if (!ReadName(class_name))
  240. return false;
  241. // Find class
  242. const RTTI *rtti = Factory::sInstance.Find(class_name.c_str());
  243. if (rtti == nullptr)
  244. Trace("ObjectStreamIn: Unknown class: \"%s\".", class_name.c_str());
  245. // Insert class description
  246. ClassDescription &class_desc = mClassDescriptionMap.try_emplace(class_name, rtti).first->second;
  247. // Read the number of entries in the description
  248. uint32 count;
  249. if (!ReadCount(count))
  250. return false;
  251. // Read the entries
  252. for (uint32 i = 0; i < count; ++i)
  253. {
  254. AttributeDescription attribute;
  255. // Read name
  256. string attribute_name;
  257. if (!ReadName(attribute_name))
  258. return false;
  259. // Read type
  260. if (!ReadDataType(attribute.mDataType))
  261. return false;
  262. // Read array depth
  263. while (attribute.mDataType == EDataType::Array)
  264. {
  265. ++attribute.mArrayDepth;
  266. if (!ReadDataType(attribute.mDataType))
  267. return false;
  268. }
  269. // Read instance/pointer class name
  270. if ((attribute.mDataType == EDataType::Instance || attribute.mDataType == EDataType::Pointer)
  271. && !ReadName(attribute.mClassName))
  272. return false;
  273. // Find attribute in rtti
  274. if (rtti)
  275. {
  276. // Find attribute index
  277. for (int idx = 0; idx < rtti->GetAttributeCount(); ++idx)
  278. {
  279. const SerializableAttribute *attr = DynamicCast<SerializableAttribute, RTTIAttribute>(rtti->GetAttribute(idx));
  280. if (attr != nullptr && strcmp(attr->GetName(), attribute_name.c_str()) == 0)
  281. {
  282. attribute.mIndex = idx;
  283. break;
  284. }
  285. }
  286. // Check if attribute is of expected type
  287. if (attribute.mIndex >= 0)
  288. {
  289. const SerializableAttribute *attr = (const SerializableAttribute *)rtti->GetAttribute(attribute.mIndex);
  290. if (!attr->IsType(attribute.mArrayDepth, attribute.mDataType, attribute.mClassName.c_str()))
  291. attribute.mIndex = -1;
  292. }
  293. }
  294. // Add attribute to the class description
  295. class_desc.mAttributes.push_back(attribute);
  296. }
  297. return true;
  298. }
  299. bool ObjectStreamIn::ReadClassData(const char *inClassName, void *inInstance)
  300. {
  301. // Find the class description
  302. ClassDescriptionMap::iterator i = mClassDescriptionMap.find(inClassName);
  303. if (i != mClassDescriptionMap.end())
  304. return ReadClassData(i->second, inInstance);
  305. return false;
  306. }
  307. bool ObjectStreamIn::ReadClassData(const ClassDescription &inClassDesc, void *inInstance)
  308. {
  309. // Read data for this class
  310. bool continue_reading = true;
  311. for (const AttributeDescription &attr_desc : inClassDesc.mAttributes)
  312. {
  313. // Read or skip the attribute data
  314. if (attr_desc.mIndex >= 0 && inInstance)
  315. {
  316. const SerializableAttribute *attr = (const SerializableAttribute *)inClassDesc.mRTTI->GetAttribute(attr_desc.mIndex);
  317. continue_reading = attr->ReadData(*this, inInstance);
  318. }
  319. else
  320. continue_reading = SkipAttributeData(attr_desc.mArrayDepth, attr_desc.mDataType, attr_desc.mClassName.c_str());
  321. if (!continue_reading)
  322. break;
  323. }
  324. return continue_reading;
  325. }
  326. bool ObjectStreamIn::ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset)
  327. {
  328. Identifier identifier;
  329. if (ReadIdentifier(identifier))
  330. {
  331. if (identifier == sNullIdentifier)
  332. {
  333. // Set nullptr pointer
  334. inPointer = nullptr;
  335. }
  336. else
  337. {
  338. // Put pointer on the list to be resolved later on
  339. mUnresolvedLinks.emplace_back();
  340. mUnresolvedLinks.back().mPointer = inPointer;
  341. mUnresolvedLinks.back().mRefCountOffset = inRefCountOffset;
  342. mUnresolvedLinks.back().mIdentifier = identifier;
  343. mUnresolvedLinks.back().mRTTI = inRTTI;
  344. }
  345. return true;
  346. }
  347. return false;
  348. }
  349. bool ObjectStreamIn::SkipAttributeData(int inArrayDepth, EDataType inDataType, const char *inClassName)
  350. {
  351. bool continue_reading = true;
  352. // Get number of items to read
  353. uint32 count = 1;
  354. for (; inArrayDepth > 0; --inArrayDepth)
  355. {
  356. uint32 temporary;
  357. if (ReadCount(temporary))
  358. {
  359. // Multiply for multi dimensional arrays
  360. count *= temporary;
  361. }
  362. else
  363. {
  364. // Fatal error while reading array size
  365. continue_reading = false;
  366. break;
  367. }
  368. }
  369. // Read data for all items
  370. if (continue_reading)
  371. {
  372. if (inDataType == EDataType::Instance)
  373. {
  374. // Get the class description
  375. ClassDescriptionMap::iterator i = mClassDescriptionMap.find(inClassName);
  376. if (i != mClassDescriptionMap.end())
  377. {
  378. for (; count > 0 && continue_reading; --count)
  379. continue_reading = ReadClassData(i->second, nullptr);
  380. }
  381. else
  382. {
  383. continue_reading = false;
  384. Trace("ObjectStreamIn: Found instance of unknown class %s.", inClassName);
  385. }
  386. }
  387. else
  388. {
  389. for (; count > 0 && continue_reading; --count)
  390. {
  391. switch (inDataType)
  392. {
  393. case EDataType::Pointer:
  394. {
  395. Identifier temporary;
  396. continue_reading = ReadIdentifier(temporary);
  397. break;
  398. }
  399. case EDataType::T_uint8:
  400. {
  401. uint8 temporary;
  402. continue_reading = ReadPrimitiveData(temporary);
  403. break;
  404. }
  405. case EDataType::T_uint16:
  406. {
  407. uint16 temporary;
  408. continue_reading = ReadPrimitiveData(temporary);
  409. break;
  410. }
  411. case EDataType::T_int:
  412. {
  413. int temporary;
  414. continue_reading = ReadPrimitiveData(temporary);
  415. break;
  416. }
  417. case EDataType::T_uint32:
  418. {
  419. uint32 temporary;
  420. continue_reading = ReadPrimitiveData(temporary);
  421. break;
  422. }
  423. case EDataType::T_uint64:
  424. {
  425. uint64 temporary;
  426. continue_reading = ReadPrimitiveData(temporary);
  427. break;
  428. }
  429. case EDataType::T_float:
  430. {
  431. float temporary;
  432. continue_reading = ReadPrimitiveData(temporary);
  433. break;
  434. }
  435. case EDataType::T_bool:
  436. {
  437. bool temporary;
  438. continue_reading = ReadPrimitiveData(temporary);
  439. break;
  440. }
  441. case EDataType::T_string:
  442. {
  443. string temporary;
  444. continue_reading = ReadPrimitiveData(temporary);
  445. break;
  446. }
  447. case EDataType::T_Float3:
  448. {
  449. Float3 temporary;
  450. continue_reading = ReadPrimitiveData(temporary);
  451. break;
  452. }
  453. case EDataType::T_Vec3:
  454. {
  455. Vec3 temporary;
  456. continue_reading = ReadPrimitiveData(temporary);
  457. break;
  458. }
  459. case EDataType::T_Vec4:
  460. {
  461. Vec4 temporary;
  462. continue_reading = ReadPrimitiveData(temporary);
  463. break;
  464. }
  465. case EDataType::T_Quat:
  466. {
  467. Quat temporary;
  468. continue_reading = ReadPrimitiveData(temporary);
  469. break;
  470. }
  471. case EDataType::T_Mat44:
  472. {
  473. Mat44 temporary;
  474. continue_reading = ReadPrimitiveData(temporary);
  475. break;
  476. }
  477. case EDataType::Array:
  478. case EDataType::Object:
  479. case EDataType::Declare:
  480. case EDataType::Instance:
  481. case EDataType::Invalid:
  482. default:
  483. continue_reading = false;
  484. break;
  485. }
  486. }
  487. }
  488. }
  489. return continue_reading;
  490. }
  491. // Define macro to declare functions for a specific primitive type
  492. #define JPH_DECLARE_PRIMITIVE(name) \
  493. bool OSReadData(ObjectStreamIn &ioStream, name &outPrimitive) \
  494. { \
  495. return ioStream.ReadPrimitiveData(outPrimitive); \
  496. }
  497. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  498. #include <Jolt/ObjectStream/ObjectStreamTypes.h>
  499. JPH_NAMESPACE_END