From 3e1710b1b2f32ac612a29398dda4973cc6fcc94b Mon Sep 17 00:00:00 2001 From: Kris Stasik Date: Wed, 12 Jan 2022 14:53:02 -0500 Subject: [PATCH] create compiler_node module --- client/data-build-cli/src/data-build/main.rs | 2 +- client/data-build-cli/src/scrape/config.rs | 2 +- client/data-build-cli/tests/tests.rs | 2 +- lib/data-build/src/databuild.rs | 4 +- lib/data-build/src/options.rs | 4 +- lib/data-build/src/test_compile.rs | 2 +- lib/data-build/src/test_general.rs | 2 +- lib/data-build/src/test_source_pull.rs | 2 +- lib/data-compiler/src/compiler_api.rs | 2 +- lib/data-compiler/src/compiler_cmd.rs | 2 +- .../binary_stub.rs | 0 .../src/compiler_node/compiler_registry.rs | 151 +++++++++ .../inproc_stub.rs | 0 lib/data-compiler/src/compiler_node/mod.rs | 167 ++++++++++ lib/data-compiler/src/compiler_reg/mod.rs | 305 ------------------ lib/data-compiler/src/lib.rs | 2 +- lib/data-transaction/src/test_transaction.rs | 2 +- .../src/property_inspector_plugin.rs | 2 +- .../editor-srv/src/resource_browser_plugin.rs | 2 +- server/ubercompiler/src/lib.rs | 6 +- .../src/offline_compiler/mod.rs | 2 +- 21 files changed, 338 insertions(+), 325 deletions(-) rename lib/data-compiler/src/{compiler_reg => compiler_node}/binary_stub.rs (100%) create mode 100644 lib/data-compiler/src/compiler_node/compiler_registry.rs rename lib/data-compiler/src/{compiler_reg => compiler_node}/inproc_stub.rs (100%) create mode 100644 lib/data-compiler/src/compiler_node/mod.rs delete mode 100644 lib/data-compiler/src/compiler_reg/mod.rs diff --git a/client/data-build-cli/src/data-build/main.rs b/client/data-build-cli/src/data-build/main.rs index ed5bdfa643..6c89d4df35 100644 --- a/client/data-build-cli/src/data-build/main.rs +++ b/client/data-build-cli/src/data-build/main.rs @@ -61,7 +61,7 @@ use clap::{AppSettings, Parser, Subcommand}; use lgn_content_store::ContentStoreAddr; use lgn_data_build::DataBuildOptions; use lgn_data_compiler::{ - compiler_api::CompilationEnv, compiler_reg::CompilerRegistryOptions, Locale, Platform, Target, + compiler_api::CompilationEnv, compiler_node::CompilerRegistryOptions, Locale, Platform, Target, }; use lgn_data_offline::ResourcePathId; use lgn_data_runtime::ResourceTypeAndId; diff --git a/client/data-build-cli/src/scrape/config.rs b/client/data-build-cli/src/scrape/config.rs index d3f408df22..03c545edc5 100644 --- a/client/data-build-cli/src/scrape/config.rs +++ b/client/data-build-cli/src/scrape/config.rs @@ -5,7 +5,7 @@ use std::{ use lgn_content_store::ContentStoreAddr; use lgn_data_build::{DataBuild, DataBuildOptions}; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::resource::Project; use lgn_data_runtime::ResourceType; use serde::{Deserialize, Serialize}; diff --git a/client/data-build-cli/tests/tests.rs b/client/data-build-cli/tests/tests.rs index 95890c324f..1515db3fd4 100644 --- a/client/data-build-cli/tests/tests.rs +++ b/client/data-build-cli/tests/tests.rs @@ -3,7 +3,7 @@ use std::fs; use lgn_content_store::{ContentStoreAddr, HddContentStore}; use lgn_data_build::DataBuildOptions; use lgn_data_compiler::{ - compiler_api::CompilationEnv, compiler_reg::CompilerRegistryOptions, Locale, Platform, Target, + compiler_api::CompilationEnv, compiler_node::CompilerRegistryOptions, Locale, Platform, Target, }; use lgn_data_offline::{ resource::{Project, ResourcePathName, ResourceRegistryOptions}, diff --git a/lib/data-build/src/databuild.rs b/lib/data-build/src/databuild.rs index 4a41a7ac47..d0de283a24 100644 --- a/lib/data-build/src/databuild.rs +++ b/lib/data-build/src/databuild.rs @@ -9,7 +9,7 @@ use std::{env, io}; use lgn_content_store::{ContentStore, ContentStoreAddr, HddContentStore}; use lgn_data_compiler::compiler_api::{CompilationEnv, CompilationOutput, DATA_BUILD_VERSION}; -use lgn_data_compiler::compiler_reg::{CompilerRegistry, CompilerNode, CompilerStub}; +use lgn_data_compiler::compiler_node::{CompilerNode, CompilerRegistry, CompilerStub}; use lgn_data_compiler::CompilerHash; use lgn_data_compiler::{CompiledResource, Manifest}; use lgn_data_offline::Transform; @@ -68,7 +68,7 @@ fn compute_context_hash( /// ```no_run /// # use lgn_data_build::{DataBuild, DataBuildOptions}; /// # use lgn_content_store::ContentStoreAddr; -/// # use lgn_data_compiler::{compiler_api::CompilationEnv, compiler_reg::CompilerRegistryOptions, Locale, Platform, Target}; +/// # use lgn_data_compiler::{compiler_api::CompilationEnv, compiler_node::CompilerRegistryOptions, Locale, Platform, Target}; /// # use lgn_data_offline::ResourcePathId; /// # use lgn_data_runtime::{ResourceId, ResourceType, ResourceTypeAndId}; /// # use std::str::FromStr; diff --git a/lib/data-build/src/options.rs b/lib/data-build/src/options.rs index 83de8e7aec..c6800d236c 100644 --- a/lib/data-build/src/options.rs +++ b/lib/data-build/src/options.rs @@ -4,7 +4,7 @@ use std::{ }; use lgn_content_store::ContentStoreAddr; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_runtime::AssetRegistry; use crate::{DataBuild, Error}; @@ -21,7 +21,7 @@ use crate::{DataBuild, Error}; /// ``` /// # use lgn_data_build::DataBuildOptions; /// # use lgn_content_store::ContentStoreAddr; -/// # use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +/// # use lgn_data_compiler::compiler_node::CompilerRegistryOptions; /// let mut build = DataBuildOptions::new(".", CompilerRegistryOptions::from_dir("./")) /// .content_store(&ContentStoreAddr::from("./content_store/")) /// .create("."); diff --git a/lib/data-build/src/test_compile.rs b/lib/data-build/src/test_compile.rs index 2c7283f5de..e0798230d6 100644 --- a/lib/data-build/src/test_compile.rs +++ b/lib/data-build/src/test_compile.rs @@ -7,7 +7,7 @@ use std::{env, vec}; use integer_asset::{IntegerAsset, IntegerAssetLoader}; use lgn_content_store::{ContentStore, ContentStoreAddr, HddContentStore}; use lgn_data_compiler::compiler_api::CompilationEnv; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_compiler::{Locale, Manifest, Platform, Target}; use lgn_data_offline::resource::ResourceRegistryOptions; use lgn_data_offline::{ diff --git a/lib/data-build/src/test_general.rs b/lib/data-build/src/test_general.rs index 6a1650c624..b420faf8f3 100644 --- a/lib/data-build/src/test_general.rs +++ b/lib/data-build/src/test_general.rs @@ -4,7 +4,7 @@ use std::{ }; use lgn_content_store::ContentStoreAddr; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::resource::Project; use tempfile::TempDir; diff --git a/lib/data-build/src/test_source_pull.rs b/lib/data-build/src/test_source_pull.rs index 2efe676143..5c2e4d2a9e 100644 --- a/lib/data-build/src/test_source_pull.rs +++ b/lib/data-build/src/test_source_pull.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use std::sync::{Arc, Mutex}; use lgn_content_store::ContentStoreAddr; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::{ resource::{Project, ResourcePathName, ResourceRegistry, ResourceRegistryOptions}, ResourcePathId, diff --git a/lib/data-compiler/src/compiler_api.rs b/lib/data-compiler/src/compiler_api.rs index ba4817c344..a6bc12d841 100644 --- a/lib/data-compiler/src/compiler_api.rs +++ b/lib/data-compiler/src/compiler_api.rs @@ -90,7 +90,7 @@ use crate::{ COMMAND_ARG_PLATFORM, COMMAND_ARG_RESOURCE_DIR, COMMAND_ARG_SRC_DEPS, COMMAND_ARG_TARGET, COMMAND_ARG_TRANSFORM, COMMAND_NAME_COMPILE, COMMAND_NAME_COMPILER_HASH, COMMAND_NAME_INFO, }, - compiler_reg::{CompilerRegistry, CompilerRegistryOptions, CompilerNode}, + compiler_node::{CompilerNode, CompilerRegistry, CompilerRegistryOptions}, CompiledResource, CompilerHash, Locale, Manifest, Platform, Target, }; diff --git a/lib/data-compiler/src/compiler_cmd.rs b/lib/data-compiler/src/compiler_cmd.rs index c963cadd95..517f1733bc 100644 --- a/lib/data-compiler/src/compiler_cmd.rs +++ b/lib/data-compiler/src/compiler_cmd.rs @@ -64,7 +64,7 @@ use serde::{Deserialize, Serialize}; use crate::{ compiler_api::{CompilationEnv, CompilerInfo}, - compiler_reg::CompilerRegistry, + compiler_node::CompilerRegistry, CompiledResource, CompilerHash, }; diff --git a/lib/data-compiler/src/compiler_reg/binary_stub.rs b/lib/data-compiler/src/compiler_node/binary_stub.rs similarity index 100% rename from lib/data-compiler/src/compiler_reg/binary_stub.rs rename to lib/data-compiler/src/compiler_node/binary_stub.rs diff --git a/lib/data-compiler/src/compiler_node/compiler_registry.rs b/lib/data-compiler/src/compiler_node/compiler_registry.rs new file mode 100644 index 0000000000..6b1fdaeab8 --- /dev/null +++ b/lib/data-compiler/src/compiler_node/compiler_registry.rs @@ -0,0 +1,151 @@ +use std::{fmt, io, path::Path, sync::Arc}; + +use lgn_content_store::ContentStoreAddr; +use lgn_data_offline::{ResourcePathId, Transform}; +use lgn_data_runtime::{AssetRegistry, AssetRegistryOptions}; + +use crate::{ + compiler_api::{ + CompilationEnv, CompilationOutput, CompilerDescriptor, CompilerError, CompilerInfo, + }, + compiler_cmd::list_compilers, + CompiledResource, CompilerHash, +}; + +use super::{binary_stub::BinCompilerStub, inproc_stub::InProcessCompilerStub}; + +/// Interface allowing to support multiple types of compilers - in-process, +/// external executables. By returning multiple `CompilerInfo` via `info` the implementation +/// of `CompilerStub` can expose multiple compilers. +pub trait CompilerStub: Send + Sync { + /// Returns information about the compiler. + fn info(&self) -> io::Result>; + + /// Returns the `CompilerHash` for the given compilation context and transform. + fn compiler_hash(&self, transform: Transform, env: &CompilationEnv) + -> io::Result; + + /// Allow the compiler to register its own type loaders. + fn init(&self, registry: AssetRegistryOptions) -> AssetRegistryOptions; + + /// Triggers compilation of provided `compile_path` and returns the + /// information about compilation output. + #[allow(clippy::too_many_arguments)] + fn compile( + &self, + compile_path: ResourcePathId, + dependencies: &[ResourcePathId], + derived_deps: &[CompiledResource], + registry: Arc, + cas_addr: ContentStoreAddr, + project_dir: &Path, + env: &CompilationEnv, + ) -> Result; +} + +/// Options and flags which can be used to configure how a compiler registry is +/// created. +#[derive(Default)] +pub struct CompilerRegistryOptions { + compilers: Vec>, +} + +impl CompilerRegistryOptions { + /// Creates `CompilerRegistry` based on provided compiler directory paths. + pub fn from_dirs(dirs: &[impl AsRef]) -> Self { + let compilers = list_compilers(dirs) + .into_iter() + .map(|info| { + let compiler: Box = Box::new(BinCompilerStub { + bin_path: info.path, + }); + compiler + }) + .collect::>>(); + + Self { compilers } + } + + /// Creates `CompilerRegistry` based on provided compiler directory path. + pub fn from_dir(dir: impl AsRef) -> Self { + Self::from_dirs(std::slice::from_ref(&dir)) + } + + /// Register an in-process compiler. + pub fn add_compiler(mut self, descriptor: &'static CompilerDescriptor) -> Self { + let compiler = Box::new(InProcessCompilerStub { descriptor }); + self.compilers.push(compiler); + self + } + + /// Creates a new compiler registry based on specified options. + pub fn create(self) -> CompilerRegistry { + let (infos, indices) = self.collect_info(); + + CompilerRegistry { + compilers: self.compilers, + infos, + indices, + } + } + + /// Gathers info on all compilers. + fn collect_info(&self) -> (Vec, Vec) { + let mut infos = vec![]; + let mut indices = vec![]; + for (index, compiler_stub) in self.compilers.iter().enumerate() { + match compiler_stub.info() { + Ok(compilers) => { + indices.extend(std::iter::repeat(index).take(compilers.len())); + infos.extend(compilers); + } + Err(_) => continue, + } + } + + (infos, indices) + } +} + +/// A registry of data compilers. +pub struct CompilerRegistry { + compilers: Vec>, + infos: Vec, + indices: Vec, +} + +impl fmt::Debug for CompilerRegistry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CompilerRegistry") + .field("infos", &self.infos) + .finish() + } +} + +impl CompilerRegistry { + /// Returns a reference to the compiler + pub fn find_compiler(&self, transform: Transform) -> Option<(&dyn CompilerStub, Transform)> { + if let Some(compiler_index) = self + .infos + .iter() + .position(|info| info.transform == transform) + { + let stub_index = self.indices[compiler_index]; + return Some((self.compilers[stub_index].as_ref(), transform)); + } + None + } + + /// A list of compilers available in the registry. + pub fn infos(&self) -> &Vec { + &self.infos + } + + /// Initializes all compilers allowing them to register type loaders with `AssetRegistry`. + pub fn init_all(&self, mut registry_options: AssetRegistryOptions) -> AssetRegistryOptions { + for compiler in &self.compilers { + registry_options = compiler.init(registry_options); + } + registry_options + } +} diff --git a/lib/data-compiler/src/compiler_reg/inproc_stub.rs b/lib/data-compiler/src/compiler_node/inproc_stub.rs similarity index 100% rename from lib/data-compiler/src/compiler_reg/inproc_stub.rs rename to lib/data-compiler/src/compiler_node/inproc_stub.rs diff --git a/lib/data-compiler/src/compiler_node/mod.rs b/lib/data-compiler/src/compiler_node/mod.rs new file mode 100644 index 0000000000..21efe7eb56 --- /dev/null +++ b/lib/data-compiler/src/compiler_node/mod.rs @@ -0,0 +1,167 @@ +//! Compiler Node groups data compilers and provides utilities required for data compilation. +//! +//! registry defines an interface between different compiler types +//! (executable, in-process, etc). + +use core::fmt; +use std::sync::Arc; + +mod binary_stub; +mod inproc_stub; + +mod compiler_registry; +pub use compiler_registry::*; + +use lgn_data_runtime::AssetRegistry; + +/// A group of compilers with compilation utilities. +pub struct CompilerNode { + compilers: CompilerRegistry, + registry: Arc, +} + +impl CompilerNode { + /// Creates a new `CompilerNode`. + pub fn new(compilers: CompilerRegistry, registry: Arc) -> Self { + Self { + compilers, + registry, + } + } + + /// Access to all compilers. + pub fn compilers(&self) -> &CompilerRegistry { + &self.compilers + } + + /// Access to `AssetRegistry` shared between compilers. + pub fn registry(&self) -> Arc { + self.registry.clone() + } +} + +impl fmt::Debug for CompilerNode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CompilerNode") + .field("compilers", &self.compilers) + .finish() + } +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use lgn_content_store::{Checksum, ContentStoreAddr}; + use lgn_data_offline::{ResourcePathId, Transform}; + use lgn_data_runtime::{ + AssetRegistryOptions, Resource, ResourceId, ResourceType, ResourceTypeAndId, + }; + + use super::CompilerRegistryOptions; + use crate::{ + compiler_api::{CompilationEnv, CompilationOutput, CompilerDescriptor}, + CompiledResource, CompilerHash, Locale, Platform, Target, + }; + + const TEST_TRANSFORM: Transform = + Transform::new(ResourceType::new(b"input"), ResourceType::new(b"output")); + const TEST_COMPILER: CompilerDescriptor = CompilerDescriptor { + name: "test_name", + build_version: "build0", + code_version: "code0", + data_version: "data0", + transform: &TEST_TRANSFORM, + init_func: |options| options, + compiler_hash_func: |_code, _data, _env| CompilerHash(7), + compile_func: |ctx| { + Ok(CompilationOutput { + compiled_resources: vec![CompiledResource { + path: ctx.target_unnamed, + checksum: Checksum::from(7), + size: 7, + }], + resource_references: vec![], + }) + }, + }; + + #[test] + fn binary() { + let target_dir = std::env::current_exe().ok().map_or_else( + || panic!("cannot find test directory"), + |mut path| { + path.pop(); + if path.ends_with("deps") { + path.pop(); + } + path + }, + ); + + let registry = CompilerRegistryOptions::from_dir(target_dir).create(); + + let env = CompilationEnv { + target: Target::Game, + platform: Platform::Windows, + locale: Locale::new("en"), + }; + + let source = ResourceTypeAndId { + kind: text_resource::TextResource::TYPE, + id: ResourceId::new(), + }; + let destination = ResourcePathId::from(source).push(integer_asset::IntegerAsset::TYPE); + + let transform = Transform::new(source.kind, destination.content_type()); + + let (compiler, transform) = registry.find_compiler(transform).expect("valid compiler"); + let _ = compiler.compiler_hash(transform, &env).expect("valid hash"); + } + + #[test] + fn in_proc() { + let registry = CompilerRegistryOptions::default() + .add_compiler(&TEST_COMPILER) + .create(); + + let env = CompilationEnv { + target: Target::Game, + platform: Platform::Windows, + locale: Locale::new("en"), + }; + + let (compiler, transform) = registry.find_compiler(TEST_TRANSFORM).expect("a compiler"); + let hash = compiler.compiler_hash(transform, &env).expect("valid hash"); + assert_eq!(hash, CompilerHash(7)); + + let source = ResourceTypeAndId { + kind: ResourceType::new(b"input"), + id: ResourceId::new(), + }; + let cas = ContentStoreAddr::from("."); + let proj_dir = PathBuf::from("."); + let compile_path = ResourcePathId::from(source).push(ResourceType::new(b"output")); + + // testing successful compilation + { + let registry = AssetRegistryOptions::new().create(); + let output = compiler + .compile( + compile_path.clone(), + &[], + &[], + registry, + cas, + &proj_dir, + &env, + ) + .expect("valid output"); + + assert_eq!(output.compiled_resources.len(), 1); + assert_eq!(output.compiled_resources[0].path, compile_path); + assert_eq!(output.compiled_resources[0].checksum, Checksum::from(7)); + assert_eq!(output.compiled_resources[0].size, 7); + } + } +} diff --git a/lib/data-compiler/src/compiler_reg/mod.rs b/lib/data-compiler/src/compiler_reg/mod.rs deleted file mode 100644 index e14cc29bbf..0000000000 --- a/lib/data-compiler/src/compiler_reg/mod.rs +++ /dev/null @@ -1,305 +0,0 @@ -//! Compiler registry defines an interface between different compiler types -//! (executable, in-process, etc). - -use core::fmt; -use std::{io, path::Path, sync::Arc}; - -use lgn_content_store::ContentStoreAddr; -use lgn_data_offline::{ResourcePathId, Transform}; -use lgn_data_runtime::{AssetRegistry, AssetRegistryOptions}; - -use crate::{ - compiler_api::{ - CompilationEnv, CompilationOutput, CompilerDescriptor, CompilerError, CompilerInfo, - }, - compiler_cmd::list_compilers, - CompiledResource, CompilerHash, -}; - -/// Interface allowing to support multiple types of compilers - in-process, -/// external executables. By returning multiple `CompilerInfo` via `info` the implementation -/// of `CompilerStub` can expose multiple compilers. -pub trait CompilerStub: Send + Sync { - /// Returns information about the compiler. - fn info(&self) -> io::Result>; - - /// Returns the `CompilerHash` for the given compilation context and transform. - fn compiler_hash(&self, transform: Transform, env: &CompilationEnv) - -> io::Result; - - fn init(&self, registry: AssetRegistryOptions) -> AssetRegistryOptions; - - /// Triggers compilation of provided `compile_path` and returns the - /// information about compilation output. - #[allow(clippy::too_many_arguments)] - fn compile( - &self, - compile_path: ResourcePathId, - dependencies: &[ResourcePathId], - derived_deps: &[CompiledResource], - registry: Arc, - cas_addr: ContentStoreAddr, - project_dir: &Path, - env: &CompilationEnv, - ) -> Result; -} - -mod inproc_stub; -use inproc_stub::InProcessCompilerStub; - -mod binary_stub; -use binary_stub::BinCompilerStub; - -/// Options and flags which can be used to configure how a compiler registry is -/// created. -#[derive(Default)] -pub struct CompilerRegistryOptions { - compilers: Vec>, -} - -impl CompilerRegistryOptions { - /// Creates `CompilerRegistry` based on provided compiler directory paths. - pub fn from_dirs(dirs: &[impl AsRef]) -> Self { - let compilers = list_compilers(dirs) - .into_iter() - .map(|info| { - let compiler: Box = Box::new(BinCompilerStub { - bin_path: info.path, - }); - compiler - }) - .collect::>>(); - - Self { compilers } - } - - /// Creates `CompilerRegistry` based on provided compiler directory path. - pub fn from_dir(dir: impl AsRef) -> Self { - Self::from_dirs(std::slice::from_ref(&dir)) - } - - /// Register an in-process compiler. - pub fn add_compiler(mut self, descriptor: &'static CompilerDescriptor) -> Self { - let compiler = Box::new(InProcessCompilerStub { descriptor }); - self.compilers.push(compiler); - self - } - - /// Creates a new compiler registry based on specified options. - pub fn create(self) -> CompilerRegistry { - let (infos, indices) = self.collect_info(); - - CompilerRegistry { - compilers: self.compilers, - infos, - indices, - } - } - - /// Gathers info on all compilers. - fn collect_info(&self) -> (Vec, Vec) { - let mut infos = vec![]; - let mut indices = vec![]; - for (index, compiler_stub) in self.compilers.iter().enumerate() { - match compiler_stub.info() { - Ok(compilers) => { - indices.extend(std::iter::repeat(index).take(compilers.len())); - infos.extend(compilers); - } - Err(_) => continue, - } - } - - (infos, indices) - } -} - -/// A registry of data compilers. -pub struct CompilerRegistry { - compilers: Vec>, - infos: Vec, - indices: Vec, -} - -impl fmt::Debug for CompilerRegistry { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("CompilerRegistry") - .field("infos", &self.infos) - .finish() - } -} - -impl CompilerRegistry { - /// Returns a reference to the compiler - pub fn find_compiler(&self, transform: Transform) -> Option<(&dyn CompilerStub, Transform)> { - if let Some(compiler_index) = self - .infos - .iter() - .position(|info| info.transform == transform) - { - let stub_index = self.indices[compiler_index]; - return Some((self.compilers[stub_index].as_ref(), transform)); - } - None - } - - /// A list of compilers available in the registry. - pub fn infos(&self) -> &Vec { - &self.infos - } - - pub fn init_all(&self, mut registry_options: AssetRegistryOptions) -> AssetRegistryOptions { - for compiler in &self.compilers { - registry_options = compiler.init(registry_options); - } - registry_options - } -} - -pub struct CompilerNode { - compilers: CompilerRegistry, - registry: Arc, -} - -impl CompilerNode { - pub fn new(compilers: CompilerRegistry, registry: Arc) -> Self { - Self { - compilers, - registry, - } - } - - pub fn compilers(&self) -> &CompilerRegistry { - &self.compilers - } - - pub fn registry(&self) -> Arc { - self.registry.clone() - } -} - -impl fmt::Debug for CompilerNode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("CompilerNode") - .field("compilers", &self.compilers) - .finish() - } -} - -#[cfg(test)] -mod tests { - use std::path::PathBuf; - - use lgn_content_store::{Checksum, ContentStoreAddr}; - use lgn_data_offline::{ResourcePathId, Transform}; - use lgn_data_runtime::{ - AssetRegistryOptions, Resource, ResourceId, ResourceType, ResourceTypeAndId, - }; - - use super::CompilerRegistryOptions; - use crate::{ - compiler_api::{CompilationEnv, CompilationOutput, CompilerDescriptor}, - CompiledResource, CompilerHash, Locale, Platform, Target, - }; - - const TEST_TRANSFORM: Transform = - Transform::new(ResourceType::new(b"input"), ResourceType::new(b"output")); - const TEST_COMPILER: CompilerDescriptor = CompilerDescriptor { - name: "test_name", - build_version: "build0", - code_version: "code0", - data_version: "data0", - transform: &TEST_TRANSFORM, - init_func: |options| options, - compiler_hash_func: |_code, _data, _env| CompilerHash(7), - compile_func: |ctx| { - Ok(CompilationOutput { - compiled_resources: vec![CompiledResource { - path: ctx.target_unnamed, - checksum: Checksum::from(7), - size: 7, - }], - resource_references: vec![], - }) - }, - }; - - #[test] - fn binary() { - let target_dir = std::env::current_exe().ok().map_or_else( - || panic!("cannot find test directory"), - |mut path| { - path.pop(); - if path.ends_with("deps") { - path.pop(); - } - path - }, - ); - - let registry = CompilerRegistryOptions::from_dir(target_dir).create(); - - let env = CompilationEnv { - target: Target::Game, - platform: Platform::Windows, - locale: Locale::new("en"), - }; - - let source = ResourceTypeAndId { - kind: text_resource::TextResource::TYPE, - id: ResourceId::new(), - }; - let destination = ResourcePathId::from(source).push(integer_asset::IntegerAsset::TYPE); - - let transform = Transform::new(source.kind, destination.content_type()); - - let (compiler, transform) = registry.find_compiler(transform).expect("valid compiler"); - let _ = compiler.compiler_hash(transform, &env).expect("valid hash"); - } - - #[test] - fn in_proc() { - let registry = CompilerRegistryOptions::default() - .add_compiler(&TEST_COMPILER) - .create(); - - let env = CompilationEnv { - target: Target::Game, - platform: Platform::Windows, - locale: Locale::new("en"), - }; - - let (compiler, transform) = registry.find_compiler(TEST_TRANSFORM).expect("a compiler"); - let hash = compiler.compiler_hash(transform, &env).expect("valid hash"); - assert_eq!(hash, CompilerHash(7)); - - let source = ResourceTypeAndId { - kind: ResourceType::new(b"input"), - id: ResourceId::new(), - }; - let cas = ContentStoreAddr::from("."); - let proj_dir = PathBuf::from("."); - let compile_path = ResourcePathId::from(source).push(ResourceType::new(b"output")); - - // testing successful compilation - { - let registry = AssetRegistryOptions::new().create(); - let output = compiler - .compile( - compile_path.clone(), - &[], - &[], - registry, - cas, - &proj_dir, - &env, - ) - .expect("valid output"); - - assert_eq!(output.compiled_resources.len(), 1); - assert_eq!(output.compiled_resources[0].path, compile_path); - assert_eq!(output.compiled_resources[0].checksum, Checksum::from(7)); - assert_eq!(output.compiled_resources[0].size, 7); - } - } -} diff --git a/lib/data-compiler/src/lib.rs b/lib/data-compiler/src/lib.rs index e153312c67..0f1fde0cbe 100644 --- a/lib/data-compiler/src/lib.rs +++ b/lib/data-compiler/src/lib.rs @@ -248,6 +248,6 @@ impl fmt::Display for Locale { pub mod compiler_api; pub mod compiler_cmd; +pub mod compiler_node; pub mod compiler_reflection; -pub mod compiler_reg; pub mod compiler_utils; diff --git a/lib/data-transaction/src/test_transaction.rs b/lib/data-transaction/src/test_transaction.rs index 95f224c32c..28d26c392b 100644 --- a/lib/data-transaction/src/test_transaction.rs +++ b/lib/data-transaction/src/test_transaction.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use generic_data::offline::TestEntity; use lgn_content_store::{ContentStoreAddr, HddContentStore}; use lgn_data_build::DataBuildOptions; -use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; +use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::resource::{Project, ResourcePathName, ResourceRegistryOptions}; use lgn_data_offline::ResourcePathId; use lgn_data_runtime::{ diff --git a/server/editor-srv/src/property_inspector_plugin.rs b/server/editor-srv/src/property_inspector_plugin.rs index 3191845a35..aa58d05d73 100644 --- a/server/editor-srv/src/property_inspector_plugin.rs +++ b/server/editor-srv/src/property_inspector_plugin.rs @@ -298,7 +298,7 @@ mod test { use generic_data::offline::TestEntity; use lgn_content_store::ContentStoreAddr; use lgn_data_build::DataBuildOptions; - use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; + use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::resource::{ Project, ResourcePathName, ResourceRegistry, ResourceRegistryOptions, }; diff --git a/server/editor-srv/src/resource_browser_plugin.rs b/server/editor-srv/src/resource_browser_plugin.rs index 129cca2704..d73978700b 100644 --- a/server/editor-srv/src/resource_browser_plugin.rs +++ b/server/editor-srv/src/resource_browser_plugin.rs @@ -246,7 +246,7 @@ mod test { use generic_data::offline::TestEntity; use lgn_content_store::ContentStoreAddr; use lgn_data_build::DataBuildOptions; - use lgn_data_compiler::compiler_reg::CompilerRegistryOptions; + use lgn_data_compiler::compiler_node::CompilerRegistryOptions; use lgn_data_offline::resource::{ Project, ResourcePathName, ResourceRegistry, ResourceRegistryOptions, }; diff --git a/server/ubercompiler/src/lib.rs b/server/ubercompiler/src/lib.rs index f4cd7a5211..8144ab6957 100644 --- a/server/ubercompiler/src/lib.rs +++ b/server/ubercompiler/src/lib.rs @@ -1,7 +1,7 @@ -use lgn_data_compiler::compiler_reg; +use lgn_data_compiler::compiler_node; -pub fn create() -> compiler_reg::CompilerRegistryOptions { - let registry = compiler_reg::CompilerRegistryOptions::default(); +pub fn create() -> compiler_node::CompilerRegistryOptions { + let registry = compiler_node::CompilerRegistryOptions::default(); registry .add_compiler(&lgn_compiler_material::COMPILER_INFO) .add_compiler(&lgn_compiler_debugcube::COMPILER_INFO) diff --git a/test/sample-data-compiler/src/offline_compiler/mod.rs b/test/sample-data-compiler/src/offline_compiler/mod.rs index fc537393a1..dee2e58cec 100644 --- a/test/sample-data-compiler/src/offline_compiler/mod.rs +++ b/test/sample-data-compiler/src/offline_compiler/mod.rs @@ -7,7 +7,7 @@ use std::{ use lgn_content_store::ContentStoreAddr; use lgn_data_build::DataBuildOptions; use lgn_data_compiler::{ - compiler_api::CompilationEnv, compiler_reg::CompilerRegistryOptions, Locale, Platform, Target, + compiler_api::CompilationEnv, compiler_node::CompilerRegistryOptions, Locale, Platform, Target, }; use lgn_data_offline::{resource::ResourcePathName, ResourcePathId}; use lgn_data_runtime::Resource;