Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "../NetworkIdentifier.h"
  4.  
  5. struct BinaryStream;
  6. struct NetEventCallback;
  7.  
  8. struct Packet {
  9.     int unk_4 = 2, unk_8 = 1;
  10.     unsigned char playerSubIndex = 0;
  11.  
  12.     Packet(unsigned char playerSubIndex) : playerSubIndex(playerSubIndex) {}
  13.  
  14.     virtual ~Packet() = 0;
  15.  
  16.     virtual void *getId() const = 0;
  17.  
  18.     virtual void *getName() const = 0;
  19.  
  20.     virtual void *write(BinaryStream &) const = 0;
  21.  
  22.     virtual void *read(BinaryStream &) = 0;
  23.  
  24.     virtual void *handle(NetworkIdentifier const &, NetEventCallback &) const = 0;
  25.  
  26.     virtual bool disallowBatching() const = 0;
  27. };
  28.  
  29. //-------------------------------------------------------------------------------------
  30.  
  31. #pragma once
  32.  
  33. #include "Packet.h"
  34. #include <string>
  35.  
  36. /*
  37.  * Server -> Client
  38.  */
  39. class ModalFormRequestPacket : public Packet {
  40. public:
  41.     int formId;
  42.     std::string formData;
  43.  
  44.     ModalFormRequestPacket(unsigned char playerSubIndex) : Packet(playerSubIndex) {}
  45.  
  46.     virtual void *getId() const;
  47.  
  48.     virtual void *getName() const;
  49.  
  50.     virtual void *write(BinaryStream &) const;
  51.  
  52.     virtual void *read(BinaryStream &);
  53.  
  54.     virtual void *handle(NetworkIdentifier const &, NetEventCallback &) const;
  55.  
  56.     virtual bool disallowBatching() const;
  57. };
  58.  
  59. //-------------------------------------------------------------------------------------
  60.  
  61. #pragma once
  62.  
  63. #include "rapidjson/document.h"
  64. #include "rapidjson/writer.h"
  65. #include "rapidjson/reader.h"
  66. #include "rapidjson/stringbuffer.h"
  67. #include "rapidjson/prettywriter.h"
  68.  
  69. using namespace rapidjson;
  70.  
  71. typedef PrettyWriter<StringBuffer> JsonWriter;
  72.  
  73. //-------------------------------------------------------------------------------------
  74.  
  75. //Element.h, Element.cpp:
  76.  
  77. #pragma once
  78.  
  79. #include "../Json.h"
  80.  
  81. class Element {
  82. protected:
  83.     const char *text;
  84. public:
  85.     explicit Element(const char *text) {
  86.         this->text = text;
  87.     }
  88.  
  89.     const char *getText() const {
  90.         return text;
  91.     };
  92.  
  93.     virtual const char *getType() {
  94.         return nullptr;
  95.     };
  96.  
  97.     void jsonSerialize(JsonWriter &writer);
  98.  
  99.     virtual void serializeElementData(JsonWriter &writer) {};
  100. };
  101.  
  102. #include "Element.h"
  103.  
  104. void Element::jsonSerialize(JsonWriter &writer) {
  105.     writer.StartObject();
  106.  
  107.     writer.Key("text");
  108.     writer.String(getText());
  109.  
  110.     auto type = getType();
  111.     if (type != nullptr) {
  112.         writer.Key("type");
  113.         writer.String(type);
  114.     }
  115.  
  116.     serializeElementData(writer);
  117.  
  118.     writer.EndObject();
  119. }
  120.  
  121. //-------------------------------------------------------------------------------------
  122.  
  123. //Button.h:
  124.  
  125. #pragma once
  126.  
  127. #include "Element.h"
  128.  
  129. class Button : public Element {
  130. public:
  131.     explicit Button(const char *text) : Element(text) {};
  132. };
  133.  
  134. //-------------------------------------------------------------------------------------
  135.  
  136. //Form.h, Form.cpp:
  137.  
  138. #pragma once
  139.  
  140. #include <vector>
  141. #include "../elements/Element.h"
  142.  
  143. using namespace std;
  144.  
  145. class Form {
  146. protected:
  147.     const char *title;
  148. public:
  149.     explicit Form(const char *title) {
  150.         this->title = title;
  151.     }
  152.  
  153.     virtual const char *getType() const = 0;
  154.  
  155.     const char *getTitle() const {
  156.         return title;
  157.     }
  158.  
  159.     string jsonSerialize();
  160.  
  161.     virtual void serializeFormData(JsonWriter &writer) {}
  162. };
  163.  
  164. #include "Form.h"
  165.  
  166. string Form::jsonSerialize() {
  167.     StringBuffer buffer;
  168.     JsonWriter writer(buffer);
  169.  
  170.     writer.StartObject();
  171.  
  172.     writer.Key("type");
  173.     writer.String(getType());
  174.  
  175.     writer.Key("title");
  176.     writer.String(getTitle());
  177.  
  178.     serializeFormData(writer);
  179.  
  180.     writer.EndObject();
  181.  
  182.     return string(buffer.GetString());
  183. }
  184.  
  185. //-------------------------------------------------------------------------------------
  186.  
  187. //MenuForm.h, MenuForm.cpp:
  188.  
  189. #include <utility>
  190.  
  191. #pragma once
  192.  
  193. #include "Form.h"
  194. #include "../elements/Button.h"
  195.  
  196. class MenuForm : public Form {
  197. protected:
  198.     const char *text;
  199.     const vector<Button *> buttons;
  200. public:
  201.     explicit MenuForm(const char *title, const char *text, vector<Button *> buttons) : Form(title), text(text), buttons(std::move(buttons)) {}
  202.  
  203.     const char *getType() const override {
  204.         return "form";
  205.     }
  206.  
  207.     void serializeFormData(JsonWriter &writer) override;
  208. };
  209.  
  210. #include "MenuForm.h"
  211.  
  212. void MenuForm::serializeFormData(JsonWriter &writer) {
  213.     writer.Key("content");
  214.     writer.String(text);
  215.     writer.Key("buttons");
  216.     writer.StartArray();
  217.     for (auto *button : buttons) {
  218.         button->jsonSerialize(writer);
  219.     }
  220.     writer.EndArray();
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement