1 #ifndef _SQLITE_LOGGER_COMMON_H
2 #define _SQLITE_LOGGER_COMMON_H
3 
4 #include <string.h>
5 #include "NativeTypes.h"
6 
7 namespace RakNet
8 {
9 	class BitStream;
10 
11 enum SQLLoggerPrimaryDataType
12 {
13 	SQLLPDT_POINTER,
14 	SQLLPDT_INTEGER,
15 	SQLLPDT_REAL,
16 	SQLLPDT_TEXT,
17 	SQLLPDT_BLOB,
18 	SQLLPDT_IMAGE,
19 	SQLLPDT_COUNT,
20 };
21 
22 extern "C" {
23 const char *GetSqlDataTypeName(SQLLoggerPrimaryDataType idx);
24 };
25 
26 struct BlobDescriptor
27 {
BlobDescriptorBlobDescriptor28 	BlobDescriptor() {}
BlobDescriptorBlobDescriptor29 	BlobDescriptor(void *_data, int _size) : data(_data), size(_size) {}
30 	void *data;
31 	int size;
32 };
33 struct RGBImageBlob
34 {
35 	enum ImageBlobCompressionMode
36 	{
37 		// Fast to compress and read back (1-5 milliseconds to compress depending on image size, server must have NVidia based 3d card)
38 		DXT,
39 		// Good storage, slow to compress (about 20 milliseconds for 320x200 on my system).
40 		JPG
41 	};
RGBImageBlobRGBImageBlob42 	RGBImageBlob() {data=0; imageWidth=0; imageHeight=0; linePitch=0; input_components=0; compressionMode=DXT; sourceFormatIsBGRA=true;}
dataRGBImageBlob43 	RGBImageBlob(void *_data, uint16_t _imageWidth, uint16_t _imageHeight, uint16_t _linePitch, unsigned char _input_components, bool _sourceFormatIsBGRA=true, ImageBlobCompressionMode mode=DXT) : data(_data), imageWidth(_imageWidth), imageHeight(_imageHeight), linePitch(_linePitch), input_components(_input_components),sourceFormatIsBGRA(_sourceFormatIsBGRA),compressionMode((unsigned char) mode) {}
44 	// This is just for testing
45 	void SaveToTGA(const char *filename);
46 	void *data;
47 	uint16_t imageWidth;
48 	uint16_t imageHeight;
49 	uint16_t linePitch; // bytes per row, may be larger than image width, in which case the excess is discarded
50 	unsigned char input_components; // 3 for RGB, 4 for RGBA
51 	unsigned char compressionMode;
52 	bool sourceFormatIsBGRA; // If true, then G and B will be swapped on the server so that the input is RGBA. This is necessary with Direct3D9
53 };
54 
55 #define MAX_SQLLITE_LOGGER_PARAMETERS 12
56 
57 struct LogParameter
58 {
LogParameterLogParameter59 	LogParameter() {}
60 
61 	/*
62 	template <class T> LogParameter(const T t) : type(SQLLPDT_UNKNOWN), size(sizeof(t)), data(&t) {}
63 	template <> LogParameter(const void *t) : type(SQLLPDT_POINTER), size(sizeof(t)), data(&t) {}
64 	template <> LogParameter(const unsigned char t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
65 	template <> LogParameter(const char t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
66 	template <> LogParameter(const unsigned int t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
67 	template <> LogParameter(const int t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
68 	template <> LogParameter(const unsigned short t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
69 	template <> LogParameter(const short t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
70 	template <> LogParameter(const unsigned long long t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
71 	template <> LogParameter(const long long t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(&t) {}
72 	template <> LogParameter(const float t) : type(SQLLPDT_REAL), size(sizeof(t)), data(&t) {}
73 	template <> LogParameter(const double t) : type(SQLLPDT_REAL), size(sizeof(t)), data(&t) {}
74 	template <> LogParameter(const unsigned char *&t) : type(SQLLPDT_TEXT), size((uint32_t) strlen((const char*) t)), data(t) {}
75 	template <> LogParameter(const char *t) : type(SQLLPDT_TEXT), size((uint32_t) strlen(t)), data(t) {}
76 	template <> LogParameter(const BlobDescriptor *t) : type(SQLLPDT_BLOB), size(t->size), data(t->data) {}
77 	template <> LogParameter(const RGBImageBlob *t) : type(SQLLPDT_IMAGE), size(t->size), data(t->data), imageDescriptorFormat(t->imageDescriptorFormat), bytesPerPixel(t->bytesPerPixel), imageWidth(t->imageWidth)  {}
78 	template <> LogParameter(const BlobDescriptor t) : type(SQLLPDT_BLOB), size(t.size), data(t.data) {}
79 	template <> LogParameter(const RGBImageBlob t) : type(SQLLPDT_IMAGE), size(t.size), data(t.data), imageDescriptorFormat(t.imageDescriptorFormat), bytesPerPixel(t.bytesPerPixel), imageWidth(t.imageWidth) {}
80 	*/
81 
LogParameterLogParameter82 	LogParameter(void *t) : type(SQLLPDT_POINTER), size(sizeof(t)), data(t) {}
LogParameterLogParameter83 	LogParameter(unsigned char t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter84 	LogParameter(char t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter85 	LogParameter(unsigned int t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter86 	LogParameter(int t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter87 	LogParameter(unsigned short t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter88 	LogParameter(short t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter89 	LogParameter(unsigned long long t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter90 	LogParameter(long long t) : type(SQLLPDT_INTEGER), size(sizeof(t)), data(t) {}
LogParameterLogParameter91 	LogParameter(float t) : type(SQLLPDT_REAL), size(sizeof(t)), data(t) {}
LogParameterLogParameter92 	LogParameter(double t) : type(SQLLPDT_REAL), size(sizeof(t)), data(t) {}
LogParameterLogParameter93 	LogParameter(unsigned char *t) : type(SQLLPDT_TEXT), size((uint32_t) strlen((const char*) t)), data(t) {}
LogParameterLogParameter94 	LogParameter(char *t) : type(SQLLPDT_TEXT), size((uint32_t) strlen(t)), data(t) {}
LogParameterLogParameter95 	LogParameter(const char t[]) : type(SQLLPDT_TEXT), size((uint32_t) strlen(t)), data(t) {}
LogParameterLogParameter96 	LogParameter(const unsigned char t[]) : type(SQLLPDT_TEXT), size((uint32_t) strlen((const char*) t)), data(t) {}
LogParameterLogParameter97 	LogParameter(BlobDescriptor *t) : type(SQLLPDT_BLOB), size(t->size), data(t->data) {}
LogParameterLogParameter98 	LogParameter(RGBImageBlob *t) : type(SQLLPDT_IMAGE), size(t->linePitch*t->imageHeight), data(t->data), imageWidth(t->imageWidth), imageHeight(t->imageHeight), linePitch(t->linePitch), input_components(t->input_components), compressionMode(t->compressionMode), sourceFormatIsBGRA(t->sourceFormatIsBGRA)  {}
LogParameterLogParameter99 	LogParameter(BlobDescriptor t) : type(SQLLPDT_BLOB), size(t.size), data(t.data) {}
LogParameterLogParameter100 	LogParameter(RGBImageBlob t) : type(SQLLPDT_IMAGE), size(t.linePitch*t.imageHeight), data(t.data), imageWidth(t.imageWidth), imageHeight(t.imageHeight), linePitch(t.linePitch), input_components(t.input_components), compressionMode(t.compressionMode), sourceFormatIsBGRA(t.sourceFormatIsBGRA) {}
101 
102 	SQLLoggerPrimaryDataType type;
103 	uint32_t size;
104 
105 	union DataUnion
106 	{
DataUnion()107 		DataUnion() {}
DataUnion(void * t)108 		DataUnion(void *t) : vptr(t) {}
DataUnion(unsigned char t)109 		DataUnion(unsigned char t) : c(t) {}
DataUnion(char t)110 		DataUnion(char t) : c(t) {}
DataUnion(unsigned int t)111 		DataUnion(unsigned int t) : i(t) {}
DataUnion(int t)112 		DataUnion(int t) : i(t) {}
DataUnion(unsigned short t)113 		DataUnion(unsigned short t) : s(t) {}
DataUnion(short t)114 		DataUnion(short t) : s(t) {}
DataUnion(unsigned long long t)115 		DataUnion(unsigned long long t) : ll(t) {}
DataUnion(long long t)116 		DataUnion(long long t) : ll(t) {}
DataUnion(float t)117 		DataUnion(float t) : f(t) {}
DataUnion(double t)118 		DataUnion(double t) : d(t) {}
DataUnion(unsigned char * t)119 		DataUnion(unsigned char *t) : ucptr(t) {}
DataUnion(char * t)120 		DataUnion(char *t) : cptr(t) {}
DataUnion(const char t[])121 		DataUnion(const char t[]) : cptr((char*) t) {}
DataUnion(const unsigned char t[])122 		DataUnion(const unsigned char t[]) : cptr((char*) t) {}
DataUnion(BlobDescriptor * t)123 		DataUnion(BlobDescriptor *t) : vptr(t->data) {}
DataUnion(RGBImageBlob * t)124 		DataUnion(RGBImageBlob *t) : vptr(t->data) {}
DataUnion(const BlobDescriptor & t)125 		DataUnion(const BlobDescriptor &t) : vptr(t.data) {}
DataUnion(const RGBImageBlob & t)126 		DataUnion(const RGBImageBlob &t) : vptr(t.data) {}
127 
128 		void *vptr;
129 		char c;
130 		int i;
131 		short s;
132 		long long ll;
133 		float f;
134 		double d;
135 		char *cptr;
136 		unsigned char *ucptr;
137 	};
138 	DataUnion data;
139 
140 	// Only used for SQLLPDT_IMAGE
141 	uint16_t imageWidth;
142 	uint16_t imageHeight;
143 	uint16_t linePitch; // Pitch is bytes per row
144 	unsigned char input_components; // Bytes per pixel
145 	unsigned char compressionMode;
146 	bool sourceFormatIsBGRA;
147 
148 	void Serialize(RakNet::BitStream *bs) const;
149 	// Don't forget to deallocate after calling Deserialize
150 	bool Deserialize(RakNet::BitStream *bs);
151 	void DoNotFree(void);
152 	void Free(void);
153 	static void Free(void *v);
154 };
155 
156 }
157 
158 
159 #endif
160