Skip to content

Commit

Permalink
Add engine info handler for getting engine version and hash (#2146)
Browse files Browse the repository at this point in the history
  • Loading branch information
HellAholic authored Oct 14, 2024
2 parents 5716bf0 + c579c96 commit 48811a3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ target_compile_definitions(_CuraEngine
$<$<AND:$<BOOL:${ENABLE_PLUGINS}>,$<BOOL:${ENABLE_REMOTE_PLUGINS}>>:ENABLE_REMOTE_PLUGINS>
$<$<BOOL:${OLDER_APPLE_CLANG}>:OLDER_APPLE_CLANG>
CURA_ENGINE_VERSION=\"${CURA_ENGINE_VERSION}\"
CURA_ENGINE_HASH=\"${CURA_ENGINE_HASH}\"
$<$<BOOL:${ENABLE_TESTING}>:BUILD_TESTS>
PRIVATE
$<$<BOOL:${WIN32}>:NOMINMAX>
Expand Down
1 change: 1 addition & 0 deletions conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "5.9.0-beta.1"
commit: "unknown"
requirements:
- "scripta/0.1.0@ultimaker/testing"
requirements_arcus:
Expand Down
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def generate(self):

tc = CMakeToolchain(self)
tc.variables["CURA_ENGINE_VERSION"] = self.version
tc.variables["CURA_ENGINE_HASH"] = self.conan_data["commit"]
tc.variables["ENABLE_ARCUS"] = self.options.enable_arcus
tc.variables["ENABLE_TESTING"] = not self.conf.get("tools.build:skip_test", False, check_type=bool)
tc.variables["ENABLE_BENCHMARKS"] = self.options.enable_benchmarks
Expand Down
9 changes: 8 additions & 1 deletion include/communication/EmscriptenCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class EmscriptenCommunication : public CommandLine
std::string progress_handler_; ///< Handler for progress messages.
std::string gcode_header_handler_; ///< Handler for getting the GCode handler.
std::string slice_info_handler_; ///< Handler for slice information messages.

std::string engine_info_handler_; ///< Handler for curaengine info : version and hash.
/**
* \brief Creates a message containing slice information.
* \return A string containing the slice information message.
*/
[[nodiscard]] static std::string createSliceInfoMessage();
[[nodiscard]] static std::string createEngineInfoMessage();


public:
/**
Expand All @@ -48,6 +50,11 @@ class EmscriptenCommunication : public CommandLine
*/
void sendGCodePrefix(const std::string& prefix) const override;

/**
* \brief Indicate that we're beginning to send g-code.
*/
void beginGCode() override;

/**
* \brief Initiates the slicing of the next item.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/communication/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ void CommandLine::sliceNext()
force_read_parent = false;
force_read_nondefault = false;
}
else if (argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb"))
else if (
argument.starts_with("--progress_cb") || argument.starts_with("--slice_info_cb") || argument.starts_with("--gcode_header_cb")
|| argument.starts_with("--engine_info_cb"))
{
// Unused in command line slicing, but used in EmscriptenCommunication.
argument_index++;
Expand Down
33 changes: 33 additions & 0 deletions src/communication/EmscriptenCommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ EmscriptenCommunication::EmscriptenCommunication(const std::vector<std::string>&
{
gcode_header_handler_ = *ranges::next(gcode_header_flag);
}
if (auto engine_info_flag = ranges::find(arguments_, "--engine_info_cb"); engine_info_flag != arguments_.end())
{
engine_info_handler_ = *ranges::next(engine_info_flag);
}
}

void EmscriptenCommunication::sendGCodePrefix(const std::string& prefix) const
Expand Down Expand Up @@ -109,6 +113,35 @@ std::string EmscriptenCommunication::createSliceInfoMessage()
return buffer.GetString();
}

std::string EmscriptenCommunication::createEngineInfoMessage()
{
// Construct a string with rapidjson containing the engine information
rapidjson::Document doc;
auto& allocator = doc.GetAllocator();
doc.SetObject();

// Set the slicer version
rapidjson::Value version("version", allocator);
rapidjson::Value version_value(CURA_ENGINE_VERSION, allocator);
doc.AddMember(version, version_value, allocator);

// Set the hash
rapidjson::Value hash("hash", allocator);
rapidjson::Value hash_value(CURA_ENGINE_HASH, allocator);
doc.AddMember(hash, hash_value, allocator);

// Serialize the JSON document to a string
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}

void EmscriptenCommunication::beginGCode()
{
auto engine_info = createEngineInfoMessage();
emscripten_run_script(fmt::format("globalThis[\"{}\"]({})", engine_info_handler_, engine_info).c_str());
}
void EmscriptenCommunication::sliceNext()
{
CommandLine::sliceNext();
Expand Down

0 comments on commit 48811a3

Please sign in to comment.