2019-09-19 06:06:11 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2019 EDuke32 developers and contributors
|
|
|
|
Copyright (C) 2019 sirlemonhead, Nuke.YKT
|
|
|
|
|
|
|
|
This file is part of NBlood.
|
|
|
|
|
|
|
|
NBlood is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2019-09-21 18:59:54 +00:00
|
|
|
#include "ns.h" // Must come before everything else!
|
|
|
|
|
2020-12-09 14:56:32 +00:00
|
|
|
#include "blood.h"
|
2020-04-11 21:54:33 +00:00
|
|
|
#include "filesystem.h"
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2019-09-22 06:39:22 +00:00
|
|
|
BEGIN_BLD_NS
|
|
|
|
|
2020-04-11 21:54:33 +00:00
|
|
|
// I don't think we still need these.
|
2020-10-11 10:18:25 +00:00
|
|
|
enum
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
DICT_LOAD = 0,
|
|
|
|
DICT_LOCK = 0,
|
|
|
|
|
|
|
|
kMaxCmdLineDefines = 5,
|
|
|
|
kMaxDefines = 1000,
|
|
|
|
kMaxParseLevels = 5
|
2020-10-11 10:18:25 +00:00
|
|
|
};
|
2019-09-19 06:06:11 +00:00
|
|
|
static int nCmdDefines = 0;
|
|
|
|
static int nDefines = 0;
|
|
|
|
|
|
|
|
static int gParseLevel = 0;
|
|
|
|
int dword_44CE0[kMaxParseLevels] = { 0, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
unsigned int nBytes = 0;
|
|
|
|
char buffer[1024];
|
|
|
|
int scriptValue = 0;
|
|
|
|
|
|
|
|
char scriptBuffer[256];
|
|
|
|
|
|
|
|
struct define_t
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
FString _text;
|
|
|
|
int _value;
|
2019-09-19 06:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
define_t gCmdDefines[kMaxCmdLineDefines];
|
|
|
|
|
2022-10-12 20:09:26 +00:00
|
|
|
void addMemoryResource(const char* fileName, int flags, int ID);
|
2019-09-19 06:06:11 +00:00
|
|
|
|
|
|
|
struct tag_t {
|
2021-12-29 19:45:55 +00:00
|
|
|
const char* _value;
|
|
|
|
uint8_t _index;
|
2019-09-19 06:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum eTags
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
kTag0,
|
|
|
|
kTagEnd,
|
|
|
|
kTagString,
|
|
|
|
kTagConstant,
|
|
|
|
kTag4, // string constant?
|
|
|
|
kTagComma,
|
|
|
|
kTagSemiColon,
|
|
|
|
kTagColon,
|
|
|
|
kTagEquals,
|
|
|
|
kTagHash,
|
|
|
|
kTagComment,
|
|
|
|
kTagInclude,
|
|
|
|
kTagResource,
|
|
|
|
kTagAs,
|
|
|
|
kTagPreload,
|
|
|
|
kTagPrelock,
|
|
|
|
kTagData,
|
|
|
|
kTagLoad,
|
|
|
|
kTagEmit,
|
|
|
|
kTagIfDef,
|
|
|
|
kTagEndif,
|
|
|
|
kTagElse
|
2019-09-19 06:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tag_t tags[] =
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
{ ",", kTagComma },
|
|
|
|
{ ";", kTagSemiColon },
|
|
|
|
{ ":", kTagColon },
|
|
|
|
{ "=", kTagEquals },
|
|
|
|
{ "#", kTagHash },
|
|
|
|
{ "//", kTagComment },
|
|
|
|
{ "INCLUDE", kTagInclude },
|
|
|
|
{ "RESOURCE", kTagResource },
|
|
|
|
{ "AS", kTagAs },
|
|
|
|
{ "PRELOAD", kTagPreload },
|
|
|
|
{ "PRELOCK", kTagPrelock },
|
|
|
|
{ "DATA", kTagData },
|
|
|
|
{ "LOAD", kTagLoad },
|
|
|
|
{ "EMIT", kTagEmit },
|
|
|
|
{ "%ifdef", kTagIfDef },
|
|
|
|
{ "%endif", kTagEndif },
|
|
|
|
{ "%else", kTagElse }
|
2019-09-19 06:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int kTagCount = sizeof(tags) / sizeof(tag_t);
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
int qsort_compar(const void* a, const void* b)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
return stricmp((const char*)a, (const char*)b);
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SortTags()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
qsort(tags, kTagCount, sizeof(tag_t), qsort_compar);
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
void AddCmdDefine(char* text, int value)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
assert(nCmdDefines < kMaxCmdLineDefines);
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2019-10-31 19:17:49 +00:00
|
|
|
gCmdDefines[nCmdDefines]._text = text;
|
2021-12-29 19:45:55 +00:00
|
|
|
gCmdDefines[nCmdDefines]._value = value;
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
nCmdDefines++;
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
static void SplitPath(const char* pzPath, char* pzDirectory, char* pzFile, char* pzType)
|
2020-09-01 17:49:05 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
int const nLength = (int)strlen(pzPath);
|
|
|
|
const char* pDot = NULL;
|
|
|
|
for (int i = nLength - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (pzPath[i] == '/' || pzPath[i] == '\\')
|
|
|
|
{
|
|
|
|
strncpy(pzDirectory, pzPath, i);
|
|
|
|
pzDirectory[i] = 0;
|
|
|
|
if (!pDot)
|
|
|
|
{
|
|
|
|
strcpy(pzFile, pzPath + i + 1);
|
|
|
|
strcpy(pzType, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(pzFile, pzPath + i + 1, pDot - (pzPath + i + 1));
|
|
|
|
pzFile[pDot - (pzPath + i + 1)] = 0;
|
|
|
|
strcpy(pzType, pDot + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (pzPath[i] == '.')
|
|
|
|
{
|
|
|
|
pDot = pzPath + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcpy(pzDirectory, "/");
|
|
|
|
if (!pDot)
|
|
|
|
{
|
|
|
|
strcpy(pzFile, pzPath);
|
|
|
|
strcpy(pzType, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(pzFile, pzPath, pDot - pzPath);
|
|
|
|
pzFile[pDot - pzPath] = 0;
|
|
|
|
strcpy(pzType, pDot + 1);
|
|
|
|
}
|
2020-09-01 17:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2019-09-19 06:06:11 +00:00
|
|
|
// 174 bytes
|
|
|
|
struct RFS
|
|
|
|
{
|
|
|
|
private:
|
2019-10-31 19:17:49 +00:00
|
|
|
TArray<char> buffer;
|
2021-12-29 19:45:55 +00:00
|
|
|
char* _ptr; // [0]
|
|
|
|
char _curChar; // [4]
|
|
|
|
char* _pUnknown2; // [5] - some sort of pointer into _ptr?
|
|
|
|
char* _pStartLine; // [9]
|
|
|
|
char* _pEnd; // [13]
|
|
|
|
char* _pMark; // [17]
|
|
|
|
char _unknown6; // [21]
|
|
|
|
int _unknown7; // [22]
|
|
|
|
int _curLine; // [26]
|
|
|
|
char _fileName[BMAX_PATH]; // [30]
|
2019-09-19 06:06:11 +00:00
|
|
|
|
|
|
|
public:
|
2021-12-29 19:45:55 +00:00
|
|
|
int Open(int lumpnum);
|
|
|
|
void Close();
|
|
|
|
void Increment();
|
|
|
|
void SkipBeyondValue(char value);
|
|
|
|
uint8_t GetNextTag();
|
|
|
|
void ScriptError(const char* message);
|
|
|
|
void SetMark();
|
|
|
|
void UnsetMark();
|
2019-09-19 06:06:11 +00:00
|
|
|
};
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-01-22 20:09:45 +00:00
|
|
|
int RFS::Open(int lumpnum)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
auto hFile = fileSystem.OpenFileReader(lumpnum);
|
|
|
|
if (!hFile.isOpen()) {
|
|
|
|
Printf("BARF: Error opening file %d", lumpnum);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-05-12 00:00:06 +00:00
|
|
|
int fileSize = (int)hFile.GetLength();
|
2021-12-29 19:45:55 +00:00
|
|
|
buffer.Resize(fileSize + 1);
|
|
|
|
_ptr = buffer.Data();
|
|
|
|
if (_ptr == NULL) {
|
|
|
|
Printf("BARF: Not enough memory to read %d", lumpnum);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile.Read(_ptr, fileSize);
|
|
|
|
buffer[fileSize] = '\n';
|
|
|
|
|
|
|
|
_curLine = 0;
|
|
|
|
_pUnknown2 = _ptr;
|
|
|
|
_curChar = '\n';
|
|
|
|
_pEnd = &_ptr[fileSize];
|
|
|
|
|
|
|
|
return 0;
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RFS::Close()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2019-09-19 06:06:11 +00:00
|
|
|
void RFS::Increment()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
if (_curChar == '\n') {
|
|
|
|
_curLine++;
|
|
|
|
_pStartLine = _pUnknown2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_pUnknown2 >= _pEnd) {
|
|
|
|
_curChar = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_curChar = *_pUnknown2; // grabs the next char
|
|
|
|
_pUnknown2++; // increment pointer into char data
|
|
|
|
}
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2019-09-19 06:06:11 +00:00
|
|
|
void RFS::SkipBeyondValue(char nVal)
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
while (_curChar && _curChar != nVal) {
|
|
|
|
Increment();
|
|
|
|
}
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RFS::SetMark()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
_pMark = _pUnknown2;
|
|
|
|
_unknown6 = _curChar;
|
|
|
|
_unknown7 = _curLine;
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// inverse of the above function
|
|
|
|
void RFS::UnsetMark()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
_pUnknown2 = _pMark;
|
|
|
|
_curChar = _unknown6;
|
|
|
|
_curLine = _unknown7;
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
void RFS::ScriptError(const char* message)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
// TODO
|
|
|
|
TArray<char> msg;
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
char* p = _pStartLine;
|
|
|
|
while (*p != '\n')
|
|
|
|
{
|
2022-10-12 20:09:26 +00:00
|
|
|
if (isprint((uint8_t) *p))
|
2021-12-29 19:45:55 +00:00
|
|
|
msg.Push(*p);
|
|
|
|
else
|
|
|
|
msg.Push(' ');
|
|
|
|
p++;
|
|
|
|
}
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
msg.Push('\n');
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
p = _pStartLine;
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
while (p < _pMark)
|
|
|
|
{
|
|
|
|
msg.Push(' ');
|
|
|
|
p++;
|
|
|
|
}
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
msg.Push('^');
|
|
|
|
msg.Push(0);
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
Printf("Error in %s line %d: %s\n\n%s", _fileName, _curLine, message, msg.Data());
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2019-09-19 06:06:11 +00:00
|
|
|
uint8_t RFS::GetNextTag()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
// skip any space characters
|
|
|
|
do {
|
|
|
|
Increment();
|
2022-10-12 20:09:26 +00:00
|
|
|
} while (isspace((uint8_t)_curChar));
|
2021-12-29 19:45:55 +00:00
|
|
|
|
|
|
|
if (_curChar == '\0') {
|
|
|
|
return kTagEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetMark();
|
|
|
|
|
|
|
|
// Path A
|
|
|
|
if (_curChar == '"')
|
|
|
|
{
|
|
|
|
Increment();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// section 1
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
if (_curChar == '\0' || _curChar == '"') {
|
|
|
|
scriptBuffer[i] = '\0';
|
|
|
|
return kTagString;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 256) {
|
|
|
|
ScriptError("String exceeds maximum string length");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptBuffer[i] = _curChar;
|
|
|
|
i++;
|
|
|
|
Increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
// section 2
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (_curChar == '\0' || _curChar == '"') {
|
|
|
|
return kTag0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Increment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scriptValue = 0;
|
|
|
|
bool isNegative = false; // or 'isSigned' ?
|
|
|
|
|
|
|
|
// is it a negative number?
|
|
|
|
if (_curChar == '-')
|
|
|
|
{
|
|
|
|
Increment();
|
|
|
|
|
|
|
|
isNegative = true;
|
|
|
|
|
2022-10-12 20:09:26 +00:00
|
|
|
if (!isdigit((uint8_t)_curChar)) {
|
2021-12-29 19:45:55 +00:00
|
|
|
UnsetMark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:09:26 +00:00
|
|
|
if (isdigit((uint8_t)_curChar))
|
2021-12-29 19:45:55 +00:00
|
|
|
{
|
|
|
|
// left path
|
|
|
|
if (_curChar == '0')
|
|
|
|
{
|
|
|
|
Increment();
|
|
|
|
|
|
|
|
// handle a hex value
|
|
|
|
if (toupper(_curChar) == 'X')
|
|
|
|
{
|
|
|
|
// orange loop
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
Increment();
|
2022-10-12 20:09:26 +00:00
|
|
|
if (!isxdigit((uint8_t)_curChar)) { // isxdigit() checks for a hex value
|
2021-12-29 19:45:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hex version of atoi?
|
|
|
|
scriptValue *= 16;
|
2022-10-12 20:09:26 +00:00
|
|
|
if (!isdigit((uint8_t)_curChar)) {
|
|
|
|
scriptValue += toupper((uint8_t)_curChar) - 55;
|
2021-12-29 19:45:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
scriptValue += _curChar - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
SetMark();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetMark();
|
|
|
|
if (isNegative) {
|
|
|
|
scriptValue = -scriptValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kTagConstant;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetMark();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the loop
|
|
|
|
while (isdigit(_curChar))
|
|
|
|
{
|
|
|
|
// atoi implementation
|
|
|
|
scriptValue = scriptValue * 10 + _curChar - '0';
|
|
|
|
SetMark();
|
|
|
|
Increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetMark();
|
|
|
|
if (isNegative) {
|
|
|
|
scriptValue = -scriptValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kTagConstant;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// BLUEISH PATH
|
|
|
|
int ebp = 0; // v11
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// blue loop #1
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
scriptBuffer[ebp] = _curChar;
|
|
|
|
ebp++;
|
|
|
|
int eax = -1;
|
|
|
|
|
|
|
|
// blue loop #2
|
|
|
|
for (i = 0; i < kTagCount; i++)
|
|
|
|
{
|
|
|
|
//if (eax >= 0) {
|
|
|
|
if (eax == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eax = strnicmp(tags[i]._value, scriptBuffer, ebp);
|
|
|
|
eax = strnicmp(scriptBuffer, tags[i]._value, ebp);
|
|
|
|
|
|
|
|
//if (eax >= 0) {
|
|
|
|
if (eax == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eax > 0 || i == kTagCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eax == 0 && (int)strlen(tags[i]._value) == ebp)
|
|
|
|
{
|
|
|
|
scriptBuffer[ebp] = 0;
|
|
|
|
return tags[i]._index;
|
|
|
|
}
|
|
|
|
|
|
|
|
Increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetMark();
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2022-10-12 20:09:26 +00:00
|
|
|
while (isalnum((uint8_t)_curChar))
|
2021-12-29 19:45:55 +00:00
|
|
|
{
|
|
|
|
scriptBuffer[i] = _curChar;
|
|
|
|
SetMark();
|
|
|
|
i++;
|
|
|
|
Increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetMark();
|
|
|
|
scriptBuffer[i] = 0;
|
|
|
|
return kTag4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// qAssert(1==0); // TODO - what to return here
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-01-22 20:09:45 +00:00
|
|
|
void ParseScript(int lumpnum)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
char text[256];
|
|
|
|
char char256_1[256];
|
|
|
|
char char256_2[256];
|
|
|
|
char fileName[BMAX_PATH];
|
|
|
|
char inp[BMAX_PATH];
|
|
|
|
//char zScriptDirectory[BMAX_PATH], zTemp1[BMAX_PATH], zTemp2[BMAX_PATH];
|
|
|
|
|
|
|
|
//SplitPath(scriptFileName, zScriptDirectory, zTemp1, zTemp2);
|
|
|
|
|
|
|
|
RFS rfs;
|
|
|
|
|
|
|
|
// AddExtension(name, ".RFS");
|
|
|
|
if (rfs.Open(lumpnum))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gParseLevel = 0;
|
|
|
|
dword_44CE0[0] = 0;
|
|
|
|
|
|
|
|
bool parsing = true;
|
|
|
|
|
|
|
|
while (parsing)
|
|
|
|
{
|
|
|
|
// START LOOP. to be fixed later
|
|
|
|
START:
|
|
|
|
|
|
|
|
uint8_t tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
switch (tag)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagEnd:
|
|
|
|
{
|
|
|
|
parsing = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagComment:
|
|
|
|
{
|
|
|
|
// skip to next line
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagEmit: // minty/light green colour
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTag4)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Symbol name expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(char256_2, scriptBuffer);
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTagEquals)
|
2019-11-02 09:20:32 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
rfs.ScriptError("Missing '='");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTagConstant)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Constant expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//AddDefine(char256_2, scriptValue);
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case kTagResource: // really light blue..
|
|
|
|
{
|
|
|
|
if (kTagString != rfs.GetNextTag()) {
|
|
|
|
rfs.ScriptError("String constant exected");
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(inp, scriptBuffer);
|
|
|
|
uint8_t nFlags = 0;
|
|
|
|
int ID = 0;
|
|
|
|
bool isDefine = false;
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag == kTagAs)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag == kTag4)
|
|
|
|
{
|
|
|
|
strcpy(text, scriptBuffer);
|
|
|
|
|
|
|
|
if (rfs.GetNextTag() != kTagEquals)
|
2019-11-02 09:20:32 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
rfs.ScriptError("Missing '='");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
2019-11-02 09:20:32 +00:00
|
|
|
}
|
2021-12-29 19:45:55 +00:00
|
|
|
|
|
|
|
isDefine = true;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag != kTagConstant)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Constant expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDefine) {
|
|
|
|
//AddDefine(text, scriptValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ID = scriptValue;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
//if (!bNoEncrypt) {
|
|
|
|
// nFlags |= kResFlagIsEncrypted;
|
|
|
|
//}
|
|
|
|
|
|
|
|
while (tag == kTagComma)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
if (tag == kTagPreload) {
|
|
|
|
nFlags |= DICT_LOAD;
|
|
|
|
}
|
|
|
|
else if (tag == kTagPrelock) {
|
|
|
|
nFlags |= DICT_LOCK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rfs.ScriptError("Unrecognized flag");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
goto START; // FIXME
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag != kTagSemiColon)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("';' expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dword_44CE0[gParseLevel] == 0)
|
|
|
|
{
|
|
|
|
// In the RFS files I have seen the outermost directory is not part of what goes into the file system.
|
|
|
|
auto inp1 = strpbrk(inp, "/\\");
|
|
|
|
if (!inp1 || !fileSystem.CreatePathlessCopy(inp1 + 1, ID, nFlags))
|
|
|
|
{
|
|
|
|
// GDX spports this so we should, too.
|
|
|
|
fileSystem.CreatePathlessCopy(inp, ID, nFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagIfDef: // purplish colour
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTag4)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Parameter error in ifdef");
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rfs.SetMark();
|
|
|
|
strcpy(char256_1, scriptBuffer);
|
|
|
|
|
|
|
|
bool bGotDefine = false;
|
|
|
|
|
|
|
|
// check if this was defined via command prompt arguments
|
|
|
|
for (int i = 0; i < nCmdDefines; i++)
|
|
|
|
{
|
|
|
|
if (stricmp(gCmdDefines[i]._text, char256_1) == 0) { // string is equivalent
|
|
|
|
bGotDefine = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loc_11FC3:
|
|
|
|
gParseLevel++;
|
|
|
|
assert(gParseLevel < kMaxParseLevels);
|
|
|
|
|
|
|
|
if (bGotDefine) {
|
|
|
|
dword_44CE0[gParseLevel] = dword_44CE0[gParseLevel - 1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dword_44CE0[gParseLevel] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagElse: // pinky colour
|
|
|
|
{
|
|
|
|
if (gParseLevel)
|
|
|
|
{
|
|
|
|
// loc_12066:
|
|
|
|
if (dword_44CE0[gParseLevel - 1] == 0) {
|
|
|
|
if (dword_44CE0[gParseLevel] == 0) {
|
|
|
|
dword_44CE0[gParseLevel] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rfs.ScriptError("Unexpected else");
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagEndif: // poo coloured
|
|
|
|
{
|
|
|
|
if (gParseLevel) {
|
|
|
|
gParseLevel--;
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Unexpected Endif");
|
|
|
|
rfs.SkipBeyondValue('\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case kTagHash: // gold colour
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag == kTagInclude)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTagString)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("String constant exected");
|
|
|
|
// why no SkipBeyondValue?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// too dangerous if we want to cumulatively collect all RFS files
|
|
|
|
//fileSystem.Rehash();
|
|
|
|
//ParseScript(scriptBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagData: // eg: data "AMB1.SFX" as 1: 80, 0x10000, 0x0, 1, -1, "amb1";
|
|
|
|
{
|
|
|
|
// green coloured section
|
|
|
|
if (rfs.GetNextTag() != kTagString) {
|
|
|
|
rfs.ScriptError("String constant expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eg strcpy(fileName, "AMB1.SFX");
|
|
|
|
strcpy(fileName, scriptBuffer);
|
|
|
|
|
|
|
|
uint8_t nFlags = 0;
|
|
|
|
int ID = 0;
|
|
|
|
|
|
|
|
bool isDefine = false;
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
// process an ID section
|
|
|
|
if (tag == kTagAs)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag == kTag4)
|
|
|
|
{
|
|
|
|
strcpy(fileName, scriptBuffer);
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTagEquals) {
|
|
|
|
rfs.ScriptError("Missing '='");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
isDefine = true;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag != kTagConstant)
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Constant Expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//if (isDefine) {
|
|
|
|
// AddDefine(fileName, scriptValue);
|
|
|
|
//}
|
|
|
|
|
|
|
|
ID = scriptValue;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag == kTagComma)
|
|
|
|
{
|
|
|
|
// process all sections on this line that are comma separated
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag == kTagPreload)
|
|
|
|
{
|
|
|
|
nFlags |= DICT_LOAD;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
if (tag == kTagComma) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tag == kTagPrelock)
|
|
|
|
{
|
|
|
|
nFlags |= DICT_LOCK;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
if (tag == kTagComma) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Unrecognized flag");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
goto START; // FIXME
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loc_12471:
|
|
|
|
if (tag != kTagColon) // marked orange in IDA
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (tag == kTagPreload)
|
|
|
|
{
|
|
|
|
nFlags |= DICT_LOAD;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
if (tag == kTagColon) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tag == kTagPrelock)
|
|
|
|
{
|
|
|
|
nFlags |= DICT_LOCK;
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
if (tag == kTagColon) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rfs.ScriptError("':' expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
goto START; // FIXME
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nBytes = 0;
|
|
|
|
|
|
|
|
// yellow loop
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
|
|
|
|
switch (tag)
|
|
|
|
{
|
|
|
|
case kTagString:
|
|
|
|
{
|
|
|
|
memcpy(&buffer[nBytes], scriptBuffer, strlen(scriptBuffer) + 1);
|
|
|
|
nBytes += (int)strlen(scriptBuffer) + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTagConstant:
|
|
|
|
{
|
|
|
|
memcpy(&buffer[nBytes], &scriptValue, sizeof(scriptValue));
|
|
|
|
nBytes += sizeof(scriptValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
rfs.ScriptError("Constant expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
goto START; // FIXME
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = rfs.GetNextTag();
|
|
|
|
if (tag != kTagComma) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag != kTagSemiColon) {
|
|
|
|
rfs.ScriptError("Semicolon expected");
|
|
|
|
rfs.SkipBeyondValue(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dword_44CE0[gParseLevel] == 0) {
|
|
|
|
addMemoryResource(fileName, nFlags, ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//CreateHeader();
|
|
|
|
rfs.Close();
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-10-12 20:09:26 +00:00
|
|
|
void addMemoryResource(const char* filePath, int flags, int ID)
|
2019-09-19 06:06:11 +00:00
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
char zDirectory[BMAX_PATH];
|
|
|
|
char zFilename[BMAX_PATH];
|
|
|
|
char zType[BMAX_PATH];
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
SplitPath(filePath, zDirectory, zFilename, zType);
|
2019-09-19 06:06:11 +00:00
|
|
|
|
2021-12-29 19:45:55 +00:00
|
|
|
fileSystem.AddFromBuffer(zFilename, zType, buffer, nBytes, ID, flags);
|
2019-09-19 06:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 20:09:45 +00:00
|
|
|
|
2021-12-29 19:03:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-01-22 20:09:45 +00:00
|
|
|
void ReadAllRFS()
|
|
|
|
{
|
2021-12-29 19:45:55 +00:00
|
|
|
bool found = false;
|
|
|
|
auto numf = fileSystem.GetNumEntries();
|
|
|
|
for (int i = 0; i < numf; i++)
|
|
|
|
{
|
|
|
|
auto rl = fileSystem.GetResourceType(i);
|
|
|
|
if (!stricmp(rl, "RFS"))
|
|
|
|
{
|
|
|
|
ParseScript(i);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) fileSystem.InitHashChains();
|
2020-01-22 20:09:45 +00:00
|
|
|
}
|
2019-09-22 06:39:22 +00:00
|
|
|
END_BLD_NS
|