1
0

write shit

This commit is contained in:
2026-01-06 16:27:19 +08:00
parent 52916db08f
commit 4cdc56a32d
24 changed files with 512 additions and 135 deletions

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cwchar>
#include <string>
#include <string_view>
#if defined(BASALT_OS_WINDOWS)
#define BSCHAR wchar_t
#define _BSTEXT(x) L##x
#define BSTEXT(x) _BSTEXT(x)
#else
#define BSCHAR char
#define BSTEXT(x) x
#endif
namespace Basalt::Shared::Char {
using BSString = std::basic_string<BSCHAR>;
using BSStringView = std::basic_string_view<BSCHAR>;
}; // namespace Basalt::Shared::Char

View File

@@ -0,0 +1,36 @@
#pragma once
// Reference: https://stackoverflow.com/questions/2164827/explicitly-exporting-shared-library-functions-in-linux
// Generate macro for import and export respectively.
#if defined(_MSC_VER)
// Microsoft
#define BS_RAW_EXPORT __declspec(dllexport)
#define BS_RAW_IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
// GCC
#define BS_RAW_EXPORT __attribute__((visibility("default")))
#define BS_RAW_IMPORT
#elif defined(__clang__)
// Clang
#define BS_RAW_EXPORT __attribute__((visibility("default")))
#define BS_RAW_IMPORT
#else
// Do nothing and hope for the best?
#define BS_RAW_EXPORT
#define BS_RAW_IMPORT
#error "Unknown dynamic link import/export semantics."
#endif
// Choosee import or export command according to special macro.
#if defined(BS_EXPORTING)
#define BS_NAKED_EXPORT BS_RAW_EXPORT
#else
#define BS_NAKED_EXPORT BS_RAW_IMPORT
#endif
// Create real export macro according to whether in C++ environment.
#if defined(__cplusplus)
#define BS_EXPORT extern "C" BS_NAKED_EXPORT
#else
#define BS_EXPORT BS_NAKED_EXPORT
#endif

View File

@@ -0,0 +1,65 @@
#include "kernel.hpp"
#include <stdexcept>
namespace Basalt::Shared::Kernel {
#pragma region Engine
IEngine::IEngine() : config(), status(EngineStatus::Ready) {}
IEngine::~IEngine() {
if (this->status != EngineStatus::Stop) {
this->Shutdown();
}
}
Guid IEngine::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IEngine::Startup(EngineConfig &&config) {
if (this->status != EngineStatus::Ready) throw std::runtime_error("unexpected engine status");
this->config = std::move(config);
this->status = EngineStatus::Running;
}
bool IEngine::Tick() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
return false;
}
void IEngine::Shutdown() {
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
this->status = EngineStatus::Stop;
}
#pragma endregion
#pragma region Deliver
IDeliver::IDeliver() {}
IDeliver::~IDeliver() {}
Guid IDeliver::GetGuid() const {
throw std::logic_error("unimplemented function");
}
void IDeliver::Startup(DeliverConfig &&config) {
if (this->status != DeliverStatus::Ready) throw std::runtime_error("unexpected deliver status");
this->config = std::move(config);
this->status = DeliverStatus::Running;
}
void IDeliver::Transmit() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
}
void IDeliver::Shutdown() {
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
this->status = DeliverStatus::Stop;
}
#pragma endregion
} // namespace Basalt::Shared::Kernel

View File

