naev 0.12.6
hook.c File Reference

Handles hooks. More...

#include "hook.h"
#include "array.h"
#include "claim.h"
#include "event.h"
#include "log.h"
#include "menu.h"
#include "mission.h"
#include "nlua_commodity.h"
#include "nlua_evt.h"
#include "nlua_hook.h"
#include "nlua_outfit.h"
#include "nlua_pilot.h"
#include "nlua_ship.h"
#include "player.h"
#include "space.h"
Include dependency graph for hook.c:

Go to the source code of this file.

Data Structures

struct  HookQueue_t
 Hook queue to delay execution. More...
struct  Hook
 Internal representation of a hook. More...

Enumerations

enum  HookType_t { HOOK_TYPE_NULL , HOOK_TYPE_MISN , HOOK_TYPE_EVENT , HOOK_TYPE_FUNC }
 Types of hook. More...

Functions

static int hooks_executeParam (const char *stack, const HookParam *param)
static void hooks_updateDateExecute (ntime_t change)
 Updates date hooks and runs them if necessary.
static void hook_rmRaw (Hook *h)
 Removes a hook.
static void hooks_purgeList (void)
 Purges the list of deletable hooks.
static Hookhook_get (unsigned int id)
 Gets a hook by ID.
static unsigned int hook_genID (void)
 Generates a new hook id.
static Hookhook_new (HookType_t type, const char *stack)
 Generates and allocates a new hook.
static int hook_parseParam (const HookParam *param)
 Parses hook parameters.
static int hook_runMisn (Hook *hook, const HookParam *param, int claims)
 Runs a mission hook.
static int hook_runEvent (Hook *hook, const HookParam *param, int claims)
 Runs a Event function hook.
static int hook_run (Hook *hook, const HookParam *param, int claims)
 Runs a hook.
static void hook_free (Hook *h)
 Frees a hook.
static int hook_needSave (Hook *h)
 Checks if a hook needs to be saved.
static int hook_parse (xmlNodePtr base)
 Parses an individual hook.
int hook_save (xmlTextWriterPtr writer)
 Saves all the hooks.
int hook_load (xmlNodePtr parent)
 Loads hooks for a player.
static Missionhook_getMission (Hook *hook)
 Gets the mission of a hook.
static int hq_add (HookQueue_t *hq)
static void hq_free (HookQueue_t *hq)
 Frees a queued hook.
static void hq_clear (void)
 Clears the queued hooks.
void hook_exclusionStart (void)
 Starts the hook exclusion zone, this makes hooks queue until exclusion is done.
void hook_exclusionEnd (double dt)
 Ends exclusion zone and runs all the queued hooks.
unsigned int hook_addMisn (unsigned int parent, const char *func, const char *stack)
 Adds a new mission type hook.
unsigned int hook_addEvent (unsigned int parent, const char *func, const char *stack)
 Adds a new event type hook.
unsigned int hook_addTimerMisn (unsigned int parent, const char *func, double ms)
 Adds a new mission type hook timer hook.
unsigned int hook_addTimerEvt (unsigned int parent, const char *func, double ms)
 Adds a new event type hook timer.
unsigned int hook_addTimerFunc (int(*func)(void *), void *data, double ms)
 Adds a function hook to be run.
unsigned int hook_addFunc (int(*func)(void *), void *data, const char *stack)
 Adds a function hook to be run.
void hooks_updateDate (ntime_t change)
 Updates the time to see if it should be updated.
unsigned int hook_addDateMisn (unsigned int parent, const char *func, ntime_t resolution)
unsigned int hook_addDateEvt (unsigned int parent, const char *func, ntime_t resolution)
void hooks_update (double dt)
 Updates all the hook timer related stuff.
void hook_rm (unsigned int id)
 Removes a hook.
void hook_rmMisnParent (unsigned int parent)
 Removes all hooks belonging to parent mission.
void hook_rmEventParent (unsigned int parent)
 Removes all hooks belonging to parent event.
int hook_hasMisnParent (unsigned int parent)
 Checks to see how many hooks there are with the same mission parent.
int hook_hasEventParent (unsigned int parent)
 Checks to see how many hooks there are with the same event parent.
int hooks_runParamDeferred (const char *stack, const HookParam *param)
 Runs all the hooks of stack in the next frame. Does not trigger right away.
