diff options
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 2733 | ||||
| -rw-r--r-- | crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs | 525 | ||||
| -rw-r--r-- | docs/appendices/knowledge-boundaries.md | 101 | ||||
| -rw-r--r-- | docs/tomes/03-resources.md | 11 | ||||
| -rw-r--r-- | docs/tomes/06-behavior.md | 149 |
5 files changed, 3460 insertions, 59 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index baba3b3..cd7f2c2 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1,20 +1,24 @@ #![forbid(unsafe_code)] //! Runtime orchestration for mission loading and world stepping. +#[cfg(test)] +use fparkan_assets::PreparedControlResourcePair; use fparkan_assets::{ decode_mission_land_path, decode_mission_payload, decode_nres_payload, 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, - TerrainWorld, TmaProfile, + MissionTerrainPaths, NresError, PreparedComponentControl, PreparedControlEffectSection, + PreparedControlNodeDataRow, PreparedControlNodeDataTable, 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, @@ -149,6 +153,208 @@ pub struct BehaviorOreState { 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>, + /// Class-9-only scale used by its selector-0 getter. + pub class9_selector0_scale_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, +} + +/// Initial row in the loaded global Control node-data provider. +/// +/// The native table is indexed independently of MSH model nodes. Its rows are +/// appended in root/EXTO resource order and begin with a type-1 value of 1.0. +#[derive(Clone, Debug, PartialEq)] +pub struct MissionControlNodeProviderRow { + /// NDP resource that contributed the row. + pub source: ResourceKey, + /// Unit DAT component that contributed the row. + pub unit_component_index: usize, + /// Index in that component's original NDP table. + pub source_row_index: usize, + /// Original NDP descriptor fields and provenance for this provider row. + pub source_row: PreparedControlNodeDataRow, + /// Initial native provider selector-1 value (`+0x14`) as raw bits. + pub initial_type1_value_bits: u32, +} + +/// Availability of the initial global Control node-data provider projection. +#[derive(Clone, Debug, PartialEq)] +pub enum MissionControlNodeProviderTableStatus { + /// Rows in native append order, without padding to the MSH node count. + Available(Vec<MissionControlNodeProviderRow>), + /// Provider lookup is not modeled for this object's available inputs. + Unavailable(MissionControlNodeProviderTableUnavailableReason), +} + +/// Why the global Control node-data provider could not be projected. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum MissionControlNodeProviderTableUnavailableReason { + /// A root or mounted EXTO Control resource has no resolved NDP sidecar. + MissingNodeData { + /// Unit DAT component needing the provider rows. + unit_component_index: usize, + /// Explicitly requested sidecar when the reference was known. + source: Option<ResourceKey>, + }, + /// A mounted EXTO needs a first NDP row for the native skip-first path. + MissingExToRootRow { unit_component_index: usize }, + /// The Control component traversal itself was incomplete. + DeviceTableUnavailable(MissionControlDeviceTableUnavailableReason), +} + +/// Why a loaded Control resource query or write could not be represented. +#[derive(Debug, Eq, PartialEq)] +pub enum ControlResourceAccessError { + /// The requested world object handle is invalid, stale, or deleted. + World(fparkan_world::WorldError), + /// No mission is currently loaded. + MissionUnavailable, + /// The live object is not part of the loaded mission projection. + ObjectUnavailable { handle: ObjectHandle }, + /// The object's Control table could not be completely projected. + TableUnavailable(MissionControlDeviceTableUnavailableReason), + /// This wrapper only implements native resource selectors 1 and 2. + UnsupportedSelector(u32), + /// A selected row has a factory class whose getter has not been checked. + UnsupportedFactoryTag { factory_tag: u32 }, + /// A device index does not exist in the object's native Control table. + DeviceIndexOutOfRange { + /// Requested global Control device index. + device_index: usize, + /// Projected device count. + device_count: usize, + }, + /// The node provider table could not be projected. + NodeProviderTableUnavailable(MissionControlNodeProviderTableUnavailableReason), + /// A device's native provider key exceeds the actual appended NDP rows. + NodeProviderKeyOutOfRange { + /// Current native device key, used directly as a provider row index. + key: u32, + /// Actual global provider row count. + row_count: usize, + }, + /// A native tag has an e00 callback that has not been implemented here. + UnsupportedCurrentTag { tag: u32 }, + /// A selected row lacks a value needed by the native getter or setter. + UnsupportedField { + factory_tag: u32, + field: &'static str, + }, + /// A native float operand or intermediate is not finite. + NonFiniteValue { + factory_tag: u32, + field: &'static str, + }, + /// A native float operand is subnormal and is outside the checked domain. + SubnormalValue { + factory_tag: u32, + field: &'static str, + }, + /// A range probe places a nonzero arithmetic result outside normal f32 range. + UnsupportedFloatRange { + factory_tag: Option<u32>, + field: &'static str, + }, +} + +/// 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, + /// Initial global NDP provider rows used by indexed Control selectors. + pub node_provider_status: MissionControlNodeProviderTableStatus, +} + +struct ControlDeviceProjection { + devices: Vec<EffectiveControlDevice>, + node_provider_status: MissionControlNodeProviderTableStatus, +} + /// Resource distributor state associated with one TMA clan's constructed AI. #[derive(Clone, Debug, PartialEq)] pub struct BehaviorDistributorState { @@ -163,6 +369,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)] @@ -480,6 +688,7 @@ struct LoadedMissionState { 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>, @@ -1079,6 +1288,46 @@ fn load_mission_with_options( Vec::new() }; + let control_device_tables = if options.asset_scope == MissionAssetScope::Full { + object_drafts + .iter() + .zip(&handles) + .enumerate() + .map(|(object_index, (draft, handle))| { + let projection = build_control_device_projection( + object_index, + draft, + &mission_assets, + prototype_graph + .root_prototype_request_spans + .get(object_index), + &resolved_prototypes, + ); + match projection { + Ok(projection) => MissionControlDeviceTableState { + original_id: draft.original_id, + handle: *handle, + status: MissionControlDeviceTableStatus::Available(projection.devices), + node_provider_status: projection.node_provider_status, + }, + Err(reason) => MissionControlDeviceTableState { + original_id: draft.original_id, + handle: *handle, + status: MissionControlDeviceTableStatus::Unavailable(reason.clone()), + node_provider_status: + MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::DeviceTableUnavailable( + reason, + ), + ), + }, + } + }) + .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"); @@ -1155,6 +1404,7 @@ fn load_mission_with_options( 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, @@ -1446,6 +1696,363 @@ pub fn loaded_mission_behavior_ore_states(engine: &Engine) -> Option<&[BehaviorO .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()) +} + +/// Queries native Control global-table selector 1 (weighted ratio) or 2 (sum). +/// `Ok(None)` preserves the original false/no-result behavior for an empty or +/// zero-denominator table. Other unsupported native paths are reported as errors. +/// Inputs and intermediates must be finite and normal, or exact zero. +/// +/// # Errors +/// Returns an error for an invalid object handle, unavailable mission/table, +/// unsupported selector or factory, missing state, or an unsupported float range. +pub fn query_loaded_mission_control_resource( + engine: &Engine, + handle: ObjectHandle, + selector: u32, +) -> Result<Option<f32>, ControlResourceAccessError> { + transform_state(&engine.world, handle).map_err(ControlResourceAccessError::World)?; + let loaded = engine + .loaded + .as_ref() + .ok_or(ControlResourceAccessError::MissionUnavailable)?; + let table = loaded + .control_device_tables + .iter() + .find(|table| table.handle == handle) + .ok_or(ControlResourceAccessError::ObjectUnavailable { handle })?; + query_control_device_table_resource(table, selector) +} + +/// Queries one loaded Control device through its original factory getter. +/// +/// Selector `0xe00` is currently modeled for current descriptor tag 26 with +/// factory tag 26. The loaded NDP provider value is its initial native state; +/// later Control updates are not synchronized into this snapshot. +/// +/// # Errors +/// Returns an error for an invalid handle, unavailable mission/table/provider, +/// an out-of-range device/provider key, or a factory/tag/float path that is not +/// supported by the current reconstruction. +pub fn query_loaded_mission_control_device_selector( + engine: &Engine, + handle: ObjectHandle, + device_index: usize, + selector: u32, +) -> Result<Option<f32>, ControlResourceAccessError> { + transform_state(&engine.world, handle).map_err(ControlResourceAccessError::World)?; + let loaded = engine + .loaded + .as_ref() + .ok_or(ControlResourceAccessError::MissionUnavailable)?; + let table = loaded + .control_device_tables + .iter() + .find(|table| table.handle == handle) + .ok_or(ControlResourceAccessError::ObjectUnavailable { handle })?; + query_control_device_table_selector(table, device_index, selector) +} + +/// Sets native Control global-table selector 1 on rows whose selector-0 value +/// is positive. Validation is completed before any row is changed. +/// +/// # Errors +/// Returns an error for an invalid object handle, unavailable mission/table, +/// missing state, a non-finite or subnormal value, or an unsupported float range. +pub fn set_loaded_mission_control_multiplier( + engine: &mut Engine, + handle: ObjectHandle, + value: f32, +) -> Result<(), ControlResourceAccessError> { + transform_state(&engine.world, handle).map_err(ControlResourceAccessError::World)?; + let loaded = engine + .loaded + .as_mut() + .ok_or(ControlResourceAccessError::MissionUnavailable)?; + let table = loaded + .control_device_tables + .iter_mut() + .find(|table| table.handle == handle) + .ok_or(ControlResourceAccessError::ObjectUnavailable { handle })?; + set_control_device_table_multiplier(table, value) +} + +fn query_control_device_table_resource( + table: &MissionControlDeviceTableState, + selector: u32, +) -> Result<Option<f32>, ControlResourceAccessError> { + if !matches!(selector, 1 | 2) { + return Err(ControlResourceAccessError::UnsupportedSelector(selector)); + } + let entries = match &table.status { + MissionControlDeviceTableStatus::Available(entries) => entries, + MissionControlDeviceTableStatus::Unavailable(reason) => { + return Err(ControlResourceAccessError::TableUnavailable(reason.clone())); + } + }; + + let mut numerator = 0.0_f32; + let mut denominator = 0.0_f32; + for entry in entries.iter().filter(|entry| entry.descriptor.tag == 19) { + let base = control_device_selector_zero(entry)?; + if base < 0.0 { + return Ok(Some(if selector == 1 { 1.0 } else { base })); + } + if selector == 2 { + denominator = checked_control_sum(entry.factory_tag, "sum", denominator, base)?; + } else { + let multiplier = control_device_float(entry, entry.multiplier_bits, "multiplier")?; + let weighted = checked_control_product( + entry.factory_tag, + "weighted value", + control_device_coefficient(entry)?, + multiplier, + )?; + numerator = checked_control_sum(entry.factory_tag, "numerator", numerator, weighted)?; + denominator = checked_control_sum(entry.factory_tag, "denominator", denominator, base)?; + } + } + + if selector == 2 { + return Ok((denominator > 0.0).then_some(denominator)); + } + if denominator <= 0.0 { + return Ok(None); + } + checked_control_ratio("ratio", numerator, denominator).map(Some) +} + +fn query_control_device_table_selector( + table: &MissionControlDeviceTableState, + device_index: usize, + selector: u32, +) -> Result<Option<f32>, ControlResourceAccessError> { + if selector != 0xe00 { + return Err(ControlResourceAccessError::UnsupportedSelector(selector)); + } + let devices = match &table.status { + MissionControlDeviceTableStatus::Available(devices) => devices, + MissionControlDeviceTableStatus::Unavailable(reason) => { + return Err(ControlResourceAccessError::TableUnavailable(reason.clone())); + } + }; + let device = + devices + .get(device_index) + .ok_or(ControlResourceAccessError::DeviceIndexOutOfRange { + device_index, + device_count: devices.len(), + })?; + match device.descriptor.tag { + 26 => {} + 5 | 10 | 21 => { + return Err(ControlResourceAccessError::UnsupportedCurrentTag { + tag: device.descriptor.tag, + }); + } + _ => return Ok(None), + } + if device.factory_tag != 26 { + return Err(ControlResourceAccessError::UnsupportedFactoryTag { + factory_tag: device.factory_tag, + }); + } + + let providers = match &table.node_provider_status { + MissionControlNodeProviderTableStatus::Available(providers) => providers, + MissionControlNodeProviderTableStatus::Unavailable(reason) => { + return Err(ControlResourceAccessError::NodeProviderTableUnavailable( + reason.clone(), + )); + } + }; + let key = usize::try_from(device.key).map_err(|_| { + ControlResourceAccessError::NodeProviderKeyOutOfRange { + key: device.key, + row_count: providers.len(), + } + })?; + let provider = + providers + .get(key) + .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { + key: device.key, + row_count: providers.len(), + })?; + let provider_value = checked_control_float( + device.factory_tag, + "node provider value", + f32::from_bits(provider.initial_type1_value_bits), + )?; + let coefficient = control_device_coefficient(device)?; + let multiplier = control_device_float(device, device.multiplier_bits, "multiplier")?; + let provider_weighted = checked_control_product( + device.factory_tag, + "provider times coefficient", + provider_value, + coefficient, + )?; + checked_control_product( + device.factory_tag, + "then multiplier", + provider_weighted, + multiplier, + ) + .map(Some) +} + +fn set_control_device_table_multiplier( + table: &mut MissionControlDeviceTableState, + value: f32, +) -> Result<(), ControlResourceAccessError> { + let entries = match &mut table.status { + MissionControlDeviceTableStatus::Available(entries) => entries, + MissionControlDeviceTableStatus::Unavailable(reason) => { + return Err(ControlResourceAccessError::TableUnavailable(reason.clone())); + } + }; + + let mut has_target = false; + let mut target_factory_tag = 0; + for entry in entries.iter() { + if entry.descriptor.tag != 19 { + continue; + } + let base = control_device_selector_zero(entry)?; + if base > 0.0 { + has_target = true; + target_factory_tag = entry.factory_tag; + } + } + if !has_target { + return Ok(()); + } + checked_control_float(target_factory_tag, "multiplier", value)?; + for entry in entries + .iter_mut() + .filter(|entry| entry.descriptor.tag == 19) + { + if control_device_selector_zero(entry)? > 0.0 { + entry.multiplier_bits = Some(value.to_bits()); + } + } + Ok(()) +} + +fn control_device_selector_zero( + entry: &EffectiveControlDevice, +) -> Result<f32, ControlResourceAccessError> { + let value = match entry.factory_tag { + 9 => { + let coefficient = control_device_coefficient(entry)?; + let scale = control_device_float( + entry, + entry.class9_selector0_scale_bits, + "class-9 selector-0 scale", + )?; + checked_control_product(entry.factory_tag, "selector-0 value", coefficient, scale)? + } + 15 | 19 | 25 => control_device_coefficient(entry)?, + factory_tag => { + return Err(ControlResourceAccessError::UnsupportedFactoryTag { factory_tag }); + } + }; + checked_control_float(entry.factory_tag, "selector-0 value", value) +} + +fn control_device_coefficient( + entry: &EffectiveControlDevice, +) -> Result<f32, ControlResourceAccessError> { + let words = entry + .coefficient_bits + .ok_or(ControlResourceAccessError::UnsupportedField { + factory_tag: entry.factory_tag, + field: "coefficient", + })?; + control_device_float(entry, Some(words[0]), "coefficient") +} + +fn control_device_float( + entry: &EffectiveControlDevice, + bits: Option<u32>, + field: &'static str, +) -> Result<f32, ControlResourceAccessError> { + let bits = bits.ok_or(ControlResourceAccessError::UnsupportedField { + factory_tag: entry.factory_tag, + field, + })?; + checked_control_float(entry.factory_tag, field, f32::from_bits(bits)) +} + +fn checked_control_float( + factory_tag: u32, + field: &'static str, + value: f32, +) -> Result<f32, ControlResourceAccessError> { + if !value.is_finite() { + return Err(ControlResourceAccessError::NonFiniteValue { factory_tag, field }); + } + if value.is_subnormal() { + return Err(ControlResourceAccessError::SubnormalValue { factory_tag, field }); + } + Ok(value) +} + +fn checked_control_product( + factory_tag: u32, + field: &'static str, + lhs: f32, + rhs: f32, +) -> Result<f32, ControlResourceAccessError> { + let range_probe = f64::from(lhs) * f64::from(rhs); + check_control_range_probe(Some(factory_tag), field, range_probe)?; + checked_control_float(factory_tag, field, lhs * rhs) +} + +fn checked_control_ratio( + field: &'static str, + numerator: f32, + denominator: f32, +) -> Result<f32, ControlResourceAccessError> { + let range_probe = f64::from(numerator) / f64::from(denominator); + check_control_range_probe(None, field, range_probe)?; + checked_control_float(0, field, numerator / denominator) +} + +fn checked_control_sum( + factory_tag: u32, + field: &'static str, + lhs: f32, + rhs: f32, +) -> Result<f32, ControlResourceAccessError> { + let range_probe = f64::from(lhs) + f64::from(rhs); + check_control_range_probe(Some(factory_tag), field, range_probe)?; + checked_control_float(factory_tag, field, lhs + rhs) +} + +fn check_control_range_probe( + factory_tag: Option<u32>, + field: &'static str, + range_probe: f64, +) -> Result<(), ControlResourceAccessError> { + let absolute = range_probe.abs(); + let minimum_normal = f64::from(f32::MIN_POSITIVE); + let maximum = f64::from(f32::MAX); + if range_probe != 0.0 && (absolute < minimum_normal || absolute > maximum) { + return Err(ControlResourceAccessError::UnsupportedFloatRange { factory_tag, field }); + } + Ok(()) +} + fn behavior_clan_for_selector( selector: u32, clan_count: usize, @@ -1494,6 +2101,580 @@ fn initialize_behavior_ore_state( } } +#[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_component_mount_node( + tree: &[UnitComponentTreeNode], + component_ids: &[Option<u32>], + first_global_nodes: &[Option<u32>], + component_index: usize, + raw_local_index: i32, +) -> Option<u32> { + 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)), + ) + }) +} + +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, + class9_selector0_scale_bits: (descriptor.tag == 9).then_some(0x3f80_0000), + 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>) { + match factory_tag { + 1 | 4 => (Some(0x3f80_0000), Some(0x200)), + 5 | 8 | 26 => (Some(0x3f80_0000), Some(0x5)), + 10 => (Some(0x3f80_0000), Some(0x2000)), + 17 | 19 => (Some(0x3f80_0000), Some(0x20)), + 9 | 15 | 25 => (Some(0x3f80_0000), None), + _ => (None, None), + } +} + +#[cfg(test)] +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> { + build_control_device_projection(object_index, draft, assets, prototype_span, prototypes) + .map(|projection| projection.devices) +} + +fn build_control_device_projection( + object_index: usize, + draft: &MissionObjectDraft, + assets: &MissionAssets, + prototype_span: Option<&std::ops::Range<usize>>, + prototypes: &[fparkan_prototype::EffectivePrototype], +) -> Result<ControlDeviceProjection, 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 node_provider_status = MissionControlNodeProviderTableStatus::Available(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); + 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 mount_node = if is_ex_to { + native_component_mount_node( + &tree, + &component_ids, + &first_global_nodes, + component_index, + raw_local_index, + ) + } else { + None + }; + if is_root { + append_control_node_provider_rows( + &mut node_provider_status, + component_index, + prototype.node_data_resource.as_ref(), + control.node_data.as_ref(), + false, + ); + } else if is_ex_to && mount_node.is_some_and(|node| node != u32::MAX) { + append_control_node_provider_rows( + &mut node_provider_status, + component_index, + prototype.node_data_resource.as_ref(), + control.node_data.as_ref(), + true, + ); + } + 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 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 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(ControlDeviceProjection { + devices, + node_provider_status, + }) +} + +fn append_control_node_provider_rows( + status: &mut MissionControlNodeProviderTableStatus, + unit_component_index: usize, + requested_source: Option<&ResourceKey>, + node_data: Option<&PreparedControlNodeDataTable>, + skip_first_row: bool, +) { + let MissionControlNodeProviderTableStatus::Available(rows) = status else { + return; + }; + let Some(node_data) = node_data else { + *status = MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::MissingNodeData { + unit_component_index, + source: requested_source.cloned(), + }, + ); + return; + }; + if skip_first_row && node_data.rows.is_empty() { + *status = MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::MissingExToRootRow { + unit_component_index, + }, + ); + return; + } + let first_row = usize::from(skip_first_row); + for (source_row_index, source_row) in node_data.rows.iter().enumerate().skip(first_row) { + rows.push(MissionControlNodeProviderRow { + source: node_data.source.clone(), + unit_component_index, + source_row_index, + source_row: source_row.clone(), + initial_type1_value_bits: 0x3f80_0000, + }); + } +} + fn add_behavior_registered_object( distributor: &mut BehaviorDistributorState, mut object: BehaviorRegisteredObject, @@ -1867,7 +3048,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 { @@ -1887,6 +3068,764 @@ 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_node_data_table( + index: usize, + row_count: usize, + ) -> PreparedControlNodeDataTable { + PreparedControlNodeDataTable { + source: synthetic_resource_key(300 + index), + rows: (0..row_count) + .map(|row_index| PreparedControlNodeDataRow { + raw_flags: u32::try_from(row_index).expect("row index fits u32"), + max_volume: (100 + row_index) as f32, + mobility_per_volume: (200 + row_index) as f32, + effect: PreparedControlResourcePair { + archive: [0; 32], + name: [0; 32], + }, + }) + .collect(), + } + } + + fn control_with_synthetic_node_data( + index: usize, + typed_records: Vec<PreparedControlTypedRecord>, + row_count: usize, + ) -> PreparedComponentControl { + let mut control = synthetic_component_control( + index, + typed_records, + PreparedControlEffectSection::Parsed(Vec::new()), + ); + control.node_data = Some(synthetic_control_node_data_table(index, row_count)); + control + } + + fn synthetic_node_provider_row( + component_index: usize, + source_row_index: usize, + initial_type1_value_bits: u32, + ) -> MissionControlNodeProviderRow { + MissionControlNodeProviderRow { + source: synthetic_resource_key(300 + component_index), + unit_component_index: component_index, + source_row_index, + source_row: PreparedControlNodeDataRow { + raw_flags: u32::try_from(source_row_index).expect("row index fits u32"), + max_volume: 100.0, + mobility_per_volume: 200.0, + effect: PreparedControlResourcePair { + archive: [0; 32], + name: [0; 32], + }, + }, + initial_type1_value_bits, + } + } + + 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()), + node_data_resource: controls[index] + .as_ref() + .and_then(|control| control.node_data.as_ref()) + .map(|node_data| node_data.source.clone()), + point_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, + ) + } + + fn synthetic_control_projection( + source_types: &[Option<u32>], + kinds: &[u32], + child_counts: &[u32], + parent_or_links: &[i32], + model_nodes: &[usize], + controls: Vec<Option<PreparedComponentControl>>, + ) -> ( + MissionControlDeviceTableStatus, + MissionControlNodeProviderTableStatus, + ) { + let (draft, assets, prototypes) = synthetic_control_fixture( + source_types, + kinds, + child_counts, + parent_or_links, + model_nodes, + controls, + ); + let (status, node_provider_status) = match build_control_device_projection( + 0, + &draft, + &assets, + Some(&(0..prototypes.len())), + &prototypes, + ) { + Ok(projection) => ( + MissionControlDeviceTableStatus::Available(projection.devices), + projection.node_provider_status, + ), + Err(reason) => ( + MissionControlDeviceTableStatus::Unavailable(reason.clone()), + MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::DeviceTableUnavailable( + reason, + ), + ), + ), + }; + (status, node_provider_status) + } + + fn synthetic_effective_control_device( + factory_tag: u32, + descriptor_tag: u32, + coefficient_bits: u32, + multiplier_bits: Option<u32>, + class9_scale_bits: Option<u32>, + ) -> EffectiveControlDevice { + let descriptor = synthetic_control_record(descriptor_tag, 2, coefficient_bits, None); + let control = synthetic_component_control( + 0, + vec![descriptor.clone()], + PreparedControlEffectSection::Parsed(Vec::new()), + ); + let provenance = control_device_record_provenance(0, 0, &control); + let mut coefficients = [0; 16]; + coefficients[0] = coefficient_bits; + EffectiveControlDevice { + factory_tag, + component_id: 3, + key: 2, + coefficient_bits: Some(coefficients), + multiplier_bits, + class9_selector0_scale_bits: class9_scale_bits, + native_word_50_bits: None, + descriptor, + factory_provenance: provenance.clone(), + descriptor_provenance: provenance, + } + } + + fn synthetic_control_device_table( + status: MissionControlDeviceTableStatus, + ) -> MissionControlDeviceTableState { + synthetic_control_device_table_with_providers( + status, + MissionControlNodeProviderTableStatus::Available(Vec::new()), + ) + } + + fn synthetic_control_device_table_with_providers( + status: MissionControlDeviceTableStatus, + node_provider_status: MissionControlNodeProviderTableStatus, + ) -> MissionControlDeviceTableState { + MissionControlDeviceTableState { + original_id: Some(OriginalObjectId(7)), + handle: ObjectHandle { + generation: 5, + slot: 7, + }, + status, + node_provider_status, + } + } + + #[test] + fn control_resource_queries_filter_current_tag_and_use_factory_getters() { + let table = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 9, + 19, + 2.0_f32.to_bits(), + Some(3.0_f32.to_bits()), + Some(0.5_f32.to_bits()), + ), + synthetic_effective_control_device( + 19, + 9, + 100.0_f32.to_bits(), + Some(77.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 15, + 19, + 4.0_f32.to_bits(), + Some(0.5_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(4.0_f32.to_bits()), + None, + ), + ])); + + assert_eq!( + query_control_device_table_resource(&table, 1) + .expect("selector 1") + .map(f32::to_bits), + Some(2.0_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_resource(&table, 2) + .expect("selector 2") + .map(f32::to_bits), + Some(6.0_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_resource(&table, 3), + Err(ControlResourceAccessError::UnsupportedSelector(3)) + ); + } + + #[test] + fn control_resource_setter_updates_matching_rows_atomically_and_round_trips() { + let mut table = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 9, + 19, + 2.0_f32.to_bits(), + Some(3.0_f32.to_bits()), + Some(0.5_f32.to_bits()), + ), + synthetic_effective_control_device( + 19, + 9, + 100.0_f32.to_bits(), + Some(77.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 15, + 19, + 4.0_f32.to_bits(), + Some(0.5_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(4.0_f32.to_bits()), + None, + ), + ])); + + set_control_device_table_multiplier(&mut table, 7.5).expect("setter 1"); + let MissionControlDeviceTableStatus::Available(entries) = &table.status else { + panic!("synthetic table is available"); + }; + assert_eq!( + entries + .iter() + .map(|entry| entry.multiplier_bits) + .collect::<Vec<_>>(), + [ + Some(7.5_f32.to_bits()), + Some(77.0_f32.to_bits()), + Some(7.5_f32.to_bits()), + Some(7.5_f32.to_bits()) + ] + ); + assert_eq!( + entries[0].class9_selector0_scale_bits, + Some(0.5_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_resource(&table, 1) + .expect("updated selector 1") + .map(f32::to_bits), + Some(8.75_f32.to_bits()) + ); + + let mut unsupported = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + 2.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device(12, 19, 1.0_f32.to_bits(), None, None), + ])); + assert_eq!( + set_control_device_table_multiplier(&mut unsupported, 2.0), + Err(ControlResourceAccessError::UnsupportedFactoryTag { factory_tag: 12 }) + ); + let MissionControlDeviceTableStatus::Available(entries) = &unsupported.status else { + panic!("synthetic table is available"); + }; + assert_eq!(entries[0].multiplier_bits, Some(1.0_f32.to_bits())); + } + + #[test] + fn control_resource_queries_preserve_native_no_result_and_negative_rules() { + let empty = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(Vec::new())); + assert_eq!(query_control_device_table_resource(&empty, 1), Ok(None)); + assert_eq!(query_control_device_table_resource(&empty, 2), Ok(None)); + assert!(set_control_device_table_multiplier(&mut empty.clone(), f32::NAN).is_ok()); + + let zero_denominator = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + 0.0_f32.to_bits(), + Some(2.0_f32.to_bits()), + None, + ), + ])); + assert_eq!( + query_control_device_table_resource(&zero_denominator, 1), + Ok(None) + ); + assert_eq!( + query_control_device_table_resource(&zero_denominator, 2), + Ok(None) + ); + + let nonmatching = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 9, + 4.0_f32.to_bits(), + Some(2.0_f32.to_bits()), + None, + ), + ])); + assert_eq!( + query_control_device_table_resource(&nonmatching, 1), + Ok(None) + ); + + let no_multiplier = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device(19, 19, 2.0_f32.to_bits(), None, None), + ])); + assert_eq!( + query_control_device_table_resource(&no_multiplier, 2) + .expect("selector 2 does not read multiplier") + .map(f32::to_bits), + Some(2.0_f32.to_bits()) + ); + + let negative = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device(19, 19, (-1.0_f32).to_bits(), None, None), + ])); + assert_eq!( + query_control_device_table_resource(&negative, 1) + .expect("negative selector 1") + .map(f32::to_bits), + Some(1.0_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_resource(&negative, 2) + .expect("negative selector 2") + .map(f32::to_bits), + Some((-1.0_f32).to_bits()) + ); + + let rounded = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(16_777_216.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some((-16_777_216.0_f32).to_bits()), + None, + ), + ])); + assert_eq!( + query_control_device_table_resource(&rounded, 1) + .expect("rounded selector 1") + .map(f32::to_bits), + Some(0.0_f32.to_bits()) + ); + + let mut replaceable = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(f32::INFINITY.to_bits()), + None, + ), + ])); + set_control_device_table_multiplier(&mut replaceable, 2.0) + .expect("setter does not read the old multiplier"); + let MissionControlDeviceTableStatus::Available(entries) = &replaceable.status else { + panic!("synthetic table is available"); + }; + assert_eq!(entries[0].multiplier_bits, Some(2.0_f32.to_bits())); + } + + #[test] + fn control_resource_access_reports_unavailable_and_nonfinite_inputs() { + let unavailable = + synthetic_control_device_table(MissionControlDeviceTableStatus::Unavailable( + MissionControlDeviceTableUnavailableReason::NoControlResource, + )); + assert_eq!( + query_control_device_table_resource(&unavailable, 1), + Err(ControlResourceAccessError::TableUnavailable( + MissionControlDeviceTableUnavailableReason::NoControlResource + )) + ); + + let nonfinite = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + f32::INFINITY.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + ])); + assert!(matches!( + query_control_device_table_resource(&nonfinite, 1), + Err(ControlResourceAccessError::NonFiniteValue { + factory_tag: 19, + field: "coefficient" + }) + )); + + let subnormal = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device(19, 19, 1, Some(1.0_f32.to_bits()), None), + ])); + assert!(matches!( + query_control_device_table_resource(&subnormal, 2), + Err(ControlResourceAccessError::SubnormalValue { + factory_tag: 19, + field: "coefficient" + }) + )); + + let mut finite = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + 1.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + ])); + assert!(matches!( + set_control_device_table_multiplier(&mut finite, f32::INFINITY), + Err(ControlResourceAccessError::NonFiniteValue { + factory_tag: 19, + field: "multiplier" + }) + )); + + let underflowing_product = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + f32::MIN_POSITIVE.to_bits(), + Some(f32::MIN_POSITIVE.to_bits()), + None, + ), + ])); + assert!(matches!( + query_control_device_table_resource(&underflowing_product, 1), + Err(ControlResourceAccessError::UnsupportedFloatRange { + factory_tag: Some(19), + field: "weighted value" + }) + )); + + let underflowing_ratio = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 19, + 19, + f32::MIN_POSITIVE.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 19, + 19, + f32::MAX.to_bits(), + Some(0.0_f32.to_bits()), + None, + ), + ])); + assert!(matches!( + query_control_device_table_resource(&underflowing_ratio, 1), + Err(ControlResourceAccessError::UnsupportedFloatRange { + factory_tag: None, + field: "ratio" + }) + )); + + let overflowing_sum = + synthetic_control_device_table(MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device(19, 19, f32::MAX.to_bits(), None, None), + synthetic_effective_control_device(19, 19, f32::MAX.to_bits(), None, None), + ])); + assert!(matches!( + query_control_device_table_resource(&overflowing_sum, 2), + Err(ControlResourceAccessError::UnsupportedFloatRange { + factory_tag: Some(19), + field: "sum" + }) + )); + } + + #[test] + fn control_resource_api_checks_live_world_handle_before_loaded_state() { + let mut engine = create(EngineServices::default()).expect("engine"); + let handle = construct_object( + &mut engine.world, + ObjectDraft { + original_id: Some(OriginalObjectId(4)), + }, + ) + .expect("live world object"); + assert_eq!( + query_loaded_mission_control_resource(&engine, handle, 1), + Err(ControlResourceAccessError::MissionUnavailable) + ); + assert_eq!( + query_loaded_mission_control_device_selector(&engine, handle, 0, 0xe00), + Err(ControlResourceAccessError::MissionUnavailable) + ); + + let stale = ObjectHandle { + generation: handle.generation.wrapping_add(1), + slot: handle.slot, + }; + assert_eq!( + query_loaded_mission_control_resource(&engine, stale, 1), + Err(ControlResourceAccessError::World( + fparkan_world::WorldError::StaleHandle + )) + ); + assert_eq!( + query_loaded_mission_control_device_selector(&engine, stale, 0, 0xe00), + Err(ControlResourceAccessError::World( + fparkan_world::WorldError::StaleHandle + )) + ); + assert_eq!( + set_loaded_mission_control_multiplier(&mut engine, stale, 2.0), + Err(ControlResourceAccessError::World( + fparkan_world::WorldError::StaleHandle + )) + ); + + fparkan_world::request_delete(&mut engine.world, handle).expect("delete object"); + assert_eq!( + query_loaded_mission_control_resource(&engine, handle, 1), + Err(ControlResourceAccessError::World( + fparkan_world::WorldError::StaleHandle + )) + ); + assert_eq!( + query_loaded_mission_control_device_selector(&engine, handle, 0, 0xe00), + Err(ControlResourceAccessError::World( + fparkan_world::WorldError::StaleHandle + )) + ); + } + + #[test] + fn control_factory_multiplier_defaults_preserve_unknown_word_50() { + for factory_tag in [9, 15, 25] { + assert_eq!( + native_control_factory_defaults(factory_tag), + (Some(0x3f80_0000), None) + ); + } + } + #[test] fn native_basement_mode_uses_unit_header_gate_not_builds_path() { assert_eq!( @@ -2090,6 +4029,788 @@ 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_node_provider_projection_keeps_native_rows_separate_from_mesh_keys() { + let controls = vec![ + Some(control_with_synthetic_node_data( + 0, + vec![synthetic_control_record(26, 1, 2.0_f32.to_bits(), None)], + 2, + )), + Some(control_with_synthetic_node_data( + 1, + vec![synthetic_control_record(26, 0, 3.0_f32.to_bits(), None)], + 7, + )), + Some(control_with_synthetic_node_data( + 2, + vec![synthetic_control_record(26, 5, 4.0_f32.to_bits(), None)], + 3, + )), + Some(control_with_synthetic_node_data( + 3, + vec![synthetic_control_record(26, 0, 5.0_f32.to_bits(), None)], + 2, + )), + Some(control_with_synthetic_node_data( + 4, + vec![synthetic_control_record(26, 0, 6.0_f32.to_bits(), None)], + 6, + )), + Some(control_with_synthetic_node_data( + 5, + vec![synthetic_control_record(26, 1, 6.0_f32.to_bits(), None)], + 5, + )), + Some(control_with_synthetic_node_data( + 6, + vec![synthetic_control_record(26, 1, 7.0_f32.to_bits(), None)], + 4, + )), + ]; + let (status, node_provider_status) = synthetic_control_projection( + &[ + None, + Some(PROTOTYPE_TYPE_INTO), + Some(PROTOTYPE_TYPE_EXTO), + Some(PROTOTYPE_TYPE_EXTO), + Some(PROTOTYPE_TYPE_EXTO), + None, + None, + ], + &[1, 1, 1, 1, 1, 0, 1], + &[4, 0, 1, 0, 0, 1, 0], + &[0, -1, 2, 0, -1, 0, 0], + &[1, 1, 4, 2, 1, 1, 1], + controls, + ); + let table = synthetic_control_device_table_with_providers(status, node_provider_status); + let MissionControlNodeProviderTableStatus::Available(rows) = &table.node_provider_status + else { + panic!( + "synthetic provider rows should be available: {:?}", + table.node_provider_status + ); + }; + + // Root contributes all rows, INTO contributes none, and each mounted + // EXTO skips its local row zero. The EXTO with C4=FFFFFFFF contributes + // no provider rows. The kind-zero component and its child are absent + // from the native active-component walk. + assert_eq!(rows.len(), 5); + assert_eq!( + rows.iter() + .map(|row| (row.unit_component_index, row.source_row_index)) + .collect::<Vec<_>>(), + [(0, 0), (0, 1), (2, 1), (2, 2), (3, 1)] + ); + assert!(rows + .iter() + .all(|row| row.initial_type1_value_bits == 1.0_f32.to_bits())); + + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + panic!("synthetic devices should be available: {:?}", table.status); + }; + assert_eq!( + devices.iter().map(|device| device.key).collect::<Vec<_>>(), + [1, u32::MAX, 5, 0, u32::MAX] + ); + assert_eq!( + query_control_device_table_selector(&table, 0, 0xe00) + .expect("root tag-26 selector") + .map(f32::to_bits), + Some(2.0_f32.to_bits()) + ); + // Device keys still use the confirmed MSH mapping. The provider list + // is the real concatenated NDP row count, so no model-node padding or + // second key remap hides an out-of-range key. + assert_eq!( + query_control_device_table_selector(&table, 2, 0xe00), + Err(ControlResourceAccessError::NodeProviderKeyOutOfRange { + key: 5, + row_count: 5 + }) + ); + assert_eq!( + query_control_device_table_selector(&table, 3, 0xe00), + Ok(Some(5.0)) + ); + assert_eq!( + query_control_device_table_selector(&table, 4, 0xe00), + Err(ControlResourceAccessError::NodeProviderKeyOutOfRange { + key: u32::MAX, + row_count: 5 + }) + ); + } + + #[test] + fn missing_node_provider_does_not_hide_aggregate_queries() { + let controls = vec![Some(synthetic_component_control( + 0, + vec![ + synthetic_control_record(26, 0, 2.0_f32.to_bits(), None), + synthetic_control_record(19, 1, 4.0_f32.to_bits(), None), + ], + PreparedControlEffectSection::Parsed(Vec::new()), + ))]; + let (status, node_provider_status) = + synthetic_control_projection(&[None], &[1], &[0], &[0], &[1], controls); + let table = synthetic_control_device_table_with_providers(status, node_provider_status); + assert!(matches!( + &table.node_provider_status, + MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::MissingNodeData { + unit_component_index: 0, + .. + } + ) + )); + assert_eq!( + query_control_device_table_resource(&table, 1) + .expect("aggregate selector 1 remains available") + .map(f32::to_bits), + Some(1.0_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_resource(&table, 2) + .expect("aggregate selector 2 remains available") + .map(f32::to_bits), + Some(4.0_f32.to_bits()) + ); + assert!(matches!( + query_control_device_table_selector(&table, 0, 0xe00), + Err(ControlResourceAccessError::NodeProviderTableUnavailable(_)) + )); + } + + #[test] + fn mounted_exto_requires_a_row_before_native_skip_first() { + let controls = vec![ + Some(control_with_synthetic_node_data( + 0, + vec![synthetic_control_record(26, 0, 1.0_f32.to_bits(), None)], + 0, + )), + Some(control_with_synthetic_node_data(1, Vec::new(), 0)), + ]; + let (_, node_provider_status) = synthetic_control_projection( + &[None, Some(PROTOTYPE_TYPE_EXTO)], + &[1, 1], + &[1, 0], + &[0, 0], + &[2, 2], + controls, + ); + assert_eq!( + node_provider_status, + MissionControlNodeProviderTableStatus::Unavailable( + MissionControlNodeProviderTableUnavailableReason::MissingExToRootRow { + unit_component_index: 1, + } + ) + ); + + // An empty root table is legal, and a mounted EXTO with exactly its + // root row skips that row and contributes an empty range. + let controls = vec![ + Some(control_with_synthetic_node_data( + 0, + vec![synthetic_control_record(26, 0, 1.0_f32.to_bits(), None)], + 0, + )), + Some(control_with_synthetic_node_data(1, Vec::new(), 1)), + ]; + let (_, node_provider_status) = synthetic_control_projection( + &[None, Some(PROTOTYPE_TYPE_EXTO)], + &[1, 1], + &[1, 0], + &[0, 0], + &[2, 2], + controls, + ); + assert_eq!( + node_provider_status, + MissionControlNodeProviderTableStatus::Available(Vec::new()) + ); + } + + #[test] + fn indexed_node_provider_query_handles_current_tags_and_factory_scope() { + let available = MissionControlNodeProviderTableStatus::Available(vec![ + synthetic_node_provider_row(0, 0, 1.0_f32.to_bits()), + synthetic_node_provider_row(0, 1, 1.0_f32.to_bits()), + synthetic_node_provider_row(0, 2, 1.0_f32.to_bits()), + ]); + let table = synthetic_control_device_table_with_providers( + MissionControlDeviceTableStatus::Available(vec![ + synthetic_effective_control_device( + 26, + 26, + 2.0_f32.to_bits(), + Some(3.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 26, + 19, + 4.0_f32.to_bits(), + Some(5.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 26, + 5, + 1.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + None, + ), + synthetic_effective_control_device( + 9, + 26, + 1.0_f32.to_bits(), + Some(1.0_f32.to_bits()), + Some(0.5_f32.to_bits()), + ), + ]), + available, + ); + assert_eq!( + query_control_device_table_selector(&table, 0, 0xe00) + .expect("tag-26 query") + .map(f32::to_bits), + Some(6.0_f32.to_bits()) + ); + assert_eq!( + query_control_device_table_selector(&table, 1, 0xe00), + Ok(None) + ); + for tag in [5, 10, 21] { + let mut unsupported = table.clone(); + let MissionControlDeviceTableStatus::Available(entries) = &mut unsupported.status + else { + unreachable!(); + }; + entries[1].descriptor.tag = tag; + assert_eq!( + query_control_device_table_selector(&unsupported, 1, 0xe00), + Err(ControlResourceAccessError::UnsupportedCurrentTag { tag }) + ); + } + assert_eq!( + query_control_device_table_selector(&table, 3, 0xe00), + Err(ControlResourceAccessError::UnsupportedFactoryTag { factory_tag: 9 }) + ); + assert_eq!( + query_control_device_table_selector(&table, 0, 1), + Err(ControlResourceAccessError::UnsupportedSelector(1)) + ); + assert_eq!( + query_control_device_table_selector(&table, 8, 0xe00), + Err(ControlResourceAccessError::DeviceIndexOutOfRange { + device_index: 8, + device_count: 4 + }) + ); + } + + #[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 control_handle = tables[0].handle; + assert_eq!( + query_loaded_mission_control_resource(&engine, control_handle, 1) + .expect("licensed selector 1") + .map(f32::to_bits), + Some(1.0_f32.to_bits()) + ); + assert_eq!( + query_loaded_mission_control_resource(&engine, control_handle, 2) + .expect("licensed selector 2") + .map(f32::to_bits), + Some(16.0_f32.to_bits()) + ); + 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.descriptor.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)) + ); + for index in [2, 3, 9] { + assert_eq!(entries[index].multiplier_bits, Some(0x3f80_0000)); + } + assert_eq!(entries[2].class9_selector0_scale_bits, Some(0x3f80_0000)); + 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!( + query_loaded_mission_control_device_selector(&engine, control_handle, 8, 0xe00) + .expect("licensed tag-26 selector") + .map(f32::to_bits), + Some(1.0_f32.to_bits()) + ); + let prepared_controls = &engine + .loaded + .as_ref() + .expect("loaded mission state") + .mission_assets + .object_component_controls[0]; + let root_node_data = prepared_controls[0] + .as_ref() + .and_then(|control| control.node_data.as_ref()) + .expect("root NDP sidecar"); + let ext_node_data = prepared_controls[5] + .as_ref() + .and_then(|control| control.node_data.as_ref()) + .expect("EXTO NDP sidecar"); + let provider_rows = match &tables[0].node_provider_status { + MissionControlNodeProviderTableStatus::Available(rows) => rows, + MissionControlNodeProviderTableStatus::Unavailable(reason) => { + panic!("licensed provider table should be available: {reason:?}"); + } + }; + assert_eq!(root_node_data.rows.len(), 22); + assert_eq!(ext_node_data.rows.len(), 4); + assert_eq!(provider_rows.len(), 25); + let expected_provider_sources = (0..root_node_data.rows.len()) + .map(|row| (0, row)) + .chain((1..ext_node_data.rows.len()).map(|row| (5, row))) + .collect::<Vec<_>>(); + assert_eq!( + provider_rows + .iter() + .map(|row| (row.unit_component_index, row.source_row_index)) + .collect::<Vec<_>>(), + expected_provider_sources + ); + assert!(provider_rows + .iter() + .all(|row| row.initial_type1_value_bits == 1.0_f32.to_bits())); + 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); + set_loaded_mission_control_multiplier(&mut engine, control_handle, 2.5) + .expect("licensed setter 1"); + assert_eq!( + query_loaded_mission_control_resource(&engine, control_handle, 1) + .expect("updated licensed selector 1") + .map(f32::to_bits), + Some(2.5_f32.to_bits()) + ); + } + + #[test] fn script_varset_prefers_bundle_adjacent_file_then_uses_shared_fallback() { let mut personal_files = MemoryVfs::default(); personal_files.insert( 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 d2e5bb9..5e89934 100644 --- a/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs +++ b/crates/fparkan-runtime/tests/behavior_distributor_tick_native_oracle.cs @@ -5,10 +5,9 @@ using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; -// Calls the original x86 Behavior.dll distributor Add, normalize, indexed resource -// property, 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; @@ -17,6 +16,7 @@ internal static class BehaviorDistributorTickOracle { 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; @@ -25,8 +25,26 @@ internal static class BehaviorDistributorTickOracle { private const int OreInitializationRva = 0x08790; private const int GetSystemArealMapIatRva = 0x59008; private const int ControlResourceDeviceGetterRva = 0x21d00; + private const int ControlResourceAggregateQueryRva = 0x2b410; + private const int ControlResourceAggregateSetRva = 0x2ba30; private const int ControlProviderInterfaceVtableRva = 0x3d254; private const int ControlProviderType1GetterRva = 0x0dc40; + private const int ControlProviderRowConstructorRva = 0x0b240; + private const int ControlProviderAmountUpdateRva = 0x10f30; + 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; @@ -57,6 +75,15 @@ internal static class BehaviorDistributorTickOracle { [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); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int NativeControlAggregateQuery(IntPtr self, uint selector, IntPtr result); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int NativeControlAggregateSet(IntPtr self, uint selector, IntPtr value); + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeControlProviderAmountUpdate(IntPtr self, int nodeIndex, float delta, uint animationFlag); + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void NativeControlProviderRowConstructor(IntPtr self, IntPtr sourceNdpRow); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void NativeControlNodeUpdateCallback(IntPtr self, uint nodeIndex, uint selector, uint valueBits); private static readonly AreaMapGetter AreaMapGetterCallback = GetAreaMap; private static readonly VectorInsert VectorInsertCallback = InsertVector; @@ -69,6 +96,13 @@ 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 readonly NativeControlNodeUpdateCallback ControlNodeUpdateCallback = OnControlNodeUpdate; + private static ArealMapFixture activeArealMap; + private static int controlNodeUpdateCount; + private static uint controlNodeUpdateIndex; + private static uint controlNodeUpdateSelector; + private static uint controlNodeUpdateValueBits; private static IntPtr fastcallThunks; private static IntPtr queryObjectThunk; private static IntPtr getInterfacePropertyThunk; @@ -183,7 +217,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); @@ -304,6 +408,12 @@ internal static class BehaviorDistributorTickOracle { active.Trace.Add("iface-set:" + item.Key.ToString("x8") + ":" + iid.ToString("x") + ":" + selector.ToString("x") + ":" + number.ToString("R", System.Globalization.CultureInfo.InvariantCulture)); return 1; } + private static void OnControlNodeUpdate(IntPtr self, uint nodeIndex, uint selector, uint valueBits) { + controlNodeUpdateCount++; + controlNodeUpdateIndex = nodeIndex; + controlNodeUpdateSelector = selector; + controlNodeUpdateValueBits = valueBits; + } private static float SequenceValue(float[] sequence, int index, float fallback) { if (sequence == null || sequence.Length == 0) return fallback; return sequence[Math.Min(index, sequence.Length - 1)]; @@ -311,11 +421,40 @@ 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"); @@ -325,6 +464,11 @@ internal static class BehaviorDistributorTickOracle { 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()); 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; @@ -346,8 +490,8 @@ internal static class BehaviorDistributorTickOracle { throw new InvalidOperationException("VirtualProtect(GetSystemArealMap IAT) failed: " + Marshal.GetLastWin32Error()); protectionChanged = true; Marshal.WriteIntPtr(import, Marshal.GetFunctionPointerForDelegate(AreaMapGetterCallback)); - RunSmokeCases(module, controlModule); - 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) { @@ -365,15 +509,18 @@ internal static class BehaviorDistributorTickOracle { GC.KeepAlive(ObjectQueryCallback); GC.KeepAlive(ObjectRateCallback); GC.KeepAlive(ObjectGetPropertyCallback); GC.KeepAlive(ObjectSetPropertyCallback); GC.KeepAlive(InterfaceGetCallback); GC.KeepAlive(InterfaceSetCallback); + GC.KeepAlive(ControlNodeUpdateCallback); + 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, IntPtr controlModule) { + 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 { @@ -381,7 +528,11 @@ 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); + RunNativeControlProviderLiveStateCase(controlModule); + RunNativeControlAggregateResourceCases(controlModule); } finally { SetX87ControlWord(savedControlWord); Console.WriteLine("x87-control-word-restored=0x" + CurrentX87ControlWord().ToString("x4")); @@ -461,6 +612,356 @@ internal static class BehaviorDistributorTickOracle { } } + private static void RunNativeControlProviderLiveStateCase(IntPtr module) { + NativeControlProviderRowConstructor constructProviderRow = (NativeControlProviderRowConstructor) + Marshal.GetDelegateForFunctionPointer(IntPtr.Add(module, ControlProviderRowConstructorRva), + typeof(NativeControlProviderRowConstructor)); + NativeControlProviderAmountUpdate update = (NativeControlProviderAmountUpdate) + Marshal.GetDelegateForFunctionPointer(IntPtr.Add(module, ControlProviderAmountUpdateRva), + typeof(NativeControlProviderAmountUpdate)); + NativeControlResourceDeviceGet getter = (NativeControlResourceDeviceGet) + Marshal.GetDelegateForFunctionPointer(IntPtr.Add(module, ControlResourceDeviceGetterRva), + typeof(NativeControlResourceDeviceGet)); + + using (Fixture fixture = NewFixture()) { + IntPtr owner = fixture.Allocate(0x664); + IntPtr rows = fixture.Allocate(2 * 0x2c); + IntPtr ndpDescriptor = fixture.Allocate(0x4c); + IntPtr initializedRow = fixture.Allocate(0x2c); + constructProviderRow(initializedRow, ndpDescriptor); + uint[] initialWords = new uint[11]; + for (int i = 0; i < initialWords.Length; i++) + initialWords[i] = unchecked((uint)Marshal.ReadInt32(initializedRow, i * 4)); + uint[] expectedInitialWords = { + unchecked((uint)ndpDescriptor.ToInt32()), 0xffffffff, 0x3f800000, + 0x3f800000, 0, 0x3f800000, 0, 1, 0, 0xffffffff, 0 + }; + Expect(String.Join(",", initialWords) == String.Join(",", expectedInitialWords), + "Control native NDP provider row constructor initializes all eleven words and type-1 current to 1.0"); + + IntPtr providerInterface = IntPtr.Add(owner, 4); + IntPtr providerVtable = IntPtr.Add(module, ControlProviderInterfaceVtableRva); + IntPtr callbackObject = fixture.Allocate(IntPtr.Size); + IntPtr callbackVtable = fixture.Allocate(0x38); + IntPtr device = fixture.Allocate(0x94); + IntPtr row = IntPtr.Add(rows, 0x2c); // Keep the root row untouched. + + Marshal.WriteIntPtr(providerInterface, providerVtable); + Marshal.WriteIntPtr(owner, 0x55c, rows); + Marshal.WriteInt32(owner, 0x548, FloatBits(1.0f)); + Marshal.WriteInt32(owner, 0x590, FloatBits(40.0f)); + Marshal.WriteInt32(owner, 0x660, FloatBits(1.0f)); + + Marshal.WriteIntPtr(row, 0x00, ndpDescriptor); + Marshal.WriteInt32(ndpDescriptor, 0x04, FloatBits(100.0f)); // Native selector 3 maximum. + Marshal.WriteInt32(row, 0x04, -1); // Root row has no parent node. + Marshal.WriteInt32(row, 0x08, FloatBits(40.0f)); + Marshal.WriteInt32(row, 0x14, FloatBits(0.4f)); + + Marshal.WriteIntPtr(callbackObject, 0, callbackVtable); + Marshal.WriteIntPtr(callbackVtable, 0x34, Marshal.GetFunctionPointerForDelegate(ControlNodeUpdateCallback)); + Marshal.WriteIntPtr(owner, 0x20, callbackObject); + + Marshal.WriteInt32(device, 0x04, 1); + Marshal.WriteInt32(device, 0x4c, FloatBits(3.0f)); + Marshal.WriteInt32(device, 0x54, FloatBits(2.0f)); + Marshal.WriteIntPtr(device, 0x44, providerInterface); + + controlNodeUpdateCount = 0; + update(owner, 1, 20.0f, 0); + Expect(ReadFloat(row, 0x08) == 60.0f && ReadFloat(row, 0x14) == 0.6f && + ReadFloat(owner, 0x590) == 60.0f, + "Control native amount update clamps the node amount, updates the normalized provider value, and keeps the aggregate amount in sync"); + Expect(controlNodeUpdateCount == 1 && controlNodeUpdateIndex == 1 && + controlNodeUpdateSelector == 0x200 && controlNodeUpdateValueBits == unchecked((uint)FloatBits(0.6f)), + "Control native amount update publishes the normalized provider state through the existing node callback"); + Expect(unchecked((uint)FloatBits(getter(device, 0x300))) == + unchecked((uint)FloatBits(0.6f * 2.0f * 3.0f)), + "Control tag-26 selector 0x300 observes the updated NDP provider value through the native provider interface"); + + update(owner, 1, -10.0f, 0); + Expect(ReadFloat(row, 0x08) == 50.0f && ReadFloat(row, 0x14) == 0.5f && + unchecked((uint)FloatBits(getter(device, 0x300))) == 0x40400000, + "Control native amount update is reversible and tag-26 selector 0x300 tracks the changed provider row"); + + update(owner, 1, 1000.0f, 0); + Expect(ReadFloat(row, 0x08) == 100.0f && ReadFloat(row, 0x14) == 1.0f && + unchecked((uint)FloatBits(getter(device, 0x300))) == 0x40c00000, + "Control native amount update clamps the provider value at its native selector-3 maximum"); + + update(owner, 1, -200.0f, 0); + Expect(ReadFloat(row, 0x08) == 0.0f && ReadFloat(row, 0x14) == 0.0f && + (unchecked((uint)Marshal.ReadInt32(row, 0x28)) & 0x10) != 0 && + unchecked((uint)FloatBits(getter(device, 0x300))) == 0, + "Control native depletion sets the exhausted flag and selector 0x300 returns the live zero provider value"); + Expect(controlNodeUpdateCount == 4 && controlNodeUpdateIndex == 1 && + controlNodeUpdateSelector == 0x200 && controlNodeUpdateValueBits == 0, + "Control native node callback receives every normalized provider update through exhaustion"); + + Console.WriteLine("native-control-live-ndp-provider: controlHash=" + ExpectedControlSha256 + + " rowCtorRva=0xb240 initialType1=1 rowStride=0x2c node=1 max=100 amount=0..100 tag26Selector300=live"); + GC.KeepAlive(constructProviderRow); + GC.KeepAlive(update); + GC.KeepAlive(getter); + } + } + + private static void RunNativeControlAggregateResourceCases(IntPtr module) { + NativeControlAggregateQuery query = (NativeControlAggregateQuery)Marshal.GetDelegateForFunctionPointer( + WriteStdCallToFastcallThunk(0x1c0, IntPtr.Add(module, ControlResourceAggregateQueryRva)), + typeof(NativeControlAggregateQuery)); + NativeControlAggregateSet set = (NativeControlAggregateSet)Marshal.GetDelegateForFunctionPointer( + WriteStdCallToFastcallThunk(0x200, IntPtr.Add(module, ControlResourceAggregateSetRva)), + typeof(NativeControlAggregateSet)); + using (Fixture fixture = NewFixture()) { + IntPtr owner = fixture.Allocate(0x5b8); + IntPtr devices = fixture.Allocate(4 * IntPtr.Size); + IntPtr result = fixture.Allocate(sizeof(uint)); + IntPtr value = fixture.Allocate(sizeof(uint)); + + IntPtr tag9Current19 = AddNativeControlAggregateDevice(fixture, module, 9, 19, + 0x120, ControlResourceTag9ConstructorRva, ControlResourceTag9VtableRva, 2.0f, 3.0f, 0.5f); + IntPtr tag19Current9 = AddNativeControlAggregateDevice(fixture, module, 19, 9, + 0x94, ControlResourceDeviceConstructorRva, ControlResourceTag19VtableRva, 100.0f, 77.0f, 1.0f); + IntPtr tag15Current19 = AddNativeControlAggregateDevice(fixture, module, 15, 19, + 0x98, ControlResourceTag15ConstructorRva, ControlResourceTag15VtableRva, 4.0f, 0.5f, 1.0f); + IntPtr tag19Current19 = AddNativeControlAggregateDevice(fixture, module, 19, 19, + 0x94, ControlResourceDeviceConstructorRva, ControlResourceTag19VtableRva, 1.0f, 4.0f, 1.0f); + + Marshal.WriteIntPtr(devices, 0, tag9Current19); + Marshal.WriteIntPtr(devices, IntPtr.Size, tag19Current9); + Marshal.WriteIntPtr(devices, 2 * IntPtr.Size, tag15Current19); + Marshal.WriteIntPtr(devices, 3 * IntPtr.Size, tag19Current19); + Marshal.WriteIntPtr(owner, 0x5b0, devices); + Marshal.WriteInt32(owner, 0x5b4, 4); + + Expect(NativeBoolean(query(owner, 1, result)) == 1 && unchecked((uint)Marshal.ReadInt32(result)) == 0x40000000, + "Control aggregate selector 1 includes descriptor-19 entries and dispatches their original factory vtables"); + Expect(NativeBoolean(query(owner, 2, result)) == 1 && unchecked((uint)Marshal.ReadInt32(result)) == 0x40c00000, + "Control aggregate selector 2 sums base values for descriptor-19 entries"); + + Marshal.WriteInt32(value, 0, FloatBits(7.5f)); + Expect(NativeBoolean(set(owner, 1, value)) == 1, + "Control aggregate setter selector 1 reports success for its recognized selector"); + Expect(unchecked((uint)Marshal.ReadInt32(tag9Current19, 0x4c)) == 0x40f00000 && + unchecked((uint)Marshal.ReadInt32(tag15Current19, 0x4c)) == 0x40f00000 && + unchecked((uint)Marshal.ReadInt32(tag19Current19, 0x4c)) == 0x40f00000 && + unchecked((uint)Marshal.ReadInt32(tag19Current9, 0x4c)) == FloatBits(77.0f), + "Control setter selector 1 updates matching positive devices regardless of factory class, and skips current descriptor tag 9"); + Expect(NativeBoolean(query(owner, 1, result)) == 1 && unchecked((uint)Marshal.ReadInt32(result)) == 0x410c0000, + "Control aggregate selector 1 reflects updated per-device multipliers"); + + // Factory tag 9's virtual selector 0 includes +0x11c. A zero base keeps + // this selected descriptor out of the native setter's positive-only write. + Marshal.WriteInt32(tag9Current19, 0x11c, FloatBits(0.0f)); + Marshal.WriteInt32(value, 0, FloatBits(9.0f)); + Expect(NativeBoolean(set(owner, 1, value)) == 1 && + unchecked((uint)Marshal.ReadInt32(tag9Current19, 0x4c)) == 0x40f00000, + "Control setter selector 1 leaves the multiplier unchanged when the original factory getter returns zero"); + + Marshal.WriteInt32(owner, 0x5b4, 0); + Marshal.WriteInt32(result, 0, unchecked((int)0x3f123456)); + Expect(NativeBoolean(query(owner, 1, result)) == 0 && unchecked((uint)Marshal.ReadInt32(result)) == 0x3f123456, + "Control aggregate selector 1 reports no result and leaves output untouched when the table is empty"); + Expect(NativeBoolean(query(owner, 2, result)) == 0 && unchecked((uint)Marshal.ReadInt32(result)) == 0x3f123456, + "Control aggregate selector 2 reports no result and leaves output untouched when the table is empty"); + Expect(NativeBoolean(set(owner, 1, value)) == 1, + "Control aggregate setter selector 1 reports success when the table is empty"); + + // The aggregate accumulators spill back to float precision every row. + Marshal.WriteInt32(owner, 0x5b4, 3); + Marshal.WriteInt32(Marshal.ReadIntPtr(tag19Current9, 0x48), 0, 19); + Marshal.WriteInt32(tag9Current19, 0x54, FloatBits(1.0f)); + Marshal.WriteInt32(tag9Current19, 0x4c, FloatBits(16777216.0f)); + Marshal.WriteInt32(tag9Current19, 0x11c, FloatBits(1.0f)); + Marshal.WriteInt32(tag19Current9, 0x54, FloatBits(1.0f)); + Marshal.WriteInt32(tag19Current9, 0x4c, FloatBits(1.0f)); + Marshal.WriteInt32(tag15Current19, 0x54, FloatBits(1.0f)); + Marshal.WriteInt32(tag15Current19, 0x4c, FloatBits(-16777216.0f)); + Expect(NativeBoolean(query(owner, 1, result)) == 1 && Marshal.ReadInt32(result) == 0, + "Control aggregate selector 1 rounds each accumulator to float after each row"); + + Marshal.WriteInt32(owner, 0x5b4, 1); + Marshal.WriteInt32(tag9Current19, 0x54, FloatBits(-1.0f)); + Marshal.WriteInt32(tag9Current19, 0x4c, FloatBits(2.0f)); + Marshal.WriteInt32(result, 0, unchecked((int)0x3f123456)); + Expect(NativeBoolean(query(owner, 1, result)) == 1 && Marshal.ReadInt32(result) == FloatBits(1.0f), + "Control aggregate selector 1 returns one immediately when a selected base value is negative"); + Expect(NativeBoolean(query(owner, 2, result)) == 1 && Marshal.ReadInt32(result) == FloatBits(-1.0f), + "Control aggregate selector 2 returns a selected negative base value immediately"); + + Console.WriteLine("native-control-global-resource-table: controlHash=" + ExpectedControlSha256 + + " getterRva=0x2b410 setterRva=0x2ba30 selectors=1,2 descriptorFilter=19" + + " factory19-descriptor9=excluded factory9-descriptor19-scale=0.5" + + " factory15-descriptor19=included initial=[2,6] multiplier=7.5 post-set-getter1=8.75" + + " empty=[false/unchanged,false/unchanged,set=true] rounding=0 negative=[1,-1]"); + GC.KeepAlive(query); + GC.KeepAlive(set); + } + } + + private static IntPtr AddNativeControlAggregateDevice(Fixture fixture, IntPtr module, + int factoryTag, int currentDescriptorTag, int objectSize, int constructorRva, int vtableRva, + float coefficient, float multiplier, float class9Scale) { + IntPtr device = fixture.Allocate(objectSize); + NativeControlResourceDeviceConstructor construct = (NativeControlResourceDeviceConstructor)Marshal.GetDelegateForFunctionPointer( + IntPtr.Add(module, constructorRva), typeof(NativeControlResourceDeviceConstructor)); + IntPtr expectedVtable = IntPtr.Add(module, vtableRva); + Expect(construct(device) == device && Marshal.ReadIntPtr(device) == expectedVtable, + "native factory tag " + factoryTag + " constructor installs its original class vtable"); + IntPtr descriptor = fixture.Allocate(sizeof(uint)); + Marshal.WriteInt32(descriptor, 0, currentDescriptorTag); + Marshal.WriteIntPtr(device, 0x48, descriptor); + Marshal.WriteInt32(device, 0x4c, FloatBits(multiplier)); + Marshal.WriteInt32(device, 0x54, FloatBits(coefficient)); + if (factoryTag == 9) Marshal.WriteInt32(device, 0x11c, FloatBits(class9Scale)); + return device; + } + + 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, @@ -966,6 +1467,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()); @@ -995,6 +1505,7 @@ internal static class BehaviorDistributorTickOracle { foreach (byte value in digest) hex.Append(value.ToString("x2")); return hex.ToString(); } } + private static int NativeBoolean(int eax) { return eax & 0xff; } 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>(); diff --git a/docs/appendices/knowledge-boundaries.md b/docs/appendices/knowledge-boundaries.md index 5791836..fa1af94 100644 --- a/docs/appendices/knowledge-boundaries.md +++ b/docs/appendices/knowledge-boundaries.md @@ -253,12 +253,26 @@ solver. То же относится к редким MSH auxiliary streams и ч разрешается в индекс устройства и сдвигает локальный индекс только когда parent ID не равен `-1` и IID `0x202` доступен; иначе используется raw локальный индекс. В этом потоке нулевой raw kind пропускает узел вместе с потомками, поэтому общий -assigned ID нельзя получать из preorder-позиции записи. Это закрывает только -передачу полей в native loader-е; Rust ещё должен строить такую же проекцию. - -Control loader также различает добавление устройств и применение одной записи -к уже выбранному устройству. Для tag `19` найденные поля и ветви описаны в -главе, но эффективное слияние пока не реализовано. В Rust parser-е +assigned ID нельзя получать из preorder-позиции записи. Полная загрузка миссии +в FParkan строит упорядоченную базовую проекцию таблицы +Control для объектов с Unit DAT: корневые записи добавляются, `EXTO` добавляет +строки с ключами глобальной MSH-иерархии, а `INTO` обновляет первую строку +выбранного индекса либо добавляет список при sentinel `-1`. Проверка полной +загрузки на оригинальном `min_l_n1.dat` получает 11 строк: 10 корневых записей, +четыре замены `INTO` и одну добавленную строку дочернего `EXTO`. Ветви C4/C8 и +добавление `EXTO` подтверждены машинными инструкциями оригинальных модулей, но +полный результат FParkan не сравнивался с таблицей из запущенной игры. Тест на +искусственном дереве также проверяет ID после пропущенного поддерева с raw kind +`0`. Проекция хранит общие поля и происхождение записей; она не исполняет все +обработчики Control. + +Control различает добавление устройства и применение одной записи к уже +выбранному устройству. Исполнение оригинальной 32-битной DLL на искусственных +данных подтверждает общую ветвь переопределения для получателей tags `9`, `15`, +`19` и `25`: остаются прежними слот, ключ, ID компонента, множитель и класс, +а коэффициенты копируются из новой записи. Искусственный tag `9`-объект создан +без FX-хоста (`+0x3c == 0`), а запись не содержит строк таблицы map; поэтому +работа FX-хоста здесь не проверялась. В Rust parser-е `Unsupported { typed_record_index: Some(_) }` означает, что разбор остановился внутри списка typed records; без индекса остановки unsupported может относиться к effect-tail после уже разобранного списка. Эти случаи нужно различать. @@ -268,28 +282,73 @@ Control loader также различает добавление устройс подготовленного CTL должно оставаться явным unavailable result, а не трактоваться как пустой список. -Глобальные Control selectors `1/2` и setter selector `1` по tag `19` описаны -там же только по статическому разбору `Control.dll`; execution oracle для этих -глобальных вызовов нет. Остаются открытыми проверка effective merge на живых -объектах, полный охват registry types и host factory paths, а также runtime -жизненный цикл множителя `+0x4c`. +Global Control getters `1/2` и setter `1` проверены в native x86 oracle для +искусственных factory/descriptor pairs `9`, `15` и `19`, а в Rust реализованы +для текущего descriptor tag `19` с factory tags `9`, `15`, `19` и `25`. Для tag +`25` общий getter slot `+0x10` подтверждён чтением vtable, но агрегатный вызов +этого класса native oracle не запускал. Rust API обращается к строкам проекции +конкретного живого `ObjectHandle`; это запрос к уже загруженной таблице, а не +исполнение Control или Behavior tick. Setter `17`, остальные factory tags и +внешние эффекты их методов не включены. Для tag `9` selector `0` использует +отдельный scale `+0x11c`; остальные поддержанные классы используют коэффициент +напрямую. Пустой набор и нулевой знаменатель остаются native no-result, а +неподдержанный factory tag и расчётный диапазон произведения/отношения за +нормальным диапазоном `f32` становятся явной ошибкой Rust. + +Проекция полного mission load недоступна для неполного списка typed records, +Control resource у non-geometric prototype и некоторых неразрешённых EXTO node +mappings; preview и объекты без Unit DAT root её не строят. Её соответствие +полной загрузке игры для всех типов компонентов и factory paths ещё не +установлено. Setter `1` меняет multiplier `+0x4c` выбранных строк, но расписание +и lifetime этих объектов после конструктора и в отложенной загрузке остаются +непроверенными. Для `Control.dll` (SHA-256 `bcb86318089028548a178b03a21accdce15df0cc85ddd91674cf3c2f55012c6e`) tag-26 getter selector `0x300` восстановлен локально: RVA `0x21d00` читает provider по ключу `fixed_words[1]`, затем умножает provider, CTLD coefficient `fixed_words[11]` -и поле объекта `+0x4c` именно в таком порядке. Rust helper принимает текущие -provider и multiplier извне; он не восстанавливает их жизненный цикл или весь -Control tick. В x86 oracle при x87 control word `0x007f` проверены порядок, -signed zero, отрицательные входы, расширенный промежуточный диапазон и -subnormal return. Helper ограничивает себя normal-or-zero значениями, поэтому -явно отклоняет промежуточные случаи, где результат выходит за поддерживаемый -диапазон f32, даже если позднее умножение вернуло бы его в диапазон. Это -ограничение поддержки Rust helper-а, а не неверный native-результат. +и поле объекта `+0x4c` именно в таком порядке. Provider getter RVA `0xdc40` +читает type-1 поле `+0x14` строки, которую `FUN_10008b10` (RVA `0x8b10`) +добавляет из NDP в таблицу с шагом `0x2c`; `FUN_1000b240` (RVA `0xb240`) +инициализирует это поле как `1.0`. Начальный Rust snapshot повторяет порядок +NDP строк корневого ресурса и примонтированных `EXTO` (без первой строки +последнего), сохраняя исходные таблицу и строку как provenance. Ключ устройства +сначала меняется по подтверждённому MSH mapping для `EXTO`, затем это готовое +значение применяется напрямую как индекс provider sequence. Длина этой +sequence определяется NDP: она не дополняется по числу узлов модели и ключ не +пересчитывается ещё раз по числу provider rows. `INTO` provider строк не +добавляет. Для примонтированного `EXTO` с пустым NDP невозможно безопасно +применить native skip-first путь, поэтому Rust явно сообщает unavailable; +таблица из одной строки допустима и даёт нулевой вклад. Если обязательный NDP +sidecar недоступен, запрос `0xe00` сообщает это отдельно, оставляя независимые +агрегатные selectors `1/2` доступными. + +Rust query обращается к загруженному объекту и поддерживает `0xe00` только для +текущего descriptor tag `26` с factory tag `26`; другие активные typed tags и +factory paths остаются явными границами. Snapshot показывает состояние сразу +после native-конструкторов, но он не обновляется при Control tick-е. Native +`FUN_10010f30` (RVA `0x10f30`) изменяет и ограничивает provider amount, а +связанные callbacks переносят его между строками; их общий переход в Rust ещё +не реализован. Поэтому вызов нельзя считать живым ресурсным симулятором. +Behavior selector `0x1002` остаётся отдельной задачей: его множитель +`Transfer_Ore_OffBoard` приходит из блока `+0x820`, но источник фактических +текущих настроек для полного runtime-пути пока не подтверждён. + +В x86 oracle при x87 control word `0x007f` проверены порядок, signed zero, +отрицательные входы, расширенный промежуточный диапазон и subnormal return. +Rust запрос ограничивает себя normal-or-zero значениями, поэтому явно отклоняет +промежуточные случаи, где результат выходит за поддерживаемый диапазон f32, +даже если позднее умножение вернуло бы его в диапазон. Это ограничение +поддержки Rust snapshot, а не неверный native-результат. В Unit DAT fixture `UNITS/BUILDS/MINE/min_l_n1.dat` проверена реальная связка -`fr_l_mine.ctl` + `fr_l_mine.ndp`: tag `26`, ключ `2` в таблице из `22` строк, -коэффициент `1.0`. Это не запись живого resource-device значения в миссии. +`fr_l_mine.ctl` + `fr_l_mine.ndp`: tag `26`, ключ `2` в локальной NDP таблице +из `22` строк, коэффициент `1.0`. Игнорируемый runtime-тест выполняет полный +загрузочный путь, подтверждает root/`EXTO` размеры `22` и `4` и итоговые `25` +provider rows после пропуска первого `EXTO` row. Он вызывает Rust query по +восстановленному native правилу адресации и получает начальное значение `1.0`; +getter оригинальной DLL здесь не исполняется. Это не запись живого +resource-device значения во время игры. Закрывающий эксперимент: трассировать `LoadControlSystem`, `LoadPhysicalModel`, `CreateCollManager` и создание collision objects; связать diff --git a/docs/tomes/03-resources.md b/docs/tomes/03-resources.md index a11ad55..a349a79 100644 --- a/docs/tomes/03-resources.md +++ b/docs/tomes/03-resources.md @@ -477,8 +477,15 @@ struct UnitDatRecord112 { При построении mission prototype graph FParkan сохраняет для каждого root упорядоченный список этих raw records. Список передаётся в -`MissionObjectDraft` с исходными `kind`, `parent_or_link`, description и tails; -эти данные пока не объединяются в таблицу эффективных устройств Control. +`MissionObjectDraft` с исходными `kind`, `parent_or_link`, description и tails. +Во время полного mission load runtime использует дерево и подготовленные CTL +resources, чтобы построить для такого объекта упорядоченную базовую таблицу +Control: корневые строки добавляются, дочерний `INTO` может обновить выбранную +строку или добавить записи при sentinel `-1`, а `EXTO` добавляет строки с +переведёнными ключами глобальной MSH-иерархии. Результат хранит исходный factory +tag и provenance; неизвестные tag-specific callbacks он не выполняет. Preview +режим эту таблицу не строит, а неполная typed-record list оставляет состояние +явно unavailable. Каждый graph edge от `UnitDatRoot` к effective component и далее к его MSH dependency также несёт `unit_component_index`. Это индекс исходной записи в diff --git a/docs/tomes/06-behavior.md b/docs/tomes/06-behavior.md index 9d7b01e..7dc6d66 100644 --- a/docs/tomes/06-behavior.md +++ b/docs/tomes/06-behavior.md @@ -593,6 +593,28 @@ parent ID через Control IID `0x202`, virtual slot `+0x44`, только еÑ нужно проверить это поведение явно. `EXTO` проходит отдельную ветвь, поэтому это правило нельзя распространять на все registry types. +Ветка `EXTO` заполняет другие поля. В `FUN_10003760` RVA `0x3760` и mount +`C4`, и target `C8` сначала равны `-1`. Ветка `EXTO` вычисляет mount и +записывает его только в `C4`; `C8` остаётся `-1`. Поэтому Control добавляет +все typed records такого компонента в конец общей таблицы. Raw +`parent_or_link` здесь выбирает mount-узел, а не строку Control. + +`AniMesh.dll` (SHA-256 +`65659bf67f20e5be77d67a5b2f59968f61b94293143807fa59ca451c560c4f7e`) +собирает глобальную нумерацию MSH-узлов: обычная корневая модель добавляет все +`N` узлов, `EXTO`-дочерняя модель добавляет `N - 1`, начиная со своего узла `1`, +а `INTO`-компонент не добавляет узлов. `FUN_1000a460` (RVA `0xa460`) выполняет +эту загрузку MSH; `FUN_10005780` (RVA `0x5780`) находит первый глобальный узел +с заданным component ID. Если у `EXTO`-модели только один узел, она не добавляет +глобальных узлов, и поиск первого узла возвращает `-1`. + +При добавлении `EXTO` Control отдельно переводит ключи CTLD в пространство +глобальной MSH-иерархии: нулевой ключ заменяется mount-узлом `C4`; ненулевой +ключ сдвигается относительно первого глобального узла компонента только при +`C4 != -1`. Например, если компонент начинается с глобального узла `22`, его +исходный ключ `1` становится `22`. Этот ключ выбирает модельный узел, а не +строку Control. Записи корня остаются в исходном виде. + ### Как Control добавляет и переопределяет устройства В `Control.dll` (SHA-256 @@ -609,6 +631,19 @@ override path применяет первую typed record и не добавлÑ путь `0x20800`, поэтому такой результат парсера сам по себе не доказывает отказ оригинального loader-а и не позволяет объявить Rust merge полным. +FParkan строит базовую проекцию этой таблицы во время полного mission load для +объектов с Unit DAT. Корневые typed records добавляются в исходном порядке; +`EXTO` тоже добавляет все свои записи с remapped MSH-ключами; `INTO` обновляет +одну строку по `C8`, а при sentinel `-1` добавляет весь список. Preview таблицу +не создаёт. Проекция хранит исходный factory tag, текущий typed record и их +provenance, но не исполняет все tag-specific callbacks и не заменяет полный +Control runtime. Лицензированный `UNITS/BUILDS/MINE/min_l_n1.dat` подтверждает +10 исходных устройств, четыре `INTO`-override по индексам `0..3` и добавление +одной дочерней записи `EXTO` с ключом `22`, всего 11 устройств. Это результат +Rust full-loader теста на оригинальных ресурсах. Ветви C4/C8 и добавление +`EXTO` подтверждены статически по native ASM; отдельный native full-load oracle +для всей таблицы не запускался. + Для tag `19` фабрика создаёт объект с его исходными классом и vtable. Базовый конструктор `FUN_100218b0` задаёт multiplier `+0x4c = 1.0`. Override через `FUN_1002d890` вызывает у выбранного объекта vtable slot `+0x1c`, то есть @@ -621,16 +656,55 @@ override path применяет первую typed record и не добавлÑ сбрасывают их. Поэтому «переопределение» здесь означает обновить часть данных того же устройства, с ограничениями конкретного метода. +Native x86 oracle для тех же GOG DLL выполняет конструкторы и этот общий setter +на уже созданных получателях factory tags `9`, `15`, `19` и `25` (размеры +объектов `0x120`, `0x98`, `0x94` и `0xa4`). У всех проверенных vtable slot +`+0x1c` указывает на RVA `0x21d50`; тест подтверждает, что override сохраняет +существующий слот, класс/vtable, ключ, component ID и multiplier `2.5`, копируя +новые коэффициенты. Slot `+0x24` у tag `9` указывает на отдельный обработчик +`0x25600`, а у остальных трёх — на no-op `0xbcd0`. Tag `9` проверяется с +нулевым pointer `+0x3c` и записью без map rows: FX host намеренно не подключён, +поэтому его поведение этим опытом не подтверждено. + Статический разбор глобального Control interface показывает aggregate getters `1/2` (`FUN_1002b410`, RVA `0x2b410`) и setter `1` (`FUN_1002ba30`, RVA -`0x2ba30`) для устройств tag `19`. Для конечных значений getter `1` вычисляет -`sum(base * multiplier) / sum(base)`, а getter `2` — `sum(base)`. При встрече -отрицательного base selector `1` сразу возвращает `1.0`, а selector -`2` — этот base. Если подходящих записей нет или сумма base неположительна, -getter возвращает неуспех без записи результата. Setter пишет multiplier `+0x4c` -только устройствам tag `19` с положительным selector-0 значением; пустой набор -возвращает успех. Это вывод статического анализа для конечных входных значений, -без отдельного native execution oracle для глобальных selectors. +`0x2ba30`) для текущего descriptor tag `19`. Getter `1` складывает ответы +виртуального selector `0x200` в числитель и selector `0` в знаменатель; getter +`2` возвращает сумму selector `0`. Для обычного factory tag `19` эти ответы +равны `coefficient * multiplier` и `coefficient`, то есть отношение сводится к +`sum(coefficient * multiplier) / sum(coefficient)`. У factory tag `9` selector +`0` умножает коэффициент на отдельное поле `+0x11c`, тогда как selector `0x200` +использует коэффициент и multiplier; эта особая база меняет знаменатель. +Обработчик factory tag `9` расположен в Control RVA `0x255c0`, а его конструктор +RVA `0x25460` задаёт `+0x11c = 1.0`. При встрече отрицательного ответа selector +`0` getter `1` сразу возвращает `1.0`, а getter `2` — этот ответ. Если подходящих +записей нет или сумма базы неположительна, getter возвращает неуспех без записи +результата. Setter пишет multiplier `+0x4c` только устройствам с текущим +descriptor tag `19` и положительным selector-0 ответом; пустой набор возвращает +успех. Фильтр использует **текущий descriptor tag**, который может измениться +при `INTO`, а виртуальный вызов остаётся методом исходного factory-класса. + +Этот путь проверяется native x86 oracle на оригинальной `Control.dll` указанной +выше: искусственные factory/descriptor пары `9`, `15` и `19` покрывают setter, +отрицательные ответы, округление и тег `9` с scale `0.5`. Для factory tag `25` +его vtable slot `+0x10` статически указывает на общий getter RVA `0x21d00`, но +отдельный aggregate oracle этот класс не запускал. Rust хранит начальный scale +factory `9` и предоставляет `query_loaded_mission_control_resource` для selector +`1`/`2` и `set_loaded_mission_control_multiplier` для setter-а `1`. Вызовы +сначала проверяют живой `ObjectHandle`, затем обращаются к таблице этого объекта +из полной загрузки миссии. Синтетические тесты проверяют границы пустой таблицы, +нулевого знаменателя, неподдержанных factory tags и чисел за диапазоном +нормального `f32`; лицензированный тест сверяет реальные descriptor tags и +получает для `min_l_n1.dat` сумму `16` и отношение `1` до setter-а. + +В этом API пока реализованы только getter selectors `1`/`2` и setter `1`. +Отдельный native setter selector `17`, tag-specific методы прочих factory tags, +их внешние эффекты и вызов из регулярного шага Behavior здесь не моделируются. +Числа должны оставаться конечными и нормальными либо точным нулём; произведения +и отношения за поддерживаемым диапазоном `f32` возвращают явную ошибку, а не +подменяются нулём или `None`. При проверке диапазона Rust оценивает операцию в +`f64`, затем выполняет её в `f32`; это ограниченная проверка диапазона, а не +эмуляция полного x87 accumulator-а. Отдельная ветка того же GOG `AniMesh.dll` показывает, когда создаётся Behavior для host object. В `FUN_10002ff0` (RVA `0x2ff0`, SHA-256 @@ -658,8 +732,21 @@ Provider interface направляет selector `1` и ключ в Control RVA читается текущее type-1 значение выбранной строки. Затем selector умножает сначала provider на коэффициент, после чего на множитель: `provider * base * multiplier`. Коэффициент приходит из CTLD; NDP связывает ключ с описанной -строкой, а текущее значение provider нельзя заменить предположением о состоянии -мира. +строкой. Native loader создаёт для этой строки отдельное runtime-состояние: у +`Control.dll` (та же сборка, SHA-256 +`bcb86318089028548a178b03a21accdce15df0cc85ddd91674cf3c2f55012c6e`) +`FUN_1000b240` (RVA `0xb240`) строит 11 слов строки provider и ставит её +selector-1 поле `+0x14` в `1.0`. `FUN_10008b10` (RVA `0x8b10`) выбирает строки +NDP, а `FUN_1000a100` (RVA `0xa100`) добавляет их в общую таблицу с шагом +`0x2c`; getter `FUN_1000dc40` (RVA `0xdc40`) использует ключ устройства как +индекс этой таблицы. Для корня загружаются все строки. `INTO` не добавляет +строк, а примонтированный `EXTO` пропускает свою первую строку. Пустая +таблица такого `EXTO` не содержит строки, которую native loader должен +пропустить, поэтому Rust оставляет provider lookup недоступным; sidecar с одной +строкой даёт пустой вклад. Если mount узел не разрешился, строки этого `EXTO` +в таблицу не попадают. Это отдельная +последовательность provider-строк: её длина берётся из NDP, она не дополняется +до числа узлов MSH и не участвует во второй перенумерации ключа. Так этот селектор включён в цепочку добычи руды оригинала. В той же сборке `Behavior.dll` (SHA-256 @@ -673,24 +760,40 @@ multiplier`. Коэффициент приходит из CTLD; NDP связыв 5.0 — RVA `0x22d90`; config selector 2 загружает блок через RVA `0x0a1a0`). Отдельный query `0x1005` (RVA `0xa490`) возвращает поле `+0x60` `Transfer_Power_Out` (имя — RVA `0x22e20`, default 100.0 — RVA `0x22d90`; -setter — RVA `0xaa40`). Rust helper пока описывает один Control selector `0x300` -при явно переданных входах; он не реализует эту сумму, Behavior-умножение или -подключение к игровому циклу. +setter — RVA `0xaa40`). В полной загрузке FParkan начальные provider-строки +хранятся вместе с таблицей Control конкретного `ObjectHandle`. Запрос +`0xe00` проходит по ключу устройства в этот снимок и пока поддержан только для +текущего descriptor tag `26` с factory tag `26`; для него результат равен +`provider * fixed_words[11] * multiplier`. Лицензированный тест загружает +оригинальные CTL/NDP и подтверждает, что tag-26 запись с ключом `2` читает +начальное значение строки `1.0`. Это проверка начального состояния после +конструкторов, не результат живого игрового тика. + +Поведение provider-строк меняется и после загрузки: например, +`Control.dll!FUN_10010f30` (RVA `0x10f30`) обновляет и ограничивает текущее +значение, а соседние процедуры распространяют его между связанными строками. +Эти изменения пока не отражены в Rust-снимке. Поэтому `0xe00` не является +исполнением всего Control tick-а, а Behavior query `0x1002` не подключён к +этому API; его множитель `Transfer_Ore_OffBoard` требует отдельного источника +текущей конфигурации. На x87 control word `0x007f` каждое умножение округляет мантиссу до 24 бит, но промежуточная степень может выйти за диапазон `f32` и вернуться в него при -следующем умножении. Rust helper поэтому принимает явные текущие значения -provider и multiplier, проверяет tag, ключ и строку NDP, сохраняет порядок -операций, но поддерживает только конечные нормализованные значения или ноль на -входе, промежуточном и конечном этапах. Остальные случаи за этим численным -диапазоном он отклоняет; полный x87 accumulator здесь не эмулируется. +следующем умножении. Native oracle проверяет такой порядок для getter-а +`0x300`. Rust запрос сохраняет тот же порядок, но поддерживает только конечные +нормализованные значения или ноль на входе, промежуточном и конечном этапах. +Остальные случаи за этим численным диапазоном он отклоняет; полный x87 +accumulator здесь не эмулируется. Лицензированный тест на Unit DAT `UNITS/BUILDS/MINE/min_l_n1.dat` проходит через -компонент `fr_l_mine.ctl` и sidecar `fr_l_mine.ndp`: у найденного tag-26 -дескриптора ключ равен `2` из `22` строк, а raw коэффициент — `0x3f800000`. -Тест передаёт helper-у искусственные текущие значения `1.0` только для проверки -записи и привязки ключа; этот Unit DAT не является наблюдением живого Control -tick-а. +`fr_l_mine.ctl` и `fr_l_mine.ndp`: у найденного tag-26 descriptor ключ равен +`2` в своей записи, raw коэффициент — `0x3f800000`. Корневой NDP содержит 22 +строки, sidecar примонтированного `EXTO` — 4; в общую provider-таблицу входит +25 строк после пропуска локальной строки `0`. Тест также сравнивает порядок +всех source-row индексов с порядком строк самих sidecar таблиц. Он вызывает +Rust query по восстановленному native правилу адресации, а не getter оригинальной +DLL на полной миссии. Значение `1.0` подтверждает начальное состояние по native +конструктору, но не наблюдение живого Control tick-а. ### Control system и physical model |
