Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
app_hooks.h
Go to the documentation of this file.
1/* Copyright (C) 2022 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * hooks to allow customization / app-specific behavior.
25 */
26
27/*
28
29Background
30----------
31
32This codebase is shared between several projects, each with differing needs.
33Some of them have e.g. complicated i18n/translation facilities and require
34all text output to go through it; others strive to minimize size and
35therefore do not want to include that.
36Since commenting things out isn't an option with shared source and
37conditional compilation is ugly, we bridge the differences via "hooks".
38These are functions whose behavior is expected to differ between projects;
39they are defined by the app, registered here and called from lib code via
40our trampolines.
41
42
43Introduction
44------------
45
46This module provides a clean interface for other code to call hooks
47and allows the app to register them. It also defines default stub
48implementations.
49
50
51Usage
52-----
53
54In the simplest case, the stubs are already acceptable. Otherwise,
55you need to implement a new version of some hooks, fill an
56AppHooks struct with pointers to those functions (zero the rest),
57and call app_hooks_update.
58
59
60Adding New Functions
61--------------------
62
63Several steps are needed (see below for rationale):
640) HOOKNAME is the name of the desired procedure (e.g. "bundle_logs")
651) add a 'trampoline' (user visible function) declaration to this header
66 (typically named ah_HOOKNAME)
672) add the corresponding implementation, i.e. call to ah.HOOKNAME
683) add a default implementation of the new functionality
69 (typically named def_HOOKNAME)
704) add HOOKNAME member to struct AppHooks declaration
715) set HOOKNAME member to def_HOOKNAME in initialization of
72 'struct AppHooks ah'
736) add HOOKNAME to list in app_hooks_update code
74
75
76Rationale
77---------
78
79While X-Macros would reduce the amount of work needed when adding new
80functions, they confuse static code analysis and VisualAssist X
81(the function definitions are not visible to them).
82We prefer convenience during usage instead of in the rare cases
83where new app hook functions are defined.
84
85note: an X-Macro would define the app hook as such:
86extern const wchar_t*, translate, (const wchar_t* text), (text), return)
87.. and in its various invocations perform the above steps automatically.
88
89*/
90
91#ifndef INCLUDED_APP_HOOKS
92#define INCLUDED_APP_HOOKS
93
94#include "lib/os_path.h"
95
96// trampolines for user code to call the hooks. they encapsulate
97// the details of how exactly to do this.
98
99/**
100 * return path to directory into which crash dumps should be written.
101 *
102 * must be callable at any time - in particular, before VFS init.
103 * paths are typically relative to sys_ExecutablePathname.
104 *
105 * @return path ending with directory separator (e.g. '/').
106 **/
107extern const OsPath& ah_get_log_dir();
108
109/**
110 * gather all app-related logs/information and write it to file.
111 *
112 * used when writing a crash log so that all relevant info is in one file.
113 *
114 * the default implementation attempts to gather 0ad data, but is
115 * fail-safe (doesn't complain if file not found).
116 *
117 * @param f file into which to write.
118 **/
119extern void ah_bundle_logs(FILE* f);
120
121/**
122 * display an error dialog, thus overriding sys_display_error.
123 *
124 * @param text error message.
125 * @param flags see DebugDisplayErrorFlags.
126 * @return ErrorReactionInternal.
127 *
128 * the default implementation just returns ERI_NOT_IMPLEMENTED, which
129 * causes the normal sys_display_error to be used.
130 **/
131extern ErrorReactionInternal ah_display_error(const wchar_t* text, size_t flags);
132
133
134/**
135 * holds a function pointer (allowed to be NULL) for each hook.
136 * passed to app_hooks_update.
137 **/
139{
140 const OsPath& (*get_log_dir)();
141 void (*bundle_logs)(FILE* f);
142 ErrorReactionInternal (*display_error)(const wchar_t* text, size_t flags);
143};
144
145/**
146 * update the app hook function pointers.
147 *
148 * @param ah AppHooks struct. any of its function pointers that are non-zero
149 * override the previous function pointer value
150 * (these default to the stub hooks which are functional but basic).
151 **/
153
154/**
155 * was the app hook changed via app_hooks_update from its default value?
156 *
157 * @param offset_in_struct byte offset within AppHooks (determined via
158 * offsetof) of the app hook function pointer.
159 **/
160extern bool app_hook_was_redefined(size_t offset_in_struct);
161// name is identifier of the function pointer within AppHooks to test.
162#define AH_IS_DEFINED(name) app_hook_was_redefined(offsetof(AppHooks, name))
163
164#endif // INCLUDED_APP_HOOKS
static AppHooks ah
Definition: app_hooks.cpp:63
void ah_bundle_logs(FILE *f)
gather all app-related logs/information and write it to file.
Definition: app_hooks.cpp:110
bool app_hook_was_redefined(size_t offset_in_struct)
was the app hook changed via app_hooks_update from its default value?
Definition: app_hooks.cpp:89
ErrorReactionInternal ah_display_error(const wchar_t *text, size_t flags)
display an error dialog, thus overriding sys_display_error.
Definition: app_hooks.cpp:115
void app_hooks_update(AppHooks *ah)
update the app hook function pointers.
Definition: app_hooks.cpp:78
const OsPath & ah_get_log_dir()
return path to directory into which crash dumps should be written.
Definition: app_hooks.cpp:105
Definition: path.h:80
ErrorReactionInternal
all choices offered by the error dialog.
Definition: debug.h:141
holds a function pointer (allowed to be NULL) for each hook.
Definition: app_hooks.h:139
ErrorReactionInternal(* display_error)(const wchar_t *text, size_t flags)
Definition: app_hooks.h:142
void(* bundle_logs)(FILE *f)
Definition: app_hooks.h:141