APE.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include "typedefs.h"
  7. #include "misc.h"
  8. #include "debug4g.h"
  9. #include "getopt.h"
  10. #include "error.h"
  11. #define kMaxTiles 4096
  12. struct PICANM {
  13. unsigned frames : 5; // number of frames - 1
  14. unsigned update : 1; // this came from upper bit of frames
  15. unsigned type : 2; // 0 = none, 1 = Oscil, 2 = Frwd, 3 = Bkwd
  16. signed xcenter : 8;
  17. signed ycenter : 8;
  18. unsigned speed : 4; // (clock >> speed) determines rate
  19. unsigned view : 2;
  20. unsigned registered : 1;
  21. } picanm[kMaxTiles];
  22. long artversion;
  23. short tilesizx[kMaxTiles];
  24. short tilesizy[kMaxTiles];
  25. int checksum[kMaxTiles];
  26. /***********************************************************************
  27. * ShowBanner
  28. *
  29. * Display application banner.
  30. **********************************************************************/
  31. void ShowBanner( void )
  32. {
  33. printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
  34. printf(" APE - Art Patch Engine Version 1.1 Copyright (c) 1995 Q Studios Corporation\n");
  35. printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
  36. }
  37. /***********************************************************************
  38. * ShowUsage
  39. *
  40. * Display command-line parameter usage, then exit.
  41. **********************************************************************/
  42. void ShowUsage(void)
  43. {
  44. printf("Syntax: APE [commands]\n");
  45. printf("-a filename Apply delta file\n");
  46. printf("-b filename Build delta file from checksum\n");
  47. printf("-c filename Create checksum file\n");
  48. printf("-? This help\n");
  49. printf("\n");
  50. exit(0);
  51. }
  52. void CreateChecksumFile( char *argument )
  53. {
  54. char filename[_MAX_PATH];
  55. int i;
  56. int nTiles;
  57. int nFile, hFile;
  58. int nSize;
  59. int tileStart, tileEnd;
  60. long numtiles;
  61. printf("Processing tile ");
  62. nFile = 0;
  63. while (1)
  64. {
  65. sprintf(filename, "TILES%03i.ART", nFile);
  66. hFile = open(filename, O_BINARY | O_RDONLY);
  67. if ( hFile == -1 )
  68. break;
  69. FileRead(hFile, &artversion, sizeof(artversion));
  70. dassert(artversion == 1);
  71. FileRead(hFile, &numtiles, sizeof(numtiles));
  72. FileRead(hFile, &tileStart, sizeof(tileStart));
  73. FileRead(hFile, &tileEnd, sizeof(tileEnd));
  74. nTiles = tileEnd - tileStart + 1;
  75. FileRead(hFile, &tilesizx[tileStart], nTiles * sizeof(tilesizx[0]));
  76. FileRead(hFile, &tilesizy[tileStart], nTiles * sizeof(tilesizy[0]));
  77. FileRead(hFile, &picanm[tileStart], nTiles * sizeof(picanm[0]));
  78. for (i = tileStart; i <= tileEnd; i++)
  79. {
  80. nSize = tilesizx[i] * tilesizy[i];
  81. if ( nSize > 0 )
  82. {
  83. printf("\b\b\b\b%4d", i);
  84. BYTE *p = (BYTE *)malloc(nSize);
  85. dassert(p != NULL);
  86. if ( !FileRead(hFile, p, nSize) )
  87. ThrowError("Error FileReading tile file", ES_ERROR);
  88. checksum[i] = CRC32(p, nSize);
  89. free(p);
  90. }
  91. }
  92. close(hFile);
  93. nFile++;
  94. }
  95. printf("\n");
  96. strcpy(filename, argument);
  97. AddExtension(filename, "APC");
  98. if ( !FileSave(filename, checksum, sizeof(checksum)) )
  99. ThrowError("Error creating output file", ES_ERROR);
  100. printf("Checksum file %s created.\n", filename);
  101. }
  102. void BuildDeltaFile( char *argument )
  103. {
  104. char filename[_MAX_PATH];
  105. int i;
  106. int nTiles;
  107. int nFile, hTileFile, hDeltaFile;
  108. int nSize;
  109. int tileStart, tileEnd;
  110. long numtiles;
  111. int crc;
  112. BYTE *p = NULL;
  113. strcpy(filename, argument);
  114. ChangeExtension(filename, "APC");
  115. if ( !FileLoad(filename, checksum, sizeof(checksum)) )
  116. ThrowError("Error FileReading checksum file", ES_ERROR);
  117. ChangeExtension(filename, "APD");
  118. hDeltaFile = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IWUSR);
  119. if (hDeltaFile == -1)
  120. ThrowError("Error creating delta file", ES_ERROR);
  121. printf("Processing tile ");
  122. nFile = 0;
  123. while (1)
  124. {
  125. sprintf(filename, "TILES%03i.ART", nFile);
  126. hTileFile = open(filename, O_BINARY | O_RDONLY);
  127. if ( hTileFile == -1 )
  128. break;
  129. FileRead(hTileFile, &artversion, sizeof(artversion));
  130. dassert(artversion == 1);
  131. FileRead(hTileFile, &numtiles, sizeof(numtiles));
  132. FileRead(hTileFile, &tileStart, sizeof(tileStart));
  133. FileRead(hTileFile, &tileEnd, sizeof(tileEnd));
  134. nTiles = tileEnd - tileStart + 1;
  135. FileRead(hTileFile, &tilesizx[tileStart], nTiles * sizeof(tilesizx[0]));
  136. FileRead(hTileFile, &tilesizy[tileStart], nTiles * sizeof(tilesizy[0]));
  137. FileRead(hTileFile, &picanm[tileStart], nTiles * sizeof(picanm[0]));
  138. for (i = tileStart; i <= tileEnd; i++)
  139. {
  140. printf("\b\b\b\b%4d", i);
  141. crc = 0;
  142. nSize = tilesizx[i] * tilesizy[i];
  143. if ( nSize > 0 )
  144. {
  145. p = (BYTE *)malloc(nSize);
  146. dassert(p != NULL);
  147. FileRead(hTileFile, p, nSize);
  148. crc = CRC32(p, nSize);
  149. }
  150. if ( crc != checksum[i] )
  151. {
  152. picanm[i].update = 1;
  153. FileWrite(hDeltaFile, &picanm[i], sizeof(picanm[i]));
  154. FileWrite(hDeltaFile, &tilesizx[i], sizeof(tilesizx[i]));
  155. FileWrite(hDeltaFile, &tilesizy[i], sizeof(tilesizy[i]));
  156. FileWrite(hDeltaFile, p, nSize);
  157. }
  158. else
  159. {
  160. picanm[i].update = 0;
  161. FileWrite(hDeltaFile, &picanm[i], sizeof(picanm[i]));
  162. }
  163. if ( p != NULL )
  164. free(p);
  165. }
  166. close(hTileFile);
  167. nFile++;
  168. }
  169. printf("\nDelta file created\n");
  170. close(hDeltaFile);
  171. }
  172. void ApplyDeltaFile( char *argument )
  173. {
  174. char tileName[_MAX_PATH], bakName[_MAX_PATH], tempName[_MAX_PATH], deltaName[_MAX_PATH];
  175. long offset;
  176. int i;
  177. int nTiles = 0; // tiles per art file
  178. int nFile, hTileFile, hDeltaFile, hTemp;
  179. int nSize;
  180. int tileStart, tileEnd;
  181. long numtiles;
  182. BYTE *p = NULL;
  183. int nChanged = 0;
  184. FILE *fp; // use stream so we can write formatted text more easily
  185. fp = fopen("ape.rpt", "wt");
  186. if ( !fp )
  187. ThrowError("Error opening report file", ES_ERROR);
  188. strcpy(deltaName, argument);
  189. ChangeExtension(deltaName, "APD");
  190. hDeltaFile = open(deltaName, O_RDONLY | O_BINARY);
  191. if (hDeltaFile == -1)
  192. ThrowError("Error opening delta file", ES_ERROR);
  193. memset(picanm, 0, sizeof(picanm));
  194. memset(tilesizx, 0, sizeof(tilesizx));
  195. memset(tilesizy, 0, sizeof(tilesizy));
  196. printf("Processing tile ");
  197. nFile = 0;
  198. while ( !eof(hDeltaFile) )
  199. {
  200. hTemp = -1;
  201. tmpnam(tempName);
  202. sprintf(tileName, "TILES%03i.ART", nFile);
  203. hTileFile = open(tileName, O_BINARY | O_RDONLY);
  204. if ( hTileFile != -1 )
  205. {
  206. FileRead(hTileFile, &artversion, sizeof(artversion));
  207. FileRead(hTileFile, &numtiles, sizeof(numtiles));
  208. FileRead(hTileFile, &tileStart, sizeof(tileStart));
  209. FileRead(hTileFile, &tileEnd, sizeof(tileEnd));
  210. nTiles = tileEnd - tileStart + 1;
  211. FileRead(hTileFile, &tilesizx[tileStart], nTiles * sizeof(tilesizx[0]));
  212. FileRead(hTileFile, &tilesizy[tileStart], nTiles * sizeof(tilesizy[0]));
  213. FileRead(hTileFile, &picanm[tileStart], nTiles * sizeof(picanm[0]));
  214. // get current position
  215. offset = lseek(hTileFile, 0, SEEK_CUR);
  216. // create the output file
  217. hTemp = open(tempName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IWUSR);
  218. // position the output file to the start of the tile data
  219. lseek(hTemp, offset, SEEK_SET);
  220. }
  221. // merge all the changed tiles
  222. for (i = tileStart; i <= tileEnd; i++)
  223. {
  224. printf("\b\b\b\b%4d", i);
  225. FileRead(hDeltaFile, &picanm[i], sizeof(picanm[i]));
  226. nSize = tilesizx[i] * tilesizy[i];
  227. if ( picanm[i].update )
  228. {
  229. // we may need to create a new art file
  230. if (hTemp == -1)
  231. {
  232. // we've got to have opened at least one previous file...
  233. dassert(nTiles != 0);
  234. // create the output file
  235. hTemp = open(tempName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IWUSR);
  236. // position the output file to the start of the tile data
  237. lseek(hTemp, offset, SEEK_SET);
  238. }
  239. picanm[i].update = 0;
  240. // skip past the tile we're replacing
  241. if ( hTileFile != -1 )
  242. lseek(hTileFile, nSize, SEEK_CUR);
  243. FileRead(hDeltaFile, &tilesizx[i], sizeof(tilesizx[i]));
  244. FileRead(hDeltaFile, &tilesizy[i], sizeof(tilesizy[i]));
  245. nSize = tilesizx[i] * tilesizy[i];
  246. if ( nSize > 0 )
  247. {
  248. p = (BYTE *)malloc(nSize);
  249. dassert(p != NULL);
  250. FileRead(hDeltaFile, p, nSize);
  251. FileWrite(hTemp, p, nSize);
  252. fprintf(fp, "Tile %4d replaced or added\n", i);
  253. }
  254. else
  255. fprintf(fp, "Tile %4d removed\n", i);
  256. nChanged++;
  257. }
  258. else
  259. {
  260. if ( nSize > 0 )
  261. {
  262. // if we're not updating, it must exist
  263. dassert(hTileFile != -1);
  264. p = (BYTE *)malloc(nSize);
  265. dassert(p != NULL);
  266. FileRead(hTileFile, p, nSize);
  267. FileWrite(hTemp, p, nSize);
  268. }
  269. }
  270. if ( p != NULL )
  271. free(p);
  272. }
  273. if ( hTileFile != -1 )
  274. close(hTileFile);
  275. if ( hTemp != -1 )
  276. {
  277. // now write out the art file header
  278. lseek(hTemp, 0, SEEK_SET);
  279. FileWrite(hTemp, &artversion, sizeof(artversion));
  280. FileWrite(hTemp, &numtiles, sizeof(numtiles));
  281. FileWrite(hTemp, &tileStart, sizeof(tileStart));
  282. FileWrite(hTemp, &tileEnd, sizeof(tileEnd));
  283. FileWrite(hTemp, &tilesizx[tileStart], nTiles * sizeof(tilesizx[0]));
  284. FileWrite(hTemp, &tilesizy[tileStart], nTiles * sizeof(tilesizy[0]));
  285. FileWrite(hTemp, &picanm[tileStart], nTiles * sizeof(picanm[0]));
  286. close(hTemp);
  287. // make the current tile file into a backup
  288. strcpy(bakName, tileName);
  289. ChangeExtension(bakName, ".BAK");
  290. rename(tileName, bakName);
  291. rename(tempName, tileName);
  292. }
  293. nFile++;
  294. // increment these just in case the next tile file doesn't exist yet
  295. tileStart += nTiles;
  296. tileEnd += nTiles;
  297. }
  298. close(hDeltaFile);
  299. fclose(fp);
  300. printf("\nUpdate complete. %d tiles modified. See APE.RPT for details.\n", nChanged);
  301. }
  302. /***********************************************************************
  303. * Process command line arguments
  304. **********************************************************************/
  305. void ParseOptions( void )
  306. {
  307. enum {
  308. kSwitchHelp,
  309. kSwitchApply,
  310. kSwitchBuild,
  311. kSwitchCreate,
  312. };
  313. static SWITCH switches[] = {
  314. { "?", kSwitchHelp, FALSE },
  315. { "A", kSwitchApply, TRUE },
  316. { "B", kSwitchBuild, TRUE },
  317. { "C", kSwitchCreate, TRUE },
  318. { NULL, 0, FALSE },
  319. };
  320. char buffer[256];
  321. int r;
  322. while ( (r = GetOptions(switches)) != GO_EOF )
  323. {
  324. switch (r)
  325. {
  326. case GO_INVALID:
  327. sprintf(buffer, "Invalid argument: %s", OptArgument);
  328. ThrowError(buffer, ES_ERROR);
  329. break;
  330. case GO_MISSING:
  331. ThrowError("Missing argument", ES_ERROR);
  332. break;
  333. case kSwitchHelp:
  334. ShowUsage();
  335. break;
  336. case GO_FULL:
  337. ShowUsage();
  338. break;
  339. case kSwitchApply:
  340. ApplyDeltaFile(OptArgument);
  341. break;
  342. case kSwitchBuild:
  343. BuildDeltaFile(OptArgument);
  344. break;
  345. case kSwitchCreate:
  346. CreateChecksumFile(OptArgument);
  347. break;
  348. }
  349. }
  350. }
  351. /***********************************************************************
  352. * Main
  353. **********************************************************************/
  354. int main( int argc )
  355. {
  356. ShowBanner();
  357. if ( argc == 1 )
  358. ShowUsage();
  359. ParseOptions();
  360. return 0;
  361. }