From 8e10817997bfe0ab35091ba2bb10bfd00e35941e Mon Sep 17 00:00:00 2001 From: avolkov Date: Tue, 30 Jun 2009 00:07:20 +0000 Subject: [PATCH] Load override.cfg from config dir to add or override menu controls git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3159 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 1 + sc2/doc/devel/debug | 4 ++-- sc2/src/sc2code/libs/input/sdl/input.c | 1 + sc2/src/sc2code/libs/resource/getres.c | 23 +++++++++++++++++++---- sc2/src/sc2code/libs/resource/index.h | 2 ++ sc2/src/sc2code/libs/resource/resinit.c | 7 ++++++- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 8e99db4af..1e6ad0f82 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.7: +- Load override.cfg from user's dir to add or override menu controls - Alex - Allow addons to override any content by placing zips into their 'shadow-content' dir - Alex - Content reorg: font chars now use hexadecimal numbering - Alex diff --git a/sc2/doc/devel/debug b/sc2/doc/devel/debug index 453498156..82063f627 100644 --- a/sc2/doc/devel/debug +++ b/sc2/doc/devel/debug @@ -11,8 +11,8 @@ to be called the next iteration of the main game loop (which will occur when the current activity (IP, HyperSpace, Communication, Battle) changes. By setting this, a function can be called from the main loop, thereby eliminating threading issues that may otherwise arrise. -The debug key can be specified in keys.cfg by adding a line with a text -similar to "Debug: key F11". +The debug key can be specified in user's override.cfg by adding a line +with a text similar to "debug.1 = STRING:key F12". An interactive way to access various debugging code, similar to the uio debug mode (see below) is in the works. diff --git a/sc2/src/sc2code/libs/input/sdl/input.c b/sc2/src/sc2code/libs/input/sdl/input.c index 7050e9ce7..ca3b0100d 100644 --- a/sc2/src/sc2code/libs/input/sdl/input.c +++ b/sc2/src/sc2code/libs/input/sdl/input.c @@ -150,6 +150,7 @@ initKeyConfig (void) /* First, load in the menu keys */ LoadResourceIndex (contentDir, "menu.key", "menu."); + LoadResourceIndex (configDir, "override.cfg", "menu."); for (i = 0; i < NUM_MENU_KEYS; i++) { if (!menu_res_names[i]) diff --git a/sc2/src/sc2code/libs/resource/getres.c b/sc2/src/sc2code/libs/resource/getres.c index f078a0d68..0eaacc0b5 100644 --- a/sc2/src/sc2code/libs/resource/getres.c +++ b/sc2/src/sc2code/libs/resource/getres.c @@ -121,10 +121,10 @@ res_GetResource (RESOURCE res) return NULL; } + if (desc->resdata.ptr == NULL) + loadResourceDesc (desc); if (desc->resdata.ptr != NULL) - return desc->resdata.ptr; - - loadResourceDesc (desc); + ++desc->refcount; return desc->resdata.ptr; // May still be NULL, if the load failed. @@ -176,6 +176,13 @@ res_FreeResource (RESOURCE res) return; } + if (desc->refcount > 0) + --desc->refcount; + else + log_add (log_Debug, "Warning: freeing an unreferenced resource."); + if (desc->refcount > 0) + return; // Still references left + freeFun = desc->vtable->freeFun; if (freeFun == NULL) { @@ -216,7 +223,7 @@ res_DetachResource (RESOURCE res) freeFun = desc->vtable->freeFun; if (freeFun == NULL) { - log_add (log_Debug, "Warning: trying to detatch from a non-heap resource."); + log_add (log_Debug, "Warning: trying to detach from a non-heap resource."); return NULL; } @@ -227,8 +234,16 @@ res_DetachResource (RESOURCE res) return NULL; } + if (desc->refcount > 1) + { + log_add (log_Debug, "Warning: trying to detach a resource referenced " + "%u times", desc->refcount); + return NULL; + } + result = desc->resdata.ptr; desc->resdata.ptr = NULL; + desc->refcount = 0; return result; } diff --git a/sc2/src/sc2code/libs/resource/index.h b/sc2/src/sc2code/libs/resource/index.h index b160ad99a..b72b23f70 100644 --- a/sc2/src/sc2code/libs/resource/index.h +++ b/sc2/src/sc2code/libs/resource/index.h @@ -37,6 +37,8 @@ typedef struct resource_desc char *fname; ResourceHandlers *vtable; RESOURCE_DATA resdata; + // refcount is rudimentary as nothing really frees the descriptors + unsigned refcount; } ResourceDesc; struct resource_index_desc diff --git a/sc2/src/sc2code/libs/resource/resinit.c b/sc2/src/sc2code/libs/resource/resinit.c index 806c9c220..2afd800cd 100644 --- a/sc2/src/sc2code/libs/resource/resinit.c +++ b/sc2/src/sc2code/libs/resource/resinit.c @@ -106,6 +106,7 @@ newResourceDesc (const char *res_id, const char *resval) strncpy (result->fname, path, pathlen); result->fname[pathlen] = '\0'; result->vtable = vtable; + result->refcount = 0; if (vtable->freeFun == NULL) { @@ -324,6 +325,9 @@ res_GetString (const char *key) ResourceDesc *desc = lookupResourceDesc (idx, key); if (!desc || !desc->resdata.ptr || strcmp(desc->vtable->resType, "STRING")) return NULL; + /* TODO: Work out exact STRING semantics, specifically, the lifetime of + * the returned value. If caller is allowed to reference the returned + * value forever, STRING has to be ref-counted. */ return (const char *)desc->resdata.ptr; } @@ -424,7 +428,8 @@ res_Remove (const char *key) { if (oldDesc->resdata.ptr != NULL) { - log_add (log_Warning, "WARNING: Replacing '%s' while it is live", key); + if (oldDesc->refcount > 0) + log_add (log_Warning, "WARNING: Replacing '%s' while it is live", key); if (oldDesc->vtable && oldDesc->vtable->freeFun) { oldDesc->vtable->freeFun(oldDesc->resdata.ptr);