Переглянути джерело

Merge pull request #206 from rathena/feature/import-maps

Import support for map data
Cydh Ramdh 10 роки тому
батько
коміт
44d599875a

BIN
db/import-tmpl/map_cache.dat


+ 18 - 0
db/import-tmpl/map_index.txt

@@ -0,0 +1,18 @@
+//======================================================================================
+// Map Index
+//======================================================================================
+//Contains the list of maps with their respective IDs for inter-server use.
+//IDs must never change, therefore any new maps need to be added at the end,
+//and old ones must not be removed, but may be replaced.
+//Format:
+//mapname<tab>index <- specifies index for this map
+//mapname <- map will use index of previous map +1
+//Note that map index 0 is special and reserved for "error" status. 
+
+//======================================================================================
+//Place your custom maps with a starting ID here.
+//======================================================================================
+//Example:
+//
+//mymap	1250
+//mymap-2

+ 0 - 8
db/map_index.txt

@@ -1015,11 +1015,3 @@ paramk
 //tank_test
 //tank_test2
 //test
-
-//======================================================================================
-//Place your custom maps with a starting ID here.
-//======================================================================================
-//Example:
-//
-//mymap	1250
-//mymap-2

+ 37 - 19
src/common/mapindex.c

@@ -1,10 +1,12 @@
 // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
 // For more information, see LICENCE in the main folder
 
+#include "../config/core.h"
+#include "../common/core.h"
+#include "../common/mapindex.h"
 #include "../common/mmo.h"
 #include "../common/showmsg.h"
 #include "../common/strlib.h"
-#include "mapindex.h"
 
 #include <stdlib.h>
 
