Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
authorunC0Rr
Thu, 22 Nov 2018 10:58:55 +0100
changeset 14271 1aac8a62be6f
parent 14270 efa901b04bad
child 14272 3152d9fdb499
Implement send_ipc and read_ipc in engine lib, send_ipc in frontend
qmlfrontend/engine_instance.cpp
qmlfrontend/engine_instance.h
qmlfrontend/engine_interface.h
qmlfrontend/hwengine.cpp
qmlfrontend/main.cpp
rust/lib-hedgewars-engine/src/lib.rs
--- a/qmlfrontend/engine_instance.cpp	Thu Nov 22 00:49:55 2018 +0300
+++ b/qmlfrontend/engine_instance.cpp	Thu Nov 22 10:58:55 2018 +0100
@@ -5,6 +5,13 @@
 
 EngineInstance::~EngineInstance() { Engine::cleanup(m_instance); }
 
+void EngineInstance::sendConfig(const GameConfig &config) {
+  for (auto b : config.config()) {
+    Engine::send_ipc(m_instance, reinterpret_cast<uint8_t *>(b.data()),
+                     static_cast<size_t>(b.size()));
+  }
+}
+
 Engine::PreviewInfo EngineInstance::generatePreview() {
   Engine::PreviewInfo pinfo;
 
--- a/qmlfrontend/engine_instance.h	Thu Nov 22 00:49:55 2018 +0300
+++ b/qmlfrontend/engine_instance.h	Thu Nov 22 10:58:55 2018 +0100
@@ -5,12 +5,15 @@
 
 #include <QObject>
 
+#include "game_config.h"
+
 class EngineInstance : public QObject {
   Q_OBJECT
  public:
   explicit EngineInstance(QObject *parent = nullptr);
   ~EngineInstance();
 
+  void sendConfig(const GameConfig &config);
   Engine::PreviewInfo generatePreview();
 
  signals:
--- a/qmlfrontend/engine_interface.h	Thu Nov 22 00:49:55 2018 +0300
+++ b/qmlfrontend/engine_interface.h	Thu Nov 22 10:58:55 2018 +0100
@@ -1,6 +1,7 @@
 #ifndef ENGINE_H
 #define ENGINE_H
 
+#include <stddef.h>
 #include <stdint.h>
 
 #ifdef __cplusplus
@@ -23,11 +24,19 @@
                                 PreviewInfo* preview);
 typedef void cleanup_t(EngineInstance* engine_state);
 
+typedef void send_ipc_t(EngineInstance* engine_state, uint8_t* buf,
+                        size_t size);
+typedef size_t read_ipc_t(EngineInstance* engine_state, uint8_t* buf,
+                          size_t size);
+
 extern protocol_version_t* protocol_version;
 extern start_engine_t* start_engine;
 extern generate_preview_t* generate_preview;
 extern cleanup_t* cleanup;
 
+extern send_ipc_t* send_ipc;
+extern read_ipc_t* read_ipc;
+
 #ifdef __cplusplus
 }
 };
--- a/qmlfrontend/hwengine.cpp	Thu Nov 22 00:49:55 2018 +0300
+++ b/qmlfrontend/hwengine.cpp	Thu Nov 22 10:58:55 2018 +0100
@@ -5,7 +5,6 @@
 
 #include "engine_instance.h"
 #include "engine_interface.h"
-#include "game_view.h"
 #include "preview_image_provider.h"
 
 #include "hwengine.h"
@@ -40,6 +39,8 @@
   m_gameConfig.setPreview(true);
 
   EngineInstance engine;
+  engine.sendConfig(m_gameConfig);
+
   Engine::PreviewInfo preview = engine.generatePreview();
 
   QVector<QRgb> colorTable;
--- a/qmlfrontend/main.cpp	Thu Nov 22 00:49:55 2018 +0300
+++ b/qmlfrontend/main.cpp	Thu Nov 22 10:58:55 2018 +0100
@@ -11,6 +11,8 @@
 start_engine_t* start_engine;
 generate_preview_t* generate_preview;
 cleanup_t* cleanup;
+send_ipc_t* send_ipc;
+read_ipc_t* read_ipc;
 };  // namespace Engine
 
 void loadEngineLibrary() {
@@ -32,6 +34,11 @@
   Engine::cleanup =
       reinterpret_cast<Engine::cleanup_t*>(hwlib.resolve("cleanup"));
 
+  Engine::send_ipc =
+      reinterpret_cast<Engine::send_ipc_t*>(hwlib.resolve("send_ipc"));
+  Engine::read_ipc =
+      reinterpret_cast<Engine::read_ipc_t*>(hwlib.resolve("read_ipc"));
+
   if (Engine::protocol_version)
     qDebug() << "Loaded engine library with protocol version"
              << Engine::protocol_version();
--- a/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 00:49:55 2018 +0300
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 10:58:55 2018 +0100
@@ -1,23 +1,32 @@
+mod ipc;
 mod world;
-mod ipc;
+
+use std::io::{Read, Write};
+
+use self::ipc::IPC;
 
 #[repr(C)]
 pub struct EngineInstance {
     world: world::World,
+    ipc: IPC,
 }
 
 impl EngineInstance {
     pub fn new() -> Self {
         let world = world::World::new();
-        Self { world }
+        Self {
+            world,
+            ipc: IPC::new(),
+        }
     }
 
     pub fn render<R, C>(
         &self,
         context: &mut gfx::Encoder<R, C>,
-        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>)
-        where R: gfx::Resources,
-              C: gfx::CommandBuffer<R>
+        target: &gfx::handle::RenderTargetView<R, gfx::format::Rgba8>,
+    ) where
+        R: gfx::Resources,
+        C: gfx::CommandBuffer<R>,
     {
         context.clear(target, [0.0, 0.5, 0.0, 1.0]);
     }
@@ -39,9 +48,7 @@
 
 #[no_mangle]
 pub extern "C" fn start_engine() -> *mut EngineInstance {
-    let engine_state = Box::new(EngineInstance {
-        world: world::World::new(),
-    });
+    let engine_state = Box::new(EngineInstance::new());
 
     Box::leak(engine_state)
 }
@@ -61,6 +68,22 @@
 }
 
 #[no_mangle]
+pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const [u8], size: usize) {
+    unsafe {
+        (*engine_state).ipc.write(&(*buf)[0..size]);
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn read_ipc(
+    engine_state: &mut EngineInstance,
+    buf: *mut [u8],
+    size: usize,
+) -> usize {
+    unsafe { (*engine_state).ipc.read(&mut (*buf)[0..size]).unwrap_or(0) }
+}
+
+#[no_mangle]
 pub extern "C" fn cleanup(engine_state: *mut EngineInstance) {
     unsafe {
         Box::from_raw(engine_state);