-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for MessagePack file type
- Loading branch information
Showing
7 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |