- SW script parser cleanup

This commit is contained in:
Christoph Oelckers 2020-08-05 16:40:07 +02:00
parent a3dfa58662
commit 4fef66c78a
8 changed files with 47 additions and 408 deletions

View file

@ -926,12 +926,6 @@ void bfirst_search_try(T *const list, uint8_t *const bitmap, T *const eltnumptr,
}
}
#if RAND_MAX == 32767
static FORCE_INLINE uint16_t system_15bit_rand(void) { return (uint16_t)rand(); }
#else // RAND_MAX > 32767, assumed to be of the form 2^k - 1
static FORCE_INLINE uint16_t system_15bit_rand(void) { return ((uint16_t)rand())&0x7fff; }
#endif
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
static FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
{
@ -940,47 +934,9 @@ static FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
return dst;
}
// Append extension when <outbuf> contains no dot.
// <ext> can be like ".mhk" or like "_crash.map", no need to start with a dot.
// The ugly name is deliberate: we should be checking the sizes of all buffers!
static inline void append_ext_UNSAFE(char *outbuf, const char *ext)
{
char *p = Bstrrchr(outbuf,'.');
if (!p)
Bstrcat(outbuf, ext);
else
Bstrcpy(p, ext);
}
////////// Paths //////////
////////// String manipulation //////////
inline char* Bstrtolower(char* str)
{
if (str) for (int i = 0; str[i]; i++) str[i] = tolower(str[i]);
return str;
}
////////// Miscellaneous //////////
int Bgetpagesize(void);
uint32_t Bgetsysmemsize(void);
////////// PANICKING ALLOCATION WRAPPERS //////////
#ifdef DEBUGGINGAIDS
extern void xalloc_set_location(int32_t line, const char *file, const char *func);
#endif
void set_memerr_handler(void (*handlerfunc)(int32_t, const char *, const char *));
void *handle_memerr(void *);
// This is for allowing the compiler's heap checker to do its job. When wrapped it only points to the wrapper for a memory leak, not to the real location where the allocation takes place.
#define Xstrdup(s) (strdup(s))
#define Xmalloc(size) (M_Malloc(size))
#define Xcalloc(nmemb, size) (M_Calloc(nmemb, size))
@ -992,12 +948,6 @@ void *handle_memerr(void *);
////////// Inlined external libraries //////////
#ifndef LIBDIVIDE_BODY
# define LIBDIVIDE_HEADER_ONLY
#endif
#define LIBDIVIDE_C_HEADERS
#define LIBDIVIDE_NONAMESPACE
#define LIBDIVIDE_NOINLINE
#include "fix16.h"
#include "vectors.h"
using ClockTicks = int;

View file

