This commit is contained in:
Jettford 2023-11-22 01:40:09 +00:00
parent f8a01dead3
commit 1ea65b3549
11 changed files with 67 additions and 83 deletions

View File

@ -28,19 +28,17 @@ FdbToSqlite::Convert::Convert(std::string binaryOutPath) {
this->m_BinaryOutPath = binaryOutPath; this->m_BinaryOutPath = binaryOutPath;
} }
bool FdbToSqlite::Convert::ConvertDatabase(AssetMemoryBuffer& buffer) { bool FdbToSqlite::Convert::ConvertDatabase(AssetStream& buffer) {
if (m_ConversionStarted) return false; if (m_ConversionStarted) return false;
std::istream cdClientBuffer(&buffer);
this->m_ConversionStarted = true; this->m_ConversionStarted = true;
try { try {
CDClientDatabase::Connect(m_BinaryOutPath + "/CDServer.sqlite"); CDClientDatabase::Connect(m_BinaryOutPath + "/CDServer.sqlite");
CDClientDatabase::ExecuteQuery("BEGIN TRANSACTION;"); CDClientDatabase::ExecuteQuery("BEGIN TRANSACTION;");
int32_t numberOfTables = ReadInt32(cdClientBuffer); int32_t numberOfTables = ReadInt32(buffer);
ReadTables(numberOfTables, cdClientBuffer); ReadTables(numberOfTables, buffer);
CDClientDatabase::ExecuteQuery("COMMIT;"); CDClientDatabase::ExecuteQuery("COMMIT;");
} catch (CppSQLite3Exception& e) { } catch (CppSQLite3Exception& e) {

View File

@ -7,7 +7,7 @@
#include <iosfwd> #include <iosfwd>
#include <map> #include <map>
class AssetMemoryBuffer; #include "AssetManager.h"
enum class eSqliteDataType : int32_t; enum class eSqliteDataType : int32_t;
@ -27,7 +27,7 @@ namespace FdbToSqlite {
* *
* @return true if the database was converted properly, false otherwise. * @return true if the database was converted properly, false otherwise.
*/ */
bool ConvertDatabase(AssetMemoryBuffer& buffer); bool ConvertDatabase(AssetStream& buffer);
/** /**
* @brief Reads a 32 bit int from the fdb file. * @brief Reads a 32 bit int from the fdb file.

View File

@ -152,13 +152,12 @@ bool AssetManager::GetFile(const char* name, char** data, uint32_t* len) {
return success; return success;
} }
AssetMemoryBuffer AssetManager::GetFileAsBuffer(const char* name) { AssetStream AssetManager::GetFile(const char* name) {
char* buf; char* buf; uint32_t len;
uint32_t len;
bool success = this->GetFile(name, &buf, &len); bool success = this->GetFile(name, &buf, &len);
return AssetMemoryBuffer(buf, len, success); return AssetStream(buf, len, success);
} }
uint32_t AssetManager::crc32b(uint32_t base, uint8_t* message, size_t l) { uint32_t AssetManager::crc32b(uint32_t base, uint8_t* message, size_t l) {

View File

@ -25,6 +25,10 @@ struct AssetMemoryBuffer : std::streambuf {
this->setg(base, base, base + n); this->setg(base, base, base + n);
} }
~AssetMemoryBuffer() {
free(m_Base);
}
pos_type seekpos(pos_type sp, std::ios_base::openmode which) override { pos_type seekpos(pos_type sp, std::ios_base::openmode which) override {
return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which); return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
} }
@ -40,9 +44,17 @@ struct AssetMemoryBuffer : std::streambuf {
setg(eback(), eback() + off, egptr()); setg(eback(), eback() + off, egptr());
return gptr() - eback(); return gptr() - eback();
} }
};
void close() { struct AssetStream : std::istream {
free(m_Base); AssetStream(char* base, std::ptrdiff_t n, bool success) : std::istream(new AssetMemoryBuffer(base, n, success)) {}
~AssetStream() {
delete rdbuf();
}
operator bool() {
return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success;
} }
}; };
@ -56,7 +68,7 @@ public:
bool HasFile(const char* name); bool HasFile(const char* name);
bool GetFile(const char* name, char** data, uint32_t* len); bool GetFile(const char* name, char** data, uint32_t* len);
AssetMemoryBuffer GetFileAsBuffer(const char* name); AssetStream GetFile(const char* name);
private: private:
void LoadPackIndex(); void LoadPackIndex();

View File

@ -44,57 +44,53 @@ inline void StripCR(std::string& str) {
void UserManager::Initialize() { void UserManager::Initialize() {
std::string line; std::string line;
AssetMemoryBuffer fnBuff = Game::assetManager->GetFileAsBuffer("names/minifigname_first.txt"); auto fnStream = Game::assetManager->GetFile("names/minifigname_first.txt");
if (!fnBuff.m_Success) { if (!fnStream) {
LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_first.txt").string().c_str()); LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_first.txt").string().c_str());
throw std::runtime_error("Aborting initialization due to missing minifigure name file."); throw std::runtime_error("Aborting initialization due to missing minifigure name file.");
} }
std::istream fnStream = std::istream(&fnBuff);
while (std::getline(fnStream, line, '\n')) { while (std::getline(fnStream, line, '\n')) {
std::string name = line; std::string name = line;
StripCR(name); StripCR(name);
m_FirstNames.push_back(name); m_FirstNames.push_back(name);
} }
fnBuff.close();
AssetMemoryBuffer mnBuff = Game::assetManager->GetFileAsBuffer("names/minifigname_middle.txt"); auto mnStream = Game::assetManager->GetFile("names/minifigname_middle.txt");
if (!mnBuff.m_Success) { if (!mnStream) {
LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_middle.txt").string().c_str()); LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_middle.txt").string().c_str());
throw std::runtime_error("Aborting initialization due to missing minifigure name file."); throw std::runtime_error("Aborting initialization due to missing minifigure name file.");
} }
std::istream mnStream = std::istream(&mnBuff);
while (std::getline(mnStream, line, '\n')) { while (std::getline(mnStream, line, '\n')) {
std::string name = line; std::string name = line;
StripCR(name); StripCR(name);
m_MiddleNames.push_back(name); m_MiddleNames.push_back(name);
} }
mnBuff.close();
AssetMemoryBuffer lnBuff = Game::assetManager->GetFileAsBuffer("names/minifigname_last.txt"); auto lnStream = Game::assetManager->GetFile("names/minifigname_last.txt");
if (!lnBuff.m_Success) { if (!lnStream) {
LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_last.txt").string().c_str()); LOG("Failed to load %s", (Game::assetManager->GetResPath() / "names/minifigname_last.txt").string().c_str());
throw std::runtime_error("Aborting initialization due to missing minifigure name file."); throw std::runtime_error("Aborting initialization due to missing minifigure name file.");
} }
std::istream lnStream = std::istream(&lnBuff);
while (std::getline(lnStream, line, '\n')) { while (std::getline(lnStream, line, '\n')) {
std::string name = line; std::string name = line;
StripCR(name); StripCR(name);
m_LastNames.push_back(name); m_LastNames.push_back(name);
} }
lnBuff.close();
// Load our pre-approved names: // Load our pre-approved names:
AssetMemoryBuffer chatListBuff = Game::assetManager->GetFileAsBuffer("chatplus_en_us.txt"); auto chatListStream = Game::assetManager->GetFile("chatplus_en_us.txt");
if (!chatListBuff.m_Success) { if (!chatListStream) {
LOG("Failed to load %s", (Game::assetManager->GetResPath() / "chatplus_en_us.txt").string().c_str()); LOG("Failed to load %s", (Game::assetManager->GetResPath() / "chatplus_en_us.txt").string().c_str());
throw std::runtime_error("Aborting initialization due to missing chat whitelist file."); throw std::runtime_error("Aborting initialization due to missing chat whitelist file.");
} }
std::istream chatListStream = std::istream(&chatListBuff);
while (std::getline(chatListStream, line, '\n')) { while (std::getline(chatListStream, line, '\n')) {
StripCR(line); StripCR(line);
m_PreapprovedNames.push_back(line); m_PreapprovedNames.push_back(line);
} }
chatListBuff.close();
} }
UserManager::~UserManager() { UserManager::~UserManager() {

View File

@ -423,25 +423,16 @@ void Item::DisassembleModel(uint32_t numToDismantle) {
if (renderAssetSplit.empty()) return; if (renderAssetSplit.empty()) return;
std::string lxfmlPath = "BrickModels" + lxfmlFolderName + "/" + GeneralUtils::SplitString(renderAssetSplit.back(), '.').at(0) + ".lxfml"; std::string lxfmlPath = "BrickModels" + lxfmlFolderName + "/" + GeneralUtils::SplitString(renderAssetSplit.back(), '.').at(0) + ".lxfml";
auto buffer = Game::assetManager->GetFileAsBuffer(lxfmlPath.c_str()); auto file = Game::assetManager->GetFile(lxfmlPath.c_str());
if (!buffer.m_Success) { if (!file) {
LOG("Failed to load %s to disassemble model into bricks, check that this file exists", lxfmlPath.c_str()); LOG("Failed to load %s to disassemble model into bricks, check that this file exists", lxfmlPath.c_str());
return; return;
} }
std::istream file(&buffer);
if (!file.good()) {
buffer.close();
return;
}
std::stringstream data; std::stringstream data;
data << file.rdbuf(); data << file.rdbuf();
buffer.close();
uint32_t fileSize; uint32_t fileSize;
file.seekg(0, std::ios::end); file.seekg(0, std::ios::end);
fileSize = static_cast<uint32_t>(file.tellg()); fileSize = static_cast<uint32_t>(file.tellg());

View File

@ -324,14 +324,9 @@ void ControlBehaviors::ProcessCommand(Entity* modelEntity, const SystemAddress&
} }
ControlBehaviors::ControlBehaviors() { ControlBehaviors::ControlBehaviors() {
auto blocksDefStreamBuffer = Game::assetManager->GetFileAsBuffer("ui\\ingame\\blocksdef.xml"); auto blocksBuffer = Game::assetManager->GetFile("ui\\ingame\\blocksdef.xml");
if (!blocksDefStreamBuffer.m_Success) { if (!blocksBuffer) {
LOG("failed to open blocksdef"); LOG("Failed to open blocksdef.xml");
return;
}
std::istream blocksBuffer(&blocksDefStreamBuffer);
if (!blocksBuffer.good()) {
LOG("Blocks buffer is not good!");
return; return;
} }

View File

@ -17,26 +17,18 @@ const BrickList& BrickDatabase::GetBricks(const LxfmlPath& lxfmlPath) {
return cached->second; return cached->second;
} }
AssetMemoryBuffer buffer = Game::assetManager->GetFileAsBuffer((lxfmlPath).c_str()); auto file = Game::assetManager->GetFile((lxfmlPath).c_str());
if (!buffer.m_Success) { if (!file) {
return emptyCache;
}
std::istream file(&buffer);
if (!file.good()) {
return emptyCache; return emptyCache;
} }
std::stringstream data; std::stringstream data;
data << file.rdbuf(); data << file.rdbuf();
if (data.str().empty()) { if (data.str().empty()) {
buffer.close();
return emptyCache; return emptyCache;
} }
buffer.close();
auto* doc = new tinyxml2::XMLDocument(); auto* doc = new tinyxml2::XMLDocument();
if (doc->Parse(data.str().c_str(), data.str().size()) != 0) { if (doc->Parse(data.str().c_str(), data.str().size()) != 0) {
delete doc; delete doc;

View File

@ -591,15 +591,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
if (args[0].find("/") != std::string::npos) return; if (args[0].find("/") != std::string::npos) return;
if (args[0].find("\\") != std::string::npos) return; if (args[0].find("\\") != std::string::npos) return;
auto buf = Game::assetManager->GetFileAsBuffer(("macros/" + args[0] + ".scm").c_str()); auto infile = Game::assetManager->GetFile(("macros/" + args[0] + ".scm").c_str());
if (!buf.m_Success) { if (!infile) {
ChatPackets::SendSystemMessage(sysAddr, u"Unknown macro! Is the filename right?"); ChatPackets::SendSystemMessage(sysAddr, u"Unknown macro! Is the filename right?");
return; return;
} }
std::istream infile(&buf);
if (infile.good()) { if (infile.good()) {
std::string line; std::string line;
while (std::getline(infile, line)) { while (std::getline(infile, line)) {
@ -609,8 +607,6 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
ChatPackets::SendSystemMessage(sysAddr, u"Unknown macro! Is the filename right?"); ChatPackets::SendSystemMessage(sysAddr, u"Unknown macro! Is the filename right?");
} }
buf.close();
return; return;
} }

View File

@ -19,17 +19,14 @@
Level::Level(Zone* parentZone, const std::string& filepath) { Level::Level(Zone* parentZone, const std::string& filepath) {
m_ParentZone = parentZone; m_ParentZone = parentZone;
auto buffer = Game::assetManager->GetFileAsBuffer(filepath.c_str()); auto stream = Game::assetManager->GetFile(filepath.c_str());
if (!buffer.m_Success) { if (!stream) {
LOG("Failed to load %s", filepath.c_str()); LOG("Failed to load %s", filepath.c_str());
return; return;
} }
std::istream file(&buffer); ReadChunks(stream);
ReadChunks(file);
buffer.close();
} }
void Level::MakeSpawner(SceneObject obj) { void Level::MakeSpawner(SceneObject obj) {

View File

@ -45,14 +45,13 @@ void Zone::LoadZoneIntoMemory() {
m_ZonePath = m_ZoneFilePath.substr(0, m_ZoneFilePath.rfind('/') + 1); m_ZonePath = m_ZoneFilePath.substr(0, m_ZoneFilePath.rfind('/') + 1);
if (m_ZoneFilePath == "ERR") return; if (m_ZoneFilePath == "ERR") return;
AssetMemoryBuffer buffer = Game::assetManager->GetFileAsBuffer(m_ZoneFilePath.c_str()); auto file = Game::assetManager->GetFile(m_ZoneFilePath.c_str());
if (!buffer.m_Success) { if (!file) {
LOG("Failed to load %s", m_ZoneFilePath.c_str()); LOG("Failed to load %s", m_ZoneFilePath.c_str());
throw std::runtime_error("Aborting Zone loading due to no Zone File."); throw std::runtime_error("Aborting Zone loading due to no Zone File.");
} }
std::istream file(&buffer);
if (file) { if (file) {
BinaryIO::BinaryRead(file, m_FileFormatVersion); BinaryIO::BinaryRead(file, m_FileFormatVersion);
@ -148,9 +147,8 @@ void Zone::LoadZoneIntoMemory() {
} else { } else {
LOG("Failed to open: %s", m_ZoneFilePath.c_str()); LOG("Failed to open: %s", m_ZoneFilePath.c_str());
} }
m_ZonePath = m_ZoneFilePath.substr(0, m_ZoneFilePath.rfind('/') + 1);
buffer.close(); m_ZonePath = m_ZoneFilePath.substr(0, m_ZoneFilePath.rfind('/') + 1);
} }
std::string Zone::GetFilePathForZoneID() { std::string Zone::GetFilePathForZoneID() {
@ -243,23 +241,33 @@ void Zone::LoadScene(std::istream& file) {
m_Scenes.insert(std::make_pair(lwoSceneID, scene)); m_Scenes.insert(std::make_pair(lwoSceneID, scene));
} }
<<<<<<< Updated upstream
void Zone::LoadLUTriggers(std::string triggerFile, SceneRef& scene) { void Zone::LoadLUTriggers(std::string triggerFile, SceneRef& scene) {
auto buffer = Game::assetManager->GetFileAsBuffer((m_ZonePath + triggerFile).c_str()); auto buffer = Game::assetManager->GetFileAsBuffer((m_ZonePath + triggerFile).c_str());
=======
std::vector<LUTriggers::Trigger*> Zone::LoadLUTriggers(std::string triggerFile, LWOSCENEID sceneID) {
std::vector<LUTriggers::Trigger*> lvlTriggers;
if (!buffer.m_Success) { auto file = Game::assetManager->GetFile((m_ZonePath + triggerFile).c_str());
>>>>>>> Stashed changes
if (!file) {
LOG("Failed to load %s from disk. Skipping loading triggers", (m_ZonePath + triggerFile).c_str()); LOG("Failed to load %s from disk. Skipping loading triggers", (m_ZonePath + triggerFile).c_str());
return; return;
} }
std::istream file(&buffer);
std::stringstream data; std::stringstream data;
data << file.rdbuf(); data << file.rdbuf();
<<<<<<< Updated upstream
buffer.close(); buffer.close();
data.seekg(0, std::ios::end); data.seekg(0, std::ios::end);
int32_t size = data.tellg(); int32_t size = data.tellg();
data.seekg(0, std::ios::beg); data.seekg(0, std::ios::beg);
=======
if (data.str().size() == 0) return lvlTriggers;
>>>>>>> Stashed changes
if (size == 0) return; if (size == 0) return;