2011-04-08 13:58:21 +00:00
|
|
|
#include "loadscpt.hpp"
|
|
|
|
|
2018-08-14 15:42:41 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2012-09-23 18:41:41 +00:00
|
|
|
#include "esmreader.hpp"
|
|
|
|
#include "esmwriter.hpp"
|
2013-09-24 11:17:28 +00:00
|
|
|
#include "defs.hpp"
|
2012-09-17 07:37:50 +00:00
|
|
|
|
2011-04-08 13:58:21 +00:00
|
|
|
namespace ESM
|
|
|
|
{
|
2013-09-24 11:17:28 +00:00
|
|
|
unsigned int Script::sRecordId = REC_SCPT;
|
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
void Script::loadSCVR(ESMReader &esm)
|
2011-04-08 13:58:21 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
int s = mData.mStringTableSize;
|
2013-12-15 15:19:45 +00:00
|
|
|
|
|
|
|
std::vector<char> tmp (s);
|
2014-12-05 22:36:06 +00:00
|
|
|
// not using getHExact, vanilla doesn't seem to mind unused bytes at the end
|
|
|
|
esm.getSubHeader();
|
|
|
|
int left = esm.getSubSize();
|
|
|
|
if (left < s)
|
|
|
|
esm.fail("SCVR string list is smaller than specified");
|
|
|
|
esm.getExact(&tmp[0], s);
|
|
|
|
if (left > s)
|
|
|
|
esm.skip(left-s); // skip the leftover junk
|
2011-04-08 13:58:21 +00:00
|
|
|
|
|
|
|
// Set up the list of variable names
|
2012-09-17 07:37:50 +00:00
|
|
|
mVarNames.resize(mData.mNumShorts + mData.mNumLongs + mData.mNumFloats);
|
2011-04-08 13:58:21 +00:00
|
|
|
|
|
|
|
// The tmp buffer is a null-byte separated string list, we
|
|
|
|
// just have to pick out one string at a time.
|
2013-12-15 15:19:45 +00:00
|
|
|
char* str = &tmp[0];
|
2018-10-12 07:28:18 +00:00
|
|
|
if (!str && mVarNames.size() > 0)
|
|
|
|
{
|
|
|
|
Log(Debug::Warning) << "SCVR with no variable names";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
for (size_t i = 0; i < mVarNames.size(); i++)
|
2011-04-08 13:58:21 +00:00
|
|
|
{
|
2014-08-10 02:54:54 +00:00
|
|
|
// Support '\r' terminated strings like vanilla. See Bug #1324.
|
2014-05-17 21:34:13 +00:00
|
|
|
char *termsym = strchr(str, '\r');
|
|
|
|
if(termsym) *termsym = '\0';
|
2012-09-17 07:37:50 +00:00
|
|
|
mVarNames[i] = std::string(str);
|
|
|
|
str += mVarNames[i].size() + 1;
|
2011-04-08 13:58:21 +00:00
|
|
|
|
2013-12-15 15:19:45 +00:00
|
|
|
if (str - &tmp[0] > s)
|
2014-08-10 02:54:54 +00:00
|
|
|
{
|
|
|
|
// Apparently SCVR subrecord is not used and variable names are
|
|
|
|
// determined on the fly from the script text. Therefore don't throw
|
|
|
|
// an exeption, just log an error and continue.
|
|
|
|
std::stringstream ss;
|
|
|
|
|
2018-08-14 15:42:41 +00:00
|
|
|
ss << "String table overflow";
|
2014-08-10 02:54:54 +00:00
|
|
|
ss << "\n File: " << esm.getName();
|
|
|
|
ss << "\n Record: " << esm.getContext().recName.toString();
|
|
|
|
ss << "\n Subrecord: " << "SCVR";
|
|
|
|
ss << "\n Offset: 0x" << std::hex << esm.getFileOffset();
|
2018-08-14 15:42:41 +00:00
|
|
|
Log(Debug::Verbose) << ss.str();
|
2014-08-10 02:54:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-08 13:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 14:23:14 +00:00
|
|
|
void Script::load(ESMReader &esm, bool &isDeleted)
|
2014-09-05 14:43:20 +00:00
|
|
|
{
|
2015-07-20 14:23:14 +00:00
|
|
|
isDeleted = false;
|
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
mVarNames.clear();
|
2011-04-08 13:58:21 +00:00
|
|
|
|
2015-07-16 16:52:31 +00:00
|
|
|
bool hasHeader = false;
|
2015-02-12 03:56:05 +00:00
|
|
|
while (esm.hasMoreSubs())
|
|
|
|
{
|
|
|
|
esm.getSubName();
|
2016-05-07 17:32:51 +00:00
|
|
|
switch (esm.retSubName().intval)
|
2015-02-12 03:56:05 +00:00
|
|
|
{
|
2015-07-16 16:52:31 +00:00
|
|
|
case ESM::FourCC<'S','C','H','D'>::value:
|
2016-05-07 17:32:51 +00:00
|
|
|
{
|
2015-07-16 16:52:31 +00:00
|
|
|
SCHD data;
|
|
|
|
esm.getHT(data, 52);
|
|
|
|
mData = data.mData;
|
|
|
|
mId = data.mName.toString();
|
|
|
|
hasHeader = true;
|
|
|
|
break;
|
2016-05-07 17:32:51 +00:00
|
|
|
}
|
2015-02-12 03:56:05 +00:00
|
|
|
case ESM::FourCC<'S','C','V','R'>::value:
|
|
|
|
// list of local variables
|
|
|
|
loadSCVR(esm);
|
|
|
|
break;
|
|
|
|
case ESM::FourCC<'S','C','D','T'>::value:
|
2017-03-18 14:42:24 +00:00
|
|
|
{
|
2015-02-12 03:56:05 +00:00
|
|
|
// compiled script
|
2017-03-18 14:42:24 +00:00
|
|
|
esm.getSubHeader();
|
|
|
|
uint32_t subSize = esm.getSubSize();
|
|
|
|
|
|
|
|
if (subSize != static_cast<uint32_t>(mData.mScriptDataSize))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2018-08-14 15:42:41 +00:00
|
|
|
ss << "Script data size defined in SCHD subrecord does not match size of SCDT subrecord";
|
2017-03-18 14:42:24 +00:00
|
|
|
ss << "\n File: " << esm.getName();
|
|
|
|
ss << "\n Offset: 0x" << std::hex << esm.getFileOffset();
|
2018-08-14 15:42:41 +00:00
|
|
|
Log(Debug::Verbose) << ss.str();
|
2017-03-18 14:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mScriptData.resize(subSize);
|
2019-03-15 21:08:24 +00:00
|
|
|
esm.getExact(mScriptData.data(), mScriptData.size());
|
2015-02-12 03:56:05 +00:00
|
|
|
break;
|
2017-03-18 14:42:24 +00:00
|
|
|
}
|
2015-02-12 03:56:05 +00:00
|
|
|
case ESM::FourCC<'S','C','T','X'>::value:
|
|
|
|
mScriptText = esm.getHString();
|
|
|
|
break;
|
2015-11-13 17:07:18 +00:00
|
|
|
case ESM::SREC_DELE:
|
2015-07-20 14:23:14 +00:00
|
|
|
esm.skipHSub();
|
|
|
|
isDeleted = true;
|
|
|
|
break;
|
2015-02-12 03:56:05 +00:00
|
|
|
default:
|
|
|
|
esm.fail("Unknown subrecord");
|
2015-07-16 16:52:31 +00:00
|
|
|
break;
|
2015-02-12 03:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-16 16:52:31 +00:00
|
|
|
|
|
|
|
if (!hasHeader)
|
|
|
|
esm.fail("Missing SCHD subrecord");
|
2014-08-28 21:41:49 +00:00
|
|
|
}
|
2012-04-13 22:14:04 +00:00
|
|
|
|
2015-07-20 14:23:14 +00:00
|
|
|
void Script::save(ESMWriter &esm, bool isDeleted) const
|
2015-02-12 03:56:05 +00:00
|
|
|
{
|
|
|
|
std::string varNameString;
|
|
|
|
if (!mVarNames.empty())
|
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
|
|
|
varNameString.append(*it);
|
2012-10-26 18:22:55 +00:00
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
SCHD data;
|
|
|
|
memset(&data, 0, sizeof(data));
|
2012-10-26 18:22:55 +00:00
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
data.mData = mData;
|
2016-05-07 17:32:51 +00:00
|
|
|
data.mName.assign(mId);
|
2013-04-07 13:17:35 +00:00
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
esm.writeHNT("SCHD", data, 52);
|
|
|
|
|
2015-07-20 14:23:14 +00:00
|
|
|
if (isDeleted)
|
2015-07-07 14:00:40 +00:00
|
|
|
{
|
2015-07-16 16:52:31 +00:00
|
|
|
esm.writeHNCString("DELE", "");
|
2015-07-28 12:04:22 +00:00
|
|
|
return;
|
2015-07-07 14:00:40 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
if (!mVarNames.empty())
|
2012-04-06 19:04:30 +00:00
|
|
|
{
|
2015-02-12 03:56:05 +00:00
|
|
|
esm.startSubRecord("SCVR");
|
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
|
|
|
{
|
|
|
|
esm.writeHCString(*it);
|
|
|
|
}
|
|
|
|
esm.endRecord("SCVR");
|
2012-04-06 19:04:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
esm.startSubRecord("SCDT");
|
2019-03-15 21:08:24 +00:00
|
|
|
esm.write(reinterpret_cast<const char *>(mScriptData.data()), mData.mScriptDataSize);
|
2015-02-12 03:56:05 +00:00
|
|
|
esm.endRecord("SCDT");
|
2012-04-12 12:00:58 +00:00
|
|
|
|
2015-02-12 03:56:05 +00:00
|
|
|
esm.writeHNOString("SCTX", mScriptText);
|
|
|
|
}
|
2011-04-08 13:58:21 +00:00
|
|
|
|
2013-04-07 13:17:35 +00:00
|
|
|
void Script::blank()
|
|
|
|
{
|
|
|
|
mData.mNumShorts = mData.mNumLongs = mData.mNumFloats = 0;
|
|
|
|
mData.mScriptDataSize = 0;
|
|
|
|
mData.mStringTableSize = 0;
|
|
|
|
|
|
|
|
mVarNames.clear();
|
|
|
|
mScriptData.clear();
|
2014-09-14 09:41:28 +00:00
|
|
|
|
|
|
|
if (mId.find ("::")!=std::string::npos)
|
|
|
|
mScriptText = "Begin \"" + mId + "\"\n\nEnd " + mId + "\n";
|
|
|
|
else
|
|
|
|
mScriptText = "Begin " + mId + "\n\nEnd " + mId + "\n";
|
2013-04-07 13:17:35 +00:00
|
|
|
}
|
|
|
|
|
2011-04-08 13:58:21 +00:00
|
|
|
}
|