Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
//! Each network's package around the one script (docs/playables.feature, the Background's table): the page every
//! network gets, with that network's own tags, as one HTML file or a ZIP, checked against its cap.
use super::R;
use std::io::Write;
use std::path::{Path, PathBuf};
pub const NETWORKS: &[&str] = &["applovin", "unity", "google", "meta", "moloco", "mintegral", "tiktok", "liftoff", "preview"];
/// Every network's cap, 5 MB (TikTok's counted zipped, which is what's written)
pub const CAP: u64 = 5 * 1024 * 1024;
/// What one network got: the file and its size
pub struct Written {
pub network: String,
pub file: PathBuf,
pub bytes: u64,
}
/// The settings a page starts with, as the globals the host reads (sdk/playable network.ts, host/main.tsx): the network,
/// the store links, the end card's words
pub fn preamble(network: &str, store: &str, card: &str) -> String {
format!("var __RAEA_NETWORK__={},__RAEA_STORE__={store},__RAEA_PLAYABLE__={card};", serde_json::to_string(network).unwrap_or_default())
}
/// The page: the network's head and body tags, then the script (a page's inline script ends at the first "</script")
pub fn page(title: &str, head: &str, body: &str, script: &str) -> String {
let title = title.replace('&', "&").replace('<', "<");
format!(
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover\">\n<title>{title}</title>\n{head}\n</head>\n<body>\n{body}\n<script>\n{}\n</script>\n</body>\n</html>\n",
script.replace("</script", "<\\/script")
)
}
fn zip(file: &Path, entries: &[(String, String)]) -> R<()> {
let f = std::fs::File::create(file).map_err(|e| format!("{}: {e}", file.display()))?;
let mut w = zip::ZipWriter::new(f);
let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
for (name, text) in entries {
w.start_file(name.as_str(), o).map_err(|e| e.to_string())?;
w.write_all(text.as_bytes()).map_err(|e| e.to_string())?;
}
w.finish().map_err(|e| e.to_string())?;
Ok(())
}
/// `network`'s package of `script` (its preamble already first) written into `out` as NAME-NETWORK.html or .zip
pub fn write(network: &str, name: &str, script: &str, out: &Path, orientation: &str) -> R<Written> {
let html = |head: &str, body: &str| page(name, head, body, script);
let file = match network {
// a ZIP with index.html; exitapi.js and the orientation tag in the head (Google Ads help 9981650)
"google" => {
let o = if orientation == "both" { "portrait,landscape" } else { orientation };
let head = format!("<meta name=\"ad.orientation\" content=\"{o}\">\n<script type=\"text/javascript\" src=\"https://tpc.googlesyndication.com/pagead/gadgets/html5/api/exitapi.js\"></script>");
let f = out.join(format!("{name}-google.zip"));
zip(&f, &[("index.html".into(), html(&head, ""))])?;
f
}
// a ZIP with index.html and config.json; TikTok's playable SDK before the game's script (TikTok Ads help)
"tiktok" => {
let o = match orientation {
"portrait" => 1,
"landscape" => 2,
_ => 0,
};
let body = "<script src=\"https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js\"></script>";
let f = out.join(format!("{name}-tiktok.zip"));
zip(&f, &[("index.html".into(), html("", body)), ("config.json".into(), format!("{{\"playable_orientation\": {o}}}\n"))])?;
f
}
// a ZIP holding a folder and a page of the same name
"mintegral" => {
let f = out.join(format!("{name}-mintegral.zip"));
zip(&f, &[(format!("{name}/{name}.html"), html("", ""))])?;
f
}
// one HTML file, everything inlined; MRAID is injected by the network's webview (Meta: data URIs only)
"applovin" | "unity" | "meta" | "moloco" | "liftoff" | "preview" => {
let f = out.join(format!("{name}-{network}.html"));
std::fs::write(&f, html("", "")).map_err(|e| format!("{}: {e}", f.display()))?;
f
}
other => return Err(format!("raea playable: no network {other} (one of {})", NETWORKS.join(", "))),
};
let bytes = std::fs::metadata(&file).map(|m| m.len()).unwrap_or(0);
Ok(Written { network: network.into(), file, bytes })
}
#[cfg(test)]
mod tests {
#[test]
fn a_script_cannot_end_its_page_early() {
let p = super::page("t", "", "", "a('</script>')");
assert_eq!(p.matches("</script").count(), 1, "{p}");
}
#[test]
fn the_preamble_names_the_network_as_a_string() {
assert_eq!(super::preamble("applovin", "{}", "{}"), "var __RAEA_NETWORK__=\"applovin\",__RAEA_STORE__={},__RAEA_PLAYABLE__={};");
}
}