int hooks_runParam (const char *stack, const HookParam *param)
 Runs all the hooks of stack.
int hooks_run (const char *stack)
 Runs all the hooks of stack.
nlua_env hook_env (unsigned int hook)
 Gets the lua env for a hook.
int hook_runIDparam (unsigned int id, const HookParam *param)
 Runs a single hook by id.
int hook_runID (unsigned int id)
 Runs a single hook by id.
void hook_cleanup (void)
 Gets rid of all current hooks.
void hook_clear (void)
 Clears the hooks.
void hook_clearMissionTimers (unsigned int parent)
 Clears the timer hooks for a mission.
void hook_clearEventTimers (unsigned int parent)
 Clears the timer hooks for an event.

Variables

static HookQueue_thook_queue = NULL
static int hook_atomic = 0
static ntime_t hook_time_accum = 0
static unsigned int hook_id = 0
static Hookhook_list = NULL
static int hook_runningstack = 0
static int hook_loadingstack = 0

Detailed Description

Handles hooks.

Hooks have a major issue, they are sort of like a poor man's threading. This means get all the issues related to threading. The main issues here are the fact that the hooks can mess with the game state during the update and break everything. The solution is to handle hooks either before the update stage (input stage) or after update stage (render stage). This leaves the update stage as sort of an atomic block that doesn't have to worry about state corruption.

The flaw in this design is that it's still possible for hooks to bash other hooks. Notably the player.teleport() is a very dangerous function as it'll destroy the entire current Naev state which will most likely cause all the other hooks to fail.

Therefore we must tread carefully. Hooks are serious business.

Definition in file hook.c.

Enumeration Type Documentation

◆ HookType_t

enum HookType_t

Types of hook.

Enumerator
HOOK_TYPE_NULL 

Invalid hook type.

HOOK_TYPE_MISN 

Mission hook type.

HOOK_TYPE_EVENT 

Event hook type.

HOOK_TYPE_FUNC 

C function hook type.

Definition at line 63 of file hook.c.

Function Documentation

◆ hook_addDateEvt()

unsigned int hook_addDateEvt ( unsigned int parent,
const char * func,
ntime_t resolution )

Definition at line 764 of file hook.c.

◆ hook_addDateMisn()

unsigned int hook_addDateMisn ( unsigned int parent,
const char * func,
ntime_t resolution )

Definition at line 746 of file hook.c.

◆ hook_addEvent()

unsigned int hook_addEvent ( unsigned int parent,
const char * func,
const char * stack )

Adds a new event type hook.

Parameters
parentHook event parent.
funcFunction to run when hook is triggered.
stackStack hook belongs to.
Returns
The new hook identifier.

Definition at line 550 of file hook.c.

◆ hook_addFunc()

unsigned int hook_addFunc ( int(* func )(void *),
void * data,
const char * stack )

Adds a function hook to be run.

Definition at line 634 of file hook.c.

◆ hook_addMisn()

unsigned int hook_addMisn ( unsigned int parent,
const char * func,
const char * stack )

Adds a new mission type hook.

Parameters
parentHook mission parent.
funcFunction to run when hook is triggered.
stackStack hook belongs to.
Returns
The new hook identifier.

Definition at line 529 of file hook.c.

◆ hook_addTimerEvt()

unsigned int hook_addTimerEvt ( unsigned int parent,
const char * func,
double ms )

Adds a new event type hook timer.

Parameters
parentHook event parent.
funcFunction to run when hook is triggered.
msMilliseconds to wait.
Returns
The new hook identifier.

Definition at line 596 of file hook.c.

◆ hook_addTimerFunc()

unsigned int hook_addTimerFunc ( int(* func )(void *),
void * data,
double ms )

Adds a function hook to be run.

Definition at line 616 of file hook.c.

◆ hook_addTimerMisn()

unsigned int hook_addTimerMisn ( unsigned int parent,
const char * func,
double ms )

Adds a new mission type hook timer hook.

Parameters
parentHook mission parent.
funcFunction to run when hook is triggered.
msMilliseconds to wait
Returns
The new hook identifier.

Definition at line 571 of file hook.c.

◆ hook_cleanup()

void hook_cleanup ( void )

Gets rid of all current hooks.

Definition at line 1167 of file hook.c.

◆ hook_clear()

void hook_clear ( void )

