src/gfxinit.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    51 
    51 
    52 
    52 
    53 static uint LoadGrfFile(const char* filename, uint load_index, int file_index)
    53 static uint LoadGrfFile(const char* filename, uint load_index, int file_index)
    54 {
    54 {
    55 	uint load_index_org = load_index;
    55 	uint load_index_org = load_index;
       
    56 	uint sprite_id = 0;
    56 
    57 
    57 	FioOpenFile(file_index, filename);
    58 	FioOpenFile(file_index, filename);
    58 
    59 
    59 	DEBUG(sprite, 2, "Reading grf-file '%s'", filename);
    60 	DEBUG(sprite, 2, "Reading grf-file '%s'", filename);
    60 
    61 
    61 	while (LoadNextSprite(load_index, file_index)) {
    62 	while (LoadNextSprite(load_index, file_index, sprite_id)) {
    62 		load_index++;
    63 		load_index++;
       
    64 		sprite_id++;
    63 		if (load_index >= MAX_SPRITES) {
    65 		if (load_index >= MAX_SPRITES) {
    64 			error("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
    66 			error("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
    65 		}
    67 		}
    66 	}
    68 	}
    67 	DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);
    69 	DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);
    71 
    73 
    72 
    74 
    73 static void LoadGrfIndexed(const char* filename, const SpriteID* index_tbl, int file_index)
    75 static void LoadGrfIndexed(const char* filename, const SpriteID* index_tbl, int file_index)
    74 {
    76 {
    75 	uint start;
    77 	uint start;
       
    78 	uint sprite_id = 0;
    76 
    79 
    77 	FioOpenFile(file_index, filename);
    80 	FioOpenFile(file_index, filename);
    78 
    81 
    79 	DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);
    82 	DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);
    80 
    83 
    81 	while ((start = *index_tbl++) != END) {
    84 	while ((start = *index_tbl++) != END) {
    82 		uint end = *index_tbl++;
    85 		uint end = *index_tbl++;
    83 
    86 
    84 		if (start == SKIP) { // skip sprites (amount in second var)
    87 		if (start == SKIP) { // skip sprites (amount in second var)
    85 			SkipSprites(end);
    88 			SkipSprites(end);
       
    89 			sprite_id += end;
    86 		} else { // load sprites and use indexes from start to end
    90 		} else { // load sprites and use indexes from start to end
    87 			do {
    91 			do {
    88 			#ifdef NDEBUG
    92 			#ifdef NDEBUG
    89 				LoadNextSprite(start, file_index);
    93 				LoadNextSprite(start, file_index, sprite_id);
    90 			#else
    94 			#else
    91 				bool b = LoadNextSprite(start, file_index);
    95 				bool b = LoadNextSprite(start, file_index, sprite_id);
    92 				assert(b);
    96 				assert(b);
    93 			#endif
    97 			#endif
       
    98 				sprite_id++;
    94 			} while (++start <= end);
    99 			} while (++start <= end);
    95 		}
   100 		}
    96 	}
   101 	}
    97 }
   102 }
    98 
   103 
   107 
   112 
   108 /* Calculate and check the MD5 hash of the supplied filename.
   113 /* Calculate and check the MD5 hash of the supplied filename.
   109  * returns true if the checksum is correct */
   114  * returns true if the checksum is correct */
   110 static bool FileMD5(const MD5File file, bool warn)
   115 static bool FileMD5(const MD5File file, bool warn)
   111 {
   116 {
   112 	FILE *f;
   117 	FILE *f = FioFOpenFile(file.filename);
   113 	char buf[MAX_PATH];
       
   114 
       
   115 	/* open file */
       
   116 	snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, file.filename);
       
   117 	f = fopen(buf, "rb");
       
   118 
       
   119 #if !defined(WIN32)
       
   120 	if (f == NULL) {
       
   121 		strtolower(buf + strlen(_paths.data_dir) - 1);
       
   122 		f = fopen(buf, "rb");
       
   123 	}
       
   124 #endif
       
   125 
       
   126 #if defined SECOND_DATA_DIR
       
   127 	/* If we failed to find the file in the first data directory, we will try the other one */
       
   128 
       
   129 	if (f == NULL) {
       
   130 		snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, file.filename);
       
   131 		f = fopen(buf, "rb");
       
   132 
       
   133 		if (f == NULL) {
       
   134 			strtolower(buf + strlen(_paths.second_data_dir) - 1);
       
   135 			f = fopen(buf, "rb");
       
   136 		}
       
   137 	}
       
   138 #endif
       
   139 
   118 
   140 	if (f != NULL) {
   119 	if (f != NULL) {
   141 		md5_state_t filemd5state;
   120 		md5_state_t filemd5state;
   142 		md5_byte_t buffer[1024];
   121 		md5_byte_t buffer[1024];
   143 		md5_byte_t digest[16];
   122 		md5_byte_t digest[16];
   145 
   124 
   146 		md5_init(&filemd5state);
   125 		md5_init(&filemd5state);
   147 		while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0)
   126 		while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0)
   148 			md5_append(&filemd5state, buffer, len);
   127 			md5_append(&filemd5state, buffer, len);
   149 
   128 
   150 		if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", buf);
   129 		if (ferror(f) && warn) ShowInfoF("Error Reading from %s \n", file.filename);
   151 		fclose(f);
   130 		fclose(f);
   152 
   131 
   153 		md5_finish(&filemd5state, digest);
   132 		md5_finish(&filemd5state, digest);
   154 		return CheckMD5Digest(file, digest, warn);
   133 		return CheckMD5Digest(file, digest, warn);
   155 	} else { // file not found
   134 	} else { // file not found