@ -366,169 +366,6 @@ int krand1(void)
#endif
#if DEBUG
SWBOOL
ValidPtr(void *ptr)
{
MEM_HDRp mhp;
uint8_t* check;
ASSERT(ptr != NULL);
mhp = (MEM_HDRp)(((uint8_t*) ptr) - sizeof(MEM_HDR));
if (mhp->size == 0 || mhp->checksum == 0)
{
printf("ValidPtr(): Size or Checksum == 0!\n");
return FALSE;
}
check = (uint8_t*) & mhp->size;
if (mhp->checksum == check[0] + check[1] + check[2] + check[3])
return TRUE;
printf("ValidPtr(): Checksum bad!\n");
return FALSE;
}
void
PtrCheckSum(void *ptr, unsigned int *stored, unsigned int *actual)
{
MEM_HDRp mhp;
uint8_t* check;
ASSERT(ptr != NULL);
mhp = (MEM_HDRp)(((uint8_t*) ptr) - sizeof(MEM_HDR));
check = (uint8_t*) & mhp->size;
*stored = mhp->checksum;
*actual = check[0] + check[1] + check[2] + check[3];
}
void *
AllocMem(int size)
{
uint8_t* bp;
MEM_HDRp mhp;
uint8_t* check;
ASSERT(size != 0);
bp = (uint8_t*) malloc(size + sizeof(MEM_HDR));
// Used for debugging, we can remove this at ship time
if (bp == NULL)
{
I_FatalError("Memory could NOT be allocated in AllocMem: size = %d\n",size);
}
ASSERT(bp != NULL);
mhp = (MEM_HDRp) bp;
mhp->size = size;
check = (uint8_t*) & mhp->size;
mhp->checksum = check[0] + check[1] + check[2] + check[3];
bp += sizeof(MEM_HDR);
return bp;
}
void *
ReAllocMem(void *ptr, int size)
{
if (ptr == nullptr)
return AllocMem(size);
if (size == 0)
{
FreeMem(ptr);
return nullptr;
}
uint8_t* bp;
MEM_HDRp mhp;
uint8_t* check;
ASSERT(ValidPtr(ptr));
mhp = (MEM_HDRp)(((uint8_t*) ptr) - sizeof(MEM_HDR));
bp = (uint8_t*) realloc(mhp, size + sizeof(MEM_HDR));
ASSERT(bp != NULL);
mhp = (MEM_HDRp) bp;
mhp->size = size;
check = (uint8_t*) & mhp->size;
mhp->checksum = check[0] + check[1] + check[2] + check[3];
bp += sizeof(MEM_HDR);
ASSERT(ValidPtr(bp));
return bp;
}
void *
CallocMem(int size, int num)
{
uint8_t* bp;
MEM_HDRp mhp;
uint8_t* check;
int num_bytes;
ASSERT(size != 0 && num != 0);
num_bytes = (size * num) + sizeof(MEM_HDR);
bp = (uint8_t*) calloc(num_bytes, 1);
// Used for debugging, we can remove this at ship time
if (bp == NULL)
{
I_FatalError("Memory could NOT be allocated in CallocMem: size = %d, num = %d\n",size,num);
}
ASSERT(bp != NULL);
mhp = (MEM_HDRp) bp;
mhp->size = size;
check = (uint8_t*) & mhp->size;
mhp->checksum = check[0] + check[1] + check[2] + check[3];
bp += sizeof(MEM_HDR);
return bp;
}
void
FreeMem(void *ptr)
{
if (ptr == nullptr)
return;
MEM_HDRp mhp;
uint8_t* check;
ASSERT(ValidPtr(ptr));
mhp = (MEM_HDRp)(((uint8_t*) ptr) - sizeof(MEM_HDR));
check = (uint8_t*)&mhp->size;
memset(mhp, 0xCC, mhp->size + sizeof(MEM_HDR));
free(mhp);
}
#endif
int PointOnLine(int x, int y, int x1, int y1, int x2, int y2)
{
// the closer to 0 the closer to the line the point is

View file

@ -1858,19 +1858,8 @@ typedef struct
unsigned int size, checksum;
} MEM_HDR,*MEM_HDRp;
#if !DEBUG
# define ValidPtr(ptr) ((SWBOOL)(TRUE))
# define AllocMem(size) Xmalloc(size)
# define CallocMem(size, num) Xcalloc(size, num)
# define ReAllocMem(ptr, size) Xrealloc(ptr, size)
# define FreeMem(ptr) Xfree(ptr)
#else
SWBOOL ValidPtr(void *ptr);
void *AllocMem(int size);
void *CallocMem(int size, int num);
void *ReAllocMem(void *ptr, int size);
void FreeMem(void *ptr);
#endif
# define CallocMem(size, num) M_Calloc(size, num)
# define FreeMem(ptr) M_Free(ptr)
typedef struct
{

View file

@ -1125,7 +1125,6 @@ void
InsertOrgTile(OrgTileP tp, OrgTileListP thelist)
{
ASSERT(tp);
ASSERT(ValidPtr(tp));
// if list is empty, insert at front
if (EMPTY(thelist))
@ -1159,7 +1158,6 @@ void
KillOrgTile(OrgTileP tp)
{
ASSERT(tp);
ASSERT(ValidPtr(tp));
REMOVE(tp);

View file

@ -7128,7 +7128,6 @@ InsertPanelSprite(PLAYERp pp, PANEL_SPRITEp psp)
PANEL_SPRITEp cur, nxt;
ASSERT(psp);
ASSERT(ValidPtr(psp));
// if list is empty, insert at front
if (EMPTY(&pp->PanelSpriteList))
@ -7218,7 +7217,6 @@ void
pKillSprite(PANEL_SPRITEp psp)
{
PRODUCTION_ASSERT(psp);
ASSERT(ValidPtr(psp));
REMOVE(psp);
@ -7320,7 +7318,6 @@ pDisplaySprites(PLAYERp pp)
set.clear();
TRAVERSE(&pp->PanelSpriteList, psp, next)
{
ASSERT(ValidPtr(psp));
ang = psp->rotate_ang;
shade = 0;
flags = 0;
@ -7646,8 +7643,6 @@ void pFlushPerms(PLAYERp pp)
TRAVERSE(&pp->PanelSpriteList, psp, next)
{
ASSERT(ValidPtr(psp));
// force kill before showing again
if (TEST(psp->flags, PANF_KILL_AFTER_SHOW))
{
@ -7667,8 +7662,6 @@ pSpriteControl(PLAYERp pp)
// somewhere else other than by themselves
// RULE: Sprites can only kill themselves
PRODUCTION_ASSERT(psp);
ASSERT(ValidPtr(psp));
// ASSERT((uint32_t) psp->Next != 0xCCCCCCCC);
if (psp->State)
pStateControl(psp);
@ -7686,7 +7679,6 @@ void
pSetState(PANEL_SPRITEp psp, PANEL_STATEp panel_state)
{
PRODUCTION_ASSERT(psp);
ASSERT(ValidPtr(psp));
psp->tics = 0;
psp->State = panel_state;
psp->picndx = panel_state ? panel_state->picndx : 0;

View file

@ -1,51 +0,0 @@
//-------------------------------------------------------------------------
/*
Copyright (C) 1997, 2005 - 3D Realms Entertainment
This file is part of Shadow Warrior version 1.2
Shadow Warrior is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Original Source: 1997 - Frank Maddin and Jim Norwood
Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------
// scriplib.h
BEGIN_SW_NS
#define MAXTOKEN 255
extern char token[MAXTOKEN];
extern char *scriptbuffer,*script_p,*scriptend_p;
extern int grabbed;
extern int scriptline;
extern SWBOOL endofscript;
SWBOOL LoadScriptFile(const char *filename);
void GetToken(SWBOOL crossline);
void UnGetToken(void);
SWBOOL TokenAvailable(void);
void DefaultExtension(char *path, char *extension);
void DefaultPath(char *path, char *basepath);
void StripFilename(char *path);
void ExtractFileBase(char *path, char *dest);
int ParseNum(char *str);
END_SW_NS

View file

@ -34,7 +34,6 @@ Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
#include "panel.h"
#include "game.h"
#include "parse.h"
#include "sprite.h"
#include "jsector.h"
#include "parent.h"
@ -45,10 +44,6 @@ Prepared for public release: 03/28/2005 - Charlie Wiederhold, 3D Realms
BEGIN_SW_NS
#define PATHSEPERATOR '\\'
//#define COMPUTE_TOTALS 1
ParentalStruct aVoxelArray[MAXTILES];
@ -59,13 +54,14 @@ ParentalStruct aVoxelArray[MAXTILES];
=============================================================================
*/
#define MAXTOKEN 255
char token[MAXTOKEN];
char *scriptbuffer,*script_p,*scriptend_p;
int grabbed;
int scriptline;
SWBOOL endofscript;
SWBOOL tokenready; // only TRUE if UnGetToken was just called
static char* script_p, * scriptend_p;
static char token[MAXTOKEN];
static int grabbed;
static int scriptline;
static SWBOOL endofscript;
static SWBOOL tokenready; // only TRUE if UnGetToken was just called
/*
==============
@ -75,60 +71,26 @@ SWBOOL tokenready; // only TRUE if UnGetToken was just ca
==============
*/
SWBOOL LoadScriptFile(const char *filename)
TArray<uint8_t> LoadScriptFile(const char *filename)
{
size_t size, readsize;
FileReader fp;
if (!(fp = fileSystem.OpenFileReader(filename)).isOpen())
{
// If there's no script file, forget it.
return FALSE;
return TArray<uint8_t>();
}
size = fp.GetLength();
auto scriptbuffer = fp.Read();
scriptbuffer = (char *)AllocMem(size+1);
ASSERT(scriptbuffer != NULL);
readsize = fp.Read(scriptbuffer, size);
ASSERT(readsize == size);
scriptbuffer[readsize] = '\0';
script_p = scriptbuffer;
scriptend_p = script_p + size;
scriptline = 1;
endofscript = FALSE;
tokenready = FALSE;
return TRUE;
}
/*
==============
=
= UnGetToken
=
= Signals that the current token was not used, and should be reported
= for the next GetToken. Note that
GetToken (TRUE);
UnGetToken ();
GetToken (FALSE);
= could cross a line boundary.
=
==============
*/
void UnGetToken(void)
{
tokenready = TRUE;
if (scriptbuffer.Size() != 0)
{
scriptbuffer.Push(0);
scriptline = 1;
endofscript = FALSE;
tokenready = FALSE;
}
return scriptbuffer;
}
@ -182,15 +144,15 @@ skipspace:
if (script_p >= scriptend_p)
{
if (!crossline)
Printf("Error: Line %i is incomplete\n",scriptline);
endofscript = TRUE;
return;
Printf("Error: Line %i is incomplete\n", scriptline);
endofscript = TRUE;
return;
}
if (*script_p == '#') // # is comment field
{
if (!crossline)
Printf("Error: Line %i is incomplete\n",scriptline);
Printf("Error: Line %i is incomplete\n", scriptline);
while (*script_p++ != '\n')
if (script_p >= scriptend_p)
{
@ -200,9 +162,9 @@ skipspace:
goto skipspace;
}
//
// copy token
//
//
// copy token
//
token_p = token;
while (*script_p > 32 && *script_p != '#')
@ -211,47 +173,13 @@ skipspace:
if (script_p == scriptend_p)
break;
ASSERT(token_p != &token[MAXTOKEN]);
// Printf("Error: Token too large on line %i\n",scriptline);
// Printf("Error: Token too large on line %i\n",scriptline);
}
*token_p = 0;
}
/*
==============
=
= TokenAvailable
=
= Returns true if there is another token on the line
=
==============
*/
SWBOOL TokenAvailable(void)
{
char *search_p;
search_p = script_p;
if (search_p >= scriptend_p)
return FALSE;
while (*search_p <= 32)
{
if (*search_p == '\n')
return FALSE;
search_p++;
if (search_p == scriptend_p)
return FALSE;
}
if (*search_p == '#')
return FALSE;
return TRUE;
}
// Load all the voxel files using swvoxfil.txt script file
@ -263,19 +191,15 @@ SWBOOL TokenAvailable(void)
// 1804 1 shotgun.kvx
// etc....
void LoadKVXFromScript(const char *filename)
void LoadKVXFromScript(const char* filename)
{
int lNumber=0,lTile=0; // lNumber is the voxel no. and lTile is the editart tile being
int lNumber = 0, lTile = 0; // lNumber is the voxel no. and lTile is the editart tile being
// replaced.
char *sName; // KVS file being loaded in.
int grabbed=0; // Number of lines parsed
sName = (char *)AllocMem(256); // Up to 256 bytes for path
ASSERT(sName != NULL);
int grabbed = 0; // Number of lines parsed
// zero out the array memory with -1's for pics not being voxelized
memset(&aVoxelArray[0],-1,sizeof(struct TILE_INFO_TYPE)*MAXTILES);
memset(&aVoxelArray[0], -1, sizeof(struct TILE_INFO_TYPE) * MAXTILES);
for (grabbed = 0; grabbed < MAXTILES; grabbed++)
{
aVoxelArray[grabbed].Voxel = -1;
@ -285,8 +209,13 @@ void LoadKVXFromScript(const char *filename)
grabbed = 0;
// Load the file
if (!LoadScriptFile(filename))
ASSERT(TRUE==FALSE);
auto buffer = LoadScriptFile(filename);
if (!buffer.Size())
{
return;
}
script_p = (char*)buffer.Data();
scriptend_p = (char*)&buffer.Last();
do
{
@ -301,10 +230,9 @@ void LoadKVXFromScript(const char *filename)
lNumber = atol(token);
GetToken(FALSE);
strcpy(sName,token); // Copy the whole token as a file name and path
// Load the voxel file into memory
if (!qloadkvx(lNumber,sName))
if (!qloadkvx(lNumber,token))
{
// Store the sprite and voxel numbers for later use
aVoxelArray[lTile].Voxel = lNumber; // Voxel num
@ -319,8 +247,6 @@ void LoadKVXFromScript(const char *filename)
}
while (script_p < scriptend_p);
FreeMem(scriptbuffer);
FreeMem(sName);
script_p = NULL;
}
@ -334,16 +260,17 @@ void LoadPLockFromScript(const char *filename)
{
int lNumber=0,lTile=0; // lNumber is the voxel no. and lTile is the editart tile being
// replaced.
char *sName; // KVS file being loaded in.
int grabbed=0; // Number of lines parsed
sName = (char *)AllocMem(256); // Up to 256 bytes for path
ASSERT(sName != NULL);
// Load the file
if (!LoadScriptFile(filename))
ASSERT(TRUE==FALSE);
auto buffer = LoadScriptFile(filename);
if (!buffer.Size())
{
return;
}
script_p = (char*)buffer.Data();
scriptend_p = (char*)&buffer.Last();
do
{
@ -366,8 +293,6 @@ void LoadPLockFromScript(const char *filename)
}
while (script_p < scriptend_p);
FreeMem(scriptbuffer);
FreeMem(sName);
script_p = NULL;
}

View file

@ -6711,7 +6711,6 @@ StateControl(int16_t SpriteNum)
if (u)
{
ASSERT(u->State);
ASSERT(ValidPtr(u));
// Set picnum to the correct pic
if (TEST(u->State->Tics, SF_WALL_STATE))
{