From 52811fe2eb2c6a6654bae019570309c53cb31ce8 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 2 Jun 2025 22:31:46 +0800 Subject: [PATCH] Add features for each script type --- .github/workflows/CI.yml | 21 ++++++++++++++ Cargo.toml | 6 ++++ check_features.py | 63 ++++++++++++++++++++++++++++++++++++++++ src/format/fixed.rs | 6 ++++ src/scripts/mod.rs | 7 +++++ src/types.rs | 4 +++ 6 files changed, 107 insertions(+) create mode 100644 .github/workflows/CI.yml create mode 100644 check_features.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..c6bb1dd --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,21 @@ +name: CI +on: + push: + branches: + - master + pull_request: + branches: + - master + +env: + CARGO_TERM_COLOR: always + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install toml + run: pip3 install toml + - name: Check + run: python check_features.py \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 120f726..cb8d90b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,11 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" unicode-segmentation = "1.12" +[features] +default = ["bgi", "circus", "escude"] +bgi = [] +circus = [] +escude = [] + [target.'cfg(windows)'.dependencies] windows-sys = { version = "0", features = ["Win32_Globalization", "Win32_System_Diagnostics_Debug"] } diff --git a/check_features.py b/check_features.py new file mode 100644 index 0000000..2115a7b --- /dev/null +++ b/check_features.py @@ -0,0 +1,63 @@ +import toml +import subprocess +import sys + +def main(): + # 检查cargo是否可用 + try: + subprocess.run( + ["cargo", "--version"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True + ) + except FileNotFoundError: + print("Error: 'cargo' not found. Install Rust and ensure it's in PATH.") + sys.exit(1) + except subprocess.CalledProcessError as e: + print(f"Error checking cargo: {e}") + sys.exit(1) + + # 读取Cargo.toml + try: + with open("Cargo.toml", "r") as f: + cargo_toml = toml.load(f) + except FileNotFoundError: + print("Error: Cargo.toml not found in current directory.") + sys.exit(1) + except Exception as e: + print(f"Error parsing Cargo.toml: {e}") + sys.exit(1) + + features = cargo_toml.get("features", {}) + feature_names = list(features.keys()) + + if not feature_names: + print("No features defined in Cargo.toml.") + sys.exit(0) + + failed_features = [] + + print(f"Testing {len(feature_names)} features...") + for idx, feature in enumerate(feature_names, 1): + print(f"\nTesting feature {idx}/{len(feature_names)}: {feature}") + cmd = ["cargo", "check", "--no-default-features", "--features", feature, '--target-dir', 'target/features_check'] + try: + subprocess.run(cmd, check=True) + except subprocess.CalledProcessError: + failed_features.append(feature) + print(f"❌ Feature '{feature}' failed to compile") + else: + print(f"✅ Feature '{feature}' compiled successfully") + + if failed_features: + print("\nFailed features:") + for f in failed_features: + print(f" - {f}") + sys.exit(1) + else: + print("\nAll features compiled successfully!") + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/src/format/fixed.rs b/src/format/fixed.rs index e578cc1..3345a90 100644 --- a/src/format/fixed.rs +++ b/src/format/fixed.rs @@ -18,10 +18,16 @@ impl FixedFormatter { } } + #[cfg(feature = "circus")] fn is_circus(&self) -> bool { matches!(self.typ, Some(ScriptType::Circus)) } + #[cfg(not(feature = "circus"))] + fn is_circus(&self) -> bool { + false + } + pub fn format(&self, message: &str) -> String { let mut result = String::new(); let vec: Vec<_> = UnicodeSegmentation::graphemes(message, true).collect(); diff --git a/src/scripts/mod.rs b/src/scripts/mod.rs index 2933970..37a7a4b 100644 --- a/src/scripts/mod.rs +++ b/src/scripts/mod.rs @@ -1,15 +1,22 @@ pub mod base; +#[cfg(feature = "bgi")] pub mod bgi; +#[cfg(feature = "circus")] pub mod circus; +#[cfg(feature = "escude")] pub mod escude; pub use base::{Script, ScriptBuilder}; lazy_static::lazy_static! { pub static ref BUILDER: Vec> = vec![ + #[cfg(feature = "circus")] Box::new(circus::script::CircusMesScriptBuilder::new()), + #[cfg(feature = "bgi")] Box::new(bgi::script::BGIScriptBuilder::new()), + #[cfg(feature = "escude")] Box::new(escude::archive::EscudeBinArchiveBuilder::new()), + #[cfg(feature = "escude")] Box::new(escude::script::EscudeBinScriptBuilder::new()), ]; pub static ref ALL_EXTS: Vec = diff --git a/src/types.rs b/src/types.rs index 3d8339b..a9c587e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -182,13 +182,17 @@ pub struct ExtraConfig { #[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)] /// Script type pub enum ScriptType { + #[cfg(feature = "circus")] /// Circus MES script Circus, + #[cfg(feature = "bgi")] #[value(alias("ethornell"))] /// Buriko General Interpreter/Ethornell Script BGI, + #[cfg(feature = "escude")] /// Escude bin archive EscudeArc, + #[cfg(feature = "escude")] /// Escude bin script Escude, }