A 2D tile-based sandbox game.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include "SRF.h"
  2. namespace Swan {
  3. enum class Type {
  4. OBJECT = 0,
  5. ARRAY = 1,
  6. STRING = 2,
  7. BYTE = 3,
  8. WORD = 4,
  9. INT = 5,
  10. FLOAT = 6,
  11. DOUBLE = 7,
  12. NONE = 8,
  13. BYTE_ARRAY = 9,
  14. WORD_ARRAY = 10,
  15. INT_ARRAY = 11,
  16. FLOAT_ARRAY = 12,
  17. DOUBLE_ARRAY = 13,
  18. };
  19. static void writeByte(std::ostream &os, uint8_t val) {
  20. os.put(val);
  21. }
  22. static uint8_t readByte(std::istream &is) {
  23. return is.get();
  24. }
  25. static void writeWord(std::ostream &os, uint16_t val) {
  26. os.put((val & 0xff00u) >> 8);
  27. os.put(val & 0x00ffu);
  28. }
  29. static uint16_t readWord(std::istream &is) {
  30. return (uint16_t)(
  31. ((uint16_t)is.get()) << 8 |
  32. ((uint16_t)is.get()));
  33. }
  34. static void writeInt(std::ostream &os, int32_t val) {
  35. uint32_t uval = val;
  36. os.put((uval & 0xff000000u) << 24);
  37. os.put((uval & 0x00ff0000u) << 16);
  38. os.put((uval & 0x0000ff00u) << 8);
  39. os.put((uval & 0x000000ffu));
  40. }
  41. int32_t readInt(std::istream &is) {
  42. return (int32_t)(
  43. ((uint32_t)is.get() << 24) |
  44. ((uint32_t)is.get() << 16) |
  45. ((uint32_t)is.get() << 8) |
  46. ((uint32_t)is.get()));
  47. }
  48. static void writeFloat(std::ostream &os, float val) {
  49. uint32_t uval = (uint32_t)val;
  50. os.put((uval & 0xff000000u) << 24);
  51. os.put((uval & 0x00ff0000u) << 16);
  52. os.put((uval & 0x0000ff00u) << 8);
  53. os.put((uval & 0x000000ffu));
  54. }
  55. static float readFloat(std::istream &is) {
  56. return (float)(
  57. ((uint32_t)is.get() << 24) |
  58. ((uint32_t)is.get() << 16) |
  59. ((uint32_t)is.get() << 8) |
  60. ((uint32_t)is.get()));
  61. }
  62. static void writeDouble(std::ostream &os, double val) {
  63. uint64_t uval = (uint64_t)val;
  64. os.put((uval & 0xff00000000000000lu) << 56);
  65. os.put((uval & 0x00ff000000000000lu) << 48);
  66. os.put((uval & 0x0000ff0000000000lu) << 40);
  67. os.put((uval & 0x000000ff00000000lu) << 32);
  68. os.put((uval & 0x00000000ff000000lu) << 24);
  69. os.put((uval & 0x0000000000ff0000lu) << 16);
  70. os.put((uval & 0x000000000000ff00lu) << 8);
  71. os.put((uval & 0x00000000000000fflu));
  72. }
  73. static double readDouble(std::istream &is) {
  74. return (double)(
  75. ((uint64_t)is.get() << 56) |
  76. ((uint64_t)is.get() << 48) |
  77. ((uint64_t)is.get() << 40) |
  78. ((uint64_t)is.get() << 32) |
  79. ((uint64_t)is.get() << 24) |
  80. ((uint64_t)is.get() << 16) |
  81. ((uint64_t)is.get() << 8) |
  82. ((uint64_t)is.get()));
  83. }
  84. static void writeString(std::ostream &os, const std::string &str) {
  85. writeInt(os, str.size());
  86. os.write(str.c_str(), str.size());
  87. }
  88. static std::string readString(std::istream &is) {
  89. std::string str;
  90. int32_t len = readInt(is);
  91. str.reserve(len);
  92. is.read(str.data(), len);
  93. return str;
  94. }
  95. static char hexchr(uint8_t nibble) {
  96. if (nibble < 10)
  97. return (char)('0' + nibble);
  98. else
  99. return (char)('a' + (nibble - 10));
  100. }
  101. SRF *SRF::read(std::istream &is) {
  102. Type type = (Type)readByte(is);
  103. SRF *srf;
  104. switch (type) {
  105. case Type::OBJECT:
  106. srf = new SRFObject(); break;
  107. case Type::ARRAY:
  108. srf = new SRFArray(); break;
  109. case Type::STRING:
  110. srf = new SRFString(); break;
  111. case Type::BYTE:
  112. srf = new SRFByte(); break;
  113. case Type::WORD:
  114. srf = new SRFWord(); break;
  115. case Type::INT:
  116. srf = new SRFInt(); break;
  117. case Type::FLOAT:
  118. srf = new SRFFloat(); break;
  119. case Type::DOUBLE:
  120. srf = new SRFDouble(); break;
  121. case Type::NONE:
  122. srf = new SRFNone(); break;
  123. case Type::BYTE_ARRAY:
  124. srf = new SRFByteArray(); break;
  125. case Type::WORD_ARRAY:
  126. srf = new SRFWordArray(); break;
  127. case Type::INT_ARRAY:
  128. srf = new SRFIntArray(); break;
  129. case Type::FLOAT_ARRAY:
  130. srf = new SRFFloatArray(); break;
  131. case Type::DOUBLE_ARRAY:
  132. srf = new SRFDoubleArray(); break;
  133. }
  134. srf->parse(is);
  135. return srf;
  136. }
  137. std::ostream &operator<<(std::ostream &os, const SRF &srf) {
  138. srf.pretty(os);
  139. return os;
  140. }
  141. SRFObject::SRFObject(std::initializer_list<std::pair<std::string, SRF *>> lst) {
  142. for (auto &[k, v]: lst)
  143. val[k] = std::unique_ptr<SRF>(v);
  144. }
  145. void SRFObject::serialize(std::ostream &os) const {
  146. writeByte(os, (uint8_t)Type::OBJECT);
  147. writeInt(os, val.size());
  148. for (auto &[k, v]: val) {
  149. writeString(os, k);
  150. v->serialize(os);
  151. }
  152. }
  153. void SRFObject::parse(std::istream &is) {
  154. int32_t count = readInt(is);
  155. for (int32_t i = 0; i < count; ++i) {
  156. std::string key = readString(is);
  157. val[key] = std::unique_ptr<SRF>(SRF::read(is));
  158. }
  159. }
  160. std::ostream &SRFObject::pretty(std::ostream &os) const {
  161. os << "{ ";
  162. bool first = true;
  163. for (auto &[k, v]: val) {
  164. if (!first)
  165. os << ", ";
  166. os << '\'' << k << "': ";
  167. v->pretty(os);
  168. first = false;
  169. }
  170. return os << " }";
  171. }
  172. SRFArray::SRFArray(std::initializer_list<SRF *> lst) {
  173. for (auto &v: lst)
  174. val.push_back(std::unique_ptr<SRF>(v));
  175. }
  176. void SRFArray::serialize(std::ostream &os) const {
  177. writeByte(os, (uint8_t)Type::ARRAY);
  178. writeInt(os, val.size());
  179. for (auto &v: val) {
  180. v->serialize(os);
  181. }
  182. }
  183. void SRFArray::parse(std::istream &is) {
  184. int32_t count = readInt(is);
  185. val.resize(count);
  186. for (int32_t i = 0; i < count; ++i) {
  187. val[i] = std::unique_ptr<SRF>(SRF::read(is));
  188. }
  189. }
  190. std::ostream &SRFArray::pretty(std::ostream &os) const {
  191. os << "[ ";
  192. bool first = true;
  193. for (auto &v: val) {
  194. if (!first)
  195. os << ", ";
  196. v->pretty(os);
  197. first = false;
  198. }
  199. return os << " ]";
  200. }
  201. void SRFString::serialize(std::ostream &os) const {
  202. writeByte(os, (uint8_t)Type::STRING);
  203. writeString(os, val);
  204. }
  205. void SRFString::parse(std::istream &is) {
  206. val = readString(is);
  207. }
  208. std::ostream &SRFString::pretty(std::ostream &os) const {
  209. return os << '"' << val << '"';
  210. }
  211. void SRFByte::serialize(std::ostream &os) const {
  212. writeByte(os, (uint8_t)Type::BYTE);
  213. writeByte(os, val);
  214. }
  215. void SRFByte::parse(std::istream &is) {
  216. val = readByte(is);
  217. }
  218. std::ostream &SRFByte::pretty(std::ostream &os) const {
  219. return os << "0x"
  220. << hexchr((val & 0xf0) >> 4)
  221. << hexchr((val & 0x0f) >> 0);
  222. }
  223. void SRFWord::serialize(std::ostream &os) const {
  224. writeByte(os, (uint8_t)Type::WORD);
  225. writeWord(os, val);
  226. }
  227. void SRFWord::parse(std::istream &is) {
  228. val = readWord(is);
  229. }
  230. std::ostream &SRFWord::pretty(std::ostream &os) const {
  231. return os << "0x"
  232. << hexchr((val & 0xf000) >> 12)
  233. << hexchr((val & 0x0f00) >> 8)
  234. << hexchr((val & 0x00f0) >> 4)
  235. << hexchr((val & 0x000f) >> 0);
  236. }
  237. void SRFInt::serialize(std::ostream &os) const {
  238. writeByte(os, (uint8_t)Type::INT);
  239. writeInt(os, val);
  240. }
  241. void SRFInt::parse(std::istream &is) {
  242. val = readInt(is);
  243. }
  244. std::ostream &SRFInt::pretty(std::ostream &os) const {
  245. return os << val;
  246. }
  247. void SRFFloat::serialize(std::ostream &os) const {
  248. writeByte(os, (uint8_t)Type::FLOAT);
  249. writeFloat(os, val);
  250. }
  251. void SRFFloat::parse(std::istream &is) {
  252. val = readFloat(is);
  253. }
  254. std::ostream &SRFFloat::pretty(std::ostream &os) const {
  255. return os << val;
  256. }
  257. void SRFDouble::serialize(std::ostream &os) const {
  258. writeByte(os, (uint8_t)Type::DOUBLE);
  259. writeDouble(os, val);
  260. }
  261. void SRFDouble::parse(std::istream &is) {
  262. val = readDouble(is);
  263. }
  264. std::ostream &SRFDouble::pretty(std::ostream &os) const {
  265. return os << val;
  266. }
  267. void SRFNone::serialize(std::ostream &os) const {
  268. writeByte(os, (uint8_t)Type::NONE);
  269. }
  270. void SRFNone::parse(std::istream &is) {}
  271. std::ostream &SRFNone::pretty(std::ostream &os) const {
  272. return os << "(null)";
  273. }
  274. void SRFByteArray::serialize(std::ostream &os) const {
  275. writeByte(os, (uint8_t)Type::BYTE_ARRAY);
  276. writeInt(os, val.size());
  277. os.write((const char *)val.data(), val.size());
  278. }
  279. void SRFByteArray::parse(std::istream &is) {
  280. int32_t count = readInt(is);
  281. val.resize(count);
  282. is.read((char *)val.data(), count);
  283. }
  284. std::ostream &SRFByteArray::pretty(std::ostream &os) const {
  285. os << "byte[ " << std::hex;
  286. for (auto v: val) {
  287. os
  288. << hexchr((v & 0xf0) >> 4)
  289. << hexchr((v & 0x0f) >> 0) << ' ';
  290. }
  291. return os << ']';
  292. }
  293. void SRFWordArray::serialize(std::ostream &os) const {
  294. writeByte(os, (uint8_t)Type::WORD_ARRAY);
  295. writeInt(os, val.size());
  296. for (auto &v: val) {
  297. writeWord(os, v);
  298. }
  299. }
  300. void SRFWordArray::parse(std::istream &is) {
  301. int32_t count = readInt(is);
  302. val.resize(count);
  303. for (int32_t i = 0; i < count; ++i) {
  304. val[i] = readWord(is);
  305. }
  306. }
  307. std::ostream &SRFWordArray::pretty(std::ostream &os) const {
  308. os << "word[ ";
  309. for (auto v: val) {
  310. os
  311. << hexchr((v & 0xf000) >> 12)
  312. << hexchr((v & 0x0f00) >> 8)
  313. << hexchr((v & 0x00f0) >> 4)
  314. << hexchr((v & 0x000f) >> 0)
  315. << ' ';
  316. }
  317. return os << ']';
  318. }
  319. void SRFIntArray::serialize(std::ostream &os) const {
  320. writeByte(os, (uint8_t)Type::INT_ARRAY);
  321. writeInt(os, val.size());
  322. for (auto &v: val) {
  323. writeInt(os, v);
  324. }
  325. }
  326. void SRFIntArray::parse(std::istream &is) {
  327. int32_t count = readInt(is);
  328. val.resize(count);
  329. for (int32_t i = 0; i < count; ++i) {
  330. val[i] = readInt(is);
  331. }
  332. }
  333. std::ostream &SRFIntArray::pretty(std::ostream &os) const {
  334. os << "int[ ";
  335. for (auto v: val) {
  336. os << v << ' ';
  337. }
  338. return os << ']';
  339. }
  340. void SRFFloatArray::serialize(std::ostream &os) const {
  341. writeByte(os, (uint8_t)Type::FLOAT_ARRAY);
  342. writeInt(os, val.size());
  343. for (auto &v: val) {
  344. writeFloat(os, v);
  345. }
  346. }
  347. void SRFFloatArray::parse(std::istream &is) {
  348. int32_t count = readInt(is);
  349. val.resize(count);
  350. for (int32_t i = 0; i < count; ++i) {
  351. val[i] = readFloat(is);
  352. }
  353. }
  354. std::ostream &SRFFloatArray::pretty(std::ostream &os) const {
  355. os << "float[ ";
  356. for (auto v: val) {
  357. os << v << ' ';
  358. }
  359. return os << ']';
  360. }
  361. void SRFDoubleArray::serialize(std::ostream &os) const {
  362. writeByte(os, (uint8_t)Type::DOUBLE_ARRAY);
  363. writeInt(os, val.size());
  364. for (auto &v: val) {
  365. writeDouble(os, v);
  366. }
  367. }
  368. void SRFDoubleArray::parse(std::istream &is) {
  369. int32_t count = readInt(is);
  370. val.resize(count);
  371. for (int32_t i = 0; i < count; ++i) {
  372. val[i] = readDouble(is);
  373. }
  374. }
  375. std::ostream &SRFDoubleArray::pretty(std::ostream &os) const {
  376. os << "double[ ";
  377. for (auto v: val) {
  378. os << v << ' ';
  379. }
  380. return os << ']';
  381. }
  382. }