Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the new Open Cloud API #504

Merged
merged 11 commits into from
Apr 19, 2022
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Rojo Changelog

## Unreleased Changes
* Added support for the new Open Cloud API when uploading. ([#486])
* Fixed Rojo's interactions with properties enabled by FFlags that are not yet enabled. ([#493])
* Improved output in Roblox Studio plugin when bad property data is encountered.
* Reintroduced support for CFrame shorthand syntax in Rojo project and `.meta.json` files, matching Rojo 6. ([#430])
* Connection settings are now remembered when reconnecting in Roblox Studio. ([#500])
* Updated reflection database to Roblox v503.

[#486]: https://github.com/rojo-rbx/rojo/issues/486
[#430]: https://github.com/rojo-rbx/rojo/issues/430
[#493]: https://github.com/rojo-rbx/rojo/pull/493
[#500]: https://github.com/rojo-rbx/rojo/pull/500
Expand Down Expand Up @@ -472,4 +474,4 @@ This is a general maintenance release for the Rojo 0.5.x release series.
* More robust syncing with a new reconciler

## [0.1.0](https://github.com/rojo-rbx/rojo/releases/tag/v0.1.0) (November 29, 2017)
* Initial release, functionally very similar to [rbxfs](https://github.com/LPGhatguy/rbxfs)
* Initial release, functionally very similar to [rbxfs](https://github.com/LPGhatguy/rbxfs)
83 changes: 79 additions & 4 deletions src/cli/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ pub struct UploadCommand {
#[structopt(long)]
pub cookie: Option<String>,

/// Used for the new Open Cloud API. Still experimental.
#[structopt(long = "api_key")]
pub api_key: Option<String>,

/// Required when using the Open Cloud API.
#[structopt(long = "universe_id")]
pub universe_id: Option<u64>,
VernandoGames marked this conversation as resolved.
Show resolved Hide resolved

/// Asset ID to upload to.
#[structopt(long = "asset_id")]
pub asset_id: u64,
Expand All @@ -33,9 +41,30 @@ impl UploadCommand {
pub fn run(self) -> Result<(), anyhow::Error> {
let project_path = resolve_path(&self.project);

let cookie = self.cookie.or_else(get_auth_cookie).context(
"Rojo could not find your Roblox auth cookie. Please pass one via --cookie.",
)?;
let use_open_cloud = self.api_key.is_some();

// Validate differently depending on if we're trying to use open cloud or not.
// If there's a better way of doing this, please do so.
let api_key = if use_open_cloud {
self.api_key
.context("Rojo could not find your api key. Please pass one via --api_key")?
} else {
"undefined".to_string()
};
let universe_id = if use_open_cloud {
self.universe_id.context(
"A Universe id is required when using the Open Cloud API. Please pass one via --universe_id"
)?
} else {
0
};
let cookie = if use_open_cloud {
"undefined".to_string()
} else {
self.cookie.or_else(get_auth_cookie).context(
"Rojo could not find your Roblox auth cookie. Please pass one via --cookie.",
)?
};
VernandoGames marked this conversation as resolved.
Show resolved Hide resolved

let vfs = Vfs::new_default();

Expand All @@ -54,7 +83,11 @@ impl UploadCommand {

log::trace!("Encoding binary model");
rbx_binary::to_writer(&mut buffer, tree.inner(), &encode_ids)?;
do_upload(buffer, self.asset_id, &cookie)
if use_open_cloud {
return do_upload_open_cloud(buffer, universe_id, self.asset_id, &api_key);
} else {
return do_upload(buffer, self.asset_id, &cookie);
}
VernandoGames marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -125,3 +158,45 @@ fn do_upload(buffer: Vec<u8>, asset_id: u64, cookie: &str) -> anyhow::Result<()>

Ok(())
}

/// Implementation of do_upload that supports the new open cloud api.
/// I'm sure there's a better of doing this. Please correct it if so.
/// see https://developer.roblox.com/en-us/articles/open-cloud
VernandoGames marked this conversation as resolved.
Show resolved Hide resolved
fn do_upload_open_cloud(
buffer: Vec<u8>,
universe_id: u64,
asset_id: u64,
api_key: &str,
) -> anyhow::Result<()> {
let url = format!(
"https://apis.roblox.com/universes/v1/{}/places/{}/versions?versionType=Published",
universe_id, asset_id
);

let client = reqwest::Client::new();

let build_request = move || {
client
.post(&url)
.header("x-api-key", api_key)
.header(CONTENT_TYPE, "application/xml")
.header(ACCEPT, "application/json")
.body(buffer)
};

log::debug!("Uploading to Roblox...");
let mut response = build_request().send()?;

// Previously would've attempted to complete a CSRF challenge.
// That should not be required using this API.(hopefully)
VernandoGames marked this conversation as resolved.
Show resolved Hide resolved

let status = response.status();
if !status.is_success() {
bail!(
"The Roblox API returned an unexpected error: {}",
response.text()?
);
}

Ok(())
}