Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, Valve LLC, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // wad2lib.c
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. //#include <sys/file.h>
  17. #include <stdarg.h>
  18. #ifdef NeXT
  19. #include <libc.h>
  20. #endif
  21. #include "cmdlib.h"
  22. #include "wadlib.h"
  23. #include "commonmacros.h"
  24. /*
  25. ============================================================================
  26. WAD READING
  27. ============================================================================
  28. */
  29. lumpinfo_t *lumpinfo; // location of each lump on disk
  30. int numlumps;
  31. wadinfo_t header;
  32. FILE *wadhandle;
  33. /*
  34. ====================
  35. W_OpenWad
  36. ====================
  37. */
  38. void W_OpenWad (char *filename)
  39. {
  40. lumpinfo_t *lump_p;
  41. unsigned i;
  42. int length;
  43. //
  44. // open the file and add to directory
  45. //
  46. wadhandle = SafeOpenRead (filename);
  47. SafeRead (wadhandle, &header, sizeof(header));
  48. if (!STRING_MATCHES_ID(header.identification,WAD_ID))
  49. Error ("Wad file %s doesn't have %s identifier\n",filename, WAD_IDNAME);
  50. header.numlumps = LittleLong(header.numlumps);
  51. header.infotableofs = LittleLong(header.infotableofs);
  52. numlumps = header.numlumps;
  53. length = numlumps*sizeof(lumpinfo_t);
  54. lumpinfo = malloc (length);
  55. lump_p = lumpinfo;
  56. fseek (wadhandle, header.infotableofs, SEEK_SET);
  57. SafeRead (wadhandle, lumpinfo, length);
  58. //
  59. // Fill in lumpinfo
  60. //
  61. for (i=0 ; i<numlumps ; i++,lump_p++)
  62. {
  63. lump_p->filepos = LittleLong(lump_p->filepos);
  64. lump_p->size = LittleLong(lump_p->size);
  65. }
  66. }
  67. void CleanupName (char *in, char *out)
  68. {
  69. int i;
  70. for (i=0 ; i<sizeof( ((lumpinfo_t *)0)->name ) ; i++ )
  71. {
  72. if (!in[i])
  73. break;
  74. out[i] = toupper(in[i]);
  75. }
  76. for ( ; i<sizeof( ((lumpinfo_t *)0)->name ); i++ )
  77. out[i] = 0;
  78. }
  79. /*
  80. ====================
  81. W_CheckNumForName
  82. Returns -1 if name not found
  83. ====================
  84. */
  85. int W_CheckNumForName (char *name)
  86. {
  87. char cleanname[TEXTURE_NAME_LENGTH];
  88. int v1,v2, v3, v4;
  89. int i;
  90. lumpinfo_t *lump_p;
  91. CleanupName (name, cleanname);
  92. // make the name into four integers for easy compares
  93. v1 = *(int *)cleanname;
  94. v2 = *(int *)&cleanname[4];
  95. v3 = *(int *)&cleanname[8];
  96. v4 = *(int *)&cleanname[12];
  97. // find it
  98. lump_p = lumpinfo;
  99. for (i=0 ; i<numlumps ; i++, lump_p++)
  100. {
  101. if ( *(int *)lump_p->name == v1
  102. && *(int *)&lump_p->name[4] == v2
  103. && *(int *)&lump_p->name[8] == v3
  104. && *(int *)&lump_p->name[12] == v4
  105. && !strcmp( lump_p->name, cleanname ) )
  106. return i;
  107. }
  108. return -1;
  109. }
  110. /*
  111. ====================
  112. W_GetNumForName
  113. Calls W_CheckNumForName, but bombs out if not found
  114. ====================
  115. */
  116. int W_GetNumForName (char *name)
  117. {
  118. int i;
  119. i = W_CheckNumForName (name);
  120. if (i != -1)
  121. return i;
  122. Error ("W_GetNumForName: %s not found!",name);
  123. return -1;
  124. }
  125. /*
  126. ====================
  127. W_LumpLength
  128. Returns the buffer size needed to load the given lump
  129. ====================
  130. */
  131. int W_LumpLength (int lump)
  132. {
  133. if (lump >= numlumps)
  134. Error ("W_LumpLength: %i >= numlumps",lump);
  135. return lumpinfo[lump].size;
  136. }
  137. /*
  138. ====================
  139. W_ReadLumpNum
  140. Loads the lump into the given buffer, which must be >= W_LumpLength()
  141. ====================
  142. */
  143. void W_ReadLumpNum (int lump, void *dest)
  144. {
  145. lumpinfo_t *l;
  146. if (lump >= numlumps)
  147. Error ("W_ReadLump: %i >= numlumps",lump);
  148. l = lumpinfo+lump;
  149. fseek (wadhandle, l->filepos, SEEK_SET);
  150. SafeRead (wadhandle, dest, l->size);
  151. }
  152. /*
  153. ====================
  154. W_LoadLumpNum
  155. ====================
  156. */
  157. void *W_LoadLumpNum (int lump)
  158. {
  159. void *buf;
  160. if ((unsigned)lump >= numlumps)
  161. Error ("W_CacheLumpNum: %i >= numlumps",lump);
  162. buf = malloc (W_LumpLength (lump));
  163. W_ReadLumpNum (lump, buf);
  164. return buf;
  165. }
  166. /*
  167. ====================
  168. W_LoadLumpName
  169. ====================
  170. */
  171. void *W_LoadLumpName (char *name)
  172. {
  173. return W_LoadLumpNum (W_GetNumForName(name));
  174. }
  175. /*
  176. ===============================================================================
  177. WAD CREATION
  178. ===============================================================================
  179. */
  180. FILE *outwad;
  181. lumpinfo_t outinfo[4096];
  182. int outlumps;
  183. short (*wadshort) (short l);
  184. int (*wadlong) (int l);
  185. /*
  186. ===============
  187. NewWad
  188. ===============
  189. */
  190. void NewWad (char *pathname, qboolean bigendien)
  191. {
  192. outwad = SafeOpenWrite (pathname);
  193. fseek (outwad, sizeof(wadinfo_t), SEEK_SET);
  194. memset (outinfo, 0, sizeof(outinfo));
  195. if (bigendien)
  196. {
  197. wadshort = BigShort;
  198. wadlong = BigLong;
  199. }
  200. else
  201. {
  202. wadshort = LittleShort;
  203. wadlong = LittleLong;
  204. }
  205. outlumps = 0;
  206. }
  207. /*
  208. ===============
  209. AddLump
  210. ===============
  211. */
  212. void AddLump (char *name, void *buffer, int length, int type, int compress)
  213. {
  214. lumpinfo_t *info;
  215. int ofs;
  216. info = &outinfo[outlumps];
  217. outlumps++;
  218. memset (info,0,sizeof(info));
  219. strcpy (info->name, name);
  220. Q_strupr (info->name);
  221. ofs = ftell(outwad);
  222. info->filepos = wadlong(ofs);
  223. info->size = info->disksize = wadlong(length);
  224. info->type = type;
  225. info->compression = compress;
  226. // FIXME: do compression
  227. SafeWrite (outwad, buffer, length);
  228. }
  229. /*
  230. ===============
  231. WriteWad
  232. ===============
  233. */
  234. void WriteWad (int wad3)
  235. {
  236. wadinfo_t header;
  237. int ofs;
  238. // write the lumpingo
  239. ofs = ftell(outwad);
  240. SafeWrite (outwad, outinfo, outlumps*sizeof(lumpinfo_t) );
  241. // write the header
  242. // a program will be able to tell the ednieness of a wad by the id
  243. ID_TO_STRING( WAD_ID, header.identification );
  244. header.numlumps = wadlong(outlumps);
  245. header.infotableofs = wadlong(ofs);
  246. fseek (outwad, 0, SEEK_SET);
  247. SafeWrite (outwad, &header, sizeof(header));
  248. fclose (outwad);
  249. }