Clears the hooks.

Definition at line 1187 of file hook.c.

◆ hook_clearEventTimers()

void hook_clearEventTimers ( unsigned int parent)

Clears the timer hooks for an event.

Definition at line 1212 of file hook.c.

◆ hook_clearMissionTimers()

void hook_clearMissionTimers ( unsigned int parent)

Clears the timer hooks for a mission.

Definition at line 1199 of file hook.c.

◆ hook_env()

nlua_env hook_env ( unsigned int hook)

Gets the lua env for a hook.

Definition at line 1069 of file hook.c.

◆ hook_exclusionEnd()

void hook_exclusionEnd ( double dt)

Ends exclusion zone and runs all the queued hooks.

Definition at line 199 of file hook.c.

◆ hook_exclusionStart()

void hook_exclusionStart ( void )

Starts the hook exclusion zone, this makes hooks queue until exclusion is done.

Definition at line 191 of file hook.c.

◆ hook_free()

void hook_free ( Hook * h)
static

Frees a hook.

Parameters
hHook to free.

Definition at line 1139 of file hook.c.

◆ hook_genID()

unsigned int hook_genID ( void )
static

Generates a new hook id.

Returns
New hook id.

Definition at line 473 of file hook.c.

◆ hook_get()

Hook * hook_get ( unsigned int id)
static

Gets a hook by ID.

Definition at line 1057 of file hook.c.

◆ hook_getMission()

Mission * hook_getMission ( Hook * hook)
static

Gets the mission of a hook.

Definition at line 832 of file hook.c.

◆ hook_hasEventParent()

int hook_hasEventParent ( unsigned int parent)

Checks to see how many hooks there are with the same event parent.

Parameters
parentID of the parent.
Returns
Number of children hooks the parent has.

Definition at line 910 of file hook.c.

◆ hook_hasMisnParent()

int hook_hasMisnParent ( unsigned int parent)

Checks to see how many hooks there are with the same mission parent.

Parameters
parentID of the parent.
Returns
Number of children hooks the parent has.

Definition at line 894 of file hook.c.

◆ hook_load()

int hook_load ( xmlNodePtr parent)

Loads hooks for a player.

Parameters
parentParent xml node containing the hooks.
Returns
0 on success.

Definition at line 1310 of file hook.c.

◆ hook_needSave()

int hook_needSave ( Hook * h)
static

Checks if a hook needs to be saved.

Parameters
hHook to check if it should be saved.
Returns
1 if hook should be saved.

Definition at line 1228 of file hook.c.

◆ hook_new()

Hook * hook_new ( HookType_t type,
const char * stack )
static

Generates and allocates a new hook.

Parameters
typeType of hook to create.
stackStack to which the new hook belongs.
Returns
The newly allocated hook.
Todo
fix this hack.

Definition at line 496 of file hook.c.

◆ hook_parse()

int hook_parse ( xmlNodePtr base)
static

Parses an individual hook.

Parameters
baseParent xml node of the hook.
Returns
0 on success.

Definition at line 1339 of file hook.c.

◆ hook_parseParam()

int hook_parseParam ( const HookParam * param)
static

Parses hook parameters.

Parameters
paramParameters to process.
Returns
Parameters found.

Definition at line 241 of file hook.c.

◆ hook_rm()

void hook_rm ( unsigned int id)

Removes a hook.

Parameters
idIdentifier of the hook to remove.

Definition at line 846 of file hook.c.

◆ hook_rmEventParent()

void hook_rmEventParent ( unsigned int parent)

Removes all hooks belonging to parent event.

Parameters
parentParent id to remove all hooks belonging to.

Definition at line 881 of file hook.c.

◆ hook_rmMisnParent()

void hook_rmMisnParent ( unsigned int parent)

Removes all hooks belonging to parent mission.

Parameters
parentParent id to remove all hooks belonging to.

Definition at line 869 of file hook.c.

◆ hook_rmRaw()

void hook_rmRaw ( Hook * h)
static

Removes a hook.

Definition at line 858 of file hook.c.

◆ hook_run()

int hook_run ( Hook * hook,
const HookParam * param,
int claims )
static

Runs a hook.

Parameters
hookHook to run.
paramParameters to pass.
claimsWhether the hook is contingent on the mission/event claiming the current system.
Returns
0 on success.

Definition at line 429 of file hook.c.

