- avoid using plain char pointers to store script data for FraggleScript.

This commit is contained in:
Christoph Oelckers 2020-04-11 12:12:22 +02:00
parent 5865c4dcca
commit 3db56651f4
11 changed files with 37 additions and 44 deletions

View File

@ -71,12 +71,12 @@ enum
//Info given to bots when they're spawned. //Info given to bots when they're spawned.
struct botinfo_t struct botinfo_t
{ {
botinfo_t *next; botinfo_t *next = nullptr;
FString Name; FString Name;
FString Info; FString Info;
botskill_t skill; botskill_t skill = {};
int inuse; int inuse = 0;
int lastteam; int lastteam = 0;
}; };
struct BotInfoData struct BotInfoData

View File

@ -500,8 +500,6 @@ bool FCajunMaster::LoadBots ()
botinfo_t *newinfo = new botinfo_t; botinfo_t *newinfo = new botinfo_t;
bool gotclass = false; bool gotclass = false;
memset (newinfo, 0, sizeof(*newinfo));
newinfo->Info = "\\autoaim\\0\\movebob\\.25"; newinfo->Info = "\\autoaim\\0\\movebob\\.25";
for (;;) for (;;)

View File

@ -264,7 +264,8 @@ bool FScriptLoader::ParseInfo(MapData * map)
} }
auto th = Level->CreateThinker<DFraggleThinker>(); auto th = Level->CreateThinker<DFraggleThinker>();
th->LevelScript->data = copystring(scriptsrc.GetChars()); th->LevelScript->Data.Resize((unsigned)scriptsrc.Len() + 1);
memcpy(th->LevelScript->Data.Data(), scriptsrc.GetChars(), scriptsrc.Len() + 1);
Level->FraggleScriptThinker = th; Level->FraggleScriptThinker = th;
if (drownflag==-1) drownflag = (Level->maptype != MAPTYPE_DOOM || fsglobal); if (drownflag==-1) drownflag = (Level->maptype != MAPTYPE_DOOM || fsglobal);

View File

@ -709,10 +709,10 @@ void FParser::ErrorMessage(FString msg)
int linenum = 0; int linenum = 0;
// find the line number // find the line number
if(Rover >= Script->data && Rover <= Script->data+Script->len) if(Rover >= Script->Data.Data() && Rover <= Script->Data.Data() +Script->len)
{ {
char *temp; char *temp;
for(temp = Script->data; temp<LineStart; temp++) for(temp = Script->Data.Data(); temp<LineStart; temp++)
if(*temp == '\n') linenum++; // count EOLs if(*temp == '\n') linenum++; // count EOLs
} }

View File

