viewing paste Unknown #6423 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
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);
 
Viewed 872 times, submitted by Guest.