# HG changeset patch # User Medo # Date 1341941637 -7200 # Node ID e704706008d4a41b159a03381730b613c89d8089 # Parent a446eafcddebc1aeddaa2db7e5de25b6eb47e572 frontlib: Renamed cfg to scheme, Un-refcounted some types, small API adjustments diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/frontlib.c --- a/project_files/frontlib/frontlib.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/frontlib.c Tue Jul 10 19:33:57 2012 +0200 @@ -21,7 +21,7 @@ #include "util/logging.h" #include -int flib_init(int flags) { +int flib_init() { flib_log_d("Initializing frontlib"); if(SDLNet_Init()==-1) { flib_log_e("Error in SDLNet_Init: %s", SDLNet_GetError()); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/frontlib.h --- a/project_files/frontlib/frontlib.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/frontlib.h Tue Jul 10 19:33:57 2012 +0200 @@ -29,20 +29,13 @@ #include "ipc/gameconn.h" #include "ipc/mapconn.h" #include "net/netconn.h" - -#define FRONTLIB_SDL_ALREADY_INITIALIZED 1 +#include "util/logging.h" /** * Call this function before anything else in this library. - * - * If the calling program uses SDL, it needs to call SDL_Init before initializing - * this library and then pass FRONTLIB_SDL_ALREADY_INITIALIZED as flag to this function. - * - * Otherwise, pass 0 to let this library handle SDL_Init an SDL_Quit itself. - * * Returns 0 on success, -1 on error. */ -int flib_init(int flags); +int flib_init(); /** * Free resources associated with the library. Call this function once diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/ipc/gameconn.c --- a/project_files/frontlib/ipc/gameconn.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/ipc/gameconn.c Tue Jul 10 19:33:57 2012 +0200 @@ -54,7 +54,7 @@ void (*onChatCb)(void* context, const char *msg, bool teamchat); void *onChatCtx; - void (*onGameRecordedCb)(void *context, const uint8_t *record, int size, bool isSavegame); + void (*onGameRecordedCb)(void *context, const uint8_t *record, size_t size, bool isSavegame); void *onGameRecordedCtx; void (*onEngineMessageCb)(void *context, const uint8_t *em, size_t size); @@ -64,22 +64,17 @@ bool destroyRequested; }; -static void defaultCallback_onConnect(void* context) {} -static void defaultCallback_onDisconnect(void* context, int reason) {} static void defaultCallback_onErrorMessage(void* context, const char *msg) { flib_log_w("Error from engine (no callback set): %s", msg); } -static void defaultCallback_onChat(void* context, const char *msg, bool teamchat) {} -static void defaultCallback_onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {} -static void defaultCallback_onEngineMessage(void *context, const uint8_t *em, size_t size) {} static void clearCallbacks(flib_gameconn *conn) { - conn->onConnectCb = &defaultCallback_onConnect; - conn->onDisconnectCb = &defaultCallback_onDisconnect; - conn->onErrorMessageCb = &defaultCallback_onErrorMessage; - conn->onChatCb = &defaultCallback_onChat; - conn->onGameRecordedCb = &defaultCallback_onGameRecorded; - conn->onEngineMessageCb = &defaultCallback_onEngineMessage; + flib_gameconn_onConnect(conn, NULL, NULL); + flib_gameconn_onDisconnect(conn, NULL, NULL); + flib_gameconn_onErrorMessage(conn, NULL, NULL); + flib_gameconn_onChat(conn, NULL, NULL); + flib_gameconn_onGameRecorded(conn, NULL, NULL); + flib_gameconn_onEngineMessage(conn, NULL, NULL); } static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) { @@ -312,7 +307,7 @@ GENERATE_CB_SETTER_AND_DEFAULT(onDisconnect, (void* context, int reason)); GENERATE_CB_SETTER(onErrorMessage, (void* context, const char *msg), defaultCallback_onErrorMessage); GENERATE_CB_SETTER_AND_DEFAULT(onChat, (void* context, const char *msg, bool teamchat)); -GENERATE_CB_SETTER_AND_DEFAULT(onGameRecorded, (void *context, const uint8_t *record, int size, bool isSavegame)); +GENERATE_CB_SETTER_AND_DEFAULT(onGameRecorded, (void *context, const uint8_t *record, size_t size, bool isSavegame)); GENERATE_CB_SETTER_AND_DEFAULT(onEngineMessage, (void *context, const uint8_t *em, size_t size)); #undef GENERATE_CB_SETTER_AND_DEFAULT diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/ipc/gameconn.h --- a/project_files/frontlib/ipc/gameconn.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/ipc/gameconn.h Tue Jul 10 19:33:57 2012 +0200 @@ -81,7 +81,7 @@ * Called when the game ends * handleGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) */ -void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, int size, bool isSavegame), void* context); +void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, size_t size, bool isSavegame), void* context); /** * Called when the game ends diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/ipc/ipcprotocol.c --- a/project_files/frontlib/ipc/ipcprotocol.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/ipc/ipcprotocol.c Tue Jul 10 19:33:57 2012 +0200 @@ -131,7 +131,7 @@ return result; } -static uint32_t buildModFlags(const flib_cfg *scheme) { +static uint32_t buildModFlags(const flib_scheme *scheme) { uint32_t result = 0; for(int i=0; imeta->modCount; i++) { if(scheme->mods[i]) { @@ -142,11 +142,11 @@ return result; } -int flib_ipc_append_gamescheme(flib_vector *vec, const flib_cfg *scheme) { +int flib_ipc_append_gamescheme(flib_vector *vec, const flib_scheme *scheme) { int result = -1; flib_vector *tempvector = flib_vector_create(); if(!log_badargs_if2(vec==NULL, scheme==NULL) && tempvector) { - const flib_cfg_meta *meta = scheme->meta; + const flib_metascheme *meta = scheme->meta; bool error = false; error |= flib_ipc_append_message(tempvector, "e$gmflags %"PRIu32, buildModFlags(scheme)); for(int i=0; isettingCount; i++) { @@ -263,9 +263,9 @@ } if(setup->gamescheme) { error |= flib_ipc_append_gamescheme(tempvector, setup->gamescheme); - sharedAmmo = flib_cfg_get_mod(setup->gamescheme, "sharedammo"); + sharedAmmo = flib_scheme_get_mod(setup->gamescheme, "sharedammo"); // Shared ammo has priority over per-hog ammo - perHogAmmo = !sharedAmmo && flib_cfg_get_mod(setup->gamescheme, "perhogammo"); + perHogAmmo = !sharedAmmo && flib_scheme_get_mod(setup->gamescheme, "perhogammo"); } if(setup->teamlist->teams && setup->teamlist->teamCount>0) { int *clanColors = flib_calloc(setup->teamlist->teamCount, sizeof(int)); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/ipc/ipcprotocol.h --- a/project_files/frontlib/ipc/ipcprotocol.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/ipc/ipcprotocol.h Tue Jul 10 19:33:57 2012 +0200 @@ -23,7 +23,7 @@ #include "../util/buffer.h" #include "../model/map.h" #include "../model/team.h" -#include "../model/cfg.h" +#include "../model/scheme.h" #include "../model/gamesetup.h" #include @@ -71,7 +71,7 @@ * Returns nonzero if something goes wrong. In that case the buffer * contents are unaffected. */ -int flib_ipc_append_gamescheme(flib_vector *vec, const flib_cfg *cfg); +int flib_ipc_append_gamescheme(flib_vector *vec, const flib_scheme *cfg); int flib_ipc_append_addteam(flib_vector *vec, const flib_team *team, bool perHogAmmo, bool noAmmoStore); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/cfg.c --- a/project_files/frontlib/model/cfg.c Thu Jul 05 22:22:48 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,237 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (C) 2012 Simeon Maxein - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "cfg.h" - -#include "../util/inihelper.h" -#include "../util/logging.h" -#include "../util/util.h" -#include "../util/refcounter.h" - -#include -#include -#include -#include - -static void flib_cfg_meta_destroy(flib_cfg_meta *cfg) { - if(cfg) { - if(cfg->settings) { - for(int i=0; isettingCount; i++) { - free(cfg->settings[i].name); - free(cfg->settings[i].engineCommand); - } - free(cfg->settings); - } - if(cfg->mods) { - for(int i=0; imodCount; i++) { - free(cfg->mods[i].name); - } - free(cfg->mods); - } - free(cfg); - } -} - -static void flib_cfg_destroy(flib_cfg* cfg) { - if(cfg) { - flib_cfg_meta_release(cfg->meta); - free(cfg->mods); - free(cfg->settings); - free(cfg->name); - free(cfg); - } -} - -static flib_cfg_meta *flib_cfg_meta_from_ini_handleError(flib_cfg_meta *result, flib_ini *ini) { - flib_cfg_meta_destroy(result); - flib_ini_destroy(ini); - return NULL; -} - -static int readMetaSettingSections(flib_ini *ini, flib_cfg_meta *result, int limit) { - while(result->settingCountsettingCount) <= 0) { - return -1; - } - if(!flib_ini_enter_section(ini, sectionName)) { - flib_cfg_setting_meta *metasetting = &result->settings[result->settingCount]; - result->settingCount++; - - bool error = false; - error |= flib_ini_get_str(ini, &metasetting->name, "name"); - error |= flib_ini_get_str_opt(ini, &metasetting->engineCommand, "command", NULL); - error |= flib_ini_get_bool(ini, &metasetting->times1000, "times1000"); - error |= flib_ini_get_bool(ini, &metasetting->maxMeansInfinity, "maxmeansinfinity"); - error |= flib_ini_get_int(ini, &metasetting->min, "min"); - error |= flib_ini_get_int(ini, &metasetting->max, "max"); - error |= flib_ini_get_int(ini, &metasetting->def, "default"); - if(error) { - flib_log_e("Missing or malformed ini parameter in metaconfig, section %s", sectionName); - return -1; - } - } else { - return 0; - } - } - return 0; -} - -static int readMetaModSections(flib_ini *ini, flib_cfg_meta *result, int limit) { - while(result->modCountmodCount) <= 0) { - return -1; - } - if(!flib_ini_enter_section(ini, sectionName)) { - flib_cfg_mod_meta *metamod = &result->mods[result->modCount]; - result->modCount++; - - bool error = false; - error |= flib_ini_get_str(ini, &metamod->name, "name"); - error |= flib_ini_get_int(ini, &metamod->bitmaskIndex, "bitmaskIndex"); - if(error) { - flib_log_e("Missing or malformed ini parameter in metaconfig, section %s", sectionName); - return -1; - } - } else { - return 0; - } - } - return 0; -} - -flib_cfg_meta *flib_cfg_meta_from_ini(const char *filename) { - if(log_badargs_if(filename==NULL)) { - return NULL; - } - flib_cfg_meta *result = flib_cfg_meta_retain(flib_calloc(1, sizeof(flib_cfg_meta))); - flib_ini *ini = flib_ini_load(filename); - - if(!result || !ini) { - return flib_cfg_meta_from_ini_handleError(result, ini); - } - - // We're overallocating here for simplicity - int sectionCount = flib_ini_get_sectioncount(ini); - result->settingCount = 0; - result->modCount = 0; - result->settings = flib_calloc(sectionCount, sizeof(flib_cfg_setting_meta)); - result->mods = flib_calloc(sectionCount, sizeof(flib_cfg_mod_meta)); - - if(!result->settings || !result->mods) { - return flib_cfg_meta_from_ini_handleError(result, ini); - } - - if(readMetaSettingSections(ini, result, sectionCount) || readMetaModSections(ini, result, sectionCount)) { - return flib_cfg_meta_from_ini_handleError(result, ini); - } - - if(result->settingCount+result->modCount != sectionCount) { - flib_log_e("Unknown or non-contiguous sections headers in metaconfig."); - return flib_cfg_meta_from_ini_handleError(result, ini); - } - - flib_ini_destroy(ini); - return result; -} - -flib_cfg_meta *flib_cfg_meta_retain(flib_cfg_meta *metainfo) { - if(metainfo) { - flib_retain(&metainfo->_referenceCount, "flib_cfg_meta"); - } - return metainfo; -} - -void flib_cfg_meta_release(flib_cfg_meta *cfg) { - if(cfg && flib_release(&cfg->_referenceCount, "flib_cfg_meta")) { - flib_cfg_meta_destroy(cfg); - } -} - -flib_cfg *flib_cfg_create(flib_cfg_meta *meta, const char *schemeName) { - flib_cfg *result = flib_cfg_retain(flib_calloc(1, sizeof(flib_cfg))); - if(log_badargs_if2(meta==NULL, schemeName==NULL) || result==NULL) { - return NULL; - } - - result->meta = flib_cfg_meta_retain(meta); - result->name = flib_strdupnull(schemeName); - result->mods = flib_calloc(meta->modCount, sizeof(*result->mods)); - result->settings = flib_calloc(meta->settingCount, sizeof(*result->settings)); - - if(!result->mods || !result->settings || !result->name) { - flib_cfg_destroy(result); - return NULL; - } - - for(int i=0; isettingCount; i++) { - result->settings[i] = meta->settings[i].def; - } - return result; -} - -flib_cfg *flib_cfg_copy(const flib_cfg *cfg) { - flib_cfg *result = NULL; - if(cfg) { - result = flib_cfg_create(cfg->meta, cfg->name); - if(result) { - memcpy(result->mods, cfg->mods, cfg->meta->modCount * sizeof(*cfg->mods)); - memcpy(result->settings, cfg->settings, cfg->meta->settingCount * sizeof(*cfg->settings)); - } - } - return result; -} - -flib_cfg *flib_cfg_retain(flib_cfg *cfg) { - if(cfg) { - flib_retain(&cfg->_referenceCount, "flib_cfg"); - } - return cfg; -} - -void flib_cfg_release(flib_cfg *cfg) { - if(cfg && flib_release(&cfg->_referenceCount, "flib_cfg")) { - flib_cfg_destroy(cfg); - } -} - -bool flib_cfg_get_mod(flib_cfg *cfg, const char *name) { - if(!log_badargs_if2(cfg==NULL, name==NULL)) { - for(int i=0; imeta->modCount; i++) { - if(!strcmp(cfg->meta->mods[i].name, name)) { - return cfg->mods[i]; - } - } - flib_log_e("Unable to find game mod %s", name); - } - return false; -} - -int flib_cfg_get_setting(flib_cfg *cfg, const char *name, int def) { - if(!log_badargs_if2(cfg==NULL, name==NULL)) { - for(int i=0; imeta->settingCount; i++) { - if(!strcmp(cfg->meta->settings[i].name, name)) { - return cfg->settings[i]; - } - } - flib_log_e("Unable to find game setting %s", name); - } - return def; -} diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/cfg.h --- a/project_files/frontlib/model/cfg.h Thu Jul 05 22:22:48 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,118 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (C) 2012 Simeon Maxein - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -/** - * Data structures for game scheme information. - */ - -#ifndef CFG_H_ -#define CFG_H_ - -#include - -// TODO: cfg/config -> scheme - -typedef struct { - char *name; - char *engineCommand; - bool maxMeansInfinity; - bool times1000; - int min; - int max; - int def; -} flib_cfg_setting_meta; - -typedef struct { - char *name; - int bitmaskIndex; -} flib_cfg_mod_meta; - -/** - * The order of the meta information in the arrays is the same as the order - * of the mod/setting information in the net protocol messages. - */ -typedef struct { - int _referenceCount; - int settingCount; - int modCount; - flib_cfg_setting_meta *settings; - flib_cfg_mod_meta *mods; -} flib_cfg_meta; - -typedef struct { - int _referenceCount; - flib_cfg_meta *meta; - - char *name; - int *settings; - bool *mods; -} flib_cfg; - -/** - * Read the meta-configuration from a .ini file (e.g. which settings exist, - * what are their defaults etc.) - * - * Returns the meta-configuration or NULL. - */ -flib_cfg_meta *flib_cfg_meta_from_ini(const char *filename); - -/** - * Increase the reference count of the object. Call this if you store a pointer to it somewhere. - * Returns the parameter. - */ -flib_cfg_meta *flib_cfg_meta_retain(flib_cfg_meta *metainfo); - -/** - * Decrease the reference count of the object and free it if this was the last reference. - */ -void flib_cfg_meta_release(flib_cfg_meta *metainfo); - -/** - * Create a new configuration with everything set to default or false - * Returns NULL on error. - */ -flib_cfg *flib_cfg_create(flib_cfg_meta *meta, const char *schemeName); - -/** - * Create a copy of the scheme. Returns NULL on error or if NULL was passed. - */ -flib_cfg *flib_cfg_copy(const flib_cfg *cfg); - -/** - * Increase the reference count of the object. Call this if you store a pointer to it somewhere. - * Returns the parameter. - */ -flib_cfg *flib_cfg_retain(flib_cfg *cfg); - -/** - * Decrease the reference count of the object and free it if this was the last reference. - */ -void flib_cfg_release(flib_cfg* cfg); - -/** - * Retrieve a mod setting by its name. If the mod is not found, logs an error and returns false. - */ -bool flib_cfg_get_mod(flib_cfg *cfg, const char *name); - -/** - * Retrieve a game setting by its name. If the setting is not found, logs an error and returns def. - */ -int flib_cfg_get_setting(flib_cfg *cfg, const char *name, int def); - -#endif /* CFG_H_ */ diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/gamesetup.c --- a/project_files/frontlib/model/gamesetup.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/gamesetup.c Tue Jul 10 19:33:57 2012 +0200 @@ -25,7 +25,7 @@ void flib_gamesetup_destroy(flib_gamesetup *gamesetup) { if(gamesetup) { free(gamesetup->script); - flib_cfg_release(gamesetup->gamescheme); + flib_scheme_release(gamesetup->gamescheme); flib_map_release(gamesetup->map); flib_teamlist_destroy(gamesetup->teamlist); free(gamesetup); @@ -40,7 +40,7 @@ flib_gamesetup *result = flib_calloc(1, sizeof(flib_gamesetup)); if(result) { result->script = flib_strdupnull(setup->script); - result->gamescheme = flib_cfg_copy(setup->gamescheme); + result->gamescheme = flib_scheme_copy(setup->gamescheme); result->map = flib_map_copy(setup->map); result->teamlist = flib_teamlist_copy(setup->teamlist); if((setup->script && !result->script) diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/gamesetup.h --- a/project_files/frontlib/model/gamesetup.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/gamesetup.h Tue Jul 10 19:33:57 2012 +0200 @@ -25,14 +25,14 @@ #ifndef MODEL_GAMESETUP_H_ #define MODEL_GAMESETUP_H_ -#include "cfg.h" +#include "scheme.h" #include "weapon.h" #include "map.h" #include "teamlist.h" typedef struct { char *script; - flib_cfg *gamescheme; + flib_scheme *gamescheme; flib_map *map; flib_teamlist *teamlist; } flib_gamesetup; diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/scheme.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/frontlib/model/scheme.c Tue Jul 10 19:33:57 2012 +0200 @@ -0,0 +1,237 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (C) 2012 Simeon Maxein + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "scheme.h" + +#include "../util/inihelper.h" +#include "../util/logging.h" +#include "../util/util.h" +#include "../util/refcounter.h" + +#include +#include +#include +#include + +static void flib_metascheme_destroy(flib_metascheme *meta) { + if(meta) { + if(meta->settings) { + for(int i=0; isettingCount; i++) { + free(meta->settings[i].name); + free(meta->settings[i].engineCommand); + } + free(meta->settings); + } + if(meta->mods) { + for(int i=0; imodCount; i++) { + free(meta->mods[i].name); + } + free(meta->mods); + } + free(meta); + } +} + +static void flib_scheme_destroy(flib_scheme* scheme) { + if(scheme) { + flib_metascheme_release(scheme->meta); + free(scheme->mods); + free(scheme->settings); + free(scheme->name); + free(scheme); + } +} + +static flib_metascheme *flib_metascheme_from_ini_handleError(flib_metascheme *result, flib_ini *ini) { + flib_metascheme_destroy(result); + flib_ini_destroy(ini); + return NULL; +} + +static int readMetaSettingSections(flib_ini *ini, flib_metascheme *result, int limit) { + while(result->settingCountsettingCount) <= 0) { + return -1; + } + if(!flib_ini_enter_section(ini, sectionName)) { + flib_metascheme_setting *metasetting = &result->settings[result->settingCount]; + result->settingCount++; + + bool error = false; + error |= flib_ini_get_str(ini, &metasetting->name, "name"); + error |= flib_ini_get_str_opt(ini, &metasetting->engineCommand, "command", NULL); + error |= flib_ini_get_bool(ini, &metasetting->times1000, "times1000"); + error |= flib_ini_get_bool(ini, &metasetting->maxMeansInfinity, "maxmeansinfinity"); + error |= flib_ini_get_int(ini, &metasetting->min, "min"); + error |= flib_ini_get_int(ini, &metasetting->max, "max"); + error |= flib_ini_get_int(ini, &metasetting->def, "default"); + if(error) { + flib_log_e("Missing or malformed ini parameter in metaconfig, section %s", sectionName); + return -1; + } + } else { + return 0; + } + } + return 0; +} + +static int readMetaModSections(flib_ini *ini, flib_metascheme *result, int limit) { + while(result->modCountmodCount) <= 0) { + return -1; + } + if(!flib_ini_enter_section(ini, sectionName)) { + flib_metascheme_mod *metamod = &result->mods[result->modCount]; + result->modCount++; + + bool error = false; + error |= flib_ini_get_str(ini, &metamod->name, "name"); + error |= flib_ini_get_int(ini, &metamod->bitmaskIndex, "bitmaskIndex"); + if(error) { + flib_log_e("Missing or malformed ini parameter in metaconfig, section %s", sectionName); + return -1; + } + } else { + return 0; + } + } + return 0; +} + +flib_metascheme *flib_metascheme_from_ini(const char *filename) { + if(log_badargs_if(filename==NULL)) { + return NULL; + } + flib_metascheme *result = flib_metascheme_retain(flib_calloc(1, sizeof(flib_metascheme))); + flib_ini *ini = flib_ini_load(filename); + + if(!result || !ini) { + return flib_metascheme_from_ini_handleError(result, ini); + } + + // We're overallocating here for simplicity + int sectionCount = flib_ini_get_sectioncount(ini); + result->settingCount = 0; + result->modCount = 0; + result->settings = flib_calloc(sectionCount, sizeof(flib_metascheme_setting)); + result->mods = flib_calloc(sectionCount, sizeof(flib_metascheme_mod)); + + if(!result->settings || !result->mods) { + return flib_metascheme_from_ini_handleError(result, ini); + } + + if(readMetaSettingSections(ini, result, sectionCount) || readMetaModSections(ini, result, sectionCount)) { + return flib_metascheme_from_ini_handleError(result, ini); + } + + if(result->settingCount+result->modCount != sectionCount) { + flib_log_e("Unknown or non-contiguous sections headers in metaconfig."); + return flib_metascheme_from_ini_handleError(result, ini); + } + + flib_ini_destroy(ini); + return result; +} + +flib_metascheme *flib_metascheme_retain(flib_metascheme *metainfo) { + if(metainfo) { + flib_retain(&metainfo->_referenceCount, "flib_metascheme"); + } + return metainfo; +} + +void flib_metascheme_release(flib_metascheme *meta) { + if(meta && flib_release(&meta->_referenceCount, "flib_metascheme")) { + flib_metascheme_destroy(meta); + } +} + +flib_scheme *flib_scheme_create(flib_metascheme *meta, const char *schemeName) { + flib_scheme *result = flib_scheme_retain(flib_calloc(1, sizeof(flib_scheme))); + if(log_badargs_if2(meta==NULL, schemeName==NULL) || result==NULL) { + return NULL; + } + + result->meta = flib_metascheme_retain(meta); + result->name = flib_strdupnull(schemeName); + result->mods = flib_calloc(meta->modCount, sizeof(*result->mods)); + result->settings = flib_calloc(meta->settingCount, sizeof(*result->settings)); + + if(!result->mods || !result->settings || !result->name) { + flib_scheme_destroy(result); + return NULL; + } + + for(int i=0; isettingCount; i++) { + result->settings[i] = meta->settings[i].def; + } + return result; +} + +flib_scheme *flib_scheme_copy(const flib_scheme *scheme) { + flib_scheme *result = NULL; + if(scheme) { + result = flib_scheme_create(scheme->meta, scheme->name); + if(result) { + memcpy(result->mods, scheme->mods, scheme->meta->modCount * sizeof(*scheme->mods)); + memcpy(result->settings, scheme->settings, scheme->meta->settingCount * sizeof(*scheme->settings)); + } + } + return result; +} + +flib_scheme *flib_scheme_retain(flib_scheme *scheme) { + if(scheme) { + flib_retain(&scheme->_referenceCount, "flib_scheme"); + } + return scheme; +} + +void flib_scheme_release(flib_scheme *scheme) { + if(scheme && flib_release(&scheme->_referenceCount, "flib_scheme")) { + flib_scheme_destroy(scheme); + } +} + +bool flib_scheme_get_mod(flib_scheme *scheme, const char *name) { + if(!log_badargs_if2(scheme==NULL, name==NULL)) { + for(int i=0; imeta->modCount; i++) { + if(!strcmp(scheme->meta->mods[i].name, name)) { + return scheme->mods[i]; + } + } + flib_log_e("Unable to find game mod %s", name); + } + return false; +} + +int flib_scheme_get_setting(flib_scheme *scheme, const char *name, int def) { + if(!log_badargs_if2(scheme==NULL, name==NULL)) { + for(int i=0; imeta->settingCount; i++) { + if(!strcmp(scheme->meta->settings[i].name, name)) { + return scheme->settings[i]; + } + } + flib_log_e("Unable to find game setting %s", name); + } + return def; +} diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/scheme.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/frontlib/model/scheme.h Tue Jul 10 19:33:57 2012 +0200 @@ -0,0 +1,116 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (C) 2012 Simeon Maxein + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** + * Data structures for game scheme information. + */ + +#ifndef SCHEME_H_ +#define SCHEME_H_ + +#include + +typedef struct { + char *name; + char *engineCommand; + bool maxMeansInfinity; + bool times1000; + int min; + int max; + int def; +} flib_metascheme_setting; + +typedef struct { + char *name; + int bitmaskIndex; +} flib_metascheme_mod; + +/** + * The order of the meta information in the arrays is the same as the order + * of the mod/setting information in the net protocol messages. + */ +typedef struct { + int _referenceCount; + int settingCount; + int modCount; + flib_metascheme_setting *settings; + flib_metascheme_mod *mods; +} flib_metascheme; + +typedef struct { + int _referenceCount; + flib_metascheme *meta; + + char *name; + int *settings; + bool *mods; +} flib_scheme; + +/** + * Read the meta-configuration from a .ini file (e.g. which settings exist, + * what are their defaults etc.) + * + * Returns the meta-configuration or NULL. + */ +flib_metascheme *flib_metascheme_from_ini(const char *filename); + +/** + * Increase the reference count of the object. Call this if you store a pointer to it somewhere. + * Returns the parameter. + */ +flib_metascheme *flib_metascheme_retain(flib_metascheme *metainfo); + +/** + * Decrease the reference count of the object and free it if this was the last reference. + */ +void flib_metascheme_release(flib_metascheme *metainfo); + +/** + * Create a new configuration with everything set to default or false + * Returns NULL on error. + */ +flib_scheme *flib_scheme_create(flib_metascheme *meta, const char *schemeName); + +/** + * Create a copy of the scheme. Returns NULL on error or if NULL was passed. + */ +flib_scheme *flib_scheme_copy(const flib_scheme *scheme); + +/** + * Increase the reference count of the object. Call this if you store a pointer to it somewhere. + * Returns the parameter. + */ +flib_scheme *flib_scheme_retain(flib_scheme *scheme); + +/** + * Decrease the reference count of the object and free it if this was the last reference. + */ +void flib_scheme_release(flib_scheme* scheme); + +/** + * Retrieve a mod setting by its name. If the mod is not found, logs an error and returns false. + */ +bool flib_scheme_get_mod(flib_scheme *scheme, const char *name); + +/** + * Retrieve a game setting by its name. If the setting is not found, logs an error and returns def. + */ +int flib_scheme_get_setting(flib_scheme *scheme, const char *name, int def); + +#endif /* SCHEME_H_ */ diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/schemelist.c --- a/project_files/frontlib/model/schemelist.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/schemelist.c Tue Jul 10 19:33:57 2012 +0200 @@ -33,7 +33,7 @@ static void flib_schemelist_destroy(flib_schemelist *list) { if(list) { for(int i=0; ischemeCount; i++) { - flib_cfg_release(list->schemes[i]); + flib_scheme_release(list->schemes[i]); } free(list->schemes); free(list); @@ -44,8 +44,8 @@ return flib_asprintf("%i\\%s", schemeIndex, settingName); } -static int readSettingsFromIni(flib_ini *ini, flib_cfg *scheme, int index) { - flib_cfg_meta *meta = scheme->meta; +static int readSettingsFromIni(flib_ini *ini, flib_scheme *scheme, int index) { + flib_metascheme *meta = scheme->meta; bool error = false; for(int i=0; isettingCount && !error; i++) { char *key = makePrefixedName(index, meta->settings[i].name); @@ -60,8 +60,8 @@ return error; } -static int readModsFromIni(flib_ini *ini, flib_cfg *scheme, int index) { - flib_cfg_meta *meta = scheme->meta; +static int readModsFromIni(flib_ini *ini, flib_scheme *scheme, int index) { + flib_metascheme *meta = scheme->meta; bool error = false; for(int i=0; imodCount && !error; i++) { char *key = makePrefixedName(index, meta->mods[i].name); @@ -76,19 +76,19 @@ return error; } -static flib_cfg *readSchemeFromIni(flib_cfg_meta *meta, flib_ini *ini, int index) { - flib_cfg *result = NULL; +static flib_scheme *readSchemeFromIni(flib_metascheme *meta, flib_ini *ini, int index) { + flib_scheme *result = NULL; char *schemeNameKey = makePrefixedName(index+1, "name"); if(schemeNameKey) { char *schemeName = NULL; if(!flib_ini_get_str_opt(ini, &schemeName, schemeNameKey, "Unnamed")) { - flib_cfg *scheme = flib_cfg_create(meta, schemeName); + flib_scheme *scheme = flib_scheme_create(meta, schemeName); if(scheme) { if(!readSettingsFromIni(ini, scheme, index) && !readModsFromIni(ini, scheme, index)) { - result = flib_cfg_retain(scheme); + result = flib_scheme_retain(scheme); } } - flib_cfg_release(scheme); + flib_scheme_release(scheme); } free(schemeName); } @@ -102,7 +102,7 @@ return NULL; } -flib_schemelist *flib_schemelist_from_ini(flib_cfg_meta *meta, const char *filename) { +flib_schemelist *flib_schemelist_from_ini(flib_metascheme *meta, const char *filename) { if(log_badargs_if2(meta==NULL, filename==NULL)) { return NULL; } @@ -126,13 +126,13 @@ } for(int i=0; imeta; +static int writeSchemeToIni(flib_scheme *scheme, flib_ini *ini, int index) { + flib_metascheme *meta = scheme->meta; bool error = false; char *key = makePrefixedName(index+1, "name"); @@ -199,7 +199,7 @@ } } -flib_cfg *flib_schemelist_find(flib_schemelist *list, const char *name) { +flib_scheme *flib_schemelist_find(flib_schemelist *list, const char *name) { if(!log_badargs_if2(list==NULL, name==NULL)) { for(int i=0; ischemeCount; i++) { if(!strcmp(name, list->schemes[i]->name)) { @@ -210,13 +210,13 @@ return NULL; } -GENERATE_STATIC_LIST_INSERT(insertScheme, flib_cfg*) -GENERATE_STATIC_LIST_DELETE(deleteScheme, flib_cfg*) +GENERATE_STATIC_LIST_INSERT(insertScheme, flib_scheme*) +GENERATE_STATIC_LIST_DELETE(deleteScheme, flib_scheme*) -int flib_schemelist_insert(flib_schemelist *list, flib_cfg *cfg, int pos) { +int flib_schemelist_insert(flib_schemelist *list, flib_scheme *cfg, int pos) { if(!log_badargs_if2(list==NULL, cfg==NULL) && !insertScheme(&list->schemes, &list->schemeCount, cfg, pos)) { - flib_cfg_retain(cfg); + flib_scheme_retain(cfg); return 0; } return -1; @@ -224,9 +224,9 @@ int flib_schemelist_delete(flib_schemelist *list, int pos) { if(!log_badargs_if(list==NULL)) { - flib_cfg *elem = list->schemes[pos]; + flib_scheme *elem = list->schemes[pos]; if(!deleteScheme(&list->schemes, &list->schemeCount, pos)) { - flib_cfg_release(elem); + flib_scheme_release(elem); return 0; } } diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/schemelist.h --- a/project_files/frontlib/model/schemelist.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/schemelist.h Tue Jul 10 19:33:57 2012 +0200 @@ -26,19 +26,19 @@ #ifndef SCHEMELIST_H_ #define SCHEMELIST_H_ -#include "cfg.h" +#include "scheme.h" typedef struct { int _referenceCount; int schemeCount; - flib_cfg **schemes; + flib_scheme **schemes; } flib_schemelist; /** * Load a list of configurations from the ini file. * Returns NULL on error. */ -flib_schemelist *flib_schemelist_from_ini(flib_cfg_meta *meta, const char *filename); +flib_schemelist *flib_schemelist_from_ini(flib_metascheme *meta, const char *filename); /** * Store the list of configurations to an ini file. @@ -57,7 +57,7 @@ * The scheme is retained automatically. * Returns 0 on success. */ -int flib_schemelist_insert(flib_schemelist *list, flib_cfg *cfg, int pos); +int flib_schemelist_insert(flib_schemelist *list, flib_scheme *cfg, int pos); /** * Delete a cfg from the list at position pos, moving down all higher schemes. @@ -69,7 +69,7 @@ /** * Find the scheme with a specific name */ -flib_cfg *flib_schemelist_find(flib_schemelist *list, const char *name); +flib_scheme *flib_schemelist_find(flib_schemelist *list, const char *name); /** * Increase the reference count of the object. Call this if you store a pointer to it somewhere. diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/team.c --- a/project_files/frontlib/model/team.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/team.c Tue Jul 10 19:33:57 2012 +0200 @@ -29,7 +29,7 @@ static flib_team *from_ini_handleError(flib_team *result, flib_ini *settingfile) { flib_ini_destroy(settingfile); - flib_team_release(result); + flib_team_destroy(result); return NULL; } @@ -38,7 +38,7 @@ return NULL; } - flib_team *result = flib_team_retain(flib_calloc(1, sizeof(flib_team))); + flib_team *result = flib_calloc(1, sizeof(flib_team)); flib_ini *ini = NULL; if(!result) { @@ -130,6 +130,30 @@ return result; } +void flib_team_destroy(flib_team *team) { + if(team) { + for(int i=0; ihogs[i].name); + free(team->hogs[i].hat); + flib_weaponset_release(team->hogs[i].weaponset); + } + free(team->name); + free(team->grave); + free(team->fort); + free(team->voicepack); + free(team->flag); + if(team->bindings) { + for(int i=0; ibindingCount; i++) { + free(team->bindings[i].action); + free(team->bindings[i].binding); + } + } + free(team->bindings); + free(team->ownerName); + free(team); + } +} + static int writeTeamSection(const flib_team *team, flib_ini *ini) { if(flib_ini_create_section(ini, "team")) { return -1; @@ -207,37 +231,6 @@ return result; } -flib_team *flib_team_retain(flib_team *team) { - if(team) { - flib_retain(&team->_referenceCount, "flib_team"); - } - return team; -} - -void flib_team_release(flib_team *team) { - if(team && flib_release(&team->_referenceCount, "flib_team")) { - for(int i=0; ihogs[i].name); - free(team->hogs[i].hat); - flib_weaponset_release(team->hogs[i].weaponset); - } - free(team->name); - free(team->grave); - free(team->fort); - free(team->voicepack); - free(team->flag); - if(team->bindings) { - for(int i=0; ibindingCount; i++) { - free(team->bindings[i].action); - free(team->bindings[i].binding); - } - } - free(team->bindings); - free(team->ownerName); - free(team); - } -} - void flib_team_set_weaponset(flib_team *team, flib_weaponset *set) { if(team) { for(int i=0; iteamCount; i++) { - flib_team_release(list->teams[i]); + flib_team_destroy(list->teams[i]); } free(list->teams); free(list); @@ -55,7 +55,6 @@ int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos) { if(!log_badargs_if2(list==NULL, team==NULL) && !insertTeam(&list->teams, &list->teamCount, team, pos)) { - flib_team_retain(team); return 0; } return -1; @@ -68,7 +67,7 @@ if(itemid>=0) { flib_team *team = list->teams[itemid]; if(!deleteTeam(&list->teams, &list->teamCount, itemid)) { - flib_team_release(team); + flib_team_destroy(team); result = 0; } } @@ -90,7 +89,7 @@ void flib_teamlist_clear(flib_teamlist *list) { if(!log_badargs_if(list==NULL)) { for(int i=0; iteamCount; i++) { - flib_team_release(list->teams[i]); + flib_team_destroy(list->teams[i]); } free(list->teams); list->teams = NULL; @@ -107,12 +106,10 @@ bool error = false; for(int i=0; !error && iteamCount; i++) { flib_team *teamcopy = flib_team_copy(list->teams[i]); - if(!teamcopy) { + if(!teamcopy || flib_teamlist_insert(result, teamcopy, i)) { + flib_team_destroy(teamcopy); error = true; - } else { - error |= flib_teamlist_insert(result, teamcopy, i); } - flib_team_release(teamcopy); } if(error) { flib_teamlist_destroy(result); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/teamlist.h --- a/project_files/frontlib/model/teamlist.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/teamlist.h Tue Jul 10 19:33:57 2012 +0200 @@ -32,12 +32,12 @@ void flib_teamlist_destroy(flib_teamlist *list); /** - * Insert a team into the list. Returns 0 on success. + * Insert a team into the list. The list takes ownership of the team. Returns 0 on success. */ int flib_teamlist_insert(flib_teamlist *list, flib_team *team, int pos); /** - * Delete the item with the name [name] from the list. + * Delete the team with the name [name] from the list and destroys it. * Returns 0 on success. */ int flib_teamlist_delete(flib_teamlist *list, const char *name); @@ -48,7 +48,7 @@ flib_team *flib_teamlist_find(const flib_teamlist *list, const char *name); /** - * Removes all items from the list and frees "teams". + * Removes all items from the list and destroys them. */ void flib_teamlist_clear(flib_teamlist *list); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/weapon.c --- a/project_files/frontlib/model/weapon.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/weapon.c Tue Jul 10 19:33:57 2012 +0200 @@ -105,7 +105,7 @@ return result; } -static void flib_weaponsetlist_destroy(flib_weaponsetlist *list) { +void flib_weaponsetlist_destroy(flib_weaponsetlist *list) { if(list) { for(int i=0; iweaponsetCount; i++) { flib_weaponset_release(list->weaponsets[i]); @@ -167,13 +167,12 @@ } else if(flib_ini_enter_section(ini, "General")) { flib_log_e("Missing section \"General\" in file %s.", filename); } else { - flib_weaponsetlist *list = flib_weaponsetlist_create(); - if(list) { - if(!fillWeaponsetsFromIni(list, ini)) { - result = flib_weaponsetlist_retain(list); - } + flib_weaponsetlist *tmpList = flib_weaponsetlist_create(); + if(tmpList && !fillWeaponsetsFromIni(tmpList, ini)) { + result = tmpList; + tmpList = NULL; } - flib_weaponsetlist_release(list); + flib_weaponsetlist_destroy(tmpList); } flib_ini_destroy(ini); } @@ -220,7 +219,7 @@ } flib_weaponsetlist *flib_weaponsetlist_create() { - return flib_weaponsetlist_retain(flib_calloc(1, sizeof(flib_weaponsetlist))); + return flib_calloc(1, sizeof(flib_weaponsetlist)); } GENERATE_STATIC_LIST_INSERT(insertWeaponset, flib_weaponset*) @@ -245,16 +244,3 @@ } return -1; } - -flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list) { - if(list) { - flib_retain(&list->_referenceCount, "flib_weaponsetlist"); - } - return list; -} - -void flib_weaponsetlist_release(flib_weaponsetlist *list) { - if(list && flib_release(&list->_referenceCount, "flib_weaponsetlist")) { - flib_weaponsetlist_destroy(list); - } -} diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/model/weapon.h --- a/project_files/frontlib/model/weapon.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/model/weapon.h Tue Jul 10 19:33:57 2012 +0200 @@ -38,7 +38,6 @@ } flib_weaponset; typedef struct { - int _referenceCount; int weaponsetCount; flib_weaponset **weaponsets; } flib_weaponsetlist; @@ -89,6 +88,11 @@ flib_weaponsetlist *flib_weaponsetlist_create(); /** + * Release all memory associated with the weaponsetlist and release all contained weaponsets + */ +void flib_weaponsetlist_destroy(flib_weaponsetlist *list); + +/** * Insert a new weaponset into the list at position pos, moving all higher weaponsets to make place. * pos must be at least 0 (insert at the start) and at most list->weaponsetCount (insert at the end). * The weaponset is retained automatically. @@ -103,15 +107,4 @@ */ int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos); -/** - * Increase the reference count of the object. Call this if you store a pointer to it somewhere. - * Returns the parameter. - */ -flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list); - -/** - * Decrease the reference count of the object and free it if this was the last reference. - */ -void flib_weaponsetlist_release(flib_weaponsetlist *list); - #endif diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netconn.c --- a/project_files/frontlib/net/netconn.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netconn.c Tue Jul 10 19:33:57 2012 +0200 @@ -34,9 +34,9 @@ #include #include -flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *dataDirPath, const char *host, uint16_t port) { +flib_netconn *flib_netconn_create(const char *playerName, flib_metascheme *metacfg, const char *dataDirPath, const char *host, int port) { flib_netconn *result = NULL; - if(!log_badargs_if3(playerName==NULL, metacfg==NULL, host==NULL)) { + if(!log_badargs_if5(playerName==NULL, metacfg==NULL, host==NULL, port<1, port>65535)) { flib_netconn *newConn = flib_calloc(1, sizeof(flib_netconn)); if(newConn) { newConn->netBase = flib_netbase_create(host, port); @@ -45,7 +45,7 @@ newConn->netconnState = NETCONN_STATE_CONNECTING; newConn->isAdmin = false; - newConn->metaCfg = flib_cfg_meta_retain(metacfg); + newConn->metaCfg = flib_metascheme_retain(metacfg); newConn->roomList.roomCount = 0; newConn->roomList.rooms = NULL; @@ -87,13 +87,13 @@ free(conn->playerName); free(conn->dataDirPath); - flib_cfg_meta_release(conn->metaCfg); + flib_metascheme_release(conn->metaCfg); flib_roomlist_clear(&conn->roomList); flib_map_release(conn->map); flib_teamlist_clear(&conn->pendingTeamlist); flib_teamlist_clear(&conn->teamlist); - flib_cfg_release(conn->scheme); + flib_scheme_release(conn->scheme); free(conn->script); flib_weaponset_release(conn->weaponset); @@ -123,7 +123,7 @@ conn->map = flib_map_create_named("", "NoSuchMap"); flib_teamlist_clear(&conn->pendingTeamlist); flib_teamlist_clear(&conn->teamlist); - flib_cfg_release(conn->scheme); + flib_scheme_release(conn->scheme); conn->scheme = NULL; free(conn->script); conn->script = NULL; @@ -159,10 +159,10 @@ } } -void netconn_setScheme(flib_netconn *conn, const flib_cfg *scheme) { - flib_cfg *copy = flib_cfg_copy(scheme); +void netconn_setScheme(flib_netconn *conn, const flib_scheme *scheme) { + flib_scheme *copy = flib_scheme_copy(scheme); if(copy) { - flib_cfg_release(conn->scheme); + flib_scheme_release(conn->scheme); conn->scheme = copy; } } @@ -182,7 +182,7 @@ if(tmpSetup) { for(int i=0; iteamlist->teamCount; i++) { flib_team_set_weaponset(tmpSetup->teamlist->teams[i], conn->weaponset); - flib_team_set_health(tmpSetup->teamlist->teams[i], flib_cfg_get_setting(conn->scheme, "health", 100)); + flib_team_set_health(tmpSetup->teamlist->teams[i], flib_scheme_get_setting(conn->scheme, "health", 100)); } if(tmpSetup->map->mapgen == MAPGEN_NAMED && tmpSetup->map->name) { flib_mapcfg mapcfg; @@ -332,19 +332,15 @@ flib_log_w("Net: Bad ADD_TEAM message"); } else { flib_team *team = flib_team_from_netmsg(netmsg->parts+1); - flib_team *teamcopy = flib_team_from_netmsg(netmsg->parts+1); - if(!team || !teamcopy) { + if(!team || flib_teamlist_insert(&conn->teamlist, team, conn->teamlist.teamCount)) { + flib_team_destroy(team); conn->netconnState = NETCONN_STATE_DISCONNECTED; conn->onDisconnectedCb(conn->onDisconnectedCtx, NETCONN_DISCONNECT_INTERNAL_ERROR, "Internal error"); exit = true; } else { team->remoteDriven = true; - teamcopy->remoteDriven = true; - flib_teamlist_insert(&conn->teamlist, teamcopy, conn->teamlist.teamCount); conn->onTeamAddCb(conn->onTeamAddCtx, team); } - flib_team_release(team); - flib_team_release(teamcopy); } } else if (!strcmp(cmd, "REMOVE_TEAM")) { if(netmsg->partCount != 2 || !flib_netconn_is_in_room_context(conn)) { @@ -467,14 +463,14 @@ } else { const char *subcmd = netmsg->parts[1]; if(!strcmp(subcmd, "SCHEME") && netmsg->partCount == conn->metaCfg->modCount + conn->metaCfg->settingCount + 3) { - flib_cfg *cfg = flib_netmsg_to_cfg(conn->metaCfg, netmsg->parts+2); + flib_scheme *cfg = flib_netmsg_to_cfg(conn->metaCfg, netmsg->parts+2); if(cfg) { netconn_setScheme(conn, cfg); conn->onCfgSchemeCb(conn->onCfgSchemeCtx, cfg); } else { flib_log_e("Error processing CFG SCHEME message"); } - flib_cfg_release(cfg); + flib_scheme_release(cfg); } else if(!strcmp(subcmd, "FULLMAPCONFIG") && netmsg->partCount == 7) { flib_map *map = flib_netmsg_to_map(netmsg->parts+2); if(map) { diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netconn.h --- a/project_files/frontlib/net/netconn.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netconn.h Tue Jul 10 19:33:57 2012 +0200 @@ -21,7 +21,7 @@ #define NETCONN_H_ #include "../model/gamesetup.h" -#include "../model/cfg.h" +#include "../model/scheme.h" #include "../model/roomlist.h" #include @@ -63,7 +63,7 @@ * Create a new netplay connection with these parameters. * The path to the data directory must end with a path delimiter (e.g. C:\Games\Hedgewars\Data\) */ -flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *dataDirPath, const char *host, uint16_t port); +flib_netconn *flib_netconn_create(const char *playerName, flib_metascheme *metacfg, const char *dataDirPath, const char *host, int port); void flib_netconn_destroy(flib_netconn *conn); /** @@ -252,7 +252,7 @@ * Set the scheme. Only makes sense in room state and if you are chief. * The server does not send a reply. */ -int flib_netconn_send_scheme(flib_netconn *conn, const flib_cfg *scheme); +int flib_netconn_send_scheme(flib_netconn *conn, const flib_scheme *scheme); /** * Inform the server that the round has ended. Call this when the engine @@ -425,7 +425,7 @@ * A new team was added to the room. The person who adds a team does NOT receive this callback (he gets onTeamAccepted instead). * The team does not contain bindings, stats, weaponset, color or the number of hogs. */ -void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, flib_team *team), void *context); +void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, const flib_team *team), void *context); /** * A team was removed from the room. @@ -462,7 +462,7 @@ void flib_netconn_onEngineMessage(flib_netconn *conn, void (*callback)(void *context, const uint8_t *message, size_t size), void *context); -void flib_netconn_onCfgScheme(flib_netconn *conn, void (*callback)(void *context, flib_cfg *scheme), void *context); +void flib_netconn_onCfgScheme(flib_netconn *conn, void (*callback)(void *context, const flib_scheme *scheme), void *context); /** * This is called when the map configuration in a room is changed (or first received). Only non-chiefs receive these messages. @@ -486,7 +486,7 @@ * The weaponset has been changed by the room chief. If you are the chief and change the weaponset yourself, * you will not receive this callback! */ -void flib_netconn_onWeaponsetChanged(flib_netconn *conn, void (*callback)(void *context, flib_weaponset *weaponset), void *context); +void flib_netconn_onWeaponsetChanged(flib_netconn *conn, void (*callback)(void *context, const flib_weaponset *weaponset), void *context); /** * This callback is called if the server informs us that we have admin rights. diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netconn_callbacks.c --- a/project_files/frontlib/net/netconn_callbacks.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netconn_callbacks.c Tue Jul 10 19:33:57 2012 +0200 @@ -116,6 +116,7 @@ static void _noop_callback_##cbName cbParameterTypes {} \ GENERATE_CB_SETTER(cbName, cbParameterTypes, _noop_callback_##cbName) +// TODO Disallow transfer of ownership? GENERATE_CB_SETTER(onMessage, (void *context, int msgtype, const char *msg), defaultCallback_onMessage); GENERATE_CB_SETTER_AND_DEFAULT(onConnected, (void *context)); GENERATE_CB_SETTER_AND_DEFAULT(onDisconnected, (void *context, int reason, const char *message)); @@ -133,17 +134,17 @@ GENERATE_CB_SETTER_AND_DEFAULT(onReadyState, (void *context, const char *nick, bool ready)); GENERATE_CB_SETTER_AND_DEFAULT(onEnterRoom, (void *context, bool chief)); GENERATE_CB_SETTER_AND_DEFAULT(onLeaveRoom, (void *context, int reason, const char *message)); -GENERATE_CB_SETTER_AND_DEFAULT(onTeamAdd, (void *context, flib_team *team)); +GENERATE_CB_SETTER_AND_DEFAULT(onTeamAdd, (void *context, const flib_team *team)); GENERATE_CB_SETTER_AND_DEFAULT(onTeamDelete, (void *context, const char *teamname)); GENERATE_CB_SETTER_AND_DEFAULT(onRunGame, (void *context)); GENERATE_CB_SETTER_AND_DEFAULT(onTeamAccepted, (void *context, const char *teamName)); GENERATE_CB_SETTER_AND_DEFAULT(onHogCountChanged, (void *context, const char *teamName, int hogs)); GENERATE_CB_SETTER_AND_DEFAULT(onTeamColorChanged, (void *context, const char *teamName, int colorIndex)); GENERATE_CB_SETTER_AND_DEFAULT(onEngineMessage, (void *context, const uint8_t *message, size_t size)); -GENERATE_CB_SETTER_AND_DEFAULT(onCfgScheme, (void *context, flib_cfg *scheme)); +GENERATE_CB_SETTER_AND_DEFAULT(onCfgScheme, (void *context, const flib_scheme *scheme)); GENERATE_CB_SETTER_AND_DEFAULT(onMapChanged, (void *context, const flib_map *map, int changetype)); GENERATE_CB_SETTER_AND_DEFAULT(onScriptChanged, (void *context, const char *script)); -GENERATE_CB_SETTER_AND_DEFAULT(onWeaponsetChanged, (void *context, flib_weaponset *weaponset)); +GENERATE_CB_SETTER_AND_DEFAULT(onWeaponsetChanged, (void *context, const flib_weaponset *weaponset)); GENERATE_CB_SETTER_AND_DEFAULT(onAdminAccess, (void *context)); GENERATE_CB_SETTER_AND_DEFAULT(onServerVar, (void *context, const char *name, const char *value)); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netconn_internal.h --- a/project_files/frontlib/net/netconn_internal.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netconn_internal.h Tue Jul 10 19:33:57 2012 +0200 @@ -26,7 +26,6 @@ #include "netconn.h" #include "netbase.h" -#include "../model/cfg.h" #include "../model/roomlist.h" #include "../model/map.h" #include "../model/team.h" @@ -44,14 +43,14 @@ int netconnState; // One of the NETCONN_STATE constants bool isAdmin; // Player is server administrator - flib_cfg_meta *metaCfg; + flib_metascheme *metaCfg; flib_roomlist roomList; bool isChief; // Player can modify the current room flib_map *map; flib_teamlist pendingTeamlist; flib_teamlist teamlist; - flib_cfg *scheme; + flib_scheme *scheme; char *script; flib_weaponset *weaponset; @@ -106,7 +105,7 @@ void (*onLeaveRoomCb)(void *context, int reason, const char *message); void *onLeaveRoomCtx; - void (*onTeamAddCb)(void *context, flib_team *team); + void (*onTeamAddCb)(void *context, const flib_team *team); void *onTeamAddCtx; void (*onTeamDeleteCb)(void *context, const char *teamname); @@ -127,7 +126,7 @@ void (*onEngineMessageCb)(void *context, const uint8_t *message, size_t size); void *onEngineMessageCtx; - void (*onCfgSchemeCb)(void *context, flib_cfg *scheme); + void (*onCfgSchemeCb)(void *context, const flib_scheme *scheme); void *onCfgSchemeCtx; void (*onMapChangedCb)(void *context, const flib_map *map, int changetype); @@ -136,7 +135,7 @@ void (*onScriptChangedCb)(void *context, const char *script); void *onScriptChangedCtx; - void (*onWeaponsetChangedCb)(void *context, flib_weaponset *weaponset); + void (*onWeaponsetChangedCb)(void *context, const flib_weaponset *weaponset); void *onWeaponsetChangedCtx; void (*onAdminAccessCb)(void *context); @@ -154,6 +153,6 @@ void netconn_setMap(flib_netconn *conn, const flib_map *map); void netconn_setWeaponset(flib_netconn *conn, const flib_weaponset *weaponset); void netconn_setScript(flib_netconn *conn, const char *script); -void netconn_setScheme(flib_netconn *conn, const flib_cfg *scheme); +void netconn_setScheme(flib_netconn *conn, const flib_scheme *scheme); #endif diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netconn_send.c --- a/project_files/frontlib/net/netconn_send.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netconn_send.c Tue Jul 10 19:33:57 2012 +0200 @@ -150,10 +150,12 @@ teamcopy->ownerName = flib_strdupnull(conn->playerName); if(teamcopy->ownerName) { flib_teamlist_delete(&conn->pendingTeamlist, team->name); - flib_teamlist_insert(&conn->pendingTeamlist, teamcopy, 0); + if(!flib_teamlist_insert(&conn->pendingTeamlist, teamcopy, 0)) { + teamcopy = NULL; + } } } - flib_team_release(teamcopy); + flib_team_destroy(teamcopy); } int flib_netconn_send_addTeam(flib_netconn *conn, const flib_team *team) { @@ -397,7 +399,7 @@ return -1; } -int flib_netconn_send_scheme(flib_netconn *conn, const flib_cfg *scheme) { +int flib_netconn_send_scheme(flib_netconn *conn, const flib_scheme *scheme) { int result = -1; if(!log_badargs_if3(conn==NULL, scheme==NULL, flib_strempty(scheme->name))) { flib_vector *vec = flib_vector_create(); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netprotocol.c --- a/project_files/frontlib/net/netprotocol.c Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netprotocol.c Tue Jul 10 19:33:57 2012 +0200 @@ -62,7 +62,7 @@ flib_team *flib_team_from_netmsg(char **parts) { flib_team *result = NULL; - flib_team *tmpTeam = flib_team_retain(flib_calloc(1, sizeof(flib_team))); + flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team)); if(tmpTeam) { if(!fillTeamFromMsg(tmpTeam, parts)) { result = tmpTeam; @@ -71,12 +71,12 @@ flib_log_e("Error parsing team from net."); } } - flib_team_release(tmpTeam); + flib_team_destroy(tmpTeam); return result; } -flib_cfg *flib_netmsg_to_cfg(flib_cfg_meta *meta, char **parts) { - flib_cfg *result = flib_cfg_create(meta, parts[0]); +flib_scheme *flib_netmsg_to_cfg(flib_metascheme *meta, char **parts) { + flib_scheme *result = flib_scheme_create(meta, parts[0]); if(result) { for(int i=0; imodCount; i++) { result->mods[i] = !strcmp(parts[i+1], "true"); diff -r a446eafcddeb -r e704706008d4 project_files/frontlib/net/netprotocol.h --- a/project_files/frontlib/net/netprotocol.h Thu Jul 05 22:22:48 2012 +0200 +++ b/project_files/frontlib/net/netprotocol.h Tue Jul 10 19:33:57 2012 +0200 @@ -21,7 +21,7 @@ #define NETPROTOCOL_H_ #include "../model/team.h" -#include "../model/cfg.h" +#include "../model/scheme.h" #include "../model/map.h" #include @@ -35,7 +35,7 @@ * Create a new scheme from this net message, which must have * meta->modCount+meta->settingCount+1 parts. */ -flib_cfg *flib_netmsg_to_cfg(flib_cfg_meta *meta, char **parts); +flib_scheme *flib_netmsg_to_cfg(flib_metascheme *meta, char **parts); /** * Create a new map from this five-part netmsg