Index: src/common/mapindex.c
===================================================================
--- src/common/mapindex.c (revision 17368)
+++ src/common/mapindex.c (working copy)
@@ -6,6 +6,7 @@
#include "../common/malloc.h"
#include "../common/strlib.h"
#include "../common/db.h"
+#include "../db/dbi.h"
#include "mapindex.h"
#include <string.h>
@@ -17,10 +18,6 @@
char name[MAP_NAME_LENGTH]; //Stores map name
} indexes[MAX_MAPINDEX];
-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).
@@ -77,9 +74,6 @@
/// Returns 1 if successful, 0 oherwise
int mapindex_addmap(int index, const char* name) {
char map_name[MAP_NAME_LENGTH];
- if (index == -1){ //autogive index
- ARR_FIND(1,max_index,index,(indexes[index].name[0] == '\0'));
- }
if (index < 0 || index >= MAX_MAPINDEX) {
ShowError("(mapindex_add) Map index (%d) for \"%s\" out of range (max is %d)\n", index, name, MAX_MAPINDEX);
@@ -105,8 +99,6 @@
safestrncpy(indexes[index].name, map_name, MAP_NAME_LENGTH);
strdb_iput(mapindex_db, map_name, index);
- if (max_index <= index)
- max_index = index+1;
return index;
}
@@ -127,43 +119,25 @@
{
if (id > MAX_MAPINDEX || !mapindex_exists(id)) {
ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache.\n", id);
- return indexes[0].name; // dummy empty string so that the callee doesn't crash
+ return indexes[0].name; // dummy empty string so that the caller doesn't crash
}
return indexes[id].name;
}
-void mapindex_init(void) {
- FILE *fp;
- char line[1024];
- int last_index = -1;
- int index;
- char map_name[MAP_NAME_LENGTH];
+void mapindex_init( void ){
+ int i, count;
+ struct map_index *maps = DB->readMaps( &count );
- 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;
+ memset( &indexes, 0, sizeof( indexes ) );
- switch (sscanf(line, "%12s\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;
+ for( i = 0; i < count; i++ ){
+ mapindex_addmap( maps[i].id, maps[i].name );
}
- 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);
+ aFree( maps );
+
+ 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 );
}
}
Index: src/common/mapindex.h
===================================================================
--- src/common/mapindex.h (revision 17368)
+++ src/common/mapindex.h (working copy)
@@ -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.
Index: src/db/dbi.c
===================================================================
--- src/db/dbi.c (revision 0)
+++ src/db/dbi.c (working copy)
@@ -0,0 +1,36 @@
+#include "../common/showmsg.h"
+#include "../common/malloc.h"
+#include <stdlib.h>
+#include "dbi.h"
+
+struct map_index *read_mapindex( int *count );
+
+void db_do_init( void ){
+ //DB = &IDB_t;
+ DB->readMaps = read_mapindex;
+}
+
+struct map_index *read_mapindex( int *count ){
+ struct map_index *maps;
+ int i;
+
+ if( Sql_Query( mmysql_handle, "SELECT `id`, `name` FROM `maps`" ) == SQL_ERROR ){
+ ShowFatalError( "Mapindex could not be read." );
+ Sql_ShowDebug( mmysql_handle );
+ exit( EXIT_FAILURE );
+ }
+
+ maps = (struct map_index *)aMalloc( Sql_NumRows( mmysql_handle ) * sizeof( struct map_index ) );
+ i = 0;
+
+ while( Sql_NextRow( mmysql_handle ) == SQL_SUCCESS ){
+ Sql_GetData( mmysql_handle, 0, &maps[i].id, NULL );
+ Sql_GetData( mmysql_handle, 1, &maps[i].name, NULL );
+ i++;
+ }
+
+ Sql_FreeResult( mmysql_handle );
+ *count = i;
+
+ return maps;
+}
\ No newline at end of file
Index: src/db/dbi.h
===================================================================
--- src/db/dbi.h (revision 0)
+++ src/db/dbi.h (working copy)
@@ -0,0 +1,21 @@
+#ifndef DBNEW_H
+#define DBNEW_H
+
+#include "../map/map.h"
+
+struct map_index{
+ int id;
+ char name[MAP_NAME_LENGTH];
+};
+
+struct IDB{
+ int placeholder;
+
+ struct map_index *(*readMaps)( int *count );
+};
+
+struct IDB *DB;
+
+void db_do_init( void );
+
+#endif
Index: src/map/map.c
===================================================================
--- src/map/map.c (revision 17368)
+++ src/map/map.c (working copy)
@@ -15,6 +15,8 @@
#include "../common/cli.h"
#include "../common/ers.h"
+#include "../db/dbi.h"
+
#include "map.h"
#include "path.h"
#include "chrif.h"
@@ -3866,6 +3868,8 @@
if (log_config.sql_logs)
log_sql_init();
+ db_do_init();
+
mapindex_init();
if(enable_grf)
grfio_init(GRF_PATH_FILENAME);