From f0bca9f4e561dd47a6f5bfc592003bd1aff8958c Mon Sep 17 00:00:00 2001 From: Joud Kandeel Date: Tue, 5 May 2026 03:49:20 +0200 Subject: [PATCH] added SampleMod For C++ --- SampleModCpp/MyMod.cpp | 28 ++++ SampleModCpp/SampleModCpp.vcxproj | 155 ++++++++++++++++++++++ SampleModCpp/SampleModCpp.vcxproj.filters | 30 +++++ SampleModCpp/XPP.cpp | 124 +++++++++++++++++ SampleModCpp/XPP.hpp | 28 ++++ 5 files changed, 365 insertions(+) create mode 100644 SampleModCpp/MyMod.cpp create mode 100644 SampleModCpp/SampleModCpp.vcxproj create mode 100644 SampleModCpp/SampleModCpp.vcxproj.filters create mode 100644 SampleModCpp/XPP.cpp create mode 100644 SampleModCpp/XPP.hpp diff --git a/SampleModCpp/MyMod.cpp b/SampleModCpp/MyMod.cpp new file mode 100644 index 0000000..ab1167c --- /dev/null +++ b/SampleModCpp/MyMod.cpp @@ -0,0 +1,28 @@ +#include "XPP.hpp" + +class MyMod : public IXPP { +public: + MyMod(); + ~MyMod(); + XAPIDescriptor query() override { + return XAPIDescriptor(); + } + int onInit() override { + XPP::logDebug("Hello C++"); + return 1; + } + int onUpdate() override { + return 1; + } + int onShutdown() override { + return 1; + } +}mod; + +MyMod::MyMod() { + XPP::initXPP(&mod); +} + +MyMod::~MyMod() { + +} diff --git a/SampleModCpp/SampleModCpp.vcxproj b/SampleModCpp/SampleModCpp.vcxproj new file mode 100644 index 0000000..bbe1bc5 --- /dev/null +++ b/SampleModCpp/SampleModCpp.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + 17.0 + Win32Proj + {62121dc8-0ebd-43f8-8d30-85db30a3574d} + SampleModCpp + 10.0 + + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)\Minecraft-Community-Edition\Mod\;$(IncludePath) + + + $(SolutionDir)\Minecraft-Community-Edition\Mod\;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;SAMPLEMODCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;SAMPLEMODCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + _DEBUG;SAMPLEMODCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + stdcpp23 + + + Windows + true + false + + + + + Level3 + true + true + true + NDEBUG;SAMPLEMODCPP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + stdcpp23 + + + Windows + true + false + + + + + + \ No newline at end of file diff --git a/SampleModCpp/SampleModCpp.vcxproj.filters b/SampleModCpp/SampleModCpp.vcxproj.filters new file mode 100644 index 0000000..dde0bb9 --- /dev/null +++ b/SampleModCpp/SampleModCpp.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/SampleModCpp/XPP.cpp b/SampleModCpp/XPP.cpp new file mode 100644 index 0000000..c7ab0cb --- /dev/null +++ b/SampleModCpp/XPP.cpp @@ -0,0 +1,124 @@ +#include "XPP.hpp" + +IXPP* XPP::_instance; +XI_createDeviceAndContextFn XI_createDeviceAndContext; +XI_destroyDeviceAndContextFn XI_destroyDeviceAndContext; +XHQEventBus qBus; +XIDevice* device; +XIContext* ctx; +XIExports exports; + +XPP& XPP::getSingleton() { + static XPP xpp; + return xpp; +} + +void XPP::initXPP(IXPP* instance) { + _instance = instance; +} + +void XPP::logDebug(const std::string& msg) { + XSEventLog log; + log.channel = "default"; + log.msg = msg.c_str(); + log.severity = XE_EVENT_LOG_DEBUG; + XPP::postEvent(nullptr, &log, XE_EVENT_TYPE_LOG); +} + +IXPP* XPP::getModInstance() { + return _instance; +} +XHQEventBus CXAPI_createQEventBus(Xcstr name) { + XHQEventBus qBus; + if (device->vtbl->createQEventBus != XAPI_NULL) { + XQEventBusDescriptor qBusdesc; + qBusdesc.name = name; + qBusdesc.maxSize = 0xFFFF; + device->vtbl->createQEventBus(device, &qBus, qBusdesc); + } + else { + qBus.idx = 0; + } + return qBus; +} +XHQEventBus XPP::createQEventBus(const std::string& name) { + XHQEventBus qBus; + if (device->vtbl->createQEventBus != XAPI_NULL) { + XQEventBusDescriptor qBusDesc; + qBusDesc.name = name.c_str(); + qBusDesc.maxSize = 0xFFFF; + device->vtbl->createQEventBus(device, &qBus, qBusDesc); + } + else { + qBus.idx = 0; + } + return qBus; +} + +int XPP::postEvent(XHQEventBus* _qBus, Xvoid* event, Xconst Xuint64 type) { + XHQEventBus thisQbus; + if (_qBus == XAPI_NULL) { + thisQbus.idx = qBus.idx; + } + else { + thisQbus.idx = _qBus->idx; + } + return (ctx->vtbl->postEvent != XAPI_NULL) ? ctx->vtbl->postEvent(&thisQbus, event, type) : XE_ERROR; +} + +extern "C" XAPI_EXPORT XAPIDescriptor XI_query(Xvoid) { + auto& instance = XPP::getSingleton(); + auto desc = instance.getModInstance()->query(); + desc.sdkName = "XPP"; + return desc; +} + +extern "C" XAPI_EXPORT Xint32 XI_main(Xvoid* pParam) { + XI_createDeviceAndContext = (XI_createDeviceAndContextFn)pParam; + Xconst Xint32 ret = XI_createDeviceAndContext(&device, &ctx); + + if (ret != XE_ERROR) { + + if (device != XAPI_NULL && ctx != XAPI_NULL) { + if (device->vtbl != XAPI_NULL && ctx->vtbl != XAPI_NULL) { + if (device->vtbl->createQEventBus != XAPI_NULL) { + XQEventBusDescriptor desc{}; + desc.maxSize = 0xFFFF; + desc.name = "APP"; + device->vtbl->createQEventBus(device, &qBus, desc); + } + else { + return XE_ERROR; + } + + exports.onInit = []() -> Xint32 {return XPP::getSingleton().getModInstance()->onInit(); }; + exports.onUpdate = []() -> Xint32 {return XPP::getSingleton().getModInstance()->onUpdate(); }; + exports.onShutdown = []() -> Xint32 {return XPP::getSingleton().getModInstance()->onShutdown(); }; + + if (device->vtbl->setXIExports != XAPI_NULL) { + device->vtbl->setXIExports(device, exports); + } + else { + return XE_ERROR; + } + } + else { + return XE_ERROR; + } + } + else { + return XE_ERROR; + } + } + XPP::getSingleton().logDebug("XPP loaded up successfully! XAPI version v1.0.0"); + return ret; +} + + +extern "C" XAPI_EXPORT Xint32 XI_terminate(Xvoid* pParam) { + XI_destroyDeviceAndContext = (XI_destroyDeviceAndContextFn)pParam; + + auto ret = XI_destroyDeviceAndContext(&device, &ctx); + + return ret; +} diff --git a/SampleModCpp/XPP.hpp b/SampleModCpp/XPP.hpp new file mode 100644 index 0000000..c2074b5 --- /dev/null +++ b/SampleModCpp/XPP.hpp @@ -0,0 +1,28 @@ +#pragma once +#define XAPI_USE_V1_0_0 +#include "XAPI.h" +#include + +class IXPP { +public: + virtual ~IXPP() { + + } + virtual XAPIDescriptor query() = 0; + virtual int onInit() = 0; + virtual int onUpdate() = 0; + virtual int onShutdown() = 0; +}; + +class XPP { +public: + static XPP& getSingleton(); + static void initXPP(IXPP* instance); + static void logDebug(const std::string& msg); + static IXPP* getModInstance(); + static XHQEventBus createQEventBus(const std::string& name); + static int postEvent(XHQEventBus* _qBus, Xvoid* event, Xconst Xuint64 type); +private: + static IXPP* _instance; +}; +