Skip to content

Commit

Permalink
Get Studio version from plugin request in rbx_reflector (#307)
Browse files Browse the repository at this point in the history
Closes #304.
  • Loading branch information
kennethloeffler authored Jul 1, 2023
1 parent 05e9c6c commit 58c0124
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
13 changes: 12 additions & 1 deletion rbx_reflector/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ local HttpService = game:GetService("HttpService")

local SERVER_URL = "http://localhost:22073"

HttpService:PostAsync(SERVER_URL, "")
local version = string.split(version(), ".")
local major = tonumber(version[1])
local minor = tonumber(version[2])
local patch = tonumber(version[3])
local build = tonumber(version[4])

HttpService:PostAsync(
SERVER_URL,
HttpService:JSONEncode({
version = { major, minor, patch, build },
})
)
25 changes: 16 additions & 9 deletions rbx_reflector/src/cli/defaults_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use clap::Parser;
use innerput::{Innerput, Key, Keyboard};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use roblox_install::RobloxStudio;
use serde::Deserialize;
use tempfile::tempdir;
use tiny_http::Response;

Expand All @@ -31,7 +32,7 @@ pub struct DefaultsPlaceSubcommand {
}

impl DefaultsPlaceSubcommand {
pub fn run(&self) -> anyhow::Result<()> {
pub fn run(&self) -> anyhow::Result<StudioInfo> {
if self.output.extension().unwrap_or_default() != "rbxlx" {
bail!("The output path must have a .rbxlx extension")
}
Expand All @@ -45,15 +46,15 @@ impl DefaultsPlaceSubcommand {
let temp_place_path = temp_dir.path().join("defaults-place.rbxlx");

generate_place_with_all_classes(&temp_place_path, &dump)?;
save_place_in_studio(&temp_place_path)?;
let studio_info = save_place_in_studio(&temp_place_path)?;

fs::copy(temp_place_path, &self.output)?;

Ok(())
Ok(studio_info)
}
}

fn save_place_in_studio(path: &PathBuf) -> anyhow::Result<()> {
fn save_place_in_studio(path: &PathBuf) -> anyhow::Result<StudioInfo> {
let studio_install =
RobloxStudio::locate().context("Could not locate Roblox Studio install")?;

Expand All @@ -67,7 +68,7 @@ fn save_place_in_studio(path: &PathBuf) -> anyhow::Result<()> {

log::info!("Waiting for Roblox Studio to re-save place...");

plugin_injector.wait_for_response()?;
let studio_info = plugin_injector.wait_for_response()?;

let (tx, rx) = mpsc::channel();
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
Expand Down Expand Up @@ -97,7 +98,7 @@ fn save_place_in_studio(path: &PathBuf) -> anyhow::Result<()> {

studio_process.kill()?;

Ok(())
Ok(studio_info)
}

fn generate_place_with_all_classes(path: &PathBuf, dump: &Dump) -> anyhow::Result<()> {
Expand Down Expand Up @@ -203,6 +204,11 @@ pub struct PluginInjector<'a> {
studio_install: &'a RobloxStudio,
}

#[derive(Debug, Deserialize)]
pub struct StudioInfo {
pub version: [u32; 4],
}

impl<'a> PluginInjector<'a> {
pub fn start(studio_install: &'a RobloxStudio) -> anyhow::Result<Self> {
let http_server = tiny_http::Server::http("0.0.0.0:22073").unwrap();
Expand All @@ -221,15 +227,16 @@ impl<'a> PluginInjector<'a> {
})
}

pub fn wait_for_response(self) -> anyhow::Result<()> {
let request = match self.http_server.recv_timeout(Duration::from_secs(30))? {
pub fn wait_for_response(self) -> anyhow::Result<StudioInfo> {
let mut request = match self.http_server.recv_timeout(Duration::from_secs(30))? {
Some(request) => request,
None => bail!("Plugin did not send a response within 30 seconds"),
};

let studio_info: StudioInfo = serde_json::from_reader(request.as_reader())?;
request.respond(Response::empty(200))?;

Ok(())
Ok(studio_info)
}
}

Expand Down
5 changes: 2 additions & 3 deletions rbx_reflector/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::{
api_dump::{Dump, DumpClassMember, Security, Tag, ValueCategory},
defaults::apply_defaults,
patches::Patches,
studio_version,
};

use super::{defaults_place::DefaultsPlaceSubcommand, dump::DumpSubcommand};
Expand Down Expand Up @@ -49,7 +48,7 @@ impl GenerateSubcommand {
let contents = fs::read_to_string(&api_dump_path).context("Could not read API dump")?;
let dump = serde_json::from_str(&contents).context("Invalid API dump")?;

DefaultsPlaceSubcommand {
let studio_info = DefaultsPlaceSubcommand {
api_dump: api_dump_path,
output: defaults_place_path.clone(),
}
Expand All @@ -66,7 +65,7 @@ impl GenerateSubcommand {

apply_defaults(&mut database, &defaults_place_path)?;

database.version = studio_version::get_studio_version()?;
database.version = studio_info.version;

for path in &self.output {
let extension = path.extension().unwrap_or_default().to_str();
Expand Down
2 changes: 1 addition & 1 deletion rbx_reflector/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Args {
pub fn run(&self) -> anyhow::Result<()> {
match &self.subcommand {
Subcommand::Dump(sub) => sub.run(),
Subcommand::DefaultsPlace(sub) => sub.run(),
Subcommand::DefaultsPlace(sub) => sub.run().map(|_| ()),
Subcommand::Generate(sub) => sub.run(),
Subcommand::Values(sub) => sub.run(),
}
Expand Down
1 change: 0 additions & 1 deletion rbx_reflector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod api_dump;
mod cli;
mod defaults;
mod patches;
mod studio_version;

use clap::Parser;

Expand Down
27 changes: 0 additions & 27 deletions rbx_reflector/src/studio_version.rs

This file was deleted.

0 comments on commit 58c0124

Please sign in to comment.