MiscFunctions.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 MISCFUNCTIONS_H
  17. #define MISCFUNCTIONS_H
  18. #include "types.h"
  19. #include "seperator.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <vector>
  23. #include <map>
  24. #ifndef ERRBUF_SIZE
  25. #define ERRBUF_SIZE 1024
  26. #endif
  27. //int MakeAnyLenString(char** ret, const char* format, ...);
  28. int32 hextoi(char* num);
  29. int64 hextoi64(char* num);
  30. sint32 filesize(FILE* fp);
  31. int32 ResolveIP(const char* hostname, char* errbuf = 0);
  32. void CoutTimestamp(bool ms = true);
  33. //char* strn0cpy(char* dest, const char* source, int32 size);
  34. // return value =true if entire string(source) fit, false if it was truncated
  35. //bool strn0cpyt(char* dest, const char* source, int32 size);
  36. string loadInt32String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_32BitString* eq_string = NULL);
  37. string loadInt16String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_16BitString* eq_string = NULL);
  38. string loadInt8String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_8BitString* eq_string = NULL);
  39. sint16 storeInt32String(uchar* buffer, int16 buffer_size, string in_str);
  40. sint16 storeInt16String(uchar* buffer, int16 buffer_size, string in_str);
  41. sint16 storeInt8String(uchar* buffer, int16 buffer_size, string in_str);
  42. int MakeRandomInt(int low, int high);
  43. float MakeRandomFloat(float low, float high);
  44. float TransformToFloat(sint16 data, int8 bits);
  45. sint16 TransformFromFloat(float data, int8 bits);
  46. int32 GenerateEQ2Color(float r, float g, float b);
  47. int32 GenerateEQ2Color(float* rgb[3]);
  48. void SetColor(EQ2_Color* color, long data);
  49. //void CreateEQ2Color(EQ2_Color* color, uchar* data, int16* size);
  50. int8 MakeInt8(uchar* data, int16* size);
  51. int8 MakeInt8(float* input);
  52. bool Unpack(int32 srcLen, uchar* data, uchar* dst, int16 dstLen, int16 version = 0, bool reverse = true);
  53. bool Unpack(uchar* data, uchar* dst, int16 dstLen, int16 version = 0, bool reverse = true);
  54. int32 Pack(uchar* data, uchar* src, int16 srcLen, int16 dstLen, int16 version = 0, bool reverse = true);
  55. void Reverse(uchar* input, int32 srcLen);
  56. void Encode(uchar* dst, uchar* src, int16 len);
  57. void Decode(uchar* dst, uchar* src, int16 len);
  58. string ToUpper(string input);
  59. string ToLower(string input);
  60. int32 ParseIntValue(string input);
  61. map<string, string> TranslateBrokerRequest(string request);
  62. void MovementDecode(uchar* dst, uchar* newval, uchar* orig, int16 len);
  63. vector<string>* SplitString(string str, char delim);
  64. int8 DoOverLoad(int32 val, uchar* data);
  65. int8 CheckOverLoadSize(int32 val);
  66. int32 CountWordsInString(const char* text);
  67. bool IsNumber(const char *num);
  68. void PrintSep(Seperator *sep, const char *name = 0);
  69. ///<summary>Gets the packet type for the given version</summary>
  70. ///<param name='version'>The client version</param>
  71. int16 GetItemPacketType(int32 version);
  72. ///<summary>Gets the opcode version_range1 from the clients version</summary>
  73. ///<param name='version'>The client version</param>
  74. int16 GetOpcodeVersion(int16 version);
  75. void SleepMS(int32 milliseconds);
  76. size_t strlcpy(char *dst, const char *src, size_t size);
  77. float short_to_float(const ushort x);
  78. uint32 float_to_int(const float x);
  79. uint32 as_uint(const float x);
  80. float as_float(const uint32 x);
  81. bool INIReadBool(FILE *f, const char *section, const char *property, bool *out);
  82. bool INIReadInt(FILE *f, const char *section, const char *property, int *out);
  83. template<class Type> void AddData(Type input, string* datastring){
  84. if(datastring)
  85. datastring->append((char*)&input, sizeof(input));
  86. }
  87. template<class Type> void AddData(Type input, int32 array_size, string* datastring){
  88. if(array_size>0){
  89. for(int32 i=0;i<array_size;i++)
  90. AddData(input[i], datastring);
  91. }
  92. else
  93. AddData(input, datastring);
  94. }
  95. #ifndef WIN32
  96. #define _ITOA_BUFLEN 25
  97. const char * itoa(int value);
  98. char * itoa(int value, char *result, int base);
  99. #endif
  100. class InitWinsock {
  101. public:
  102. InitWinsock();
  103. ~InitWinsock();
  104. };
  105. template<class T> class AutoDelete {
  106. public:
  107. AutoDelete(T** iVar, T* iSetTo = 0) {
  108. init(iVar, iSetTo);
  109. }
  110. AutoDelete() {}
  111. void init(T** iVar, T* iSetTo = 0)
  112. {
  113. pVar = iVar;
  114. if (iSetTo)
  115. *pVar = iSetTo;
  116. }
  117. ~AutoDelete() {
  118. safe_delete(*pVar);
  119. }
  120. private:
  121. T** pVar;
  122. };
  123. #endif