From 0ad5aaaa7f05112f8fb0caaddb67fd8d311f7d51 Mon Sep 17 00:00:00 2001 From: jeparlefrancais <35781636+jeparlefrancais@users.noreply.github.com> Date: Mon, 10 May 2021 20:10:58 -0400 Subject: [PATCH] Add GetFullName() method on instances (#49) * implement instance:GetFullName() * add entry to changelog * use as_str --- CHANGELOG.md | 1 + src/roblox_api/instance.rs | 26 ++++++++++++++++++++++++++ test-scripts/get-full-name.lua | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 test-scripts/get-full-name.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d184cc..c522394 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased Changes +* Added Instance:GetFullName() ([#49](https://github.com/rojo-rbx/remodel/pull/49)) * Added `Instance:FindFirstChildOfClass()` ([#50](https://github.com/rojo-rbx/remodel/pull/50)) * Added support for CFrame ([#48](https://github.com/rojo-rbx/remodel/pull/48)) * Added support for Vector3, and improved Vector3int16 ([#46](https://github.com/rojo-rbx/remodel/pull/46)) diff --git a/src/roblox_api/instance.rs b/src/roblox_api/instance.rs index 022e287..855e411 100644 --- a/src/roblox_api/instance.rs +++ b/src/roblox_api/instance.rs @@ -118,6 +118,28 @@ impl LuaInstance { Ok(child) } + fn get_full_name(&self) -> rlua::Result { + let tree = self.tree.lock().unwrap(); + + let instance = tree.get_by_ref(self.id).ok_or_else(|| { + rlua::Error::external("Cannot call GetFullName() on a destroyed instance") + })?; + + let mut names = vec![instance.name.as_str()]; + let mut current = instance.parent(); + + while let Some(parent_instance) = tree.get_by_ref(current) { + if current != tree.root_ref() && parent_instance.class != "DataModel" { + names.push(parent_instance.name.as_str()); + } + current = parent_instance.parent(); + } + + names.reverse(); + + Ok(names.join(".")) + } + fn get_descendants(&self) -> rlua::Result> { let tree = self.tree.lock().unwrap(); @@ -373,6 +395,10 @@ impl UserData for LuaInstance { this.find_first_child_of_class(&class) }); + methods.add_method("GetFullName", |_context, this, _args: ()| { + this.get_full_name() + }); + methods.add_method("GetChildren", |_context, this, _args: ()| { this.get_children() }); diff --git a/test-scripts/get-full-name.lua b/test-scripts/get-full-name.lua new file mode 100644 index 0000000..4fab7b0 --- /dev/null +++ b/test-scripts/get-full-name.lua @@ -0,0 +1,21 @@ +local function assertString(result, expected) + assert(result == expected, ('expected `%s` but got `%s`'):format(expected, result)) +end + +local FOLDER_A_NAME = "foo" +local FOLDER_B_NAME = "bar" + +local folderA = Instance.new("Folder") +folderA.Name = FOLDER_A_NAME + +assertString(folderA:GetFullName(), FOLDER_A_NAME) + +local folderB = Instance.new("Folder") +folderB.Name = FOLDER_B_NAME +folderB.Parent = folderA + +assertString(folderB:GetFullName(), FOLDER_A_NAME .. '.' .. FOLDER_B_NAME) + +local game = remodel.readPlaceFile("test-models/place-with-models.rbxlx") + +assertString(game.Workspace.Baseplate:GetFullName(), "Workspace.Baseplate")