diff --git a/Cargo.lock b/Cargo.lock index b926c5a..34a99ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -338,6 +338,15 @@ dependencies = [ "wio", ] +[[package]] +name = "encoding_rs" +version = "0.8.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" +dependencies = [ + "cfg-if", +] + [[package]] name = "expat-sys" version = "2.1.6" @@ -662,6 +671,7 @@ dependencies = [ name = "librashader-preprocess" version = "0.1.0-beta.9" dependencies = [ + "encoding_rs", "librashader-common", "nom", "rustc-hash", diff --git a/librashader-preprocess/Cargo.toml b/librashader-preprocess/Cargo.toml index b7e9f60..30fd745 100644 --- a/librashader-preprocess/Cargo.toml +++ b/librashader-preprocess/Cargo.toml @@ -16,6 +16,7 @@ thiserror = "1.0.37" nom = "7.1.1" librashader-common = { path = "../librashader-common", version = "0.1.0-beta.9" } rustc-hash = "1.1.0" +encoding_rs = "0.8.31" [features] default = [ "line_directives" ] diff --git a/librashader-preprocess/src/error.rs b/librashader-preprocess/src/error.rs index 3d38cdf..f7b974d 100644 --- a/librashader-preprocess/src/error.rs +++ b/librashader-preprocess/src/error.rs @@ -11,6 +11,9 @@ pub enum PreprocessError { /// An IO error occurred when reading the source file. #[error("the file was not found during resolution")] IOError(PathBuf, std::io::Error), + /// A known encoding was not found for the file. + #[error("a known encoding was not found for the file. supported encodings are UTF-8 and Latin-1")] + EncodingError(PathBuf), /// Unexpected EOF when reading the source file. #[error("unexpected end of file")] UnexpectedEof, diff --git a/librashader-preprocess/src/include.rs b/librashader-preprocess/src/include.rs index 2b59a40..075001b 100644 --- a/librashader-preprocess/src/include.rs +++ b/librashader-preprocess/src/include.rs @@ -3,6 +3,7 @@ use std::fs::File; use std::io::Read; use std::path::Path; use std::str::Lines; +use encoding_rs::{DecoderResult, WINDOWS_1252}; #[cfg(feature = "line_directives")] const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &str = @@ -10,11 +11,34 @@ const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &str = fn read_file(path: impl AsRef) -> Result { let path = path.as_ref(); - let mut source = String::new(); + let mut buf = Vec::new(); File::open(path) - .and_then(|mut f| f.read_to_string(&mut source)) + .and_then(|mut f| { + f.read_to_end(&mut buf)?; + Ok(()) + }) .map_err(|e| PreprocessError::IOError(path.to_path_buf(), e))?; - Ok(source) + + match String::from_utf8(buf) { + Ok(s) => Ok(s), + Err(e) => { + let buf = e.into_bytes(); + let decoder = WINDOWS_1252.new_decoder(); + let Some(len) = decoder.max_utf8_buffer_length_without_replacement(buf.len()) else { + return Err(PreprocessError::EncodingError(path.to_path_buf())) + }; + + let mut latin1_string = String::with_capacity(len); + + let (result, _) = WINDOWS_1252.new_decoder() + .decode_to_string_without_replacement(&buf, &mut latin1_string, true); + if result == DecoderResult::InputEmpty { + Ok(latin1_string) + } else { + Err(PreprocessError::EncodingError(path.to_path_buf())) + } + } + } } pub fn read_source(path: impl AsRef) -> Result { diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 3484a86..fd83db8 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -261,7 +261,7 @@ where blame: SemanticErrorBlame, ) -> Result { let size = ast.get_declared_struct_size(push.base_type_id)?; - if size >= MAX_PUSH_BUFFER_SIZE { + if size > MAX_PUSH_BUFFER_SIZE { return Err(blame.error(SemanticsErrorKind::InvalidPushBufferSize(size))); } Ok(size) diff --git a/librashader-runtime-d3d11/src/lib.rs b/librashader-runtime-d3d11/src/lib.rs index c4e6d91..2ca36b8 100644 --- a/librashader-runtime-d3d11/src/lib.rs +++ b/librashader-runtime-d3d11/src/lib.rs @@ -33,7 +33,7 @@ mod tests { #[test] fn triangle_d3d11() { let sample = hello_triangle::d3d11_hello_triangle::Sample::new( - "../test/slang-shaders/crt/crt-lottes.slangp", + "../test/slang-shaders/presets/crt-royale-kurozumi.slangp", // "../test/basic.slangp", Some(&FilterChainOptionsD3D11 { use_deferred_context: false,