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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/**********************************************************************
Freeciv - Copyright (C) 2005 - The Freeciv Project
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, 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.
***********************************************************************/
/**************************************************************************
Signals implementation.
New signal types can be declared with script_signal_create. Each
signal should have a unique name string.
All signal declarations are in signals_create, for convenience.
A signal may have any number of Lua callback functions connected to it
at any given time.
A signal emission invokes all associated callbacks in the order they were
connected:
* A callback can stop the current signal emission, preventing the callbacks
connected after it from being invoked.
* A callback can detach itself from its associated signal.
Lua callbacks functions are able to do these via their return values.
All Lua callback functions can return a value. Example:
return false
If the value is 'true' the current signal emission will be stopped.
**************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <stdarg.h>
#include "hash.h"
#include "log.h"
#include "registry.h"
#include "script.h"
#include "script_signal.h"
struct signal;
struct signal_callback;
/* get 'struct signal_callback_list' and related functions: */
#define SPECLIST_TAG signal_callback
#define SPECLIST_TYPE struct signal_callback
#include "speclist.h"
#define signal_callback_list_iterate(list, pcallback) \
TYPED_LIST_ITERATE(struct signal_callback, list, pcallback)
#define signal_callback_list_iterate_end LIST_ITERATE_END
/**************************************************************************
Signal datastructure.
**************************************************************************/
struct signal {
int nargs; /* number of arguments to pass */
struct signal_callback_list *callbacks; /* connected callbacks */
};
/**************************************************************************
Signal callback datastructure.
**************************************************************************/
struct signal_callback {
char *name; /* callback function name */
};
/**************************************************************************
Signal datastructure.
**************************************************************************/
static struct hash_table *signals;
/**************************************************************************
Api type names.
**************************************************************************/
static const char *api_type_names[] = {
NULL, NULL, NULL,
"Player",
"City",
"Unit",
"Tile",
"Government",
"Building_Type",
"Nation_Type",
"Unit_Type",
"Tech_Type",
"Terrain"
};
/**************************************************************************
Api type names.
**************************************************************************/
const char *get_api_type_name(enum api_types id)
{
if (id >= 0 && id < API_TYPE_LAST) {
return api_type_names[id];
} else {
return NULL;
}
}
/**************************************************************************
Declare any new signal types you need here.
**************************************************************************/
static void signals_create(void)
{
script_signal_create("turn_started", 2, API_TYPE_INT, API_TYPE_INT);
script_signal_create("unit_moved",
3, API_TYPE_UNIT, API_TYPE_TILE, API_TYPE_TILE);
/* Includes all newly-built cities. */
script_signal_create("city_built", 1, API_TYPE_CITY);
script_signal_create("city_growth", 2, API_TYPE_CITY, API_TYPE_INT);
/* Only includes units built in cities, for now. */
script_signal_create("unit_built", 2, API_TYPE_UNIT, API_TYPE_CITY);
script_signal_create("building_built",
2, API_TYPE_BUILDING_TYPE, API_TYPE_CITY);
/* These can happen for various reasons; the third argument gives the
* reason (a simple string identifier). Example identifiers:
* "pop_cost", "need_tech", "need_building", "need_special",
* "need_terrain", "need_government", "need_nation", "never",
* "unavailable". */
script_signal_create("unit_cant_be_built",
3, API_TYPE_UNIT_TYPE, API_TYPE_CITY, API_TYPE_STRING);
script_signal_create("building_cant_be_built",
3, API_TYPE_BUILDING_TYPE, API_TYPE_CITY,
API_TYPE_STRING);
/* The third argument contains the source: "researched", "traded",
* "stolen", "hut". */
script_signal_create("tech_researched",
3, API_TYPE_TECH_TYPE, API_TYPE_PLAYER,
API_TYPE_STRING);
script_signal_create("hut_enter", 1, API_TYPE_UNIT);
}
/**************************************************************************
Connects a callback function to a certain signal (internal).
**************************************************************************/
static struct signal_callback *
internal_signal_callback_append(struct signal_callback_list *list,
const char *callback_name)
{
struct signal_callback *callback;
callback = fc_malloc(sizeof(*callback));
callback->name = mystrdup(callback_name);
signal_callback_list_append(list, callback);
return callback;
}
/**************************************************************************
Disconnects a callback function from a certain signal (internal).
**************************************************************************/
static void internal_signal_callback_remove(struct signal_callback_list *list,
struct signal_callback *callback)
{
signal_callback_list_unlink(list, callback);
free(callback->name);
free(callback);
}
/**************************************************************************
Create a new signal type (internal).
**************************************************************************/
static void internal_signal_create(const char *signal_name,
int nargs, enum api_types args[])
{
if (hash_key_exists(signals, signal_name)) {
freelog(LOG_ERROR, "Signal \"%s\" was already created.", signal_name);
} else {
char *name;
struct signal *signal;
name = mystrdup(signal_name);
signal = fc_malloc(sizeof(*signal));
signal->nargs = nargs;
signal->callbacks = signal_callback_list_new();
hash_insert(signals, name, signal);
}
}
/**************************************************************************
Free a signal type (internal).
**************************************************************************/
static void internal_signal_free(const char *signal_name)
{
const void *pname;
const void *psignal;
if (hash_lookup(signals, signal_name, &pname, &psignal)) {
struct signal *signal = (struct signal *)psignal;
signal_callback_list_iterate(signal->callbacks, pcallback) {
internal_signal_callback_remove(signal->callbacks, pcallback);
} signal_callback_list_iterate_end;
signal_callback_list_unlink_all(signal->callbacks);
signal_callback_list_free(signal->callbacks);
free(signal);
} else {
freelog(LOG_ERROR, "Signal \"%s\" does not exist, so cannot be freed.",
signal_name);
}
}
/**************************************************************************
Invoke all the callback functions attached to a given signal.
**************************************************************************/
void script_signal_emit(const char *signal_name, int nargs, ...)
{
struct hash_table *hash;
struct signal *signal;
va_list args;
hash = signals;
signal = hash_lookup_data(hash, signal_name);
if (signal) {
if (signal->nargs != nargs) {
freelog(LOG_ERROR,
"Signal \"%s\" requires %d args, was passed %d on invoke.",
signal_name, signal->nargs, nargs);
} else {
signal_callback_list_iterate(signal->callbacks, pcallback) {
va_start(args, nargs);
if (script_callback_invoke(pcallback->name, nargs, args)) {
va_end(args);
break;
}
va_end(args);
} signal_callback_list_iterate_end;
}
} else {
freelog(LOG_ERROR, "Signal \"%s\" does not exist, so cannot be invoked.",
signal_name);
}
}
/**************************************************************************
Create a new signal type.
**************************************************************************/
void script_signal_create_valist(const char *signal_name,
int nargs, va_list args)
{
struct signal *signal;
signal = hash_lookup_data(signals, signal_name);
if (signal) {
freelog(LOG_ERROR, "Signal \"%s\" was already created.", signal_name);
} else {
enum api_types args_array[nargs];
int i;
for (i = 0; i < nargs; i++) {
args_array[i] = va_arg(args, int);
}
internal_signal_create(signal_name, nargs, args_array);
}
}
/**************************************************************************
Create a new signal type.
**************************************************************************/
void script_signal_create(const char *signal_name, int nargs, ...)
{
va_list args;
va_start(args, nargs);
script_signal_create_valist(signal_name, nargs, args);
va_end(args);
}
/**************************************************************************
Connects a callback function to a certain signal.
**************************************************************************/
void script_signal_connect(const char *signal_name, const char *callback_name)
{
if (!signal_name) {
script_error("nil string argument 'signal_name'.");
} else if (!callback_name) {
script_error("nil string argument 'callback_name'.");
} else {
struct signal *signal;
signal = hash_lookup_data(signals, signal_name);
if (signal) {
internal_signal_callback_append(signal->callbacks, callback_name);
} else {
script_error("Signal \"%s\" does not exist.", signal_name);
}
}
}
/**************************************************************************
Initialize script signals and callbacks.
**************************************************************************/
void script_signals_init(void)
{
if (!signals) {
signals = hash_new(hash_fval_string, hash_fcmp_string);
assert(ARRAY_SIZE(api_type_names) == API_TYPE_LAST);
signals_create();
}
}
/**************************************************************************
Free script signals and callbacks.
**************************************************************************/
void script_signals_free(void)
{
if (signals) {
unsigned int n = hash_num_entries(signals), i;
for (i = 0; i < n; i++) {
const char *signal_name;
signal_name = hash_key_by_number(signals, i);
internal_signal_free(signal_name);
}
hash_free(signals);
signals = NULL;
}
}
|