[ruamoko] Make cbuf functions take a cbuf param

A nil param refers to the default cbuf if it has been set by the engine,
otherwise it is invalid. This is the first step to fleshing out the cbuf
API.
This commit is contained in:
Bill Currie 2023-05-24 20:58:33 +09:00
parent b9cff7aae0
commit db9a6a9a5c
5 changed files with 49 additions and 45 deletions

View file

@ -38,7 +38,8 @@
#include "rua_internal.h" #include "rua_internal.h"
typedef struct { typedef struct {
cbuf_t *cbuf; progs_t *pr;
cbuf_t *default_cbuf;
} cbuf_resources_t; } cbuf_resources_t;
static cbuf_t * __attribute__((pure)) static cbuf_t * __attribute__((pure))
@ -47,44 +48,43 @@ _get_cbuf (progs_t *pr, cbuf_resources_t *res, int arg, const char *func)
cbuf_t *cbuf = 0; cbuf_t *cbuf = 0;
if (arg == 0) { if (arg == 0) {
cbuf = res->cbuf; // a nil cbuf is valid only if the default cbuf has been set
cbuf = res->default_cbuf;
} else { } else {
PR_RunError (pr, "%s: Invalid cbuf_t", func);
} }
if (!cbuf) if (!cbuf) {
PR_RunError (pr, "Invalid cbuf_t"); PR_RunError (pr, "%s: Invalid cbuf_t: %d", func, arg);
}
return cbuf; return cbuf;
} }
#define get_cbuf(pr, res, arg) _get_cbuf(pr, res, arg, __FUNCTION__) #define get_cbuf(pr, res, arg) _get_cbuf(pr, res, arg, __FUNCTION__)
static void #define bi(n) static void bi_##n (progs_t *pr, void *data)
bi_Cbuf_AddText (progs_t *pr, void *data)
bi(Cbuf_AddText)
{ {
const char *text = P_GSTRING (pr, 0); cbuf_t *cbuf = get_cbuf (pr, data, P_INT (pr, 0));
cbuf_t *cbuf = get_cbuf (pr, data, 0); const char *text = P_GSTRING (pr, 1);
Cbuf_AddText (cbuf, text); Cbuf_AddText (cbuf, text);
} }
static void bi(Cbuf_InsertText)
bi_Cbuf_InsertText (progs_t *pr, void *data)
{ {
const char *text = P_GSTRING (pr, 0); cbuf_t *cbuf = get_cbuf (pr, data, P_INT (pr, 0));
cbuf_t *cbuf = get_cbuf (pr, data, 0); const char *text = P_GSTRING (pr, 1);
Cbuf_InsertText (cbuf, text); Cbuf_InsertText (cbuf, text);
} }
static void bi(Cbuf_Execute)
bi_Cbuf_Execute (progs_t *pr, void *data)
{ {
cbuf_t *cbuf = get_cbuf (pr, data, 0); cbuf_t *cbuf = get_cbuf (pr, data, P_INT (pr, 0));
Cbuf_Execute (cbuf); Cbuf_Execute (cbuf);
} }
static void bi(Cbuf_Execute_Sets)
bi_Cbuf_Execute_Sets (progs_t *pr, void *data)
{ {
cbuf_t *cbuf = get_cbuf (pr, data, 0); cbuf_t *cbuf = get_cbuf (pr, data, P_INT (pr, 0));
Cbuf_Execute_Sets (cbuf); Cbuf_Execute_Sets (cbuf);
} }
@ -99,14 +99,15 @@ bi_cbuf_destroy (progs_t *pr, void *data)
free (data); free (data);
} }
#undef bi
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}} #define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
#define p(type) PR_PARAM(type) #define p(type) PR_PARAM(type)
#define P(a, s) { .size = (s), .alignment = BITOP_LOG2 (a), } #define P(a, s) { .size = (s), .alignment = BITOP_LOG2 (a), }
static builtin_t builtins[] = { static builtin_t builtins[] = {
bi(Cbuf_AddText, 1, p(string)), bi(Cbuf_AddText, 2, p(ptr), p(string)),
bi(Cbuf_InsertText, 1, p(string)), bi(Cbuf_InsertText, 2, p(ptr), p(string)),
bi(Cbuf_Execute, 0), bi(Cbuf_Execute, 1, p(ptr)),
bi(Cbuf_Execute_Sets, 0), bi(Cbuf_Execute_Sets, 1, p(ptr)),
{0} {0}
}; };
@ -114,6 +115,7 @@ void
RUA_Cbuf_Init (progs_t *pr, int secure) RUA_Cbuf_Init (progs_t *pr, int secure)
{ {
cbuf_resources_t *res = calloc (sizeof (cbuf_resources_t), 1); cbuf_resources_t *res = calloc (sizeof (cbuf_resources_t), 1);
res->pr = pr;
PR_Resources_Register (pr, "Cbuf", res, bi_cbuf_clear, bi_cbuf_destroy); PR_Resources_Register (pr, "Cbuf", res, bi_cbuf_clear, bi_cbuf_destroy);
PR_RegisterBuiltins (pr, builtins, res); PR_RegisterBuiltins (pr, builtins, res);
} }
@ -122,5 +124,5 @@ VISIBLE void
RUA_Cbuf_SetCbuf (progs_t *pr, cbuf_t *cbuf) RUA_Cbuf_SetCbuf (progs_t *pr, cbuf_t *cbuf)
{ {
cbuf_resources_t *res = PR_Resources_Find (pr, "Cbuf"); cbuf_resources_t *res = PR_Resources_Find (pr, "Cbuf");
res->cbuf = cbuf; res->default_cbuf = cbuf;
} }

