DataBuffer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __EQ2_DATABUFFER_
  17. #define __EQ2_DATABUFFER_
  18. #include <string>
  19. #include "../common/types.h"
  20. #include "../common/EQPacket.h"
  21. #include "../common/EQ2_Common_Structs.h"
  22. #ifdef WORLD
  23. #include "../WorldServer/SpawnLists.h"
  24. #endif
  25. using namespace std;
  26. class DataBuffer{
  27. public:
  28. bool changed;
  29. uchar* getData(){ return (uchar*)buffer.c_str(); }
  30. int32 getDataSize(){ return buffer.length(); }
  31. string* getDataString(){ return &buffer; }
  32. void CreateEQ2Color(EQ2_Color& color){
  33. CreateEQ2Color(&color);
  34. }
  35. uchar* GetLoadBuffer(){
  36. return load_buffer;
  37. }
  38. int32 GetLoadPos(){
  39. return load_pos;
  40. }
  41. int32 GetLoadLen(){
  42. return load_len;
  43. }
  44. void SetLoadPos(int32 new_pos){
  45. load_pos = new_pos;
  46. }
  47. void CreateEQ2Color(EQ2_Color* color){
  48. int8 rgb[3];
  49. float* tmp = 0;
  50. for(int i=0;i<3;i++){
  51. tmp = (float*)(load_buffer + load_pos);
  52. rgb[i] = (int8)((*tmp)*255);
  53. load_pos += sizeof(float);
  54. }
  55. color->red = rgb[0];
  56. color->green = rgb[1];
  57. color->blue = rgb[2];
  58. }
  59. template<class Type> void MakeEQ2_Int8(Type& output){
  60. MakeEQ2_Int8(&output);
  61. }
  62. template<class Type> void MakeEQ2_Int8(Type* output){
  63. float* tmp = (float*)(load_buffer + load_pos);
  64. if(*tmp < 0)
  65. *tmp *= -1;
  66. sint8 result = (sint8)((*tmp)*100);
  67. memcpy(output, &result, sizeof(sint8));
  68. load_pos += sizeof(float);
  69. }
  70. void InitializeGetData(){
  71. get_buffer = (uchar*)buffer.c_str();
  72. get_len = buffer.length();
  73. get_pos = 0;
  74. }
  75. void InitializeLoadData(uchar* input, int32 size){
  76. buffer = string((char*)input, size);
  77. load_buffer = (uchar*)buffer.c_str();
  78. load_len = size;
  79. load_pos = 0;
  80. }
  81. template<class String> void LoadDataString(String& output){
  82. LoadDataString(&output);
  83. }
  84. template<class String> void LoadDataString(String* output){
  85. if((sizeof(output->size) + load_pos) <= load_len){
  86. memcpy(&output->size, load_buffer + load_pos, sizeof(output->size));
  87. load_pos += sizeof(output->size);
  88. }
  89. if((output->size + load_pos) <= load_len){
  90. output->data = string((char*)(load_buffer + load_pos), output->size);
  91. load_pos += output->size;
  92. }
  93. }
  94. template<class Type> void LoadData(Type& output){
  95. LoadData(&output);
  96. }
  97. template<class Type> void LoadData(Type* output, int32 array_size){
  98. if(array_size<=1){
  99. LoadData(output);
  100. }
  101. else{
  102. for(int32 i=0;i<array_size;i++)
  103. LoadData(&output[i]);
  104. }
  105. }
  106. template<class Type> void LoadData(Type* output){
  107. if((sizeof(Type) + load_pos) <= load_len){
  108. memcpy(output, load_buffer + load_pos, sizeof(Type));
  109. load_pos += sizeof(Type);
  110. }
  111. }
  112. template<class Type> void LoadData(Type& output, int32 array_size){
  113. LoadData(&output, array_size);
  114. }
  115. void LoadSkip(int8 bytes){
  116. load_pos += bytes;
  117. }
  118. template<class Type> void LoadSkip(Type& skip){
  119. LoadSkip(&skip);
  120. }
  121. template<class Type> void LoadSkip(Type* skip){
  122. load_pos += sizeof(Type);
  123. }
  124. template<class Type> void GetData(Type* output){
  125. if((sizeof(Type) + get_pos) <= get_len){
  126. *output = (Type*)get_buffer;
  127. get_pos += sizeof(output);
  128. }
  129. }
  130. void AddZeros(int16 num){
  131. int8* data = new int8[num];
  132. memset(data, 0, num);
  133. AddData(*data);
  134. safe_delete_array(data);
  135. }
  136. template<class Type> void StructAddData(Type input, int16 size, string* datastring){
  137. if(datastring)
  138. datastring->append((char*)&input, size);
  139. else
  140. buffer.append((char*)&input, size);
  141. }
  142. template<class Type> void StructAddData(Type input, int32 array_size, int16 size, string* datastring){
  143. if(array_size>0){
  144. for(int32 i=0;i<array_size;i++)
  145. StructAddData(input[i], size, datastring);
  146. }
  147. else
  148. StructAddData(input, size, datastring);
  149. }
  150. template<class Type> void AddData(Type input, string* datastring = 0){
  151. if(!datastring)
  152. datastring = &buffer;
  153. datastring->append((char*)&input, sizeof(input));
  154. }
  155. template<class Type> void AddData(Type input, int32 array_size, string* datastring = 0){
  156. if(array_size>0){
  157. for(int32 i=0;i<array_size;i++)
  158. AddData(input[i], datastring);
  159. }
  160. else
  161. AddData(input, datastring);
  162. }
  163. template<class String> void AddDataString(String* input, string* datastring = 0){
  164. AddDataString(*input, datastring);
  165. }
  166. template<class String> void AddDataString(String input, string* datastring = 0){
  167. input.size = input.data.length();
  168. if(!datastring)
  169. datastring = &buffer;
  170. datastring->append((char*)&input.size, sizeof(input.size));
  171. datastring->append(input.data);
  172. }
  173. void AddCharArray(char* array, string* datastring = 0){
  174. if(!datastring)
  175. datastring = &buffer;
  176. datastring->append(array);
  177. }
  178. void AddCharArray(char* array, int16 size, string* datastring = 0){
  179. if(!datastring)
  180. datastring = &buffer;
  181. datastring->append(array, size);
  182. }
  183. void AddData(string data, string* datastring = 0){
  184. if(!datastring)
  185. datastring = &buffer;
  186. datastring->append(data);
  187. }
  188. void Clear() { buffer.clear(); }
  189. private:
  190. string buffer;
  191. uchar* get_buffer;
  192. uchar* load_buffer;
  193. int32 get_len;
  194. int32 get_pos;
  195. int32 load_len;
  196. int32 load_pos;
  197. };
  198. #endif