feat: UGC server that makes and serves player models' meshes and icons

A new server (dUgcServer, started by master with enable_ugc_server=1) that
takes unprocessed ugc and ugc_modular_build rows from the database and makes
what the client downloads with UGCUSE3DSERVICES: an optimized NIF (hidden
faces removed, ambient occlusion baked into vertex colors) and a 128px DDS
icon for player models, rendered by a software rasterizer from the client's
LDD brick primitives, and icons for cars and rockets assembled from their
modules per ModularBuildComponent/ModuleComponent. It serves them, with the
models' LXFML, over HTTP in the client's UGCC<dc>/3DOPTIMIZED and
IMAGE128DDS layout with .gz and .checksum files, and keeps its folder under
a size cap.

Processing state lives in ugc.is_optimized plus new processed_at,
process_attempts and process_error columns (and the same on
ugc_modular_build). ServiceType::UGC is appended. NifFile moves to dCommon
and records named node transforms for the modules' attach points.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-27 03:52:22 -05:00
parent 90ded73596
commit abf1cc1d80
52 changed files with 3869 additions and 14 deletions

View File

@@ -6,6 +6,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <vector>
class IUgc {
public:
@@ -31,5 +32,53 @@ public:
virtual void UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) = 0;
virtual std::optional<IUgc::Model> GetUgcModel(const LWOOBJID ugcId) = 0;
// ---- Processing by the UGC server (is_optimized, processed_at, process_attempts, process_error) ----
// is_optimized's values
enum class eProcessState : int32_t {
PENDING = 0, // not made yet (or to be made again)
DONE = 1, // the UGC server's files are made
FAILED = 2, // gave up after the allowed attempts
};
// A row's processing state, for the UGC server and the dashboard
struct ProcessInfo {
LWOOBJID id{};
LWOOBJID characterId{};
std::string characterName; // empty when the character is gone
eProcessState state{};
uint32_t attempts{};
int64_t processedAt{}; // Unix seconds of the last attempt, 0 for none
std::string error;
bool bakeAo{};
std::string details; // modular builds: the modules (ldf_config)
};
// A model waiting to be made: its id, stored LXFML (sd0) and the attempts so far
struct PendingModel {
LWOOBJID id{};
std::string lxfml;
uint32_t attempts{};
};
// Up to `limit` pending models, the least tried and then the newest first
virtual std::vector<PendingModel> GetUgcModelsToProcess(const uint32_t limit) = 0;
// Records an attempt: the new state, how many attempts there have been, why it failed (empty when it didn't) and
// whether lighting was baked in; processed_at becomes now
virtual void SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) = 0;
virtual std::optional<ProcessInfo> GetUgcProcessInfo(const LWOOBJID id) = 0;
// Sets models back to pending with no attempts: one (`id`), or all of them (`id` nullopt; only the failed ones
// with `failedOnly`). Returns how many rows changed.
virtual uint64_t ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) = 0;
// A page of models (all, or those in `state`), the newest first
virtual std::vector<ProcessInfo> GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) = 0;
// How many models are in each state
virtual std::vector<std::pair<eProcessState, uint64_t>> GetUgcProcessCounts() = 0;
};
#endif //!__IUGC__H__

View File

@@ -4,11 +4,30 @@
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include "IUgc.h"
class IUgcModularBuild {
public:
virtual void InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<LWOOBJID> characterId) = 0;
virtual void DeleteUgcBuild(const LWOOBJID bigId) = 0;
// ---- Icons made by the UGC server; the same columns and meanings as IUgc's processing ----
// A modular build waiting for its icon: its id, modules (ldf_config) and the attempts so far
struct PendingBuild {
LWOOBJID id{};
std::string modules;
uint32_t attempts{};
};
virtual std::vector<PendingBuild> GetModularBuildsToProcess(const uint32_t limit) = 0;
virtual void SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) = 0;
virtual std::optional<IUgc::ProcessInfo> GetModularBuildProcessInfo(const LWOOBJID id) = 0;
virtual uint64_t ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) = 0;
virtual std::vector<IUgc::ProcessInfo> GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) = 0;
virtual std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetModularBuildProcessCounts() = 0;
};
#endif //!IUGCMODULARBUILD_H

