PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/GameSparksSample/Plugins/GameSparks/Source/GameSparksBaseSDK/src/GameSparksRT/Proto/RTData.Serializer.cpp

https://bitbucket.org/NuclearCookie/gamesparks-cpp-unreal
C++ | 276 lines | 216 code | 39 blank | 21 comment | 46 complexity | 7fcab8d5a1fcaf46842c22ec99102ad0 MD5 | raw file
  1. #include "./RTData.Serializer.hpp"
  2. #include "ProtocolParser.hpp"
  3. #include "ReusableBinaryWriter.hpp"
  4. #include "./ProtocolBufferException.hpp"
  5. #include "../../../include/GameSparksRT/Proto/RTVal.hpp"
  6. #include "../../System/IO/EndOfStreamException.hpp"
  7. #include "../../../include/GameSparksRT/RTData.hpp"
  8. typedef unsigned char byte;
  9. namespace GameSparks { namespace RT { namespace Proto {
  10. System::Failable<void> RTValSerializer::ReadRTVal (System::IO::Stream& stream, System::IO::BinaryReader& br, RTVal& instance)
  11. {
  12. GS_ASSIGN_OR_THROW(limit_, ::GameSparks::RT::Proto::ProtocolParser::ReadUInt32(stream));
  13. int limit = static_cast<int>(limit_);
  14. limit += stream.Position();
  15. for (;;)
  16. {
  17. if (stream.Position() >= limit)
  18. {
  19. if (stream.Position() == limit)
  20. break;
  21. else
  22. return ::GameSparks::RT::Proto::ProtocolBufferException("Read past max limit");
  23. }
  24. GS_ASSIGN_OR_THROW(keyByte, stream.ReadByte());
  25. if (keyByte == -1)
  26. GS_THROW(System::IO::EndOfStreamException("EndOfStreamException"));
  27. // Optimized reading of known fields with field ID < 16
  28. switch (keyByte)
  29. {
  30. // Field 1 LengthDelimited
  31. case 10:
  32. {
  33. GS_ASSIGN_OR_THROW(tmp, ::GameSparks::RT::Proto::ProtocolParser::ReadString(stream));
  34. instance.string_val = tmp;
  35. continue;
  36. }
  37. // Field 2 LengthDelimited
  38. case 18:{
  39. // repeated packed
  40. GS_ASSIGN_OR_THROW(end2_, ::GameSparks::RT::Proto::ProtocolParser::ReadUInt32 (stream));
  41. int end2 = static_cast<int>(end2_);
  42. end2 += stream.Position();
  43. RTVector v;
  44. int i = 0;
  45. while (stream.Position() < end2) {
  46. GS_ASSIGN_OR_THROW(read, br.ReadSingle ());
  47. switch (i) {
  48. case 0:
  49. v.x = read;
  50. break;
  51. case 1:
  52. v.y = read;
  53. break;
  54. case 2:
  55. v.z = read;
  56. break;
  57. case 3:
  58. v.w = read;
  59. break;
  60. default:
  61. break;
  62. }
  63. i++;
  64. }
  65. instance.vec_val = v;
  66. if (stream.Position() != end2)
  67. return ::GameSparks::RT::Proto::ProtocolBufferException("Read too many bytes in packed data");
  68. continue;}
  69. // Field 14 LengthDelimited
  70. case 114:
  71. {
  72. if (!instance.data_val.HasValue()) {
  73. instance.data_val = RTData();
  74. }
  75. GS_CALL_OR_THROW(RTData::ReadRTData(stream, br, instance.data_val.Value()));
  76. continue;
  77. }
  78. }
  79. GS_ASSIGN_OR_THROW(key, ::GameSparks::RT::Proto::ProtocolParser::ReadKey((unsigned char)keyByte, stream));
  80. // Reading field ID > 16 and unknown field ID/wire type combinations
  81. switch (key.Field)
  82. {
  83. case 0:
  84. return ::GameSparks::RT::Proto::ProtocolBufferException("Invalid field id: 0, something went wrong in the stream");
  85. default:
  86. GS_CALL_OR_THROW(::GameSparks::RT::Proto::ProtocolParser::SkipKey(stream, key));
  87. break;
  88. }
  89. }
  90. return {};
  91. }
  92. System::Failable<void> RTValSerializer::WriteRTVal (System::IO::Stream& stream, const RTVal& val)
  93. {
  94. BinaryWriteMemoryStream ms;
  95. if (val.string_val.HasValue())
  96. {
  97. // Key for field: 1, LengthDelimited
  98. GS_CALL_OR_THROW(ms.WriteByte(10));
  99. GS_CALL_OR_THROW(::GameSparks::RT::Proto::ProtocolParser::WriteBytes(ms, System::Text::Encoding::UTF8::GetBytes(val.string_val.Value())));
  100. }
  101. else if(val.data_val.HasValue())
  102. {
  103. GS_CALL_OR_THROW(ms.WriteByte(114));
  104. GS_CALL_OR_THROW(RTData::WriteRTData(ms, val.data_val.Value()));
  105. }
  106. else if (val.vec_val.HasValue())
  107. {
  108. RTVector vec_value = val.vec_val.Value();
  109. // Key for field: 2, LengthDelimited
  110. GS_CALL_OR_THROW(ms.WriteByte(18));
  111. /*!
  112. * You need to set x, xy, xyz or xyzw. You cannot leave dimensions of the vector blank.
  113. * For example you cannot only set y and leave the rest unset.
  114. * */
  115. assert(
  116. ((vec_value.x.HasValue() && vec_value.y.HasValue() && vec_value.z.HasValue() && vec_value.w.HasValue()) ||
  117. (vec_value.x.HasValue() && vec_value.y.HasValue() && vec_value.z.HasValue()) ||
  118. (vec_value.x.HasValue() && vec_value.y.HasValue()) ||
  119. (vec_value.x.HasValue())) && "RTVector cannot be sparse."
  120. );
  121. int numberOfFloatsSet = vec_value.w.HasValue() ? 4 : (vec_value.z.HasValue() ? 3 : (vec_value.y.HasValue() ? 2 : (vec_value.x.HasValue() ? 1 : 0)));
  122. GS_CALL_OR_THROW(::GameSparks::RT::Proto::ProtocolParser::WriteUInt32(ms, 4u * (uint)numberOfFloatsSet));
  123. for(int i=0 ; i<numberOfFloatsSet ; i++)
  124. {
  125. switch (i)
  126. {
  127. case 0:
  128. GS_CALL_OR_THROW(ms.BinaryWriter.Write(vec_value.x.Value()));
  129. break;
  130. case 1:
  131. GS_CALL_OR_THROW(ms.BinaryWriter.Write(vec_value.y.Value()));
  132. break;
  133. case 2:
  134. GS_CALL_OR_THROW(ms.BinaryWriter.Write(vec_value.z.Value()));
  135. break;
  136. case 3:
  137. GS_CALL_OR_THROW(ms.BinaryWriter.Write(vec_value.w.Value()));
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. }
  144. auto data = ms.GetBuffer();
  145. GS_CALL_OR_THROW(::GameSparks::RT::Proto::ProtocolParser::WriteUInt32(stream, (uint)ms.Position()));
  146. GS_CALL_OR_THROW(stream.Write(data, 0, (int)ms.Position()));
  147. return {};
  148. }
  149. System::Failable<void> RTDataSerializer::ReadRTData(System::IO::Stream& stream, System::IO::BinaryReader& br, RTData& instance){
  150. //BinaryReader br = ((PositionStream)stream).BinaryReader;
  151. GS_ASSIGN_OR_THROW(limit_, ::GameSparks::RT::Proto::ProtocolParser::ReadUInt32(stream))
  152. int limit = static_cast<int>(limit_);
  153. limit += stream.Position();
  154. for (;;)
  155. {
  156. if (stream.Position() >= limit)
  157. {
  158. if (stream.Position() == limit)
  159. break;
  160. else
  161. return ::GameSparks::RT::Proto::ProtocolBufferException("Read past max limit");
  162. }
  163. GS_ASSIGN_OR_THROW(keyByte, stream.ReadByte());
  164. if (keyByte == -1)
  165. break;
  166. GS_ASSIGN_OR_THROW(key, ::GameSparks::RT::Proto::ProtocolParser::ReadKey((unsigned char)keyByte, stream));
  167. /*if (key.Field == 0) {
  168. GS_THROW(::GameSparks::RT::Proto::ProtocolBufferException("Invalid field id: 0, something went wrong in the stream"));
  169. }*/
  170. if (key.Field >= GameSparksRT::MAX_RTDATA_SLOTS) {
  171. GS_THROW(::GameSparks::RT::Proto::ProtocolBufferException("Invalid field id: to many RTData fields"));
  172. }
  173. switch (key.WireType) {
  174. case Wire::Varint:
  175. {
  176. GS_ASSIGN_OR_THROW(tmp, ProtocolParser::ReadZInt64 (stream));
  177. instance.data.at(key.Field) = RTVal(tmp);
  178. break;
  179. }
  180. case Wire::Fixed32:
  181. {
  182. GS_ASSIGN_OR_THROW(tmp, br.ReadSingle ());
  183. instance.data.at(key.Field) = tmp;
  184. break;
  185. }
  186. case Wire::Fixed64:
  187. {
  188. GS_ASSIGN_OR_THROW(tmp, br.ReadDouble ());
  189. instance.data.at(key.Field) = tmp;
  190. break;
  191. }
  192. case Wire::LengthDelimited:
  193. GS_CALL_OR_THROW(RTVal::DeserializeLengthDelimited (stream, br, instance.data.at(key.Field)));
  194. break;
  195. default:
  196. break;
  197. }
  198. // Reading field ID > 16 and unknown field ID/wire type combinations
  199. switch (key.Field)
  200. {
  201. case 0:
  202. return ::GameSparks::RT::Proto::ProtocolBufferException("Invalid field id: 0, something went wrong in the stream");
  203. default:
  204. break;
  205. }
  206. }
  207. return {};
  208. }
  209. System::Failable<void> RTDataSerializer::WriteRTData(System::IO::Stream& stream, const RTData& instance)
  210. {
  211. BinaryWriteMemoryStream ms;
  212. for (ProtocolParser::uint index = 1; index < ProtocolParser::uint(instance.data.size()); index++) {
  213. RTVal entry = instance.data [index];
  214. if (entry.long_val.HasValue()) {
  215. GS_CALL_OR_THROW(ProtocolParser::WriteUInt32 (ms, index << 3));
  216. //ms.WriteByte ((byte)(index << 3));
  217. GS_CALL_OR_THROW(ProtocolParser::WriteZInt64 (ms, (int64_t)entry.long_val.Value()));
  218. } else if (entry.double_val.HasValue()) {
  219. GS_CALL_OR_THROW(ProtocolParser::WriteUInt32 (ms, (index << 3) | ((uint)1)));
  220. //ms.WriteByte ((byte)((index << 3) + 1));
  221. GS_CALL_OR_THROW(ms.BinaryWriter.Write ((double)entry.double_val.Value()));
  222. } else if (entry.float_val.HasValue()) {
  223. GS_CALL_OR_THROW(ProtocolParser::WriteUInt32 (ms, (index << 3) | ((uint)5)));
  224. //ms.WriteByte ((byte)((index << 3) + 5));
  225. GS_CALL_OR_THROW(ms.BinaryWriter.Write ((float)entry.float_val.Value()));
  226. } else if (entry.data_val.HasValue() || entry.string_val.HasValue() || entry.vec_val.HasValue()) {
  227. GS_CALL_OR_THROW(ProtocolParser::WriteUInt32 (ms, (index << 3) | ((uint)2)));
  228. //ms.WriteByte ((byte)((index << 3) + 2));
  229. GS_CALL_OR_THROW(entry.SerializeLengthDelimited (ms));
  230. }
  231. }
  232. auto buffer = ms.GetBuffer();
  233. GS_CALL_OR_THROW(::GameSparks::RT::Proto::ProtocolParser::WriteUInt32(stream, (uint)ms.Position()));
  234. GS_CALL_OR_THROW(stream.Write(buffer, 0, (int)ms.Position()));
  235. return {};
  236. }
  237. }}} /* namespace GameSparks.RT.Proto */