@ -88,7 +88,7 @@ void DFsSection::Serialize(FSerializer &arc)
char *DFsScript::SectionStart(const DFsSection *sec) char *DFsScript::SectionStart(const DFsSection *sec)
{ {
return data + sec->start_index; return Data.Data() + sec->start_index;
} }
//========================================================================== //==========================================================================
@ -99,7 +99,7 @@ char *DFsScript::SectionStart(const DFsSection *sec)
char *DFsScript::SectionEnd(const DFsSection *sec) char *DFsScript::SectionEnd(const DFsSection *sec)
{ {
return data + sec->end_index; return Data.Data() + sec->end_index;
} }
//========================================================================== //==========================================================================
@ -110,7 +110,7 @@ char *DFsScript::SectionEnd(const DFsSection *sec)
char *DFsScript::SectionLoop(const DFsSection *sec) char *DFsScript::SectionLoop(const DFsSection *sec)
{ {
return data + sec->loop_index; return Data.Data() + sec->loop_index;
} }
//========================================================================== //==========================================================================
@ -335,8 +335,8 @@ char *DFsScript::ProcessFindChar(char *datap, char find)
void DFsScript::DryRunScript(FLevelLocals *Level) void DFsScript::DryRunScript(FLevelLocals *Level)
{ {
char *end = data + len; char *end = Data.Data() + len;
char *rover = data; char *rover = Data.Data();
// allocate space for the tokens // allocate space for the tokens
FParser parse(Level, this); FParser parse(Level, this);
@ -389,8 +389,8 @@ void DFsScript::DryRunScript(FLevelLocals *Level)
void DFsScript::Preprocess(FLevelLocals *Level) void DFsScript::Preprocess(FLevelLocals *Level)
{ {
len = (int)strlen(data); len = (int)Data.Size() - 1;
ProcessFindChar(data, 0); // fill in everything ProcessFindChar(Data.Data(), 0); // fill in everything
DryRunScript(Level); DryRunScript(Level);
} }

View File

@ -127,7 +127,6 @@ DFsScript::DFsScript()
for(i=0; i<VARIABLESLOTS; i++) variables[i] = nullptr; for(i=0; i<VARIABLESLOTS; i++) variables[i] = nullptr;
for(i=0; i<MAXSCRIPTS; i++) children[i] = nullptr; for(i=0; i<MAXSCRIPTS; i++) children[i] = nullptr;
data = nullptr;
scriptnum = -1; scriptnum = -1;
len = 0; len = 0;
parent = nullptr; parent = nullptr;
@ -135,19 +134,6 @@ DFsScript::DFsScript()
lastiftrue = false; lastiftrue = false;
} }
//==========================================================================
//
// This is here to delete the locally allocated buffer in case this
// gets forcibly destroyed
//
//==========================================================================
DFsScript::~DFsScript()
{
if (data != nullptr) delete[] data;
data = nullptr;
}
//========================================================================== //==========================================================================
// //
// //
@ -160,8 +146,7 @@ void DFsScript::OnDestroy()
ClearSections(); ClearSections();
ClearChildren(); ClearChildren();
parent = nullptr; parent = nullptr;
if (data != nullptr) delete [] data; Data.Reset(); // lose the buffer now and don't wait until getting collected.
data = nullptr;
parent = nullptr; parent = nullptr;
trigger = nullptr; trigger = nullptr;
Super::OnDestroy(); Super::OnDestroy();
@ -177,7 +162,7 @@ void DFsScript::Serialize(FSerializer &arc)
{ {
Super::Serialize(arc); Super::Serialize(arc);
arc("data", data) arc("data", Data)
("scriptnum", scriptnum) ("scriptnum", scriptnum)
("len", len) ("len", len)
("parent", parent) ("parent", parent)
@ -201,11 +186,11 @@ void DFsScript::ParseScript(char *position, DFraggleThinker *th)
if (position == nullptr) if (position == nullptr)
{ {
lastiftrue = false; lastiftrue = false;
position = data; position = Data.Data();
} }
// check for valid position // check for valid position
if(position < data || position > data+len) if(position < Data.Data() || position > Data.Data() +len)
{ {
Printf("script %d: trying to continue from point outside script!\n", scriptnum); Printf("script %d: trying to continue from point outside script!\n", scriptnum);
return; return;
@ -216,7 +201,7 @@ void DFsScript::ParseScript(char *position, DFraggleThinker *th)
try try
{ {
FParser parse(th->Level, this); FParser parse(th->Level, this);
parse.Run(position, data, data + len); parse.Run(position, Data.Data(), Data.Data() + len);
} }
catch (CFraggleScriptError &err) catch (CFraggleScriptError &err)
{ {
@ -528,7 +513,7 @@ void DFraggleThinker::Tick()
next = current->next; // save before freeing next = current->next; // save before freeing
// continue the script // continue the script
current->script->ParseScript (current->script->data + current->save_point, this); current->script->ParseScript (current->script->Data.Data() + current->save_point, this);
// free // free
current->Destroy(); current->Destroy();

View File

@ -303,7 +303,7 @@ class DFsScript : public DObject
public: public:
// script data // script data
char *data; TArray<char> Data;
int scriptnum; // this script's number int scriptnum; // this script's number
int len; int len;
@ -335,7 +335,6 @@ public:
// true or false // true or false
DFsScript(); DFsScript();
~DFsScript();
void OnDestroy() override; void OnDestroy() override;
void Serialize(FSerializer &ar); void Serialize(FSerializer &ar);
@ -354,7 +353,7 @@ public:
void ClearSections(); void ClearSections();
void ClearChildren(); void ClearChildren();
int MakeIndex(const char *p) { return int(p-data); } int MakeIndex(const char *p) { return int(p-Data.Data()); }
// preprocessor // preprocessor
int section_hash(const char *b) { return MakeIndex(b) % SECTIONSLOTS; } int section_hash(const char *b) { return MakeIndex(b) % SECTIONSLOTS; }

View File

@ -416,14 +416,14 @@ void FParser::spec_script()
datasize = (Section->end_index - Section->start_index - 2); datasize = (Section->end_index - Section->start_index - 2);
// alloc extra 10 for safety // alloc extra 10 for safety
newscript->data = (char *)malloc(datasize+10); newscript->Data.Resize(datasize+1);
// copy from parent newscript (levelscript) // copy from parent newscript (levelscript)
// ignore first char which is { // ignore first char which is {
memcpy(newscript->data, Script->SectionStart(Section) + 1, datasize); memcpy(newscript->Data.Data(), Script->SectionStart(Section) + 1, datasize);
// tack on a 0 to end the string // tack on a 0 to end the string
newscript->data[datasize] = '\0'; newscript->Data.Data()[datasize] = '\0';
newscript->scriptnum = scriptnum; newscript->scriptnum = scriptnum;
newscript->parent = Script; // remember parent newscript->parent = Script; // remember parent

View File

@ -428,7 +428,7 @@ void DFsScript::ClearVariables(bool complete)
char *DFsScript::LabelValue(const svalue_t &v) char *DFsScript::LabelValue(const svalue_t &v)
{ {
if (v.type == svt_label) return data + v.value.i; if (v.type == svt_label) return Data.Data() + v.value.i;
else return NULL; else return NULL;
} }

View File

@ -1281,6 +1281,15 @@ FSerializer &Serialize(FSerializer &arc, const char *key, uint32_t &value, uint3
// //
//========================================================================== //==========================================================================
FSerializer& Serialize(FSerializer& arc, const char* key, char& value, char* defval)
{
int32_t vv = value;
int32_t vvd = defval ? *defval : value - 1;
Serialize(arc, key, vv, &vvd);
value = (int8_t)vv;
return arc;
}
FSerializer &Serialize(FSerializer &arc, const char *key, int8_t &value, int8_t *defval) FSerializer &Serialize(FSerializer &arc, const char *key, int8_t &value, int8_t *defval)
{ {
int32_t vv = value; int32_t vv = value;

View File

@ -186,6 +186,7 @@ public:
int mErrors = 0; int mErrors = 0;
}; };
FSerializer& Serialize(FSerializer& arc, const char* key, char& value, char* defval);
FSerializer &Serialize(FSerializer &arc, const char *key, bool &value, bool *defval); FSerializer &Serialize(FSerializer &arc, const char *key, bool &value, bool *defval);
FSerializer &Serialize(FSerializer &arc, const char *key, int64_t &value, int64_t *defval); FSerializer &Serialize(FSerializer &arc, const char *key, int64_t &value, int64_t *defval);
FSerializer &Serialize(FSerializer &arc, const char *key, uint64_t &value, uint64_t *defval); FSerializer &Serialize(FSerializer &arc, const char *key, uint64_t &value, uint64_t *defval);