SQLiteLoggerCommon.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "SQLiteLoggerCommon.h"
  2. #include "BitStream.h"
  3. using namespace RakNet;
  4. static const char *sqlDataTypeNames[SQLLPDT_COUNT] =
  5. {
  6. "INTEGER",
  7. "INTEGER",
  8. "NUMERIC",
  9. "TEXT",
  10. "BLOB",
  11. "BLOB",
  12. };
  13. extern "C" const char *GetSqlDataTypeName(SQLLoggerPrimaryDataType idx) {return sqlDataTypeNames[(int)idx];}
  14. void LogParameter::Serialize(RakNet::BitStream *bs) const
  15. {
  16. unsigned char c = type;
  17. bs->Write(c);
  18. bs->Write(size);
  19. switch (type)
  20. {
  21. case SQLLPDT_POINTER:
  22. case SQLLPDT_BLOB:
  23. case SQLLPDT_TEXT:
  24. bs->WriteAlignedBytes(data.ucptr, size);
  25. break;
  26. case SQLLPDT_IMAGE:
  27. bs->WriteAlignedBytes(data.ucptr, size);
  28. bs->Write(imageWidth);
  29. bs->Write(imageHeight);
  30. bs->Write(linePitch);
  31. bs->Write(input_components);
  32. bs->Write(compressionMode);
  33. break;
  34. case SQLLPDT_REAL:
  35. case SQLLPDT_INTEGER:
  36. {
  37. bs->WriteAlignedBytes((const unsigned char*) &data, size);
  38. bs->EndianSwapBytes(bs->GetNumberOfBytesUsed()-size,size);
  39. }
  40. break;
  41. }
  42. }
  43. bool LogParameter::Deserialize(RakNet::BitStream *bs)
  44. {
  45. bool b;
  46. unsigned char c;
  47. bs->Read(c);
  48. type=(SQLLoggerPrimaryDataType)c;
  49. b=bs->Read(size);
  50. if (size==0)
  51. {
  52. data.vptr=0;
  53. return b;
  54. }
  55. switch (type)
  56. {
  57. case SQLLPDT_POINTER:
  58. case SQLLPDT_BLOB:
  59. case SQLLPDT_TEXT:
  60. data.vptr=rakMalloc_Ex(size,_FILE_AND_LINE_);
  61. b=bs->ReadAlignedBytes(data.ucptr, size);
  62. break;
  63. case SQLLPDT_IMAGE:
  64. data.vptr=rakMalloc_Ex(size,_FILE_AND_LINE_);
  65. bs->ReadAlignedBytes(data.ucptr, size);
  66. bs->Read(imageWidth);
  67. bs->Read(imageHeight);
  68. bs->Read(linePitch);
  69. bs->Read(input_components);
  70. b=bs->Read(compressionMode);
  71. break;
  72. case SQLLPDT_REAL:
  73. case SQLLPDT_INTEGER:
  74. {
  75. b=bs->ReadAlignedBytes((unsigned char*) &data, size);
  76. if (bs->DoEndianSwap())
  77. bs->ReverseBytesInPlace((unsigned char *)&data, size);
  78. }
  79. break;
  80. }
  81. return b;
  82. }
  83. void LogParameter::DoNotFree(void)
  84. {
  85. type=SQLLPDT_COUNT;
  86. }
  87. void LogParameter::Free(void)
  88. {
  89. if (type==SQLLPDT_BLOB || type==SQLLPDT_TEXT || type==SQLLPDT_IMAGE || type==SQLLPDT_POINTER)
  90. Free(data.vptr);
  91. }
  92. void LogParameter::Free(void *v)
  93. {
  94. rakFree_Ex(v,_FILE_AND_LINE_);
  95. }
  96. #pragma pack(push)
  97. #pragma pack(1)
  98. // 18 bytes
  99. struct TGAHEADER {
  100. char idlength;
  101. char colourmaptype;
  102. char datatypecode;
  103. short int colourmaporigin;
  104. short int colourmaplength;
  105. char colourmapdepth;
  106. short int x_origin;
  107. short int y_origin;
  108. short width;
  109. short height;
  110. char bitsperpixel;
  111. char imagedescriptor;
  112. };
  113. #pragma pack(pop)
  114. void RGBImageBlob::SaveToTGA(const char *filename)
  115. {
  116. // DirectX Color format in memory is BGRA, and written as such to disk.
  117. // Written to disk is the correct side up (point of triangle facing up, as it should). However, TGA displays this incorrectly (upside down)
  118. // TGA color format, on disk, is BGRA.
  119. // DXT compressor input format is ARGB.
  120. // http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/
  121. FILE *fptr = fopen(filename, "wb");
  122. TGAHEADER h;
  123. memset(&h,0,sizeof(h));
  124. h.datatypecode=2;
  125. h.width=imageWidth;
  126. if (BitStream::IsBigEndian()==true)
  127. BitStream::ReverseBytesInPlace((unsigned char*) &h.width,sizeof(h.width));
  128. h.height=imageHeight;
  129. if (BitStream::IsBigEndian()==true)
  130. BitStream::ReverseBytesInPlace((unsigned char*) &h.height,sizeof(h.height));
  131. h.bitsperpixel=input_components*8;
  132. // TGAs have a flag indicating if they are upside down or right side up
  133. // Be sure to set right side up.
  134. // http://www.gamedev.net/community/forums/topic.asp?topic_id=42001
  135. h.imagedescriptor=(1<<5);
  136. fwrite(&h,1,sizeof(h),fptr);
  137. /*
  138. putc(0,fptr);
  139. putc(0,fptr);
  140. putc(2,fptr);
  141. putc(0,fptr); putc(0,fptr);
  142. putc(0,fptr); putc(0,fptr);
  143. putc(0,fptr);
  144. putc(0,fptr); putc(0,fptr);
  145. putc(0,fptr); putc(0,fptr);
  146. putc((imageWidth & 0x00FF),fptr);
  147. putc((imageWidth & 0xFF00) / 256,fptr);
  148. putc((imageHeight & 0x00FF),fptr);
  149. putc((imageHeight & 0xFF00) / 256,fptr);
  150. putc(input_components*8,fptr);
  151. putc(0,fptr);
  152. */
  153. for (int row=0; row < imageHeight; row++)
  154. {
  155. fwrite((char*) data+row*linePitch, input_components, imageWidth, fptr);
  156. }
  157. fclose(fptr);
  158. }
粤ICP备19079148号