View File

@@ -393,6 +393,18 @@ public:
nlohmann::json GetCharacterById(const LWOOBJID charId) override;
std::optional<IPropertyContents::Model> GetModel(const LWOOBJID modelID) override;
std::optional<IUgc::Model> GetUgcModel(const LWOOBJID ugcId) override;
std::vector<IUgc::PendingModel> GetUgcModelsToProcess(const uint32_t limit) override;
void SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) override;
std::optional<IUgc::ProcessInfo> GetUgcProcessInfo(const LWOOBJID id) override;
uint64_t ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override;
std::vector<IUgc::ProcessInfo> GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) override;
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetUgcProcessCounts() override;
std::vector<IUgcModularBuild::PendingBuild> GetModularBuildsToProcess(const uint32_t limit) override;
void SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) override;
std::optional<IUgc::ProcessInfo> GetModularBuildProcessInfo(const LWOOBJID id) override;
uint64_t ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override;
std::vector<IUgc::ProcessInfo> GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) override;
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetModularBuildProcessCounts() override;
std::optional<IProperty::Info> GetPropertyInfo(const LWOOBJID id) override;
std::string GetPropertiesTable(uint32_t start, uint32_t length, const std::string_view search = "", uint32_t orderColumn = 0, bool orderAsc = true, bool pendingOnly = false) override;
uint32_t GetPropertyCount() override;

View File

@@ -1,5 +1,27 @@
#include "MySQLDatabase.h"
#include <chrono>
namespace {
IUgc::ProcessInfo ReadUgcProcessInfo(PreparedStmtResultSet& result, bool modular) {
IUgc::ProcessInfo info;
info.id = result->getInt64("id");
info.characterId = result->getInt64("character_id");
info.characterName = std::string(result->getString("character_name").c_str());
info.state = static_cast<IUgc::eProcessState>(result->getInt("is_optimized"));
info.attempts = static_cast<uint32_t>(result->getInt("process_attempts"));
info.processedAt = result->getInt64("processed_at");
info.error = std::string(result->getString("process_error").c_str());
if (modular) info.details = std::string(result->getString("ldf_config").c_str());
else info.bakeAo = result->getInt("bake_ao") != 0;
return info;
}
int64_t UnixNow() {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
}
IUgc::Model ReadModel(PreparedStmtResultSet& result) {
IUgc::Model model;
@@ -67,7 +89,8 @@ void MySQLDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
void MySQLDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) {
const std::istream stream(lxfml.rdbuf());
ExecuteUpdate("UPDATE ugc SET lxfml = ? WHERE id = ?;", &stream, modelId);
// The UGC server makes the model's files again
ExecuteUpdate("UPDATE ugc SET lxfml = ?, is_optimized = 0, process_attempts = 0, process_error = '' WHERE id = ?;", &stream, modelId);
}
std::optional<IUgc::Model> MySQLDatabase::GetUgcModel(const LWOOBJID ugcId) {
@@ -81,3 +104,66 @@ std::optional<IUgc::Model> MySQLDatabase::GetUgcModel(const LWOOBJID ugcId) {
return toReturn;
}
std::vector<IUgc::PendingModel> MySQLDatabase::GetUgcModelsToProcess(const uint32_t limit) {
auto result = ExecuteSelect("SELECT id, lxfml, process_attempts FROM ugc WHERE is_optimized = 0 ORDER BY process_attempts ASC, id DESC LIMIT ?;", limit);
std::vector<IUgc::PendingModel> models;
while (result->next()) {
auto& model = models.emplace_back();
model.id = result->getInt64("id");
std::unique_ptr<std::istream> blob(result->getBlob("lxfml"));
std::stringstream contents;
contents << blob->rdbuf();
model.lxfml = contents.str();
model.attempts = static_cast<uint32_t>(result->getInt("process_attempts"));
}
return models;
}
void MySQLDatabase::SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) {
ExecuteUpdate("UPDATE ugc SET is_optimized = ?, process_attempts = ?, process_error = ?, bake_ao = ?, processed_at = ? WHERE id = ?;",
static_cast<int32_t>(state), attempts, error, bakeAo, UnixNow(), id);
}
std::optional<IUgc::ProcessInfo> MySQLDatabase::GetUgcProcessInfo(const LWOOBJID id) {
auto result = ExecuteSelect(
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao "
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id WHERE u.id = ? LIMIT 1;", id);
if (!result->next()) return std::nullopt;
return ReadUgcProcessInfo(result, false);
}
uint64_t MySQLDatabase::ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) {
if (id) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE id = ?;", *id);
if (failedOnly) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE is_optimized = 2;");
return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '';");
}
std::vector<IUgc::ProcessInfo> MySQLDatabase::GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) {
const std::string select =
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao "
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id ";
std::vector<IUgc::ProcessInfo> list;
auto read = [&list](PreparedStmtResultSet& result) {
while (result->next()) {
list.push_back(ReadUgcProcessInfo(result, false));
}
};
if (state) {
auto result = ExecuteSelect(select + "WHERE u.is_optimized = ? ORDER BY u.id DESC LIMIT ? OFFSET ?;", static_cast<int32_t>(*state), limit, offset);
read(result);
} else {
auto result = ExecuteSelect(select + "ORDER BY u.id DESC LIMIT ? OFFSET ?;", limit, offset);
read(result);
}
return list;
}
std::vector<std::pair<IUgc::eProcessState, uint64_t>> MySQLDatabase::GetUgcProcessCounts() {
auto result = ExecuteSelect("SELECT is_optimized, COUNT(*) AS count FROM ugc GROUP BY is_optimized;");
std::vector<std::pair<eProcessState, uint64_t>> counts;
while (result->next()) {
counts.emplace_back(static_cast<eProcessState>(result->getInt("is_optimized")), static_cast<uint64_t>(result->getInt64("count")));
}
return counts;
}

