Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ thiserror = { workspace = true }
chrono = { workspace = true, features = ["clock"] }
web-time = "1.1.0"
encoding_rs = { workspace = true }
rand = { version = "0.9.1", features = ["std", "small_rng", "os_rng"], default-features = false }
rand = { version = "0.9.1", features = ["std", "os_rng"], default-features = false }
serde = { workspace = true }
serde_json = { workspace = true, features = ["preserve_order"] }
nellymoser-rs = { git = "https://github.com/ruffle-rs/nellymoser", rev = "073eb48d907201f46dea0c8feb4e8d9a1d92208c", optional = true }
Expand Down
3 changes: 1 addition & 2 deletions core/src/avm1/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::{avm_error, avm_warn};
use gc_arena::{Gc, Mutation};
use indexmap::IndexMap;
use rand::Rng;
use ruffle_macros::istr;
use std::borrow::Cow;
use std::cell::Cell;
Expand Down Expand Up @@ -1828,7 +1827,7 @@
// The max value is clamped to the range [0, 2^31 - 1).
let max = self.context.avm1.pop().coerce_to_f64(self)? as i32;
let result = if max > 0 {
self.context.rng.random_range(0..max)
self.context.rng.generate_random_number() % max

Check warning on line 1830 in core/src/avm1/activation.rs

View workflow job for this annotation

GitHub Actions / Coverage Report

Coverage

Uncovered line (1830)
} else {
0
};
Expand Down
3 changes: 1 addition & 2 deletions core/src/avm1/globals/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::avm1::property_decl::{DeclContext, Declaration};
use crate::avm1::{Object, Value};

use rand::Rng;
use std::f64::consts;

