Skip to content

Commit

Permalink
Remove unnecessary files and add CoreContext and BufferStream
Browse files Browse the repository at this point in the history
Removed multiple unused files as part of code cleanup for unused functionality/modules. Two new files, CoreContext and BufferStream, were added to handle buffer streams and core contexts. A change in DLL interface was done: pointers are now more generally typed with `c_void` instead of `u8`. This increases the safety of the code by making it less
  • Loading branch information
xorza committed Jun 8, 2024
1 parent 9fc7555 commit f8da612
Show file tree
Hide file tree
Showing 31 changed files with 319 additions and 347 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ png = "0.17"
image = { version = "0.25" }
num-traits = "0.2"
glam = "0.27"
mlua = { version = "0.9", features = ["lua54", "vendored"] }
strum = "0.26"
strum_macros = "0.26"
once_cell = "1.19"
Expand All @@ -38,3 +37,5 @@ parking_lot = "0.12"
hashbrown = { version = "0.14", features = ["serde"] }
bytes = { version = "1.6" }



2 changes: 1 addition & 1 deletion ScenariumEditor.NET/CoreInterop/CoreNative.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static unsafe partial class CoreNative


[DllImport(__DllName, EntryPoint = "create_context", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern byte* create_context();
public static extern void* create_context();

[DllImport(__DllName, EntryPoint = "destroy_context", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void destroy_context(byte* ctx);
Expand Down
24 changes: 24 additions & 0 deletions ScenariumEditor.QML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,37 @@ target_link_libraries(scenarium_editor
PUBLIC
Qt6::Quick
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
target_link_libraries(scenarium_editor
PUBLIC
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/libcs_interop.dll.a
)
else ()
target_link_libraries(scenarium_editor
PUBLIC
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/release/libcs_interop.dll.a
)
endif ()

target_include_directories(scenarium_editor PUBLIC lib)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_options(scenarium_editor
PUBLIC
-static -static-libgcc -static-libstdc++
)
endif ()


add_custom_command(
TARGET scenarium_editor POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/../target/x86_64-pc-windows-gnu/debug/cs_interop.dll
$<TARGET_FILE_DIR:scenarium_editor>
COMMENT "Copying DLL file to build directory"
)

set_target_properties(scenarium_editor PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER com.cssodessa.scenarium_editor
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
Expand Down
10 changes: 9 additions & 1 deletion ScenariumEditor.QML/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "src/CoreContext.hpp"

#include "qml/NodeController.hpp"
#include "qml/AppController.hpp"
#include "qml/ConnectionController.hpp"
Expand All @@ -10,6 +12,11 @@


int main(int argc, char *argv[]) {
{
auto ctx = Ctx{};
auto funcs = ctx.get_funcs();
}

QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QObject::connect(
Expand All @@ -31,5 +38,6 @@ int main(int argc, char *argv[]) {

engine.loadFromModule("scenarium_editor", "Main");

return QGuiApplication::exec();
int res = QGuiApplication::exec();
return res;
}
34 changes: 34 additions & 0 deletions ScenariumEditor.QML/src/BufferStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "BufferStream.hpp"




BufferStream::~BufferStream() {
delete[] data;
}


template<>
[[maybe_unused]] std::string BufferStream::read<std::string>() {
uint32_t str_len = read<uint32_t>();
if (pos + str_len > len) {
throw std::runtime_error("BufferStream: out of bounds");
}

std::string str(reinterpret_cast<char *>(data + pos), str_len);
pos += str_len;
return str;
}

std::string BufferStream::read_cstr() {
std::string str;
while (pos < len) {
char c = *reinterpret_cast<char *>(data + pos);
pos += 1;
if (c == '\0') {
break;
}
str.push_back(c);
}
return str;
}
66 changes: 66 additions & 0 deletions ScenariumEditor.QML/src/BufferStream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <cstdint>
#include <type_traits>
#include <stdexcept>
#include <vector>
#include <string>



class BufferStream {
private:
uint8_t *data = nullptr;
uint32_t len = 0;
uint32_t pos = 0;

public:

~BufferStream();

[[nodiscard]] uint32_t get_len() const {
return len;
}

[[nodiscard]] void *get_data() const {
return data;
}

template<typename T>
[[maybe_unused]] T read() {
static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
if (pos + sizeof(T) > len) {
throw std::runtime_error("BufferStream: out of bounds");
}

T val = *reinterpret_cast<T *>(data + pos);
pos += sizeof(T);
return val;
}


// partial implementation for vector<T>
template<typename T>
[[maybe_unused]] std::vector<T> read_vec() {
uint32_t vec_len = read<uint32_t>();
if (pos + vec_len * sizeof(T) > len) {
throw std::runtime_error("BufferStream: out of bounds");
}

std::vector<T> vec;
vec.reserve(vec_len);
for (uint32_t i = 0; i < vec_len; i++) {
vec.push_back(read<T>());
}
return vec;
}

std::string read_cstr();

std::string read_str_buf();
};


template<>
[[maybe_unused]] std::string BufferStream::read<std::string>();

100 changes: 100 additions & 0 deletions ScenariumEditor.QML/src/CoreContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "CoreContext.hpp"

#include "BufferStream.hpp"

#include <cassert>

extern "C" {
struct FfiBuf {
uint8_t *data;
uint32_t len;
uint32_t cap;

};

__declspec(dllimport) void *create_context();
__declspec(dllimport) void destroy_context(void *ctx);
__declspec(dllimport) void destroy_ffi_buf(FfiBuf buf);
__declspec(dllimport) FfiBuf get_funcs(void *ctx);


}

struct Buf {
FfiBuf ffi_buf;

explicit Buf(FfiBuf ffi_buf) : ffi_buf(ffi_buf) {}

~Buf() {
destroy_ffi_buf(ffi_buf);
}

// Buf(const Buf &other) = delete;
//
// Buf &operator=(const Buf &other) = delete;

[[nodiscard]] uint32_t len() const {
return ffi_buf.len;
}

[[nodiscard]] uint8_t *data() const {
return ffi_buf.data;
}

[[nodiscard]] std::string to_string() const {
return std::string(reinterpret_cast<char *>(data()), len());
}
};

struct FfiFunc {
Buf id;
Buf name;
Buf category;
uint32_t behaviour;
bool output;
Buf inputs;
Buf outputs;
Buf events;
};


Ctx::Ctx() {
this->ctx = create_context();
}

Ctx::~Ctx() {
destroy_context(this->ctx);
this->ctx = nullptr;
}

std::vector<Func> Ctx::get_funcs() const {
Buf buf = Buf{::get_funcs(this->ctx)};

assert(buf.len() % sizeof(FfiFunc) == 0); // check that the buffer is a multiple of the size of FfiFunc
auto len = buf.len() / sizeof(FfiFunc);
auto funcs = static_cast <FfiFunc *>( static_cast <void *>(buf.data()));

std::vector<Func> result;
result.reserve(len);

for (uint32_t i = 0; i < len; i++) {
auto ffi_func = funcs[i];
Func func{
ffi_func.id.to_string(),
ffi_func.name.to_string(),
ffi_func.category.to_string(),
ffi_func.behaviour,
ffi_func.output,
{},
{},
{}
// BufferStream{ffi_func->inputs.data(), ffi_func->inputs.len()}.read_strings(),
// BufferStream{ffi_func->outputs.data(), ffi_func->outputs.len()}.read_strings(),
// BufferStream{ffi_func->events.data(), ffi_func->events.len()}.read_strings()
};
result.push_back(func);
}


return result;
}
37 changes: 37 additions & 0 deletions ScenariumEditor.QML/src/CoreContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <vector>
#include <cstdint>
#include <memory>
#include <string>
#include <cstdlib>


struct Func {
std::string id;
std::string name;
std::string category;
uint32_t behaviour;
bool output;
std::vector<std::string> inputs;
std::vector<std::string> outputs;
std::vector<std::string> events;

};


struct Ctx {
void *ctx = nullptr;

Ctx();

~Ctx();

Ctx(const Ctx &other) = delete;

Ctx &operator=(const Ctx &other) = delete;


[[nodiscard]] std::vector<Func> get_funcs() const;
};

3 changes: 0 additions & 3 deletions cs_interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ imaginarium = { workspace = true }
graph = { workspace = true }
uuid = { workspace = true }
bytes = { workspace = true }

[build-dependencies]
csbindgen = "1.9"
12 changes: 0 additions & 12 deletions cs_interop/build.rs

This file was deleted.

5 changes: 2 additions & 3 deletions cs_interop/src/func_lib_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::c_void;
use crate::{get_context, FfiBuf, FfiId, FfiStr, FfiStrVec};

#[repr(C)]
Expand All @@ -21,7 +22,7 @@ struct FfiFunc {
}

#[no_mangle]
extern "C" fn get_funcs(ctx: *mut u8) -> FfiBuf {
extern "C" fn get_funcs(ctx: *mut c_void) -> FfiBuf {
// let yaml = include_str!("../../test_resources/test_funcs.yml");
// let func_lib = FuncLib::from_yaml(yaml).unwrap();

Expand Down Expand Up @@ -58,8 +59,6 @@ impl From<&graph::function::Func> for FfiFunc {
}
}

#[no_mangle]
extern "C" fn dummy2(_a: FfiFunc) {}

#[cfg(test)]
mod tests {
Expand Down
7 changes: 3 additions & 4 deletions cs_interop/src/graph_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::c_void;
use std::mem::ManuallyDrop;

use graph::function::FuncId;
Expand Down Expand Up @@ -35,7 +36,7 @@ impl From<&graph::graph::Node> for FfiNode {
}

#[no_mangle]
extern "C" fn get_nodes(ctx: *mut u8) -> FfiBuf {
extern "C" fn get_nodes(ctx: *mut c_void) -> FfiBuf {
// let graph = Graph::from_yaml(include_str!("../../test_resources/test_graph.yml")).unwrap();

get_context(ctx)
Expand All @@ -48,7 +49,7 @@ extern "C" fn get_nodes(ctx: *mut u8) -> FfiBuf {
}

#[no_mangle]
extern "C" fn new_node(ctx: *mut u8, func_id: FfiId) -> FfiNode {
extern "C" fn new_node(ctx: *mut c_void, func_id: FfiId) -> FfiNode {
let context = get_context(ctx);
let func_id: FuncId = ManuallyDrop::new(func_id).to_uuid().into();

Expand All @@ -59,5 +60,3 @@ extern "C" fn new_node(ctx: *mut u8, func_id: FfiId) -> FfiNode {
context.graph.nodes().last().unwrap().into()
}

#[no_mangle]
extern "C" fn dummy1(_a: FfiNode, _b: FfiInput) {}
Loading

0 comments on commit f8da612

Please sign in to comment.