Enhance CLI functionality with palette support and update exporters to use BlockPalette

This commit is contained in:
2026-03-23 01:21:21 +01:00
parent 1893b3427f
commit 484415870a
12 changed files with 149 additions and 29 deletions

View File

@@ -17,6 +17,8 @@ lib = { path = "../lib" }
exporters = { path = "../exporters" }
clap = { version = "4.6.0", features = ["derive"] }
anyhow = { workspace = true }
serde = { workspace = true }
serde_json = "1"
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

View File

@@ -2,26 +2,34 @@ use std::{fs, path::PathBuf};
use clap::Parser;
use exporters::McFunctionExporter;
use lib::{StructureExporter, TextBuilder, TtfFont};
use lib::{BlockPalette, StructureExporter, TextBuilder, TtfFont};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// The text you want to generate
#[arg(short, long)]
text: String,
#[arg(short, long, required_unless_present = "list_palettes")]
text: Option<String>,
/// Path to the .ttf font file
#[arg(short, long)]
font: PathBuf,
#[arg(short, long, required_unless_present = "list_palettes")]
font: Option<PathBuf>,
/// How many blocks deep the text should be
#[arg(short, long, default_value_t = 1)]
depth: u32,
/// The Minecraft block ID to use for the text body
#[arg(short, long, default_value = "minecraft:quartz_block")]
block: String,
/// Name of a built-in palette preset (from the palettes/ directory)
#[arg(short, long, conflicts_with = "palette_file")]
palette: Option<String>,
/// Path to a JSON palette file
#[arg(long)]
palette_file: Option<PathBuf>,
/// List available palette presets and exit
#[arg(long)]
list_palettes: bool,
/// Output file path (without extension)
#[arg(short, long, default_value = "output")]
@@ -32,26 +40,70 @@ struct Cli {
size: f32,
}
fn palettes_dir() -> PathBuf {
std::env::current_exe()
.unwrap()
.parent()
.unwrap()
.join("palettes")
}
fn load_palette_by_name(name: &str) -> anyhow::Result<BlockPalette> {
let path = palettes_dir().join(format!("{}.json", name));
let json = fs::read_to_string(&path)
.map_err(|_| anyhow::anyhow!("palette '{}' not found in {:?}", name, palettes_dir()))?;
Ok(serde_json::from_str(&json)?)
}
pub fn run() -> anyhow::Result<()> {
let cli = Cli::parse();
tracing::info!("loading font from: {:?}", cli.font);
let font_bytes = fs::read(&cli.font)?;
if cli.list_palettes {
let dir = palettes_dir();
let entries = fs::read_dir(&dir)
.map_err(|_| anyhow::anyhow!("palettes directory not found at {:?}", dir))?;
println!("Available palettes:");
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) == Some("json") {
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
println!(" {}", stem);
}
}
}
return Ok(());
}
let font = TtfFont::from_bytes(&font_bytes, cli.size)
.map_err(|e| anyhow::anyhow!(e))?;
let text = cli.text.unwrap();
let font_path = cli.font.unwrap();
tracing::info!("generating voxel grid for text: '{}'", cli.text);
let palette = if let Some(path) = cli.palette_file {
let json = fs::read_to_string(&path)
.map_err(|e| anyhow::anyhow!("failed to read palette file {:?}: {}", path, e))?;
serde_json::from_str(&json)?
} else {
let name = cli.palette.as_deref().unwrap_or("default");
load_palette_by_name(name)?
};
tracing::info!("using palette: {}", palette.name);
tracing::info!("loading font from: {:?}", font_path);
let font_bytes = fs::read(&font_path)?;
let font = TtfFont::from_bytes(&font_bytes, cli.size).map_err(|e| anyhow::anyhow!(e))?;
tracing::info!("generating voxel grid for text: '{}'", text);
let builder = TextBuilder::new(&font).with_depth(cli.depth);
let grid = builder.generate(&cli.text);
let grid = builder.generate(&text);
tracing::info!(
"grid generated: {}x{}x{}",
grid.width, grid.height, grid.depth
grid.width,
grid.height,
grid.depth
);
let exporter = McFunctionExporter::new(&cli.block, "minecraft:obsidian");
let exporter = McFunctionExporter::new(&palette);
let output_bytes = exporter.export(&grid)?;
let mut out_path = cli.out.clone();

View File

@@ -1,4 +1,5 @@
mod litematica;
mod mcfunction;
pub use litematica::LitematicaExporter;
pub use mcfunction::McFunctionExporter;

View File

@@ -7,7 +7,7 @@ use std::{
use anyhow::Context;
use flate2::Compression;
use flate2::write::GzEncoder;
use lib::{StructureExporter, VoxelType};
use lib::{BlockPalette, StructureExporter, VoxelType};
use serde::Serialize;
#[derive(Serialize)]
@@ -61,10 +61,11 @@ pub struct LitematicaExporter {
}
impl LitematicaExporter {
pub fn new(body_block: &str, outline_block: &str) -> Self {
pub fn new(palette: &BlockPalette) -> Self {
let mut palette_map = HashMap::new();
palette_map.insert(VoxelType::Body, body_block.to_string());
palette_map.insert(VoxelType::Outline, outline_block.to_string());
palette_map.insert(VoxelType::Body, palette.resolve(&VoxelType::Body));
palette_map.insert(VoxelType::Outline, palette.resolve(&VoxelType::Outline));
palette_map.insert(VoxelType::Shadow, palette.resolve(&VoxelType::Shadow));
Self { palette_map }
}

View File

@@ -1,19 +1,18 @@
use std::collections::HashMap;
use lib::{StructureExporter, VoxelType};
use lib::{BlockPalette, StructureExporter, VoxelType};
pub struct McFunctionExporter {
palette: HashMap<VoxelType, String>,
}
impl McFunctionExporter {
pub fn new(body_block: &str, outline_block: &str) -> Self {
let mut palette = HashMap::new();
palette.insert(VoxelType::Body, body_block.to_string());
palette.insert(VoxelType::Outline, outline_block.to_string());
Self { palette }
pub fn new(palette: &BlockPalette) -> Self {
let mut map = HashMap::new();
map.insert(VoxelType::Body, palette.resolve(&VoxelType::Body));
map.insert(VoxelType::Outline, palette.resolve(&VoxelType::Outline));
map.insert(VoxelType::Shadow, palette.resolve(&VoxelType::Shadow));
Self { palette: map }
}
}

View File

@@ -12,6 +12,7 @@ pub use font::FontProvider;
pub use fonts::ttf_font::TtfFont;
pub use grid::VoxelGrid;
pub use models::VoxelType;
pub use palette::BlockPalette;
pub trait StructureExporter {
fn export(&self, grid: &VoxelGrid) -> anyhow::Result<Vec<u8>>;