preprocess: support latin-1 encoding

This commit is contained in:
chyyran 2023-01-28 11:58:59 -05:00
parent b5ce7ce30d
commit 43b7d6fb53
6 changed files with 43 additions and 5 deletions

10
Cargo.lock generated
View file

@ -338,6 +338,15 @@ dependencies = [
"wio", "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]] [[package]]
name = "expat-sys" name = "expat-sys"
version = "2.1.6" version = "2.1.6"
@ -662,6 +671,7 @@ dependencies = [
name = "librashader-preprocess" name = "librashader-preprocess"
version = "0.1.0-beta.9" version = "0.1.0-beta.9"
dependencies = [ dependencies = [
"encoding_rs",
"librashader-common", "librashader-common",
"nom", "nom",
"rustc-hash", "rustc-hash",

View file

@ -16,6 +16,7 @@ thiserror = "1.0.37"
nom = "7.1.1" nom = "7.1.1"
librashader-common = { path = "../librashader-common", version = "0.1.0-beta.9" } librashader-common = { path = "../librashader-common", version = "0.1.0-beta.9" }
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
encoding_rs = "0.8.31"
[features] [features]
default = [ "line_directives" ] default = [ "line_directives" ]

View file

@ -11,6 +11,9 @@ pub enum PreprocessError {
/// An IO error occurred when reading the source file. /// An IO error occurred when reading the source file.
#[error("the file was not found during resolution")] #[error("the file was not found during resolution")]
IOError(PathBuf, std::io::Error), 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. /// Unexpected EOF when reading the source file.
#[error("unexpected end of file")] #[error("unexpected end of file")]
UnexpectedEof, UnexpectedEof,

View file

@ -3,6 +3,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use std::str::Lines; use std::str::Lines;
use encoding_rs::{DecoderResult, WINDOWS_1252};
#[cfg(feature = "line_directives")] #[cfg(feature = "line_directives")]
const GL_GOOGLE_CPP_STYLE_LINE_DIRECTIVE: &str = 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<Path>) -> Result<String, PreprocessError> { fn read_file(path: impl AsRef<Path>) -> Result<String, PreprocessError> {
let path = path.as_ref(); let path = path.as_ref();
let mut source = String::new(); let mut buf = Vec::new();
File::open(path) 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))?; .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<Path>) -> Result<String, PreprocessError> { pub fn read_source(path: impl AsRef<Path>) -> Result<String, PreprocessError> {

View file

@ -261,7 +261,7 @@ where
blame: SemanticErrorBlame, blame: SemanticErrorBlame,
) -> Result<u32, ShaderReflectError> { ) -> Result<u32, ShaderReflectError> {
let size = ast.get_declared_struct_size(push.base_type_id)?; 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))); return Err(blame.error(SemanticsErrorKind::InvalidPushBufferSize(size)));
} }
Ok(size) Ok(size)

View file

@ -33,7 +33,7 @@ mod tests {
#[test] #[test]
fn triangle_d3d11() { fn triangle_d3d11() {
let sample = hello_triangle::d3d11_hello_triangle::Sample::new( 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", // "../test/basic.slangp",
Some(&FilterChainOptionsD3D11 { Some(&FilterChainOptionsD3D11 {
use_deferred_context: false, use_deferred_context: false,