pyo3 的文档看着还挺舒服的

This commit is contained in:
shenjack 2023-01-23 23:46:43 +08:00
parent 281e5df555
commit a591ad9dbd
9 changed files with 191 additions and 57 deletions

View File

@ -21,7 +21,7 @@ class SR1PartData:
x: float
y: float
id: int
type: str
type_: str
active: bool
angle: float
angle_v: float
@ -29,7 +29,7 @@ class SR1PartData:
flip_x: bool
flip_y: bool
explode: bool
textures: Optional[str]
textures: Optional[str] = None
connections: Optional[List[int]] = None

View File

@ -55,7 +55,7 @@ def get_sr1_part(part_xml: Element) -> Optional[SR1PartData]:
# f'angle: {part_angle} angle_v: {part_angle_v} editor_angle: {part_editor_angle} '
# f'flip_x: {part_flip_x} flip_y: {part_flip_y} explode: {part_explode} '
# f'textures: {SR1PartTexture.get_textures_from_type(part_type)}')
part_data = SR1PartData(x=part_x, y=part_y, id=part_id, type=part_type,
part_data = SR1PartData(x=part_x, y=part_y, id=part_id, type_=part_type,
active=part_activate, angle=part_angle, angle_v=part_angle_v,
editor_angle=part_editor_angle, flip_x=part_flip_x,
flip_y=part_flip_y, explode=part_explode, textures=part_textures)
@ -173,6 +173,8 @@ class SR1ShipRender(BaseScreen):
self.rendered = True
def update_parts(self) -> bool:
if DR_option.DR_rust_available:
return better_update_parts(self, SR1ShipRender_Option, self.window_pointer)
if not self.rendered:
return False
self.debug_line.x2, self.debug_line.y2 = self.dx + (self.window_pointer.width / 2), self.dy + (self.window_pointer.height / 2)

View File

@ -1,6 +1,6 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
@ -9,6 +9,7 @@ from .lib import *
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from Difficult_Rocket.client.screen import BaseScreen
from Difficult_Rocket.client.render.sr1_ship import SR1ShipRender, SR1ShipRender_Option
def sum_as_string(a: int, b: int) -> int: ...
@ -16,4 +17,4 @@ if TYPE_CHECKING:
def test_call(py_obj) -> bool: ...
def better_update_parts(render: SR1ShipRender, option: SR1ShipRender_Option) -> bool: ...
def better_update_parts(render: SR1ShipRender, option: SR1ShipRender_Option, window: BaseScreen) -> bool: ...

View File

@ -0,0 +1,44 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
import os
import shutil
import warnings
import traceback
package_path = 'Difficult_Rocket_rs'
lib_path = '../lib'
build_path = 'build'
if not os.path.exists(lib_path):
os.mkdir(lib_path)
builds = os.listdir(build_path)
print(os.path.abspath('.'))
try:
shutil.copy('src/__init__.py', os.path.join(lib_path, '__init__.py'))
except shutil.SameFileError:
traceback.print_exc()
for build_dir in builds:
if not os.path.exists(os.path.join(build_path, build_dir, package_path)):
warnings.warn(f'package not found at {build_path}/{build_dir}')
continue
for file in os.listdir(os.path.join(build_path, build_dir, package_path)):
# file_name = os.path.join(lib_path, file.replace(package_path, f'{package_path}.{DR_runtime.DR_Rust_version}'))
file_name = os.path.join(lib_path, file)
shutil.rmtree(file_name, ignore_errors=True)
try:
shutil.copy(os.path.join(build_path, build_dir, package_path, file), file_name)
except (shutil.SameFileError, PermissionError):
# print(os.path.exists(os.path))
print(os.listdir(lib_path))
traceback.print_exc()
continue
# os.remove(file_name)
# shutil.copy(os.path.join(build_path, build_dir, package_path, file), file_name)
# shutil.rmtree(os.path.join(build_path, build_dir))
# print(os.path.join(build_path, build_dir))

View File

@ -3,11 +3,8 @@
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
import os
import sys
import shutil
import warnings
import traceback
from setuptools import setup
from setuptools_rust import Binding, RustExtension
@ -25,7 +22,7 @@ setup(
rust_extensions=[RustExtension(target="Difficult_Rocket_rs.Difficult_Rocket_rs",
# rust_version='2021',
binding=Binding.PyO3)],
zip_safe=False
zip_safe=False,
)
lib_path = '../lib'
@ -36,34 +33,3 @@ if 'clean' in sys.argv:
shutil.rmtree(build_path, ignore_errors=True)
shutil.rmtree(f'{package_path}.egg-info', ignore_errors=True)
sys.exit(0)
if not os.path.exists(lib_path):
os.mkdir(lib_path)
builds = os.listdir(build_path)
print(os.path.abspath('.'))
try:
shutil.copy('src/__init__.py', os.path.join(lib_path, '__init__.py'))
except shutil.SameFileError:
traceback.print_exc()
for build_dir in builds:
if not os.path.exists(os.path.join(build_path, build_dir, package_path)):
warnings.warn(f'package not found at {build_path}/{build_dir}')
continue
for file in os.listdir(os.path.join(build_path, build_dir, package_path)):
# file_name = os.path.join(lib_path, file.replace(package_path, f'{package_path}.{DR_runtime.DR_Rust_version}'))
file_name = os.path.join(lib_path, file)
shutil.rmtree(file_name, ignore_errors=True)
try:
shutil.copy(os.path.join(build_path, build_dir, package_path, file), file_name)
except (shutil.SameFileError, PermissionError):
# print(os.path.exists(os.path))
print(os.listdir(lib_path))
traceback.print_exc()
continue
os.remove(file_name)
shutil.copy(os.path.join(build_path, build_dir, package_path, file), file_name)
# shutil.rmtree(os.path.join(build_path, build_dir))
# print(os.path.join(build_path, build_dir))

View File

@ -1,6 +1,6 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------

View File

@ -1,4 +1,14 @@
use pyo3::intern;
/*
* -------------------------------
* Difficult Rocket
* Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
* All rights reserved
* -------------------------------
*/
mod sr1_render;
mod types;
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
@ -13,20 +23,6 @@ fn test_call(py_obj: &PyAny) -> PyResult<bool> {
Ok(true)
}
#[pyfunction]
#[allow(non_snake_case)]
fn better_update_parts(render: &PyAny, option: &PyAny) -> PyResult<bool> {
if !render.getattr(intern!(render.py(), "rendered")).unwrap().is_true().unwrap() {
// Ok(false);
return Ok(false);
// println!("aaaaa");
}
if option.getattr("debug_d_pos").unwrap().is_true().unwrap() {
let line = render.getattr("debug_line").unwrap();
// line.
}
Ok(true)
}
#[pyfunction]
fn for_x_in_range(a: usize, b: usize) -> PyResult<()> {
@ -46,6 +42,7 @@ fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
m.add_function(wrap_pyfunction!(for_x_in_range, m)?)?;
m.add_function(wrap_pyfunction!(test_call, m)?)?;
m.add_function(wrap_pyfunction!(better_update_parts, m)?)?;
m.add_function(wrap_pyfunction!(sr1_render::better_update_parts, m)?)?;
m.add_class::<sr1_render::types::PartDatas>()?;
Ok(())
}

View File

@ -0,0 +1,117 @@
/*
* -------------------------------
* Difficult Rocket
* Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
* All rights reserved
* -------------------------------
*/
use std::collections::HashMap;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use crate::sr1_render::types::Point;
pub mod types {
use std::collections::HashMap;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::iter::PyDictIterator;
use pyo3::types::PyDict;
pub struct SR1PartData {
pub x: f64,
pub y: f64,
pub id: usize,
pub type_: String,
pub active: bool,
pub angle: f64,
pub angle_v: f64,
pub editor_angle: usize,
pub flip_x: bool,
pub flip_y: bool,
pub explode: bool,
pub textures: String,
pub connections: Vec<usize>
}
pub struct Point {
pub x: f64,
pub y: f64,
pub id: usize,
pub type_: String
}
#[pyclass]
pub struct PartDatas {
pub part_structs: HashMap<usize, SR1PartData>
}
#[pymethods]
impl PartDatas {
#[new]
pub fn py_new(py_part_data: &PyDict) -> PyResult<Self>{
let mut
}
}
#[allow(dead_code)]
pub fn convert_py_any_sr1_part_data(input: &PyAny) -> Result<SR1PartData, PyErr> {
return Ok(SR1PartData{
x: input.getattr(intern!(input.py(), "x"))?.extract()?,
y: input.getattr(intern!(input.py(), "y"))?.extract()?,
id: input.getattr(intern!(input.py(), "id"))?.extract()?,
type_: input.getattr(intern!(input.py(), "type_"))?.extract()?,
active: input.getattr(intern!(input.py(), "active"))?.extract()?,
angle: input.getattr(intern!(input.py(), "angle"))?.extract()?,
angle_v: input.getattr(intern!(input.py(), "angle_v"))?.extract()?,
editor_angle: input.getattr(intern!(input.py(), "editor_angle"))?.extract()?,
flip_x: input.getattr(intern!(input.py(), "flip_x"))?.extract()?,
flip_y: input.getattr(intern!(input.py(), "flip_y"))?.extract()?,
explode: input.getattr(intern!(input.py(), "explode"))?.extract()?,
textures: input.getattr(intern!(input.py(), "textures"))?.extract()?,
connections: input.getattr(intern!(input.py(), "connections"))?.extract()?,
})
}
pub fn get_point_from_sr1_part_data(input: &PyAny) -> Result<Point, PyErr> {
return Ok(Point{
x: input.getattr(intern!(input.py(), "x"))?.extract()?,
y: input.getattr(intern!(input.py(), "y"))?.extract()?,
id: input.getattr(intern!(input.py(), "id"))?.extract()?,
type_: input.getattr(intern!(input.py(), "type_"))?.extract()?
})
}
pub fn point_dict_from_part_datas(input: &PyDict) -> Result<HashMap<usize, Point>, PyErr> {
let mut result: HashMap<usize, Point> = HashMap::with_capacity(input.len());
for key in &input.iter() {
key[]
}
return Ok(result);
}
}
#[pyfunction]
#[allow(non_snake_case)]
pub fn better_update_parts(render: &PyAny, option: &PyAny, window: &PyAny) -> PyResult<bool> {
if !render.getattr(intern!(render.py(), "rendered"))?.is_true()? {
return Ok(false);
}
let dx: usize = render.getattr(intern!(render.py(), "dx"))?.extract()?;
let dy: usize = render.getattr(intern!(render.py(), "dy"))?.extract()?;
let x_center: usize = window.getattr(intern!(window.py(), "width"))?.extract()?;
let y_center: usize = window.getattr(intern!(window.py(), "height"))?.extract()?;
let x_center: usize = x_center / 2;
let y_center: usize = y_center / 2;
let part_datas: &PyDict = render.getattr(intern!(render.py(), "part_data"))?.extract()?;
let parts: HashMap<usize, Point> = types::point_dict_from_part_datas(part_datas)?;
if option.getattr("debug_d_pos")?.is_true()? {
let line = render.getattr(intern!(render.py(), "debug_line"))?;
}
Ok(true)
}

View File

@ -0,0 +1,7 @@
/*
* -------------------------------
* Difficult Rocket
* Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
* All rights reserved
* -------------------------------
*/