Skip to content

Commit

Permalink
feat: lua output generation (#15)
Browse files Browse the repository at this point in the history
* feat: lua output generation

* feat: add scale lua output
  • Loading branch information
fgardt authored Mar 23, 2024
1 parent 0290fed commit 4b3e9e5
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.vscode
/target

*.png
test/
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[package]
name = "spritter"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["fgardt <[email protected]>"]
description = "Spritesheet generator for factorio"
repository = "https://github.com/fgardt/factorio-spritter"

[profile.release]
strip = true
Expand All @@ -17,7 +18,9 @@ nursery = "warn"
pedantic = "warn"
unwrap_used = "warn"
expect_used = "warn"
module_name_repetitions = "allow"
cast_possible_truncation = "allow"
cast_precision_loss = "allow"
cast_possible_wrap = "allow"
cast_lossless = "allow"
cast_sign_loss = "allow"
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![release](https://github.com/fgardt/factorio-spritter/actions/workflows/release.yml/badge.svg)](https://github.com/fgardt/factorio-spritter/releases)
![actions status](https://img.shields.io/github/actions/workflow/status/fgardt/factorio-spritter/rust.yml)
[![release](https://img.shields.io/github/v/release/fgardt/factorio-spritter)](https://github.com/fgardt/factorio-spritter/releases)
[![ko-fi](https://img.shields.io/badge/Ko--fi-Donate%20-hotpink?logo=kofi&logoColor=white)](https://ko-fi.com/fgardt)

# Spritter

Expand Down Expand Up @@ -62,7 +64,3 @@ Arguments:
<OUTPUT>
Output folder
```

## Todo

- Lua output generation
166 changes: 166 additions & 0 deletions src/lua.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use std::{collections::BTreeMap, io::Write, path::Path};

#[derive(Debug, Clone)]
pub enum LuaValue {
String(String),
Float(f64),
Int(i64),
Bool(bool),
Shift(f64, f64, usize),
Require(String),
}

impl From<String> for LuaValue {
fn from(value: String) -> Self {
Self::String(value)
}
}

impl From<&str> for LuaValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}

impl From<f64> for LuaValue {
fn from(value: f64) -> Self {
Self::Float(value)
}
}

impl From<f32> for LuaValue {
fn from(value: f32) -> Self {
Self::Float(value as f64)
}
}

impl From<isize> for LuaValue {
fn from(value: isize) -> Self {
Self::Int(value as i64)
}
}

impl From<i64> for LuaValue {
fn from(value: i64) -> Self {
Self::Int(value)
}
}

impl From<i32> for LuaValue {
fn from(value: i32) -> Self {
Self::Int(value as i64)
}
}

impl From<i16> for LuaValue {
fn from(value: i16) -> Self {
Self::Int(value as i64)
}
}

impl From<i8> for LuaValue {
fn from(value: i8) -> Self {
Self::Int(value as i64)
}
}

impl From<usize> for LuaValue {
fn from(value: usize) -> Self {
Self::Int(value as i64)
}
}

impl From<u64> for LuaValue {
fn from(value: u64) -> Self {
Self::Int(value as i64)
}
}

impl From<u32> for LuaValue {
fn from(value: u32) -> Self {
Self::Int(value as i64)
}
}

impl From<u16> for LuaValue {
fn from(value: u16) -> Self {
Self::Int(value as i64)
}
}

impl From<u8> for LuaValue {
fn from(value: u8) -> Self {
Self::Int(value as i64)
}
}

impl From<bool> for LuaValue {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}

impl From<(f64, f64, usize)> for LuaValue {
fn from((shift_x, shift_y, res): (f64, f64, usize)) -> Self {
Self::Shift(shift_x, shift_y, res)
}
}

impl std::fmt::Display for LuaValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(value) => write!(f, "\"{value}\""),
Self::Float(value) => write!(f, "{value}"),
Self::Int(value) => write!(f, "{value}"),
Self::Bool(value) => write!(f, "{value}"),
Self::Shift(x, y, res) => write!(f, "{{x = {x} / {res}, y = {y} / {res}}}"),
Self::Require(value) => write!(f, "require(\"{value}\")"),
}
}
}

pub struct LuaOutput {
map: BTreeMap<String, LuaValue>,
}

impl LuaOutput {
pub const fn new() -> Self {
Self {
map: BTreeMap::new(),
}
}

pub fn set(mut self, key: impl AsRef<str>, value: impl Into<LuaValue>) -> Self {
self.map.insert(key.as_ref().to_owned(), value.into());
self
}

pub fn reexport(mut self, name: impl AsRef<str>) -> Self {
self.map.insert(
name.as_ref().to_owned(),
LuaValue::Require(name.as_ref().to_owned()),
);
self
}

pub fn save(&self, path: impl AsRef<Path>) -> std::io::Result<()> {
let mut file = std::fs::File::create(path)?;

writeln!(
file,
"-- Generated by {} v{} - {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_REPOSITORY")
)?;
writeln!(file, "return {{")?;

for (key, data) in &self.map {
writeln!(file, " [\"{key}\"] = {data},")?;
}

writeln!(file, "}}")?;

Ok(())
}
}
Loading

0 comments on commit 4b3e9e5

Please sign in to comment.