Skip to content

Commit

Permalink
New feature: poll external file for console commands
Browse files Browse the repository at this point in the history
The commit adds "-refreshfile" and "-refreshinterval" command-line
arguments. Every refreshinterval tics (or once a second if not
specified) a refreshfile is read and is evaluated if exists and not
empty. Then it is truncated.

This feature provides a facility for an external program to control
the game.
  • Loading branch information
kmeaw committed Feb 2, 2022
1 parent 751f859 commit ad834bf
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/common/console/c_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "g_input.h"
#include "c_commandbuffer.h"
#include "vm.h"
#include "m_argv.h"

#define LEFTMARGIN 8
#define RIGHTMARGIN 8
Expand Down Expand Up @@ -110,6 +111,9 @@ static int TopLine, InsertLine;

static void ClearConsole ();

static const char *RefreshFile = NULL;
static int RefreshInterval = 0;

struct GameAtExit
{
GameAtExit(FString str) : Command(str) {}
Expand Down Expand Up @@ -235,6 +239,7 @@ void C_InitConback(FTextureID fallback, bool tile, double brightness)
void C_InitConsole (int width, int height, bool ingame)
{
int cwidth, cheight;
const char *v;

vidactive = ingame;
if (CurrentConsoleFont != NULL)
Expand All @@ -250,6 +255,18 @@ void C_InitConsole (int width, int height, bool ingame)
CmdLine.ConCols = ConWidth / cwidth;

if (conbuffer == NULL) conbuffer = new FConsoleBuffer;

v = Args->CheckValue ("-refreshfile");
if (v != NULL)
{
RefreshFile = v;
RefreshInterval = TICRATE;
v = Args->CheckValue ("-refreshinterval");
if (v != NULL && (atoi(v) != 0))
{
RefreshInterval = atoi(v);
}
}
}

//==========================================================================
Expand Down Expand Up @@ -534,9 +551,45 @@ void C_NewModeAdjust ()
int consoletic = 0;
void C_Ticker()
{
FILE *refresh;
static char refresh_cmd[PATH_MAX + 6];
static int lasttic = 0;
long filesize = 1;
consoletic++;

if (RefreshInterval > 0 && (consoletic % RefreshInterval) == 0)
{
refresh = fopen (RefreshFile, "r");
if (refresh != NULL)
{
if (fseek (refresh, 0, SEEK_END) == 0)
{
filesize = ftell (refresh);
}
else
{
filesize = 1;
}
fclose (refresh);

if (!strlen (refresh_cmd)) {
strcpy (refresh_cmd, "exec ");
strcat (refresh_cmd, RefreshFile);
}

if (filesize > 0)
{
C_DoCommand (refresh_cmd);

// Truncate the file
refresh = fopen (RefreshFile, "w");
if (refresh != NULL) {
fclose (refresh);
}
}
}
}

if (lasttic == 0)
lasttic = consoletic - 1;

Expand Down

0 comments on commit ad834bf

Please sign in to comment.