Skip to content

Commit

Permalink
prefix refactor, enabled asc by default (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored May 15, 2023
1 parent a30a3f6 commit 542df11
Show file tree
Hide file tree
Showing 20 changed files with 359 additions and 177 deletions.
46 changes: 39 additions & 7 deletions bin/src/addons.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
use std::{fs::DirEntry, path::PathBuf, str::FromStr};

use hemtt_pbo::{prefix, Prefix};

use crate::{config::addon::Configuration, error::Error};

#[derive(Debug, Clone)]
pub struct Addon {
name: String,
location: Location,
config: Option<Configuration>,
prefix: Prefix,
}

impl Addon {
pub fn new(name: String, location: Location) -> Self {
Self {
pub fn new(name: String, location: Location) -> Result<Self, Error> {
let path = PathBuf::from(location.to_string()).join(&name);
Ok(Self {
config: {
let path =
PathBuf::from(format!("{}/{name}", location.to_string())).join("addon.toml");
let path = path.join("addon.toml");
if path.exists() {
Some(Configuration::from_file(&path).unwrap())
} else {
None
}
},
prefix: {
let mut prefix = None;
let mut files = prefix::FILES
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
files.append(
&mut prefix::FILES
.iter()
.map(|f| f.to_uppercase())
.collect::<Vec<String>>(),
);
'search: for file in &files {
let path = path.join(file);
if path.exists() {
let content = std::fs::read_to_string(path).unwrap();
prefix = Some(Prefix::new(&content).unwrap());
break 'search;
}
}
if prefix.is_none() {
return Err(Error::AddonPrefixMissing(name));
}
prefix.unwrap()
},
location,
name,
}
})
}

pub fn name(&self) -> &str {
Expand All @@ -38,6 +66,10 @@ impl Addon {
&self.location
}

pub const fn prefix(&self) -> &Prefix {
&self.prefix
}

/// addons/foobar
/// optionals/foobar
pub fn folder(&self) -> String {
Expand Down Expand Up @@ -68,13 +100,13 @@ impl Location {
if !PathBuf::from(self.to_string()).exists() {
return Ok(Vec::new());
}
Ok(std::fs::read_dir(self.to_string())?
std::fs::read_dir(self.to_string())?
.collect::<std::io::Result<Vec<DirEntry>>>()?
.iter()
.map(std::fs::DirEntry::path)
.filter(|file_or_dir| file_or_dir.is_dir())
.map(|file| Addon::new(file.file_name().unwrap().to_str().unwrap().to_owned(), self))
.collect())
.collect()
}
}

Expand Down
5 changes: 4 additions & 1 deletion bin/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ pub fn execute(matches: &ArgMatches, executor: &mut Executor) -> Result<(), Erro
executor.add_module(Box::<Binarize>::default());
}
executor.add_module(Box::<Hooks>::default());
executor.add_module(Box::<ArmaScriptCompiler>::default());
#[cfg(not(target_os = "macos"))]
{
executor.add_module(Box::<ArmaScriptCompiler>::default());
}
executor.add_module(Box::<Files>::default());

info!("Creating `build` version");
Expand Down
2 changes: 1 addition & 1 deletion bin/src/config/project/asc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Options {
impl Options {
#[must_use]
pub fn enabled(&self) -> bool {
self.enabled.unwrap_or(false)
self.enabled.unwrap_or(true)
}

#[must_use]
Expand Down
15 changes: 0 additions & 15 deletions bin/src/config/project/hemtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ pub struct Features {

#[serde(default)]
release: ReleaseOptions,

#[serde(default)]
/// Can PBO prefixes have a leading slash?
///
/// Default: false
pbo_prefix_allow_leading_slash: Option<bool>,
}

impl Features {
Expand Down Expand Up @@ -49,15 +43,6 @@ impl Features {
pub const fn release(&self) -> &ReleaseOptions {
&self.release
}

#[must_use]
pub const fn pbo_prefix_allow_leading_slash(&self) -> bool {
if let Some(allow) = self.pbo_prefix_allow_leading_slash {
allow
} else {
false
}
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions bin/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Error {
AddonLocationInvalid(String),
#[error("Optional addon not found: {0}")]
AddonOptionalNotFound(String),
#[error("Addon prefix not found: {0}")]
AddonPrefixMissing(String),

#[error("Unable to create link: {0}")]
#[allow(dead_code)] // Unused on Linux and Mac
Expand Down
29 changes: 10 additions & 19 deletions bin/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::fs::{create_dir_all, remove_dir_all};

use hemtt_pbo::{prefix::FILES, Prefix};

use crate::error::Error;
use crate::utils::create_link;

Expand Down Expand Up @@ -109,23 +107,16 @@ fn setup_tmp(ctx: &Context) -> Result<(), Error> {
let tmp = ctx.tmp().join("source");
create_dir_all(&tmp)?;
for addon in ctx.addons() {
for file in FILES {
let root = ctx.vfs().join(addon.folder()).unwrap();
let path = root.join(file).unwrap();
if path.exists().unwrap() {
let prefix = Prefix::new(
&path.read_to_string().unwrap(),
ctx.config().hemtt().pbo_prefix_allow_leading_slash(),
)?
.into_inner();
let tmp_addon = tmp.join(prefix);
create_dir_all(tmp_addon.parent().unwrap())?;
let target = std::env::current_dir()?
.join(root.as_str().trim_start_matches('/').replace('/', "\\"));
create_link(tmp_addon.to_str().unwrap(), target.to_str().unwrap())?;
break;
}
}
let tmp_addon = tmp.join(addon.prefix().as_pathbuf());
create_dir_all(tmp_addon.parent().unwrap())?;
let target = std::env::current_dir()?.join(
addon
.folder()
.as_str()
.trim_start_matches('/')
.replace('/', "\\"),
);
create_link(tmp_addon.to_str().unwrap(), target.to_str().unwrap())?;
}
let include = std::env::current_dir().unwrap().join("include");
if !include.exists() {
Expand Down
39 changes: 24 additions & 15 deletions bin/src/modules/asc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ impl Module for ArmaScriptCompiler {
std::fs::set_permissions(out, PermissionsExt::from_mode(0o744))?;
}
}
let resolver = VfsResolver::new(ctx)?;
let resolver = VfsResolver::new(ctx);
let sqf_ext = Some(String::from("sqf"));
let files = Arc::new(RwLock::new(Vec::new()));
let start = Instant::now();
let mut root_dirs = Vec::new();
for addon in ctx.addons() {
let tmp_addon = tmp.join("source").join(addon.folder());
if !root_dirs.contains(&addon.prefix().main_prefix()) {
root_dirs.push(addon.prefix().main_prefix());
}
let tmp_addon = tmp.join(addon.prefix().as_pathbuf());
create_dir_all(&tmp_addon)?;
let mut entries = Vec::new();
for entry in ctx.vfs().join(addon.folder())?.walk_dir()? {
Expand Down Expand Up @@ -134,13 +138,22 @@ impl Module for ArmaScriptCompiler {
for t in tokens {
std::io::copy(&mut BufReader::new(t.to_source().as_bytes()), &mut write)?;
}
files.write().unwrap().push(
files.write().unwrap().push((
format!(
"{}{}",
addon
.prefix()
.to_string()
.replace('\\', "/")
.trim_end_matches(&addon.folder()),
entry.as_str().to_string().trim_start_matches('/'),
),
entry
.as_str()
.to_string()
.trim_start_matches('/')
.to_string(),
);
));
Ok(())
})
.collect::<Result<_, Error>>()?;
Expand All @@ -149,7 +162,9 @@ impl Module for ArmaScriptCompiler {
"ASC Preprocess took {:?}",
start.elapsed().whole_milliseconds()
);
config.add_input_dir(tmp.join("source").display().to_string());
for root in root_dirs {
config.add_input_dir(root.to_string());
}
config.set_output_dir(tmp.join("output").display().to_string());
let include = tmp.join("source").join("include");
if include.exists() {
Expand All @@ -173,17 +188,11 @@ impl Module for ArmaScriptCompiler {
));
}
std::env::set_current_dir(old_dir)?;
let tmp_output = tmp.join("output").join(
tmp.join("source")
.display()
.to_string()
.trim_start_matches(&tmp.ancestors().last().unwrap().display().to_string()),
);
let tmp_output = tmp.join("output");
let counter = AtomicI16::new(0);
for file in &*files.read().unwrap() {
let file = format!("{file}c");
let from = tmp_output.join(&file);
let to = ctx.vfs().join(&file)?;
for (src, dst) in &*files.read().unwrap() {
let from = tmp_output.join(&format!("{src}c"));
let to = ctx.vfs().join(&format!("{dst}c"))?;
if !from.exists() {
// Likely excluded
continue;
Expand Down
36 changes: 2 additions & 34 deletions bin/src/modules/binarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
sync::atomic::{AtomicI16, Ordering},
};

use hemtt_pbo::{prefix::FILES, Prefix};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use vfs::VfsFileType;

Expand Down Expand Up @@ -80,38 +79,7 @@ impl Module for Binarize {
continue;
}

let addon_root = PathBuf::from(entry.as_str())
.components()
.take(3)
.collect::<PathBuf>();
let mut tmp_sourced = None;
let mut tmp_outed = None;
for file in FILES {
let prefix_path = ctx
.vfs()
.join(
addon_root
.join(file)
.to_str()
.unwrap()
.replace('\\', "/")
.trim_start_matches('/'),
)
.unwrap();
if prefix_path.exists().unwrap() {
let prefix = Prefix::new(
&prefix_path.read_to_string().unwrap(),
ctx.config().hemtt().pbo_prefix_allow_leading_slash(),
)
.unwrap()
.into_inner();
tmp_sourced = Some(tmp_source.join(prefix.trim_start_matches('\\')));
tmp_outed =
Some(tmp_out.join(entry.parent().as_str().trim_start_matches('/')));
break;
}
}
let tmp_sourced = tmp_sourced.unwrap().join(
let tmp_sourced = tmp_source.join(addon.prefix().as_pathbuf()).join(
entry
.as_str()
.trim_start_matches('/')
Expand All @@ -120,7 +88,7 @@ impl Module for Binarize {
.trim_end_matches(&entry.filename())
.replace('/', "\\"),
);
let tmp_outed = tmp_outed.unwrap();
let tmp_outed = tmp_out.join(entry.parent().as_str().trim_start_matches('/'));

targets.push(BinarizeTarget {
source: tmp_sourced
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Module for Lint {
}
let counter = AtomicI32::new(0);
for addon in ctx.addons() {
let resolver = VfsResolver::new(ctx)?;
let resolver = VfsResolver::new(ctx);
let sqf_ext = Some(String::from("sqf"));
let mut entries = Vec::new();
for entry in ctx.vfs().join(addon.folder())?.walk_dir()? {
Expand Down
7 changes: 2 additions & 5 deletions bin/src/modules/pbo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ fn _build(
}

if FILES.contains(&entry.filename().to_lowercase().as_str()) {
let prefix = Prefix::new(
&entry.read_to_string().unwrap(),
ctx.config().hemtt().pbo_prefix_allow_leading_slash(),
)?;
pbo.add_property("prefix", prefix.into_inner());
let prefix = Prefix::new(&entry.read_to_string().unwrap())?;
pbo.add_property("prefix", prefix.to_string());
pbo.add_property("version", version.to_string());
if let Some(hash) = git_hash {
pbo.add_property("git", hash);
Expand Down
Loading

0 comments on commit 542df11

Please sign in to comment.