MessageListParser.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file MessageListParser.h
  13. @brief The SerializedMessageList class. */
  14. #include <vector>
  15. #include "BasicSerializedDataTypes.h"
  16. // TinyXML fwd
  17. class TiXmlElement;
  18. namespace kNet
  19. {
  20. /** Represents a single element of data in the stream. Can be a single value (one u32, or
  21. one float), an array of data (nine bits, 12 u64's), a struct (see the elements
  22. member), or even an array of structs. An array can be fixed-size (the protocol
  23. file specifies the number of elements in the array), or of dynamic size (the
  24. stream stores the element count just before the first actual element). */
  25. struct SerializedElementDesc
  26. {
  27. /// The data type of this element.
  28. BasicSerializedDataType type;
  29. /// A string version of this type is stored here. This field is used if the type of this element is something
  30. /// else than a basic type.
  31. std::string typeString;
  32. /// If true, the number of times this element is instanced is specified in the stream.
  33. bool varyingCount;
  34. /// If varyingCount=false, denotes the number of times this element is present in the stream. If varyingCount=true,
  35. /// specifies how many *bits* of storage is used in the stream to store the number of times this element is stored.
  36. int count;
  37. /// The name of this element.
  38. std::string name;
  39. /// If this element denotes a structure (type == SerialStruct), then this vector contains all the child nodes.
  40. std::vector<SerializedElementDesc*> elements;
  41. /// The parent element, or 0 if this is the root element.
  42. SerializedElementDesc *parent;
  43. };
  44. /// Describes a whole serialized message.
  45. struct SerializedMessageDesc
  46. {
  47. /// This is a weak pointer to the root element of this message description. The memory is owned by the SerializedMessageList
  48. /// where this SerializedMessageDesc belongs to.
  49. SerializedElementDesc *data;
  50. std::string name;
  51. u32 id;
  52. bool reliable;
  53. bool inOrder;
  54. u32 priority;
  55. };
  56. /// Stores a whole list of message templates.
  57. class SerializedMessageList
  58. {
  59. public:
  60. /// Loads a set of message templates from a protocol .xml file.
  61. void LoadMessagesFromFile(const char *filename);
  62. /// Returns a message template associated with the given id, or 0 if no such message exists.
  63. const SerializedMessageDesc *FindMessageByID(u32 id);
  64. /// Returns a message template associated with the given name, or 0 if no such message exists.
  65. const SerializedMessageDesc *FindMessageByName(const char *name);
  66. /// Returns the whole list of messages.
  67. const std::list<SerializedMessageDesc> &GetMessages() const { return messages; }
  68. /// Returns a flat list of all the message elements.
  69. const std::list<SerializedElementDesc> &GetElements() const { return elements; }
  70. private:
  71. std::list<SerializedElementDesc> elements;
  72. std::list<SerializedMessageDesc> messages;
  73. SerializedElementDesc *ParseNode(TiXmlElement *node, SerializedElementDesc *parentNode);
  74. void ParseMessages(TiXmlElement *root);
  75. void ParseStructs(TiXmlElement *root);
  76. };
  77. } // ~kNet