View file

@ -247,7 +247,7 @@ int (int key, int unicode, int down) load_quickbup_keyevent =
if (loadable[load_cursor]) { if (loadable[load_cursor]) {
S_LocalSound ("misc/menu2.wav"); S_LocalSound ("misc/menu2.wav");
Menu_SelectMenu (nil); Menu_SelectMenu (nil);
Cbuf_AddText (sprintf ("load quick%i.sav\n", load_cursor)); Cbuf_AddText (nil, sprintf ("load quick%i.sav\n", load_cursor));
load_cursor = MAX_SAVEGAMES; load_cursor = MAX_SAVEGAMES;
} }
return 1; return 1;
@ -279,7 +279,7 @@ int (int key, int unicode, int down) load_keyevent =
} else if (loadable[load_cursor]) { } else if (loadable[load_cursor]) {
S_LocalSound ("misc/menu2.wav"); S_LocalSound ("misc/menu2.wav");
Menu_SelectMenu (nil); Menu_SelectMenu (nil);
Cbuf_AddText (sprintf ("load s%i.sav\n", load_cursor)); Cbuf_AddText (nil, sprintf ("load s%i.sav\n", load_cursor));
} }
return 1; return 1;
} }
@ -304,7 +304,7 @@ int (int key, int unicode, int down) save_keyevent =
case QFK_RETURN: case QFK_RETURN:
//case QFM_BUTTON1: //case QFM_BUTTON1:
Menu_SelectMenu (nil); Menu_SelectMenu (nil);
Cbuf_AddText (sprintf ("save s%i.sav\n", save_cursor)); Cbuf_AddText (nil, sprintf ("save s%i.sav\n", save_cursor));
return 1; return 1;
} }
return 0; return 0;
@ -398,15 +398,15 @@ void () quit_menu =
int (string text, int key) sp_start = int (string text, int key) sp_start =
{ {
Menu_SelectMenu (nil); Menu_SelectMenu (nil);
Cbuf_AddText ("disconnect\n"); Cbuf_AddText (nil, "disconnect\n");
Cbuf_AddText ("maxplayers 1\n"); Cbuf_AddText (nil, "maxplayers 1\n");
Cbuf_AddText ("coop 0\n"); Cbuf_AddText (nil, "coop 0\n");
Cbuf_AddText ("deathmatch 0\n"); Cbuf_AddText (nil, "deathmatch 0\n");
Cbuf_AddText ("teamplay 0\n"); Cbuf_AddText (nil, "teamplay 0\n");
Cbuf_AddText ("listen 0\n"); Cbuf_AddText (nil, "listen 0\n");
Cbuf_AddText ("noexit 0\n"); Cbuf_AddText (nil, "noexit 0\n");
Cbuf_AddText ("samelevel 0\n"); Cbuf_AddText (nil, "samelevel 0\n");
Cbuf_AddText ("map start\n"); Cbuf_AddText (nil, "map start\n");
return 0; return 0;
}; };

View file

@ -436,7 +436,7 @@ int (string text, int key)
op_goto_console = op_goto_console =
{ {
Menu_SelectMenu (""); Menu_SelectMenu ("");
Cbuf_AddText ("toggleconsole\n"); Cbuf_AddText (nil, "toggleconsole\n");
return 0; return 0;
}; };

View file

@ -1,9 +1,11 @@
#ifndef __ruamoko_cbuf_h #ifndef __ruamoko_cbuf_h
#define __ruamoko_cbuf_h #define __ruamoko_cbuf_h
@extern void Cbuf_AddText (string text); typedef struct { int x; } cbuf_t;
@extern void Cbuf_InsertText (string text);
@extern void Cbuf_Execute (void); @extern void Cbuf_AddText (cbuf_t *cbuf, string text);
@extern void Cbuf_Execute_Sets (void); @extern void Cbuf_InsertText (cbuf_t *cbuf, string text);
@extern void Cbuf_Execute (cbuf_t *cbuf);
@extern void Cbuf_Execute_Sets (cbuf_t *cbuf);
#endif//__ruamoko_cbuf_h #endif//__ruamoko_cbuf_h

View file

@ -1,6 +1,6 @@
#include <cbuf.h> #include <cbuf.h>
void (string text) Cbuf_AddText = #0; void Cbuf_AddText (cbuf_t *cbuf, string text) = #0;
void (string text) Cbuf_InsertText = #0; void Cbuf_InsertText (cbuf_t *cbuf, string text) = #0;
void () Cbuf_Execute = #0; void Cbuf_Execute (cbuf_t *cbuf) = #0;
void () Cbuf_Execute_Sets = #0; void Cbuf_Execute_Sets (cbuf_t *cbuf) = #0;