diff --git a/CHANGELOG.md b/CHANGELOG.md index f6b3797..92e1415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * **Breaking:** `Instance.new` now only works for instances that actually exist. * Added `Instance:Clone()` for copying instances all over the place, as is Roblox tradition. ([#12](https://github.com/rojo-rbx/remodel/issues/12)) * Added `DataModel:GetService()` for finding services and creating them if they don't exist, like Roblox does. ([#10](https://github.com/rojo-rbx/remodel/issues/10)) +* Added `remodel.getRawProperty(instance, name)` for an initial stab at reading property values. * Fixed Remodel dropping unknown properties when reading/writing XML models. This should make Remodel's behavior line up with Rojo. * Improved error messages in preparation for [#7](https://github.com/rojo-rbx/remodel/issues/7) to be fixed upstream. diff --git a/src/remodel_api/remodel.rs b/src/remodel_api/remodel.rs index a4af662..f16c989 100644 --- a/src/remodel_api/remodel.rs +++ b/src/remodel_api/remodel.rs @@ -7,9 +7,9 @@ use std::{ sync::Arc, }; -use rbx_dom_weak::{RbxInstanceProperties, RbxTree}; +use rbx_dom_weak::{RbxInstanceProperties, RbxTree, RbxValue}; use reqwest::header::{CONTENT_TYPE, COOKIE, USER_AGENT}; -use rlua::{Context, UserData, UserDataMethods}; +use rlua::{Context, ToLua, UserData, UserDataMethods}; use super::LuaInstance; use crate::remodel_context::RemodelContext; @@ -297,10 +297,37 @@ impl Remodel { ))) } } + + fn get_raw_property<'a>( + context: Context<'a>, + lua_instance: LuaInstance, + name: &str, + ) -> rlua::Result> { + let tree = lua_instance.tree.lock().unwrap(); + + let instance = tree.get_instance(lua_instance.id).ok_or_else(|| { + rlua::Error::external("Cannot call remodel.GetRawProperty on a destroyed instance.") + })?; + + match instance.properties.get(name) { + Some(value) => match value { + RbxValue::String { value } => value.as_str().to_lua(context), + _ => unimplemented!(), + }, + None => Ok(rlua::Value::Nil), + } + } } impl UserData for Remodel { fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_function( + "getRawProperty", + |context, (instance, name): (LuaInstance, String)| { + Self::get_raw_property(context, instance, &name) + }, + ); + methods.add_function("readPlaceFile", |context, lua_path: String| { let path = Path::new(&lua_path); diff --git a/test-scripts/get-raw-property.lua b/test-scripts/get-raw-property.lua new file mode 100644 index 0000000..c69f165 --- /dev/null +++ b/test-scripts/get-raw-property.lua @@ -0,0 +1,7 @@ +local model = remodel.readModelFile("test-models/folder-and-value.rbxmx")[1] + +local stringValue = model.String +assert(stringValue.ClassName == "StringValue") + +local value = remodel.getRawProperty(stringValue, "Value") +assert(value == "Hello") \ No newline at end of file