From 889c5b466ead93bb52fb2595499de559ae0a1465 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 8 Apr 2023 18:32:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=BD=E5=BC=80=E5=BF=83~=20=E4=BB=8A?= =?UTF-8?q?=E5=A4=A9=E6=98=AF=E6=9C=80=E7=BE=8E=E5=A5=BD=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E5=A4=A9=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Difficult_Rocket/__init__.py | 2 +- Difficult_Rocket/client/__init__.py | 5 + configs/main.toml | 4 +- libs/Difficult_Rocket_rs/__init__.py | 1 + libs/Difficult_Rocket_rs/src/src/lib.rs | 3 +- libs/Difficult_Rocket_rs/src/src/sr1_data.rs | 81 ++++-- libs/Difficult_Rocket_rs/src/src/types.rs | 261 +++++++++++++++---- 7 files changed, 282 insertions(+), 75 deletions(-) diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index dadfb91..89c2539 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -24,7 +24,7 @@ from libs.MCDR.version import Version game_version = Version("0.7.2.1") # 游戏版本 build_version = Version("1.2.1.0") # 编译文件版本(与游戏本体无关) -DR_rust_version = Version("0.2.6.0") # DR 的 Rust 编写部分的版本 +DR_rust_version = Version("0.2.6.1") # DR 的 Rust 编写部分的版本 Api_version = Version("0.0.1.0") # API 版本 __version__ = game_version diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index cabc287..a4a451c 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -90,6 +90,11 @@ class Client: file_drops=file_drop) end_time = time.time_ns() self.use_time = end_time - start_time + if DR_option.use_DR_rust: + from libs.Difficult_Rocket_rs import read_ship_test, part_list_read_test + part_list_read_test() + read_ship_test() + self.logger.info(tr().client.setup.use_time().format(Decimal(self.use_time) / 1000000000)) self.logger.debug(tr().client.setup.use_time_ns().format(self.use_time)) diff --git a/configs/main.toml b/configs/main.toml index ca661b8..1f8b9b3 100644 --- a/configs/main.toml +++ b/configs/main.toml @@ -7,8 +7,8 @@ fonts_folder = "libs/fonts" [window] style = "None" -width = 1912 -height = 987 +width = 1406 +height = 893 visible = true gui_scale = 1 caption = "Difficult Rocket v{DR_version}|DR_rs v{DR_Rust_get_version}" diff --git a/libs/Difficult_Rocket_rs/__init__.py b/libs/Difficult_Rocket_rs/__init__.py index d7f67b7..03c57ef 100644 --- a/libs/Difficult_Rocket_rs/__init__.py +++ b/libs/Difficult_Rocket_rs/__init__.py @@ -27,6 +27,7 @@ if TYPE_CHECKING: def part_list_read_test(file_name: Optional[str] = "./configs/PartList.xml") -> None: ... + def read_ship_test(path: Optional[str] = "./configs/dock1.xml") -> None: ... class Camera_rs: """ 用于闲的没事 用 rust 写一个 camera """ diff --git a/libs/Difficult_Rocket_rs/src/src/lib.rs b/libs/Difficult_Rocket_rs/src/src/lib.rs index 0649e85..28300f2 100644 --- a/libs/Difficult_Rocket_rs/src/src/lib.rs +++ b/libs/Difficult_Rocket_rs/src/src/lib.rs @@ -17,7 +17,7 @@ use pyo3::prelude::*; #[pyfunction] fn get_version_str() -> String { - return "0.2.6.0".to_string(); + return "0.2.6.1".to_string(); } #[pyfunction] @@ -36,6 +36,7 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(test_call, m)?)?; m.add_function(wrap_pyfunction!(simulator::simluation, m)?)?; m.add_function(wrap_pyfunction!(sr1_data::part_list::read_part_list_py, m)?)?; + m.add_function(wrap_pyfunction!(sr1_data::ship::py_raw_ship_from_file, m)?)?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/libs/Difficult_Rocket_rs/src/src/sr1_data.rs b/libs/Difficult_Rocket_rs/src/src/sr1_data.rs index 3a556fc..c5891cb 100644 --- a/libs/Difficult_Rocket_rs/src/src/sr1_data.rs +++ b/libs/Difficult_Rocket_rs/src/src/sr1_data.rs @@ -388,11 +388,13 @@ pub mod ship { use pyo3::prelude::*; use serde::{Deserialize, Serialize}; // use quick_xml::de::from_str; - use serde_xml_rs::{expect, from_str}; + use serde_xml_rs::from_str; - use crate::types::sr1::{SR1Ship, SR1ShipTrait}; + use crate::types::sr1::{SR1PartData, SR1PartDataAttr, SR1Ship}; + use crate::types::sr1::{SR1PartDataTrait, SR1ShipTrait}; #[derive(Debug, Serialize, Deserialize, Clone)] + #[serde(rename = "Ship")] pub struct RawShip { #[serde(rename = "Parts")] pub parts: Parts, @@ -403,8 +405,9 @@ pub mod ship { pub lift_off: i8, #[serde(rename = "touchingGround")] pub touch_ground: i8, + #[serde(rename = "DisconnectedParts")] + pub disconnected: Option>, } - // #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Parts { @@ -427,6 +430,8 @@ pub mod ship { #[serde(rename = "Pod")] pub pod: Option, + #[serde(rename = "partType")] + pub part_type: String, pub id: i64, pub x: f64, pub y: f64, @@ -439,35 +444,27 @@ pub mod ship { pub flip_x: Option, #[serde(rename = "flippedY")] pub flip_y: Option, + // 降落伞 #[serde(rename = "chuteX")] pub chute_x: Option, #[serde(rename = "chuteY")] pub chute_y: Option, - #[serde(rename = "partType")] - pub part_type: String, - pub extension: Option, - pub exploded: Option, #[serde(rename = "chuteAngle")] pub chute_angle: Option, #[serde(rename = "chuteHeight")] pub chute_height: Option, - pub deployed: Option, - pub rope: Option, + pub extension: Option, pub inflate: Option, pub inflation: Option, + pub exploded: Option, + pub rope: Option, + // ? + pub deployed: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Engine { - pub fuel: i64, - } - - #[derive(Debug, Serialize, Deserialize, Clone)] - pub struct Pod { - #[serde(rename = "Staging")] - pub stages: Vec, - pub name: String, - pub throttle: f64, + pub fuel: f64, } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -476,14 +473,29 @@ pub mod ship { } #[derive(Debug, Serialize, Deserialize, Clone)] - pub struct Staging { - #[serde(rename = "currentStage")] - pub current_stage: u32, - #[serde(rename = "Activate")] - pub steps: Vec, + pub struct Pod { + #[serde(rename = "Staging")] + pub stages: Staging, + pub name: String, + pub throttle: f64, } #[derive(Debug, Serialize, Deserialize, Clone)] + pub struct Staging { + #[serde(rename = "currentStage")] + pub current_stage: u32, + #[serde(rename = "Step")] + pub steps: Vec, + } + + #[derive(Debug, Serialize, Deserialize, Clone)] + pub struct Step { + #[serde(rename = "Activate")] + pub activates: Vec, + } + + #[derive(Debug, Serialize, Deserialize, Clone)] + #[serde(rename = "Activate")] pub struct Activate { #[serde(rename = "Id")] pub id: i64, @@ -498,9 +510,9 @@ pub mod ship { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct DisconnectedPart { #[serde(rename = "Parts")] - pub parts: Vec, + pub parts: Vec, #[serde(rename = "Connections")] - pub connects: Vec, + pub connects: Vec, } /// /// @@ -531,6 +543,15 @@ pub mod ship { impl SR1ShipTrait for RawShip { #[inline] fn to_sr_ship(&self, name: Option) -> SR1Ship { + let connects: Vec<(i64, i64, i64, i64)> = + Vec::from_iter(self.connects.connects.iter().map(|connect| { + ( + connect.parent_attach_point, + connect.child_attach_point, + connect.parent_part, + connect.child_part, + ) + })); todo!() } @@ -548,4 +569,14 @@ pub mod ship { Some(ship) } } + + #[pyfunction] + #[pyo3(name = "read_ship_test")] + #[pyo3(signature = (path = "./configs/dock1.xml".to_string()))] + pub fn py_raw_ship_from_file(path: String) -> PyResult { + let file = fs::read_to_string(path).unwrap(); + let raw_ship: RawShip = from_str(&file).unwrap(); + println!("{:?}", raw_ship); + Ok(true) + } } diff --git a/libs/Difficult_Rocket_rs/src/src/types.rs b/libs/Difficult_Rocket_rs/src/src/types.rs index f6c37f4..e4c1307 100644 --- a/libs/Difficult_Rocket_rs/src/src/types.rs +++ b/libs/Difficult_Rocket_rs/src/src/types.rs @@ -14,7 +14,12 @@ pub mod sr1 { AttachPoint, AttachPoints, Engine, Lander, Rcs, Shape as RawShape, Solar, Tank, }; use crate::sr1_data::part_list::{RawPartList, RawPartType, SR1PartTypeEnum}; - use crate::sr1_data::ship::RawShip; + use crate::sr1_data::ship::{ + Activate as RawActivate, Connection, Connections, DisconnectedPart as RawDisconnectedPart, + DisconnectedParts as RawDisconnectedParts, Engine as RawEngine, Part as RawPartData, + Parts as RawParts, Pod as RawPod, RawShip, Staging as RawStaging, Step as RawStep, + Tank as RawTank, + }; #[inline] pub fn map_ptype_textures(ptype: String) -> String { @@ -51,34 +56,20 @@ pub mod sr1 { .to_string() } - #[derive(Debug, Clone)] - pub struct SR1PartData { - // 单独的属性 - pub attr: Option, - // 基本状态属性 - pub x: f64, - pub y: f64, - pub id: i64, - pub angle: f64, // 弧度制 - pub angle_v: f64, - // 状态属性 - pub part_type: String, - pub active: bool, - pub editor_angle: i32, - pub flip_x: bool, - pub flip_y: bool, - // 降落伞属性 - pub chute_x: f64, - pub chute_y: f64, - pub chute_angle: f64, - pub chute_height: f64, - pub inflate: bool, - pub inflation: bool, - pub deployed: bool, - // 太阳能板属性 - pub extension: f64, + #[inline] + pub fn i8_to_bool(i: i8) -> bool { + match i { + 0 => false, + _ => true, + } + } - pub explode: bool, + #[inline] + pub fn bool_to_i8(b: bool) -> i8 { + match b { + false => 0, + true => 1, + } } #[derive(Debug, Copy, Clone)] @@ -118,22 +109,6 @@ pub mod sr1 { }, } - #[derive(Debug, Clone)] - pub enum SR1PartDataAttr { - Tank { - fuel: f64, - }, - Engine { - fuel: f64, - }, - Pod { - name: String, - throttle: f64, - current_stage: u32, - steps: Vec<(i64, bool)>, - }, - } - #[derive(Debug, Copy, Clone)] pub struct Damage { pub disconnect: i32, @@ -213,6 +188,7 @@ pub mod sr1 { } impl SR1PartList { + #[inline] pub fn from_file(file_name: String) -> Option { if let Some(raw_list) = RawPartList::from_file(file_name) { return Some(raw_list.to_sr_part_list(None)); @@ -239,6 +215,11 @@ pub mod sr1 { fn to_raw_part_type(&self) -> RawPartType; } + pub trait SR1PartDataTrait { + fn to_sr_part_data(&self) -> SR1PartData; + fn to_raw_part_data(&self) -> RawPartData; + } + pub trait SR1PartListTrait { fn to_sr_part_list(&self, name: Option) -> SR1PartList; fn to_raw_part_list(&self) -> RawPartList; @@ -416,14 +397,179 @@ pub mod sr1 { } } + #[derive(Debug, Clone)] + pub struct SR1PartData { + // 单独的属性 + pub attr: Option, + // 基本状态属性 + pub x: f64, + pub y: f64, + pub id: i64, + pub angle: f64, // 弧度制 + pub angle_v: f64, + // 状态属性 + pub part_type: String, + pub active: bool, + pub editor_angle: i32, + pub flip_x: bool, + pub flip_y: bool, + + pub explode: bool, + } + + impl SR1PartDataTrait for SR1PartData { + #[inline] + fn to_sr_part_data(&self) -> SR1PartData { + self.clone() + } + + #[inline] + fn to_raw_part_data(&self) -> RawPartData { + let tank = match &self.attr { + Some(attr) => match attr { + SR1PartDataAttr::Tank { fuel } => Some(RawTank { fuel: *fuel }), + _ => None, + }, + _ => None, + }; + let engine = match &self.attr { + Some(attr) => match attr { + SR1PartDataAttr::Engine { fuel } => Some(RawEngine { fuel: *fuel }), + _ => None, + }, + _ => None, + }; + let pod = match &self.attr { + Some(attr) => match attr { + SR1PartDataAttr::Pod { + name, + throttle, + current_stage, + steps, + } => Some({ + let mut actives = Vec::new(); + for step in steps { + let mut steps_ = Vec::new(); + for active in step { + steps_.push(RawActivate { + id: active.0, + moved: bool_to_i8(active.1), + }); + } + actives.push(RawStep { activates: steps_ }); + } + let stages = RawStaging { + current_stage: *current_stage, + steps: actives, + }; + RawPod { + name: name.clone(), + throttle: *throttle, + stages, + } + }), + _ => None, + }, + _ => None, + }; + let (chute_x, chute_y, chute_angle, chute_height, inflate, inflation, deployed, rope) = + match &self.attr { + Some(attr) => match attr { + SR1PartDataAttr::Parachute { + chute_x, + chute_y, + chute_angle, + chute_height, + inflate, + inflation, + deployed, + rope, + } => ( + Some(*chute_x), + Some(*chute_y), + Some(*chute_angle), + Some(*chute_height), + Some(bool_to_i8(*inflate)), + Some(bool_to_i8(*inflation)), + Some(bool_to_i8(*deployed)), + Some(bool_to_i8(*rope)), + ), + _ => (None, None, None, None, None, None, None, None), + }, + _ => (None, None, None, None, None, None, None, None), + }; + let extension = match &self.attr { + Some(attr) => match attr { + SR1PartDataAttr::Solar { extension } => Some(*extension), + _ => None, + }, + _ => None, + }; + RawPartData { + tank, + engine, + pod, + part_type: self.part_type.clone(), + id: self.id, + x: self.x, + y: self.y, + editor_angle: self.editor_angle, + angle: self.angle, + angle_v: self.angle_v, + flip_x: Some(bool_to_i8(self.flip_x)), + flip_y: Some(bool_to_i8(self.flip_y)), + chute_x, + chute_y, + chute_height, + extension, + inflate, + inflation, + exploded: Some(bool_to_i8(self.explode)), + rope, + chute_angle, + deployed, + } + } + } + + #[derive(Debug, Clone)] + pub enum SR1PartDataAttr { + Tank { + fuel: f64, + }, + Engine { + fuel: f64, + }, + Pod { + name: String, + throttle: f64, + current_stage: u32, + steps: Vec>, + }, + Solar { + extension: f64, + }, + Parachute { + chute_x: f64, + chute_y: f64, + chute_angle: f64, + chute_height: f64, + inflate: bool, + inflation: bool, + deployed: bool, + rope: bool, + }, + } + #[derive(Debug, Clone)] pub struct SR1Ship { pub name: String, pub description: String, pub parts: Vec, - pub connections: Vec, + pub connections: Vec, pub lift_off: bool, pub touch_ground: bool, + pub disconnected: Vec<(Vec, Vec)>, } impl SR1ShipTrait for SR1Ship { @@ -440,7 +586,30 @@ pub mod sr1 { #[inline] fn to_raw_ship(&self) -> RawShip { - todo!() // 1145行的内容 + todo!(); + // let mut parts = Vec::new(); + // for part in &self.parts { + // parts.push(part.to_raw_part_data()); + // } + // let connections = Connections { + // connects: self.connections.clone(), + // }; + // let mut disconnected = Vec::new(); + // for (parts, connections) in &self.disconnected { + // let mut parts = Vec::new(); + // for part in parts { + // parts.push(part.to_raw_part_data()); + // } + // disconnected.push((parts, connections.clone())); + // } + // RawShip { + // parts: RawParts { parts }, + // connects: connections, + // version: 1, + // lift_off: bool_to_i8(self.lift_off), + // touch_ground: bool_to_i8(self.touch_ground), + // disconnected, + // } } } }