Skip to content

Commit

Permalink
Merge pull request #29 from rakaly/tokens1
Browse files Browse the repository at this point in the history
Swap compile time tokens to embedded runtime decoding
  • Loading branch information
nickbabcock authored Sep 14, 2024
2 parents 933f0de + b82544f commit 49efec9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ jobs:
with:
path: assets/saves
key: assets/saves
- name: Set cargo environment variables
shell: bash
run: |
echo "EU4_IRONMAN_TOKENS=$current_dir/assets/tokens/eu4.txt" >> $GITHUB_ENV
echo "CK3_IRONMAN_TOKENS=$current_dir/assets/tokens/ck3.txt" >> $GITHUB_ENV
echo "IMPERATOR_TOKENS=$current_dir/assets/tokens/imperator.txt" >> $GITHUB_ENV
echo "HOI4_IRONMAN_TOKENS=$current_dir/assets/tokens/hoi4.txt" >> $GITHUB_ENV
echo "VIC3_IRONMAN_TOKENS=$current_dir/assets/tokens/vic3.txt" >> $GITHUB_ENV
env:
current_dir: ${{ github.workspace }}
- name: Build
run: cargo build --all --verbose --release
- name: create save assets directory
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
assets/tokens/*.txt
assets/tokens
14 changes: 7 additions & 7 deletions Cargo.lock

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

18 changes: 7 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::env;
use std::path::Path;

fn main() {
if env::var("EU4_IRONMAN_TOKENS").is_err() {
panic!("librakaly needs to be built with EU4_IRONMAN_TOKENS specified")
}

if env::var("CK3_IRONMAN_TOKENS").is_err() {
panic!("librakaly needs to be built with CK3_IRONMAN_TOKENS specified")
}

if env::var("IMPERATOR_TOKENS").is_err() {
panic!("librakaly needs to be built with IMPERATOR_TOKENS specified")
std::fs::create_dir_all("assets/tokens").expect("to create tokens directory");
for game in ["ck3", "hoi4", "eu4", "imperator", "vic3"] {
let fp = format!("assets/tokens/{game}.txt");
let p = std::path::Path::new(&fp);
if !p.exists() {
std::fs::write(p, "").expect("to write file");
}
}

let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod errors;
mod file;
mod melter;
mod tokens;

use crate::errors::LibError;
use errors::PdsError;
Expand Down
21 changes: 13 additions & 8 deletions src/melter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::io::Cursor;

use crate::errors::LibError;
use crate::{
errors::LibError,
tokens::{
ck3_tokens_resolver, eu4_tokens_resolver, hoi4_tokens_resolver, imperator_tokens_resolver,
vic3_tokens_resolver,
},
};
use ck3save::Ck3Melter;
use eu4save::Eu4Melter;
use hoi4save::Hoi4Melter;
use imperator_save::ImperatorMelter;
use std::io::Cursor;
use vic3save::Vic3Melter;

pub enum MeltedBufferResult {
Expand Down Expand Up @@ -43,7 +48,7 @@ impl<'a> Melter for Eu4Melter<'a> {
let doc = self
.on_failed_resolve(eu4save::FailedResolveStrategy::Stringify)
.verbatim(true)
.melt(&mut out, &eu4save::EnvTokens)?;
.melt(&mut out, eu4_tokens_resolver())?;

if self.input_encoding().is_text() {
Ok(MeltedBuffer::Text {
Expand All @@ -69,7 +74,7 @@ impl<'a> Melter for Ck3Melter<'a> {
let doc = self
.on_failed_resolve(ck3save::FailedResolveStrategy::Stringify)
.verbatim(true)
.melt(&mut out, &ck3save::EnvTokens)?;
.melt(&mut out, &ck3_tokens_resolver())?;

if matches!(self.input_encoding(), ck3save::Encoding::TextZip) {
Ok(MeltedBuffer::Text {
Expand All @@ -95,7 +100,7 @@ impl<'a> Melter for ImperatorMelter<'a> {
let doc = self
.on_failed_resolve(imperator_save::FailedResolveStrategy::Stringify)
.verbatim(true)
.melt(&mut out, &imperator_save::EnvTokens)?;
.melt(&mut out, &imperator_tokens_resolver())?;

if matches!(self.input_encoding(), imperator_save::Encoding::TextZip) {
Ok(MeltedBuffer::Text {
Expand All @@ -121,7 +126,7 @@ impl<'a> Melter for Hoi4Melter<'a> {
let doc = self
.on_failed_resolve(hoi4save::FailedResolveStrategy::Stringify)
.verbatim(true)
.melt(&mut out, &hoi4save::EnvTokens)?;
.melt(&mut out, &hoi4_tokens_resolver())?;

Ok(MeltedBuffer::Binary {
body: out.into_inner(),
Expand All @@ -140,7 +145,7 @@ impl<'a> Melter for Vic3Melter<'a> {
let doc = self
.on_failed_resolve(vic3save::FailedResolveStrategy::Stringify)
.verbatim(true)
.melt(&mut out, &vic3save::EnvTokens)?;
.melt(&mut out, &vic3_tokens_resolver())?;

if matches!(self.input_encoding(), vic3save::Encoding::TextZip) {
Ok(MeltedBuffer::Text {
Expand Down
27 changes: 27 additions & 0 deletions src/tokens.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub fn eu4_tokens_resolver() -> eu4save::BasicTokenResolver {
let data = include_bytes!("../assets/tokens/eu4.txt");
eu4save::BasicTokenResolver::from_text_lines(&data[..]).expect("embedded tokens invalid format")
}

pub fn ck3_tokens_resolver() -> ck3save::BasicTokenResolver {
let data = include_bytes!("../assets/tokens/ck3.txt");
ck3save::BasicTokenResolver::from_text_lines(&data[..]).expect("embedded tokens invalid format")
}

pub fn vic3_tokens_resolver() -> vic3save::BasicTokenResolver {
let data = include_bytes!("../assets/tokens/vic3.txt");
vic3save::BasicTokenResolver::from_text_lines(&data[..])
.expect("embedded tokens invalid format")
}

pub fn imperator_tokens_resolver() -> imperator_save::BasicTokenResolver {
let data = include_bytes!("../assets/tokens/imperator.txt");
imperator_save::BasicTokenResolver::from_text_lines(&data[..])
.expect("embedded tokens invalid format")
}

pub fn hoi4_tokens_resolver() -> hoi4save::BasicTokenResolver {
let data = include_bytes!("../assets/tokens/hoi4.txt");
hoi4save::BasicTokenResolver::from_text_lines(&data[..])
.expect("embedded tokens invalid format")
}

0 comments on commit 49efec9

Please sign in to comment.