Skip to content

Commit

Permalink
Add support for MessagePack file type
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed May 3, 2024
1 parent 57cd36f commit 46cbdc0
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Added

- Support for MessagePack (`.msgpack`) - binary format, great for storing big amount of data

## [2.0.1] - 2024-05-02

### Fixed
Expand Down
26 changes: 24 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ clap = { version = "4.4.1", features = ["derive", "cargo"] }
serde = { version = "1.0.189", features = ["derive"] }
tokio = { version = "1.32.0", features = ["full"] }
reqwest = { version = "0.11.23", features = ["blocking", "rustls-tls"] }
rmpv = {version = "1.3.0", features = ["with-serde"] }
self_update = { version = "0.39.0", features = [
"compression-zip-deflate",
"rustls",
Expand Down
1 change: 1 addition & 0 deletions src/cli/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Exec {
code: code.to_owned(),
focus: self.focus,
})?;

let response = Client::default()
.post(url)
.header(CONTENT_TYPE, "application/msgpack")
Expand Down
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pub fn default_sync_rules() -> &'static Vec<SyncRule> {
SyncRule::new(Middleware::TomlModule)
.with_pattern("*.toml")
.with_child_pattern(".src.toml"),
SyncRule::new(Middleware::MsgpackModule)
.with_pattern("*.msgpack")
.with_child_pattern(".src.msgpack"),
// Model files, Argon only
SyncRule::new(Middleware::JsonModel)
.with_pattern("*.model.json")
Expand Down
5 changes: 5 additions & 0 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod dir;
pub mod json;
pub mod json_model;
pub mod lua;
pub mod msgpack;
pub mod project;
pub mod rbxm;
pub mod rbxmx;
Expand All @@ -42,8 +43,10 @@ pub enum Middleware {

StringValue,
LocalizationTable,

JsonModule,
TomlModule,
MsgpackModule,

JsonModel,
RbxmModel,
Expand All @@ -68,8 +71,10 @@ impl Middleware {
//
Middleware::StringValue => txt::read_txt(path, vfs),
Middleware::LocalizationTable => csv::read_csv(path, vfs),
//
Middleware::JsonModule => json::read_json(path, vfs),
Middleware::TomlModule => toml::read_toml(path, vfs),
Middleware::MsgpackModule => msgpack::read_msgpack(path, vfs),
//
Middleware::JsonModel => json_model::read_json_model(path, vfs),
Middleware::RbxmModel => rbxm::read_rbxm(path, vfs),
Expand Down
89 changes: 89 additions & 0 deletions src/middleware/msgpack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use anyhow::Result;
use rbx_dom_weak::types::Variant;
use rmp_serde::Deserializer;
use rmpv::Value;
use std::{collections::HashMap, path::Path};

use crate::{core::snapshot::Snapshot, vfs::Vfs};

#[profiling::function]
pub fn read_msgpack(path: &Path, vfs: &Vfs) -> Result<Snapshot> {
let msgpack = vfs.read(path)?;

let mut deserializer = Deserializer::from_read_ref(&msgpack).with_human_readable();
let msgpack: Value = serde::Deserialize::deserialize(&mut deserializer)?;

let lua = format!("return {}", msgpack_to_lua(&msgpack));

let mut properties = HashMap::new();
properties.insert(String::from("Source"), Variant::String(lua));

Ok(Snapshot::new().with_class("ModuleScript").with_properties(properties))
}

fn msgpack_to_lua(value: &Value) -> String {
let mut lua = String::new();

match value {
Value::Nil => lua.push_str("nil"),
Value::Boolean(b) => lua.push_str(&b.to_string()),
Value::Integer(i) => lua.push_str(&i.to_string()),
Value::F32(f) => {
if (*f as f64).is_infinite() {
lua.push_str("math.huge")
} else {
lua.push_str(&f.to_string())
}
}
Value::F64(f) => {
if f.is_infinite() {
lua.push_str("math.huge")
} else {
lua.push_str(&f.to_string())
}
}
Value::String(s) => lua.push_str(&format!("\"{}\"", &escape_chars(s.as_str().unwrap_or_default()))),
Value::Binary(b) => lua.push_str(&String::from_utf8_lossy(b)),
Value::Array(a) => {
lua.push('{');

for v in a {
lua.push_str(&msgpack_to_lua(v));
lua.push(',');
}

lua.push('}');
}
Value::Map(t) => {
lua.push('{');

for (k, v) in t {
lua.push_str(&format!("[{}] = ", &msgpack_to_lua(k)));
lua.push_str(&msgpack_to_lua(v));
lua.push(',');
}

lua.push('}');
}
Value::Ext(_, _) => {}
}

lua
}

fn escape_chars(string: &str) -> String {
let mut validated = String::new();

for char in string.chars() {
match char {
'\n' => validated.push_str("\\n"),
'\t' => validated.push_str("\\t"),
'\r' => validated.push_str("\\r"),
'\\' => validated.push_str("\\\\"),
'"' => validated.push_str("\\\""),
_ => validated.push(char),
}
}

validated
}

0 comments on commit 46cbdc0

Please sign in to comment.