Compare commits

..

2 Commits

Author SHA1 Message Date
b483e4a12b
fetch up pyglet 2024-11-05 23:55:12 +08:00
1b01887374
进行一波为了拆分的大重构 2024-11-05 23:53:53 +08:00
16 changed files with 1672 additions and 151 deletions

@ -1 +1 @@
Subproject commit 0ff06e9edc2f5b5f0c9ba15d5979894447add897
Subproject commit 4da7e7457051870cfd4c539dfa7c0352db9aaa1c

View File

@ -52,6 +52,11 @@ if TYPE_CHECKING:
"""
...
def render_hack() -> None:
"""
进行一些 rust 方面的 渲染接管
"""
class SR1PartType_rs: # NOQA
"""
用于从 rust 中读取 SR1PartType

File diff suppressed because it is too large Load Diff

View File

@ -24,15 +24,18 @@ opt-level = 2
[dependencies]
anyhow = "1.0"
quick-xml = { version = "0.36.1", features = ["serialize"] }
quick-xml = { version = "0.37", features = ["serialize"] }
serde = { version = "1.0", features = ["derive"] }
nalgebra = "0.33"
pyo3 = { version = "0.22.2", features = [
pyo3 = { version = "0.22", features = [
"extension-module",
"macros",
"py-clone",
] }
rapier2d-f64 = { version = "0.22.0", features = ["simd-stable"] }
# 让我们来试试黑魔法
winit = "0.30.5"
rapier2d-f64 = { version = "0.22", features = ["simd-stable"] }
# 虽然但是, raiper在这里!

View File

@ -8,12 +8,13 @@
mod dr_physics;
mod python;
/// 也许是一些渲染的东西
mod renders;
/// sr1 的逆天数据结构解析
mod sr1_parse;
use pyo3::prelude::*;
pub type IdType = i64;
#[pyfunction]
fn get_version_str() -> String {
env!("CARGO_PKG_VERSION").to_string()
@ -23,10 +24,11 @@ fn get_version_str() -> String {
#[pyo3(name = "Difficult_Rocket_rs")]
fn module_init(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_version_str, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::part_list::read_part_list_py, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::ship::py_raw_ship_from_file, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::py::read_part_list_py, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::py::py_raw_ship_from_file, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::py::py_assert_ship, m)?)?;
m.add_function(wrap_pyfunction!(python::data::load_and_save_test, m)?)?;
m.add_function(wrap_pyfunction!(sr1_parse::ship::py_assert_ship, m)?)?;
m.add_function(wrap_pyfunction!(renders::render_hack, m)?)?;
m.add_class::<python::data::PySR1Ship>()?;
m.add_class::<python::data::PySR1PartList>()?;
m.add_class::<python::data::PySR1PartType>()?;

View File

@ -1,4 +1,4 @@
use std::io::{self, Write};
use std::io::Write;
use pyo3::prelude::*;
@ -24,7 +24,7 @@ impl PyConsole {
let (stop_sender, stop_receiver) = std::sync::mpsc::channel();
let (keyboard_input_sender, keyboard_input_receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let std_in = io::stdin();
let std_in = std::io::stdin();
loop {
if let Ok(()) = stop_receiver.try_recv() {
break;
@ -39,7 +39,7 @@ impl PyConsole {
}
});
print!("rs>");
io::stdout().flush().unwrap();
std::io::stdout().flush().unwrap();
self.stop_sender = Some(stop_sender);
self.keyboard_input_receiver = Some(keyboard_input_receiver);
}
@ -54,7 +54,7 @@ impl PyConsole {
fn new_command(&self) -> bool {
print!("rs>");
io::stdout().flush().unwrap();
std::io::stdout().flush().unwrap();
true
}

View File

@ -6,10 +6,10 @@ use pyo3::prelude::*;
use crate::dr_physics::math::{Point2D, Rotate};
use crate::sr1_parse::part_list::RawPartList;
use crate::sr1_parse::ship::{Connection, RawConnectionData, RawShip};
use crate::sr1_parse::IdType;
use crate::sr1_parse::SaveStatus;
use crate::sr1_parse::{get_max_box, SR1PartData, SR1PartListTrait};
use crate::sr1_parse::{SR1PartList, SR1PartType, SR1Ship};
use crate::IdType;
use quick_xml::se::to_string;

View File

@ -6,7 +6,7 @@
* -------------------------------
*/
use crate::IdType;
use crate::sr1_parse::IdType;
use nalgebra::{Matrix2, Vector2};
use pyo3::prelude::*;

View File

@ -0,0 +1,38 @@
use pyo3::pyfunction;
use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window};
#[pyfunction]
pub fn render_hack() {
println!("render_hacking_start");
render_main();
println!("render_hacking_end");
}
#[derive(Default)]
struct App {
window: Option<Window>,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
// 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里
// self.window = Some(event_loop.ge)
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
}
}
fn render_main() -> anyhow::Result<()> {
let event_loop = EventLoop::new()?;
let mut app = App::default();
event_loop.run_app(&mut app);
Ok(())
}

View File

@ -1,6 +1,9 @@
mod data_structure;
pub mod part_list;
/// 所有的 Python 相关交互都应该在这里
pub mod py;
pub mod ship;
pub use self::data_structure::*;
pub type IdType = i64;
use crate::dr_physics::math::{Edge, Shape};
use crate::dr_physics::math::{Point2D, Rotate};
@ -12,7 +15,6 @@ use crate::sr1_parse::ship::{
Staging as RawStaging, Step as RawStep, Tank as RawTank,
};
use crate::sr1_parse::ship::{Connections as RawConnections, DisconnectedParts, Parts as RawParts};
use crate::IdType;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;

View File

@ -1,10 +0,0 @@
/*
* -------------------------------
* Difficult Rocket
* Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
* All rights reserved
* -------------------------------
*/
pub mod part_list;
pub mod ship;

View File

@ -2,7 +2,6 @@ use crate::sr1_parse::{SR1PartList, SR1PartType, SR1PartTypeAttr};
use crate::sr1_parse::{SR1PartListTrait, SR1PartTypeData};
use anyhow::Result;
use pyo3::prelude::*;
use quick_xml::de::from_str;
use serde::{Deserialize, Serialize};
@ -407,17 +406,3 @@ impl SR1PartListTrait for RawPartList {
self.clone()
}
}
#[pyfunction]
#[pyo3(name = "part_list_read_test", signature = (file_name = "./assets/builtin/PartList.xml".to_string()))]
pub fn read_part_list_py(_py: Python, file_name: Option<String>) -> PyResult<()> {
let file_name = file_name.unwrap_or("./assets/builtin/PartList.xml".to_string());
// let _parts = RawPartList::from_file(file_name);
// if let Some(parts) = _parts {
// // println!("{:?}", parts)
// parts.list_print();
// let _part_list = parts.to_sr_part_list(Some("Vanilla".to_string()));
// }
println!("{:?}", RawPartList::from_file(file_name));
Ok(())
}

View File

@ -0,0 +1,96 @@
use pyo3::{pyfunction, PyResult, Python};
use quick_xml::{de::from_str, events::Event, Reader};
use super::part_list::RawPartList;
use super::ship::RawShip;
#[pyfunction]
#[pyo3(name = "part_list_read_test", signature = (file_name = "./assets/builtin/PartList.xml".to_string()))]
pub fn read_part_list_py(_py: Python, file_name: Option<String>) -> PyResult<()> {
let file_name = file_name.unwrap_or("./assets/builtin/PartList.xml".to_string());
// let _parts = RawPartList::from_file(file_name);
// if let Some(parts) = _parts {
// // println!("{:?}", parts)
// parts.list_print();
// let _part_list = parts.to_sr_part_list(Some("Vanilla".to_string()));
// }
println!("{:?}", RawPartList::from_file(file_name));
Ok(())
}
#[pyfunction]
#[pyo3(name = "read_ship_test")]
#[pyo3(signature = (path = "./assets/builtin/dock1.xml".to_string()))]
pub fn py_raw_ship_from_file(path: String) -> PyResult<bool> {
let file = std::fs::read_to_string(path)?;
let raw_ship = from_str::<RawShip>(&file);
match raw_ship {
Ok(ship) => {
println!("{:?}", ship);
Ok(true)
}
Err(e) => {
println!("{:?}", e);
Ok(false)
}
}
}
#[pyfunction]
#[pyo3(name = "assert_ship")]
/// 校验这玩意是不是个船
pub fn py_assert_ship(path: String) -> bool {
let file_data = match std::fs::read_to_string(path) {
Ok(data) => data,
Err(e) => {
println!("ERROR while reading file!\n{}\n----------", e);
return false;
}
};
let mut reader = Reader::from_str(&file_data);
// 读取第一个
loop {
match reader.read_event() {
Ok(Event::Start(e)) => {
if e.name().as_ref() == b"Ship" {
// 再验证一下 version, liftedOff, touchingGround
let mut founds = (false, false, false);
for attr in e.attributes().flatten() {
match attr.key.as_ref() {
b"version" => {
founds.0 = true;
}
b"liftedOff" => {
founds.1 = true;
}
b"touchingGround" => {
founds.2 = true;
}
_ => (),
}
}
if !(founds.0 && founds.1 && founds.2) {
println!(
"warning: {}{}{} not found",
if founds.0 { "" } else { "version " },
if founds.1 { "" } else { "liftedOff " },
if founds.2 { "" } else { "touchingGround " }
);
return false;
} else {
return true;
}
}
}
Ok(Event::Eof) => {
println!("EOF");
return false;
}
Err(e) => {
println!("ERROR while using xml to parse the file!\n{:?}\n----------", e);
return false;
}
_ => (),
}
}
}

View File

@ -1,9 +1,7 @@
use crate::sr1_parse::IdType;
use crate::sr1_parse::{SR1PartData, SR1PartDataAttr, SR1Ship};
use crate::sr1_parse::{SR1PartDataTrait, SR1ShipTrait};
use crate::IdType;
use anyhow::Result;
use pyo3::prelude::*;
use quick_xml::de::from_str;
use quick_xml::events::Event;
use quick_xml::reader::Reader;
@ -344,87 +342,10 @@ impl RawShip {
}
#[allow(unused)]
pub fn save(&self, file_name: String) -> Result<()> {
pub fn save(&self, file_name: String) -> anyhow::Result<()> {
let part_list_file = to_string(self)?;
print!("{:?}", part_list_file);
std::fs::write(file_name, part_list_file)?;
Ok(())
}
}
#[pyfunction]
#[pyo3(name = "read_ship_test")]
#[pyo3(signature = (path = "./assets/builtin/dock1.xml".to_string()))]
pub fn py_raw_ship_from_file(path: String) -> PyResult<bool> {
let file = std::fs::read_to_string(path)?;
let raw_ship = from_str::<RawShip>(&file);
match raw_ship {
Ok(ship) => {
println!("{:?}", ship);
Ok(true)
}
Err(e) => {
println!("{:?}", e);
Ok(false)
}
}
}
#[pyfunction]
#[pyo3(name = "assert_ship")]
/// 校验这玩意是不是个船
pub fn py_assert_ship(path: String) -> bool {
let file_data = match std::fs::read_to_string(path) {
Ok(data) => data,
Err(e) => {
println!("ERROR while reading file!\n{}\n----------", e);
return false;
}
};
let mut reader = Reader::from_str(&file_data);
// 读取第一个
loop {
match reader.read_event() {
Ok(Event::Start(e)) => {
if e.name().as_ref() == b"Ship" {
// 再验证一下 version, liftedOff, touchingGround
let mut founds = (false, false, false);
for attr in e.attributes().flatten() {
match attr.key.as_ref() {
b"version" => {
founds.0 = true;
}
b"liftedOff" => {
founds.1 = true;
}
b"touchingGround" => {
founds.2 = true;
}
_ => (),
}
}
if !(founds.0 && founds.1 && founds.2) {
println!(
"warning: {}{}{} not found",
if founds.0 { "" } else { "version " },
if founds.1 { "" } else { "liftedOff " },
if founds.2 { "" } else { "touchingGround " }
);
return false;
} else {
return true;
}
}
}
Ok(Event::Eof) => {
println!("EOF");
return false;
}
Err(e) => {
println!("ERROR while using xml to parse the file!\n{:?}\n----------", e);
return false;
}
_ => (),
}
}
}

View File

@ -16,6 +16,7 @@ from lib_not_dr import loggers
# from pyglet.text import Label
# from . import DR_mod_runtime
from .render import render_hack_init
logger = loggers.config.get_logger_from_old("client.dr_game_menu", "client")
@ -50,6 +51,18 @@ class Menu(BaseScreen):
group=self.main_group,
)
self.magic_rust_test_button = OreuiButton(
x=100,
y=150,
width=150,
height=30,
text="一些魔法 rust 测试",
toggle_mode=False,
auto_release=True,
batch=self.main_batch,
group=self.main_group,
)
def on_release(x, y):
from .sr1_ship import SR1ShipEditor, SR1ShipSelecter
@ -60,7 +73,9 @@ class Menu(BaseScreen):
logger.info("added SR1_ship screen", tag="dr_game")
self.enter_ship_editor_button.set_handler("on_release", on_release)
self.magic_rust_test_button.set_handler("on_release", render_hack_init)
main_window.push_handlers(self.enter_ship_editor_button)
main_window.push_handlers(self.magic_rust_test_button)
def on_draw(self, window: ClientWindow):
self.main_batch.draw()

9
mods/dr_game/render.py Normal file
View File

@ -0,0 +1,9 @@
"""
温馨提示: 这里都是用来让 rust 接管的, 所以不用想别的了
"""
from .Difficult_Rocket_rs import render_hack
def render_hack_init(x, y):
render_hack()