feat: Translate

This commit is contained in:
shenjack 2023-04-08 02:14:06 +08:00
parent 94fb280d98
commit 04636e0979
6 changed files with 121 additions and 17 deletions

6
DR.py
View File

@ -28,7 +28,9 @@ def print_path() -> None:
print(f'{sys.path=}') print(f'{sys.path=}')
print(f'{sys.path[0]=}') print(f'{sys.path[0]=}')
print(f'{sys.argv[0]=}') print(f'{sys.argv[0]=}')
print(f'{os.curdir=}')
print(f'{os.getcwd()=}') print(f'{os.getcwd()=}')
print(f'{os.path.abspath(os.curdir)=}')
print(f'{os.path.abspath(__file__)=}') print(f'{os.path.abspath(__file__)=}')
print(f'{os.path.realpath(__file__)=}') print(f'{os.path.realpath(__file__)=}')
print(f'{os.path.split(os.path.split(os.path.realpath(__file__))[0])=}') print(f'{os.path.split(os.path.split(os.path.realpath(__file__))[0])=}')
@ -49,10 +51,10 @@ def main() -> None:
from Difficult_Rocket import crash from Difficult_Rocket import crash
from Difficult_Rocket import DR_option from Difficult_Rocket import DR_option
try: try:
from libs.pyglet_rs import get_version_str from libs.pyglet_rs import get_version_str, patch_vector
print('pyglet_rs available:', get_version_str()) print('pyglet_rs available:', get_version_str())
print('trying to patch pyglet_rs') print('trying to patch pyglet_rs')
from libs.pyglet_rs import patch_vector patch_vector()
except ImportError as e: except ImportError as e:
print('pyglet_rs import error') print('pyglet_rs import error')
traceback.print_exc() traceback.print_exc()

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View File

