Skip to content

Commit

Permalink
Fix lua dumps in directories
Browse files Browse the repository at this point in the history
Have no idea how I overlooked this but lua dumps were broken if they weren't at the top level of the dump folder.

* All files dumped now automatically have .lua attached (even if they didn't already have it)
* Initial plugin code
* Bump to 1.0.0-beta5
  • Loading branch information
Vurv78 committed Feb 21, 2022
1 parent e17ec57 commit b175eba
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion autorun/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autorun"
version = "1.0.0-beta4"
version = "1.0.0-beta5"
authors = ["Vurv78 <[email protected]>"]
edition = "2021"
publish = false
Expand Down
1 change: 1 addition & 0 deletions autorun/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub static DUMP_DIR: &str = concat!(adir!(), "/lua_dumps");
#[cfg(feature = "logging")]
pub static LOG_DIR: &str = concat!(adir!(), "/logs");
pub static INCLUDE_DIR: &str = concat!(adir!(), "/scripts");
pub static PLUGIN_DIR: &str = concat!(adir!(), "/plugins");

pub static AUTORUN_PATH: &str = concat!(adir!(), "/autorun.lua");
pub static HOOK_PATH: &str = concat!(adir!(), "/hook.lua");
Expand Down
8 changes: 4 additions & 4 deletions autorun/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ fn loadbufferx_hook(l: LuaState, code: LuaString, len: usize, identifier: LuaStr
if curtime < CONNECTED.load(Ordering::Relaxed) {
debug!("Curtime is less than last time connected, assuming startup");
startup = true;
CONNECTED.store(curtime, Ordering::Relaxed);
} else {
// Awful
CONNECTED.store(curtime, Ordering::Relaxed);
}

// Awful
CONNECTED.store(curtime, Ordering::Relaxed);

let raw_path = unsafe { CStr::from_ptr(identifier) };
let path = &raw_path.to_string_lossy()[1..]; // Remove the @ from the beginning of the path

Expand Down Expand Up @@ -106,6 +105,7 @@ pub fn dispatch(l: LuaState, startup: bool, path: &str, ip: LuaString, mut code:
// Will be reset by JoinServer.
let ar_path = configs::path(configs::AUTORUN_PATH);
trace!("Running autorun script at {}", ar_path.display());

if let Ok(script) = fs::read_to_string(&ar_path) {
// Try to run here
if let Err(why) = lua::run_env(&script, env) {
Expand Down
1 change: 1 addition & 0 deletions autorun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod global;
mod hooks;
mod lua;
mod util;
mod plugins;

use logging::*;

Expand Down
44 changes: 44 additions & 0 deletions autorun/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::configs::PLUGIN_DIR;

#[derive(Debug, thiserror::Error)]
pub enum PluginError {
#[error("IO Error: {0}")]
IO(#[from] std::io::Error)
}

#[non_exhaustive]
pub enum PluginLanguage {
// In the future there could be more languages like Teal or Expressive
Lua
}

impl Default for PluginLanguage {
fn default() -> Self { Self::Lua }
}

#[derive(Default)]
pub struct PluginSettings {
language: PluginLanguage
}

pub struct Plugin {
name: String,
path: std::path::PathBuf,

// Path to entrypoint file.
autorun: std::path::PathBuf,

// Retrieved from plugin.toml
settings: PluginSettings
}

// Plugin system
pub fn find() -> Result<(), PluginError> {
let dir = std::fs::read_dir( PLUGIN_DIR )?;

Ok(())
}

pub fn exec() -> Result<(), PluginError> {
Ok(())
}
23 changes: 7 additions & 16 deletions autorun/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,17 @@ pub fn get_handle<S: AsRef<str>>(location: &str, server_ip: S) -> Result<File, H
};

let server_ip = server_ip.as_ref();
let mut lua_run_path = PathBuf::from(location);

let extension = match lua_run_path.extension() {
Some(ext) => {
match ext.to_str() {
Some(ext) if ext == "lua" => "lua", // Using guards check if the extension is lua, else it will fall under _.
_ => "txt",
}
}
None => "txt",
};

lua_run_path.set_extension(extension);

let file_loc = &*configs::path(configs::DUMP_DIR)
.join(strip_invalid(server_ip));

fs::create_dir_all(file_loc)?;
let mut path = file_loc.join(location);

let dir = path.parent().unwrap_or(file_loc);
fs::create_dir_all(&dir)?;

path.set_extension("lua");

Ok( File::create( file_loc.join(location) )? )
Ok( File::create( path )? )
}

// Removes basic invalid filename stuff
Expand Down

0 comments on commit b175eba

Please sign in to comment.