View File

@@ -1,5 +1,27 @@
#include "MySQLDatabase.h"
#include <chrono>
namespace {
IUgc::ProcessInfo ReadModularProcessInfo(PreparedStmtResultSet& result, bool modular) {
IUgc::ProcessInfo info;
info.id = result->getInt64("id");
info.characterId = result->getInt64("character_id");
info.characterName = std::string(result->getString("character_name").c_str());
info.state = static_cast<IUgc::eProcessState>(result->getInt("is_optimized"));
info.attempts = static_cast<uint32_t>(result->getInt("process_attempts"));
info.processedAt = result->getInt64("processed_at");
info.error = std::string(result->getString("process_error").c_str());
if (modular) info.details = std::string(result->getString("ldf_config").c_str());
else info.bakeAo = result->getInt("bake_ao") != 0;
return info;
}
int64_t UnixNow() {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
}
void MySQLDatabase::InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<LWOOBJID> characterId) {
ExecuteInsert("INSERT INTO ugc_modular_build (ugc_id, ldf_config, character_id) VALUES (?,?,?)", bigId, modules, characterId);
}
@@ -7,3 +29,61 @@ void MySQLDatabase::InsertUgcBuild(const std::string& modules, const LWOOBJID bi
void MySQLDatabase::DeleteUgcBuild(const LWOOBJID bigId) {
ExecuteDelete("DELETE FROM ugc_modular_build WHERE ugc_id = ?;", bigId);
}
std::vector<IUgcModularBuild::PendingBuild> MySQLDatabase::GetModularBuildsToProcess(const uint32_t limit) {
auto result = ExecuteSelect("SELECT ugc_id, ldf_config, process_attempts FROM ugc_modular_build WHERE is_optimized = 0 ORDER BY process_attempts ASC, ugc_id DESC LIMIT ?;", limit);
std::vector<PendingBuild> builds;
while (result->next()) {
builds.push_back({ result->getInt64("ugc_id"), std::string(result->getString("ldf_config").c_str()), static_cast<uint32_t>(result->getInt("process_attempts")) });
}
return builds;
}
void MySQLDatabase::SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) {
ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = ?, process_attempts = ?, process_error = ?, processed_at = ? WHERE ugc_id = ?;",
static_cast<int32_t>(state), attempts, error, UnixNow(), id);
}
namespace {
const std::string MODULAR_SELECT =
"SELECT b.ugc_id AS id, b.character_id, c.name AS character_name, b.is_optimized, b.process_attempts, b.processed_at, b.process_error, b.ldf_config "
"FROM ugc_modular_build AS b LEFT JOIN charinfo AS c ON c.id = b.character_id ";
}
std::optional<IUgc::ProcessInfo> MySQLDatabase::GetModularBuildProcessInfo(const LWOOBJID id) {
auto result = ExecuteSelect(MODULAR_SELECT + "WHERE b.ugc_id = ? LIMIT 1;", id);
if (!result->next()) return std::nullopt;
return ReadModularProcessInfo(result, true);
}
uint64_t MySQLDatabase::ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) {
if (id) return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE ugc_id = ?;", *id);
if (failedOnly) return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE is_optimized = 2;");
return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '';");
}
std::vector<IUgc::ProcessInfo> MySQLDatabase::GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) {
std::vector<IUgc::ProcessInfo> list;
auto read = [&list](PreparedStmtResultSet& result) {
while (result->next()) {
list.push_back(ReadModularProcessInfo(result, true));
}
};
if (state) {
auto result = ExecuteSelect(MODULAR_SELECT + "WHERE b.is_optimized = ? ORDER BY b.ugc_id DESC LIMIT ? OFFSET ?;", static_cast<int32_t>(*state), limit, offset);
read(result);
} else {
auto result = ExecuteSelect(MODULAR_SELECT + "ORDER BY b.ugc_id DESC LIMIT ? OFFSET ?;", limit, offset);
read(result);
}
return list;
}
std::vector<std::pair<IUgc::eProcessState, uint64_t>> MySQLDatabase::GetModularBuildProcessCounts() {
auto result = ExecuteSelect("SELECT is_optimized, COUNT(*) AS count FROM ugc_modular_build GROUP BY is_optimized;");
std::vector<std::pair<IUgc::eProcessState, uint64_t>> counts;
while (result->next()) {
counts.emplace_back(static_cast<IUgc::eProcessState>(result->getInt("is_optimized")), static_cast<uint64_t>(result->getInt64("count")));
}
return counts;
}

