Skip to content

Commit

Permalink
feat: upgrade rust version & simconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Sequal32 committed May 9, 2023
1 parent 349aaab commit 07e3c9e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

2 changes: 1 addition & 1 deletion src/yourcontrols/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ semver = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.8"
simconnect = { git = "https://github.com/Sequal32/simconnect-rust", branch = "test" }
simconnect = "0.2"
simplelog = "0.12"
spin_sleep = "1.1"
tungstenite = "0.18"
Expand Down
4 changes: 3 additions & 1 deletion src/yourcontrols/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ fn main() {
}
// Exception occured
DispatchResult::Exception(data) => {
warn!("[SIM] SimConnect exception occurred: {}", data.dwException);
warn!("[SIM] SimConnect exception occurred: {}", unsafe {
std::ptr::addr_of!(data.dwException).read_unaligned()
});

if data.dwException == 31 {
// Client data area was not initialized by the gauge
Expand Down
112 changes: 63 additions & 49 deletions src/yourcontrols/src/sync/gaugecommunicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,34 @@ impl GaugeCommunicator {
writer.write_string(format!(r#"{} (>{})"#, val.trim(), var_name.trim()));
}

conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);
unsafe {
conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}

pub fn send_raw(&self, conn: &SimConnector, string: &str) {
let mut writer = MemWriter::new(128, 4).unwrap();
writer.write_i32(0);
writer.pad(4);
writer.write_str(string);
conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);

unsafe {
conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}

pub fn add_definition(&mut self, var_name: String, var_units: Option<&str>) {
Expand All @@ -151,14 +156,16 @@ impl GaugeCommunicator {
}
}

conn.set_client_data(
SEND_MULTIPLE,
SEND_MULTIPLE,
0,
0,
8064,
writer.get_data_location() as *mut std::ffi::c_void,
);
unsafe {
conn.set_client_data(
SEND_MULTIPLE,
SEND_MULTIPLE,
0,
0,
8064,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}
}

Expand Down Expand Up @@ -210,27 +217,32 @@ impl GaugeCommunicator {
}
}

conn.set_client_data(
SEND_INTERPOLATE,
SEND_INTERPOLATE,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
0,
count * 12 + 12,
writer.get_data_location() as *mut std::ffi::c_void,
);
unsafe {
conn.set_client_data(
SEND_INTERPOLATE,
SEND_INTERPOLATE,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
0,
count * 12 + 12,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}

fn do_operation(&self, operation: i32, conn: &SimConnector) {
let mut writer = MemWriter::new(128, 4).unwrap();
writer.write_i32(operation);
conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);

unsafe {
conn.set_client_data(
SEND,
SEND,
0,
0,
128,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}

fn clear_definitions(&mut self, conn: &SimConnector) {
Expand All @@ -246,7 +258,7 @@ impl GaugeCommunicator {
data: &simconnect::SIMCONNECT_RECV_CLIENT_DATA,
) -> Vec<GetResult> {
let datums: &'static [ReturnDatum] = unsafe {
let pointer = &data._base.dwData as *const u32;
let pointer = std::ptr::addr_of!(data._base.dwData);
let array_pointer = pointer.add(2) as *const ReturnDatum;
let length = pointer.read();

Expand Down Expand Up @@ -288,14 +300,16 @@ impl GaugeCommunicator {
writer.pad(64 - datum.exec_string.len() as isize);
}

conn.set_client_data(
MAP_INTERPOLATE,
MAP_INTERPOLATE,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
0,
(self.interpolate_datums.len() * 72) as u32,
writer.get_data_location() as *mut std::ffi::c_void,
);
unsafe {
conn.set_client_data(
MAP_INTERPOLATE,
MAP_INTERPOLATE,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
0,
(self.interpolate_datums.len() * 72) as u32,
writer.get_data_location() as *mut std::ffi::c_void,
);
}
}

pub fn on_connected(&mut self, conn: &SimConnector) {
Expand Down
27 changes: 15 additions & 12 deletions src/yourcontrols/src/sync/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ impl AircraftVars {
&mut self,
data: &simconnect::SIMCONNECT_RECV_SIMOBJECT_DATA,
) -> Result<SimValue, io::Error> {
let vars = match self
.reader
.read_from_bytes(data.dwDefineCount, &data.dwData as *const u32)
{
let vars = match self.reader.read_from_bytes(
data.dwDefineCount,
std::ptr::addr_of!(data.dwData) as *const u32,
) {
Ok(v) => v,
Err(e) => return Err(e),
};
Expand All @@ -225,14 +225,17 @@ impl AircraftVars {

pub fn set_vars(&self, conn: &SimConnector, data: &SimValue) {
let mut bytes = self.reader.write_to_data(data);
conn.set_data_on_sim_object(
self.define_id,
0,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
data.len() as u32,
bytes.len() as u32,
bytes.as_mut_ptr() as *mut std::ffi::c_void,
);

unsafe {
conn.set_data_on_sim_object(
self.define_id,
0,
simconnect::SIMCONNECT_CLIENT_DATA_SET_FLAG_TAGGED,
data.len() as u32,
bytes.len() as u32,
bytes.as_mut_ptr() as *mut std::ffi::c_void,
);
}
}

pub fn get_var(&self, var_name: &str) -> Option<&VarReaderTypes> {
Expand Down

0 comments on commit 07e3c9e

Please sign in to comment.