feat: migrate old code to project

This commit is contained in:
2024-08-01 22:38:55 +08:00
parent ef1261e349
commit 6736bfbde5
16 changed files with 622 additions and 2 deletions

View File

@@ -11,9 +11,17 @@ endif ()
target_sources(VSWMaterializer
PRIVATE
# Sources
# Main
main.cpp
PluginMain.cpp
StandaloneMain.cpp
# Exporter
ExportContext.cpp
ExportEnvironment.cpp
ExportScript.cpp
# Utilities
Utilities.cpp
Database.cpp
# Defination file
"$<$<STREQUAL:${MATERIALIZER_BUILD_TYPE},plugin>:Materializer.def>"
)
@@ -23,10 +31,20 @@ PRIVATE
FILE_SET HEADERS
FILES
# Headers
# Misc
stdafx.hpp
resource.h
# Main
PluginMain.hpp
StandaloneMain.hpp
# Exporter
ExportContext.hpp
ExportEnvironment.hpp
ExportScript.hpp
# Utilities
Utilities.hpp
Database.hpp
DataTypes.hpp
)
# Setup header infomations
target_include_directories(VSWMaterializer

224
materializer/DataTypes.hpp Normal file
View File

@@ -0,0 +1,224 @@
#pragma once
#include "stdafx.hpp"
namespace VSW::Materializer::DataTypes {
namespace Script {
struct Table_script {
CK_ID thisobj;
std::string host_name;
int index;
CK_ID behavior;
};
struct Table_behavior {
CK_ID thisobj;
std::string name;
CK_BEHAVIOR_TYPE type;
std::string proto_name;
std::string proto_guid;
CK_BEHAVIOR_FLAGS flags;
int priority;
CKDWORD version;
//pTarget, pIn, pOut, bIn, bOut
std::string pin_count;
CK_ID parent;
};
struct Table_bIO {
CK_ID thisobj;
int index;
std::string name;
CK_ID parent;
};
struct Table_bIn : public Table_bIO {};
struct Table_bOut : public Table_bIO {};
struct Table_pTarget {
CK_ID thisobj;
std::string name;
std::string type;
std::string type_guid;
CK_ID parent;
CK_ID direct_source;
CK_ID shared_source;
};
struct Table_pIn {
CK_ID thisobj;
int index;
std::string name;
std::string type;
std::string type_guid;
CK_ID parent;
CK_ID direct_source;
CK_ID shared_source;
};
struct Table_pOut {
CK_ID thisobj;
int index;
std::string name;
std::string type;
std::string type_guid;
CK_ID parent;
};
struct Table_bLink {
CK_ID input;
CK_ID output;
int delay;
CK_ID parent;
// additional field
CK_ID input_obj;
bLinkInputOutputType input_type;
int input_index;
CK_ID output_obj;
bLinkInputOutputType output_type;
int output_index;
};
struct Table_pLocal {
CK_ID thisobj;
std::string name;
std::string type;
std::string type_guid;
BOOL is_setting;
CK_ID parent;
};
struct Table_pAttr {
CK_ID thisobj;
std::string name;
std::string type;
std::string type_guid;
};
struct Table_pLink {
CK_ID input;
CK_ID output;
CK_ID parent;
//additional field
CK_ID input_obj;
pLinkInputOutputType input_type;
BOOL input_is_bb;
int input_index;
CK_ID output_obj;
pLinkInputOutputType output_type;
BOOL output_is_bb;
int output_index;
};
struct Table_pOper {
CK_ID thisobj;
std::string op;
std::string op_guid;
CK_ID parent;
};
struct Table_eLink {
CK_ID export_obj;
CK_ID internal_obj;
BOOL is_in;
int index;
CK_ID parent;
};
struct Table_data {
std::string field;
std::string data;
CK_ID parent;
};
}
namespace Context {
struct Table_msg {
CKMessageType index;
std::string name;
};
struct Table_obj {
CK_ID id;
std::string name;
CK_CLASSID classid;
std::string classtype;
};
}
namespace Environment {
struct Table_op {
CK_PARAMETEROPERATION funcPtr;
std::string in1_guid;
std::string in2_guid;
std::string out_guid;
std::string op_guid;
std::string op_name;
CKOperationType op_code;
};
struct Table_param {
CKParameterType index;
std::string guid;
std::string derived_from;
std::string type_name;
int default_size;
CK_PARAMETERCREATEDEFAULTFUNCTION func_CreateDefault;
CK_PARAMETERDELETEFUNCTION func_Delete;
CK_PARAMETERSAVELOADFUNCTION func_SaveLoad;
CK_PARAMETERCHECKFUNCTION func_Check;
CK_PARAMETERCOPYFUNCTION func_Copy;
CK_PARAMETERSTRINGFUNCTION func_String;
CK_PARAMETERUICREATORFUNCTION func_UICreator;
int creator_dll_index;
int creator_plugin_index;
CKDWORD dw_param;
CKDWORD dw_flags;
CKDWORD cid;
std::string saver_manager;
};
struct Table_attr {
CKAttributeType index;
std::string name;
CKAttributeCategory category_index;
std::string category_name;
CK_ATTRIBUT_FLAGS flags;
CKParameterType param_index;
CK_CLASSID compatible_classid;
std::string default_value;
};
struct Table_plugin {
int dll_index;
std::string dll_name;
int plugin_index;
std::string category;
CKBOOL active;
std::string guid;
std::string desc;
std::string author;
std::string summary;
DWORD version;
CK_INITINSTANCEFCT func_init;
CK_EXITINSTANCEFCT func_exit;
};
struct Table_variable {
std::string name;
std::string desciption;
XWORD flags;
UNIVERSAL_VAR_TYPE type;
std::string representation;
std::string data;
};
}
}

217
materializer/Database.cpp Normal file
View File

@@ -0,0 +1,217 @@
#include "Database.hpp"
#include <stdexcept>
namespace VSW::Materializer::Database {
#pragma region Help Macros
#define BEGIN_SAFE_SQL_EXEC { int errcode;
#define SAFE_SQL_EXEC(sql) errcode = sqlite3_exec(this->GetDb(), sql, nullptr, nullptr, nullptr); \
if (errcode != SQLITE_OK) return false;
#define END_SAFE_SQL_EXEC }
#pragma endregion
#pragma region Abstract Database
AbstractDatabase::AbstractDatabase(const YYCC::yycc_u8string_view& file) :
m_Db(nullptr), m_StmtCache() {
// connect to database (the argument of sqlite3 open function is UTF8)
int errcode;
if (file.empty()) goto failed;
errcode = sqlite3_open(YYCC::EncodingHelper::ToOrdinary(file.data()), &m_Db);
if (errcode != SQLITE_OK) goto failed;
// disable synchronous to accelerate speed.
errcode = sqlite3_exec(m_Db, "PRAGMA synchronous = OFF;", nullptr, nullptr, nullptr);
if (errcode != SQLITE_OK) goto failed_after_open;
// run extra stuff
if (!PostOpen()) goto failed_after_open;
// start transaction
errcode = sqlite3_exec(m_Db, "begin;", nullptr, nullptr, nullptr);
if (errcode != SQLITE_OK) goto failed_after_open;
return;
failed_after_open:
sqlite3_close(m_Db);
failed:
m_Db = nullptr;
}
AbstractDatabase::~AbstractDatabase() {
if (m_Db == nullptr) return;
int errcode;
// free all cached stmts
for (const auto& pair : m_StmtCache) {
if (pair.second != nullptr) {
errcode = sqlite3_finalize(pair.second);
if (errcode != SQLITE_OK) goto failed;
}
}
// commit transaction
errcode = sqlite3_exec(m_Db, "commit;", nullptr, nullptr, nullptr);
if (errcode != SQLITE_OK) goto failed;
// run extra stuff
if (!PreClose()) goto failed;
failed:
// directly release resources
sqlite3_close(m_Db);
m_Db = nullptr;
}
bool AbstractDatabase::IsValid() const {
return m_Db != nullptr;
}
sqlite3_stmt* AbstractDatabase::GetStmt(const char* stmt_str) {
// try to find matched cache
auto finder = m_StmtCache.find(stmt_str);
if (finder != m_StmtCache.end()) {
return finder->second;
}
// no found. create one
sqlite3_stmt* stmt = nullptr;
int errcode = sqlite3_prepare_v2(m_Db, stmt_str, -1, &stmt, nullptr);
if (errcode != SQLITE_OK) throw std::runtime_error("fail to create sqlite3 stmt!");
// append new one
m_StmtCache.emplace(stmt_str, stmt);
return stmt;
}
sqlite3* AbstractDatabase::GetDb() {
return m_Db;
}
#pragma endregion
#pragma region Script Database
ScriptDatabase::ScriptDatabase(const YYCC::yycc_u8string_view& file) :
AbstractDatabase(file) {}
ScriptDatabase::~ScriptDatabase() {}
bool ScriptDatabase::PostOpen() {
// initialize table
BEGIN_SAFE_SQL_EXEC;
SAFE_SQL_EXEC("begin;");
SAFE_SQL_EXEC("CREATE TABLE [script] ([thisobj] INTEGER, [name] TEXT, [index] INTEGER, [behavior] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [behavior] ([thisobj] INTEGER, [name] TEXT, [type] INTEGER, [proto_name] TEXT, [proto_guid] TEXT, [flags] INTEGER, [priority] INTEGER, [version] INTEGER, [pin_count] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pTarget] ([thisobj] INTEGER, [name] TEXT, [type] TEXT, [type_guid] TEXT, [parent] INTEGER, [direct_source] INTEGER, [shard_source] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pIn] ([thisobj] INTEGER, [index] INTEGER, [name] TEXT, [type] TEXT, [type_guid] TEXT, [parent] INTEGER, [direct_source] INTEGER, [shared_source] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pOut] ([thisobj] INTEGER, [index] INTEGER, [name] TEXT, [type] TEXT, [type_guid] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [bIn] ([thisobj] INTEGER, [index] INTEGER, [name] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [bOut] ([thisobj] INTEGER, [index] INTEGER, [name] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [bLink] ([input] INTEGER, [output] INTEGER, [delay] INTEGER, [input_obj] INTEGER, [input_type] INTEGER, [input_index] INETEGR, [output_obj] INTEGER, [output_type] INTEGER, [output_index] INETEGR, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pLocal] ([thisobj] INTEGER, [name] TEXT, [type] TEXT, [type_guid] TEXT, [is_setting] INTEGER, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pLink] ([input] INTEGER, [output] INTEGER, [input_obj] INTEGER, [input_type] INTEGER, [input_is_bb] INTEGER, [input_index] INETEGR, [output_obj] INTEGER, [output_type] INTEGER, [output_is_bb] INTEGER, [output_index] INETEGR, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pOper] ([thisobj] INTEGER, [op] TEXT, [op_guid] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [eLink] ([export_obj] INTEGER, [internal_obj] INTEGER, [is_in] INTEGER, [index] INTEGER, [parent] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [pAttr] ([thisobj] INTEGER, [name] TEXT, [type] TEXT, [type_guid] TEXT);");
//SAFE_SQL_EXEC("CREATE TABLE [msg] ([index] INTEGER, [name] TEXT);");
//SAFE_SQL_EXEC("CREATE TABLE [obj] ([id] INTEGER, [name] TEXT, [classid] INTEGER, [classtype] TEXT);");
SAFE_SQL_EXEC("CREATE TABLE [data] ([field] TEXT, [data] TEXT, [parent] INTEGER);");
SAFE_SQL_EXEC("commit;");
END_SAFE_SQL_EXEC;
return true;
}
bool ScriptDatabase::PreClose() {
// create index for quick select in following process
BEGIN_SAFE_SQL_EXEC;
SAFE_SQL_EXEC("begin;");
SAFE_SQL_EXEC("CREATE INDEX [quick_where1] ON [behavior] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where2] ON [pOper] ([parent], [thisobj])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where3] ON [pTarget] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where4] ON [bIn] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where5] ON [bOut] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where6] ON [pIn] ([parent], [thisobj])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where7] ON [pOut] ([parent], [thisobj])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where8] ON [pLocal] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where9] ON [pLink] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where10] ON [bLink] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where11] ON [elink] ([parent])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where12] ON [pAttr] ([thisobj])");
SAFE_SQL_EXEC("CREATE INDEX [quick_where13] ON [data] ([parent])");
SAFE_SQL_EXEC("commit;");
END_SAFE_SQL_EXEC;
return true;
}
#pragma endregion
#pragma region Context Database
ContextDatabase::ContextDatabase(const YYCC::yycc_u8string_view& file) :
AbstractDatabase(file) {}
ContextDatabase::~ContextDatabase() {}
bool ContextDatabase::PostOpen() {
// initialize table
BEGIN_SAFE_SQL_EXEC;
SAFE_SQL_EXEC("begin;");
SAFE_SQL_EXEC("CREATE TABLE [msg] ([index] INTEGER, [name] TEXT);");
SAFE_SQL_EXEC("CREATE TABLE [obj] ([id] INTEGER, [name] TEXT, [classid] INTEGER, [classtype] TEXT);");
SAFE_SQL_EXEC("commit;");
END_SAFE_SQL_EXEC;
return true;
}
bool ContextDatabase::PreClose() {
// do nothing
return true;
}
#pragma endregion
#pragma region Environment Database
EnvironmentDatabase::EnvironmentDatabase(const YYCC::yycc_u8string_view& file) :
AbstractDatabase(file) {}
EnvironmentDatabase::~EnvironmentDatabase() {}
bool EnvironmentDatabase::PostOpen() {
// initialize table
BEGIN_SAFE_SQL_EXEC;
SAFE_SQL_EXEC("begin;");
SAFE_SQL_EXEC("CREATE TABLE [op] ([funcptr] INTEGER, [in1_guid] TEXT, [in2_guid] TEXT, [out_guid] TEXT, [op_guid] TEXT, [op_name] TEXT, [op_code] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [param] ([index] INTEGER, [guid] TEXT, [derived_from] TEXT, [type_name] TEXT, [default_size] INTEGER, [func_CreateDefault] INTEGER, [func_Delete] INTEGER, [func_SaveLoad] INTEGER, [func_Check] INTEGER, [func_Copy] INTEGER, [func_String] INTEGER, [func_UICreator] INTEGER, [creator_dll_index] INTEGER, [creator_plugin_index] INTEGER, [dw_param] INTEGER, [dw_flags] INTEGER, [cid] INTEGER, [saver_manager] TEXT);");
SAFE_SQL_EXEC("CREATE TABLE [attr] ([index] INTEGER, [name] TEXT, [category_index] INTEGER, [category_name] TEXT, [flags] INTEGER, [param_index] INTEGER, [compatible_classid] INTEGER, [default_value] TEXT);");
SAFE_SQL_EXEC("CREATE TABLE [plugin] ([dll_index] INTEGER, [dll_name] TEXT, [plugin_index] INTEGER, [category] TEXT, [active] INTEGER, [guid] TEXT, [desc] TEXT, [author] TEXT, [summary] TEXT, [version] INTEGER, [func_init] INTEGER, [func_exit] INTEGER);");
SAFE_SQL_EXEC("CREATE TABLE [variable] ([name] TEXT, [description] TEXT, [flags] INTEGER, [type] INTEGER, [representation] TEXT, [data] TEXT);");
SAFE_SQL_EXEC("commit;");
END_SAFE_SQL_EXEC;
return true;
}
bool EnvironmentDatabase::PreClose() {
// do nothing
return true;
}
#pragma endregion
}

95
materializer/Database.hpp Normal file
View File

@@ -0,0 +1,95 @@
#pragma once
#include "stdafx.hpp"
#include <sqlite3.h>
#include <map>
namespace VSW::Materializer::Database {
class AbstractDatabase {
public:
AbstractDatabase(const YYCC::yycc_u8string_view& file);
virtual ~AbstractDatabase();
AbstractDatabase(const AbstractDatabase&) = delete;
AbstractDatabase& operator=(const AbstractDatabase&) = delete;
AbstractDatabase(AbstractDatabase&&) = delete;
AbstractDatabase& operator=(AbstractDatabase&&) = delete;
public:
/// @brief Check whether this database is ready for use.
bool IsValid() const;
protected:
/**
* @brief User implemented function called after connect to the database before transition.
* @return True if success, otherwise false.
*/
virtual bool PostOpen() = 0;
/**
* @brief User implemented function called after transition before disconnect from database.
* @return True if success, otherwise false.
*/
virtual bool PreClose() = 0;
protected:
/// @brief Subclass used for creating stmt with cache feature.
sqlite3_stmt* GetStmt(const char* stmt_str);
/// @brief Subclass used for get pointer to opened database.
sqlite3* GetDb();
private:
sqlite3* m_Db;
std::map<const void*, sqlite3_stmt*> m_StmtCache;
};
class ScriptDatabase : public AbstractDatabase {
public:
ScriptDatabase(const YYCC::yycc_u8string_view& file);
virtual ~ScriptDatabase();
ScriptDatabase(const ScriptDatabase&) = delete;
ScriptDatabase& operator=(const ScriptDatabase&) = delete;
ScriptDatabase(ScriptDatabase&&) = delete;
ScriptDatabase& operator=(ScriptDatabase&&) = delete;
protected:
virtual bool PostOpen() override;
virtual bool PreClose() override;
public:
};
class ContextDatabase : public AbstractDatabase {
public:
ContextDatabase(const YYCC::yycc_u8string_view& file);
virtual ~ContextDatabase();
ContextDatabase(const ContextDatabase&) = delete;
ContextDatabase& operator=(const ContextDatabase&) = delete;
ContextDatabase(ContextDatabase&&) = delete;
ContextDatabase& operator=(ContextDatabase&&) = delete;
protected:
virtual bool PostOpen() override;
virtual bool PreClose() override;
public:
};
class EnvironmentDatabase : public AbstractDatabase {
public:
EnvironmentDatabase(const YYCC::yycc_u8string_view& file);
virtual ~EnvironmentDatabase();
EnvironmentDatabase(const EnvironmentDatabase&) = delete;
EnvironmentDatabase& operator=(const EnvironmentDatabase&) = delete;
EnvironmentDatabase(EnvironmentDatabase&&) = delete;
EnvironmentDatabase& operator=(EnvironmentDatabase&&) = delete;
protected:
virtual bool PostOpen() override;
virtual bool PreClose() override;
public:
};
}

View File

@@ -0,0 +1,5 @@
#include "ExportContext.hpp"
namespace VSW::Materializer::ExportContext {
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include "stdafx.hpp"
namespace VSW::Materializer::ExportContext {
}

View File

@@ -0,0 +1,5 @@
#include "ExportEnvironment.hpp"
namespace VSW::Materializer::ExportEnvironment {
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include "stdafx.hpp"
namespace VSW::Materializer::ExportEnvironment {
}

View File

@@ -0,0 +1,5 @@
#include "ExportScript.hpp"
namespace VSW::Materializer::ExportScript {
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include "stdafx.hpp"
namespace VSW::Materializer::ExportScript {
}

View File

@@ -0,0 +1,5 @@
#include "Utilities.hpp"
namespace VSW::Materializer::Utilities {
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include "stdafx.hpp"
namespace VSW::Materializer::Utilities {
}