Support pickle dump DamageResult

This commit is contained in:
omg-xtao 2024-08-23 06:18:47 +00:00 committed by GitHub
parent 87f63527cf
commit a19777bbd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View File

@ -33,6 +33,7 @@ serde="1.0"
serde_json = "1.0"
anyhow = "1.0"
pythonize = "0.20.0"
bincode = "1.3.3"
[features]
default = ["pyo3/extension-module"]

View File

@ -1,9 +1,12 @@
use mona::damage::damage_result::DamageResult as MonaDamageResult;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyBytes, PyDict};
#[pyclass(name = "DamageResult")]
#[derive(Clone)]
use serde::{Serialize, Deserialize};
use bincode::{serialize, deserialize};
#[pyclass(module = "python_genshin_artifact", name = "DamageResult")]
#[derive(Clone, Deserialize, Serialize)]
pub struct PyDamageResult {
#[pyo3(get, set)]
pub critical: f64,
@ -53,6 +56,25 @@ impl PyDamageResult {
dict.set_item("is_shield", self.is_shield)?;
Ok(dict.into())
}
pub fn __setstate__(&mut self, state: &PyBytes) -> PyResult<()> {
*self = deserialize(state.as_bytes()).unwrap();
Ok(())
}
pub fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult<&'py PyBytes> {
Ok(PyBytes::new(py, &serialize(&self).unwrap()))
}
pub fn __getnewargs__(&self) -> PyResult<(f64, f64, f64, bool, bool)> {
Ok((
self.critical,
self.non_critical,
self.expectation,
self.is_heal,
self.is_shield,
))
}
}
impl From<MonaDamageResult> for PyDamageResult {