aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/lib.rs332
-rw-r--r--crates/fparkan-runtime/src/lib.rs1380
-rw-r--r--crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs507
3 files changed, 2203 insertions, 16 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index d34effb..da3411e 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -1180,6 +1180,19 @@ impl Default for ControlTag21InstanceState {
/// Input or metadata prevented a safe native control sample.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ControlSamplingError {
+ /// This resource-device helper only implements typed-record tag 26.
+ UnsupportedResourceDeviceTag { actual: u32 },
+ /// The record's raw provider key has no resolved NDP table.
+ MissingResourceDeviceNodeData { key: u32 },
+ /// The record's raw provider key is not a row in its NDP table.
+ ResourceDeviceNodeDataKeyOutOfRange { key: u32, row_count: usize },
+ /// The caller did not supply the provider's current type-1 value.
+ MissingResourceDeviceProviderValue { key: u32 },
+ /// A coefficient is outside the helper's finite normal-or-zero contract.
+ InvalidResourceDeviceOperand { operand: &'static str },
+ /// The native x87 intermediate is outside the range represented by normal
+ /// f32 values, so the Rust result could differ from the original.
+ UnsupportedResourceDeviceFloatRange { stage: &'static str },
/// A state object was reused with a different model node count.
StateNodeCountMismatch { expected: usize, actual: usize },
/// A state object was reused with a different number of tag-21 records.
@@ -1216,6 +1229,74 @@ pub enum ControlSamplingError {
NonFiniteSample { record_index: usize },
}
+/// Samples Control's tag-26 typed-record query `0x300` for one current state.
+///
+/// The key in fixed word 1 must select an existing NDP row. The caller supplies
+/// that provider's current type-1 value and the object's current multiplier;
+/// this function does not invent their runtime state. Fixed word 11 is the
+/// coefficient loaded by selector `0x300`. Native code multiplies in the order
+/// `provider_now * coefficient * multiplier_now`.
+///
+/// The source x87 code returns a value with an extended exponent range even
+/// though it uses 24-bit precision. This f32 helper therefore accepts only
+/// finite normal-or-zero operands and normal-or-exact-zero intermediate and
+/// final products. It rejects subnormal or out-of-f32-range products rather
+/// than silently underflowing or overflowing differently from x87.
+pub fn sample_control_resource_device_selector_300(
+ record: &PreparedControlTypedRecord,
+ node_data: Option<&PreparedControlNodeDataTable>,
+ provider_now: Option<f32>,
+ multiplier_now: f32,
+) -> Result<f32, ControlSamplingError> {
+ if record.tag != 26 {
+ return Err(ControlSamplingError::UnsupportedResourceDeviceTag { actual: record.tag });
+ }
+
+ let key = record.fixed_words[1];
+ let Some(node_data) = node_data else {
+ return Err(ControlSamplingError::MissingResourceDeviceNodeData { key });
+ };
+ if !usize::try_from(key).is_ok_and(|key| key < node_data.rows.len()) {
+ return Err(ControlSamplingError::ResourceDeviceNodeDataKeyOutOfRange {
+ key,
+ row_count: node_data.rows.len(),
+ });
+ }
+ let provider_now =
+ provider_now.ok_or(ControlSamplingError::MissingResourceDeviceProviderValue { key })?;
+ let base = f32::from_bits(record.fixed_words[11]);
+
+ for (operand, value) in [
+ ("provider", provider_now),
+ ("base coefficient", base),
+ ("multiplier", multiplier_now),
+ ] {
+ if !value.is_finite() || (value != 0.0 && !value.is_normal()) {
+ return Err(ControlSamplingError::InvalidResourceDeviceOperand { operand });
+ }
+ }
+
+ let provider_times_base =
+ native_range_compatible_product(provider_now, base, "provider times base")?;
+ native_range_compatible_product(provider_times_base, multiplier_now, "then multiplier")
+}
+
+fn native_range_compatible_product(
+ lhs: f32,
+ rhs: f32,
+ stage: &'static str,
+) -> Result<f32, ControlSamplingError> {
+ let exact = f64::from(lhs) * f64::from(rhs);
+ let absolute = exact.abs();
+ let minimum_normal = f64::from(f32::MIN_POSITIVE);
+ let maximum = f64::from(f32::MAX);
+ if exact != 0.0 && (absolute < minimum_normal || absolute > maximum) {
+ return Err(ControlSamplingError::UnsupportedResourceDeviceFloatRange { stage });
+ }
+
+ Ok(lhs * rhs)
+}
+
/// Samples per-node animation times using the decoded Control timeline.
///
/// `native_clock_ms` is the absolute u32 millisecond clock passed to native
@@ -4141,6 +4222,203 @@ mod tests {
// Preserve the authored CTLD fixture bits; this is not the `f32` value of tau.
const AUTHORED_TAG21_RAW_PARAMETER_3: f32 = f32::from_bits(0x40C8_F5C3);
+ fn resource_device_record(tag: u32, key: u32, base_bits: u32) -> PreparedControlTypedRecord {
+ let mut fixed_words = [0; 44];
+ fixed_words[1] = key;
+ fixed_words[11] = base_bits;
+ PreparedControlTypedRecord {
+ tag,
+ fixed_words,
+ values: Vec::new(),
+ string_bytes: Vec::new(),
+ gate_provider: PreparedControlGateProvider::NotApplicable,
+ }
+ }
+
+ fn resource_device_node_data_table(row_count: usize) -> PreparedControlNodeDataTable {
+ PreparedControlNodeDataTable {
+ source: ResourceKey {
+ archive: parse_path("parts.rlb").expect("NDP archive"),
+ name: resource_name(b"resource-device.ndp"),
+ type_id: None,
+ },
+ rows: vec![
+ PreparedControlNodeDataRow {
+ raw_flags: 0,
+ max_volume: 0.0,
+ mobility_per_volume: 0.0,
+ effect: PreparedControlResourcePair {
+ archive: [0; 32],
+ name: [0; 32],
+ },
+ };
+ row_count
+ ],
+ }
+ }
+
+ #[test]
+ fn tag26_selector_300_multiplies_provider_then_fixed_base_then_multiplier() {
+ let record = resource_device_record(26, 1, 0x3D25_AE4A);
+ let node_data = resource_device_node_data_table(2);
+ let provider_now = f32::from_bits(0x3FE6_982F);
+ let multiplier_now = f32::from_bits(0x4071_5539);
+
+ let sampled = sample_control_resource_device_selector_300(
+ &record,
+ Some(&node_data),
+ Some(provider_now),
+ multiplier_now,
+ )
+ .expect("valid tag-26 resource-device sample");
+
+ assert_eq!(sampled.to_bits(), 0x3E8C_B02E);
+ assert_ne!(
+ sampled.to_bits(),
+ ((provider_now * multiplier_now) * f32::from_bits(record.fixed_words[11])).to_bits(),
+ "the original selector multiplies the provider by the record base first"
+ );
+ }
+
+ #[test]
+ fn tag26_selector_300_requires_its_ndp_row_and_explicit_provider() {
+ let record = resource_device_record(26, 1, 2.0_f32.to_bits());
+ let one_row = resource_device_node_data_table(1);
+ assert_eq!(
+ sample_control_resource_device_selector_300(&record, Some(&one_row), Some(3.0), 4.0,),
+ Err(ControlSamplingError::ResourceDeviceNodeDataKeyOutOfRange {
+ key: 1,
+ row_count: 1,
+ })
+ );
+
+ let two_rows = resource_device_node_data_table(2);
+ assert_eq!(
+ sample_control_resource_device_selector_300(&record, Some(&two_rows), None, 4.0),
+ Err(ControlSamplingError::MissingResourceDeviceProviderValue { key: 1 })
+ );
+ assert_eq!(
+ sample_control_resource_device_selector_300(&record, None, Some(3.0), 4.0),
+ Err(ControlSamplingError::MissingResourceDeviceNodeData { key: 1 })
+ );
+ }
+
+ #[test]
+ fn tag26_selector_300_rejects_other_tags_and_extended_x87_ranges() {
+ let node_data = resource_device_node_data_table(1);
+ let tag21 = resource_device_record(21, 0, 1.0_f32.to_bits());
+ assert_eq!(
+ sample_control_resource_device_selector_300(&tag21, Some(&node_data), Some(1.0), 1.0),
+ Err(ControlSamplingError::UnsupportedResourceDeviceTag { actual: 21 })
+ );
+
+ let tag26 = resource_device_record(26, 0, f32::MIN_POSITIVE.to_bits());
+ assert_eq!(
+ sample_control_resource_device_selector_300(
+ &tag26,
+ Some(&node_data),
+ Some(f32::MIN_POSITIVE),
+ 1.0,
+ ),
+ Err(ControlSamplingError::UnsupportedResourceDeviceFloatRange {
+ stage: "provider times base",
+ })
+ );
+ }
+
+ #[test]
+ fn tag26_selector_300_preserves_negative_and_signed_zero_values() {
+ let node_data = resource_device_node_data_table(1);
+ let record = resource_device_record(26, 0, 0.5_f32.to_bits());
+ let negative =
+ sample_control_resource_device_selector_300(&record, Some(&node_data), Some(-2.0), 4.0)
+ .expect("negative factors are not clamped");
+ assert_eq!(negative, -4.0);
+
+ let negative_zero = sample_control_resource_device_selector_300(
+ &resource_device_record(26, 0, 2.0_f32.to_bits()),
+ Some(&node_data),
+ Some(-0.0),
+ 3.0,
+ )
+ .expect("signed zero is an exact native input");
+ assert_eq!(negative_zero.to_bits(), (-0.0_f32).to_bits());
+
+ let positive_zero = sample_control_resource_device_selector_300(
+ &resource_device_record(26, 0, 2.0_f32.to_bits()),
+ Some(&node_data),
+ Some(0.0),
+ 3.0,
+ )
+ .expect("positive zero is an exact native input");
+ assert_eq!(positive_zero.to_bits(), 0.0_f32.to_bits());
+ }
+
+ #[test]
+ fn tag26_selector_300_rejects_nonfinite_and_subnormal_operands() {
+ let node_data = resource_device_node_data_table(1);
+ for (operand, provider_now, multiplier_now, base_bits) in [
+ ("provider", f32::NAN, 1.0, 1.0_f32.to_bits()),
+ ("multiplier", 1.0, f32::INFINITY, 1.0_f32.to_bits()),
+ ("base coefficient", 1.0, 1.0, 1),
+ ] {
+ let record = resource_device_record(26, 0, base_bits);
+ assert_eq!(
+ sample_control_resource_device_selector_300(
+ &record,
+ Some(&node_data),
+ Some(provider_now),
+ multiplier_now,
+ ),
+ Err(ControlSamplingError::InvalidResourceDeviceOperand { operand })
+ );
+ }
+ }
+
+ #[test]
+ fn tag26_selector_300_rejects_extended_intermediate_and_final_ranges() {
+ let node_data = resource_device_node_data_table(1);
+ let provider = f32::MAX;
+ let overflowing_intermediate = resource_device_record(26, 0, 2.0_f32.to_bits());
+ assert_eq!(
+ sample_control_resource_device_selector_300(
+ &overflowing_intermediate,
+ Some(&node_data),
+ Some(provider),
+ 0.25,
+ ),
+ Err(ControlSamplingError::UnsupportedResourceDeviceFloatRange {
+ stage: "provider times base",
+ })
+ );
+
+ let underflowing_intermediate = resource_device_record(26, 0, f32::MIN_POSITIVE.to_bits());
+ assert_eq!(
+ sample_control_resource_device_selector_300(
+ &underflowing_intermediate,
+ Some(&node_data),
+ Some(f32::MIN_POSITIVE),
+ f32::MAX,
+ ),
+ Err(ControlSamplingError::UnsupportedResourceDeviceFloatRange {
+ stage: "provider times base",
+ })
+ );
+
+ let subnormal_final = resource_device_record(26, 0, 1.0_f32.to_bits());
+ assert_eq!(
+ sample_control_resource_device_selector_300(
+ &subnormal_final,
+ Some(&node_data),
+ Some(f32::MIN_POSITIVE),
+ 0.5,
+ ),
+ Err(ControlSamplingError::UnsupportedResourceDeviceFloatRange {
+ stage: "then multiplier",
+ })
+ );
+ }
+
fn tag21_fullcycle_control() -> PreparedComponentControl {
let source = ResourceKey {
archive: parse_path("parts.rlb").expect("archive"),
@@ -6166,6 +6444,60 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
+ fn tag26_selector_300_uses_a_real_control_record_and_ndp_key() {
+ let root = fixture_root("IS");
+ let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(&root));
+ let repository = CachedResourceRepository::new(Arc::clone(&vfs));
+ let roots = [resource_name(b"UNITS/BUILDS/MINE/min_l_n1.dat")];
+ let (_graph, prototypes) =
+ build_prototype_graph(&repository, vfs.as_ref(), &roots).expect("prototype graph");
+ let assets = prepare_mission_assets_with_repository(
+ &repository,
+ &[0..prototypes.len()],
+ &prototypes,
+ )
+ .expect("prepared licensed unit controls");
+
+ let mut sampled_records = 0;
+ for control in assets.object_component_controls.iter().flatten().flatten() {
+ for record in control
+ .typed_records
+ .iter()
+ .filter(|record| record.tag == 26)
+ {
+ let node_data = control
+ .node_data
+ .as_ref()
+ .expect("tag-26 control has its explicit NDP table");
+ assert_eq!(control.source.name.0, b"fr_l_mine.ctl");
+ assert_eq!(node_data.source.name.0, b"fr_l_mine.ndp");
+ assert_eq!(node_data.rows.len(), 22);
+ assert_eq!(record.fixed_words[1], 2);
+ assert_eq!(record.fixed_words[11], 0x3F80_0000);
+ let sample = sample_control_resource_device_selector_300(
+ record,
+ Some(node_data),
+ Some(1.0),
+ 1.0,
+ )
+ .expect("real tag-26 key selects an NDP row");
+ assert_eq!(sample.to_bits(), record.fixed_words[11]);
+ println!(
+ "control={}: ndp={}: key={} rows={} coefficient=0x{:08x}",
+ String::from_utf8_lossy(&control.source.name.0),
+ String::from_utf8_lossy(&node_data.source.name.0),
+ record.fixed_words[1],
+ node_data.rows.len(),
+ record.fixed_words[11],
+ );
+ sampled_records += 1;
+ }
+ }
+ assert_eq!(sampled_records, 1);
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
fn repository_plan_deduplicates_duplicate_visuals_but_graph_preserves_requests() {
let root = fixture_root("IS");
let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(&root));
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 3964f78..a4985a6 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -6,15 +6,17 @@ use fparkan_assets::{
derive_mission_land_paths, extend_graph_report_with_visual_dependencies,
mission_script_bundle_bases, prepare_terrain_world, AssetError as AssetPreparationError,
AssetId, AssetManager, BuildCategory, MissionAssetPlan, MissionDocument, MissionError,
- MissionTerrainPaths, NresError, PreparedVisual, TerrainFormatError, TerrainPreparationError,
+ MissionTerrainPaths, NresError, PreparedComponentControl, PreparedControlEffectSection,
+ PreparedControlTypedRecord, PreparedVisual, TerrainFormatError, TerrainPreparationError,
TerrainWorld, TmaProfile,
};
use fparkan_path::{normalize_relative, NormalizedPath, PathError, PathPolicy};
use fparkan_prototype::{
- build_prototype_graph_report, PrototypeGraph, PrototypeGraphFailure, PrototypeGraphReport,
- UnitComponentRecord, UNIT_DAT_BAS_PLACEMENT_FLAG,
+ build_prototype_graph_report, unit_component_tree, PrototypeGraph, PrototypeGraphFailure,
+ PrototypeGraphReport, UnitComponentRecord, UnitComponentTreeNode, PROTOTYPE_TYPE_EXTO,
+ UNIT_DAT_BAS_PLACEMENT_FLAG,
};
-use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository};
+use fparkan_resource::{resource_name, CachedResourceRepository, ResourceKey, ResourceRepository};
use fparkan_script::{
Handler19DwordWrite, Handler19InitInput, Handler19InitResolution, Handler19ResolveError,
ScriptInstruction, ScriptPackage, VarSet, VarSetDefault, VmHostCallbackCommand,
@@ -134,6 +136,116 @@ pub struct BehaviorRegisteredObject {
pub native_tail: [u32; 5],
}
+/// Native `CurrentOre`/`MaximumOre` state for one full Unit DAT mission object.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct BehaviorOreState {
+ /// Stable identity from the source TMA object order.
+ pub original_id: OriginalObjectId,
+ /// Corresponding live Rust world object.
+ pub handle: ObjectHandle,
+ /// Class word recovered from the root Unit DAT header.
+ pub class_flags: u32,
+ /// Raw bits of the `CurrentOre` float property.
+ pub current_ore_bits: u32,
+ /// Raw bits of the read-only `MaximumOre` float property.
+ pub maximum_ore_bits: u32,
+}
+
+/// Origin of a Control descriptor retained in the effective table projection.
+#[derive(Clone, Debug, PartialEq)]
+pub struct ControlDeviceRecordProvenance {
+ /// Unit DAT component index that supplied the descriptor.
+ pub unit_component_index: usize,
+ /// Zero-based record index in that component's CTLD typed-record list.
+ pub typed_record_index: usize,
+ /// CTLD resource that supplied the descriptor.
+ pub source: ResourceKey,
+}
+
+/// Common fields known for one row in a host object's Control device table.
+///
+/// The row index is its position in the containing `Vec`. This stores the
+/// confirmed base fields and latest raw descriptor, not tag-specific callbacks.
+#[derive(Clone, Debug, PartialEq)]
+pub struct EffectiveControlDevice {
+ /// Factory tag of the original device instance; INTO does not replace it.
+ pub factory_tag: u32,
+ /// Native component ID that first appended this row.
+ pub component_id: u32,
+ /// Current common key field. INTO preserves the receiver's key.
+ pub key: u32,
+ /// Sixteen raw DWORD coefficients copied by the common setter.
+ pub coefficient_bits: Option<[u32; 16]>,
+ /// Multiplier bits when the factory default is confirmed.
+ pub multiplier_bits: Option<u32>,
+ /// Current native word at `+0x50` when it is known.
+ pub native_word_50_bits: Option<u32>,
+ /// Current raw descriptor, including typed values and string bytes.
+ pub descriptor: PreparedControlTypedRecord,
+ /// Source location that created the original factory instance.
+ pub factory_provenance: ControlDeviceRecordProvenance,
+ /// Source location of the descriptor currently applied to the row.
+ pub descriptor_provenance: ControlDeviceRecordProvenance,
+}
+
+/// Why the base Control device table could not be projected for an object.
+#[derive(Clone, Debug, Eq, PartialEq)]
+pub enum MissionControlDeviceTableUnavailableReason {
+ /// The mission root has no decoded Unit DAT component tree.
+ NoUnitDatTree,
+ /// Unit DAT child counts do not form a valid preorder tree.
+ InvalidUnitComponentTree,
+ /// Prototype, visual, and component data are not aligned.
+ ComponentAlignmentMismatch,
+ /// No component explicitly references a Control resource.
+ NoControlResource,
+ /// An explicit Control resource could not be prepared.
+ ControlResourceUnavailable { unit_component_index: usize },
+ /// A Control resource belongs to a non-geometric prototype.
+ NonGeometricControlResource { unit_component_index: usize },
+ /// The parser stopped before the component's typed record list completed.
+ IncompleteTypedRecordList {
+ unit_component_index: usize,
+ first_unparsed_record: usize,
+ },
+ /// An INTO descriptor has no first typed record for its one native override call.
+ MissingOverrideRecord { unit_component_index: usize },
+ /// Native wrapping produced a non-sentinel index outside the current table.
+ OverrideIndexOutOfRange {
+ unit_component_index: usize,
+ target_index: u32,
+ },
+ /// The receiver tag does not have a verified common override method.
+ UnsupportedOverrideReceiver { factory_tag: u32 },
+ /// The EXTO key needs a global model-node mapping that is not available.
+ UnsupportedExToNodeMapping { unit_component_index: usize },
+ /// An unknown component kind prevents calculating EXTO global node offsets.
+ UnknownComponentKindForExTo {
+ unit_component_index: usize,
+ source_type: Option<u32>,
+ },
+}
+
+/// Availability of the confirmed common-field Control table projection.
+#[derive(Clone, Debug, PartialEq)]
+pub enum MissionControlDeviceTableStatus {
+ /// Rows in native global-table order.
+ Available(Vec<EffectiveControlDevice>),
+ /// No complete table is exposed when required source state is unsupported.
+ Unavailable(MissionControlDeviceTableUnavailableReason),
+}
+
+/// Control device table projection attached to one loaded mission object.
+#[derive(Clone, Debug, PartialEq)]
+pub struct MissionControlDeviceTableState {
+ /// Stable identity from the source TMA object order.
+ pub original_id: Option<OriginalObjectId>,
+ /// Corresponding live Rust world object.
+ pub handle: ObjectHandle,
+ /// Complete base projection, or an explicit unsupported boundary.
+ pub status: MissionControlDeviceTableStatus,
+}
+
/// Resource distributor state associated with one TMA clan's constructed AI.
#[derive(Clone, Debug, PartialEq)]
pub struct BehaviorDistributorState {
@@ -148,6 +260,8 @@ pub struct BehaviorDistributorState {
const LAST_SUPER_AI_TABLE_INDEX: u32 = 63;
const BEHAVIOR_REGISTRATION_KEY_FLAG: u32 = 0x8000_0000;
+#[cfg(test)]
+const PROTOTYPE_TYPE_INTO: u32 = u32::from_le_bytes(*b"INTO");
/// Native terrain adjustment selected for a mission object's BAS geometry.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -464,6 +578,8 @@ struct LoadedMissionState {
asset_plan: MissionAssetPlan,
object_drafts: Vec<MissionObjectDraft>,
behavior_distributors: Vec<BehaviorDistributorState>,
+ behavior_ore_states: Vec<BehaviorOreState>,
+ control_device_tables: Vec<MissionControlDeviceTableState>,
script_bundles: Vec<MissionScriptBundle>,
script_varset: Option<MissionScriptVarSet>,
script_init_states: Vec<MissionScriptInitState>,
@@ -1045,6 +1161,53 @@ fn load_mission_with_options(
Vec::new()
};
+ let behavior_ore_states = if options.asset_scope == MissionAssetScope::Full {
+ // Property callbacks also run for Unit DAT objects that do not join a distributor.
+ object_drafts
+ .iter()
+ .zip(&handles)
+ .filter_map(|(draft, handle)| {
+ Some(initialize_behavior_ore_state(
+ draft.original_id?,
+ *handle,
+ draft.unit_dat_header_flags?,
+ &draft.properties,
+ ))
+ })
+ .collect()
+ } else {
+ Vec::new()
+ };
+
+ let control_device_tables = if options.asset_scope == MissionAssetScope::Full {
+ object_drafts
+ .iter()
+ .zip(&handles)
+ .enumerate()
+ .map(
+ |(object_index, (draft, handle))| MissionControlDeviceTableState {
+ original_id: draft.original_id,
+ handle: *handle,
+ status: build_control_device_table(
+ object_index,
+ draft,
+ &mission_assets,
+ prototype_graph
+ .root_prototype_request_spans
+ .get(object_index),
+ &resolved_prototypes,
+ )
+ .map_or_else(
+ MissionControlDeviceTableStatus::Unavailable,
+ MissionControlDeviceTableStatus::Available,
+ ),
+ },
+ )
+ .collect()
+ } else {
+ Vec::new()
+ };
+
let mut next_behavior_rng_state = engine.behavior_rng_state;
if options.asset_scope == MissionAssetScope::Full {
let queue_time_ms = queue_time_ms.expect("full mission loading requires a queue time");
@@ -1120,6 +1283,8 @@ fn load_mission_with_options(
asset_plan: mission_asset_plan,
object_drafts,
behavior_distributors,
+ behavior_ore_states,
+ control_device_tables,
script_bundles: loaded_scripts.bundles,
script_varset: loaded_scripts.varset,
script_init_states: loaded_scripts.init_states,
@@ -1402,6 +1567,26 @@ pub fn loaded_mission_behavior_distributors(
.map(|state| state.behavior_distributors.as_slice())
}
+/// Returns native ore properties attached to full Unit DAT mission objects.
+#[must_use]
+pub fn loaded_mission_behavior_ore_states(engine: &Engine) -> Option<&[BehaviorOreState]> {
+ engine
+ .loaded
+ .as_ref()
+ .map(|state| state.behavior_ore_states.as_slice())
+}
+
+/// Returns the base Control device table projections for loaded mission objects.
+#[must_use]
+pub fn loaded_mission_control_device_tables(
+ engine: &Engine,
+) -> Option<&[MissionControlDeviceTableState]> {
+ engine
+ .loaded
+ .as_ref()
+ .map(|state| state.control_device_tables.as_slice())
+}
+
fn behavior_clan_for_selector(
selector: u32,
clan_count: usize,
@@ -1418,6 +1603,523 @@ fn has_behavior_registration_key(class_flags: u32) -> bool {
class_flags & BEHAVIOR_REGISTRATION_KEY_FLAG != 0
}
+fn native_behavior_ore_defaults(class_flags: u32) -> (u32, u32) {
+ match class_flags {
+ 0x8000_0004 => (0, 0x43fa_0000),
+ 0x8000_0008 => (0, 0x457a_0000),
+ 0x0100_2000 => (0, 0x44fa_0000),
+ 0x0100_4000 => (0x44fa_0000, 0x44fa_0000),
+ _ => (0, 0),
+ }
+}
+
+fn initialize_behavior_ore_state(
+ original_id: OriginalObjectId,
+ handle: ObjectHandle,
+ class_flags: u32,
+ properties: &[MissionObjectProperty],
+) -> BehaviorOreState {
+ let (mut current_ore_bits, maximum_ore_bits) = native_behavior_ore_defaults(class_flags);
+ if let Some(property) = properties
+ .iter()
+ .find(|property| property.name_raw.as_slice() == b"CurrentOre")
+ {
+ current_ore_bits = property.raw_value[1];
+ }
+ BehaviorOreState {
+ original_id,
+ handle,
+ class_flags,
+ current_ore_bits,
+ maximum_ore_bits,
+ }
+}
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+enum ControlTableTarget {
+ Append,
+ Override(u32),
+}
+
+fn native_control_target_action(target: u32) -> ControlTableTarget {
+ if target == u32::MAX {
+ ControlTableTarget::Append
+ } else {
+ ControlTableTarget::Override(target)
+ }
+}
+
+fn native_component_target_index(
+ source_type: Option<u32>,
+ devices: &[EffectiveControlDevice],
+ parent_component_id: u32,
+ raw_local_index: i32,
+) -> u32 {
+ if source_type == Some(PROTOTYPE_TYPE_EXTO) {
+ // AniMesh writes EXTO's mount node to descriptor C4; C8 remains -1,
+ // the Control append sentinel.
+ u32::MAX
+ } else {
+ native_into_table_target_index(devices, parent_component_id, raw_local_index)
+ }
+}
+
+fn native_into_table_target_index(
+ devices: &[EffectiveControlDevice],
+ parent_component_id: u32,
+ raw_local_index: i32,
+) -> u32 {
+ let first_parent_device = devices
+ .iter()
+ .position(|device| device.component_id == parent_component_id)
+ .and_then(|index| u32::try_from(index).ok())
+ .unwrap_or(u32::MAX);
+ first_parent_device.wrapping_add(raw_local_index as u32)
+}
+
+fn native_into_append_key(raw_key: u32) -> u32 {
+ if raw_key == 0 {
+ u32::MAX
+ } else {
+ raw_key
+ }
+}
+
+fn native_ex_to_append_key(
+ raw_key: u32,
+ component_id: u32,
+ mount_node: Option<u32>,
+ first_global_node: Option<u32>,
+) -> Option<u32> {
+ if raw_key == 0 {
+ return mount_node;
+ }
+ if component_id == 0 {
+ return Some(raw_key);
+ }
+ let mount_node = mount_node?;
+ if mount_node == u32::MAX {
+ return Some(raw_key);
+ }
+ let first_global_node = first_global_node.unwrap_or(u32::MAX);
+ Some(raw_key.wrapping_add(first_global_node).wrapping_sub(1))
+}
+
+fn active_unit_component_order(
+ records: &[UnitComponentRecord],
+ tree: &[UnitComponentTreeNode],
+) -> Vec<usize> {
+ if records.is_empty() || tree.is_empty() {
+ return Vec::new();
+ }
+ let mut order = vec![0];
+ let mut pending = tree[0].children.iter().rev().copied().collect::<Vec<_>>();
+ while let Some(node_index) = pending.pop() {
+ let Some(node) = tree.get(node_index) else {
+ continue;
+ };
+ let component_index = node.record_index;
+ let Some(component) = records.get(component_index) else {
+ continue;
+ };
+ if component.kind == 0 {
+ // The native raw-kind guard returns before visiting this subtree.
+ continue;
+ }
+ order.push(component_index);
+ pending.extend(node.children.iter().rev().copied());
+ }
+ order
+}
+
+fn control_device_record_provenance(
+ component_index: usize,
+ record_index: usize,
+ control: &PreparedComponentControl,
+) -> ControlDeviceRecordProvenance {
+ ControlDeviceRecordProvenance {
+ unit_component_index: component_index,
+ typed_record_index: record_index,
+ source: control.source.clone(),
+ }
+}
+
+fn append_effective_control_device(
+ devices: &mut Vec<EffectiveControlDevice>,
+ component_id: u32,
+ key: u32,
+ component_index: usize,
+ record_index: usize,
+ control: &PreparedComponentControl,
+ descriptor: &PreparedControlTypedRecord,
+) {
+ let provenance = control_device_record_provenance(component_index, record_index, control);
+ let mut coefficient_words = [0; 16];
+ coefficient_words.copy_from_slice(&descriptor.fixed_words[11..27]);
+ let coefficient_bits =
+ control_factory_uses_common_setter(descriptor.tag).then_some(coefficient_words);
+ let (multiplier_bits, default_word_50) = native_control_factory_defaults(descriptor.tag);
+ let native_word_50_bits = if descriptor.fixed_words[6] == u32::MAX {
+ default_word_50
+ } else {
+ Some(descriptor.fixed_words[6])
+ };
+ devices.push(EffectiveControlDevice {
+ factory_tag: descriptor.tag,
+ component_id,
+ key,
+ coefficient_bits,
+ multiplier_bits,
+ native_word_50_bits,
+ descriptor: descriptor.clone(),
+ factory_provenance: provenance.clone(),
+ descriptor_provenance: provenance,
+ });
+}
+
+fn apply_effective_control_override(
+ device: &mut EffectiveControlDevice,
+ descriptor: &PreparedControlTypedRecord,
+ component_index: usize,
+ record_index: usize,
+ control: &PreparedComponentControl,
+) {
+ let mut coefficient_words = [0; 16];
+ coefficient_words.copy_from_slice(&descriptor.fixed_words[11..27]);
+ device.coefficient_bits = Some(coefficient_words);
+ if descriptor.fixed_words[6] != u32::MAX {
+ device.native_word_50_bits = Some(descriptor.fixed_words[6]);
+ }
+ device.descriptor = descriptor.clone();
+ device.descriptor_provenance =
+ control_device_record_provenance(component_index, record_index, control);
+}
+
+fn control_factory_uses_common_setter(factory_tag: u32) -> bool {
+ matches!(
+ factory_tag,
+ 1 | 2 | 4 | 5 | 8 | 9 | 10 | 15 | 17 | 19 | 25 | 26 | 30
+ )
+}
+
+fn native_control_factory_defaults(factory_tag: u32) -> (Option<u32>, Option<u32>) {
+ let flags = match factory_tag {
+ 1 | 4 => 0x200,
+ 5 | 8 | 26 => 0x5,
+ 10 => 0x2000,
+ 17 | 19 => 0x20,
+ _ => return (None, None),
+ };
+ (Some(0x3f80_0000), Some(flags))
+}
+
+fn build_control_device_table(
+ object_index: usize,
+ draft: &MissionObjectDraft,
+ assets: &MissionAssets,
+ prototype_span: Option<&std::ops::Range<usize>>,
+ prototypes: &[fparkan_prototype::EffectivePrototype],
+) -> Result<Vec<EffectiveControlDevice>, MissionControlDeviceTableUnavailableReason> {
+ use MissionControlDeviceTableUnavailableReason as Unavailable;
+
+ draft
+ .unit_dat_header_flags
+ .ok_or(Unavailable::NoUnitDatTree)?;
+ if draft.unit_components.is_empty() {
+ return Err(Unavailable::InvalidUnitComponentTree);
+ }
+ let tree = match unit_component_tree(&draft.unit_components) {
+ Ok(tree) if !tree.is_empty() => tree,
+ _ => return Err(Unavailable::InvalidUnitComponentTree),
+ };
+ let span = prototype_span.ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ let component_controls = assets
+ .object_component_controls
+ .get(object_index)
+ .ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ let object_visuals = assets
+ .object_visuals
+ .get(object_index)
+ .ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ if span.start > span.end
+ || span.end > prototypes.len()
+ || span.end - span.start != draft.unit_components.len()
+ || component_controls.len() != draft.unit_components.len()
+ || object_visuals.len() != draft.unit_components.len()
+ {
+ return Err(Unavailable::ComponentAlignmentMismatch);
+ }
+
+ let prototype_for = |component_index: usize| {
+ span.start
+ .checked_add(component_index)
+ .and_then(|index| prototypes.get(index))
+ };
+ let active_components = active_unit_component_order(&draft.unit_components, &tree);
+ if active_components.first() != Some(&0) {
+ return Err(Unavailable::InvalidUnitComponentTree);
+ }
+
+ let has_ex_to = active_components.iter().any(|component_index| {
+ prototype_for(*component_index)
+ .is_some_and(|prototype| prototype.source_type == Some(PROTOTYPE_TYPE_EXTO))
+ });
+ if has_ex_to {
+ for component_index in active_components
+ .iter()
+ .copied()
+ .filter(|index| *index != 0)
+ {
+ let prototype =
+ prototype_for(component_index).ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ if prototype.source_type.is_none() {
+ return Err(Unavailable::UnknownComponentKindForExTo {
+ unit_component_index: component_index,
+ source_type: None,
+ });
+ }
+ }
+ }
+
+ let mut component_ids = vec![None; draft.unit_components.len()];
+ component_ids[0] = Some(0_u32);
+ let mut next_component_id = 1_u32;
+ for component_index in active_components.iter().copied().skip(1) {
+ component_ids[component_index] = Some(next_component_id);
+ let next = next_component_id
+ .checked_add(1)
+ .ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ next_component_id = next;
+ }
+
+ let mut first_global_nodes = vec![None; draft.unit_components.len()];
+ if has_ex_to {
+ let root_prototype = prototype_for(0).ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ let root_is_mesh = matches!(
+ root_prototype.geometry,
+ fparkan_prototype::PrototypeGeometry::Mesh(_)
+ );
+ let root_node_count = object_visuals
+ .first()
+ .and_then(|visual_id| assets.visual_by_id(*visual_id))
+ .map(|visual| visual.model_nodes)
+ .ok_or(Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: 0,
+ })?;
+ if !root_is_mesh || root_node_count == 0 {
+ return Err(Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: 0,
+ });
+ }
+ let mut global_node_cursor = u32::try_from(root_node_count).map_err(|_| {
+ Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: 0,
+ }
+ })?;
+ first_global_nodes[0] = Some(0_u32);
+ for component_index in active_components.iter().copied().skip(1) {
+ let prototype =
+ prototype_for(component_index).ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ match prototype.source_type {
+ Some(PROTOTYPE_TYPE_EXTO) => {
+ if !matches!(
+ prototype.geometry,
+ fparkan_prototype::PrototypeGeometry::Mesh(_)
+ ) {
+ return Err(Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ });
+ }
+ let Some(node_count) = object_visuals
+ .get(component_index)
+ .and_then(|visual_id| assets.visual_by_id(*visual_id))
+ .map(|visual| visual.model_nodes)
+ else {
+ return Err(Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ });
+ };
+ if node_count == 0 {
+ return Err(Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ });
+ }
+ if node_count > 1 {
+ first_global_nodes[component_index] = Some(global_node_cursor);
+ }
+ let contribution = u32::try_from(node_count - 1).map_err(|_| {
+ Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ }
+ })?;
+ let next_cursor =
+ global_node_cursor
+ .checked_add(contribution)
+ .ok_or_else(|| Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ })?;
+ global_node_cursor = next_cursor;
+ }
+ Some(_) => {}
+ None => {
+ return Err(Unavailable::UnknownComponentKindForExTo {
+ unit_component_index: component_index,
+ source_type: None,
+ });
+ }
+ }
+ }
+ }
+
+ let mut devices = Vec::new();
+ let mut has_control_resource = false;
+ for component_index in active_components {
+ let prototype =
+ prototype_for(component_index).ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ if prototype.control_resource.is_none() {
+ continue;
+ }
+ has_control_resource = true;
+ if matches!(
+ prototype.geometry,
+ fparkan_prototype::PrototypeGeometry::NonGeometric
+ ) {
+ return Err(Unavailable::NonGeometricControlResource {
+ unit_component_index: component_index,
+ });
+ }
+ let control = component_controls[component_index].as_ref().ok_or(
+ Unavailable::ControlResourceUnavailable {
+ unit_component_index: component_index,
+ },
+ )?;
+ if let PreparedControlEffectSection::Unsupported {
+ typed_record_index: Some(first_unparsed_record),
+ ..
+ } = &control.effect_section
+ {
+ return Err(Unavailable::IncompleteTypedRecordList {
+ unit_component_index: component_index,
+ first_unparsed_record: *first_unparsed_record,
+ });
+ }
+
+ let component_id =
+ component_ids[component_index].ok_or(Unavailable::ComponentAlignmentMismatch)?;
+ let is_root = component_index == 0;
+ let is_ex_to = prototype.source_type == Some(PROTOTYPE_TYPE_EXTO);
+ if is_root {
+ for (record_index, descriptor) in control.typed_records.iter().enumerate() {
+ append_effective_control_device(
+ &mut devices,
+ component_id,
+ descriptor.fixed_words[1],
+ component_index,
+ record_index,
+ control,
+ descriptor,
+ );
+ }
+ continue;
+ }
+
+ let raw_local_index = draft.unit_components[component_index].parent_or_link;
+ let parent_component_id = tree
+ .get(component_index)
+ .and_then(|node| node.parent_index)
+ .and_then(|parent_index| component_ids.get(parent_index).copied().flatten())
+ .unwrap_or(u32::MAX);
+ let native_target = native_component_target_index(
+ prototype.source_type,
+ &devices,
+ parent_component_id,
+ raw_local_index,
+ );
+ match native_control_target_action(native_target) {
+ ControlTableTarget::Append => {
+ let mount_node = if is_ex_to {
+ tree.get(component_index)
+ .and_then(|node| node.parent_index)
+ .and_then(|parent_index| {
+ let parent_id = component_ids.get(parent_index).copied().flatten()?;
+ let parent_first = first_global_nodes
+ .get(parent_index)
+ .copied()
+ .flatten()
+ .unwrap_or(u32::MAX);
+ Some(
+ parent_first
+ .wrapping_add(raw_local_index as u32)
+ .wrapping_sub(u32::from(parent_id != 0)),
+ )
+ })
+ } else {
+ None
+ };
+ let first_global_node = first_global_nodes.get(component_index).copied().flatten();
+ for (record_index, descriptor) in control.typed_records.iter().enumerate() {
+ let raw_key = descriptor.fixed_words[1];
+ let key = if is_ex_to {
+ native_ex_to_append_key(
+ raw_key,
+ component_id,
+ mount_node,
+ first_global_node,
+ )
+ .ok_or(
+ Unavailable::UnsupportedExToNodeMapping {
+ unit_component_index: component_index,
+ },
+ )?
+ } else {
+ native_into_append_key(raw_key)
+ };
+ append_effective_control_device(
+ &mut devices,
+ component_id,
+ key,
+ component_index,
+ record_index,
+ control,
+ descriptor,
+ );
+ }
+ }
+ ControlTableTarget::Override(target_index) => {
+ let descriptor =
+ control
+ .typed_records
+ .first()
+ .ok_or(Unavailable::MissingOverrideRecord {
+ unit_component_index: component_index,
+ })?;
+ let target_index_usize = usize::try_from(target_index).map_err(|_| {
+ Unavailable::OverrideIndexOutOfRange {
+ unit_component_index: component_index,
+ target_index,
+ }
+ })?;
+ let device = devices.get_mut(target_index_usize).ok_or(
+ Unavailable::OverrideIndexOutOfRange {
+ unit_component_index: component_index,
+ target_index,
+ },
+ )?;
+ if !matches!(device.factory_tag, 9 | 15 | 19 | 25) {
+ return Err(Unavailable::UnsupportedOverrideReceiver {
+ factory_tag: device.factory_tag,
+ });
+ }
+ apply_effective_control_override(device, descriptor, component_index, 0, control);
+ }
+ }
+ }
+ if !has_control_resource {
+ return Err(Unavailable::NoControlResource);
+ }
+ Ok(devices)
+}
+
fn add_behavior_registered_object(
distributor: &mut BehaviorDistributorState,
mut object: BehaviorRegisteredObject,
@@ -1791,7 +2493,7 @@ fn read_vfs(vfs: &Arc<dyn Vfs>, path: &NormalizedPath) -> Result<Arc<[u8]>, Engi
mod tests {
use super::*;
use fparkan_script::ScriptDispatchSelector;
- use fparkan_vfs::{DirectoryVfs, MemoryVfs, VfsEntry, VfsMetadata};
+ use fparkan_vfs::{DirectoryVfs, MemoryVfs, OverlayVfs, VfsEntry, VfsMetadata};
use std::path::{Path, PathBuf};
fn basement_draft(header_flags: Option<u32>, raw_after_name: u32) -> MissionObjectDraft {
@@ -1811,6 +2513,171 @@ mod tests {
}
}
+ fn synthetic_resource_key(index: usize) -> ResourceKey {
+ ResourceKey {
+ archive: normalize_engine_path("test", &format!("synthetic/{index}.rlb"))
+ .expect("synthetic archive path"),
+ name: fparkan_path::ResourceName(format!("entry{index}").into_bytes()),
+ type_id: None,
+ }
+ }
+
+ fn synthetic_control_record(
+ tag: u32,
+ key: u32,
+ first_coefficient: u32,
+ native_word_50: Option<u32>,
+ ) -> PreparedControlTypedRecord {
+ let mut fixed_words = [0; 44];
+ fixed_words[1] = key;
+ fixed_words[6] = native_word_50.unwrap_or(u32::MAX);
+ fixed_words[11] = first_coefficient;
+ PreparedControlTypedRecord {
+ tag,
+ fixed_words,
+ values: Vec::new(),
+ string_bytes: Vec::new(),
+ gate_provider: fparkan_assets::PreparedControlGateProvider::NotApplicable,
+ }
+ }
+
+ fn synthetic_component_control(
+ index: usize,
+ typed_records: Vec<PreparedControlTypedRecord>,
+ effect_section: PreparedControlEffectSection,
+ ) -> PreparedComponentControl {
+ PreparedComponentControl {
+ source: synthetic_resource_key(100 + index),
+ model_node_count: 0,
+ bindings: Vec::new(),
+ timeline_rows: Vec::new(),
+ typed_records,
+ node_data: None,
+ effect_section,
+ initial_effect_group: None,
+ point_list: None,
+ }
+ }
+
+ fn synthetic_control_fixture(
+ source_types: &[Option<u32>],
+ kinds: &[u32],
+ child_counts: &[u32],
+ parent_or_links: &[i32],
+ model_nodes: &[usize],
+ controls: Vec<Option<PreparedComponentControl>>,
+ ) -> (
+ MissionObjectDraft,
+ MissionAssets,
+ Vec<fparkan_prototype::EffectivePrototype>,
+ ) {
+ let component_count = source_types.len();
+ assert_eq!(kinds.len(), component_count);
+ assert_eq!(child_counts.len(), component_count);
+ assert_eq!(parent_or_links.len(), component_count);
+ assert_eq!(model_nodes.len(), component_count);
+ assert_eq!(controls.len(), component_count);
+
+ let unit_components = (0..component_count)
+ .map(|index| UnitComponentRecord {
+ archive_raw: [0; 32],
+ resource_raw: [0; 32],
+ kind: kinds[index],
+ parent_or_link: parent_or_links[index],
+ description_raw: [0; 32],
+ tail0: 0,
+ immediate_child_count: child_counts[index],
+ })
+ .collect::<Vec<_>>();
+ let mut assets = MissionAssets::default();
+ let mut prototypes = Vec::with_capacity(component_count);
+ let mut visuals = Vec::with_capacity(component_count);
+ let mut visual_ids = Vec::with_capacity(component_count);
+ for index in 0..component_count {
+ let mesh = synthetic_resource_key(200 + index);
+ let visual_id = AssetId::new(index as u64);
+ visual_ids.push(visual_id);
+ visuals.push(PreparedVisual {
+ id: visual_id,
+ mesh: Some(mesh.clone()),
+ source_type: source_types[index],
+ model_id: None,
+ wear_id: None,
+ model_nodes: model_nodes[index],
+ model_slots: 0,
+ model_batches: 0,
+ material_count: 0,
+ material_ids: Vec::new(),
+ texture_ids: Vec::new(),
+ lightmap_ids: Vec::new(),
+ texture_count: 0,
+ lightmap_count: 0,
+ });
+ prototypes.push(fparkan_prototype::EffectivePrototype {
+ key: fparkan_prototype::PrototypeKey(fparkan_path::ResourceName(
+ format!("prototype{index}").into_bytes(),
+ )),
+ geometry: fparkan_prototype::PrototypeGeometry::Mesh(mesh),
+ source: fparkan_prototype::PrototypeSource::ObjectsRegistry,
+ source_type: source_types[index],
+ dependencies: Vec::new(),
+ control_resource: controls[index]
+ .as_ref()
+ .map(|control| control.source.clone()),
+ point_resource: None,
+ node_data_resource: None,
+ bas_resource: None,
+ });
+ }
+ assets.visuals = visuals;
+ assets.object_visuals = vec![visual_ids.clone()];
+ assets.object_component_controls = vec![controls];
+ let draft = MissionObjectDraft {
+ original_id: Some(OriginalObjectId(0)),
+ resource_name_raw: b"TEST/ROOT.DAT".to_vec(),
+ raw_after_resource: 0,
+ identity_or_clan_raw: 0,
+ raw_after_name: 0,
+ unit_dat_header_flags: Some(0),
+ position: [0.0; 3],
+ orientation_raw: [0.0; 3],
+ scale: [1.0; 3],
+ visual_ids,
+ properties: Vec::new(),
+ unit_components,
+ };
+ (draft, assets, prototypes)
+ }
+
+ fn synthetic_control_table(
+ source_types: &[Option<u32>],
+ kinds: &[u32],
+ child_counts: &[u32],
+ parent_or_links: &[i32],
+ model_nodes: &[usize],
+ controls: Vec<Option<PreparedComponentControl>>,
+ ) -> MissionControlDeviceTableStatus {
+ let (draft, assets, prototypes) = synthetic_control_fixture(
+ source_types,
+ kinds,
+ child_counts,
+ parent_or_links,
+ model_nodes,
+ controls,
+ );
+ build_control_device_table(
+ 0,
+ &draft,
+ &assets,
+ Some(&(0..prototypes.len())),
+ &prototypes,
+ )
+ .map_or_else(
+ MissionControlDeviceTableStatus::Unavailable,
+ MissionControlDeviceTableStatus::Available,
+ )
+ }
+
#[test]
fn native_basement_mode_uses_unit_header_gate_not_builds_path() {
assert_eq!(
@@ -2014,6 +2881,428 @@ mod tests {
}
#[test]
+ fn into_table_target_uses_native_wrapping_and_append_sentinel() {
+ let no_parent_devices = [];
+ let local_one = native_into_table_target_index(&no_parent_devices, 9, 1);
+ assert_eq!(local_one, 0);
+ assert_eq!(
+ native_control_target_action(local_one),
+ ControlTableTarget::Override(0)
+ );
+
+ let local_zero = native_into_table_target_index(&no_parent_devices, 9, 0);
+ assert_eq!(local_zero, u32::MAX);
+ assert_eq!(
+ native_control_target_action(local_zero),
+ ControlTableTarget::Append
+ );
+ }
+
+ #[test]
+ fn into_missing_parent_append_maps_only_zero_key_to_native_minus_one() {
+ assert_eq!(native_into_append_key(0), u32::MAX);
+ assert_eq!(native_into_append_key(0x1234), 0x1234);
+ }
+
+ #[test]
+ fn exto_key_mapping_obeys_native_component_and_mount_sentinels() {
+ assert_eq!(native_ex_to_append_key(2, 0, None, Some(20)), Some(2));
+ assert_eq!(
+ native_ex_to_append_key(2, 1, Some(u32::MAX), Some(20)),
+ Some(2)
+ );
+ assert_eq!(
+ native_ex_to_append_key(0, 1, Some(u32::MAX), None),
+ Some(u32::MAX)
+ );
+ assert_eq!(native_ex_to_append_key(2, 1, Some(4), Some(10)), Some(11));
+ assert_eq!(native_ex_to_append_key(2, 1, Some(4), None), Some(0));
+ assert_eq!(native_ex_to_append_key(1, 5, Some(9), Some(22)), Some(22));
+ }
+
+ #[test]
+ fn synthetic_ex_to_builder_appends_remapped_rows_and_skips_inactive_subtree() {
+ let controls = vec![
+ Some(synthetic_component_control(
+ 0,
+ vec![synthetic_control_record(19, 7, 0x10, None)],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 1,
+ vec![
+ synthetic_control_record(19, 0, 0x20, None),
+ synthetic_control_record(19, 2, 0x21, None),
+ ],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 2,
+ vec![
+ synthetic_control_record(19, 0, 0x30, None),
+ synthetic_control_record(19, 2, 0x31, None),
+ ],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 3,
+ vec![synthetic_control_record(19, 9, 0x40, None)],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 4,
+ vec![synthetic_control_record(19, 10, 0x50, None)],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 5,
+ vec![synthetic_control_record(19, 2, 0x60, None)],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ ];
+ let status = synthetic_control_table(
+ &[
+ Some(PROTOTYPE_TYPE_EXTO),
+ Some(PROTOTYPE_TYPE_EXTO),
+ Some(PROTOTYPE_TYPE_EXTO),
+ None,
+ None,
+ Some(PROTOTYPE_TYPE_EXTO),
+ ],
+ &[1, 1, 1, 0, 1, 1],
+ &[3, 1, 0, 1, 0, 0],
+ &[0, 3, 2, 0, 0, 4],
+ &[5, 3, 2, 1, 1, 2],
+ controls,
+ );
+ let MissionControlDeviceTableStatus::Available(entries) = status else {
+ panic!("synthetic EXTO table should be available: {status:?}");
+ };
+ assert_eq!(entries.len(), 6);
+ // Root EXTO keys remain raw. Child keys map to mount/global nodes.
+ assert_eq!(
+ entries.iter().map(|entry| entry.key).collect::<Vec<_>>(),
+ [7, 3, 6, 6, 8, 9]
+ );
+ assert_eq!(
+ entries
+ .iter()
+ .map(|entry| entry.component_id)
+ .collect::<Vec<_>>(),
+ [0, 1, 1, 2, 2, 3]
+ );
+ assert_eq!(
+ entries
+ .iter()
+ .map(|entry| entry.descriptor_provenance.unit_component_index)
+ .collect::<Vec<_>>(),
+ [0, 1, 1, 2, 2, 5]
+ );
+ assert_ne!(
+ entries[5].component_id as usize,
+ entries[5].descriptor_provenance.unit_component_index
+ );
+ }
+
+ #[test]
+ fn synthetic_into_builder_appends_all_rows_and_overrides_only_first_record() {
+ let append = synthetic_control_table(
+ &[None, Some(PROTOTYPE_TYPE_INTO)],
+ &[1, 1],
+ &[1, 0],
+ &[0, 0],
+ &[1, 0],
+ vec![
+ None,
+ Some(synthetic_component_control(
+ 1,
+ vec![
+ synthetic_control_record(19, 0, 0x10, None),
+ synthetic_control_record(19, 2, 0x20, None),
+ ],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ ],
+ );
+ let MissionControlDeviceTableStatus::Available(appended) = append else {
+ panic!("synthetic INTO sentinel should append: {append:?}");
+ };
+ assert_eq!(appended.len(), 2);
+ assert_eq!(
+ appended.iter().map(|entry| entry.key).collect::<Vec<_>>(),
+ [u32::MAX, 2]
+ );
+ assert_eq!(
+ appended
+ .iter()
+ .map(|entry| entry.descriptor_provenance.typed_record_index)
+ .collect::<Vec<_>>(),
+ [0, 1]
+ );
+
+ let override_table = synthetic_control_table(
+ &[None, Some(PROTOTYPE_TYPE_INTO), Some(PROTOTYPE_TYPE_INTO)],
+ &[1, 1, 1],
+ &[2, 0, 0],
+ &[0, 0, 1],
+ &[1, 0, 0],
+ vec![
+ Some(synthetic_component_control(
+ 0,
+ vec![
+ synthetic_control_record(19, 11, 0x10, None),
+ synthetic_control_record(19, 12, 0x20, None),
+ ],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 1,
+ vec![
+ synthetic_control_record(21, 99, 0xA1, Some(0x77)),
+ synthetic_control_record(25, 88, 0xA2, Some(0x88)),
+ ],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ Some(synthetic_component_control(
+ 2,
+ vec![synthetic_control_record(21, 77, 0xB1, None)],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ )),
+ ],
+ );
+ let MissionControlDeviceTableStatus::Available(overridden) = override_table else {
+ panic!("synthetic INTO overrides should be available: {override_table:?}");
+ };
+ assert_eq!(overridden.len(), 2);
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| (entry.factory_tag, entry.key, entry.component_id))
+ .collect::<Vec<_>>(),
+ [(19, 11, 0), (19, 12, 0)]
+ );
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| entry.coefficient_bits.map(|words| words[0]))
+ .collect::<Vec<_>>(),
+ [Some(0xA1), Some(0xB1)]
+ );
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| entry.native_word_50_bits)
+ .collect::<Vec<_>>(),
+ [Some(0x77), Some(0x20)]
+ );
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| entry.multiplier_bits)
+ .collect::<Vec<_>>(),
+ [Some(0x3F80_0000); 2]
+ );
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| entry.descriptor_provenance.unit_component_index)
+ .collect::<Vec<_>>(),
+ [1, 2]
+ );
+ assert_eq!(
+ overridden
+ .iter()
+ .map(|entry| entry.descriptor_provenance.typed_record_index)
+ .collect::<Vec<_>>(),
+ [0, 0]
+ );
+ assert_eq!(overridden[0].descriptor.tag, 21);
+
+ let mut live_device = overridden[0].clone();
+ live_device.multiplier_bits = Some(0x4020_0000); // Live native multiplier 2.5.
+ let replacement = synthetic_control_record(25, 100, 0xC1, None);
+ let replacement_control = synthetic_component_control(
+ 3,
+ vec![replacement.clone()],
+ PreparedControlEffectSection::Parsed(Vec::new()),
+ );
+ apply_effective_control_override(
+ &mut live_device,
+ &replacement,
+ 3,
+ 0,
+ &replacement_control,
+ );
+ assert_eq!(live_device.factory_tag, 19);
+ assert_eq!(live_device.key, 11);
+ assert_eq!(live_device.component_id, 0);
+ assert_eq!(live_device.multiplier_bits, Some(0x4020_0000));
+ assert_eq!(
+ live_device.coefficient_bits,
+ Some([0xC1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
+ );
+ }
+
+ #[test]
+ fn synthetic_incomplete_typed_list_does_not_expose_partial_table() {
+ let status = synthetic_control_table(
+ &[None],
+ &[1],
+ &[0],
+ &[0],
+ &[1],
+ vec![Some(synthetic_component_control(
+ 0,
+ vec![synthetic_control_record(19, 1, 0x10, None)],
+ PreparedControlEffectSection::Unsupported {
+ typed_record_index: Some(1),
+ typed_record_tag: Some(0xDEAD),
+ },
+ ))],
+ );
+ assert_eq!(
+ status,
+ MissionControlDeviceTableStatus::Unavailable(
+ MissionControlDeviceTableUnavailableReason::IncompleteTypedRecordList {
+ unit_component_index: 0,
+ first_unparsed_record: 1,
+ }
+ )
+ );
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
+ fn full_load_builds_effective_control_device_table_for_mine_unit_dat() {
+ fn push_u32(bytes: &mut Vec<u8>, value: u32) {
+ bytes.extend_from_slice(&value.to_le_bytes());
+ }
+ fn push_lp(bytes: &mut Vec<u8>, value: &[u8]) {
+ push_u32(bytes, u32::try_from(value.len()).expect("length fits u32"));
+ bytes.extend_from_slice(value);
+ }
+ fn single_unit_tma(land_path: &[u8]) -> Vec<u8> {
+ let mut bytes = Vec::new();
+ push_u32(&mut bytes, 1); // TMA format version.
+ push_u32(&mut bytes, 0); // Paths.
+ push_u32(&mut bytes, 6); // Clan section version.
+ push_u32(&mut bytes, 0); // Clans.
+ push_u32(&mut bytes, 10); // Object section version.
+ push_u32(&mut bytes, 1); // One Unit DAT object.
+ push_u32(&mut bytes, 1); // Raw object kind.
+ push_u32(&mut bytes, 0x8000_0002); // Strict object class/flags word.
+ push_lp(&mut bytes, b"UNITS/BUILDS/MINE/min_l_n1.dat");
+ push_u32(&mut bytes, 0); // Raw word after resource.
+ push_u32(&mut bytes, 0); // Identity/clan.
+ for value in [0.0_f32, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0] {
+ bytes.extend_from_slice(&value.to_le_bytes());
+ }
+ push_lp(&mut bytes, b"mine-control-table-test");
+ push_u32(&mut bytes, 0); // Raw word after name.
+ push_u32(&mut bytes, 0); // Link 0.
+ push_u32(&mut bytes, 0); // Link 1.
+ push_u32(&mut bytes, 1); // Property schema version.
+ push_u32(&mut bytes, 0); // Properties.
+ push_lp(&mut bytes, land_path);
+ push_u32(&mut bytes, 0); // Mission flag.
+ push_lp(&mut bytes, b""); // Description.
+ push_u32(&mut bytes, 1); // Extra section version.
+ push_u32(&mut bytes, 0); // Extras.
+ bytes
+ }
+
+ let root = licensed_root("IS");
+ let source_tma = std::fs::read(root.join("MISSIONS/Autodemo.00/data.tma"))
+ .expect("licensed source mission");
+ let land_path = decode_mission_land_path(&source_tma, TmaProfile::Strict)
+ .expect("source mission land path")
+ .raw;
+ let mut overlay = MemoryVfs::default();
+ overlay.insert(
+ normalize_engine_path("test mission", "MISSIONS/TEST/MINE/data.tma")
+ .expect("mission path"),
+ Arc::from(single_unit_tma(&land_path)),
+ );
+ let vfs = Arc::new(OverlayVfs::from_layers(vec![
+ Arc::new(overlay),
+ Arc::new(DirectoryVfs::new(&root)),
+ ]));
+ let mut engine = create(EngineServices::new(vfs)).expect("engine");
+ let loaded = load_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/TEST/MINE/data.tma".to_string(),
+ mode: MissionMode::SingleMission,
+ },
+ 0,
+ )
+ .expect("full normal mission load");
+ assert_eq!(loaded.object_count, 1);
+
+ let draft = &loaded_mission_object_drafts(&engine).expect("object drafts")[0];
+ assert_eq!(draft.unit_components.len(), 6);
+ assert_eq!(draft.unit_components[0].immediate_child_count, 5);
+ let tables = loaded_mission_control_device_tables(&engine).expect("Control tables");
+ assert_eq!(tables.len(), 1);
+ assert_eq!(tables[0].original_id, Some(OriginalObjectId(0)));
+ let MissionControlDeviceTableStatus::Available(entries) = &tables[0].status else {
+ panic!(
+ "licensed mine table should be available: {:?}",
+ tables[0].status
+ );
+ };
+ assert_eq!(entries.len(), 11);
+ assert_eq!(
+ entries
+ .iter()
+ .map(|entry| entry.factory_tag)
+ .collect::<Vec<_>>(),
+ [19, 19, 9, 15, 12, 12, 13, 13, 26, 25, 21]
+ );
+ assert_eq!(
+ entries.iter().map(|entry| entry.key).collect::<Vec<_>>(),
+ [2, 2, 1, 1, 18, 21, 15, 16, 2, 1, 22]
+ );
+ assert_eq!(
+ (
+ entries[0].coefficient_bits.map(|words| words[0]),
+ entries[1].coefficient_bits.map(|words| words[0])
+ ),
+ (Some(0x4100_0000), Some(0x4100_0000))
+ );
+ assert_eq!(
+ (entries[0].multiplier_bits, entries[0].native_word_50_bits),
+ (Some(0x3f80_0000), Some(0x20))
+ );
+ assert_eq!(
+ entries
+ .iter()
+ .map(|entry| entry.descriptor_provenance.unit_component_index)
+ .collect::<Vec<_>>(),
+ [1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 5]
+ );
+ assert_eq!(entries[8].descriptor_provenance.typed_record_index, 8);
+ assert_eq!(
+ (
+ entries[9].factory_tag,
+ entries[9].component_id,
+ entries[9].key
+ ),
+ (25, 0, 1)
+ );
+ assert_eq!(entries[9].descriptor.tag, 25);
+ assert_eq!(
+ (
+ entries[10].factory_tag,
+ entries[10].component_id,
+ entries[10].key
+ ),
+ (21, 5, 22)
+ );
+ assert_eq!(entries[10].descriptor.tag, 21);
+ }
+
+ #[test]
fn script_varset_prefers_bundle_adjacent_file_then_uses_shared_fallback() {
let mut personal_files = MemoryVfs::default();
personal_files.insert(
@@ -2470,6 +3759,26 @@ mod tests {
.collect::<Vec<_>>(),
vec![(0, true), (1, true)]
);
+ let ore_states = loaded_mission_behavior_ore_states(&engine).expect("Behavior ore state");
+ let unit_dat_drafts = drafts
+ .iter()
+ .filter(|draft| draft.unit_dat_header_flags.is_some())
+ .collect::<Vec<_>>();
+ assert_eq!(ore_states.len(), unit_dat_drafts.len());
+ assert_eq!(ore_states.len(), 8);
+ for state in ore_states {
+ let draft = unit_dat_drafts
+ .iter()
+ .find(|draft| draft.original_id == Some(state.original_id))
+ .expect("ore state has a Unit DAT source");
+ assert_eq!(
+ state.handle,
+ handle_by_original_id(&engine.world, state.original_id).unwrap()
+ );
+ assert_eq!(state.class_flags, draft.unit_dat_header_flags.unwrap());
+ assert_eq!(state.current_ore_bits, 0);
+ assert_eq!(state.maximum_ore_bits, 0);
+ }
let registered = distributors
.iter()
.flat_map(|distributor| {
@@ -2538,6 +3847,15 @@ mod tests {
]
);
assert_eq!(registered.len(), 4);
+ assert!(ore_states.iter().any(|state| {
+ state.class_flags == 0x0100_8000
+ && !distributors.iter().any(|distributor| {
+ distributor
+ .registered_objects
+ .iter()
+ .any(|object| object.handle == state.handle)
+ })
+ }));
for (_, object) in registered {
assert_eq!(
handle_by_original_id(&engine.world, object.original_id),
@@ -2620,6 +3938,55 @@ mod tests {
}
#[test]
+ fn behavior_ore_initialization_uses_first_exact_current_value_and_read_only_maximum() {
+ let handle = ObjectHandle {
+ generation: 7,
+ slot: 19,
+ };
+ let state = initialize_behavior_ore_state(
+ OriginalObjectId(19),
+ handle,
+ 0x0100_2000,
+ &[
+ MissionObjectProperty {
+ name_raw: b"currentore".to_vec(),
+ raw_value: [0x1111_1111, 0xdead_beef, 0, 0],
+ },
+ MissionObjectProperty {
+ name_raw: b"CurrentOre".to_vec(),
+ raw_value: [0x3f80_0000, 0x7fc1_2345, 0, 0],
+ },
+ MissionObjectProperty {
+ name_raw: b"CurrentOre".to_vec(),
+ raw_value: [0, 0x4400_0000, 0, 0],
+ },
+ MissionObjectProperty {
+ name_raw: b"MaximumOre".to_vec(),
+ raw_value: [0, 0x4123_4567, 0, 0],
+ },
+ ],
+ );
+
+ assert_eq!(state.original_id, OriginalObjectId(19));
+ assert_eq!(state.handle, handle);
+ assert_eq!(state.class_flags, 0x0100_2000);
+ assert_eq!(state.current_ore_bits, 0x7fc1_2345);
+ assert_eq!(state.maximum_ore_bits, 0x44fa_0000);
+ }
+
+ #[test]
+ fn behavior_ore_defaults_follow_native_class_dispatch() {
+ assert_eq!(native_behavior_ore_defaults(0x8000_0004), (0, 0x43fa_0000));
+ assert_eq!(native_behavior_ore_defaults(0x8000_0008), (0, 0x457a_0000));
+ assert_eq!(native_behavior_ore_defaults(0x0100_2000), (0, 0x44fa_0000));
+ assert_eq!(
+ native_behavior_ore_defaults(0x0100_4000),
+ (0x44fa_0000, 0x44fa_0000)
+ );
+ assert_eq!(native_behavior_ore_defaults(0x0100_8000), (0, 0));
+ }
+
+ #[test]
fn behavior_distributor_add_uses_handle_and_preserves_duplicate_tail() {
let mut distributor = BehaviorDistributorState {
clan_index: 0,
@@ -2767,6 +4134,9 @@ mod tests {
assert!(loaded_mission_behavior_distributors(&engine)
.expect("preview Behavior distributors")
.is_empty());
+ assert!(loaded_mission_behavior_ore_states(&engine)
+ .expect("preview Behavior ore state")
+ .is_empty());
assert!(bundles
.iter()
.all(|bundle| bundle.behavior_warm_start.is_none()));
diff --git a/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs b/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs
index 5c7c4f2..5f5f139 100644
--- a/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs
+++ b/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs
@@ -5,9 +5,9 @@ using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
-// Calls the original x86 Behavior.dll distributor Add, normalize, and tick paths
-// against synthetic storage. Only GetSystemArealMap and the bounded vector backing
-// are substituted; Behavior's corresponding instructions remain the installed DLL.
+// Calls selected original x86 Behavior.dll, ArealMap.dll, and Control.dll paths
+// against synthetic storage. Only host callbacks, GetSystemArealMap, and bounded
+// vector backing are substituted; the tested module instructions remain installed code.
internal static class BehaviorDistributorTickOracle {
private const uint LoadWithAlteredSearchPath = 0x00000008;
private const uint MemCommitReserve = 0x00003000;
@@ -15,10 +15,32 @@ internal static class BehaviorDistributorTickOracle {
private const uint MemRelease = 0x00008000;
private const uint PageReadWrite = 0x04;
private const string ExpectedBehaviorSha256 = "ddfbdbde07832358fe6e35e01c8dba9b58e7313789498b6049ed51d4ed9baf99";
+ private const string ExpectedControlSha256 = "bcb86318089028548a178b03a21accdce15df0cc85ddd91674cf3c2f55012c6e";
+ private const string ExpectedArealMapSha256 = "a25f9254ebb339d70e2e07c8121950191e9584ca0858d8e9c2e2b6c671edf6ea";
private const int TickRva = 0x19e80;
private const int AddObjectRva = 0x1aac0;
private const int NormalizeRva = 0x1ac90;
+ private const int IndexedPropertySetRva = 0x0b470;
+ private const int IndexedPropertyGetRva = 0x0b5b0;
+ private const int OreInitializationRva = 0x08790;
private const int GetSystemArealMapIatRva = 0x59008;
+ private const int ControlResourceDeviceGetterRva = 0x21d00;
+ private const int ControlProviderInterfaceVtableRva = 0x3d254;
+ private const int ControlProviderType1GetterRva = 0x0dc40;
+ private const int ArealMapUnitDatComponentBuilderRva = 0x14b90;
+ private const int ControlResourceDeviceConstructorRva = 0x22980;
+ private const int ControlResourceComponentOverrideRva = 0x2d890;
+ private const int ControlResourceCommonSetterRva = 0x21d50;
+ private const int ControlResourceTag9ConstructorRva = 0x25460;
+ private const int ControlResourceTag15ConstructorRva = 0x22ae0;
+ private const int ControlResourceTag25ConstructorRva = 0x20800;
+ private const int ControlResourceTag9VtableRva = 0x3c938;
+ private const int ControlResourceTag15VtableRva = 0x3c648;
+ private const int ControlResourceTag25VtableRva = 0x3c448;
+ private const int ControlResourceTag9StringPairRva = 0x25600;
+ private const int ControlResourceNoOpStringPairRva = 0x0bcd0;
+ private const int ControlResourceTag19VtableRva = 0x3c608;
+ private const int ControlResourceTag19StringPairNoOpRva = 0x0bcd0;
private const uint ResourceProperty = 0x02000100;
private static Fixture active;
@@ -45,6 +67,14 @@ internal static class BehaviorDistributorTickOracle {
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void ObjectSetProperty(IntPtr self, uint property, float value, IntPtr result);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int InterfaceGetProperty(IntPtr self, uint selector, IntPtr result);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int InterfaceSetProperty(IntPtr self, uint selector, IntPtr value);
+ [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void IndexedPropertySet(IntPtr self, uint index, uint valueBits);
+ [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int IndexedPropertyGet(IntPtr self, uint index, IntPtr resultBits);
+ [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeOreInitialization(IntPtr self);
+ [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate float NativeControlResourceDeviceGet(IntPtr self, uint selector);
+ [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void NativeFastcallComponentBuilder(IntPtr owner, IntPtr node, uint parentId);
+ [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int NativeArealMapEvent(IntPtr owner, uint message, uint subcode, IntPtr descriptor);
+ [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate IntPtr NativeControlResourceDeviceConstructor(IntPtr self);
+ [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate IntPtr NativeControlResourceComponentOverride(IntPtr self, int deviceIndex, IntPtr typedRecord);
private static readonly AreaMapGetter AreaMapGetterCallback = GetAreaMap;
private static readonly VectorInsert VectorInsertCallback = InsertVector;
@@ -57,6 +87,8 @@ internal static class BehaviorDistributorTickOracle {
private static readonly ObjectSetProperty ObjectSetPropertyCallback = SetProperty;
private static readonly InterfaceGetProperty InterfaceGetCallback = GetInterfaceProperty;
private static readonly InterfaceSetProperty InterfaceSetCallback = SetInterfaceProperty;
+ private static readonly NativeArealMapEvent ArealMapEventCallback = OnArealMapEvent;
+ private static ArealMapFixture activeArealMap;
private static IntPtr fastcallThunks;
private static IntPtr queryObjectThunk;
private static IntPtr getInterfacePropertyThunk;
@@ -158,7 +190,7 @@ internal static class BehaviorDistributorTickOracle {
Marshal.WriteInt32(entry, 0x04, BitConverter.ToInt32(BitConverter.GetBytes(1.0f), 0));
}
}
- private IntPtr Allocate(int size) {
+ public IntPtr Allocate(int size) {
IntPtr value = Marshal.AllocHGlobal(size);
Zero(value, size);
Allocations.Add(value);
@@ -171,7 +203,77 @@ internal static class BehaviorDistributorTickOracle {
}
}
+ private sealed class ComponentNode {
+ public readonly string ArchiveName;
+ public readonly string ResourceName;
+ public readonly uint Kind;
+ public readonly uint LocalIndex;
+ public readonly ComponentNode[] Children;
+ public ComponentNode(string archiveName, string resourceName, uint kind, uint localIndex, params ComponentNode[] children) {
+ ArchiveName = archiveName;
+ ResourceName = resourceName;
+ Kind = kind;
+ LocalIndex = localIndex;
+ Children = children ?? new ComponentNode[0];
+ }
+ }
+
+ private sealed class CapturedComponentDescriptor {
+ public string ArchiveName;
+ public string ResourceName;
+ public uint ParentId;
+ public uint LocalIndex;
+ public uint AssignedIdBeforeCallback;
+ public uint AssignedId;
+ public uint Message;
+ public uint Subcode;
+ }
+
+ private sealed class ArealMapFixture : IDisposable {
+ public IntPtr Owner;
+ public IntPtr OwnerVtable;
+ public readonly List<IntPtr> Allocations = new List<IntPtr>();
+ public readonly List<CapturedComponentDescriptor> Descriptors = new List<CapturedComponentDescriptor>();
+ public readonly uint[] AssignedIds = new uint[] { 41, 113, 127, 179 };
+ public string CallbackError;
+ public IntPtr Allocate(int size) {
+ IntPtr value = Marshal.AllocHGlobal(size);
+ Zero(value, size);
+ Allocations.Add(value);
+ return value;
+ }
+ public void Dispose() {
+ activeArealMap = null;
+ for (int i = Allocations.Count - 1; i >= 0; i--) Marshal.FreeHGlobal(Allocations[i]);
+ Allocations.Clear();
+ }
+ }
+
private static IntPtr GetAreaMap() { return active.Manager; }
+ private static int OnArealMapEvent(IntPtr owner, uint message, uint subcode, IntPtr descriptor) {
+ ArealMapFixture fixture = activeArealMap;
+ if (fixture == null) return 0;
+ if (owner != fixture.Owner) fixture.CallbackError = "ArealMap callback received an unexpected owner";
+ if (descriptor == IntPtr.Zero) {
+ fixture.CallbackError = "ArealMap callback received a null descriptor";
+ return 0;
+ }
+ int ordinal = fixture.Descriptors.Count;
+ uint assignedId = ordinal < fixture.AssignedIds.Length ? fixture.AssignedIds[ordinal] : 0xfffffff0u;
+ CapturedComponentDescriptor captured = new CapturedComponentDescriptor();
+ captured.ArchiveName = Marshal.PtrToStringAnsi(descriptor);
+ captured.ResourceName = Marshal.PtrToStringAnsi(IntPtr.Add(descriptor, 0x20));
+ captured.ParentId = unchecked((uint)Marshal.ReadInt32(descriptor, 0x40));
+ captured.LocalIndex = unchecked((uint)Marshal.ReadInt32(descriptor, 0x44));
+ captured.AssignedIdBeforeCallback = unchecked((uint)Marshal.ReadInt32(descriptor, 0x48));
+ captured.AssignedId = assignedId;
+ captured.Message = message;
+ captured.Subcode = subcode;
+ fixture.Descriptors.Add(captured);
+ Marshal.WriteInt32(descriptor, 0x48, unchecked((int)assignedId));
+ return 1;
+ }
+
private static void InsertVector(IntPtr self, IntPtr source, uint count, uint position) {
int length = Marshal.ReadInt32(self, 4);
int capacity = Marshal.ReadInt32(self, 8);
@@ -299,13 +401,54 @@ internal static class BehaviorDistributorTickOracle {
private static int Main() {
if (IntPtr.Size != 4) { Console.Error.WriteLine("Build with /platform:x86."); return 2; }
+ return RunWithIsolatedWorkingDirectory();
+ }
+
+ private static int RunWithIsolatedWorkingDirectory() {
+ string originalDirectory = Environment.CurrentDirectory;
+ string tempRoot = Path.GetFullPath(Path.GetTempPath()).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
+ string workDirectory = Path.Combine(tempRoot, "fparkan-native-oracle-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(workDirectory);
+ try {
+ // ArealMap.dll runs its attach initializer on load; keep any relative log output out of the workspace and game directory.
+ Environment.CurrentDirectory = workDirectory;
+ return RunOracle();
+ } finally {
+ Environment.CurrentDirectory = originalDirectory;
+ string fullWorkDirectory = Path.GetFullPath(workDirectory);
+ if (!String.Equals(fullWorkDirectory, workDirectory, StringComparison.OrdinalIgnoreCase) ||
+ !String.Equals(Path.GetDirectoryName(fullWorkDirectory), tempRoot, StringComparison.OrdinalIgnoreCase) ||
+ !Path.GetFileName(fullWorkDirectory).StartsWith("fparkan-native-oracle-", StringComparison.Ordinal))
+ throw new InvalidOperationException("refusing to remove an unexpected native oracle working directory");
+ if (Directory.Exists(fullWorkDirectory)) Directory.Delete(fullWorkDirectory, true);
+ }
+ }
+
+ private static int RunOracle() {
+ string controlPath = Environment.GetEnvironmentVariable("FPARKAN_ORIGINAL_CONTROL_DLL");
+ if (String.IsNullOrEmpty(controlPath)) controlPath = @"C:\GOG Games\Parkan - Iron Strategy\Control.dll";
+ if (!File.Exists(controlPath)) { Console.Error.WriteLine("Set FPARKAN_ORIGINAL_CONTROL_DLL to the installed Control.dll."); return 7; }
+ string controlHash = Hash(controlPath);
+ if (controlHash != ExpectedControlSha256) { Console.Error.WriteLine("Unexpected Control.dll SHA-256: " + controlHash); return 8; }
+ string arealMapPath = Environment.GetEnvironmentVariable("FPARKAN_ORIGINAL_AREALMAP_DLL");
+ if (String.IsNullOrEmpty(arealMapPath)) arealMapPath = @"C:\GOG Games\Parkan - Iron Strategy\ArealMap.dll";
+ if (!File.Exists(arealMapPath)) { Console.Error.WriteLine("Set FPARKAN_ORIGINAL_AREALMAP_DLL to the installed ArealMap.dll."); return 10; }
+ string arealMapHash = Hash(arealMapPath);
+ if (arealMapHash != ExpectedArealMapSha256) { Console.Error.WriteLine("Unexpected ArealMap.dll SHA-256: " + arealMapHash); return 11; }
+ IntPtr controlModule = LoadLibraryEx(controlPath, IntPtr.Zero, LoadWithAlteredSearchPath);
+ if (controlModule == IntPtr.Zero) { Console.Error.WriteLine("LoadLibraryEx(Control.dll) failed: " + Marshal.GetLastWin32Error()); return 9; }
string path = Environment.GetEnvironmentVariable("FPARKAN_ORIGINAL_BEHAVIOR_DLL");
if (String.IsNullOrEmpty(path)) path = @"C:\GOG Games\Parkan - Iron Strategy\Behavior.dll";
- if (!File.Exists(path)) { Console.Error.WriteLine("Set FPARKAN_ORIGINAL_BEHAVIOR_DLL to the installed Behavior.dll."); return 3; }
+ if (!File.Exists(path)) { Console.Error.WriteLine("Set FPARKAN_ORIGINAL_BEHAVIOR_DLL to the installed Behavior.dll."); FreeLibrary(controlModule); return 3; }
string hash = Hash(path);
- if (hash != ExpectedBehaviorSha256) { Console.Error.WriteLine("Unexpected Behavior.dll SHA-256: " + hash); return 4; }
+ if (hash != ExpectedBehaviorSha256) { Console.Error.WriteLine("Unexpected Behavior.dll SHA-256: " + hash); FreeLibrary(controlModule); return 4; }
IntPtr module = LoadLibraryEx(path, IntPtr.Zero, LoadWithAlteredSearchPath);
- if (module == IntPtr.Zero) { Console.Error.WriteLine("LoadLibraryEx(Behavior.dll) failed: " + Marshal.GetLastWin32Error()); return 5; }
+ if (module == IntPtr.Zero) { Console.Error.WriteLine("LoadLibraryEx(Behavior.dll) failed: " + Marshal.GetLastWin32Error()); FreeLibrary(controlModule); return 5; }
+ IntPtr arealMapModule = LoadLibraryEx(arealMapPath, IntPtr.Zero, LoadWithAlteredSearchPath);
+ if (arealMapModule == IntPtr.Zero) {
+ Console.Error.WriteLine("LoadLibraryEx(ArealMap.dll) failed: " + Marshal.GetLastWin32Error());
+ FreeLibrary(module); FreeLibrary(controlModule); return 12;
+ }
IntPtr import = IntPtr.Add(module, GetSystemArealMapIatRva);
IntPtr originalImport = Marshal.ReadIntPtr(import);
uint oldProtection = 0;
@@ -327,8 +470,8 @@ internal static class BehaviorDistributorTickOracle {
throw new InvalidOperationException("VirtualProtect(GetSystemArealMap IAT) failed: " + Marshal.GetLastWin32Error());
protectionChanged = true;
Marshal.WriteIntPtr(import, Marshal.GetFunctionPointerForDelegate(AreaMapGetterCallback));
- RunSmokeCases(module);
- Console.WriteLine("original Behavior.dll distributor oracle passed (native Add 0x1aac0, normalize 0x1ac90, tick 0x19e80; only GetSystemArealMap IAT and bounded vector insertion are substituted; this does not compare Rust parity).");
+ RunSmokeCases(module, controlModule, arealMapModule);
+ Console.WriteLine("original native Behavior/ArealMap/Control oracle passed (Behavior Add 0x1aac0, normalize 0x1ac90, tick 0x19e80; ArealMap UnitDAT projection 0x14b90; Control INTO override 0x2d890; only synthetic host callbacks, GetSystemArealMap IAT, and bounded vector insertion are substituted; this does not compare Rust parity).");
return 0;
}
catch (Exception error) {
@@ -346,14 +489,17 @@ internal static class BehaviorDistributorTickOracle {
GC.KeepAlive(ObjectQueryCallback); GC.KeepAlive(ObjectRateCallback);
GC.KeepAlive(ObjectGetPropertyCallback); GC.KeepAlive(ObjectSetPropertyCallback);
GC.KeepAlive(InterfaceGetCallback); GC.KeepAlive(InterfaceSetCallback);
+ GC.KeepAlive(ArealMapEventCallback);
if (thunksAllocated && !VirtualFree(fastcallThunks, UIntPtr.Zero, MemRelease))
throw new InvalidOperationException("VirtualFree(fastcall callback thunks) failed: " + Marshal.GetLastWin32Error());
if (x87ControlWordCell != IntPtr.Zero) { Marshal.FreeHGlobal(x87ControlWordCell); x87ControlWordCell = IntPtr.Zero; }
+ FreeLibrary(arealMapModule);
FreeLibrary(module);
+ FreeLibrary(controlModule);
}
}
- private static void RunSmokeCases(IntPtr module) {
+ private static void RunSmokeCases(IntPtr module, IntPtr controlModule, IntPtr arealMapModule) {
ushort savedControlWord = CurrentX87ControlWord();
Console.WriteLine("x87-control-word-before=0x" + savedControlWord.ToString("x4") + " (x86 oracle process)");
try {
@@ -361,10 +507,15 @@ internal static class BehaviorDistributorTickOracle {
Console.WriteLine("native-add-normalize-x87-control-word=0x" + CurrentX87ControlWord().ToString("x4"));
RunNativeAddCases(module);
RunNativeNormalizeCases(module);
+ RunNativeArealMapComponentBuilderCase(arealMapModule);
+ RunNativeControlIntoOverrideCase(controlModule);
+ RunNativeControlResourceDeviceCase(controlModule);
} finally {
SetX87ControlWord(savedControlWord);
Console.WriteLine("x87-control-word-restored=0x" + CurrentX87ControlWord().ToString("x4"));
}
+ RunNativeIndexedPropertyCases(module);
+ RunNativeOreInitializationCases(module);
TickDelegate tick = (TickDelegate)Marshal.GetDelegateForFunctionPointer(IntPtr.Add(module, TickRva), typeof(TickDelegate));
using (Fixture empty = NewFixture()) {
empty.MakeDistributor(); active = empty; tick(empty.Distributor, 0.0f);
@@ -387,6 +538,331 @@ internal static class BehaviorDistributorTickOracle {
RunSkippedCase(tick);
}
+ private static void RunNativeControlResourceDeviceCase(IntPtr module) {
+ Expect(CurrentX87ControlWord() == 0x007f, "Control selector 0x300 uses x87 control word 0x007f");
+ const uint key = 2;
+ const int rowCount = 22;
+ const int providerRow = (int)key * 0x2c;
+ IntPtr providerOwner = Marshal.AllocHGlobal(0x560);
+ IntPtr providerRows = Marshal.AllocHGlobal(rowCount * 0x2c);
+ IntPtr device = IntPtr.Zero;
+ try {
+ Zero(providerOwner, 0x560);
+ Zero(providerRows, rowCount * 0x2c);
+ IntPtr providerInterface = IntPtr.Add(providerOwner, 4);
+ IntPtr providerVtable = IntPtr.Add(module, ControlProviderInterfaceVtableRva);
+ Marshal.WriteIntPtr(providerInterface, providerVtable);
+ Expect(
+ Marshal.ReadIntPtr(providerVtable, 0x0c) == IntPtr.Add(module, ControlProviderType1GetterRva),
+ "Control provider interface vtable uses the native type-1 getter");
+ Marshal.WriteIntPtr(providerOwner, 0x55c, providerRows);
+
+ device = Marshal.AllocHGlobal(0x94);
+ Zero(device, 0x94);
+ Marshal.WriteInt32(device, 0x04, unchecked((int)key));
+ Marshal.WriteIntPtr(device, 0x44, providerInterface);
+
+ NativeControlResourceDeviceGet getter = (NativeControlResourceDeviceGet)
+ Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, ControlResourceDeviceGetterRva),
+ typeof(NativeControlResourceDeviceGet));
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0x3fe6982f, 0x3d25ae4a, 0x40715539, 0x3e8cb02e, "order-sensitive normal");
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0x7f7fffff, 0x40000000, 0x3e800000, 0x7effffff, "extended intermediate overflow then finite");
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0x00800000, 0x00800000, 0x7f7fffff, 0x017fffff, "extended intermediate underflow then finite");
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0x00800000, 0x3f800000, 0x3f000000, 0x00400000, "subnormal final result");
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0xc0000000, 0x3f000000, 0x40800000, 0xc0800000, "negative factors");
+ ExpectControlResourceDeviceResult(getter, device, providerRows, providerRow,
+ 0x80000000, 0x40000000, 0x40400000, 0x80000000, "negative zero");
+ Expect(CurrentX87ControlWord() == 0x007f, "Control selector 0x300 retains x87 control word 0x007f");
+ Console.WriteLine("native-control-tag26-selector-300: controlHash=" + ExpectedControlSha256 +
+ " cw=0x" + CurrentX87ControlWord().ToString("x4") + " key=2 rows=22");
+ GC.KeepAlive(getter);
+ } finally {
+ if (device != IntPtr.Zero) Marshal.FreeHGlobal(device);
+ Marshal.FreeHGlobal(providerRows);
+ Marshal.FreeHGlobal(providerOwner);
+ }
+ }
+
+ private static void RunNativeArealMapComponentBuilderCase(IntPtr module) {
+ using (ArealMapFixture fixture = new ArealMapFixture()) {
+ fixture.OwnerVtable = fixture.Allocate(0x40);
+ fixture.Owner = fixture.Allocate(4);
+ Marshal.WriteIntPtr(fixture.Owner, 0, fixture.OwnerVtable);
+ Marshal.WriteIntPtr(fixture.OwnerVtable, 0x34, Marshal.GetFunctionPointerForDelegate(ArealMapEventCallback));
+
+ ComponentNode tree = new ComponentNode("objects.rlb", "i_pws_f_df", 1, 9,
+ new ComponentNode("objects.rlb", "nested-slot", 1, 5,
+ new ComponentNode("objects.rlb", "nested-child", 1, 12)),
+ new ComponentNode("objects.rlb", "zero-kind-parent", 0, 77,
+ new ComponentNode("objects.rlb", "skipped-descendant", 1, 88)),
+ new ComponentNode("objects.rlb", "later-sibling", 1, 3));
+ IntPtr rootNode = fixture.Allocate(0x78);
+ PopulateUnitDatNode(fixture, rootNode, tree);
+
+ NativeFastcallComponentBuilder build = (NativeFastcallComponentBuilder)Marshal.GetDelegateForFunctionPointer(
+ WriteStdCallToFastcallThunk(0x180, IntPtr.Add(module, ArealMapUnitDatComponentBuilderRva)),
+ typeof(NativeFastcallComponentBuilder));
+ activeArealMap = fixture;
+ build(fixture.Owner, rootNode, 0);
+
+ Expect(fixture.CallbackError == null, "ArealMap descriptor callback owner and pointer");
+ Expect(fixture.Descriptors.Count == 4, "raw kind zero skips its node and descendants while later siblings continue");
+ string[] expectedResources = new string[] { "i_pws_f_df", "nested-slot", "nested-child", "later-sibling" };
+ uint[] expectedParents = new uint[] { 0, 41, 113, 41 };
+ uint[] expectedIndices = new uint[] { 9, 5, 12, 3 };
+ uint[] expectedAssignedIds = new uint[] { 41, 113, 127, 179 };
+ for (int i = 0; i < fixture.Descriptors.Count; i++) {
+ CapturedComponentDescriptor descriptor = fixture.Descriptors[i];
+ Expect(descriptor.ArchiveName == "objects.rlb" && descriptor.ResourceName == expectedResources[i] &&
+ descriptor.ParentId == expectedParents[i] && descriptor.LocalIndex == expectedIndices[i] &&
+ descriptor.AssignedIdBeforeCallback == 0xffffffff && descriptor.AssignedId == expectedAssignedIds[i] &&
+ descriptor.Message == 6 && descriptor.Subcode == 0x80000020,
+ "ArealMap descriptor " + i + " fields and callback-assigned parent ID");
+ }
+ Console.WriteLine("native-arealmap-unitdat-components: events=" + fixture.Descriptors.Count + " archive=objects.rlb" +
+ " parentIds=[" + String.Join(",", Array.ConvertAll(fixture.Descriptors.ToArray(), d => d.ParentId.ToString())) +
+ "] localIndices=[" + String.Join(",", Array.ConvertAll(fixture.Descriptors.ToArray(), d => d.LocalIndex.ToString())) +
+ "]; kind0-subtree=skipped sparse-assigned-ids=propagated");
+ GC.KeepAlive(build);
+ }
+ }
+
+ private static void RunNativeControlIntoOverrideCase(IntPtr module) {
+ NativeControlResourceComponentOverride applyOverride = (NativeControlResourceComponentOverride)Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, ControlResourceComponentOverrideRva), typeof(NativeControlResourceComponentOverride));
+ RunNativeControlOverrideReceiverCase(module, 19, 0x94, ControlResourceDeviceConstructorRva,
+ ControlResourceTag19VtableRva, ControlResourceTag19StringPairNoOpRva, applyOverride);
+ RunNativeControlOverrideReceiverCase(module, 9, 0x120, ControlResourceTag9ConstructorRva,
+ ControlResourceTag9VtableRva, ControlResourceTag9StringPairRva, applyOverride);
+ RunNativeControlOverrideReceiverCase(module, 15, 0x98, ControlResourceTag15ConstructorRva,
+ ControlResourceTag15VtableRva, ControlResourceNoOpStringPairRva, applyOverride);
+ RunNativeControlOverrideReceiverCase(module, 25, 0xa4, ControlResourceTag25ConstructorRva,
+ ControlResourceTag25VtableRva, ControlResourceNoOpStringPairRva, applyOverride);
+ GC.KeepAlive(applyOverride);
+ }
+
+ private static void RunNativeControlOverrideReceiverCase(IntPtr module, int tag, int objectSize,
+ int constructorRva, int vtableRva, int stringPairRva,
+ NativeControlResourceComponentOverride applyOverride) {
+ NativeControlResourceDeviceConstructor construct = (NativeControlResourceDeviceConstructor)Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, constructorRva), typeof(NativeControlResourceDeviceConstructor));
+ IntPtr expectedVtable = IntPtr.Add(module, vtableRva);
+ Expect(Marshal.ReadIntPtr(expectedVtable, 0x1c) == IntPtr.Add(module, ControlResourceCommonSetterRva),
+ "tag-" + tag + " vtable common setter slot resolves to native RVA 0x21d50");
+ Expect(Marshal.ReadIntPtr(expectedVtable, 0x24) == IntPtr.Add(module, stringPairRva),
+ "tag-" + tag + " vtable string-pair slot matches its native callback");
+ using (Fixture fixture = NewFixture()) {
+ IntPtr owner = fixture.Allocate(0x5c4);
+ IntPtr deviceArray = fixture.Allocate(IntPtr.Size);
+ IntPtr targetDevice = fixture.Allocate(objectSize);
+ IntPtr typedRecord = fixture.Allocate(0xb4);
+ Expect(construct(targetDevice) == targetDevice,
+ "native tag-" + tag + " constructor returns its exact-size device object");
+ Marshal.WriteIntPtr(deviceArray, 0, targetDevice);
+ Marshal.WriteIntPtr(owner, 0x5bc, deviceArray);
+ Marshal.WriteInt32(owner, 0x5c0, 1);
+
+ Marshal.WriteInt32(targetDevice, 0x04, unchecked((int)0x22222222));
+ Marshal.WriteInt32(targetDevice, 0x08, unchecked((int)0x12345678)); // component ID
+ Marshal.WriteInt32(targetDevice, 0x4c, unchecked((int)0x40200000)); // multiplier 2.5
+ for (int i = 0; i < 16; i++) {
+ Marshal.WriteInt32(targetDevice, 0x54 + i * 4, unchecked((int)(0xb2000000u + (uint)i)));
+ Marshal.WriteInt32(typedRecord, 0x2c + i * 4, unchecked((int)(0xc3000000u + (uint)i)));
+ }
+ Marshal.WriteInt32(typedRecord, 0x00, 19);
+ Marshal.WriteInt32(typedRecord, 0x04, unchecked((int)0x77777777)); // differs from the existing device key
+ Marshal.WriteInt32(typedRecord, 0x18, -1); // preserve the current class field
+ Marshal.WriteInt32(typedRecord, 0xac, 0); // no map rows or auxiliary string tail
+ if (tag == 9) {
+ // This synthetic receiver has no FX host and the descriptor has no map rows.
+ Marshal.WriteIntPtr(targetDevice, 0x3c, IntPtr.Zero);
+ }
+ int priorWord50 = Marshal.ReadInt32(targetDevice, 0x50);
+
+ IntPtr result = applyOverride(owner, 0, typedRecord);
+ Expect(result == IntPtr.Add(typedRecord, 0xb4),
+ "Control indexed override completes one tag-" + tag + " typed record");
+ Expect(Marshal.ReadInt32(owner, 0x5c0) == 1 && Marshal.ReadIntPtr(owner, 0x5bc) == deviceArray &&
+ Marshal.ReadIntPtr(deviceArray, 0) == targetDevice,
+ "Control INTO override keeps the existing tag-" + tag + " device slot and count");
+ Expect(Marshal.ReadIntPtr(targetDevice, 0) == expectedVtable &&
+ unchecked((uint)Marshal.ReadInt32(targetDevice, 0x04)) == 0x22222222 &&
+ unchecked((uint)Marshal.ReadInt32(targetDevice, 0x08)) == 0x12345678 &&
+ unchecked((uint)Marshal.ReadInt32(targetDevice, 0x4c)) == 0x40200000 &&
+ Marshal.ReadInt32(targetDevice, 0x50) == priorWord50 &&
+ Marshal.ReadIntPtr(targetDevice, 0x48) == typedRecord,
+ "selected tag-" + tag + " device retains class, existing key, component ID, multiplier, word +0x50, and records the replacement descriptor");
+ if (tag == 9)
+ Expect(Marshal.ReadIntPtr(targetDevice, 0x3c) == IntPtr.Zero,
+ "tag-9 string callback runs with no FX host on the synthetic receiver");
+ for (int i = 0; i < 16; i++) {
+ Expect(unchecked((uint)Marshal.ReadInt32(targetDevice, 0x54 + i * 4)) == 0xc3000000u + (uint)i,
+ "Control replaces selected tag-" + tag + " device coefficient " + i);
+ }
+ Console.WriteLine("native-control-tag" + tag + "-into-override: size=0x" + objectSize.ToString("x") +
+ " index=0 count=1 key=22222222 component=12345678 multiplier=" +
+ Float(unchecked((uint)Marshal.ReadInt32(targetDevice, 0x4c))) +
+ " original-class=preserved descriptor=attached coefficients=replaced");
+ GC.KeepAlive(construct);
+ }
+ }
+
+ private static void PopulateUnitDatNode(ArealMapFixture fixture, IntPtr destination, ComponentNode node) {
+ Zero(destination, 0x78);
+ Marshal.WriteInt32(destination, 0x04, node.Children.Length);
+ Marshal.WriteInt32(destination, 0x4c, unchecked((int)node.Kind));
+ Marshal.WriteInt32(destination, 0x50, unchecked((int)node.LocalIndex));
+ WriteFixedAnsi(destination, 0x0c, node.ArchiveName, 0x20);
+ WriteFixedAnsi(destination, 0x2c, node.ResourceName, 0x20);
+ if (node.Children.Length == 0) return;
+ IntPtr children = fixture.Allocate(node.Children.Length * 0x78);
+ Marshal.WriteIntPtr(destination, 0x08, children);
+ for (int i = 0; i < node.Children.Length; i++)
+ PopulateUnitDatNode(fixture, IntPtr.Add(children, i * 0x78), node.Children[i]);
+ }
+
+ private static void WriteFixedAnsi(IntPtr destination, int offset, string value, int capacity) {
+ byte[] bytes = Encoding.ASCII.GetBytes(value + "\0");
+ if (bytes.Length > capacity) throw new InvalidOperationException("synthetic UnitDAT string exceeds native field");
+ Marshal.Copy(bytes, 0, IntPtr.Add(destination, offset), bytes.Length);
+ }
+
+ private static void ExpectControlResourceDeviceResult(
+ NativeControlResourceDeviceGet getter,
+ IntPtr device,
+ IntPtr providerRows,
+ int providerRow,
+ uint providerBits,
+ uint baseBits,
+ uint multiplierBits,
+ uint expectedBits,
+ string label) {
+ Marshal.WriteInt32(providerRows, providerRow + 0x14, unchecked((int)providerBits));
+ Marshal.WriteInt32(device, 0x54, unchecked((int)baseBits));
+ Marshal.WriteInt32(device, 0x4c, unchecked((int)multiplierBits));
+ uint actualBits = unchecked((uint)FloatBits(getter(device, 0x300)));
+ Expect(actualBits == expectedBits,
+ "Control selector 0x300 " + label + " expected " + Hex(expectedBits) + " but got " + Hex(actualBits));
+ Console.WriteLine("native-control-tag26-selector-300-" + label + ": result=" + Hex(actualBits));
+ }
+
+ private static void RunNativeIndexedPropertyCases(IntPtr module) {
+ IndexedPropertySet set = (IndexedPropertySet)Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, IndexedPropertySetRva), typeof(IndexedPropertySet));
+ IndexedPropertyGet get = (IndexedPropertyGet)Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, IndexedPropertyGetRva), typeof(IndexedPropertyGet));
+ using (Fixture fixture = NewFixture()) {
+ // The handlers receive the embedded interface at owner+4. Its descriptor
+ // vector is at interface+0xa3c and its keyed property store at interface+0x5e4.
+ IntPtr owner = fixture.Allocate(0x0b20);
+ IntPtr iface = IntPtr.Add(owner, 4);
+ IntPtr descriptors = fixture.Allocate(6 * 0x18);
+ IntPtr propertyEntry = fixture.Allocate(0x18);
+ IntPtr maximumOutput = fixture.Allocate(sizeof(uint));
+ IntPtr currentOutput = fixture.Allocate(sizeof(uint));
+
+ Marshal.WriteInt32(iface, 0xa3c, 6);
+ Marshal.WriteIntPtr(iface, 0xa44, descriptors);
+ Marshal.WriteInt32(IntPtr.Add(descriptors, 4 * 0x18), 4, 4);
+ Marshal.WriteInt32(IntPtr.Add(descriptors, 5 * 0x18), 4, 5);
+
+ Marshal.WriteInt32(iface, 0x5e4, 0); // vector vtable is not used by these callbacks
+ Marshal.WriteInt32(iface, 0x5e8, 1); // count
+ Marshal.WriteInt32(iface, 0x5ec, 1); // capacity
+ Marshal.WriteIntPtr(iface, 0x5f0, propertyEntry);
+ Marshal.WriteInt32(propertyEntry, 0x00, unchecked((int)ResourceProperty));
+ Marshal.WriteInt32(propertyEntry, 0x04, unchecked((int)0x11223344));
+ Marshal.WriteInt32(propertyEntry, 0x08, FloatBits(2500.0f)); // auxiliary maximum
+ Marshal.WriteInt32(propertyEntry, 0x0c, unchecked((int)0x55667788));
+ Marshal.WriteInt32(propertyEntry, 0x10, unchecked((int)0x99aabbcc));
+ Marshal.WriteInt32(propertyEntry, 0x14, FloatBits(12.5f)); // primary current amount
+
+ Expect(get(iface, 4, maximumOutput) == 1 && Marshal.ReadInt32(maximumOutput) == FloatBits(2500.0f),
+ "descriptor 4 reads keyed auxiliary value as MaximumOre");
+ Expect(get(iface, 5, currentOutput) == 1 && Marshal.ReadInt32(currentOutput) == FloatBits(12.5f),
+ "descriptor 5 reads keyed primary float as CurrentOre");
+
+ set(iface, 5, unchecked((uint)FloatBits(21.75f)));
+ Expect(Marshal.ReadInt32(propertyEntry, 0x14) == FloatBits(21.75f) &&
+ Marshal.ReadInt32(propertyEntry, 0x08) == FloatBits(2500.0f),
+ "descriptor 5 updates CurrentOre and preserves MaximumOre auxiliary value");
+ set(iface, 4, unchecked((uint)FloatBits(999.0f)));
+ Expect(Marshal.ReadInt32(propertyEntry, 0x14) == FloatBits(21.75f) &&
+ Marshal.ReadInt32(propertyEntry, 0x08) == FloatBits(2500.0f),
+ "descriptor 4 is read-only through the indexed setter");
+ Expect(get(iface, 4, maximumOutput) == 1 && Marshal.ReadInt32(maximumOutput) == FloatBits(2500.0f) &&
+ get(iface, 5, currentOutput) == 1 && Marshal.ReadInt32(currentOutput) == FloatBits(21.75f),
+ "indexed resource values remain independently readable after CurrentOre write");
+
+ IntPtr reorderedDescriptors = fixture.Allocate(2 * 0x18);
+ Marshal.WriteInt32(iface, 0xa3c, 2);
+ Marshal.WriteIntPtr(iface, 0xa44, reorderedDescriptors);
+ Marshal.WriteInt32(reorderedDescriptors, 4, 5);
+ Marshal.WriteInt32(IntPtr.Add(reorderedDescriptors, 0x18), 4, 4);
+ Expect(get(iface, 0, currentOutput) == 1 && Marshal.ReadInt32(currentOutput) == FloatBits(21.75f),
+ "descriptor index zero follows its CurrentOre code 5 row");
+ Expect(get(iface, 1, maximumOutput) == 1 && Marshal.ReadInt32(maximumOutput) == FloatBits(2500.0f),
+ "descriptor index one follows its MaximumOre code 4 row");
+ set(iface, 0, unchecked((uint)FloatBits(31.25f)));
+ set(iface, 1, unchecked((uint)FloatBits(999.0f)));
+ Expect(Marshal.ReadInt32(propertyEntry, 0x14) == FloatBits(31.25f) &&
+ Marshal.ReadInt32(propertyEntry, 0x08) == FloatBits(2500.0f),
+ "reordered descriptor rows preserve CurrentOre write and MaximumOre read-only behavior");
+ Expect(get(iface, 0, currentOutput) == 1 && Marshal.ReadInt32(currentOutput) == FloatBits(31.25f) &&
+ get(iface, 1, maximumOutput) == 1 && Marshal.ReadInt32(maximumOutput) == FloatBits(2500.0f),
+ "reordered descriptor rows remain readable after their indexed writes");
+ Console.WriteLine("native-indexed-resource-property: maximum=" + Float(unchecked((uint)Marshal.ReadInt32(maximumOutput))) +
+ " current=" + Float(unchecked((uint)Marshal.ReadInt32(currentOutput))) +
+ "; descriptor4-write=no-op descriptor5-write=primary-only; reordered-indices=verified");
+ }
+ }
+
+ private static void RunNativeOreInitializationCases(IntPtr module) {
+ NativeOreInitialization initialize = (NativeOreInitialization)Marshal.GetDelegateForFunctionPointer(
+ IntPtr.Add(module, OreInitializationRva), typeof(NativeOreInitialization));
+ uint[][] cases = new uint[][] {
+ new uint[] { 0x80000004, 0x00000000, 0x43fa0000 },
+ new uint[] { 0x80000008, 0x00000000, 0x457a0000 },
+ new uint[] { 0x01002000, 0x00000000, 0x44fa0000 },
+ new uint[] { 0x01004000, 0x44fa0000, 0x44fa0000 },
+ new uint[] { 0x01008000, 0x00000000, 0x00000000 }
+ };
+ IntPtr vtable = IntPtr.Add(module, 0x59250);
+ foreach (uint[] test in cases) {
+ using (Fixture fixture = NewFixture()) {
+ IntPtr owner = fixture.Allocate(0x0b20);
+ IntPtr propertyEntry = fixture.Allocate(0x18);
+ Marshal.WriteIntPtr(owner, vtable);
+ Marshal.WriteIntPtr(owner, 0x30, owner);
+ Marshal.WriteInt32(owner, 0xa04, 2);
+ Marshal.WriteInt32(owner, 0xa64, 1); // suppress the wrapper's deferred side effect
+ Marshal.WriteInt32(owner, 0xafc, unchecked((int)test[0]));
+ Marshal.WriteInt32(owner, 0x70c, FloatBits(500.0f));
+ Marshal.WriteInt32(owner, 0x710, FloatBits(4000.0f));
+ Marshal.WriteInt32(owner, 0x720, FloatBits(2000.0f));
+ Marshal.WriteInt32(propertyEntry, 0x00, unchecked((int)ResourceProperty));
+ Marshal.WriteInt32(propertyEntry, 0x08, unchecked((int)0xbf800000)); // native add default auxiliary
+ Marshal.WriteInt32(propertyEntry, 0x14, FloatBits(123.0f)); // prove initialization overwrites primary
+ Marshal.WriteInt32(owner, 0x5ec, 1); // property count
+ Marshal.WriteInt32(owner, 0x5f0, 1); // property capacity
+ Marshal.WriteIntPtr(owner, 0x5f4, propertyEntry);
+
+ initialize(owner);
+
+ Expect(unchecked((uint)Marshal.ReadInt32(propertyEntry, 0x14)) == test[1] &&
+ unchecked((uint)Marshal.ReadInt32(propertyEntry, 0x08)) == test[2],
+ "native ore defaults for class 0x" + test[0].ToString("x8"));
+ }
+ }
+ Console.WriteLine("native-behavior-ore-initialization: classes=80000004,80000008,01002000,01004000,01008000; defaults=verified");
+ }
+
private static ushort CurrentX87ControlWord() {
if (x87ControlWordReader == null || x87ControlWordCell == IntPtr.Zero)
throw new InvalidOperationException("x87 control-word reader is not initialized");
@@ -762,6 +1238,15 @@ internal static class BehaviorDistributorTickOracle {
Marshal.Copy(code, 0, thunk, code.Length);
return thunk;
}
+ private static IntPtr WriteStdCallToFastcallThunk(int offset, IntPtr fastcallTarget) {
+ // Adapt stdcall(owner, node, parent ID) to the native __fastcall entry.
+ byte[] code = { 0x8b, 0x4c, 0x24, 0x04, 0x8b, 0x54, 0x24, 0x08, 0xff, 0x74, 0x24, 0x0c,
+ 0xb8, 0, 0, 0, 0, 0xff, 0xd0, 0xc2, 0x0c, 0x00 };
+ WriteImm32(code, 13, fastcallTarget.ToInt32());
+ IntPtr thunk = IntPtr.Add(fastcallThunks, offset);
+ Marshal.Copy(code, 0, thunk, code.Length);
+ return thunk;
+ }
private static IntPtr WriteX87ControlWordReader(int offset) {
byte[] code = { 0xd9, 0x3d, 0, 0, 0, 0, 0xc3 }; // fnstcw [absolute address]; ret
WriteImm32(code, 2, x87ControlWordCell.ToInt32());
@@ -791,7 +1276,7 @@ internal static class BehaviorDistributorTickOracle {
foreach (byte value in digest) hex.Append(value.ToString("x2")); return hex.ToString();
}
}
- private static void Expect(bool value, string message) { if (!value) throw new InvalidOperationException("original Behavior oracle failed: " + message); }
+ private static void Expect(bool value, string message) { if (!value) throw new InvalidOperationException("original native oracle failed: " + message); }
private static void ExpectLookupOrder(Fixture fixture, params string[] keys) {
List<string> actual = new List<string>();
foreach (string item in fixture.Trace) if (item.StartsWith("lookup:", StringComparison.Ordinal)) actual.Add(item.Substring(7));