1use toml_edit::DocumentMut; 2 3#[must_use] 4pub fn migrate_document(document: &mut DocumentMut) -> bool { 5 let mut changed = false; 6 7 if let Some((_, common_value)) = document.get_key_value_mut("common") 8 && let Some(common) = common_value.as_table_like_mut() 9 { 10 // v0.12.0: Removed OpenGL wgpu backend option 11 if let Some((_, wgpu_backend)) = common.get_key_value_mut("wgpu_backend") 12 && wgpu_backend.as_str() == Some("OpenGl") 13 { 14 log::info!("OpenGL wgpu backend option no longer exists; changing to Auto"); 15 16 *wgpu_backend = toml_edit::value("Auto"); 17 changed = true; 18 } 19 20 // v0.12.0: Moved anti-dither shaders from preprocess_shader to their own config field 21 if let Some((_, preprocess_shader)) = common.get_key_value_mut("preprocess_shader") { 22 match preprocess_shader.as_str() { 23 Some("AntiDitherWeak") => { 24 *preprocess_shader = toml_edit::value("None"); 25 common.insert("anti_dither_shader", toml_edit::value("Weak")); 26 27 changed = true; 28 } 29 Some("AntiDitherStrong") => { 30 *preprocess_shader = toml_edit::value("None"); 31 common.insert("anti_dither_shader", toml_edit::value("Strong")); 32 33 changed = true; 34 } 35 _ => {} 36 } 37 } 38 39 // v0.12.0: Changed scanlines from an enum to a bool+f64 pair 40 if let Some((_, scanlines)) = common.get_key_value_mut("scanlines") { 41 match scanlines.as_str() { 42 Some("Dim") => { 43 *scanlines = toml_edit::Item::None; 44 common.insert("scanlines_enabled", toml_edit::value(true)); 45 common.insert("scanlines_brightness", toml_edit::value(0.5)); 46 47 changed = true; 48 } 49 Some("Black") => { 50 *scanlines = toml_edit::Item::None; 51 common.insert("scanlines_enabled", toml_edit::value(true)); 52 common.insert("scanlines_brightness", toml_edit::value(0.0)); 53 54 changed = true; 55 } 56 _ => {} 57 } 58 } 59 } 60 61 changed 62} 63 64#[cfg(test)] 65mod tests { 66 use crate::{AppConfig, migrate_config_str}; 67 use jgenesis_renderer::config::{AntiDitherShader, PreprocessShader, WgpuBackend}; 68 69 #[test] 70 fn v0_12_0_opengl() { 71 const OLD_STR: &str = " 72[common] 73wgpu_backend = \"OpenGl\" 74"; 75 76 let mut config_str = OLD_STR.to_owned(); 77 migrate_config_str(&mut config_str); 78 let config: AppConfig = toml::from_str(&config_str).expect("Failed to parse config"); 79 assert_eq!(config.common.wgpu_backend, WgpuBackend::Auto); 80 } 81 82 fn v0_12_0_anti_dither(preprocess_str: &str, expected_anti_dither: AntiDitherShader) { 83 let mut config_str = format!( 84 " 85[common] 86preprocess_shader = \"{preprocess_str}\" 87" 88 ); 89 90 migrate_config_str(&mut config_str); 91 let config: AppConfig = toml::from_str(&config_str).expect("Failed to parse config"); 92 assert_eq!(config.common.anti_dither_shader, expected_anti_dither); 93 assert_eq!(config.common.preprocess_shader, PreprocessShader::None); 94 } 95 96 #[test] 97 fn v0_12_0_anti_dither_weak() { 98 v0_12_0_anti_dither("AntiDitherWeak", AntiDitherShader::Weak); 99 } 100 101 #[test] 102 fn v0_12_0_anti_dither_strong() { 103 v0_12_0_anti_dither("AntiDitherStrong", AntiDitherShader::Strong); 104 } 105 106 fn v0_12_0_scanlines(prev_enum_value: &str, expected_brightness: f64) { 107 let mut config_str = format!( 108 " 109[common] 110scanlines = \"{prev_enum_value}\" 111 " 112 ); 113 114 migrate_config_str(&mut config_str); 115 116 let config: AppConfig = toml::from_str(&config_str).expect("Failed to parse config"); 117 assert!(config.common.scanlines_enabled); 118 assert_eq!(config.common.scanlines_brightness, expected_brightness); 119 } 120 121 #[test] 122 fn v0_12_0_scanlines_dim() { 123 v0_12_0_scanlines("Dim", 0.5); 124 } 125 126 #[test] 127 fn v0_12_0_scanlines_black() { 128 v0_12_0_scanlines("Black", 0.0); 129 } 130}