libcmo21/LibCmo/CKMinContext.cpp

98 lines
4.3 KiB
C++
Raw Normal View History

2023-02-25 22:58:28 +08:00
#include "VTUtils.hpp"
#include "CKMinContext.hpp"
2023-02-27 22:23:05 +08:00
#include "CKObjects.hpp"
#include "CKManagers.hpp"
2023-02-25 22:58:28 +08:00
2023-02-26 21:48:03 +08:00
namespace LibCmo::CK2 {
2023-02-25 22:58:28 +08:00
#if defined(LIBCMO_OS_WIN32)
static wchar_t g_UniqueFolder[] = L"LibCmo";
#else
static char g_UniqueFolder[] = "LibCmo";
#endif
CKMinContext::CKMinContext() :
2023-02-26 21:48:03 +08:00
m_NameEncoding(), m_NameEncodingToken(EncodingHelper::ENCODING_TOKEN_DEFAULT),
m_TempFolder(),
2023-02-27 22:23:05 +08:00
m_PrintCallback(nullptr),
// register CKObjects
m_ObjectsCreationMap{
{CK_CLASSID::CKCID_OBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKObject(ctx, id, name); })},
{CK_CLASSID::CKCID_SCENEOBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKSceneObject(ctx, id, name); })},
{CK_CLASSID::CKCID_BEOBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKBeObject(ctx, id, name); })},
{CK_CLASSID::CKCID_GROUP, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKGroup(ctx, id, name); })},
{CK_CLASSID::CKCID_MESH, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMesh(ctx, id, name); })},
{CK_CLASSID::CKCID_TEXTURE, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKTexture(ctx, id, name); })},
{CK_CLASSID::CKCID_MATERIAL, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKMaterial(ctx, id, name); })},
{CK_CLASSID::CKCID_RENDEROBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CKRenderObject(ctx, id, name); })},
{CK_CLASSID::CKCID_3DENTITY, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CK3dEntity(ctx, id, name); })},
{CK_CLASSID::CKCID_3DOBJECT, ([](CKMinContext* ctx, CK_ID id, CKSTRING name) ->CKObjectImplements::CKObject* { return new(std::nothrow) CKObjectImplements::CK3dObject(ctx, id, name); })}
},
// register CKBaseManagers
m_ManagersCreationMap{
{PredefinedGuids::ATTRIBUTE_MANAGER_GUID, ([](CKMinContext* ctx, CK_ID id) ->CKManagerImplements::CKBaseManager* { return new(std::nothrow) CKManagerImplements::CKAttributeManager(ctx, id); })},
}
2023-02-25 22:58:28 +08:00
{
// preset for temp folder
2023-02-26 21:48:03 +08:00
m_TempFolder = std::filesystem::temp_directory_path();
m_TempFolder /= g_UniqueFolder;
std::filesystem::create_directory(m_TempFolder);
2023-02-25 22:58:28 +08:00
}
CKMinContext::~CKMinContext() {
}
void CKMinContext::Printf(CKSTRING fmt, ...) {
2023-02-26 21:48:03 +08:00
if (m_PrintCallback == nullptr) return;
2023-02-25 22:58:28 +08:00
va_list argptr;
va_start(argptr, fmt);
2023-02-26 21:48:03 +08:00
std::string result;
int count = std::vsnprintf(nullptr, 0, fmt, argptr);
result.resize(count);
int write_result = std::vsnprintf(result.data(), count, fmt, argptr);
if (write_result < 0 || write_result > count) return;
2023-02-25 22:58:28 +08:00
va_end(argptr);
2023-02-26 21:48:03 +08:00
m_PrintCallback(result);
}
void CKMinContext::SetPrintCallback(PrintCallback cb) {
m_PrintCallback = cb;
2023-02-25 22:58:28 +08:00
}
2023-02-26 21:48:03 +08:00
void CKMinContext::GetUtf8String(std::string& native_name, std::string& u8_name) {
EncodingHelper::GetUtf8VirtoolsName(native_name, u8_name, this->m_NameEncodingToken);
2023-02-25 22:58:28 +08:00
}
2023-02-26 21:48:03 +08:00
void CKMinContext::GetNativeString(std::string& u8_name, std::string& native_name) {
EncodingHelper::GetNativeVirtoolsName(u8_name, native_name, this->m_NameEncodingToken);
2023-02-25 22:58:28 +08:00
}
void CKMinContext::SetEncoding(CKSTRING encoding) {
2023-02-26 21:48:03 +08:00
this->m_NameEncoding = encoding;
2023-02-25 22:58:28 +08:00
this->RefetchEncodingToken();
}
void CKMinContext::SetTempPath(CKSTRING u8_temp) {
2023-02-26 21:48:03 +08:00
EncodingHelper::SetStdPathFromU8Path(this->m_TempFolder, u8_temp);
2023-02-25 22:58:28 +08:00
}
FILE* CKMinContext::OpenTempFile(CKSTRING u8_filename, bool is_read) {
std::filesystem::path stdfilename;
2023-02-26 21:48:03 +08:00
EncodingHelper::SetStdPathFromU8Path(stdfilename, u8_filename);
2023-02-25 22:58:28 +08:00
2023-02-26 21:48:03 +08:00
auto realfile = this->m_TempFolder / stdfilename;
return EncodingHelper::OpenStdPathFile(realfile, is_read);
2023-02-25 22:58:28 +08:00
}
void CKMinContext::RefetchEncodingToken(void) {
2023-02-26 21:48:03 +08:00
EncodingHelper::DestroyEncodingToken(this->m_NameEncodingToken);
this->m_NameEncodingToken = EncodingHelper::CreateEncodingToken(this->m_NameEncoding);
2023-02-25 22:58:28 +08:00
}
}