Browse Source

ILuaShared interface plug

IVogel 2 years ago
parent
commit
ace7e24143
3 changed files with 56 additions and 9 deletions
  1. 14 5
      example/src/lib.rs
  2. 39 0
      server-plugin/src/iluashared.rs
  3. 3 4
      server-plugin/src/lib.rs

+ 14 - 5
example/src/lib.rs

@@ -1,11 +1,14 @@
-use std::{ffi::{CStr, CString, c_void}, error::Error};
+use std::{
+    error::Error,
+    ffi::{c_void, CStr, CString},
+};
 
-use server_plugin::{ServerPlugin, INetworkStringTableContainer, CreateInterfaceFn};
+use server_plugin::{CreateInterfaceFn, INetworkStringTableContainer, ServerPlugin};
 use server_plugin_derive::create_interface;
 
 struct TestPlugin {
     plugin_name: CString,
-    string_tables: Option<&'static INetworkStringTableContainer>
+    string_tables: Option<&'static INetworkStringTableContainer>,
 }
 
 impl TestPlugin {
@@ -18,13 +21,19 @@ impl TestPlugin {
 }
 
 impl ServerPlugin for TestPlugin {
-    fn load(&mut self, sys_appfactory: CreateInterfaceFn, _server_factorty: CreateInterfaceFn) -> Result<(), Box<dyn Error>> {
+    fn load(
+        &mut self,
+        sys_appfactory: CreateInterfaceFn,
+        _server_factorty: CreateInterfaceFn,
+    ) -> Result<(), Box<dyn Error>> {
         self.string_tables = INetworkStringTableContainer::from_factory(sys_appfactory).ok();
         Ok(())
     }
     fn server_activate(&mut self, _edict_list: &mut [*mut c_void], _max_players: i32) {
         let string_tables = self.string_tables.unwrap();
-        for string_table in (0..string_tables.get_num_tables()).map_while(|id| string_tables.get_table(id)) {
+        for string_table in
+            (0..string_tables.get_num_tables()).map_while(|id| string_tables.get_table(id))
+        {
             dbg!(string_table);
         }
         let table = string_tables.find_table("GModGameInfo").unwrap();

+ 39 - 0
server-plugin/src/iluashared.rs

@@ -0,0 +1,39 @@
+use std::ffi::c_void;
+use std::mem;
+
+use crate::CreateInterfaceFn;
+use vtables::VTable;
+use vtables_derive::has_vtable;
+use vtables_derive::virtual_index;
+use vtables_derive::VTable;
+
+#[repr(u8)]
+pub enum LuaState {
+    Client,
+    Server,
+    Menu,
+}
+
+#[has_vtable]
+#[derive(VTable)]
+pub struct ILuaShared {}
+
+#[allow(dead_code)]
+impl ILuaShared {
+    #[virtual_index(6)]
+    pub fn get_lua_interface(&mut self, state: LuaState) -> *mut c_void {}
+
+    pub fn from_ptr(ptr: *mut c_void) -> &'static ILuaShared {
+        unsafe { mem::transmute(ptr) }
+    }
+
+    pub fn from_factory(factory: CreateInterfaceFn) -> Result<&'static ILuaShared, i32> {
+        let mut result = 0;
+        let ptr = unsafe { factory(b"LUASHARED003\0".as_ptr() as _, &mut result) };
+        if ptr.is_null() {
+            Err(result)
+        } else {
+            Ok(ILuaShared::from_ptr(ptr))
+        }
+    }
+}

+ 3 - 4
server-plugin/src/lib.rs

@@ -5,13 +5,12 @@ use std::ffi::{c_void, CStr};
 pub use platform::current::BasePlugin;
 
 mod ifilesystem;
+mod iluashared;
 mod inetworkstringtable;
 mod platform;
 
-pub use inetworkstringtable::{
-    INetworkStringTable,
-    INetworkStringTableContainer
-};
+pub use inetworkstringtable::{INetworkStringTable, INetworkStringTableContainer};
+pub use iluashared::{ILuaShared, LuaState};
 
 #[repr(i32)]
 #[derive(Debug, Clone, Copy)]