naev 0.11.5
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_commodity.h"
#include "nlua_pilot.h"
#include "nlua_outfit.h"
#include "nlua_ship.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.
 
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_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.
 

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

Function Documentation

◆ hook_addDateEvt()

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

Definition at line 718 of file hook.c.

◆ hook_addDateMisn()

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

Definition at line 701 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 531 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 594 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 511 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 575 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 551 of file hook.c.

◆ hook_cleanup()

void hook_cleanup ( void )

Gets rid of all current hooks.

Definition at line 1116 of file hook.c.

◆ hook_clear()

void hook_clear ( void )

Clears the hooks.

Definition at line 1136 of file hook.c.

◆ hook_env()

nlua_env hook_env ( unsigned int hook)

Gets the lua env for a hook.

Definition at line 1019 of file hook.c.

◆ hook_exclusionEnd()

void hook_exclusionEnd ( double dt)

Ends exclusion zone and runs all the queued hooks.

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

◆ hook_free()

static void hook_free ( Hook * h)
static

Frees a hook.

Parameters
hHook to free.

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

◆ hook_get()

static Hook * hook_get ( unsigned int id)
static

Gets a hook by ID.

Definition at line 1007 of file hook.c.

◆ hook_getMission()

static Mission * hook_getMission ( Hook * hook)
static

Gets the mission of a hook.

Definition at line 784 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 862 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 846 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 1232 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 1151 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 478 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 1261 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 239 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 798 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 833 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 821 of file hook.c.

◆ hook_rmRaw()

static void hook_rmRaw ( Hook * h)
static

Removes a hook.

Definition at line 810 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 412 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 363 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 1078 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 1053 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 305 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 1183 of file hook.c.

◆ hooks_executeParam()

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

Definition at line 872 of file hook.c.

◆ hooks_purgeList()

static void hooks_purgeList ( void )
static

Purges the list of deletable hooks.

Definition at line 608 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 999 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 979 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 947 of file hook.c.

◆ hooks_update()

void hooks_update ( double dt)

Updates all the hook timer related stuff.

Definition at line 738 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 647 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 656 of file hook.c.

◆ hq_add()

static int hq_add ( HookQueue_t * hq)
static

Adds a hook to the queue.

Definition at line 149 of file hook.c.

◆ hq_clear()

static void hq_clear ( void )
static

Clears the queued hooks.

Definition at line 177 of file hook.c.

◆ hq_free()

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

◆ hook_id

unsigned int hook_id = 0
static

Unique hook id generator.

Definition at line 116 of file hook.c.

◆ hook_list

Hook* hook_list = NULL
static

Stack of hooks.

Definition at line 117 of file hook.c.

◆ hook_loadingstack

int hook_loadingstack = 0
static

Check if the hooks are being loaded.

Definition at line 119 of file hook.c.

◆ hook_queue

HookQueue_t* hook_queue = NULL
static

The hook queue.

Definition at line 58 of file hook.c.

◆ hook_runningstack

int hook_runningstack = 0
static

Check if stack is running.

Definition at line 118 of file hook.c.

◆ hook_time_accum

ntime_t hook_time_accum = 0
static

Time accumulator.

Definition at line 60 of file hook.c.