@@ -15,8 +17,6 @@ struct _indexes {
 
 int max_index = 0;
 
-char mapindex_cfgfile[80] = "db/map_index.txt";
-
 #define mapindex_exists(id) (indexes[id].name[0] != '\0')
 
 /// Retrieves the map name from 'string' (removing .gat extension if present).
@@ -134,29 +134,47 @@ void mapindex_init(void) {
 	int last_index = -1;
 	int index;
 	char map_name[MAP_NAME_LENGTH];
+	char path[255];
+	const char* mapindex_cfgfile[] = {
+		"map_index.txt",
+		DBIMPORT"/map_index.txt"
+	};
+	int i;
 
-	if( ( fp = fopen(mapindex_cfgfile,"r") ) == NULL ){
-		ShowFatalError("Unable to read mapindex config file %s!\n", mapindex_cfgfile);
-		exit(EXIT_FAILURE); //Server can't really run without this file.
-	}
 	memset (&indexes, 0, sizeof (indexes));
 	mapindex_db = strdb_alloc(DB_OPT_DUP_KEY, MAP_NAME_LENGTH);
-	while(fgets(line, sizeof(line), fp)) {
-		if(line[0] == '/' && line[1] == '/')
-			continue;
-
-		switch (sscanf(line, "%11s\t%d", map_name, &index)) {
-			case 1: //Map with no ID given, auto-assign
-				index = last_index+1;
-			case 2: //Map with ID given
-				mapindex_addmap(index,map_name);
+
+	for( i = 0; i < ARRAYLENGTH(mapindex_cfgfile); i++ ){
+		sprintf( path, "%s/%s", db_path, mapindex_cfgfile[i] );
+
+		if( ( fp = fopen( path, "r" ) ) == NULL ){
+			// It is only fatal if it is the main file
+			if( i == 0 ){
+				ShowFatalError("Unable to read mapindex config file %s!\n", path );
+				exit(EXIT_FAILURE); //Server can't really run without this file.
+			}else{
+				ShowWarning("Unable to read mapindex config file %s!\n", path );
 				break;
-			default:
+			}
+		}
+
+		while(fgets(line, sizeof(line), fp)) {
+			if(line[0] == '/' && line[1] == '/')
 				continue;
+
+			switch (sscanf(line, "%11s\t%d", map_name, &index)) {
+				case 1: //Map with no ID given, auto-assign
+					index = last_index+1;
+				case 2: //Map with ID given
+					mapindex_addmap(index,map_name);
+					break;
+				default:
+					continue;
+			}
+			last_index = index;
 		}
-		last_index = index;
+		fclose(fp);
 	}
-	fclose(fp);
 
 	if( !strdb_iget(mapindex_db, MAP_DEFAULT) ) {
 		ShowError("mapindex_init: MAP_DEFAULT '%s' not found in cache! Update MAP_DEFAULT in mapindex.h!\n",MAP_DEFAULT);

+ 0 - 3
src/common/mapindex.h

@@ -4,9 +4,6 @@
 #ifndef _MAPINDEX_H_
 #define _MAPINDEX_H_
 
-//File in charge of assigning a numberic ID to each map in existance for space saving when passing map info between servers.
-extern char mapindex_cfgfile[80];
-
 #define MAX_MAPINDEX 2000
 
 //Some definitions for the mayor city maps.

+ 55 - 24
src/map/map.c

@@ -3345,41 +3345,71 @@ int map_readallmaps (void)
 	int i;
 	FILE* fp=NULL;
 	int maps_removed = 0;
-	char *map_cache_buffer = NULL; // Has the uncompressed gat data of all maps, so just one allocation has to be made
+	// Has the uncompressed gat data of all maps, so just one allocation has to be made
+	char *map_cache_buffer[2] = {
+		NULL,
+		NULL
+	};
 	char map_cache_decode_buffer[MAX_MAP_SIZE];
 
 	if( enable_grf )
 		ShowStatus("Loading maps (using GRF files)...\n");
 	else {
-		char mapcachefilepath[254];
-		sprintf(mapcachefilepath,"%s/%s%s",db_path,DBPATH,"map_cache.dat");
-		ShowStatus("Loading maps (using %s as map cache)...\n", mapcachefilepath);
-		if( (fp = fopen(mapcachefilepath, "rb")) == NULL ) {
-			ShowFatalError("Unable to open map cache file "CL_WHITE"%s"CL_RESET"\n", mapcachefilepath);
-			exit(EXIT_FAILURE); //No use launching server if maps can't be read.
-		}
+		char* mapcachefilepath[] = {
+			"db/"DBPATH"map_cache.dat",
+			"db/import/map_cache.dat"
+		};
+
+		for( i = 0; i < 2; i++ ){
+			ShowStatus( "Loading maps (using %s as map cache)...\n", mapcachefilepath[i] );
+
+			if( ( fp = fopen(mapcachefilepath[i], "rb") ) == NULL ){
+				if( i == 0 ){
+					ShowFatalError( "Unable to open map cache file "CL_WHITE"%s"CL_RESET"\n", mapcachefilepath[i] );
+					exit(EXIT_FAILURE); //No use launching server if maps can't be read.
+				}else{
+					ShowWarning( "Unable to open map cache file "CL_WHITE"%s"CL_RESET"\n", mapcachefilepath[i] );
+					break;
+				}
+			}
+
+			// Init mapcache data. [Shinryo]
+			map_cache_buffer[i] = map_init_mapcache(fp);
+
+			if( !map_cache_buffer[i] ) {
+				ShowFatalError( "Failed to initialize mapcache data (%s)..\n", mapcachefilepath );
+				exit(EXIT_FAILURE);
+			}
 
-		// Init mapcache data. [Shinryo]
-		map_cache_buffer = map_init_mapcache(fp);
-		if(!map_cache_buffer) {
-			ShowFatalError("Failed to initialize mapcache data (%s)..\n", mapcachefilepath);
-			exit(EXIT_FAILURE);
+			fclose(fp);
 		}
 	}
 
 	for(i = 0; i < map_num; i++) {
 		size_t size;
+		bool success = false;
 
-		// show progress
-		if(enable_grf)
+		if( enable_grf ){
+			// show progress
 			ShowStatus("Loading maps [%i/%i]: %s"CL_CLL"\r", i, map_num, map[i].name);
 
-		// try to load the map
-		if( !
-			(enable_grf?
-				 map_readgat(&map[i])
-				:map_readfromcache(&map[i], map_cache_buffer, map_cache_decode_buffer))
-			) {
+			// try to load the map
+			success = map_readgat(&map[i]) != 0;
+		}else{
+			// try to load the map
+			// Read from import first, in case of override
+			if( map_cache_buffer[1] != NULL ){
+				success = map_readfromcache( &map[i], map_cache_buffer[1], map_cache_decode_buffer ) != 0;
+			}
+
+			// Nothing was found in import - try to find it in the main file
+			if( !success ){
+				success = map_readfromcache( &map[i], map_cache_buffer[0], map_cache_decode_buffer ) != 0;
+			}
+		}
+
+		// The map was not found - remove it
+		if( !success ){
 			map_delmapid(i);
 			maps_removed++;
 			i--;
@@ -3419,10 +3449,11 @@ int map_readallmaps (void)
 	map_flags_init();
 
 	if( !enable_grf ) {
-		fclose(fp);
-
 		// The cache isn't needed anymore, so free it. [Shinryo]
-		aFree(map_cache_buffer);
+		if( map_cache_buffer[1] != NULL ){
+			aFree(map_cache_buffer[1]);
+		}
+		aFree(map_cache_buffer[0]);
 	}
 
 	// finished map loading

+ 2 - 0
vcproj-10/map-server.vcxproj

@@ -325,6 +325,8 @@
     <Copy SourceFiles="..\db\import-tmpl\job_param_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\job_param_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\level_penalty.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\level_penalty.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\magicmushroom_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\magicmushroom_db.txt')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_cache.dat" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_cache.dat')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_index.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_index.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_skill_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_skill_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mob_avail.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mob_avail.txt')" />

+ 2 - 0
vcproj-12/map-server.vcxproj

@@ -329,6 +329,8 @@
     <Copy SourceFiles="..\db\import-tmpl\job_param_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\job_param_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\level_penalty.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\level_penalty.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\magicmushroom_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\magicmushroom_db.txt')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_cache.dat" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_cache.dat')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_index.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_index.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_skill_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_skill_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mob_avail.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mob_avail.txt')" />

+ 2 - 0
vcproj-13/map-server.vcxproj

@@ -329,6 +329,8 @@
     <Copy SourceFiles="..\db\import-tmpl\job_param_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\job_param_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\level_penalty.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\level_penalty.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\magicmushroom_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\magicmushroom_db.txt')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_cache.dat" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_cache.dat')" />
+    <Copy SourceFiles="..\db\import-tmpl\map_index.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\map_index.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mercenary_skill_db.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mercenary_skill_db.txt')" />
     <Copy SourceFiles="..\db\import-tmpl\mob_avail.txt" DestinationFolder="..\db\import\" ContinueOnError="true" Condition="!Exists('..\db\import\mob_avail.txt')" />