diff options
Diffstat (limited to 'utils/xbox/toollib')
| -rw-r--r-- | utils/xbox/toollib/piclib.cpp | 557 | ||||
| -rw-r--r-- | utils/xbox/toollib/piclib.h | 61 | ||||
| -rw-r--r-- | utils/xbox/toollib/scriplib.cpp | 264 | ||||
| -rw-r--r-- | utils/xbox/toollib/scriplib.h | 28 | ||||
| -rw-r--r-- | utils/xbox/toollib/toollib.cpp | 1322 | ||||
| -rw-r--r-- | utils/xbox/toollib/toollib.h | 114 |
6 files changed, 2346 insertions, 0 deletions
diff --git a/utils/xbox/toollib/piclib.cpp b/utils/xbox/toollib/piclib.cpp new file mode 100644 index 0000000..fc0101a --- /dev/null +++ b/utils/xbox/toollib/piclib.cpp @@ -0,0 +1,557 @@ +#include "toollib.h" +#include "piclib.h" + +byte_t* g_tgabuffer; +byte_t* g_tgabuffptr; + +/***************************************************************************** + TL_LoadPCX + +*****************************************************************************/ +void TL_LoadPCX(char* filename, byte_t** pic, byte_t** palette, int* width, int* height) +{ + byte_t* raw; + pcx_t* pcx; + int x; + int y; + int len; + int databyte; + int runlength; + byte_t* out; + byte_t* pix; + + // load the file + len = TL_LoadFile(filename,(void **)&raw); + + // parse the PCX file + pcx = (pcx_t*)raw; + raw = &pcx->data; + + pcx->xmin = TL_LittleShort(pcx->xmin); + pcx->ymin = TL_LittleShort(pcx->ymin); + pcx->xmax = TL_LittleShort(pcx->xmax); + pcx->ymax = TL_LittleShort(pcx->ymax); + pcx->hres = TL_LittleShort(pcx->hres); + pcx->vres = TL_LittleShort(pcx->vres); + pcx->bytes_per_line = TL_LittleShort(pcx->bytes_per_line); + pcx->palette_type = TL_LittleShort(pcx->palette_type); + + if (pcx->manufacturer != 0x0a || + pcx->version != 5 || + pcx->encoding != 1 || + pcx->bits_per_pixel != 8 || + pcx->xmax >= 640 || + pcx->ymax >= 480) + TL_Error("Bad pcx file %s",filename); + + if (palette) + { + *palette = (byte_t*)TL_Malloc(768); + memcpy(*palette,(byte_t*)pcx + len - 768,768); + } + + if (width) + *width = pcx->xmax+1; + + if (height) + *height = pcx->ymax+1; + + if (!pic) + return; + + out = (byte_t*)TL_Malloc((pcx->ymax+1)*(pcx->xmax+1)); + *pic = out; + pix = out; + + for (y=0; y<=pcx->ymax; y++, pix += pcx->xmax+1) + { + for (x=0; x<=pcx->xmax; ) + { + databyte = *raw++; + + if((databyte & 0xC0) == 0xC0) + { + runlength = databyte & 0x3F; + databyte = *raw++; + } + else + runlength = 1; + + while (runlength-- > 0) + pix[x++] = databyte; + } + } + + if (raw - (byte_t *)pcx > len) + TL_Error("PCX file %s was malformed",filename); + + TL_Free(pcx); +} + +/***************************************************************************** + TL_SavePCX + +*****************************************************************************/ +void TL_SavePCX(char* filename, byte_t* data, int width, int height, byte_t* palette) +{ + int i; + int j; + int length; + pcx_t* pcx; + byte_t* pack; + + pcx = (pcx_t*)TL_Malloc(width*height*2+1000); + + pcx->manufacturer = 0x0A; // PCX id + pcx->version = 5; // 256 color + pcx->encoding = 1; // uncompressed + pcx->bits_per_pixel = 8; // 256 color + pcx->xmin = 0; + pcx->ymin = 0; + pcx->xmax = TL_LittleShort((short)(width-1)); + pcx->ymax = TL_LittleShort((short)(height-1)); + pcx->hres = TL_LittleShort((short)width); + pcx->vres = TL_LittleShort((short)height); + pcx->color_planes = 1; // chunky image + pcx->bytes_per_line = TL_LittleShort((short)width); + pcx->palette_type = TL_LittleShort(2); // not a grey scale + + // pack the image + pack = &pcx->data; + + for (i=0; i<height; i++) + { + for (j=0; j<width; j++) + { + if ((*data & 0xc0) != 0xC0) + *pack++ = *data++; + else + { + *pack++ = 0xC1; + *pack++ = *data++; + } + } + } + + // write the palette + *pack++ = 0x0C; + for (i=0; i<768; i++) + *pack++ = *palette++; + + // write output file + length = pack - (byte_t*)pcx; + TL_SaveFile(filename,pcx,length); + + TL_Free(pcx); +} + +/***************************************************************************** + TGA_GetByte + +*****************************************************************************/ +byte_t TGA_GetByte(void) +{ + return (*g_tgabuffptr++); +} + +/***************************************************************************** + TGA_GetShort + +*****************************************************************************/ +short TGA_GetShort(void) +{ + byte_t msb; + byte_t lsb; + + lsb = g_tgabuffptr[0]; + msb = g_tgabuffptr[1]; + + g_tgabuffptr += 2; + + return ((msb<<8)|lsb); +} + +/***************************************************************************** + TL_LoadTGA + +*****************************************************************************/ +void TL_LoadTGA(char* name, byte_t** pixels, int* width, int* height) +{ + int columns; + int rows; + int numPixels; + byte_t* pixbuf; + int row; + int column; + byte_t* targa_rgba; + tga_t targa_header; + byte_t red; + byte_t green; + byte_t blue; + byte_t alphabyte; + byte_t packetHeader; + byte_t packetSize; + byte_t j; + + TL_LoadFile(name,(void**)&g_tgabuffer); + g_tgabuffptr = g_tgabuffer; + + /* load unaligned tga data */ + targa_header.id_length = TGA_GetByte(); + targa_header.colormap_type = TGA_GetByte(); + targa_header.image_type = TGA_GetByte(); + targa_header.colormap_index = TGA_GetShort(); + targa_header.colormap_length = TGA_GetShort(); + targa_header.colormap_size = TGA_GetByte(); + targa_header.x_origin = TGA_GetShort(); + targa_header.y_origin = TGA_GetShort(); + targa_header.width = TGA_GetShort(); + targa_header.height = TGA_GetShort(); + targa_header.pixel_size = TGA_GetByte(); + targa_header.attributes = TGA_GetByte(); + + if (targa_header.image_type != 2 && targa_header.image_type != 10) + TL_Error("TL_LoadTGA: %s - Only type 2 and 10 targa RGB images supported",name); + + if ((targa_header.colormap_type != 0) || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24)) + TL_Error("TL_LoadTGA: %s - Only 32 or 24 bit images supported (no colormaps)",name); + + columns = targa_header.width; + rows = targa_header.height; + numPixels = columns * rows; + + if (width) + *width = columns; + if (height) + *height = rows; + + targa_rgba = (byte_t*)TL_Malloc(numPixels*4); + *pixels = targa_rgba; + + if (targa_header.id_length != 0) + { + // skip TARGA image comment + g_tgabuffptr += targa_header.id_length; + } + + if (targa_header.image_type==2) + { + // Uncompressed, RGB images + for (row=rows-1; row>=0; row--) + { + pixbuf = targa_rgba + row*columns*4; + for(column=0; column<columns; column++) + { + switch (targa_header.pixel_size) + { + case 24: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = 255; + break; + + case 32: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = TGA_GetByte(); + break; + } + + *pixbuf++ = red; + *pixbuf++ = green; + *pixbuf++ = blue; + *pixbuf++ = alphabyte; + + } + } + } + else if (targa_header.image_type==10) + { + // Runlength encoded RGB images + for (row=rows-1; row>=0; row--) + { + pixbuf = targa_rgba + row*columns*4; + for(column=0; column<columns; ) + { + packetHeader = TGA_GetByte(); + packetSize = 1 + (packetHeader & 0x7f); + if (packetHeader & 0x80) + { + // run-length packet + switch (targa_header.pixel_size) + { + case 24: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = 255; + break; + + case 32: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = TGA_GetByte(); + break; + } + + for(j=0; j<packetSize; j++) + { + *pixbuf++ = red; + *pixbuf++ = green; + *pixbuf++ = blue; + *pixbuf++ = alphabyte; + column++; + + if (column==columns) + { + // run spans across rows + column=0; + if (row>0) + row--; + else + goto breakOut; + pixbuf = targa_rgba + row*columns*4; + } + } + } + else + { + // non run-length packet + for(j=0; j<packetSize; j++) + { + switch (targa_header.pixel_size) + { + case 24: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = 255; + break; + + case 32: + blue = TGA_GetByte(); + green = TGA_GetByte(); + red = TGA_GetByte(); + alphabyte = TGA_GetByte(); + break; + } + + *pixbuf++ = red; + *pixbuf++ = green; + *pixbuf++ = blue; + *pixbuf++ = alphabyte; + column++; + + if (column == columns) + { + // pixel packet run spans across rows + column=0; + if (row>0) + row--; + else + goto breakOut; + pixbuf = targa_rgba + row*columns*4; + } + } + } + } +breakOut:; + } + } + + TL_Free(g_tgabuffer); +} + +/***************************************************************************** + TL_SaveTGA + + Saves TGA. Supports r/w 16/24/32 bpp. +*****************************************************************************/ +void TL_SaveTGA(char* filename, byte_t* pixels, int width, int height, int sbpp, int tbpp) +{ + int handle; + tga_t tga; + unsigned short rgba5551; + unsigned long rgba8888; + int r; + int g; + int b; + int x; + int y; + int a; + byte_t* tgabuffer; + byte_t* tgabufferptr; + byte_t* rawbufferptr; + byte_t* tempbuffer; + byte_t* tempbufferptr; + int bytesperpixel; + + // all source is upsampled into easy 32 bit rgba8888 + // and downsampled into tga buffer + tempbuffer = (byte_t*)TL_Malloc(width*height*4); + + if (sbpp == 16) + { + /* source is 16 bit rgba */ + rawbufferptr = pixels; + for (y=0; y<height; y++) + { + tempbufferptr = tempbuffer + y*width*4; + for (x=0; x<width; x++) + { + rgba5551 = *(unsigned short*)rawbufferptr; + r = (rgba5551 & 0xF800)>>11; + g = (rgba5551 & 0x07C0)>>6; + b = (rgba5551 & 0x003E)>>1; + a = (rgba5551 & 0x01); + tempbufferptr[0] = (byte_t)(b * (255.0/31.0)); + tempbufferptr[1] = (byte_t)(g * (255.0/31.0)); + tempbufferptr[2] = (byte_t)(r * (255.0/31.0)); + tempbufferptr[3] = (byte_t)(a * 255.0); + + rawbufferptr += sizeof(unsigned short); + tempbufferptr += 4; + } + } + } + else if (sbpp == 24) + { + /* source is 24 bit rgba */ + rawbufferptr = pixels; + for (y=0; y<height; y++) + { + tempbufferptr = tempbuffer + y*width*4; + for (x=0; x<width; x++) + { + tempbufferptr[0] = rawbufferptr[0]; + tempbufferptr[1] = rawbufferptr[1]; + tempbufferptr[2] = rawbufferptr[2]; + tempbufferptr[3] = 255; + + rawbufferptr += 3; + tempbufferptr += 4; + } + } + } + else if (sbpp == 32) + { + /* source is 32 bit rgba */ + memcpy(tempbuffer,pixels,width*height*4); + } + else + TL_Error("TL_SaveTGA: cannot handle source %d bits per pixel",sbpp); + + if (tbpp == 16) + bytesperpixel = 2; + else if (tbpp == 24) + bytesperpixel = 3; + else if (tbpp == 32) + bytesperpixel = 4; + else + TL_Error("TL_SaveTGA: cannot handle target %d bits per pixel",tbpp); + + handle = TL_SafeOpenWrite(filename); + + /* write the targa header */ + tga.id_length = 0; + tga.colormap_type = 0; + tga.image_type = 2; + tga.colormap_index = 0; + tga.colormap_length = 0; + tga.colormap_size = 0; + tga.x_origin = 0; + tga.y_origin = 0; + tga.width = width; + tga.height = height; + tga.pixel_size = tbpp; + tga.attributes = 0; + + TL_SafeWrite(handle,&tga.id_length,sizeof(tga.id_length)); + TL_SafeWrite(handle,&tga.colormap_type,sizeof(tga.colormap_type)); + TL_SafeWrite(handle,&tga.image_type,sizeof(tga.image_type)); + TL_SafeWrite(handle,&tga.colormap_index,sizeof(tga.colormap_index)); + TL_SafeWrite(handle,&tga.colormap_length,sizeof(tga.colormap_length)); + TL_SafeWrite(handle,&tga.colormap_size,sizeof(tga.colormap_size)); + TL_SafeWrite(handle,&tga.x_origin,sizeof(tga.x_origin)); + TL_SafeWrite(handle,&tga.y_origin,sizeof(tga.y_origin)); + TL_SafeWrite(handle,&tga.width,sizeof(tga.width)); + TL_SafeWrite(handle,&tga.height,sizeof(tga.height)); + TL_SafeWrite(handle,&tga.pixel_size,sizeof(tga.pixel_size)); + TL_SafeWrite(handle,&tga.attributes,sizeof(tga.attributes)); + + /* tga images are upside down left to right - !@#$% */ + tgabuffer = (byte_t*)TL_Malloc(width*height*bytesperpixel); + + /* source is 32 bit rgba */ + rawbufferptr = tempbuffer; + for (y=height-1; y>=0; y--) + { + tgabufferptr = tgabuffer + y*width*bytesperpixel; + for (x=0; x<width; x++) + { + switch (bytesperpixel) + { + case 2: + break; + + case 3: + rgba8888 = TL_BigLong(*(unsigned long*)rawbufferptr); + r = (rgba8888 & 0xFF000000)>>24; + g = (rgba8888 & 0x00FF0000)>>16; + b = (rgba8888 & 0x0000FF00)>>8; + + tgabufferptr[0] = b; + tgabufferptr[1] = g; + tgabufferptr[2] = r; + break; + + case 4: + rgba8888 = TL_BigLong(*(unsigned long*)rawbufferptr); + r = (rgba8888 & 0xFF000000)>>24; + g = (rgba8888 & 0x00FF0000)>>16; + b = (rgba8888 & 0x0000FF00)>>8; + a = rgba8888 & 0xFF; + + tgabufferptr[0] = b; + tgabufferptr[1] = g; + tgabufferptr[2] = r; + tgabufferptr[3] = a; + break; + } + + rawbufferptr += 4; + tgabufferptr += bytesperpixel; + } + } + + TL_SafeWrite(handle,tgabuffer,width*height*bytesperpixel); + close(handle); + + TL_Free(tempbuffer); + TL_Free(tgabuffer); +} + +/***************************************************************************** + TL_LoadImage + + Loads an image based on extension. +*****************************************************************************/ +void TL_LoadImage(char* name, byte_t** pixels, byte_t** palette, int* width, int* height) +{ + char ext[16]; + + TL_GetExtension(name,ext); + if (!stricmp(ext,"pcx")) + TL_LoadPCX(name,pixels,palette,width,height); + else if (!stricmp(ext,"tga")) + { + TL_LoadTGA(name,pixels,width,height); + *palette = NULL; + } + else + TL_Error("TL_LoadImage: unknown image extension %s",ext); +} + diff --git a/utils/xbox/toollib/piclib.h b/utils/xbox/toollib/piclib.h new file mode 100644 index 0000000..346d94c --- /dev/null +++ b/utils/xbox/toollib/piclib.h @@ -0,0 +1,61 @@ +#ifndef _PICLIB_H_ +#define _PICLIB_H_ + +typedef enum +{ + ms_none, + ms_mask, + ms_transcolor, + ms_lasso +} +mask_t; + +typedef enum +{ + cm_none, + cm_rle1 +} +compress_t; + +typedef struct +{ + char manufacturer; + char version; + char encoding; + char bits_per_pixel; + unsigned short xmin,ymin,xmax,ymax; + unsigned short hres,vres; + unsigned char palette[48]; + char reserved; + char color_planes; + unsigned short bytes_per_line; + unsigned short palette_type; + char filler[58]; + unsigned char data; +} pcx_t; + +typedef struct +{ + unsigned char id_length; + unsigned char colormap_type; + unsigned char image_type; + unsigned char pad1; // not in file + unsigned short colormap_index; + unsigned short colormap_length; + unsigned char colormap_size; + unsigned char pad2; // not in file + unsigned short x_origin; + unsigned short y_origin; + unsigned short width; + unsigned short height; + unsigned char pixel_size; + unsigned char attributes; +} tga_t; + +extern void TL_LoadPCX(char* filename, byte_t** picture, byte_t** palette, int* width, int* height); +extern void TL_SavePCX(char* filename, byte_t* data, int width, int height, byte_t* palette); +extern void TL_LoadTGA(char* name, byte_t** pixels, int* width, int* height); +extern void TL_SaveTGA(char* filename, byte_t* pixels, int width, int height, int sbpp, int tbpp); +extern void TL_LoadImage(char* name, byte_t** pixels, byte_t** palette, int* width, int* height); + +#endif diff --git a/utils/xbox/toollib/scriplib.cpp b/utils/xbox/toollib/scriplib.cpp new file mode 100644 index 0000000..e674673 --- /dev/null +++ b/utils/xbox/toollib/scriplib.cpp @@ -0,0 +1,264 @@ +#include "toollib.h" + +char g_tl_token[MAXTOKEN]; +char* g_tl_scriptbuff; +char* g_tl_scriptptr; +char* g_tl_scriptendptr; +int g_tl_scriptsize; +int g_tl_scriptline; +bool g_tl_endofscript; +bool g_tl_tokenready; +int g_tl_oldscriptline; +char* g_tl_oldscriptptr; + +/***************************************************************************** + TL_FreeScriptFile + +*****************************************************************************/ +void TL_FreeScriptFile(void) +{ + if (g_tl_scriptbuff) + { + TL_Free(g_tl_scriptbuff); + g_tl_scriptbuff = NULL; + } +} + +/***************************************************************************** + TL_LoadScriptFile + +*****************************************************************************/ +void TL_LoadScriptFile(const char* filename) +{ + g_tl_scriptsize = TL_LoadFile(filename,(void **)&g_tl_scriptbuff); + + TL_ResetParser(); +} + +/***************************************************************************** + TL_SetScriptData + +*****************************************************************************/ +void TL_SetScriptData(char* data, int length) +{ + g_tl_scriptbuff = data; + g_tl_scriptsize = length; + + TL_ResetParser(); +} + +/***************************************************************************** + TL_UnGetToken + +*****************************************************************************/ +void TL_UnGetToken(void) +{ + g_tl_tokenready = true; +} + +/***************************************************************************** + TL_GetToken + +*****************************************************************************/ +char* TL_GetToken(bool crossline) +{ + char* tokenptr; + + if (g_tl_tokenready) + { + g_tl_tokenready = false; + return (g_tl_token); + } + + g_tl_token[0] = '\0'; + + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + if (!crossline) + TL_Error("TL_GetToken: Line %d is incomplete",g_tl_scriptline); + + g_tl_endofscript = true; + return (NULL); + } + +skipspace: + while (*g_tl_scriptptr <= ' ') + { + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + if (!crossline) + TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline); + + g_tl_endofscript = true; + return (NULL); + } + + if (*g_tl_scriptptr++ == '\n') + { + if (!crossline) + TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline); + + g_tl_scriptline++; + } + } + + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + if (!crossline) + TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline); + + g_tl_endofscript = true; + return (NULL); + } + + // skip commented line + if ((g_tl_scriptptr[0] == ';') || (g_tl_scriptptr[0] == '/' && g_tl_scriptptr[1] == '/')) + { + if (!crossline) + TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline); + + while (*g_tl_scriptptr++ != '\n') + { + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + g_tl_endofscript = true; + return (NULL); + } + } + + g_tl_scriptline++; + goto skipspace; + } + + tokenptr = g_tl_token; + if (g_tl_scriptptr[0] == '\"' && g_tl_scriptptr[1]) + { + // copy quoted token + do + { + *tokenptr++ = *g_tl_scriptptr++; + if (g_tl_scriptptr == g_tl_scriptendptr) + break; + + if (tokenptr == &g_tl_token[MAXTOKEN]) + TL_Error("GetToken: Token too large on line %i",g_tl_scriptline); + } + while (*g_tl_scriptptr >= ' ' && *g_tl_scriptptr != '\"'); + + if (g_tl_scriptptr[0] == '\"') + *tokenptr++ = *g_tl_scriptptr++; + } + else + { + // copy token + while (*g_tl_scriptptr > ' ' && *g_tl_scriptptr != ';' && *g_tl_scriptptr != '\"') + { + *tokenptr++ = *g_tl_scriptptr++; + if (g_tl_scriptptr == g_tl_scriptendptr) + break; + + if (tokenptr == &g_tl_token[MAXTOKEN]) + TL_Error("GetToken: Token too large on line %i",g_tl_scriptline); + } + } + + *tokenptr = '\0'; + + return (g_tl_token); +} + +/***************************************************************************** + TL_SkipRestOfLine + +*****************************************************************************/ +void TL_SkipRestOfLine(void) +{ + while (*g_tl_scriptptr++ != '\n') + { + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + break; + } + } + + g_tl_scriptline++; +} + +/***************************************************************************** + TL_TokenAvailable + + Returns (TRUE) if token available on line. +*****************************************************************************/ +bool TL_TokenAvailable (void) +{ + char* ptr; + + ptr = g_tl_scriptptr; + while (*ptr <= ' ') + { + if (ptr >= g_tl_scriptendptr) + { + g_tl_endofscript = true; + return (false); + } + + if (*ptr++ == '\n') + return (false); + } + + return (true); +} + + +/***************************************************************************** + TL_EndOfScript + + Returns (TRUE) at end of script +*****************************************************************************/ +bool TL_EndOfScript(void) +{ + if (g_tl_scriptptr >= g_tl_scriptendptr) + { + g_tl_endofscript = true; + return (true); + } + + return (false); +} + +/***************************************************************************** + TL_ResetParser + +*****************************************************************************/ +void TL_ResetParser(void) +{ + g_tl_scriptptr = g_tl_scriptbuff; + g_tl_scriptendptr = g_tl_scriptptr + g_tl_scriptsize; + g_tl_scriptline = 1; + g_tl_endofscript = false; + g_tl_tokenready = false; +} + +/***************************************************************************** + TL_SaveParser + +*****************************************************************************/ +void TL_SaveParser(void) +{ + g_tl_oldscriptline = g_tl_scriptline; + g_tl_oldscriptptr = g_tl_scriptptr; +} + +/***************************************************************************** + TL_RestoreParser + +*****************************************************************************/ +void TL_RestoreParser(void) +{ + g_tl_scriptline = g_tl_oldscriptline; + g_tl_scriptptr = g_tl_oldscriptptr; +} + + + + diff --git a/utils/xbox/toollib/scriplib.h b/utils/xbox/toollib/scriplib.h new file mode 100644 index 0000000..292a16c --- /dev/null +++ b/utils/xbox/toollib/scriplib.h @@ -0,0 +1,28 @@ +#ifndef _SCRIPLIB_H_ +#define _SCRIPLIB_H_ + +#define MAXTOKEN 128 + +extern void TL_LoadScriptFile(const char* filename); +extern void TL_SetScriptData(char* data, int length); +extern void TL_FreeScriptFile(void); +extern char* TL_GetToken(bool crossline); +extern char* TL_GetQuotedToken(bool crossline); +extern void TL_UnGetToken(void); +extern bool TL_TokenAvailable(void); +extern void TL_SaveParser(void); +extern void TL_RestoreParser(void); +extern void TL_ResetParser(void); +extern void TL_SkipRestOfLine(void); +extern bool TL_EndOfScript(void); +extern char* TL_GetRawToken(void); + +extern char g_tl_token[MAXTOKEN]; +extern char* g_tl_scriptbuffer; +extern char* g_tl_scriptptr; +extern char* g_tl_scriptendptr; +extern int g_tl_scriptsize; // ydnar +extern int g_tl_scriptline; +extern bool g_tl_endofscript; + +#endif diff --git a/utils/xbox/toollib/toollib.cpp b/utils/xbox/toollib/toollib.cpp new file mode 100644 index 0000000..998bf91 --- /dev/null +++ b/utils/xbox/toollib/toollib.cpp @@ -0,0 +1,1322 @@ +#include "toollib.h" + +#ifdef _DEBUG +#define HEAP_CHECK +#endif + +int g_tl_argc; +char** g_tl_argv; +int g_tl_byteorder; +int g_tl_dircount; +char** g_tl_dirlist; +int g_tl_start; +int g_tl_abort; +bool g_tl_quiet; + +#pragma warning(disable:4311) +#pragma warning(disable:4267) + +/***************************************************************************** + TL_Setup + +*****************************************************************************/ +void TL_Setup(char* appname, int argc, char** argv) +{ + const char* buildStr; + + g_tl_argc = argc; + g_tl_argv = argv; + + g_tl_quiet = (TL_CheckParm("q") > 0) || (TL_CheckParm("quiet") > 0) || (TL_CheckParm("noheader") > 0); + + if (appname) + { + TL_printf("\n%s \n",appname); +#ifdef _DEBUG + buildStr = "Debug Build"; +#else + buildStr = "Release Build"; +#endif + TL_printf("%s - %s %s\n\n", buildStr, __DATE__, __TIME__); + } + + g_tl_abort = TL_CheckParm("abort"); + g_tl_start = TL_CPUCount(); +} + +/***************************************************************************** + TL_End + +*****************************************************************************/ +void TL_End(bool showtime) +{ + int end; + + if (showtime && !g_tl_quiet) + { + end = TL_CPUCount(); + TL_printf("\n%f seconds.\n",TL_CPUTime(g_tl_start,end)); + } +} + +/***************************************************************************** + TL_Error + +*****************************************************************************/ +void TL_Error(char* error, ...) +{ + va_list argptr; + + va_start(argptr,error); + vprintf(error,argptr); + va_end(argptr); + + printf("\n"); + +#if !defined( _X360 ) + __asm + { + int 3; + } +#endif + + if (g_tl_abort) + abort(); + + exit(-1); +} + +/***************************************************************************** + TL_CheckParm + + Returns the argument number (1 to argc-1) or 0 if not present +*****************************************************************************/ +int TL_CheckParm(char* check) +{ + int i; + char* parm; + + for (i=1; i<g_tl_argc; i++) + { + parm = g_tl_argv[i]; + + if (!isalpha(*parm)) + if (!*++parm) + continue; + + if (!stricmp(check,parm)) + return (i); + } + + return (0); +} + +/***************************************************************************** + TL_SafeRead + +*****************************************************************************/ +void TL_SafeRead(int handle, void* buffer, long count) +{ + if (_read(handle,buffer,count) != count) + TL_Error("SafeRead(): read failure"); +} + +/***************************************************************************** + TL_SafeOpenRead + +*****************************************************************************/ +int TL_SafeOpenRead(const char* filename) +{ + int handle; + + handle = _open(filename,_O_RDONLY|_O_BINARY); + if (handle == -1) + TL_Error("TL_SafeOpenRead(): Error opening %s: %s",filename,strerror(errno)); + + return (handle); +} + +/***************************************************************************** + TL_SafeOpenWrite + +*****************************************************************************/ +int TL_SafeOpenWrite(const char* filename) +{ + int handle; + + handle = _open(filename,_O_RDWR|_O_BINARY|_O_CREAT|_O_TRUNC,0666); + if (handle == -1) + TL_Error("TL_SafeOpenWrite(): Error opening %s: %s",filename,strerror(errno)); + + return (handle); +} + +/***************************************************************************** + TL_SafeWrite + +*****************************************************************************/ +void TL_SafeWrite(int handle, void* buffer, long count) +{ + int status; + + status = _write(handle,buffer,count); + if (status != count) + TL_Error("TL_SafeWrite(): write failure %d, errno=%d",status,errno); +} + +/***************************************************************************** + TL_SafeClose + +*****************************************************************************/ +void TL_SafeClose(int handle, int touch) +{ + // ensure date and time of modification get set + if (touch) + _futime(handle,NULL); + + close(handle); +} + +/***************************************************************************** + TL_Malloc + +*****************************************************************************/ +void* TL_Malloc(int size) +{ + void* ptr; + int newsize; + + newsize = size + sizeof(tlmem_t); + newsize = (newsize + 3) & ~3; + + ptr = malloc(newsize); + if (!ptr) + TL_Error("TL_Malloc(): failure for %lu bytes",size); + + memset(ptr,0,newsize); + + ((tlmem_t*)ptr)->id = TL_MEMID; + ((tlmem_t*)ptr)->size = size; + + return ((byte_t*)ptr + sizeof(tlmem_t)); +} + +/***************************************************************************** + TL_Free + +*****************************************************************************/ +void TL_Free(void* ptr) +{ + tlmem_t* memptr; + + if (!ptr) + TL_Error("TL_Free(): null pointer"); + + memptr = (tlmem_t*)((byte_t*)ptr - sizeof(tlmem_t)); + + if (((u32)memptr) & 3) + TL_Error("TL_Free(): bad pointer %8.8x",ptr); + + if (memptr->id != TL_MEMID) + TL_Error("TL_Free(): corrupted pointer %8.8x",ptr); + + memptr->id = 0; + memptr->size = 0; + + free(memptr); + +#ifdef HEAP_CHECK + if (_heapchk() != _HEAPOK) + TL_Error("TL_Free(): heap corrupted"); +#endif +} + +bool TL_Check(void* ptr) +{ + tlmem_t* memptr; + + if (!ptr) + return false; + + memptr = (tlmem_t*)((byte_t*)ptr - sizeof(tlmem_t)); + + if (((u32)memptr) & 3) + return false; + + if (memptr->id != TL_MEMID) + return false; + + return true; +} + +/***************************************************************************** + TL_Realloc + +*****************************************************************************/ +void* TL_Realloc(void* ptr, int newsize) +{ + int len; + tlmem_t* oldmemptr; + void* newptr; + + if (!ptr) + { + newptr = TL_Malloc(newsize); + return (newptr); + } + + oldmemptr = (tlmem_t*)((byte_t*)ptr - sizeof(tlmem_t)); + + if ((u32)oldmemptr & 3) + TL_Error("TL_Realloc(): bad pointer %8.8x",ptr); + + if (oldmemptr->id != TL_MEMID) + TL_Error("TL_Realloc(): corrupted pointer %8.8x",ptr); + + newptr = TL_Malloc(newsize); + + len = TL_min(newsize,oldmemptr->size); + + memcpy(newptr,ptr,len); + + TL_Free(ptr); + + return (newptr); +} + +/***************************************************************************** + TL_strncpyz + + Copy up to (N) bytes including appending null. +*****************************************************************************/ +void TL_strncpyz(char* dst, char* src, int n) +{ + if (n <= 0) + return; + + if (n > 1) + strncpy(dst,src,n-1); + + dst[n-1] = '\0'; +} + +/***************************************************************************** + TL_strncatz + + Concatenate up to dstsize bytes including appending null. +*****************************************************************************/ +void TL_strncatz(char* dst, char* src, int dstsize) +{ + int len; + + if (dstsize <= 0) + return; + + len = (int)strlen(dst); + + TL_strncpyz(dst+len,src,dstsize-len); +} + +/***************************************************************************** + TL_LoadFile + +*****************************************************************************/ +long TL_LoadFile(const char* filename, void** bufferptr) +{ + int handle; + long length; + char* buffer; + + handle = TL_SafeOpenRead(filename); + length = TL_FileLength(handle); + buffer = (char*)TL_Malloc(length+1); + TL_SafeRead(handle,buffer,length); + close(handle); + + // for parsing + buffer[length] = '\0'; + + *bufferptr = (void*)buffer; + + return (length); +} + +/***************************************************************************** + TL_TouchFile + +*****************************************************************************/ +void TL_TouchFile(char* filename) +{ + int h; + + h = _open(filename,_O_RDWR|_O_BINARY,0666); + if (h < 0) + return; + + _futime(h,NULL); + _close(h); +} + +/***************************************************************************** + TL_SaveFile + +*****************************************************************************/ +void TL_SaveFile(char* filename, void* buffer, long count) +{ + int handle; + + handle = TL_SafeOpenWrite(filename); + TL_SafeWrite(handle,buffer,count); + + TL_SafeClose(handle,true); +} + +/***************************************************************************** + TL_FileLength + +*****************************************************************************/ +long TL_FileLength(int handle) +{ + long pos; + long length; + + pos = lseek(handle,0,SEEK_CUR); + length = lseek(handle,0,SEEK_END); + lseek(handle,pos,SEEK_SET); + + return (length); +} + +/***************************************************************************** + TL_StripFilename + + Removes filename from path. +*****************************************************************************/ +void TL_StripFilename(char* path) +{ + int length; + + length = (int)strlen(path)-1; + while ((length > 0) && (path[length] != '\\') && (path[length] != '/') && (path[length] != ':')) + length--; + + /* leave possible seperator */ + if (!length) + path[0] = '\0'; + else + path[length+1] = '\0'; +} + +/***************************************************************************** + TL_StripExtension + + Removes extension from path. +*****************************************************************************/ +void TL_StripExtension(char* path) +{ + int length; + + length = (int)strlen(path)-1; + while (length > 0 && path[length] != '.') + length--; + + if (length && path[length] == '.') + path[length] = 0; +} + +/***************************************************************************** + TL_StripPath + + Removes path from full path. +*****************************************************************************/ +void TL_StripPath(char* path, char* dest) +{ + char* src; + + src = path + strlen(path); + while ((src != path) && (*(src-1) != '\\') && (*(src-1) != '/') && (*(src-1) != ':')) + src--; + + strcpy(dest,src); +} + +/***************************************************************************** + TL_GetExtension + + Gets any extension from the full path. +*****************************************************************************/ +void TL_GetExtension(char* path, char* dest) +{ + char* src; + + src = path + strlen(path) - 1; + + // back up until a . or the start + while (src != path && *(src-1) != '.') + src--; + + if (src == path) + { + *dest = '\0'; // no extension + return; + } + + strcpy(dest,src); +} + +/***************************************************************************** + TL_DefaultPath + + Adds basepath to head of path. +*****************************************************************************/ +void TL_DefaultPath(char* path, char* basepath) +{ + char temp[TL_MAXPATH]; + char* ptr; + char ch; + + if (path[0] == '\\') + { + // path is absolute + return; + } + + ptr = path; + while (1) + { + ch = *ptr++; + if (!ch) + break; + + if (ch == ':') + { + // path has a device - must be absolute + return; + } + } + + // place basepath at head of path + // do intermediate copy to preserve any arg wierdness + strcpy(temp,path); + strcpy(path,basepath); + strcat(path,temp); +} + +/***************************************************************************** + TL_AddSeperatorToPath + +*****************************************************************************/ +void TL_AddSeperatorToPath(char* inpath, char* outpath) +{ + int len; + + strcpy(outpath,inpath); + + len = (int)strlen(outpath); + if (outpath[len-1] != '\\') + { + outpath[len] = '\\'; + outpath[len+1] = '\0'; + } +} + +/***************************************************************************** + TL_DefaultExtension + + Adds extension a path that has no extension. +*****************************************************************************/ +void TL_DefaultExtension(char* path, char* extension, bool bForce) +{ + char* src; + + if ( !bForce && path[0] ) + { + src = path + strlen(path) - 1; + while ((src != path) && (*src != '\\') && (*src != '/')) + { + if (*src == '.') + return; + src--; + } + } + + strcat(path,extension); +} + +/***************************************************************************** + TL_ReplaceDosExtension + + Handles files of the form xxxx.xxxxxxx.xxxxx.zzz +*****************************************************************************/ +void TL_ReplaceDosExtension(char* path, char* extension) +{ + int len; + + len = (int)strlen(path); + if (!len) + return; + + if (path[len-1] == '.') + { + path[len-1] = '\0'; + strcat(path,extension); + return; + } + + if (len-4 > 0 && path[len-4] == '.') + path[len-4] = '\0'; + + strcat(path,extension); +} + +/***************************************************************************** + TL_ReplaceExtension + + Replaces any extension found after '.' +*****************************************************************************/ +void TL_ReplaceExtension(const char* inPath, const char* extension, char* outPath) +{ + int len; + char* src; + + if (outPath != inPath) + strcpy(outPath, inPath); + + len = (int)strlen(outPath); + if (!len) + return; + + if (outPath[len-1] == '.') + { + outPath[len-1] = '\0'; + strcat(outPath, extension); + return; + } + + src = outPath + len - 1; + while ((src != outPath) && (*src != '\\') && (*src != '/')) + { + if (*src == '.') + { + *src = '\0'; + break; + } + src--; + } + + strcat(outPath, extension); +} + +/***************************************************************************** + TL_TempFilename + + Builds a temporary filename at specified path. +*****************************************************************************/ +void TL_TempFilename(char* path) +{ + int len; + + len = (int)strlen(path); + if (len) + { + /* tack on appending seperator */ + if (path[len-1] != '\\') + { + path[len] = '\\'; + path[len+1] = '\0'; + } + } + + strcat(path,tmpnam(NULL)); +} + +/***************************************************************************** + TL_AlignFile + + TL_Aligns data in file to any boundary. +*****************************************************************************/ +int TL_AlignFile(int handle, int align) +{ + int i; + int pos; + int empty; + int count; + + empty = 0; + pos = lseek(handle,0,SEEK_CUR); + count = ((pos+align-1)/align)*align - pos; + + for (i=0; i<count; i++) + TL_SafeWrite(handle,&empty,1); + + return (pos+count); +} + +/***************************************************************************** + TL_GetByteOrder + + Gets byte ordering, true is bigendian. +*****************************************************************************/ +int TL_GetByteOrder(void) +{ + return (g_tl_byteorder); +} + +/***************************************************************************** + TL_SetByteOrder + + Sets byte ordering, true is bigendian. +*****************************************************************************/ +void TL_SetByteOrder(int flag) +{ + g_tl_byteorder = flag; +} + +/***************************************************************************** + TL_LongSwap + + Swap according to set state. +*****************************************************************************/ +long TL_LongSwap(long l) +{ + if (!g_tl_byteorder) + return (l); + + return (TL_BigLong(l)); +} + +/***************************************************************************** + TL_ShortSwap + + Swap according to set state. +*****************************************************************************/ +short TL_ShortSwap(short s) +{ + if (!g_tl_byteorder) + return (s); + + return (TL_BigShort(s)); +} + +/***************************************************************************** + TL_BigShort + + Converts native short to big endian +*****************************************************************************/ +short TL_BigShort(short l) +{ + byte_t b1; + byte_t b2; + + b1 = l&255; + b2 = (l>>8)&255; + + return (b1<<8) + b2; +} + +/***************************************************************************** + TL_LittleShort + + Converts native short to little endian +*****************************************************************************/ +short TL_LittleShort(short l) +{ + return (l); +} + +/***************************************************************************** + TL_BigLong + + Converts native long to big endian +*****************************************************************************/ +long TL_BigLong(long l) +{ + byte_t b1; + byte_t b2; + byte_t b3; + byte_t b4; + + b1 = (byte_t)(l&255); + b2 = (byte_t)((l>>8)&255); + b3 = (byte_t)((l>>16)&255); + b4 = (byte_t)((l>>24)&255); + + return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4; +} + +/***************************************************************************** + TL_LittleLong + + Converts native long to little endian +*****************************************************************************/ +long TL_LittleLong(long l) +{ + return (l); +} + +/***************************************************************************** + TL_BigFloat + + Converts native float to big endian +*****************************************************************************/ +float TL_BigFloat(float f) +{ + union + { + float f; + byte_t b[4]; + } dat1,dat2; + + dat1.f = f; + dat2.b[0] = dat1.b[3]; + dat2.b[1] = dat1.b[2]; + dat2.b[2] = dat1.b[1]; + dat2.b[3] = dat1.b[0]; + + return (dat2.f); +} + +/***************************************************************************** + TL_Exists + + Returns TRUE if file exists. +*****************************************************************************/ +bool TL_Exists(const char* filename) +{ + FILE* test; + + if (!filename || !filename[0]) + return (false); + + if ((test = fopen(filename,"rb")) == NULL) + return (false); + + fclose(test); + + return (true); +} + +/***************************************************************************** + TL_FileTime + + Returns a file's time and data word. +*****************************************************************************/ +u32 TL_FileTime(char* filename) +{ + struct _finddata_t finddata; + intptr_t h; + + h = _findfirst(filename, &finddata); + if (h == -1) + return (0); + + _findclose(h); + + return (finddata.time_write); +} + +/***************************************************************************** + TL_SortNames + +*****************************************************************************/ +int TL_SortNames(const void *a, const void *b) +{ + return (strcmp(*((char **)a), *((char **)b))); +} + +/***************************************************************************** + TL_FindFiles + +*****************************************************************************/ +int TL_FindFiles(char* filemask, char*** filenames) +{ + struct _finddata_t finddata; + intptr_t h; + char sourcepath[TL_MAXPATH]; + int count; + int len; + char** names = NULL; + char* ptr; + + h = _findfirst(filemask,&finddata); + if (h == -1) + return (0); + + TL_strncpyz(sourcepath,filemask,TL_MAXPATH); + TL_StripFilename(sourcepath); + if (!sourcepath[0]) + strcpy(sourcepath,".\\"); + else + { + len = (int)strlen(sourcepath); + if (sourcepath[len-1] != '\\') + TL_strncatz(sourcepath,"\\",TL_MAXPATH); + } + + count = 0; + do + { + if (finddata.attrib & _A_SUBDIR) + continue; + + if (!count) + names = (char**)TL_Malloc(sizeof(char*)); + else + names = (char**)TL_Realloc(names,(count+1)*sizeof(char*)); + + ptr = (char*)TL_Malloc(TL_MAXPATH); + + names[count] = ptr; + TL_strncpyz(names[count],sourcepath,TL_MAXPATH); + TL_strncatz(names[count],finddata.name,TL_MAXPATH); + + count++; + } + while (!_findnext(h,&finddata)); + + _findclose(h); + + // ascending sort the names + qsort(names,count,sizeof(char*),TL_SortNames); + + *filenames = names; + return (count); +} + +/***************************************************************************** + TL_GetFileList + +*****************************************************************************/ +int TL_GetFileList(char* dirpath, char* pattern, tlfile_t*** filelist) +{ + struct _finddata_t finddata; + char sourcepath[TL_MAXPATH]; + char fullpath[TL_MAXPATH]; + char* filename; + intptr_t h; + int filecount; + int finddirs; + int len; + + filecount = 0; + + strcpy(sourcepath,dirpath); + len = (int)strlen(sourcepath); + + if (!len) + strcpy(sourcepath,".\\"); + else if (sourcepath[len-1] != '\\') + { + sourcepath[len] = '\\'; + sourcepath[len+1] = '\0'; + } + + strcpy(fullpath,sourcepath); + + if (pattern[0] == '\\' && pattern[1] == '\0') + { + // find directories + finddirs = true; + strcat(fullpath,"*"); + } + else + { + finddirs = false; + strcat(fullpath,pattern); + } + + h = _findfirst(fullpath,&finddata); + if (h == -1) + return (0); + + do + { + // dos attribute complexities i.e. _A_NORMAL is 0 + if (finddirs) + { + // skip non dirs + if (!(finddata.attrib & _A_SUBDIR)) + continue; + } + else + { + // skip dirs + if (finddata.attrib & _A_SUBDIR) + continue; + } + + if (!stricmp(finddata.name,".")) + continue; + + if (!stricmp(finddata.name,"..")) + continue; + + if (!filecount) + *filelist = (tlfile_t**)TL_Malloc(sizeof(tlfile_t*)); + else + *filelist = (tlfile_t**)TL_Realloc(*filelist,(filecount+1)*sizeof(tlfile_t*)); + + (*filelist)[filecount] = (tlfile_t*)TL_Malloc(sizeof(tlfile_t)); + + len = (int)strlen(sourcepath) + (int)strlen(finddata.name) + 1; + filename = (char*)TL_Malloc(len); + + strcpy(filename,sourcepath); + strcat(filename,finddata.name); + + (*filelist)[filecount]->filename = filename; + (*filelist)[filecount]->time_write = finddata.time_write; + + filecount++; + } + while (!_findnext(h,&finddata)); + + _findclose(h); + + return (filecount); +} + +/***************************************************************************** + _RecurseFileTree + +*****************************************************************************/ +void _RecurseFileTree(char* dirpath, int depth) +{ + tlfile_t** filelist; + int numfiles; + int i; + int len; + + // recurse from source directory + numfiles = TL_GetFileList(dirpath,"\\",&filelist); + if (!numfiles) + { + // add directory name to search tree + if (!g_tl_dircount) + g_tl_dirlist = (char**)TL_Malloc(sizeof(char*)); + else + g_tl_dirlist = (char**)TL_Realloc(g_tl_dirlist,(g_tl_dircount+1)*sizeof(char*)); + + len = (int)strlen(dirpath); + g_tl_dirlist[g_tl_dircount] = (char*)TL_Malloc(len+1); + strcpy(g_tl_dirlist[g_tl_dircount],dirpath); + + g_tl_dircount++; + return; + } + + for (i=0; i<numfiles; i++) + { + // form new path name + _RecurseFileTree(filelist[i]->filename,depth+1); + } + + g_tl_dirlist = (char**)TL_Realloc(g_tl_dirlist,(g_tl_dircount+1)*sizeof(char*)); + + len = (int)strlen(dirpath); + g_tl_dirlist[g_tl_dircount] = (char*)TL_Malloc(len+1); + strcpy(g_tl_dirlist[g_tl_dircount],dirpath); + + g_tl_dircount++; +} + +/***************************************************************************** + TL_BuildFileTree + +*****************************************************************************/ +int TL_BuildFileTree(char* dirpath, char*** dirlist) +{ + g_tl_dircount = 0; + g_tl_dirlist = NULL; + + _RecurseFileTree(dirpath,0); + + *dirlist = g_tl_dirlist; + return (g_tl_dircount); +} + +/***************************************************************************** + TL_FindFiles2 + +*****************************************************************************/ +int TL_FindFiles2(char* filemask, bool recurse, tlfile_t*** filelist) +{ + char dirpath[TL_MAXPATH]; + char pattern[TL_MAXPATH]; + char** dirlist; + tlfile_t*** templists; + tlfile_t** list; + int* numfiles; + int numoutfiles; + int count; + int numdirs; + int i; + int j; + int k; + + // get path only + strcpy(dirpath,filemask); + TL_StripFilename(dirpath); + + // get pattern only + TL_StripPath(filemask,pattern); + + numoutfiles = 0; + + if (recurse) + { + // get the tree + numdirs = TL_BuildFileTree(dirpath,&dirlist); + if (numdirs) + { + templists = (tlfile_t***)TL_Malloc(numdirs * sizeof(tlfile_t**)); + numfiles = (int*)TL_Malloc(numdirs * sizeof(int)); + + // iterate each directory found + for (i=0; i<numdirs; i++) + numfiles[i] = TL_GetFileList(dirlist[i],pattern,&templists[i]); + + // count all the files + numoutfiles = 0; + for (i=0; i<numdirs; i++) + numoutfiles += numfiles[i]; + + // allocate single list + if (numoutfiles) + { + *filelist = (tlfile_t**)TL_Malloc(numoutfiles*sizeof(tlfile_t*)); + + k = 0; + for (i=0; i<numdirs; i++) + { + count = numfiles[i]; + list = templists[i]; + for (j=0; j<count; j++,k++) + { + (*filelist)[k] = list[j]; + } + } + } + + // free the directory lists + for (i=0; i<numdirs; i++) + { + TL_Free(dirlist[i]); + + if (numfiles[i]) + TL_Free(templists[i]); + } + + TL_Free(dirlist); + TL_Free(templists); + TL_Free(numfiles); + } + } + else + { + numoutfiles = TL_GetFileList(dirpath,pattern,filelist); + } + + return (numoutfiles); +} + +/***************************************************************************** + TL_FreeFileList + +*****************************************************************************/ +void TL_FreeFileList(int count, tlfile_t** filelist) +{ + int i; + + for (i=0; i<count; i++) + { + TL_Free(filelist[i]->filename); + TL_Free(filelist[i]); + } + + if (count) + TL_Free(filelist); +} + +/***************************************************************************** + TL_CPUCount + +*****************************************************************************/ +int TL_CPUCount(void) +{ + int time; + + time = clock(); + + return (time); +} + +/***************************************************************************** + TL_CPUTime + +*****************************************************************************/ +double TL_CPUTime(int start, int stop) +{ + double duration; + + duration = (double)(stop - start)/CLOCKS_PER_SEC; + + return (duration); +} + +/***************************************************************************** + TL_CreatePath + +*****************************************************************************/ +void TL_CreatePath(const char* inPath) +{ + char* ptr; + char dirPath[TL_MAXPATH]; + + // prime and skip to first seperator + strcpy(dirPath, inPath); + ptr = strchr(dirPath, '\\'); + while (ptr) + { + ptr = strchr(ptr+1, '\\'); + if (ptr) + { + *ptr = '\0'; + mkdir(dirPath); + *ptr = '\\'; + } + } +} + +/***************************************************************************** + TL_Warning + +*****************************************************************************/ +void TL_Warning(const char* format, ...) +{ + char msg[4096]; + va_list argptr; + + if (g_tl_quiet) + return; + + va_start(argptr, format); + vsprintf(msg, format, argptr); + va_end(argptr); + + printf("WARNING: %s", msg); +} + +/***************************************************************************** + TL_printf + +*****************************************************************************/ +void TL_printf(const char* format, ...) +{ + char msg[4096]; + va_list argptr; + + if (g_tl_quiet) + return; + + va_start(argptr, format); + vsprintf(msg, format, argptr); + va_end(argptr); + + printf(msg); +} + +//----------------------------------------------------------------------------- +// TL_IsWildcardMatch +// +// See if a string matches a wildcard specification that uses * or ? +//----------------------------------------------------------------------------- +bool TL_IsWildcardMatch( const char *wildcardString, const char *stringToCheck, bool caseSensitive ) +{ + char wcChar; + char strChar; + + // use the starMatchesZero variable to determine whether an asterisk + // matches zero or more characters ( TRUE ) or one or more characters + // ( FALSE ) + bool starMatchesZero = true; + + while ( ( strChar = *stringToCheck ) && ( wcChar = *wildcardString ) ) + { + // we only want to advance the pointers if we successfully assigned + // both of our char variables, so we'll do it here rather than in the + // loop condition itself + *stringToCheck++; + *wildcardString++; + + // if this isn't a case-sensitive match, make both chars uppercase + // ( thanks to David John Fielder ( Konan ) at http://innuendo.ev.ca + // for pointing out an error here in the original code ) + if ( !caseSensitive ) + { + wcChar = toupper( wcChar ); + strChar = toupper( strChar ); + } + + // check the wcChar against our wildcard list + switch ( wcChar ) + { + // an asterisk matches zero or more characters + case '*' : + // do a recursive call against the rest of the string, + // until we've either found a match or the string has + // ended + if ( starMatchesZero ) + *stringToCheck--; + + while ( *stringToCheck ) + { + if ( TL_IsWildcardMatch( wildcardString, stringToCheck++, caseSensitive ) ) + return true; + } + + break; + + // a question mark matches any single character + case '?' : + break; + + // if we fell through, we want an exact match + default : + if ( wcChar != strChar ) + return false; + break; + } + } + + // if we have any asterisks left at the end of the wildcard string, we can + // advance past them if starMatchesZero is TRUE ( so "blah*" will match "blah" ) + while ( ( *wildcardString ) && ( starMatchesZero ) ) + { + if ( *wildcardString == '*' ) + wildcardString++; + else + break; + } + + // if we got to the end but there's still stuff left in either of our strings, + // return false; otherwise, we have a match + if ( ( *stringToCheck ) || ( *wildcardString ) ) + return false; + else + return true; +} + +//----------------------------------------------------------------------------- +// TL_CopyString +// +//----------------------------------------------------------------------------- +char *TL_CopyString( const char* pString ) +{ + int size = strlen( pString ) + 1; + char *pNewString = (char *)TL_Malloc( size ); + memcpy( pNewString, pString, size ); + + return pNewString; +} + diff --git a/utils/xbox/toollib/toollib.h b/utils/xbox/toollib/toollib.h new file mode 100644 index 0000000..c62886a --- /dev/null +++ b/utils/xbox/toollib/toollib.h @@ -0,0 +1,114 @@ +#ifndef _TOOLLIB_H_ +#define _TOOLLIB_H_ + +#include <ctype.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/utime.h> +#include <string.h> +#include <io.h> +#include <direct.h> +#include <process.h> +#include <dos.h> +#include <stdarg.h> +#include <conio.h> +#include <math.h> +#include <limits.h> +#include <malloc.h> +#include <errno.h> +#include <time.h> +#include <assert.h> +#include <share.h> + +#define TL_MAXPATH 128 +#define TL_MEMID 0x44434241 + +typedef unsigned char byte_t; +typedef unsigned int rgba_t; +typedef unsigned int abgr_t; +typedef unsigned char u8; +typedef signed char s8; +typedef unsigned short u16; +typedef signed short s16; +typedef unsigned long u32; +typedef signed long s32; + +typedef struct +{ + int id; + int size; +} tlmem_t; + +typedef struct +{ + char* filename; + time_t time_write; +} tlfile_t; + +#include "scriplib.h" + +#define TL_max(a,b) ((a) > (b) ? (a) : (b)) +#define TL_min(a,b) ((a) < (b) ? (a) : (b)) + +extern void TL_Setup(char* appname, int argc, char** argv); +extern void TL_End(bool showtime); +extern void TL_Error(char* error, ...); +extern int TL_CheckParm(char* check); +extern void TL_strncpyz(char* dst, char* src, int n); +extern void TL_strncatz(char* dst, char* src, int dstsize); +extern void* TL_Malloc(int size); +extern void* TL_Realloc(void* ptr, int newsize); +extern void TL_Free(void* ptr); +extern int TL_SafeOpenRead(const char* filename); +extern int TL_SafeOpenWrite(const char* filename); +extern void TL_SafeRead(int handle, void* buffer, long count); +extern void TL_SafeWrite(int handle, void* buffer, long count); +extern void TL_SafeClose(int handle, int touch); +extern long TL_LoadFile(const char* filename, void** bufferptr); +extern void TL_SaveFile(char* filename, void* buffer, long count); +extern void TL_TouchFile(char* filename); +extern long TL_FileLength(int handle); +extern void TL_StripPath(char* path, char* dest); +extern void TL_StripExtension(char* path); +extern void TL_StripFilename(char* path); +extern void TL_GetExtension(char* path, char* dest); +extern void TL_DefaultPath(char* path, char* basepath); +extern void TL_AddSeperatorToPath(char* inpath, char* outpath); +extern void TL_DefaultExtension(char* path, char* extension, bool bForce = false); +extern void TL_TempFilename(char* path); +extern int TL_AlignFile(int handle, int align); +extern int TL_GetByteOrder(void); +extern void TL_SetByteOrder(int flag); +extern long TL_LongSwap(long l); +extern short TL_ShortSwap(short s); +extern short TL_LittleShort(short l); +extern short TL_BigShort(short l); +extern long TL_LittleLong(long l); +extern long TL_BigLong(long l); +extern float TL_BigFloat(float f); +extern bool TL_Exists(const char* filename); +extern u32 TL_FileTime(char* filename); +extern void TL_ReplaceDosExtension(char* path, char* extension); +extern void TL_ReplaceExtension(const char* inPath, const char* extension, char* outPath); +extern int TL_CPUCount(void); +extern double TL_CPUTime(int start, int stop); +extern int TL_BuildFileTree(char* dirpath, char*** dirlist); +extern int TL_GetFileList(char* dirpath, char* pattern, tlfile_t*** filelist); +extern int TL_FindFiles(char* filemask, char*** filenames); +extern int TL_FindFiles2(char* filemask, bool recurse, tlfile_t*** filelist); +extern void TL_FreeFileList(int count, tlfile_t** filelist); +extern void TL_CreatePath(const char* inPath); +extern void TL_printf(const char* format, ...); +extern void TL_Warning(const char* format, ...); +extern bool TL_IsWildcardMatch( const char *wildcardString, const char *stringToCheck, bool caseSensitive ); +extern char *TL_CopyString( const char* pString ); +extern bool TL_Check(void* ptr); + +extern int g_argc; +extern char** g_argv; + +#endif |