From 0ad0124b5507202e2731da5a87d84adc1ea763ab Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 15 May 2020 19:19:31 +0200 Subject: [PATCH] python: faulty unpackers::bnd bindings It would require to add an implementation to Bnd directly to convert it to a Python class. Committing for history but gonna revert it before merge. --- bindings/python/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index c2806f8..7a17519 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -1,8 +1,20 @@ use std::collections::HashMap; use pyo3::wrap_pymodule; +use pyo3::exceptions::RuntimeError; use pyo3::prelude::*; +fn runtime_error(message: String) -> PyErr { + RuntimeError::py_err(message) +} + +fn wrap_ironring_errors(result: Result) -> PyResult { + match result { + Ok(r) => Ok(r), + Err(e) => Err(runtime_error(format!("{:?}", e))), + } +} + #[pymodule] fn name_hashes(_py: Python, m: &PyModule) -> PyResult<()> { use ironring::name_hashes::*; @@ -25,8 +37,27 @@ fn name_hashes(_py: Python, m: &PyModule) -> PyResult<()> { Ok(()) } +#[pymodule] +fn unpack_bnd(_py: Python, m: &PyModule) -> PyResult<()> { + use ironring::unpackers::bnd::*; + use ironring::parsers::bnd::Bnd; + + #[pyfn(m, "load_bnd_file")] + fn py_load_bnd_file(_py: Python, bnd_path: &str) -> PyResult<(Bnd, Vec)> { + wrap_ironring_errors(load_bnd_file(bnd_path)) + } + + #[pyfn(m, "load_bnd")] + fn py_load_bnd(_py: Python, bnd_data: &[u8]) -> PyResult { + wrap_ironring_errors(load_bnd(bnd_data)) + } + + Ok(()) +} + #[pymodule] fn pyironring(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pymodule!(name_hashes))?; + m.add_wrapped(wrap_pymodule!(unpack_bnd))?; Ok(()) }