最近在游戲編程精粹4(Game Programming Gems 4)中看到了對于XDS的介紹,解開了我對于XML低效的困惑。也許在小型的XML應(yīng)用中不覺得,但是在大數(shù)據(jù)量的應(yīng)用中XML的速度甚至無法和普通的.ini相提并論。首先讓我們來看看XDS是什么吧。
XDS技術(shù)由DSD和XDS兩種文件格式組成。前者跟XSD相似,后者跟XML相似,只不過這兩種格式都是二進制的。正是采用了二進制格式,無論是在體積還是在速度上XDS的性能比XML都有明顯的提升。目前支持XDS的免費庫主要有XDSToolkit,現(xiàn)在可以下載到1.03版本。這是一個開源項目,解壓后我們可以看到它由兩個工具一個API包組成,另外還附一個例子。兩個工具的名字分別叫做xdsConvert和xdsMakeSchema,分別是用來進行XML和XDS相互轉(zhuǎn)換,以及生成DSD文件的。
在一個C/C++項目中,我們經(jīng)常需要用struct定義一系列數(shù)據(jù)結(jié)構(gòu)。xdsMakeSchema就可以通過輸入數(shù)據(jù)結(jié)構(gòu)的定義文件.h來生成DSD和相應(yīng)的c頭文件。在一個項目的初期,你可能需要用XML編輯器來編寫這個項目所需要的XML數(shù)據(jù),然后在程序中通過XDSLiteAPI來進行解析。這套API有兩個Paser,一個服務(wù)于XML,另一個服務(wù)于XDS。當(dāng)你的項目完全可以自動生成XML的時候就可以由XML轉(zhuǎn)向XDS了。游戲編程精粹中解釋的很詳細(xì),這邊就說說需要注意的地方了。
要利用API對XDS進行解析需要以下步驟:
① 以struct定義的C數(shù)據(jù)類型
② XDS的數(shù)據(jù)類型定義,可以在DSD中,也可以在程序中定義
③ 回調(diào)函數(shù)的編寫,主要是XDS_PROCESSNODE函數(shù)
以該工具包附帶的Powerup為例,struct看起來是這樣的:
struct PowerUp_t {
char szName[10]; // display name
char szImage[16]; // image file name
// health increase/decrease (-128 to 127)
signed char iHealth;
// temporary abilities/penalties
// (value is duration in seconds)
unsigned char iInvulnerability;
unsigned char iFastMove;
unsigned char iHighJump;
unsigned char iStunPlayer;
// extra life (count)
unsigned char iLifeUp;
};
// global power-up definition cache
extern struct PowerUp_t *g_PowerUps;
可以通過使用xdsMakeSchema來生成dsd,同時生成的xxxx_dsd.h只是為了免除將dsd文件讀入內(nèi)存,查看它的內(nèi)容就可以看到它定義了一個dsd數(shù)組:
// XDS DSD literal -- use this in calls to xdsInit()
//
#ifdef DEFINE_DSD
const unsigned char XDSDSD_Powerups[216] = {
0x58, 0x44, 0x53, 0x21, 0x30, 0x33, 。。。
};
#else
extern const unsigned char XDSDSD_Powerups[216];
#endif
// XDS DSD IDs -- use these in implementation of XDS_PROCESSNODE()
//
#define XDS_Powerups_Powerup 0x0100 // Record
#define XDS_Powerups_PowerUp_t 0x0101 // Type
#define XDS_Powerups__xdsType1 0x0102 // Type
#define XDS_Powerups_g_PowerUps 0x0103 // Element
同時還定義了一些常量,這些常量在解析xds中會用到。
除了在dsd中對于xds格式的定義之外,我們還可以在main.cpp中看到程序內(nèi)的定義:
#ifdef XDS_SUPPORT_DEFTYPE
void regDsd(struct xdsHandle *hXds)
{
// Register my types (test only)
xdsDefRecord(hXds, "Powerup", 2);
unsigned short iStructType = xdsDefStructType(hXds, "PowerUp_t");
xdsDefStructField(hXds, iStructType, "szName", XDS_TYPE_CHAR, 10);
xdsDefStructField(hXds, iStructType, "szImage", XDS_TYPE_CHAR, 16);
xdsDefStructField(hXds, iStructType, "iHealth", XDS_TYPE_CHAR, 0);
xdsDefStructField(hXds, iStructType, "iInvulnerability", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iFastMove", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iHighJump", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iStunPlayer", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iLifeUp", XDS_TYPE_BYTE, 0);
xdsDefStructDone(hXds, iStructType);
unsigned short iArrayType = xdsDefArrayType(hXds, "_xdsType1", iStructType, 0, 2);
xdsDefElement(hXds, "g_PowerUps", iArrayType, 0);
}
#endif
注意:交叉使用dsd定義和程序定義容易造成一個錯誤,就是在程序和dsd可能在定義的時候沖突,數(shù)據(jù)類型沖突,或者數(shù)據(jù)長度沖突,從而導(dǎo)致程序的崩潰。附帶的例子中程序定義數(shù)據(jù)類型如下:
#ifdef XDS_SUPPORT_DEFTYPE
void regDsd(struct xdsHandle *hXds)
{
// Register my types (test only)
xdsDefRecord(hXds, "Powerup", 4);
unsigned short iStructType = xdsDefStructType(hXds, "PowerUp_t");
xdsDefStructField(hXds, iStructType, "szName", XDS_TYPE_CHAR, 10);
xdsDefStructField(hXds, iStructType, "szImage", XDS_TYPE_CHAR, 16);
xdsDefStructField(hXds, iStructType, "iHealth", XDS_TYPE_CHAR, 0);
xdsDefStructField(hXds, iStructType, "iInvulnerability", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iFastMove", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iHighJump", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iStunPlayer", XDS_TYPE_BYTE, 0);
xdsDefStructField(hXds, iStructType, "iLifeUp", XDS_TYPE_BYTE, 0);
xdsDefStructDone(hXds, iStructType);
unsigned short iArrayType = xdsDefArrayType(hXds, "_xdsType1", iStructType, 0, 2);
xdsDefElement(hXds, "g_PowerUps", iArrayType, 0);
}
#endif
要是在生成dsd時用參數(shù)-r Powerup:2而這里用xdsDefRecord(hXds, "Powerup", 4)的話就會導(dǎo)致沖突。