View File

@@ -377,6 +377,18 @@ public:
nlohmann::json GetCharacterById(const LWOOBJID charId) override;
std::optional<IPropertyContents::Model> GetModel(const LWOOBJID modelID) override;
std::optional<IUgc::Model> GetUgcModel(const LWOOBJID ugcId) override;
std::vector<IUgc::PendingModel> GetUgcModelsToProcess(const uint32_t limit) override;
void SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) override;
std::optional<IUgc::ProcessInfo> GetUgcProcessInfo(const LWOOBJID id) override;
uint64_t ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override;
std::vector<IUgc::ProcessInfo> GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) override;
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetUgcProcessCounts() override;
std::vector<IUgcModularBuild::PendingBuild> GetModularBuildsToProcess(const uint32_t limit) override;
void SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) override;
std::optional<IUgc::ProcessInfo> GetModularBuildProcessInfo(const LWOOBJID id) override;
uint64_t ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override;
std::vector<IUgc::ProcessInfo> GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) override;
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetModularBuildProcessCounts() override;
std::optional<IProperty::Info> GetPropertyInfo(const LWOOBJID id) override;
std::string GetPropertiesTable(uint32_t start, uint32_t length, const std::string_view search = "", uint32_t orderColumn = 0, bool orderAsc = true, bool pendingOnly = false) override;
uint32_t GetPropertyCount() override;

View File