@ -22,7 +22,23 @@
- [![Readme-gitee](https://img.shields.io/badge/Readme-中文(点我!)-blue.svg?style=flat-square)](../../README.md) - [![Readme-gitee](https://img.shields.io/badge/Readme-中文(点我!)-blue.svg?style=flat-square)](../../README.md)
- Using [SemVer 2.0.0](https://semver.org/) to manage version - Using [SemVer 2.0.0](https://semver.org/) to manage version
## 202304 DR `0.7.2.1` ## 202304 DR `0.7.2.2` + DR_rs `0.2.7.0`
### DR_rs V 0.2.7.0
- `python::translate::Translate`
- 正在尝试用 Rust 重写一遍 `Difficult_Rocket.utils.translate`
- Trying to rewrite `Difficult_Rocket.utils.translate` in Rust
### Changes
- `DR.py`
- 向输出环境信息的地方加入 运行目录 信息
- Add running directory information to the place where the output environment information
- `os.curdir`
- `os.path.abspath(os.curdir)`
## 20230405 DR `0.7.2.1`
### Changes ### Changes
@ -218,6 +234,9 @@
### 内部协议 13 ### 内部协议 13
```python ```python
from collections import namedtuple
Version = namedtuple("Version", ["version"]) # 版本号 (用于通过 Pycharm 的检查)
game_version = Version("0.7.1.2") # 游戏版本 game_version = Version("0.7.1.2") # 游戏版本
build_version = Version("1.1.0.0") # 编译文件版本(与游戏本体无关) build_version = Version("1.1.0.0") # 编译文件版本(与游戏本体无关)
DR_rust_version = Version("0.2.5.3") # DR 的 Rust 编写部分的版本 DR_rust_version = Version("0.2.5.3") # DR 的 Rust 编写部分的版本
@ -783,7 +802,7 @@ long_version: 一个用于标记内部协议的整数
- `test_logging_conf.py` - `test_logging_conf.py`
- `test_speed_of_sprite.py` - `test_speed_of_sprite.py`
## 2021/06/26 V 0.4.3 ## 20210626 V 0.4.3
### DEBUG ### DEBUG
@ -799,7 +818,7 @@ long_version: 一个用于标记内部协议的整数
- add performance_test folder - add performance_test folder
- add some performances test - add some performances test
## 2021/05/24 V 0.4.2 ## 20210524 V 0.4.2
### DEBUG ### DEBUG
@ -815,7 +834,7 @@ long_version: 一个用于标记内部协议的整数
- debug name_format - debug name_format
## 2021/04/17 V 0.4.1 ## 20210417 V 0.4.1
PS: PS:
@ -836,7 +855,7 @@ PS:
- plan to change config file format to .config (plan to) - plan to change config file format to .config (plan to)
- reformat all files (including libs) - reformat all files (including libs)
## 2021/04/09 V 0.2.3/4 ## 20210409 V 0.2.3/4
### Add ### Add
@ -853,7 +872,7 @@ PS:
- `{date}` can be successful use in `tools.name_handler()` (if you define the format of date) - `{date}` can be successful use in `tools.name_handler()` (if you define the format of date)
- log file's filename incorrect (should be `xxxx-xx-xx xx-xx-xx DR.log` but be `{date} DR.log`) - log file's filename incorrect (should be `xxxx-xx-xx xx-xx-xx DR.log` but be `{date} DR.log`)
## 2021/03/27 V 0.2.2/1 ## 20210327 V 0.2.2/1
### Add ### Add

View File

@ -7,13 +7,14 @@
*/ */
pub mod data { pub mod data {
use pyo3::prelude::*;
use serde_xml_rs::from_str;
use std::collections::HashMap; use std::collections::HashMap;
use pyo3::prelude::*;
use crate::sr1_data::part_list::RawPartList; use crate::sr1_data::part_list::RawPartList;
use crate::sr1_data::ship::RawShip; use crate::sr1_data::ship::RawShip;
use crate::types::sr1::{SR1PartList, SR1PartListTrait, SR1PartType, SR1Ship}; use crate::types::sr1::{SR1PartList, SR1PartType, SR1Ship};
use crate::types::sr1::{SR1PartListTrait, SR1ShipTrait};
#[pyclass] #[pyclass]
#[pyo3(name = "SR1PartType_rs")] #[pyo3(name = "SR1PartType_rs")]
@ -77,10 +78,32 @@ 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,
}
#[pymethods]
impl PySR1Ship {
#[new]
fn new(file_path: String, part_list: String, ship_name: String) -> Self {
let raw_ship: RawShip = RawShip::from_file(file_path).unwrap();
let ship = raw_ship.to_sr_ship(Some(ship_name));
let part_list = SR1PartList::from_file(part_list).unwrap();
Self { ship, part_list }
}
}
} }
pub mod translate { pub mod translate {
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyDict;
#[pyclass] #[pyclass]
pub struct TranslateConfig { pub struct TranslateConfig {
@ -94,8 +117,8 @@ pub mod translate {
#[pyclass] #[pyclass]
pub struct Translate { pub struct Translate {
pub data: PyObject, pub data: Py<PyDict>,
pub get_list: Vec<(String, bool)>, pub get_list: Vec<(String, bool)>,
pub config: Config, pub config: TranslateConfig,
} }
} }

View File

@ -100,14 +100,17 @@ pub mod part_list {
} }
impl AttachPoints { impl AttachPoints {
#[inline]
pub fn new(attaches: Vec<AttachPoint>) -> Self { pub fn new(attaches: Vec<AttachPoint>) -> Self {
AttachPoints { points: attaches } AttachPoints { points: attaches }
} }
#[inline]
pub fn insert(&mut self, attach: AttachPoint) { pub fn insert(&mut self, attach: AttachPoint) {
self.points.push(attach); self.points.push(attach);
} }
#[inline]
pub fn unzip(&self) -> Vec<AttachPoint> { pub fn unzip(&self) -> Vec<AttachPoint> {
self.points.clone() self.points.clone()
} }
@ -124,6 +127,7 @@ pub mod part_list {
} }
impl Damage { impl Damage {
#[inline]
pub fn to_damage(&self) -> crate::types::sr1::Damage { pub fn to_damage(&self) -> crate::types::sr1::Damage {
crate::types::sr1::Damage { crate::types::sr1::Damage {
disconnect: self.disconnect, disconnect: self.disconnect,
@ -235,6 +239,7 @@ pub mod part_list {
} }
impl SR1PartTypeData for RawPartType { impl SR1PartTypeData for RawPartType {
#[inline]
fn to_sr_part_type(&self) -> SR1PartType { fn to_sr_part_type(&self) -> SR1PartType {
let part_attr: Option<SR1PartTypeAttr> = match self.r#type { let part_attr: Option<SR1PartTypeAttr> = match self.r#type {
SR1PartTypeEnum::tank => { SR1PartTypeEnum::tank => {
@ -347,6 +352,7 @@ pub mod part_list {
} }
impl SR1PartListTrait for RawPartList { impl SR1PartListTrait for RawPartList {
#[inline]
fn to_sr_part_list(&self, name: Option<String>) -> SR1PartList { fn to_sr_part_list(&self, name: Option<String>) -> SR1PartList {
let mut types: Vec<SR1PartType> = Vec::new(); let mut types: Vec<SR1PartType> = Vec::new();
for part_data in self.part_types.iter() { for part_data in self.part_types.iter() {
@ -360,6 +366,7 @@ pub mod part_list {
} }
} }
#[inline]
#[pyfunction] #[pyfunction]
#[pyo3(name = "part_list_read_test", signature = (file_name = "./configs/PartList.xml".to_string()))] #[pyo3(name = "part_list_read_test", signature = (file_name = "./configs/PartList.xml".to_string()))]
pub fn read_part_list_py(_py: Python, file_name: Option<String>) -> PyResult<()> { pub fn read_part_list_py(_py: Python, file_name: Option<String>) -> PyResult<()> {
@ -376,11 +383,14 @@ pub mod part_list {
#[allow(unused)] #[allow(unused)]
pub mod ship { pub mod ship {
use std::fs;
use pyo3::prelude::*; use pyo3::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// use quick_xml::de::from_str; // use quick_xml::de::from_str;
use serde_xml_rs::from_str; use serde_xml_rs::{expect, from_str};
use crate::types::sr1::{SR1Ship, SR1ShipTrait};
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RawShip { pub struct RawShip {
@ -518,7 +528,24 @@ pub mod ship {
pub child_part: i64, pub child_part: i64,
} }
// pub fn read_ship_from_file(file_name: String) -> Result<RawShip, String> { impl SR1ShipTrait for RawShip {
#[inline]
// } fn to_sr_ship(&self, name: Option<String>) -> SR1Ship {
todo!()
}
#[inline]
fn to_raw_ship(&self) -> RawShip {
self.clone()
}
}
impl RawShip {
#[inline]
pub fn from_file(path: String) -> Option<RawShip> {
let ship_file = fs::read_to_string(path).unwrap();
let ship: RawShip = from_str(&ship_file).unwrap();
Some(ship)
}
}
} }

View File

@ -8,12 +8,13 @@
pub mod sr1 { pub mod sr1 {
use std::collections::HashMap; use std::collections::HashMap;
// use super::math::{Shape, Point2D};
use crate::sr1_data::part_list::Damage as RawDamage; use crate::sr1_data::part_list::Damage as RawDamage;
use crate::sr1_data::part_list::{ use crate::sr1_data::part_list::{
AttachPoint, AttachPoints, Engine, Lander, Rcs, Shape as RawShape, Solar, Tank, AttachPoint, AttachPoints, Engine, Lander, Rcs, Shape as RawShape, Solar, Tank,
}; };
use crate::sr1_data::part_list::{RawPartList, RawPartType, SR1PartTypeEnum}; use crate::sr1_data::part_list::{RawPartList, RawPartType, SR1PartTypeEnum};
use crate::sr1_data::ship::RawShip;
#[inline] #[inline]
pub fn map_ptype_textures(ptype: String) -> String { pub fn map_ptype_textures(ptype: String) -> String {
@ -212,6 +213,14 @@ pub mod sr1 {
} }
impl SR1PartList { impl SR1PartList {
pub fn from_file(file_name: String) -> Option<SR1PartList> {
if let Some(raw_list) = RawPartList::from_file(file_name) {
return Some(raw_list.to_sr_part_list(None));
}
None
}
#[inline]
pub fn get_hash_map(&mut self) -> HashMap<String, SR1PartType> { pub fn get_hash_map(&mut self) -> HashMap<String, SR1PartType> {
if let Some(map) = &self.cache { if let Some(map) = &self.cache {
return map.clone(); return map.clone();
@ -235,6 +244,11 @@ pub mod sr1 {
fn to_raw_part_list(&self) -> RawPartList; fn to_raw_part_list(&self) -> RawPartList;
} }
pub trait SR1ShipTrait {
fn to_sr_ship(&self, name: Option<String>) -> SR1Ship;
fn to_raw_ship(&self) -> RawShip;
}
impl SR1PartList { impl SR1PartList {
#[inline] #[inline]
pub fn new(name: String, types: Vec<SR1PartType>) -> Self { pub fn new(name: String, types: Vec<SR1PartType>) -> Self {
@ -402,6 +416,7 @@ pub mod sr1 {
} }
} }
#[derive(Debug, Clone)]
pub struct SR1Ship { pub struct SR1Ship {
pub name: String, pub name: String,
pub description: String, pub description: String,
@ -410,6 +425,24 @@ pub mod sr1 {
pub lift_off: bool, pub lift_off: bool,
pub touch_ground: bool, pub touch_ground: bool,
} }
impl SR1ShipTrait for SR1Ship {
#[inline]
fn to_sr_ship(&self, name: Option<String>) -> SR1Ship {
if let Some(name) = name {
let mut dupe = self.clone();
dupe.name = name;
dupe
} else {
self.clone()
}
}
#[inline]
fn to_raw_ship(&self) -> RawShip {
todo!() // 1145行的内容
}
}
} }
#[allow(unused)] #[allow(unused)]