Skip to content

Commit

Permalink
Merge pull request #39 from eatnumber1/event_scripts
Browse files Browse the repository at this point in the history
Add an events framework which calls out to scripts.
  • Loading branch information
clockfort committed Jun 30, 2013
2 parents d96b329 + 37cea97 commit 5dd1677
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
15 changes: 15 additions & 0 deletions libnethack/include/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef EVENTS_H
#define EVENTS_H

#include <stddef.h>
#include <stdbool.h>

typedef enum {
EVENT_TYPE_DEAD = 0,
EVENT_TYPE_MAX
} EventType;

int event_trigger( EventType type, size_t args_len, char * const args[static args_len], bool async );
void event_trigger_async( EventType type, size_t args_len, char * const args[static args_len] );

#endif
1 change: 1 addition & 0 deletions libnethack/include/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ extern int delete_bonesfile(char *bonesid);
extern void paniclog(const char *, const char *);
extern boolean lock_fd(int fd, int retry);
extern void unlock_fd(int fd);
const char *fqname(const char *filename, int whichprefix, int buffnum);

/* ### fountain.c ### */

Expand Down
1 change: 1 addition & 0 deletions libnethack/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set (LIBNETHACK_SRC
steal.c steed.c teleport.c timeout.c topten.c track.c trap.c
uhitm.c u_init.c vault.c version.c vision.c weapon.c were.c
wield.c windows.c wizard.c worm.c worn.c write.c xmalloc.c zap.c
events.c
)
set (LIBNETHACK_GENERATED_SRC
${LNH_SRC_GEN}/monstr.c
Expand Down
52 changes: 52 additions & 0 deletions libnethack/src/events.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "events.h"
#include "hack.h"
#include "extern.h"

#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/wait.h>

// TODO: Make this a configurable option
#define EVENT_SCRIPT "nethack_event.sh"

static const char * const event_type_strings[EVENT_TYPE_MAX] = {
[EVENT_TYPE_DEAD] = "EVENT_TYPE_DEAD",
};

int event_trigger( EventType type, size_t args_len, char * const args[static args_len], bool async ) {
if( type >= EVENT_TYPE_MAX ) impossible("unrecognized event type");

pid_t pid = fork();
if( pid == -1 ) {
if( async ) {
pline("fork(): %s", strerror(errno));
} else {
impossible("fork(): %s", strerror(errno));
}
} else if( pid == 0 ) {
const char *argv[args_len + 3];
memcpy(argv + 2, args, args_len * sizeof(char *));
argv[args_len + 2] = NULL;
argv[0] = EVENT_SCRIPT;
argv[1] = event_type_strings[type];
chdir(fqn_prefix[DATAPREFIX]);
execvp(fqname(EVENT_SCRIPT, DATAPREFIX, DATAPREFIX), (char **) argv);
char *str = strerror(errno);
exit(errno == ENOENT ? EXIT_SUCCESS : EXIT_FAILURE); // l'impossible!
}

if( async ) return 0;

int child_status;
pid_t waitpid_ret = waitpid(pid, &child_status, 0);
if( waitpid_ret == -1 ) impossible("waitpid(): %s", strerror(errno));
// Undefined if the process exited due to a signal.
return WEXITSTATUS(child_status);
}

void event_trigger_async( EventType type, size_t args_len, char * const args[static args_len] ) {
event_trigger(type, args_len, args, true);
}
2 changes: 0 additions & 2 deletions libnethack/src/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ char bones[] = "bonesnn.xxx";
# endif
#endif

static const char *fqname(const char *, int, int);


void
display_file(const char *fname, boolean complain)
Expand Down
22 changes: 22 additions & 0 deletions libnethack/src/topten.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "patchlevel.h"
#include "quest.h"
#include "dlb.h"
#include "events.h"

#include <fcntl.h>

Expand All @@ -20,6 +21,7 @@
#define NAMSZ 15
#define DTHSZ 99

// If you modify this struct, remember to update emit_dead_event
struct toptenentry {
int points;
int deathdnum, deathlev;
Expand Down Expand Up @@ -428,6 +430,25 @@ toptenlist_insert(struct toptenentry *ttlist, struct toptenentry *newtt)
return TRUE;
}

static void
emit_dead_event(struct toptenentry *tt)
{
char *argv[9];
argv[0] = tt->name;
argv[1] = tt->plrole;
argv[2] = tt->plrace;
argv[3] = tt->plgend;
argv[4] = tt->plalign;
asprintf(&argv[5], "%d", tt->deathlev);
asprintf(&argv[6], "%d", tt->hp);
asprintf(&argv[7], "%d", tt->maxhp);
argv[8] = tt->death;
event_trigger_async(EVENT_TYPE_DEAD, 9, argv);
free(argv[5]);
free(argv[6]);
free(argv[7]);
}

/*
* Add the result of the current game to the score list
*/
Expand All @@ -446,6 +467,7 @@ update_topten(int how)
fill_topten_entry(&newtt, how);
update_log(&newtt);
update_xlog(&newtt);
emit_dead_event(&newtt);

/* nothing more to do for non-scoring games */
if (wizard || discover)
Expand Down

0 comments on commit 5dd1677

Please sign in to comment.