@@ -1,5 +1,27 @@
#include "SQLiteDatabase.h"
#include <chrono>
namespace {
IUgc::ProcessInfo ReadUgcProcessInfo(CppSQLite3Query& result, bool modular) {
IUgc::ProcessInfo info;
info.id = result.getInt64Field("id");
info.characterId = result.getInt64Field("character_id");
info.characterName = result.getStringField("character_name", "");
info.state = static_cast<IUgc::eProcessState>(result.getIntField("is_optimized"));
info.attempts = static_cast<uint32_t>(result.getIntField("process_attempts"));
info.processedAt = result.getInt64Field("processed_at");
info.error = result.getStringField("process_error", "");
if (modular) info.details = result.getStringField("ldf_config", "");
else info.bakeAo = result.getIntField("bake_ao") != 0;
return info;
}
int64_t UnixNow() {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
}
IUgc::Model ReadModel(CppSQLite3Query& result) {
IUgc::Model model;
@@ -68,7 +90,8 @@ void SQLiteDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
void SQLiteDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) {
const std::istream stream(lxfml.rdbuf());
ExecuteUpdate("UPDATE ugc SET lxfml = ? WHERE id = ?;", &stream, modelId);
// The UGC server makes the model's files again
ExecuteUpdate("UPDATE ugc SET lxfml = ?, is_optimized = 0, process_attempts = 0, process_error = '' WHERE id = ?;", &stream, modelId);
}
std::optional<IUgc::Model> SQLiteDatabase::GetUgcModel(const LWOOBJID ugcId) {
@@ -82,3 +105,68 @@ std::optional<IUgc::Model> SQLiteDatabase::GetUgcModel(const LWOOBJID ugcId) {
return toReturn;
}
std::vector<IUgc::PendingModel> SQLiteDatabase::GetUgcModelsToProcess(const uint32_t limit) {
auto [_, result] = ExecuteSelect("SELECT id, lxfml, process_attempts FROM ugc WHERE is_optimized = 0 ORDER BY process_attempts ASC, id DESC LIMIT ?;", limit);
std::vector<IUgc::PendingModel> models;
while (!result.eof()) {
auto& model = models.emplace_back();
model.id = result.getInt64Field("id");
int blobSize{};
const auto* blob = result.getBlobField("lxfml", blobSize);
model.lxfml.assign(reinterpret_cast<const char*>(blob), blobSize);
model.attempts = static_cast<uint32_t>(result.getIntField("process_attempts"));
result.nextRow();
}
return models;
}
void SQLiteDatabase::SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) {
ExecuteUpdate("UPDATE ugc SET is_optimized = ?, process_attempts = ?, process_error = ?, bake_ao = ?, processed_at = ? WHERE id = ?;",
static_cast<int32_t>(state), attempts, error, bakeAo, UnixNow(), id);
}
std::optional<IUgc::ProcessInfo> SQLiteDatabase::GetUgcProcessInfo(const LWOOBJID id) {
auto [_, result] = ExecuteSelect(
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao "
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id WHERE u.id = ? LIMIT 1;", id);
if (result.eof()) return std::nullopt;
return ReadUgcProcessInfo(result, false);
}
uint64_t SQLiteDatabase::ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) {
if (id) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE id = ?;", *id);
if (failedOnly) return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE is_optimized = 2;");
return ExecuteUpdate("UPDATE ugc SET is_optimized = 0, process_attempts = 0, process_error = '';");
}
std::vector<IUgc::ProcessInfo> SQLiteDatabase::GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) {
const std::string select =
"SELECT u.id, u.character_id, c.name AS character_name, u.is_optimized, u.process_attempts, u.processed_at, u.process_error, u.bake_ao "
"FROM ugc AS u LEFT JOIN charinfo AS c ON c.id = u.character_id ";
std::vector<IUgc::ProcessInfo> list;
auto read = [&list](CppSQLite3Query& result) {
while (!result.eof()) {
list.push_back(ReadUgcProcessInfo(result, false));
result.nextRow();
}
};
if (state) {
auto [_, result] = ExecuteSelect(select + "WHERE u.is_optimized = ? ORDER BY u.id DESC LIMIT ? OFFSET ?;", static_cast<int32_t>(*state), limit, offset);
read(result);
} else {
auto [_, result] = ExecuteSelect(select + "ORDER BY u.id DESC LIMIT ? OFFSET ?;", limit, offset);
read(result);
}
return list;
}
std::vector<std::pair<IUgc::eProcessState, uint64_t>> SQLiteDatabase::GetUgcProcessCounts() {
auto [_, result] = ExecuteSelect("SELECT is_optimized, COUNT(*) AS count FROM ugc GROUP BY is_optimized;");
std::vector<std::pair<eProcessState, uint64_t>> counts;
while (!result.eof()) {
counts.emplace_back(static_cast<eProcessState>(result.getIntField("is_optimized")), static_cast<uint64_t>(result.getInt64Field("count")));
result.nextRow();
}
return counts;
}