@@ -0,0 +1,123 @@
#pragma once
#include "char_types.hpp"
#include <string>
#include <cinttypes>
namespace Basalt::Shared::Kernel {
#pragma region Guid & Predefined Guid
struct Guid {
std::uint32_t d1, d2;
constexpr Guid(std::uint32_t gd1 = 0, std::uint32_t gd2 = 0) : d1(gd1), d2(gd2) {}
Guid(const Guid& rhs) : d1(rhs.d1), d2(rhs.d2) {}
Guid(Guid&& rhs) noexcept : d1(rhs.d1), d2(rhs.d2) {}
Guid& operator=(const Guid& rhs) {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
Guid& operator=(Guid&& rhs) noexcept {
this->d1 = rhs.d1;
this->d2 = rhs.d2;
return *this;
}
auto operator<=>(const Guid& rhs) const {
if (auto cmp = this->d1 <=> rhs.d1; cmp != 0) return cmp;
return this->d2 <=> rhs.d2;
}
bool operator==(const Guid& rhs) const { return ((this->d1 == rhs.d1) && (this->d2 == rhs.d2)); }
};
constexpr Guid DIRECTX8_ENGINE{0x0, 0x1};
constexpr Guid DIRECTX9_ENGINE{0x0, 0x2};
constexpr Guid DIRECTX11_ENGINE{0x0, 0x3};
constexpr Guid DIRECTX12_ENGINE{0x0, 0x4};
constexpr Guid OPENGL_ENGINE{0x0, 0x5};
constexpr Guid VULKAN_ENGINE{0x0, 0x6};
constexpr Guid CUDA_DELIVER{0x1, 0x1};
constexpr Guid ROCM_DELIVER{0x1, 0x2};
constexpr Guid PIPE_DELIVER{0x1, 0x3};
constexpr Guid TCP_DELIVER{0x1, 0x4};
constexpr Guid MMAP_DELIVER{0x1, 0x5};
constexpr Guid TINYOBJLOADER_OBJECT_LOADER{0x2, 0x1};
constexpr Guid CGLTF_OBJECT_LOADER{0x2, 0x2};
constexpr Guid ASSIMP_OBJECT_LOADER{0x2, 0x3};
constexpr Guid HOMEBREW_ANIME_LOADER{0x3, 0x1};
#pragma endregion
#pragma region Engine
struct EngineConfig {
bool headless; ///< Whether enable headless mode (No Window created).
std::basic_string<BSCHAR> title; ///< Window title.
std::uint32_t width; ///< Window width.
std::uint32_t height; ///< Window height.
Guid deliver; ///< The GUID of deliver.
};
enum class EngineStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IEngine {
public:
IEngine();
virtual ~IEngine();
public:
virtual Guid GetGuid() const;
virtual void Startup(EngineConfig&& config);
/**
* @brief
* @return True for active exit.
*/
virtual bool Tick();
virtual void Shutdown();
protected:
EngineConfig config;
EngineStatus status;
};
#pragma endregion
#pragma region Deliver
struct DeliverConfig {
Guid engine; ///< The GUID of render engine.
};
enum class DeliverStatus {
Ready, ///< Engine was allocated but not initialized.
Running, ///< Engine has been initialized and running.
Stop, ///< Engine is shutdown.
};
class IDeliver {
public:
IDeliver();
virtual ~IDeliver();
public:
virtual Guid GetGuid() const;
virtual void Startup(DeliverConfig&& config);
virtual void Transmit();
virtual void Shutdown();
protected:
DeliverConfig config;
DeliverStatus status;
};
#pragma endregion
} // namespace Basalt::Shared::Kernel

View File

@@ -0,0 +1,5 @@
#include "math.hpp"
namespace Basalt::Shared::Math {
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include <stdexcept>
namespace Basalt::Shared::Math {
using FloatPoint = float;
struct Vector3 {
FloatPoint x, y, z;
};
struct Vector4 {
FloatPoint x, y, z, w;
};
struct Matrix4x4 {
Vector4 data[4];
};
//template<typename TEle, size_t VCnt>
// requires(std::integral<TEle> || std::floating_point<TEle>)
//struct Vector {
// TEle factor[VCnt];
//};
#define NOT_IMPLEMENTED throw std::logic_error("not implemented function");
template<typename TNum, typename TVec>
struct Vector3Traits {};
template<typename TNum, typename TMat>
struct Matrix4x4Traits {};
#undef NOT_IMPLEMENTED
} // namespace Basalt::Shared::Math

View File

@@ -0,0 +1,167 @@
#include "pipe_operator.hpp"
#include <format>
#if defined(BASALT_OS_WINDOWS)
#include <Windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#endif
namespace Basalt::Shared {
PipeOperator::PipeOperator(const std::basic_string_view<BSCHAR> name) {
#if defined(BASALT_OS_WINDOWS)
// Create Windows pipe name from given name
auto fullname = std::format(BSTEXT("\\\\.\\pipe\\{}"), name);
m_Handle = CreateFileW(fullname.c_str(), // 管道名称
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
NULL, // 默认安全属性
OPEN_EXISTING, // 打开已存在的管道
0, // 默认属性
NULL // 无模板文件
);
if (m_Handle == BAD_PIPE_HANDLE) {
throw std::runtime_error("Failed to open named pipe.");
}
#else
// Create Linux pipe name from given name
auto fullname = std::format(BSTEXT("/tmp/{}"), name);
m_Handle = open(fullname.c_str(), O_RDWR);
if (m_Handle == BAD_PIPE_HANDLE) {
throw std::runtime_error("Failed to open named pipe.");
}
#endif
}
PipeOperator::~PipeOperator() {
#if defined(BASALT_OS_WINDOWS)
if (m_Handle != BAD_PIPE_HANDLE) {
CloseHandle(m_Handle);
}
#else
if (m_Handle != BAD_PIPE_HANDLE) {
close(m_Handle);
}
#endif
}
PipeOperator::PipeOperator(PipeOperator &&rhs) noexcept {
#if defined(BASALT_OS_WINDOWS)
m_Handle = rhs.m_Handle;
rhs.m_Handle = BAD_PIPE_HANDLE;
#else
m_Handle = rhs.m_Handle;
rhs.m_Handle = BAD_PIPE_HANDLE;
#endif
}
PipeOperator &PipeOperator::operator=(PipeOperator &&rhs) noexcept {
#if defined(BASALT_OS_WINDOWS)
if (m_Handle != BAD_PIPE_HANDLE) {
CloseHandle(m_Handle);
}
m_Handle = rhs.m_Handle;
rhs.m_Handle = BAD_PIPE_HANDLE;
#else
if (m_Handle != BAD_PIPE_HANDLE) {
close(m_Handle);
}
m_Handle = rhs.m_Handle;
rhs.m_Handle = BAD_PIPE_HANDLE;
#endif
return *this;
}
void PipeOperator::Read(void *buffer, size_t size) {
if (size == 0) {
return; // 读取0字节直接返回
}
#if defined(BASALT_OS_WINDOWS)
DWORD bytesRead = 0;
BOOL success = ReadFile(m_Handle, // 管道句柄
buffer, // 缓冲区
static_cast<DWORD>(size), // 缓冲区大小
&bytesRead, // 实际读取的字节数
NULL // 不使用重叠I/O
);
if (!success) {
throw std::runtime_error("Failed to read from named pipe.");
}
if (bytesRead != static_cast<DWORD>(size)) {
throw std::runtime_error("Incomplete read from named pipe.");
}
#else
// Due to POSIX "write" may write bytes less than given,
// so we need use "while" syntax to implement it.
size_t totalRead = 0;
while (totalRead < size) {
ssize_t bytesRead = read(m_Handle, static_cast<char *>(buffer) + totalRead, size - totalRead);
if (bytesRead == -1) {
throw std::runtime_error("Failed to read from named pipe");
}
if (bytesRead == 0) {
// 管道已关闭或到达末尾
throw std::runtime_error("Named pipe closed during read.");
}
totalRead += bytesRead;
}
if (totalRead != size) {
throw std::runtime_error("Incomplete read from named pipe.");
}
#endif
}
void PipeOperator::Write(const void *buffer, size_t size) {
if (size == 0) {
return; // 写入0字节直接返回
}
#if defined(BASALT_OS_WINDOWS)
DWORD bytesWritten = 0;
BOOL success = WriteFile(m_Handle, // 管道句柄
buffer, // 数据缓冲区
static_cast<DWORD>(size), // 数据大小
&bytesWritten, // 实际写入的字节数
NULL // 不使用重叠I/O
);
if (!success) {
throw std::runtime_error("Failed to write to named pipe.");
}
if (bytesWritten != static_cast<DWORD>(size)) {
throw std::runtime_error("Incomplete write to named pipe.");
}
#else
// Due to the same reason, use "while" syntax.
size_t totalWritten = 0;
while (totalWritten < size) {
ssize_t bytesWritten = write(m_Handle, static_cast<const char *>(buffer) + totalWritten, size - totalWritten);
if (bytesWritten == -1) {
throw std::runtime_error("Failed to write to named pipe.");
}
totalWritten += bytesWritten;
}
if (totalWritten != size) {
throw std::runtime_error("Incomplete write to named pipe.");
}
#endif
}
} // namespace Basalt::Shared

View File

@@ -0,0 +1,37 @@
#include "char_types.hpp"
#include <string_view>
#if defined(BASALT_OS_WINDOWS)
#include <Windows.h>
#else
#endif
namespace Basalt::Shared {
class PipeOperator {
public:
#if defined(BASALT_OS_WINDOWS)
using Handle = HANDLE;
static constexpr Handle BAD_PIPE_HANDLE = INVALID_HANDLE_VALUE;
#else
using Handle = int;
static constexpr Handle BAD_PIPE_HANDLE = -1;
#endif
public:
PipeOperator(const std::basic_string_view<BSCHAR> name);
~PipeOperator();
// Delete copy ctor
PipeOperator(const PipeOperator&) = delete;
PipeOperator& operator=(const PipeOperator&) = delete;
// Implement move ctor
PipeOperator(PipeOperator&&) noexcept;
PipeOperator& operator=(PipeOperator&& other) noexcept;
void Read(void* buffer, size_t size);
void Write(const void* buffer, size_t size);
private:
Handle m_Handle;
};
} // namespace Basalt::Shared