Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Add remodel.getRawProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Sep 26, 2019
1 parent 3818195 commit 468b6b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
31 changes: 29 additions & 2 deletions src/remodel_api/remodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -297,10 +297,37 @@ impl Remodel {
)))
}
}

fn get_raw_property<'a>(
context: Context<'a>,
lua_instance: LuaInstance,
name: &str,
) -> rlua::Result<rlua::Value<'a>> {
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);

Expand Down
7 changes: 7 additions & 0 deletions test-scripts/get-raw-property.lua
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 468b6b5

Please sign in to comment.