Skip to content

Commit

Permalink
Get the image ids from decals & use use64 instead of String for asset…
Browse files Browse the repository at this point in the history
… ids
  • Loading branch information
jackTabsCode committed Mar 7, 2024
1 parent 1f8916f commit 3616bbf
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Release
on:
push:
tags: ["*"]
workflow_dispatch:

jobs:
windows:
Expand Down
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ clap = { version = "4.5.1", features = ["derive"] }
console = { version = "0.15.8", features = ["ansi-parsing"] }
log = "0.4.21"
rbxcloud = "0.6.1"
reqwest = { version = "0.11.24", features = ["gzip"] }
serde = { version = "1.0.197", features = ["derive"] }
serde-xml-rs = "0.6.0"
tokio = { version = "1.36.0" }
toml = "0.8.10"
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod upload;
#[derive(Debug, Serialize, Deserialize)]
struct FileEntry {
hash: String,
asset_id: Option<String>,
asset_id: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -78,15 +78,15 @@ async fn main() {
hasher.update(&bytes);
let hash = hasher.finalize().to_string();

let mut asset_id: Option<String> = None;
let mut asset_id: Option<u64> = None;

let existing = existing_lockfile.entries.get(path_str);

if let Some(existing_value) = existing {
if existing_value.hash != hash || existing_value.asset_id.is_none() {
changed = true;
} else {
asset_id = existing_value.asset_id.clone();
asset_id = existing_value.asset_id;
}
} else {
changed = true;
Expand Down Expand Up @@ -120,7 +120,7 @@ async fn main() {
file_entry
.asset_id
.as_ref()
.unwrap_or(&String::from("None"))
.expect("we never got an asset id?")
)
})
.collect::<Vec<String>>()
Expand Down
75 changes: 70 additions & 5 deletions src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,66 @@ use rbxcloud::rbx::assets::{
AssetUserCreator, CreateAssetParams, GetAssetParams,
};
use rbxcloud::rbx::error::Error;
use reqwest::Client;
use serde::Deserialize;
use serde_xml_rs::from_str;

pub async fn upload_asset(path: PathBuf, asset_type: AssetType, api_key: String) -> String {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Roblox {
item: Item,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Item {
properties: Properties,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Properties {
content: Content,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Content {
url: String,
}

static CONTENT_URL_PREFIX: &str = "http://www.roblox.com/asset/?id=";

async fn get_image_id(asset_id: u64) -> u64 {
let client = Client::new();
let url = format!("https://assetdelivery.roblox.com/v1/asset?id={}", asset_id);

let response = client
.get(url)
.send()
.await
.expect("failed to get image id");
let body = response
.text()
.await
.expect("failed to parse request body to text");

let roblox: Roblox =
from_str(&body).expect("failed to parse request body to Roblox XML format");

let id_str = roblox
.item
.properties
.content
.url
.strip_prefix(CONTENT_URL_PREFIX)
.unwrap()
.to_string();

id_str.parse::<u64>().unwrap()
}

pub async fn upload_asset(path: PathBuf, asset_type: AssetType, api_key: String) -> u64 {
let path_str = path.to_str().unwrap();

let create_params = CreateAssetParams {
Expand All @@ -21,17 +79,16 @@ pub async fn upload_asset(path: PathBuf, asset_type: AssetType, api_key: String)
}),
expected_price: None,
},
description: "Hey".to_string(),
description: "Uploaded by Asphalt".to_string(),
},
};
let operation = create_asset(&create_params).await.unwrap();

let id = operation
.path
.unwrap()
.split_once('/')
.strip_prefix("operations/")
.unwrap()
.1
.to_string();

let create_params = GetAssetParams {
Expand All @@ -44,7 +101,15 @@ pub async fn upload_asset(path: PathBuf, asset_type: AssetType, api_key: String)
Ok(asset_operation) => {
if let Some(done) = asset_operation.done {
if done {
return asset_operation.response.unwrap().asset_id;
let id_str = asset_operation.response.unwrap().asset_id;
let id = id_str.parse::<u64>().unwrap();

match asset_type {
AssetType::DecalPng | AssetType::DecalJpeg | AssetType::DecalBmp => {
return get_image_id(id).await
}
_ => return id,
}
}
}
}
Expand Down

0 comments on commit 3616bbf

Please sign in to comment.