macro_rules! wrap_std {
Expand Down Expand Up @@ -161,7 +160,7 @@
// See https://github.com/adobe/avmplus/blob/858d034a3bd3a54d9b70909386435cf4aec81d21/core/MathUtils.cpp#L1731C24-L1731C44
// This generated a restricted set of 'f64' values, which some SWFs implicitly rely on.
const MAX_VAL: u32 = 0x7FFFFFFF;
let rand = activation.context.rng.random_range(0..MAX_VAL);
let rand = activation.context.rng.generate_random_number();

Check warning on line 163 in core/src/avm1/globals/math.rs

View workflow job for this annotation

GitHub Actions / Coverage Report

Coverage

Uncovered line (163)
Ok(((rand as f64) / (MAX_VAL as f64 + 1f64)).into())
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/avm2/globals/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::avm2::parameters::ParametersExt;
use crate::avm2::value::Value;
use crate::avm2::{ClassObject, Error};
use num_traits::ToPrimitive;
use rand::Rng;

macro_rules! wrap_std {
($name:ident, $std:expr) => {
Expand Down Expand Up @@ -159,6 +158,6 @@ pub fn random<'gc>(
// See https://github.com/adobe/avmplus/blob/858d034a3bd3a54d9b70909386435cf4aec81d21/core/MathUtils.cpp#L1731C24-L1731C44
// This generated a restricted set of 'f64' values, which some SWFs implicitly rely on.
const MAX_VAL: u32 = 0x7FFFFFFF;
let rand = activation.context.rng.random_range(0..MAX_VAL);
let rand = activation.context.rng.generate_random_number();
Ok(((rand as f64) / (MAX_VAL as f64 + 1f64)).into())
}
66 changes: 66 additions & 0 deletions core/src/avm_rng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::locale::get_current_date_time;

// https://github.com/adobe/avmplus/blob/858d034a3bd3a54d9b70909386435cf4aec81d21/core/MathUtils.cpp#L1546
const C1: i32 = 1376312589;
const C2: i32 = 789221;
const C3: i32 = 15731;
const K_RANDOM_PURE_MAX: i32 = 0x7FFFFFFF;

const U_XOR_MASK: u32 = 0x48000000;

// This struct should not be cloned or copied.
#[derive(Debug, Default)]
pub struct AvmRng {
u_value: u32,
}

impl AvmRng {
fn init_with_seed(&mut self, seed: u32) {
self.u_value = seed;
}

fn random_fast_next(&mut self) -> i32 {
if (self.u_value & 1) != 0 {
self.u_value = (self.u_value >> 1) ^ U_XOR_MASK;
} else {
self.u_value >>= 1;
}
self.u_value as i32
}

fn random_pure_hasher(&self, mut i_seed: i32) -> i32 {
i_seed = ((i_seed << 13) ^ i_seed).wrapping_sub(i_seed >> 21);

let mut i_result = i_seed.wrapping_mul(i_seed);
i_result = i_result.wrapping_mul(C3);
i_result = i_result.wrapping_add(C2);
i_result = i_result.wrapping_mul(i_seed);
i_result = i_result.wrapping_add(C1);
i_result &= K_RANDOM_PURE_MAX;

i_result = i_result.wrapping_add(i_seed);

i_result = ((i_result << 13) ^ i_result).wrapping_sub(i_result >> 21);

i_result
}

pub fn generate_random_number(&mut self) -> i32 {
// In avmplus, RNG is initialized on first use.
if self.u_value == 0 {
let seed = get_seed();
self.init_with_seed(seed);
}

let mut a_num = self.random_fast_next();

a_num = self.random_pure_hasher(a_num.wrapping_mul(71));

a_num & K_RANDOM_PURE_MAX
}
}

// https://github.com/adobe-flash/avmplus/blob/65a05927767f3735db37823eebf7d743531f5d37/VMPI/PosixSpecificUtils.cpp#L18
fn get_seed() -> u32 {
get_current_date_time().timestamp_micros() as u32
}
4 changes: 2 additions & 2 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::avm1::{Object as Avm1Object, Value as Avm1Value};
use crate::avm2::api_version::ApiVersion;
use crate::avm2::Activation as Avm2Activation;
use crate::avm2::{Avm2, LoaderInfoObject, Object as Avm2Object, SoundChannelObject};
use crate::avm_rng::AvmRng;
use crate::backend::{
audio::{AudioBackend, AudioManager, SoundHandle, SoundInstanceHandle},
log::LogBackend,
Expand Down Expand Up @@ -43,7 +44,6 @@ use crate::PlayerMode;
use async_channel::Sender;
use core::fmt;
use gc_arena::{Collect, Mutation};
use rand::rngs::SmallRng;
use ruffle_render::backend::{BitmapCacheEntry, RenderBackend};
use ruffle_render::commands::{CommandHandler, CommandList};
use ruffle_render::transform::TransformStack;
Expand Down Expand Up @@ -119,7 +119,7 @@ pub struct UpdateContext<'gc> {
pub video: &'gc mut dyn VideoBackend,

/// The RNG, used by the AVM `RandomNumber` opcode, `Math.random(),` and `random()`.
pub rng: &'gc mut SmallRng,
pub rng: &'gc mut AvmRng,

/// The current player's stage (including all loaded levels)
pub stage: Stage<'gc>,
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate num_derive;
#[macro_use]
mod avm1;
mod avm2;
mod avm_rng;
mod binary_data;
pub mod bitmap;
pub mod buffer;
Expand Down
9 changes: 5 additions & 4 deletions core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::avm1::VariableDumper;
use crate::avm1::{Activation, ActivationIdentifier};
use crate::avm2::object::{EventObject as Avm2EventObject, Object as Avm2Object};
use crate::avm2::{Activation as Avm2Activation, Avm2, CallStack};
use crate::avm_rng::AvmRng;
use crate::backend::ui::FontDefinition;
use crate::backend::{
audio::{AudioBackend, AudioManager},
Expand Down Expand Up @@ -38,7 +39,6 @@ use crate::library::Library;
use crate::limits::ExecutionLimit;
use crate::loader::{LoadBehavior, LoadManager};
use crate::local_connection::LocalConnections;
use crate::locale::get_current_date_time;
use crate::net_connection::NetConnections;
use crate::orphan_manager::OrphanManager;
use crate::prelude::*;
Expand All @@ -54,7 +54,6 @@ use crate::DefaultFont;
use async_channel::Sender;
use gc_arena::lock::GcRefLock;
use gc_arena::{Collect, DynamicRootSet, Mutation, Rootable};
use rand::{rngs::SmallRng, SeedableRng};
use ruffle_macros::istr;
use ruffle_render::backend::{null::NullRenderer, RenderBackend, ViewportDimensions};
use ruffle_render::commands::CommandList;
Expand Down Expand Up @@ -316,7 +315,7 @@ pub struct Player {

transform_stack: TransformStack,

rng: SmallRng,
rng: AvmRng,

gc_arena: Rc<RefCell<GcArena>>,

Expand Down Expand Up @@ -2950,7 +2949,9 @@ impl PlayerBuilder {
mouse_cursor_needs_check: false,

// Misc. state
rng: SmallRng::seed_from_u64(get_current_date_time().timestamp_millis() as u64),
// TODO: AVM1 and AVM2 use separate RNGs (though algorithm is same), so this is technically incorrect.
// See: https://github.com/ruffle-rs/ruffle/issues/20244
rng: AvmRng::default(),
system: SystemProperties::new(language),
page_url: self.page_url.clone(),
transform_stack: TransformStack::new(),
Expand Down
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/rng/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Binary file added tests/tests/swfs/avm2/rng/test.swf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/rng/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_ticks = 1
Binary file modified tests/tests/swfs/avm2/stage3d_bitmap/output.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/tests/swfs/avm2/stage3d_texture/output.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading