From bbdc5d98ac0ae974f98f39313c55803baf294299 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 24 Jun 2023 21:16:32 +0800 Subject: [PATCH] add some xml test --- mods/dr_game/Difficult_Rocket_rs/__init__.py | 9 +- .../Difficult_Rocket_rs/src/Cargo.lock | 44 +++--- .../Difficult_Rocket_rs/src/Cargo.toml | 10 +- .../Difficult_Rocket_rs/src/src/lib.rs | 2 + .../Difficult_Rocket_rs/src/src/python.rs | 136 +++++++++++++++++- .../Difficult_Rocket_rs/src/src/sr1_data.rs | 1 + mods/dr_game/lang/zh-CN.toml | 1 + mods/dr_game/sr1_ship.py | 9 +- 8 files changed, 179 insertions(+), 33 deletions(-) diff --git a/mods/dr_game/Difficult_Rocket_rs/__init__.py b/mods/dr_game/Difficult_Rocket_rs/__init__.py index ee6ba0a..2f19ca3 100644 --- a/mods/dr_game/Difficult_Rocket_rs/__init__.py +++ b/mods/dr_game/Difficult_Rocket_rs/__init__.py @@ -18,16 +18,11 @@ if TYPE_CHECKING: def get_version_str() -> str: ... - - def simluation() -> None: ... - - - """ 用来测试 rust 的物理模拟能不能用 """ - - def part_list_read_test(file_name: Optional[str] = "./configs/PartList.xml") -> None: ... def read_ship_test(path: Optional[str] = "./configs/dock1.xml") -> None: ... + + def load_and_save_test(file_name: str): ... class Camera_rs: """ 用于闲的没事 用 rust 写一个 camera """ diff --git a/mods/dr_game/Difficult_Rocket_rs/src/Cargo.lock b/mods/dr_game/Difficult_Rocket_rs/src/Cargo.lock index d40428d..59acc84 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/Cargo.lock +++ b/mods/dr_game/Difficult_Rocket_rs/src/Cargo.lock @@ -91,7 +91,7 @@ dependencies = [ "autocfg", "cfg-if", "crossbeam-utils", - "memoffset 0.9.0", + "memoffset", "scopeguard", ] @@ -119,6 +119,7 @@ name = "difficult_rocket_rs" version = "0.2.5" dependencies = [ "pyo3", + "quick-xml", "rapier2d-f64", "serde", "serde-xml-rs", @@ -182,13 +183,10 @@ dependencies = [ ] [[package]] -name = "memoffset" -version = "0.8.0" +name = "memchr" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" -dependencies = [ - "autocfg", -] +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" @@ -350,14 +348,14 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.18.3" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3b1ac5b3731ba34fdaa9785f8d74d17448cd18f30cf19e0c7e7b1fdb5272109" +checksum = "cffef52f74ec3b1a1baf295d9b8fcc3070327aefc39a6d00656b13c1d0b8885c" dependencies = [ "cfg-if", "indoc", "libc", - "memoffset 0.8.0", + "memoffset", "parking_lot", "pyo3-build-config", "pyo3-ffi", @@ -367,9 +365,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.18.3" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cb946f5ac61bb61a5014924910d936ebd2b23b705f7a4a3c40b05c720b079a3" +checksum = "713eccf888fb05f1a96eb78c0dbc51907fee42b3377272dc902eb38985f418d5" dependencies = [ "once_cell", "target-lexicon", @@ -377,9 +375,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.18.3" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd4d7c5337821916ea2a1d21d1092e8443cf34879e53a0ac653fbb98f44ff65c" +checksum = "5b2ecbdcfb01cbbf56e179ce969a048fd7305a66d4cdf3303e0da09d69afe4c3" dependencies = [ "libc", "pyo3-build-config", @@ -387,9 +385,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.18.3" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d39c55dab3fc5a4b25bbd1ac10a2da452c4aca13bb450f22818a002e29648d" +checksum = "b78fdc0899f2ea781c463679b20cb08af9247febc8d052de941951024cd8aea0" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -399,15 +397,25 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.18.3" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97daff08a4c48320587b5224cc98d609e3c27b6d437315bd40b605c98eeb5918" +checksum = "60da7b84f1227c3e2fe7593505de274dcf4c8928b4e0a1c23d551a14e4e80a0f" dependencies = [ "proc-macro2", "quote", "syn 1.0.109", ] +[[package]] +name = "quick-xml" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b9228215d82c7b61490fec1de287136b5de6f5700f6e58ea9ad61a7964ca51" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "quote" version = "1.0.28" diff --git a/mods/dr_game/Difficult_Rocket_rs/src/Cargo.toml b/mods/dr_game/Difficult_Rocket_rs/src/Cargo.toml index c787117..de5b5f3 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/Cargo.toml +++ b/mods/dr_game/Difficult_Rocket_rs/src/Cargo.toml @@ -16,11 +16,11 @@ opt-level = 3 [profile.release] strip = true opt-level = "s" -codegen-units = 1 +# codegen-units = 1 -#[dependencies.quick-xml] -#version = "0.27.1" -#features = ["serialize"] +[dependencies.quick-xml] +version = "0.29.0" +features = ["serialize"] [dependencies.serde] version = "1.0.164" @@ -37,5 +37,5 @@ version = "0.17.2" features = ["simd-stable"] [dependencies.pyo3] -version = "0.18.3" +version = "0.19.0" features = ["extension-module"] diff --git a/mods/dr_game/Difficult_Rocket_rs/src/src/lib.rs b/mods/dr_game/Difficult_Rocket_rs/src/src/lib.rs index 0d831d3..ec4bd7a 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/src/lib.rs +++ b/mods/dr_game/Difficult_Rocket_rs/src/src/lib.rs @@ -44,6 +44,8 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(test_call, 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_function(wrap_pyfunction!(python::data::load_and_save_test, m)?)?; + m.add_function(wrap_pyfunction!(python::serde_test::test_ship_read_and_write, m)?)?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs b/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs index b13307e..894a194 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs +++ b/mods/dr_game/Difficult_Rocket_rs/src/src/python.rs @@ -55,7 +55,6 @@ pub mod data { #[pyclass] #[pyo3(name = "SR1PartList_rs")] - #[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")] pub struct PySR1PartList { pub data: SR1PartList, } @@ -63,6 +62,7 @@ pub mod data { #[pymethods] impl PySR1PartList { #[new] + #[pyo3(text_signature = "(file_path = './configs/PartList.xml', list_name = 'NewPartList')")] fn new(file_path: String, list_name: String) -> Self { let raw_part_list: RawPartList = RawPartList::from_file(file_path).unwrap(); let data = raw_part_list.to_sr_part_list(Some(list_name)); @@ -113,7 +113,6 @@ pub mod data { #[pyclass] #[pyo3(name = "SR1Ship_rs")] - #[pyo3(text_signature = "(file_path = './configs/dock1.xml', part_list = './configs/PartList.xml', ship_name = 'NewShip')")] pub struct PySR1Ship { pub ship: SR1Ship, pub part_list: SR1PartList, @@ -122,6 +121,7 @@ pub mod data { #[pymethods] impl PySR1Ship { #[new] + #[pyo3(text_signature = "(file_path = './configs/dock1.xml', part_list = './configs/PartList.xml', ship_name = 'NewShip')")] fn new(file_path: String, part_list: String, ship_name: String) -> Self { let mut ship = SR1Ship::from_file(file_path, Some(ship_name)).unwrap(); let part_list = SR1PartList::from_file(part_list).unwrap(); @@ -205,6 +205,15 @@ pub mod data { Ok(()) } } + + #[pyfunction] + pub fn load_and_save_test(file_name: String) -> PyResult<()> { + use crate::sr1_data::ship::RawShip; + use serde_xml_rs::to_string; + let ship = RawShip::from_file(file_name).unwrap(); + let _save_string = to_string(&ship); + Ok(()) + } } pub mod console { @@ -276,3 +285,126 @@ pub mod console { } } } + +pub mod serde_test { + use pyo3::prelude::*; + use quick_xml::de::from_str; + use quick_xml::se::to_string; + use serde::{Deserialize, Serialize}; + use std::fs; + + type IdType = i64; + + #[derive(Serialize, Deserialize, Debug, Clone)] + #[serde(rename = "Ship")] + pub struct TestShip { + #[serde(rename = "@version")] + pub version: i32, + #[serde(rename = "@liftedOff")] + pub lift_off: i8, + #[serde(rename = "@touchingGround")] + pub touching_ground: i8, + #[serde(rename = "Connections")] + pub connections: Connections, + #[serde(rename = "DisconnectedParts")] + pub disconnected_parts: Option, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct DisconnectedParts { + #[serde(rename = "DisconnectedPart")] + pub disconnected_part: Option>, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct DisconnectedPart { + #[serde(rename = "Parts")] + pub parts: Parts, + #[serde(rename = "Connections")] + pub connections: Connections, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct Connections { + #[serde(rename = "Connection")] + pub connection: Option>, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct Connection { + #[serde(rename = "@parentAttachPoint")] + pub parent_attach_point: i32, + #[serde(rename = "@childAttachPoint")] + pub child_attach_point: i32, + #[serde(rename = "@parentPart")] + pub parent_part: IdType, + #[serde(rename = "@childPart")] + pub child_part: IdType, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct Parts { + #[serde(rename = "Part")] + pub part: Vec, + } + + #[derive(Serialize, Deserialize, Debug, Clone)] + pub struct Part { + // #[serde(rename = "@Tank")] + // pub tank: Option, + // #[serde(rename = "@Engine")] + // pub engine: Option, + // #[serde(rename = "@Pod")] + // pub pod: Option, + #[serde(rename = "@partType")] + pub part_type_id: String, + #[serde(rename = "@id")] + pub id: i64, + #[serde(rename = "@x")] + pub x: f64, + #[serde(rename = "@y")] + pub y: f64, + #[serde(rename = "@editorAngle")] + pub editor_angle: i32, + #[serde(rename = "@angle")] + pub angle: f64, + #[serde(rename = "@angleV")] + pub angle_v: f64, + #[serde(rename = "@flippedX")] + 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 = "@chuteAngle")] + pub chute_angle: Option, + #[serde(rename = "@chuteHeight")] + pub chute_height: Option, + #[serde(rename = "@extension")] + pub extension: Option, + #[serde(rename = "@inflate")] + pub inflate: Option, + #[serde(rename = "@inflation")] + pub inflation: Option, + #[serde(rename = "@exploded")] + pub exploded: Option, + #[serde(rename = "@rope")] + pub rope: Option, + #[serde(rename = "@activated")] + pub activated: Option, + #[serde(rename = "@deployed")] + pub deployed: Option, + } + + #[pyfunction] + #[pyo3(name = "test_ship_read_and_write")] + pub fn test_ship_read_and_write(file_name: String) -> PyResult<()> { + let file = fs::read_to_string(file_name).unwrap(); + let ship: TestShip = from_str(&file).unwrap(); + let save_string = to_string(&ship).unwrap(); + fs::write("./test-xml-rs.xml", save_string).unwrap(); + Ok(()) + } +} diff --git a/mods/dr_game/Difficult_Rocket_rs/src/src/sr1_data.rs b/mods/dr_game/Difficult_Rocket_rs/src/src/sr1_data.rs index 9397ab6..e80160a 100644 --- a/mods/dr_game/Difficult_Rocket_rs/src/src/sr1_data.rs +++ b/mods/dr_game/Difficult_Rocket_rs/src/src/sr1_data.rs @@ -611,6 +611,7 @@ pub mod ship { #[inline] pub fn save(&self, file_name: String) -> Option<()> { let part_list_file = serde_xml_rs::to_string(self); + print!("{:?}", part_list_file); if let Ok(part_list_file) = part_list_file { fs::write(file_name, part_list_file).unwrap(); Some(()) diff --git a/mods/dr_game/lang/zh-CN.toml b/mods/dr_game/lang/zh-CN.toml index cf38f64..b124803 100644 --- a/mods/dr_game/lang/zh-CN.toml +++ b/mods/dr_game/lang/zh-CN.toml @@ -10,3 +10,4 @@ ship.load = "正在加载飞船: {}" ship.load_time = "飞船加载消耗时间: {} 秒" ship.info = "飞船信息:\n- 部件数量: {}\n- 部件重量: {}" ship.render.done = "飞船渲染完成" +save.start = "正在保存飞船: {}" diff --git a/mods/dr_game/sr1_ship.py b/mods/dr_game/sr1_ship.py index 4b20680..e92b810 100644 --- a/mods/dr_game/sr1_ship.py +++ b/mods/dr_game/sr1_ship.py @@ -36,7 +36,11 @@ if TYPE_CHECKING: from Difficult_Rocket.client import ClientWindow if DR_mod_runtime.use_DR_rust: - from .Difficult_Rocket_rs import CenterCamera_rs, SR1PartList_rs, SR1Ship_rs + from .Difficult_Rocket_rs import (CenterCamera_rs, + SR1PartList_rs, + SR1Ship_rs, + load_and_save_test, + test_ship_read_and_write) logger = logging.getLogger('client.dr_game_sr1_ship') sr_tr = Tr(lang_path=Path('./mods/dr_game/lang')) @@ -150,6 +154,8 @@ class SR1ShipRender(BaseScreen): self.xml_name = file_path if DR_mod_runtime.use_DR_rust: try: + load_and_save_test(self.xml_name) + test_ship_read_and_write(self.xml_name) self.rust_ship = SR1Ship_rs(file_path, 'configs/PartList.xml', 'a_new_ship') print(self.rust_ship.name) print(self.rust_ship.img_pos) @@ -436,6 +442,7 @@ class SR1ShipRender(BaseScreen): return if not DR_mod_runtime.use_DR_rust: return + logger.info(sr_tr().sr1.ship.save.start().format(self.rust_ship)) self.rust_ship.save('./test-save.xml') def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int, window: "ClientWindow"):