2011-04-08 13:58:21 +00:00
|
|
|
#include "loadscpt.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
|
|
|
|
{
|
|
|
|
|
2012-10-26 18:22:55 +00:00
|
|
|
struct SCHD
|
|
|
|
{
|
2012-11-05 13:41:36 +00:00
|
|
|
NAME32 mName;
|
|
|
|
Script::SCHDstruct mData;
|
2012-10-26 18:22:55 +00:00
|
|
|
};
|
|
|
|
|
2013-09-24 11:17:28 +00:00
|
|
|
unsigned int Script::sRecordId = REC_SCPT;
|
|
|
|
|
2011-04-08 13:58:21 +00:00
|
|
|
void Script::load(ESMReader &esm)
|
|
|
|
{
|
2012-10-26 18:22:55 +00:00
|
|
|
SCHD data;
|
|
|
|
esm.getHNT(data, "SCHD", 52);
|
|
|
|
mData = data.mData;
|
|
|
|
mId = data.mName.toString();
|
2011-04-08 13:58:21 +00:00
|
|
|
|
|
|
|
// List of local variables
|
|
|
|
if (esm.isNextSub("SCVR"))
|
|
|
|
{
|
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);
|
|
|
|
esm.getHExact (&tmp[0], s);
|
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];
|
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-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)
|
2011-04-08 13:58:21 +00:00
|
|
|
esm.fail("String table overflow");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
// Script mData
|
|
|
|
mScriptData.resize(mData.mScriptDataSize);
|
|
|
|
esm.getHNExact(&mScriptData[0], mScriptData.size(), "SCDT");
|
2011-04-08 13:58:21 +00:00
|
|
|
|
|
|
|
// Script text
|
2012-09-17 07:37:50 +00:00
|
|
|
mScriptText = esm.getHNOString("SCTX");
|
2011-04-08 13:58:21 +00:00
|
|
|
}
|
2013-09-16 10:32:35 +00:00
|
|
|
void Script::save(ESMWriter &esm) const
|
2012-04-06 19:04:30 +00:00
|
|
|
{
|
2012-04-12 12:00:58 +00:00
|
|
|
std::string varNameString;
|
2012-09-17 07:37:50 +00:00
|
|
|
if (!mVarNames.empty())
|
2013-09-16 10:32:35 +00:00
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
2012-04-13 22:14:04 +00:00
|
|
|
varNameString.append(*it);
|
|
|
|
|
2012-10-26 18:22:55 +00:00
|
|
|
SCHD data;
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
|
|
|
|
data.mData = mData;
|
|
|
|
memcpy(data.mName.name, mId.c_str(), mId.size());
|
|
|
|
|
|
|
|
esm.writeHNT("SCHD", data, 52);
|
2013-04-07 13:17:35 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if (!mVarNames.empty())
|
2012-04-06 19:04:30 +00:00
|
|
|
{
|
2012-04-13 22:14:04 +00:00
|
|
|
esm.startSubRecord("SCVR");
|
2013-09-16 10:32:35 +00:00
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
2012-04-06 19:04:30 +00:00
|
|
|
{
|
2012-04-13 22:14:04 +00:00
|
|
|
esm.writeHCString(*it);
|
2012-04-06 19:04:30 +00:00
|
|
|
}
|
2012-04-13 22:14:04 +00:00
|
|
|
esm.endRecord("SCVR");
|
2012-04-06 19:04:30 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 22:14:04 +00:00
|
|
|
esm.startSubRecord("SCDT");
|
2013-04-07 13:17:35 +00:00
|
|
|
esm.write(reinterpret_cast<const char * >(&mScriptData[0]), mData.mScriptDataSize);
|
2012-04-13 22:14:04 +00:00
|
|
|
esm.endRecord("SCDT");
|
2012-04-12 12:00:58 +00:00
|
|
|
|
2012-09-17 07:37:50 +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();
|
|
|
|
mScriptText = "Begin " + mId + "\n\nEnd " + mId + "\n";
|
|
|
|
}
|
|
|
|
|
2011-04-08 13:58:21 +00:00
|
|
|
}
|