2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* hooks to allow customization / app-specific behavior.
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "app_hooks.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "lib/path_util.h"
|
2007-04-30 16:35:19 +02:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// default implementations
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
static void def_override_gl_upload_caps()
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
static const char* def_get_log_dir()
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
static char N_log_dir[PATH_MAX];
|
|
|
|
ONCE(\
|
2007-01-13 19:37:51 +01:00
|
|
|
(void)sys_get_executable_name(N_log_dir, ARRAY_SIZE(N_log_dir));\
|
2006-04-24 01:14:18 +02:00
|
|
|
/* strip app name (we only want its path) */\
|
2007-01-13 19:37:51 +01:00
|
|
|
path_strip_fn(N_log_dir);\
|
2006-04-24 01:14:18 +02:00
|
|
|
);
|
|
|
|
return N_log_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 19:37:51 +01:00
|
|
|
static void def_bundle_logs(FILE* UNUSED(f))
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
static const wchar_t* def_translate(const wchar_t* text)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
static void def_translate_free(const wchar_t* UNUSED(text))
|
2006-06-25 17:35:28 +02:00
|
|
|
{
|
|
|
|
// no-op - translate() doesn't own the pointer.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
static void def_log(const wchar_t* text)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
wprintf(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 13:27:55 +02:00
|
|
|
static ErrorReaction def_display_error(const wchar_t* UNUSED(text), size_t UNUSED(flags))
|
2006-06-22 20:26:08 +02:00
|
|
|
{
|
|
|
|
return ER_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
// contains the current set of hooks. starts with the default values and
|
2006-06-25 17:35:28 +02:00
|
|
|
// may be changed via app_hooks_update.
|
2006-04-24 01:14:18 +02:00
|
|
|
//
|
|
|
|
// rationale: we don't ever need to switch "hook sets", so one global struct
|
2007-01-13 19:37:51 +01:00
|
|
|
// is fine. by always having one defined, we also avoid the trampolines
|
|
|
|
// having to check whether their function pointer is valid.
|
2006-04-24 01:14:18 +02:00
|
|
|
static AppHooks ah =
|
|
|
|
{
|
2006-09-21 14:06:56 +02:00
|
|
|
def_override_gl_upload_caps,
|
|
|
|
def_get_log_dir,
|
|
|
|
def_bundle_logs,
|
|
|
|
def_translate,
|
|
|
|
def_translate_free,
|
|
|
|
def_log,
|
|
|
|
def_display_error
|
2006-04-24 01:14:18 +02:00
|
|
|
};
|
|
|
|
|
2007-01-13 19:37:51 +01:00
|
|
|
// separate copy of ah; used to determine if a particular hook has been
|
|
|
|
// redefined. the additional storage needed is negligible and this is
|
|
|
|
// easier than comparing each value against its corresponding def_ value.
|
|
|
|
static AppHooks default_ah = ah;
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// register the specified hook function pointers. any of them that
|
|
|
|
// are non-zero override the previous function pointer value
|
|
|
|
// (these default to the stub hooks which are functional but basic).
|
2006-09-21 14:06:56 +02:00
|
|
|
void app_hooks_update(AppHooks* new_ah)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-09-21 14:06:56 +02:00
|
|
|
debug_assert(new_ah);
|
|
|
|
|
|
|
|
#define OVERRIDE_IF_NONZERO(HOOKNAME) if(new_ah->HOOKNAME) ah.HOOKNAME = new_ah->HOOKNAME;
|
|
|
|
OVERRIDE_IF_NONZERO(override_gl_upload_caps)
|
|
|
|
OVERRIDE_IF_NONZERO(get_log_dir)
|
|
|
|
OVERRIDE_IF_NONZERO(bundle_logs)
|
|
|
|
OVERRIDE_IF_NONZERO(translate)
|
|
|
|
OVERRIDE_IF_NONZERO(translate_free)
|
|
|
|
OVERRIDE_IF_NONZERO(log)
|
|
|
|
OVERRIDE_IF_NONZERO(display_error)
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-01-13 19:37:51 +01:00
|
|
|
bool app_hook_was_redefined(size_t offset_in_struct)
|
|
|
|
{
|
|
|
|
const u8* ah_bytes = (const u8*)&ah;
|
|
|
|
const u8* default_ah_bytes = (const u8*)&default_ah;
|
|
|
|
typedef void(*FP)(); // a bit safer than comparing void* pointers
|
|
|
|
if(*(FP)(ah_bytes+offset_in_struct) != *(FP)(default_ah_bytes+offset_in_struct))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// trampoline implementations
|
|
|
|
// (boilerplate code; hides details of how to call the app hook)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void ah_override_gl_upload_caps(void)
|
|
|
|
{
|
2007-01-13 19:37:51 +01:00
|
|
|
if(ah.override_gl_upload_caps)
|
|
|
|
ah.override_gl_upload_caps();
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2006-09-21 14:06:56 +02:00
|
|
|
const char* ah_get_log_dir(void)
|
|
|
|
{
|
|
|
|
return ah.get_log_dir();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ah_bundle_logs(FILE* f)
|
|
|
|
{
|
|
|
|
ah.bundle_logs(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t* ah_translate(const wchar_t* text)
|
|
|
|
{
|
|
|
|
return ah.translate(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ah_translate_free(const wchar_t* text)
|
|
|
|
{
|
|
|
|
ah.translate_free(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ah_log(const wchar_t* text)
|
|
|
|
{
|
|
|
|
ah.log(text);
|
|
|
|
}
|
|
|
|
|
2008-09-18 13:27:55 +02:00
|
|
|
ErrorReaction ah_display_error(const wchar_t* text, size_t flags)
|
2006-09-21 14:06:56 +02:00
|
|
|
{
|
|
|
|
return ah.display_error(text, flags);
|
|
|
|
}
|