◆ hook_runEvent()

int hook_runEvent ( Hook * hook,
const HookParam * param,
int claims )
static

Runs a Event function hook.

Parameters
hookHook to run.
paramParameters to pass.
claimsWhether the hook is contingent on the mission/event claiming the current system.
Returns
0 on success.

Definition at line 375 of file hook.c.

◆ hook_runID()

int hook_runID ( unsigned int id)

Runs a single hook by id.

Parameters
idIdentifier of the hook to run.
Returns
The ID of the hook or 0 if it got deleted.

Definition at line 1129 of file hook.c.

◆ hook_runIDparam()

int hook_runIDparam ( unsigned int id,
const HookParam * param )

Runs a single hook by id.

Parameters
idIdentifier of the hook to run.
paramParameters to process.
Returns
The ID of the hook or 0 if it got deleted.

Definition at line 1103 of file hook.c.

◆ hook_runMisn()

int hook_runMisn ( Hook * hook,
const HookParam * param,
int claims )
static

Runs a mission hook.

Parameters
hookHook to run.
paramParameters to pass.
claimsWhether the hook is contingent on the mission/event claiming the current system.
Returns
0 on success.

Definition at line 311 of file hook.c.

◆ hook_save()

int hook_save ( xmlTextWriterPtr writer)

Saves all the hooks.

Parameters
writerXML Writer to use.
Returns
0 on success.

Definition at line 1261 of file hook.c.

◆ hooks_executeParam()

int hooks_executeParam ( const char * stack,
const HookParam * param )
static

Definition at line 920 of file hook.c.

◆ hooks_purgeList()

void hooks_purgeList ( void )
static

Purges the list of deletable hooks.

Definition at line 649 of file hook.c.

◆ hooks_run()

int hooks_run ( const char * stack)

Runs all the hooks of stack.

Parameters
stackStack to run.
Returns
0 on success.

Definition at line 1049 of file hook.c.

◆ hooks_runParam()

int hooks_runParam ( const char * stack,
const HookParam * param )

Runs all the hooks of stack.

Parameters
stackStack to run.
paramParameters to pass.
Returns
0 on success.

Definition at line 1029 of file hook.c.

◆ hooks_runParamDeferred()

int hooks_runParamDeferred ( const char * stack,
const HookParam * param )

Runs all the hooks of stack in the next frame. Does not trigger right away.

Parameters
stackStack to run.
paramParameters to pass.
Returns
0 on success.

Definition at line 996 of file hook.c.

◆ hooks_update()

void hooks_update ( double dt)

Updates all the hook timer related stuff.

Definition at line 785 of file hook.c.

◆ hooks_updateDate()

void hooks_updateDate ( ntime_t change)

Updates the time to see if it should be updated.

Definition at line 688 of file hook.c.

◆ hooks_updateDateExecute()

void hooks_updateDateExecute ( ntime_t change)
static

Updates date hooks and runs them if necessary.

Definition at line 697 of file hook.c.

◆ hq_add()

int hq_add ( HookQueue_t * hq)
static

Adds a hook to the queue.

Definition at line 148 of file hook.c.

◆ hq_clear()

void hq_clear ( void )
static

Clears the queued hooks.

Definition at line 177 of file hook.c.

◆ hq_free()

void hq_free ( HookQueue_t * hq)
static

Frees a queued hook.

Definition at line 168 of file hook.c.

Variable Documentation

◆ hook_atomic

int hook_atomic = 0
static

Whether or not hooks should be queued.

Definition at line 57 of file hook.c.

◆ hook_id

unsigned int hook_id = 0
static

Unique hook id generator.

Definition at line 115 of file hook.c.

◆ hook_list

Hook* hook_list = NULL
static

Stack of hooks.

Definition at line 116 of file hook.c.

◆ hook_loadingstack

int hook_loadingstack = 0
static

Check if the hooks are being loaded.

Definition at line 118 of file hook.c.

◆ hook_queue

HookQueue_t* hook_queue = NULL
static

The hook queue.

Definition at line 56 of file hook.c.

◆ hook_runningstack

int hook_runningstack = 0
static

Check if stack is running.

Definition at line 117 of file hook.c.

◆ hook_time_accum

ntime_t hook_time_accum = 0
static

Time accumulator.

Definition at line 58 of file hook.c.