View File

@@ -1,5 +1,27 @@
#include "SQLiteDatabase.h"
#include <chrono>
namespace {
IUgc::ProcessInfo ReadModularProcessInfo(CppSQLite3Query& result, bool modular) {
IUgc::ProcessInfo info;
info.id = result.getInt64Field("id");
info.characterId = result.getInt64Field("character_id");
info.characterName = result.getStringField("character_name", "");
info.state = static_cast<IUgc::eProcessState>(result.getIntField("is_optimized"));
info.attempts = static_cast<uint32_t>(result.getIntField("process_attempts"));
info.processedAt = result.getInt64Field("processed_at");
info.error = result.getStringField("process_error", "");
if (modular) info.details = result.getStringField("ldf_config", "");
else info.bakeAo = result.getIntField("bake_ao") != 0;
return info;
}
int64_t UnixNow() {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
}
void SQLiteDatabase::InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<LWOOBJID> characterId) {
ExecuteInsert("INSERT INTO ugc_modular_build (ugc_id, ldf_config, character_id) VALUES (?,?,?)", bigId, modules, characterId);
}
@@ -7,3 +29,64 @@ void SQLiteDatabase::InsertUgcBuild(const std::string& modules, const LWOOBJID b
void SQLiteDatabase::DeleteUgcBuild(const LWOOBJID bigId) {
ExecuteDelete("DELETE FROM ugc_modular_build WHERE ugc_id = ?;", bigId);
}
std::vector<IUgcModularBuild::PendingBuild> SQLiteDatabase::GetModularBuildsToProcess(const uint32_t limit) {
auto [_, result] = ExecuteSelect("SELECT ugc_id, ldf_config, process_attempts FROM ugc_modular_build WHERE is_optimized = 0 ORDER BY process_attempts ASC, ugc_id DESC LIMIT ?;", limit);
std::vector<PendingBuild> builds;
while (!result.eof()) {
builds.push_back({ result.getInt64Field("ugc_id"), result.getStringField("ldf_config", ""), static_cast<uint32_t>(result.getIntField("process_attempts")) });
result.nextRow();
}
return builds;
}
void SQLiteDatabase::SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) {
ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = ?, process_attempts = ?, process_error = ?, processed_at = ? WHERE ugc_id = ?;",
static_cast<int32_t>(state), attempts, error, UnixNow(), id);
}
namespace {
const std::string MODULAR_SELECT =
"SELECT b.ugc_id AS id, b.character_id, c.name AS character_name, b.is_optimized, b.process_attempts, b.processed_at, b.process_error, b.ldf_config "
"FROM ugc_modular_build AS b LEFT JOIN charinfo AS c ON c.id = b.character_id ";
}
std::optional<IUgc::ProcessInfo> SQLiteDatabase::GetModularBuildProcessInfo(const LWOOBJID id) {
auto [_, result] = ExecuteSelect(MODULAR_SELECT + "WHERE b.ugc_id = ? LIMIT 1;", id);
if (result.eof()) return std::nullopt;
return ReadModularProcessInfo(result, true);
}
uint64_t SQLiteDatabase::ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) {
if (id) return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE ugc_id = ?;", *id);
if (failedOnly) return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '' WHERE is_optimized = 2;");
return ExecuteUpdate("UPDATE ugc_modular_build SET is_optimized = 0, process_attempts = 0, process_error = '';");
}
std::vector<IUgc::ProcessInfo> SQLiteDatabase::GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) {
std::vector<IUgc::ProcessInfo> list;
auto read = [&list](CppSQLite3Query& result) {
while (!result.eof()) {
list.push_back(ReadModularProcessInfo(result, true));
result.nextRow();
}
};
if (state) {
auto [_, result] = ExecuteSelect(MODULAR_SELECT + "WHERE b.is_optimized = ? ORDER BY b.ugc_id DESC LIMIT ? OFFSET ?;", static_cast<int32_t>(*state), limit, offset);
read(result);
} else {
auto [_, result] = ExecuteSelect(MODULAR_SELECT + "ORDER BY b.ugc_id DESC LIMIT ? OFFSET ?;", limit, offset);
read(result);
}
return list;
}
std::vector<std::pair<IUgc::eProcessState, uint64_t>> SQLiteDatabase::GetModularBuildProcessCounts() {
auto [_, result] = ExecuteSelect("SELECT is_optimized, COUNT(*) AS count FROM ugc_modular_build GROUP BY is_optimized;");
std::vector<std::pair<IUgc::eProcessState, uint64_t>> counts;
while (!result.eof()) {
counts.emplace_back(static_cast<IUgc::eProcessState>(result.getIntField("is_optimized")), static_cast<uint64_t>(result.getInt64Field("count")));
result.nextRow();
}
return counts;
}

