core: Add CompatibilityRules struct for on-the-fly replacements for compatibility. Initially konggames -> kongregate.com rules

This commit is contained in:
Nathan Adams 2023-02-28 17:14:54 +01:00
parent f282331204
commit 1aedb7ff8d
4 changed files with 62 additions and 3 deletions

View File

@ -0,0 +1,42 @@
use regress::Regex;
#[derive(Debug, Clone)]
pub struct UrlRewriteRule {
pub pattern: Regex,
pub replacement: String,
}
#[derive(Debug, Default, Clone)]
pub struct CompatibilityRules {
swf_url_rewrite_rules: Vec<UrlRewriteRule>,
}
impl CompatibilityRules {
pub fn empty() -> Self {
Self::default()
}
pub fn builtin_rules() -> Self {
Self {
swf_url_rewrite_rules: vec![UrlRewriteRule {
pattern: Regex::new(r"//game(\d+).konggames.com").expect("Regex must compile"),
replacement: "//kongregate.com".to_string(),
}],
}
}
pub fn rewrite_swf_url(&self, original_url: String) -> String {
let mut url = original_url.clone();
for rule in &self.swf_url_rewrite_rules {
if let Some(found) = rule.pattern.find(&url) {
url.replace_range(found.range, &rule.replacement);
}
}
if original_url != url {
tracing::info!("Rewritten SWF url from {original_url} to {url}");
}
url
}
}

View File

@ -44,6 +44,7 @@ mod vminterface;
mod xml; mod xml;
pub mod backend; pub mod backend;
pub mod compatibility_rules;
pub mod config; pub mod config;
pub mod external; pub mod external;
pub mod stub; pub mod stub;

View File

@ -696,14 +696,19 @@ impl<'gc> Loader<'gc> {
})?; })?;
// The spoofed root movie URL takes precedence over the actual URL. // The spoofed root movie URL takes precedence over the actual URL.
let url = player let swf_url = player
.lock()
.unwrap()
.compatibility_rules()
.rewrite_swf_url(response.url);
let spoofed_or_swf_url = player
.lock() .lock()
.unwrap() .unwrap()
.spoofed_url() .spoofed_url()
.map(|u| u.to_string()) .map(|u| u.to_string())
.unwrap_or(response.url); .unwrap_or(swf_url);
let mut movie = SwfMovie::from_data(&response.body, url, None)?; let mut movie = SwfMovie::from_data(&response.body, spoofed_or_swf_url, None)?;
on_metadata(movie.header()); on_metadata(movie.header());
movie.append_parameters(parameters); movie.append_parameters(parameters);
player.lock().unwrap().set_root_movie(movie); player.lock().unwrap().set_root_movie(movie);

View File

@ -17,6 +17,7 @@ use crate::backend::{
storage::StorageBackend, storage::StorageBackend,
ui::{InputManager, MouseCursor, UiBackend}, ui::{InputManager, MouseCursor, UiBackend},
}; };
use crate::compatibility_rules::CompatibilityRules;
use crate::config::Letterbox; use crate::config::Letterbox;
use crate::context::{ActionQueue, ActionType, RenderContext, UpdateContext}; use crate::context::{ActionQueue, ActionType, RenderContext, UpdateContext};
use crate::context_menu::{ use crate::context_menu::{
@ -296,6 +297,9 @@ pub struct Player {
/// The root SWF URL provided to ActionScript. If None, /// The root SWF URL provided to ActionScript. If None,
/// the actual loaded url will be used /// the actual loaded url will be used
spoofed_url: Option<String>, spoofed_url: Option<String>,
/// Any compatibility rules to apply for this movie.
compatibility_rules: CompatibilityRules,
} }
impl Player { impl Player {
@ -1886,6 +1890,10 @@ impl Player {
self.spoofed_url.as_deref() self.spoofed_url.as_deref()
} }
pub fn compatibility_rules(&self) -> &CompatibilityRules {
&self.compatibility_rules
}
pub fn log_backend(&self) -> &Log { pub fn log_backend(&self) -> &Log {
&self.log &self.log
} }
@ -1931,6 +1939,7 @@ pub struct PlayerBuilder {
warn_on_unsupported_content: bool, warn_on_unsupported_content: bool,
load_behavior: LoadBehavior, load_behavior: LoadBehavior,
spoofed_url: Option<String>, spoofed_url: Option<String>,
compatibility_rules: CompatibilityRules,
player_version: Option<u8>, player_version: Option<u8>,
quality: StageQuality, quality: StageQuality,
sandbox_type: SandboxType, sandbox_type: SandboxType,
@ -1971,6 +1980,7 @@ impl PlayerBuilder {
warn_on_unsupported_content: true, warn_on_unsupported_content: true,
load_behavior: LoadBehavior::Streaming, load_behavior: LoadBehavior::Streaming,
spoofed_url: None, spoofed_url: None,
compatibility_rules: CompatibilityRules::builtin_rules(),
player_version: None, player_version: None,
quality: StageQuality::High, quality: StageQuality::High,
sandbox_type: SandboxType::LocalTrusted, sandbox_type: SandboxType::LocalTrusted,
@ -2198,6 +2208,7 @@ impl PlayerBuilder {
self_reference: self_ref.clone(), self_reference: self_ref.clone(),
load_behavior: self.load_behavior, load_behavior: self.load_behavior,
spoofed_url: self.spoofed_url.clone(), spoofed_url: self.spoofed_url.clone(),
compatibility_rules: self.compatibility_rules.clone(),
stub_tracker: StubCollection::new(), stub_tracker: StubCollection::new(),
// GC data // GC data