- Move EngineInstance into a separate module
authorunC0Rr
Thu, 22 Nov 2018 13:39:51 +0100
changeset 14272 3152d9fdb499
parent 14271 1aac8a62be6f
child 14273 645e4591361f
- Move EngineInstance into a separate module - Refactor ipc a bit and process messages
rust/hedgewars-engine-messages/src/messages.rs
rust/lib-hedgewars-engine/src/instance.rs
rust/lib-hedgewars-engine/src/ipc.rs
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hedgewars-engine-messages/src/messages.rs	Thu Nov 22 10:58:55 2018 +0100
+++ b/rust/hedgewars-engine-messages/src/messages.rs	Thu Nov 22 13:39:51 2018 +0100
@@ -1,12 +1,12 @@
 use byteorder::{BigEndian, WriteBytesExt};
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum KeystrokeAction {
     Press,
     Release,
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum SyncedEngineMessage {
     Left(KeystrokeAction),
     Right(KeystrokeAction),
@@ -32,13 +32,13 @@
     Heartbeat,
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum UnsyncedEngineMessage {
     TeamControlGained(String),
     TeamControlLost(String),
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum UnorderedEngineMessage {
     Ping,
     Pong,
@@ -53,7 +53,7 @@
     PauseToggled,
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum ConfigEngineMessage {
     GameType(u8),
     ConfigRequest,
@@ -126,7 +126,7 @@
     SetShoppaBorder(bool),
 }
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Clone)]
 pub enum EngineMessage {
     Unknown,
     Empty,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/lib-hedgewars-engine/src/instance.rs	Thu Nov 22 13:39:51 2018 +0100
@@ -0,0 +1,63 @@
+use hedgewars_engine_messages::messages::{
+    ConfigEngineMessage::*, EngineMessage::*, KeystrokeAction::*, SyncedEngineMessage::*,
+    UnorderedEngineMessage::*, UnsyncedEngineMessage::*, *
+};
+
+use super::{ipc::IPC, world::World};
+
+#[repr(C)]
+pub struct EngineInstance {
+    pub world: World,
+    pub ipc: IPC,
+}
+
+impl EngineInstance {
+    pub fn new() -> Self {
+        let world = World::new();
+        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>,
+    {
+        context.clear(target, [0.0, 0.5, 0.0, 1.0]);
+    }
+
+    fn process_unordered_message(&mut self, message: &UnorderedEngineMessage) {
+        match message {
+            Pong => println!("Pong!"),
+            _ => unimplemented!(),
+        }
+    }
+
+    fn process_config_message(&mut self, message: &ConfigEngineMessage) {
+        match message {
+            SetSeed(seed) => self.world.set_seed(seed.as_bytes()),
+            _ => unimplemented!(),
+        }
+    }
+
+    pub fn process_ipc_queue(&mut self) {
+        let messages: Vec<EngineMessage> = self.ipc.iter().collect();
+
+        for message in messages {
+            println!("Processing message: {:?}", message);
+            match message {
+                Unknown => println!("Unknown message"),
+                Empty => println!("Empty message") ,
+                Synced(_, _) => unimplemented!(),
+                Unsynced(_) => unimplemented!(),
+                Unordered(unordered_message) => self.process_unordered_message(&unordered_message),
+                Config(config_message) => self.process_config_message(&config_message)
+            }
+        }
+    }
+}
--- a/rust/lib-hedgewars-engine/src/ipc.rs	Thu Nov 22 10:58:55 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/ipc.rs	Thu Nov 22 13:39:51 2018 +0100
@@ -18,6 +18,10 @@
     pub fn send_message(&mut self, message: &EngineMessage) {
         self.out_buffer.write(&message.to_bytes()).unwrap();
     }
+
+    pub fn iter(& mut self) -> IPCMessagesIterator {
+        IPCMessagesIterator::new(self)
+    }
 }
 
 impl Write for IPC {
@@ -40,13 +44,25 @@
     }
 }
 
-impl Iterator for IPC {
+pub struct IPCMessagesIterator<'a> {
+    ipc: &'a mut IPC
+}
+
+impl<'a> IPCMessagesIterator<'a> {
+    pub fn new(ipc: &'a mut IPC) -> Self {
+        Self {
+            ipc
+        }
+    }
+}
+
+impl<'a> Iterator for IPCMessagesIterator<'a> {
     type Item = EngineMessage;
 
     fn next(&mut self) -> Option<Self::Item> {
-        let (consumed, message) = extract_message(&self.in_buffer[..])?;
+        let (consumed, message) = extract_message(&self.ipc.in_buffer[..])?;
 
-        self.in_buffer.consume(consumed);
+        self.ipc.in_buffer.consume(consumed);
 
         Some(message)
     }
--- a/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 10:58:55 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Thu Nov 22 13:39:51 2018 +0100
@@ -1,36 +1,10 @@
 mod ipc;
 mod world;
+mod instance;
 
 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,
-            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>,
-    {
-        context.clear(target, [0.0, 0.5, 0.0, 1.0]);
-    }
-}
+use self::instance::EngineInstance;
 
 #[repr(C)]
 #[derive(Copy, Clone)]
@@ -55,6 +29,8 @@
 
 #[no_mangle]
 pub extern "C" fn generate_preview(engine_state: &mut EngineInstance, preview: &mut PreviewInfo) {
+    (*engine_state).process_ipc_queue();
+
     (*engine_state).world.generate_preview();
 
     let land_preview = (*engine_state).world.preview();
@@ -68,19 +44,19 @@
 }
 
 #[no_mangle]
-pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const [u8], size: usize) {
+pub extern "C" fn send_ipc(engine_state: &mut EngineInstance, buf: *const u8, size: usize) {
     unsafe {
-        (*engine_state).ipc.write(&(*buf)[0..size]);
+        (*engine_state).ipc.write(std::slice::from_raw_parts(buf, size)).unwrap();
     }
 }
 
 #[no_mangle]
 pub extern "C" fn read_ipc(
     engine_state: &mut EngineInstance,
-    buf: *mut [u8],
+    buf: *mut u8,
     size: usize,
 ) -> usize {
-    unsafe { (*engine_state).ipc.read(&mut (*buf)[0..size]).unwrap_or(0) }
+    unsafe { (*engine_state).ipc.read(std::slice::from_raw_parts_mut(buf, size)).unwrap_or(0) }
 }
 
 #[no_mangle]
--- a/rust/lib-hedgewars-engine/src/world.rs	Thu Nov 22 10:58:55 2018 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Thu Nov 22 13:39:51 2018 +0100
@@ -19,6 +19,10 @@
         }
     }
 
+    pub fn set_seed(&mut self, seed: &[u8]) {
+        self.random_numbers_gen = LaggedFibonacciPRNG::new(seed);
+    }
+
     pub fn preview(&self) -> &Land2D<u8> {
         &self.preview
     }