From 5322b28c1abb9f2cbe23f1a9ae6641bd8612c30b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 20 Feb 2023 05:31:39 +0100 Subject: [PATCH] Rework(module) - rework for even more modularity --- .github/workflows/CI.yml | 90 ++++++++++++++++++++++ .gitignore | 155 +++++++++++++++++++++++++++++++++++++ Cargo.lock | 156 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 18 ++++- pyproject.toml | 14 ++++ src/main.rs | 23 ++---- src/{libs.rs => utils.rs} | 23 +++--- src/{ => utils}/action.rs | 6 +- src/{ => utils}/craft.rs | 6 +- src/{ => utils}/solver.rs | 6 +- src/{ => utils}/specs.rs | 0 11 files changed, 460 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/CI.yml create mode 100644 pyproject.toml rename src/{libs.rs => utils.rs} (96%) rename src/{ => utils}/action.rs (98%) rename src/{ => utils}/craft.rs (96%) rename src/{ => utils}/solver.rs (98%) rename src/{ => utils}/specs.rs (100%) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..d0670e0 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,90 @@ +on: + push: + branches: + - main + - master + pull_request: + workflow_dispatch: + +jobs: + linux: + runs-on: ubuntu-latest + strategy: + matrix: + target: [x86_64, x86, aarch64, armv7, s390x, ppc64le] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + manylinux: auto + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + windows: + runs-on: windows-latest + strategy: + matrix: + target: [x64, x86] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + architecture: ${{ matrix.target }} + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + macos: + runs-on: macos-latest + strategy: + matrix: + target: [x86_64, aarch64] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + release: + name: Release + runs-on: ubuntu-latest + if: "startsWith(github.ref, 'refs/tags/')" + needs: [linux, windows, macos] + steps: + - uses: actions/download-artifact@v3 + with: + name: wheels + - name: Publish to PyPI + uses: PyO3/maturin-action@v1 + env: + MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} + with: + command: upload + args: --skip-existing * diff --git a/.gitignore b/.gitignore index 15bdd96..495887a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,158 @@ /target /.idea *.iml + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 10defab..9d5c6dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -14,6 +20,12 @@ version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.1.6" @@ -93,6 +105,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "indoc" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" + [[package]] name = "io-lifetimes" version = "1.0.5" @@ -133,6 +151,25 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memoffset" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -155,6 +192,29 @@ version = "6.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -188,6 +248,66 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pyo3" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06a3d8e8a46ab2738109347433cb7b96dffda2e4a218b03ef27090238886b147" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75439f995d07ddfad42b192dfcf3bc66a7ecfd8b4a1f5f6f046aa5c2c5d7677d" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "839526a5c07a17ff44823679b68add4a58004de00512a95b6c1c98a6dcac0ee5" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd44cf207476c6a9760c4653559be4f206efafb924d3e4cbf2721475fc0d6cc5" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1f43d8e30460f36350d18631ccf85ded64c059829208fe680904c65bcd0a4c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.21" @@ -197,6 +317,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "rustix" version = "0.36.8" @@ -217,12 +346,24 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "serde" version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + [[package]] name = "strsim" version = "0.10.0" @@ -262,6 +403,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "target-lexicon" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae9980cab1db3fceee2f6c6f643d5d8de2997c58ee8d25fb0cc8a9e9e7348e5" + [[package]] name = "termcolor" version = "1.2.0" @@ -295,6 +442,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" +[[package]] +name = "unindent" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" + [[package]] name = "version_check" version = "0.9.4" @@ -399,11 +552,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" [[package]] -name = "xiv_craft_simulator" +name = "xiv_craft_solver" version = "0.1.2" dependencies = [ "clap", "lazy_static", + "pyo3", "strum", "strum_macros", "threadpool", diff --git a/Cargo.toml b/Cargo.toml index 1710e78..cce3c8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "xiv_craft_simulator" +name = "xiv_craft_solver" version = "0.1.2" edition = "2021" authors = ["谭俊健","Dandaedre"] @@ -14,8 +14,22 @@ lazy_static = "1.4.0" toml = "0.5.9" clap = {version = "4.1.6", features = ["derive"]} threadpool = "1.8.1" +# For python bindings TODO: make it optionnal +pyo3 = { version = "0.18.0", features = ["extension-module"] } [profile.release] panic = 'abort' opt-level = 3 -lto = true \ No newline at end of file +lto = true + +# [lib] +# name = "xiv_craft_solver" +# path = "src/lib/libs.rs" +# crate-type = ["cdylib"] + +# [[bin]] +# name = "xiv_craft_solver" +# path = "src/main.rs" + +[tool.maturin] +python-source = "python" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..538e4e2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[build-system] +requires = ["maturin>=0.14,<0.15"] +build-backend = "maturin" + +[project] +name = "ffxiv" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Rust", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] + + diff --git a/src/main.rs b/src/main.rs index bbf090c..5a9f784 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,22 @@ // #![warn(missing_docs,unsafe_code,unstable_features,)] -use crate::libs::Parameters; -use crate::libs::load_from_config; +#[allow(dead_code)] + use clap::Parser; -use crate::libs::Args; -use crate::craft::Craft; -use crate::specs::{Recipe, Stats}; - - -mod specs; -pub mod action; -pub mod craft; -mod solver; -pub mod libs; +mod utils; #[derive(Debug)] struct CustomError(String); fn main() { - let args = Args::parse(); - let params = Parameters{ + let args = utils::Args::parse(); + let params = utils::Parameters{ depth: args.depth, threads: args.threads, verbose: args.verbose, }; - let (recipe,stats) = load_from_config(&args.recipe_name, &args.file_name, &args.character_name); - libs::solve_craft(recipe,stats,params); + let (recipe,stats) = utils::load_from_config(&args.recipe_name, &args.file_name, &args.character_name); + utils::solve_craft(recipe,stats,params); () } diff --git a/src/libs.rs b/src/utils.rs similarity index 96% rename from src/libs.rs rename to src/utils.rs index abc1b32..4381ecd 100644 --- a/src/libs.rs +++ b/src/utils.rs @@ -1,14 +1,17 @@ -use std::sync::{Arc,Mutex}; -use crate::solver::{generate_routes_phase1, generate_routes_phase2}; +use crate::utils::{ + craft::Craft, + specs::{Recipe,Stats}, +}; use std::time::{Instant}; use threadpool::ThreadPool; -use crate::Recipe; -use crate::Craft; -use crate::Stats; +use std::sync::{Arc,Mutex}; use clap::Parser; -// use crate::Stats; -// use crate::Recipe; -// use crate::Craft; + +mod solver; +mod specs; +pub mod action; +pub mod craft; + #[derive(Debug, Clone, Copy)] pub struct Parameters { @@ -59,7 +62,7 @@ pub fn solve_craft<'a>(recipe: Recipe, stats: Stats, params: Parameters) -> Opti println!("Solving...\n"); println!("[P1] Starting phase 1..."); } - let phase1_routes = generate_routes_phase1(craft); + let phase1_routes = solver::generate_routes_phase1(craft); if params.verbose>0{ println!("[P1] Found {} routes, testing them all...",phase1_routes.len()); @@ -82,7 +85,7 @@ pub fn solve_craft<'a>(recipe: Recipe, stats: Stats, params: Parameters) -> Opti let _phase2_routes = Arc::clone(&arc_phase2_routes); pool.execute(move || { - if let Some(_route) = generate_routes_phase2(route){ + if let Some(_route) = solver::generate_routes_phase2(route){ let mut shared = _phase2_routes.lock().unwrap(); shared.push(_route); }; diff --git a/src/action.rs b/src/utils/action.rs similarity index 98% rename from src/action.rs rename to src/utils/action.rs index 92b9b43..cb2089c 100644 --- a/src/action.rs +++ b/src/utils/action.rs @@ -1,7 +1,9 @@ +#[allow(dead_code)] + use std::fmt::Debug; use lazy_static::lazy_static; -use crate::craft::Craft; -use crate::specs::{Buff, Success}; +use crate::utils::craft::Craft; +use crate::utils::specs::{Buff, Success}; pub struct Action { pub name: String, diff --git a/src/craft.rs b/src/utils/craft.rs similarity index 96% rename from src/craft.rs rename to src/utils/craft.rs index c4055f3..0773c57 100644 --- a/src/craft.rs +++ b/src/utils/craft.rs @@ -1,7 +1,7 @@ use std::fmt::{Debug, Formatter,Display}; -use crate::action::Action; -use crate::specs::{Buff, BuffState, Recipe, Stats, Success}; -use crate::libs::Parameters; +use crate::utils::action::Action; +use crate::utils::specs::{Buff, BuffState, Recipe, Stats, Success}; +use crate::utils::Parameters; #[derive(Clone)] diff --git a/src/solver.rs b/src/utils/solver.rs similarity index 98% rename from src/solver.rs rename to src/utils/solver.rs index 2730936..7ac92bb 100644 --- a/src/solver.rs +++ b/src/utils/solver.rs @@ -1,8 +1,8 @@ use std::collections::VecDeque; use std::ops::Not; -use crate::action::{Action, ACTIONS}; -use crate::craft::Craft; -use crate::specs::Success; +use crate::utils::action::{Action, ACTIONS}; +use crate::utils::craft::Craft; +use crate::utils::specs::Success; macro_rules! action_vec { ($($tt:expr),*) => { vec![ $(Some(&$tt),)*]}; diff --git a/src/specs.rs b/src/utils/specs.rs similarity index 100% rename from src/specs.rs rename to src/utils/specs.rs