|
|
@ -1,6 +1,8 @@
|
|
|
|
#include "loadscpt.hpp"
|
|
|
|
#include "loadscpt.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <sstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
@ -11,15 +13,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace ESM
|
|
|
|
namespace ESM
|
|
|
|
{
|
|
|
|
{
|
|
|
|
template <Misc::SameAsWithoutCvref<Script::SCHD> T>
|
|
|
|
namespace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct SCHD
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Data from script-precompling in the editor.
|
|
|
|
|
|
|
|
/// \warning Do not use them. OpenCS currently does not precompile scripts.
|
|
|
|
|
|
|
|
std::uint32_t mNumShorts = 0;
|
|
|
|
|
|
|
|
std::uint32_t mNumLongs = 0;
|
|
|
|
|
|
|
|
std::uint32_t mNumFloats = 0;
|
|
|
|
|
|
|
|
std::uint32_t mScriptDataSize = 0;
|
|
|
|
|
|
|
|
std::uint32_t mStringTableSize = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <Misc::SameAsWithoutCvref<SCHD> T>
|
|
|
|
void decompose(T&& v, const auto& f)
|
|
|
|
void decompose(T&& v, const auto& f)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
f(v.mNumShorts, v.mNumLongs, v.mNumFloats, v.mScriptDataSize, v.mStringTableSize);
|
|
|
|
f(v.mNumShorts, v.mNumLongs, v.mNumFloats, v.mScriptDataSize, v.mStringTableSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Script::loadSCVR(ESMReader& esm)
|
|
|
|
void loadVarNames(const SCHD& header, std::vector<std::string>& varNames, ESMReader& esm)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint32_t s = mData.mStringTableSize;
|
|
|
|
uint32_t s = header.mStringTableSize;
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<char> tmp(s);
|
|
|
|
std::vector<char> tmp(s);
|
|
|
|
// not using getHExact, vanilla doesn't seem to mind unused bytes at the end
|
|
|
|
// not using getHExact, vanilla doesn't seem to mind unused bytes at the end
|
|
|
@ -32,14 +47,13 @@ namespace ESM
|
|
|
|
esm.skip(left - s); // skip the leftover junk
|
|
|
|
esm.skip(left - s); // skip the leftover junk
|
|
|
|
|
|
|
|
|
|
|
|
// Set up the list of variable names
|
|
|
|
// Set up the list of variable names
|
|
|
|
mVarNames.resize(mData.mNumShorts + mData.mNumLongs + mData.mNumFloats);
|
|
|
|
varNames.resize(header.mNumShorts + header.mNumLongs + header.mNumFloats);
|
|
|
|
|
|
|
|
|
|
|
|
// The tmp buffer is a null-byte separated string list, we
|
|
|
|
// The tmp buffer is a null-byte separated string list, we
|
|
|
|
// just have to pick out one string at a time.
|
|
|
|
// just have to pick out one string at a time.
|
|
|
|
char* str = tmp.data();
|
|
|
|
|
|
|
|
if (tmp.empty())
|
|
|
|
if (tmp.empty())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (mVarNames.size() > 0)
|
|
|
|
if (!varNames.empty())
|
|
|
|
Log(Debug::Warning) << "SCVR with no variable names";
|
|
|
|
Log(Debug::Warning) << "SCVR with no variable names";
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -50,7 +64,7 @@ namespace ESM
|
|
|
|
// Avoid heap corruption
|
|
|
|
// Avoid heap corruption
|
|
|
|
if (tmp.back() != '\0')
|
|
|
|
if (tmp.back() != '\0')
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tmp.emplace_back('\0');
|
|
|
|
tmp.push_back('\0');
|
|
|
|
std::stringstream ss;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Malformed string table";
|
|
|
|
ss << "Malformed string table";
|
|
|
|
ss << "\n File: " << esm.getName();
|
|
|
|
ss << "\n File: " << esm.getName();
|
|
|
@ -59,14 +73,14 @@ namespace ESM
|
|
|
|
<< "SCVR";
|
|
|
|
<< "SCVR";
|
|
|
|
ss << "\n Offset: 0x" << std::hex << esm.getFileOffset();
|
|
|
|
ss << "\n Offset: 0x" << std::hex << esm.getFileOffset();
|
|
|
|
Log(Debug::Verbose) << ss.str();
|
|
|
|
Log(Debug::Verbose) << ss.str();
|
|
|
|
str = tmp.data();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto tmpEnd = tmp.data() + tmp.size();
|
|
|
|
const char* str = tmp.data();
|
|
|
|
for (size_t i = 0; i < mVarNames.size(); i++)
|
|
|
|
const char* const tmpEnd = tmp.data() + tmp.size();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < varNames.size(); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mVarNames[i] = std::string(str);
|
|
|
|
varNames[i] = std::string(str);
|
|
|
|
str += mVarNames[i].size() + 1;
|
|
|
|
str += varNames[i].size() + 1;
|
|
|
|
if (str >= tmpEnd)
|
|
|
|
if (str >= tmpEnd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (str > tmpEnd)
|
|
|
|
if (str > tmpEnd)
|
|
|
@ -83,11 +97,12 @@ namespace ESM
|
|
|
|
Log(Debug::Verbose) << ss.str();
|
|
|
|
Log(Debug::Verbose) << ss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get rid of empty strings in the list.
|
|
|
|
// Get rid of empty strings in the list.
|
|
|
|
mVarNames.resize(i + 1);
|
|
|
|
varNames.resize(i + 1);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Script::load(ESMReader& esm, bool& isDeleted)
|
|
|
|
void Script::load(ESMReader& esm, bool& isDeleted)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -96,7 +111,7 @@ namespace ESM
|
|
|
|
|
|
|
|
|
|
|
|
mVarNames.clear();
|
|
|
|
mVarNames.clear();
|
|
|
|
|
|
|
|
|
|
|
|
bool hasHeader = false;
|
|
|
|
std::optional<SCHD> header;
|
|
|
|
while (esm.hasMoreSubs())
|
|
|
|
while (esm.hasMoreSubs())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
esm.getSubName();
|
|
|
|
esm.getSubName();
|
|
|
@ -106,22 +121,27 @@ namespace ESM
|
|
|
|
{
|
|
|
|
{
|
|
|
|
esm.getSubHeader();
|
|
|
|
esm.getSubHeader();
|
|
|
|
mId = esm.getMaybeFixedRefIdSize(32);
|
|
|
|
mId = esm.getMaybeFixedRefIdSize(32);
|
|
|
|
esm.getComposite(mData);
|
|
|
|
esm.getComposite(header.emplace());
|
|
|
|
|
|
|
|
mNumShorts = header->mNumShorts;
|
|
|
|
hasHeader = true;
|
|
|
|
mNumLongs = header->mNumLongs;
|
|
|
|
|
|
|
|
mNumFloats = header->mNumFloats;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case fourCC("SCVR"):
|
|
|
|
case fourCC("SCVR"):
|
|
|
|
// list of local variables
|
|
|
|
if (!header.has_value())
|
|
|
|
loadSCVR(esm);
|
|
|
|
esm.fail("SCVR is placed before SCHD record");
|
|
|
|
|
|
|
|
loadVarNames(*header, mVarNames, esm);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case fourCC("SCDT"):
|
|
|
|
case fourCC("SCDT"):
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!header.has_value())
|
|
|
|
|
|
|
|
esm.fail("SCDT is placed before SCHD record");
|
|
|
|
|
|
|
|
|
|
|
|
// compiled script
|
|
|
|
// compiled script
|
|
|
|
esm.getSubHeader();
|
|
|
|
esm.getSubHeader();
|
|
|
|
uint32_t subSize = esm.getSubSize();
|
|
|
|
uint32_t subSize = esm.getSubSize();
|
|
|
|
|
|
|
|
|
|
|
|
if (subSize != mData.mScriptDataSize)
|
|
|
|
if (subSize != header->mScriptDataSize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Script data size defined in SCHD subrecord does not match size of SCDT subrecord";
|
|
|
|
ss << "Script data size defined in SCHD subrecord does not match size of SCDT subrecord";
|
|
|
@ -147,22 +167,21 @@ namespace ESM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!hasHeader)
|
|
|
|
if (!header.has_value())
|
|
|
|
esm.fail("Missing SCHD subrecord");
|
|
|
|
esm.fail("Missing SCHD subrecord");
|
|
|
|
// Reported script data size is not always trustworthy, so override it with actual data size
|
|
|
|
|
|
|
|
mData.mScriptDataSize = static_cast<uint32_t>(mScriptData.size());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Script::save(ESMWriter& esm, bool isDeleted) const
|
|
|
|
void Script::save(ESMWriter& esm, bool isDeleted) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string varNameString;
|
|
|
|
|
|
|
|
if (!mVarNames.empty())
|
|
|
|
|
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
|
|
|
|
|
|
|
varNameString.append(*it);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esm.startSubRecord("SCHD");
|
|
|
|
esm.startSubRecord("SCHD");
|
|
|
|
esm.writeMaybeFixedSizeRefId(mId, 32);
|
|
|
|
esm.writeMaybeFixedSizeRefId(mId, 32);
|
|
|
|
esm.writeComposite(mData);
|
|
|
|
esm.writeComposite(SCHD{
|
|
|
|
|
|
|
|
.mNumShorts = mNumShorts,
|
|
|
|
|
|
|
|
.mNumLongs = mNumLongs,
|
|
|
|
|
|
|
|
.mNumFloats = mNumFloats,
|
|
|
|
|
|
|
|
.mScriptDataSize = static_cast<std::uint32_t>(mScriptData.size()),
|
|
|
|
|
|
|
|
.mStringTableSize = computeScriptStringTableSize(mVarNames),
|
|
|
|
|
|
|
|
});
|
|
|
|
esm.endRecord("SCHD");
|
|
|
|
esm.endRecord("SCHD");
|
|
|
|
|
|
|
|
|
|
|
|
if (isDeleted)
|
|
|
|
if (isDeleted)
|
|
|
@ -174,15 +193,13 @@ namespace ESM
|
|
|
|
if (!mVarNames.empty())
|
|
|
|
if (!mVarNames.empty())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
esm.startSubRecord("SCVR");
|
|
|
|
esm.startSubRecord("SCVR");
|
|
|
|
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
|
|
|
|
for (const std::string& v : mVarNames)
|
|
|
|
{
|
|
|
|
esm.writeHCString(v);
|
|
|
|
esm.writeHCString(*it);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
esm.endRecord("SCVR");
|
|
|
|
esm.endRecord("SCVR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esm.startSubRecord("SCDT");
|
|
|
|
esm.startSubRecord("SCDT");
|
|
|
|
esm.write(reinterpret_cast<const char*>(mScriptData.data()), mData.mScriptDataSize);
|
|
|
|
esm.write(reinterpret_cast<const char*>(mScriptData.data()), mScriptData.size());
|
|
|
|
esm.endRecord("SCDT");
|
|
|
|
esm.endRecord("SCDT");
|
|
|
|
|
|
|
|
|
|
|
|
esm.writeHNOString("SCTX", mScriptText);
|
|
|
|
esm.writeHNOString("SCTX", mScriptText);
|
|
|
@ -191,9 +208,9 @@ namespace ESM
|
|
|
|
void Script::blank()
|
|
|
|
void Script::blank()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mRecordFlags = 0;
|
|
|
|
mRecordFlags = 0;
|
|
|
|
mData.mNumShorts = mData.mNumLongs = mData.mNumFloats = 0;
|
|
|
|
mNumShorts = 0;
|
|
|
|
mData.mScriptDataSize = 0;
|
|
|
|
mNumLongs = 0;
|
|
|
|
mData.mStringTableSize = 0;
|
|
|
|
mNumFloats = 0;
|
|
|
|
|
|
|
|
|
|
|
|
mVarNames.clear();
|
|
|
|
mVarNames.clear();
|
|
|
|
mScriptData.clear();
|
|
|
|
mScriptData.clear();
|
|
|
@ -204,4 +221,9 @@ namespace ESM
|
|
|
|
mScriptText = "Begin " + stringId + "\n\nEnd " + stringId + "\n";
|
|
|
|
mScriptText = "Begin " + stringId + "\n\nEnd " + stringId + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::uint32_t computeScriptStringTableSize(const std::vector<std::string>& varNames)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return std::accumulate(varNames.begin(), varNames.end(), static_cast<std::uint32_t>(0),
|
|
|
|
|
|
|
|
[](std::uint32_t r, const std::string& v) { return r + 1 + static_cast<std::uint32_t>(v.size()); });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|