naev 0.10.4
Data Structures | Enumerations | Functions | Variables
hook.c File Reference

Handles hooks. More...

#include "hook.h"
#include "array.h"
#include "claim.h"
#include "event.h"
#include "gatherable.h"
#include "log.h"
#include "menu.h"
#include "mission.h"
#include "nlua_evt.h"
#include "nlua_hook.h"
#include "nlua_pilot.h"
#include "nstring.h"
#include "nxml.h"
#include "player.h"
#include "space.h"

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

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 62 of file hook.c.

Function Documentation

◆ hook_addDateEvt()

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

Definition at line 706 of file hook.c.

◆ hook_addDateMisn()

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

Definition at line 689 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 519 of file hook.c.

◆ hook_addFunc()

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

Adds a function hook to be run.

Definition at line 582 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 499 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 563 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 539 of file hook.c.

◆ hook_cleanup()

void hook_cleanup ( void  )

Gets rid of all current hooks.

Definition at line 1104 of file hook.c.

◆ hook_env()

nlua_env hook_env ( unsigned int  hook)

Gets the lua env for a hook.

Definition at line 1007 of file hook.c.

◆ hook_exclusionEnd()

void hook_exclusionEnd ( double  dt)

Ends exclusion zone and runs all the queued hooks.

Definition at line 195 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 187 of file hook.c.

◆ hook_free()

static void hook_free ( Hook h)
static

Frees a hook.

Parameters
hHook to free.

Definition at line 1076 of file hook.c.

◆ hook_genID()

static unsigned int hook_genID ( void  )
static

Generates a new hook id.

Returns
New hook id.

Definition at line 443 of file hook.c.

◆ hook_get()

static Hook * hook_get ( unsigned int  id)
static

Gets a hook by ID.

Definition at line 995 of file hook.c.

◆ hook_getMission()

static Mission * hook_getMission ( Hook hook)
static

Gets the mission of a hook.

Definition at line 772 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 850 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 834 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 1208 of file hook.c.

◆ hook_needSave()

static 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 1127 of file hook.c.

◆ hook_new()

static 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 466 of file hook.c.

◆ hook_parse()

static int hook_parse ( xmlNodePtr  base)
static

Parses an individual hook.

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

Definition at line 1237 of file hook.c.

◆ hook_parseParam()

static int hook_parseParam ( const HookParam param)
static

Parses hook parameters.

Parameters
paramParameters to process.
Returns
Parameters found.

Definition at line 236 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 786 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 821 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 809 of file hook.c.

◆ hook_rmRaw()

static void hook_rmRaw ( Hook h)
static

Removes a hook.

Definition at line 798 of file hook.c.

◆ hook_run()

static 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 400 of file hook.c.

◆ hook_runEvent()

static 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 351 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 1066 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 1041 of file hook.c.

◆ hook_runMisn()

static 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 293 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 1159 of file hook.c.

◆ hooks_executeParam()

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

Definition at line 860 of file hook.c.

◆ hooks_purgeList()

static void hooks_purgeList ( void  )
static

Purges the list of deletable hooks.

Definition at line 596 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 987 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 967 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 935 of file hook.c.

◆ hooks_update()

void hooks_update ( double  dt)

Updates all the hook timer related stuff.

Definition at line 726 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 635 of file hook.c.

◆ hooks_updateDateExecute()

static void hooks_updateDateExecute ( ntime_t  change)
static

Updates date hooks and runs them if necessary.

Definition at line 644 of file hook.c.

◆ hq_add()

static int hq_add ( HookQueue_t hq)
static

Adds a hook to the queue.

Definition at line 146 of file hook.c.

◆ hq_clear()

static void hq_clear ( void  )
static

Clears the queued hooks.

Definition at line 174 of file hook.c.

◆ hq_free()

static void hq_free ( HookQueue_t hq)
static

Frees a queued hook.

Definition at line 165 of file hook.c.

Variable Documentation

◆ hook_atomic

int hook_atomic = 0
static

Whether or not hooks should be queued.

Definition at line 56 of file hook.c.

◆ hook_id

unsigned int hook_id = 0
static

Unique hook id generator.

Definition at line 113 of file hook.c.

◆ hook_list

Hook* hook_list = NULL
static

Stack of hooks.

Definition at line 114 of file hook.c.

◆ hook_loadingstack

int hook_loadingstack = 0
static

Check if the hooks are being loaded.

Definition at line 116 of file hook.c.

◆ hook_queue

HookQueue_t* hook_queue = NULL
static

The hook queue.

Definition at line 55 of file hook.c.

◆ hook_runningstack

int hook_runningstack = 0
static

Check if stack is running.

Definition at line 115 of file hook.c.

◆ hook_time_accum

ntime_t hook_time_accum = 0
static

Time accumulator.

Definition at line 57 of file hook.c.