View File

@@ -372,6 +372,18 @@ class TestSQLDatabase : public GameDatabase {
std::optional<IPropertyContents::Model> GetModel(const LWOOBJID modelID) override { return {}; }
std::optional<IProperty::Info> GetPropertyInfo(const LWOOBJID id) override { return {}; }
std::optional<IUgc::Model> GetUgcModel(const LWOOBJID ugcId) override { return {}; }
std::vector<IUgc::PendingModel> GetUgcModelsToProcess(const uint32_t limit) override { return {}; }
void SetUgcModelProcessed(const LWOOBJID id, const eProcessState state, const uint32_t attempts, const std::string_view error, const bool bakeAo) override {}
std::optional<IUgc::ProcessInfo> GetUgcProcessInfo(const LWOOBJID id) override { return {}; }
uint64_t ResetUgcModelProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override { return 0; }
std::vector<IUgc::ProcessInfo> GetUgcProcessList(const std::optional<eProcessState> state, const uint32_t offset, const uint32_t limit) override { return {}; }
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetUgcProcessCounts() override { return {}; }
std::vector<IUgcModularBuild::PendingBuild> GetModularBuildsToProcess(const uint32_t limit) override { return {}; }
void SetModularBuildProcessed(const LWOOBJID id, const IUgc::eProcessState state, const uint32_t attempts, const std::string_view error) override {}
std::optional<IUgc::ProcessInfo> GetModularBuildProcessInfo(const LWOOBJID id) override { return {}; }
uint64_t ResetModularBuildProcessing(const std::optional<LWOOBJID> id, const bool failedOnly) override { return 0; }
std::vector<IUgc::ProcessInfo> GetModularBuildProcessList(const std::optional<IUgc::eProcessState> state, const uint32_t offset, const uint32_t limit) override { return {}; }
std::vector<std::pair<IUgc::eProcessState, uint64_t>> GetModularBuildProcessCounts() override { return {}; }
void DeleteAccount(const uint32_t accountId) override {};
void InsertAuditLog(uint32_t accountId, const std::string_view accountName, const std::string_view action, const std::string_view description, uint32_t targetAccountId, LWOOBJID targetCharacterId) override {};
std::string GetAuditLogTable(uint32_t start, uint32_t length, const std::string_view search = "", uint32_t orderColumn = 0, bool orderAsc = true) override { return "{}"; };