Mercurial > touhou
annotate python/src/lib.rs @ 781:5b43c42fa680
Stop exposing PBG3 to Python
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Sun, 09 Nov 2025 19:50:35 +0100 |
| parents | ee09657d3789 |
| children | a30ce01b9154 |
| rev | line source |
|---|---|
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
1 use pyo3::exceptions::{PyIOError, PyKeyError}; |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 use pyo3::prelude::*; |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
3 use pyo3::types::{PyBytes, PyTuple}; |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 use touhou_formats::th06::pbg3; |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
5 use touhou_formats::th06::std as stage; |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
6 use std::collections::HashMap; |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 use std::fs::File; |
|
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 use std::io::BufReader; |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
9 use std::path::PathBuf; |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
10 use std::sync::{Arc, Mutex}; |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 |
|
772
7492d384d122
Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
770
diff
changeset
|
12 #[cfg(feature = "glide")] |
|
7492d384d122
Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
770
diff
changeset
|
13 mod glide; |
|
7492d384d122
Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
770
diff
changeset
|
14 |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
15 #[pyclass(module = "libtouhou")] |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
16 struct PyModel { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
17 inner: stage::Model, |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
18 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
19 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
20 #[pymethods] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
21 impl PyModel { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
22 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
23 fn quads(&self) -> Vec<(u16, f32, f32, f32, f32, f32)> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
24 self.inner.quads.iter().map(|quad| (quad.anm_script, quad.pos.x, quad.pos.y, quad.pos.z, quad.size_override.width, quad.size_override.height)).collect() |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
25 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
26 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
27 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
28 fn bounding_box(&self) -> [f32; 6] { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
29 self.inner.bounding_box |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
30 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
31 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
32 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
33 #[pyclass(module = "libtouhou")] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
34 struct PyStage { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
35 inner: stage::Stage, |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
36 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
37 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
38 #[pymethods] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
39 impl PyStage { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
40 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
41 fn models(&self) -> Vec<PyModel> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
42 self.inner.models.clone().into_iter().map(|inner| PyModel { inner }).collect() |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
43 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
44 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
45 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
46 fn object_instances(&self) -> Vec<(u16, f32, f32, f32)> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
47 self.inner.instances.iter().map(|instance| (instance.id, instance.pos.x, instance.pos.y, instance.pos.z)).collect() |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
48 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
49 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
50 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
51 fn name(&self) -> &str { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
52 &self.inner.name |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
53 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
54 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
55 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
56 fn bgms(&self) -> Vec<Option<(String, String)>> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
57 self.inner.musics.clone() |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
58 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
59 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
60 #[getter] |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
61 fn script(&self, py: Python) -> Vec<(u32, u16, Py<PyTuple>)> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
62 fn call_to_python(py: Python, call: &stage::Call) -> PyResult<(u32, u16, Py<PyTuple>)> { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
63 let (opcode, args) = match call.instr { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
64 stage::Instruction::SetViewpos(x, y, z) => (0, (x, y, z).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
65 stage::Instruction::SetFog(r, g, b, _a, near, far) => (1, (r, g, b, near, far).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
66 stage::Instruction::SetViewpos2(x, y, z) => (2, (x, y, z).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
67 stage::Instruction::StartInterpolatingViewpos2(frame, _unused1, _unused2) => (3, (frame,).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
68 stage::Instruction::StartInterpolatingFog(frame, _unused1, _unused2) => (4, (frame,).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
69 stage::Instruction::Unknown(unused1, unused2, unused3) => (5, (unused1, unused2, unused3).into_pyobject(py)?.unbind()), |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
70 }; |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
71 Ok((call.time, opcode, args)) |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
72 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
73 |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
74 self.inner.script.iter().map(|call| call_to_python(py, call).unwrap()).collect() |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
75 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
76 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
77 |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
78 /// A loader for Touhou files. |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
79 #[pyclass(module = "libtouhou", subclass)] |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
80 #[derive(Default)] |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
81 struct Loader { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
82 /// The file names to the possible executable. |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
83 #[pyo3(get)] |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
84 exe_files: Vec<PathBuf>, |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
85 |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
86 /// The path to the game directory. |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
87 #[pyo3(get)] |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
88 game_dir: Option<PathBuf>, |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
89 |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
90 /// A map from inner filenames to the archive containing them. |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
91 known_files: HashMap<String, Arc<Mutex<pbg3::PBG3<BufReader<File>>>>>, |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
92 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
93 |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
94 #[pymethods] |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
95 impl Loader { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
96 /// Create a new Loader for the given game_dir. |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
97 #[new] |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
98 fn new(game_dir: Option<PathBuf>) -> Loader { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
99 Loader { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
100 exe_files: Vec::new(), |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
101 game_dir, |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
102 known_files: HashMap::new(), |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
103 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
104 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
105 |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
106 /// Scan the game_dir for archives. |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
107 /// |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
108 /// paths_lists is a list of ':'-separated glob patterns, the first matching file will be used |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
109 /// and the other ones ignored. |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
110 fn scan_archives(&mut self, paths_lists: Vec<String>) -> PyResult<()> { |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
111 for paths in paths_lists.iter() { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
112 let found_paths: Vec<_> = paths.split(':').map(|path| { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
113 glob::glob(if let Some(game_dir) = self.game_dir.as_ref() { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
114 game_dir.join(path) |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
115 } else { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
116 PathBuf::from(path) |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
117 }.to_str().unwrap()).unwrap() |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
118 }).flatten().map(Result::unwrap).map(PathBuf::from).collect(); |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
119 if found_paths.is_empty() { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
120 return Err(PyIOError::new_err(format!("No path found for {paths:?}"))); |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
121 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
122 let path = &found_paths[0]; |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
123 if let Some(extension) = path.extension() && extension == "exe" { |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
124 self.exe_files.extend(found_paths); |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
125 } else { |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
126 let pbg3 = pbg3::from_path_buffered(path)?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
127 let filenames: Vec<_> = pbg3.list_files().cloned().collect(); |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
128 let pbg3 = Arc::new(Mutex::new(pbg3)); |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
129 for name in filenames { |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
130 self.known_files.insert(name.clone(), Arc::clone(&pbg3)); |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
131 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
132 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
133 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
134 Ok(()) |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
135 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
136 |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
137 /// Return the given file as a Vec<u8>. |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
138 fn get_file_internal(&self, name: &str) -> PyResult<Vec<u8>> { |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
139 if let Some(archive) = self.known_files.get(name) { |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
140 let mut archive = archive.lock().unwrap(); |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
141 let bytes = archive.get_file(name, true)?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
142 Ok(bytes) |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
143 } else { |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
144 Err(PyKeyError::new_err(format!("Unknown file {name:?}"))) |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
145 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
146 } |
|
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
147 |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
148 /// Return the given file as an io.BytesIO object. |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
149 fn get_file(&self, py: Python, name: String) -> PyResult<Py<PyAny>> { |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
150 let vec = self.get_file_internal(&name)?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
151 let bytes = PyBytes::new(py, &vec); |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
152 let io = py.import("io")?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
153 let bytesio_class = io.dict().get_item("BytesIO")?.unwrap(); |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
154 let file = bytesio_class.call1((bytes,))?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
155 Ok(file.unbind()) |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
156 } |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
157 |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
158 fn get_stage(&self, py: Python, name: String) -> PyResult<Py<PyStage>> { |
|
781
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
159 let vec = self.get_file_internal(&name)?; |
|
5b43c42fa680
Stop exposing PBG3 to Python
Link Mauve <linkmauve@linkmauve.fr>
parents:
779
diff
changeset
|
160 let (_, inner) = stage::Stage::from_slice(&vec).unwrap(); |
|
779
ee09657d3789
Replace the stage parser with the Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
778
diff
changeset
|
161 Ok(Py::new(py, PyStage { inner })?) |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
162 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
163 } |
|
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
164 |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
165 #[pymodule] |
|
775
28d8b892fd06
Python: Upgrade pyo3 from 0.17 to 0.26
Link Mauve <linkmauve@linkmauve.fr>
parents:
772
diff
changeset
|
166 mod libtouhou { |
|
28d8b892fd06
Python: Upgrade pyo3 from 0.17 to 0.26
Link Mauve <linkmauve@linkmauve.fr>
parents:
772
diff
changeset
|
167 #[pymodule_export] |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
775
diff
changeset
|
168 use super::Loader; |
|
775
28d8b892fd06
Python: Upgrade pyo3 from 0.17 to 0.26
Link Mauve <linkmauve@linkmauve.fr>
parents:
772
diff
changeset
|
169 |
|
772
7492d384d122
Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
770
diff
changeset
|
170 #[cfg(feature = "glide")] |
|
775
28d8b892fd06
Python: Upgrade pyo3 from 0.17 to 0.26
Link Mauve <linkmauve@linkmauve.fr>
parents:
772
diff
changeset
|
171 #[pymodule_export] |
|
28d8b892fd06
Python: Upgrade pyo3 from 0.17 to 0.26
Link Mauve <linkmauve@linkmauve.fr>
parents:
772
diff
changeset
|
172 use super::glide::module; |
|
770
f6c287745a67
Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
173 } |
