11package net .coderbot .iris ;
22
33import java .io .IOException ;
4+ import java .io .OutputStream ;
45import java .nio .file .FileSystem ;
56import java .nio .file .FileSystemNotFoundException ;
67import java .nio .file .FileSystems ;
2122import net .coderbot .iris .gui .screen .ShaderPackScreen ;
2223import net .coderbot .iris .pipeline .*;
2324import net .coderbot .iris .shaderpack .DimensionId ;
25+ import net .coderbot .iris .shaderpack .OptionalBoolean ;
2426import net .coderbot .iris .shaderpack .ProgramSet ;
2527import net .coderbot .iris .shaderpack .ShaderPack ;
28+ import net .coderbot .iris .shaderpack .option .OptionSet ;
2629import net .coderbot .iris .shaderpack .option .Profile ;
2730import net .coderbot .iris .shaderpack .discovery .ShaderpackDirectoryManager ;
31+ import net .coderbot .iris .shaderpack .option .values .MutableOptionValues ;
32+ import net .coderbot .iris .shaderpack .option .values .OptionValues ;
2833import net .fabricmc .loader .api .ModContainer ;
2934import net .irisshaders .iris .api .v0 .IrisApi ;
3035import net .minecraft .ChatFormatting ;
@@ -68,6 +73,9 @@ public class Iris implements ClientModInitializer {
6873 private static KeyMapping shaderpackScreenKeybind ;
6974
7075 private static final Map <String , String > shaderPackOptionQueue = new HashMap <>();
76+ // Flag variable used when reloading
77+ // Used in favor of queueDefaultShaderPackOptionValues() for resetting as the
78+ // behavior is more concrete and therefore is more likely to repair a user's issues
7179 private static boolean resetShaderPackOptions = false ;
7280
7381 private static String IRIS_VERSION ;
@@ -276,7 +284,7 @@ private static boolean loadExternalShaderpack(String name) {
276284 return false ;
277285 }
278286
279- Map <String , String > changedConfigs = loadConfigProperties (shaderPackConfigTxt )
287+ Map <String , String > changedConfigs = tryReadConfigProperties (shaderPackConfigTxt )
280288 .map (properties -> (Map <String , String >) (Map ) properties )
281289 .orElse (new HashMap <>());
282290
@@ -288,12 +296,17 @@ private static boolean loadExternalShaderpack(String name) {
288296 }
289297 resetShaderPackOptions = false ;
290298
291- Properties configsToSave = new Properties ();
292- configsToSave .putAll (changedConfigs );
293- saveConfigProperties (shaderPackConfigTxt , configsToSave );
294-
295299 try {
296300 currentPack = new ShaderPack (shaderPackPath , changedConfigs );
301+
302+ MutableOptionValues changedConfigsValues = currentPack .getShaderPackOptions ().getOptionValues ().mutableCopy ();
303+
304+ // Store changed values from those currently in use by the shader pack
305+ Properties configsToSave = new Properties ();
306+ changedConfigsValues .getBooleanValues ().forEach ((k , v ) -> configsToSave .setProperty (k , Boolean .toString (v )));
307+ changedConfigsValues .getStringValues ().forEach (configsToSave ::setProperty );
308+
309+ tryUpdateConfigPropertiesFile (shaderPackConfigTxt , configsToSave );
297310 } catch (Exception e ) {
298311 logger .error ("Failed to load the shaderpack \" {}\" !" , name );
299312 logger .catching (e );
@@ -358,24 +371,37 @@ private static void setShadersDisabled() {
358371 logger .info ("Shaders are disabled" );
359372 }
360373
361- private static Optional <Properties > loadConfigProperties (Path path ) {
374+ private static Optional <Properties > tryReadConfigProperties (Path path ) {
362375 Properties properties = new Properties ();
363376
364- try {
365- // NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
366- // so we don't need to do the UTF-8 workaround here.
367- properties .load (Files .newInputStream (path ));
368- } catch (IOException e ) {
369- // TODO: Better error handling
370- return Optional .empty ();
377+ if (Files .exists (path )) {
378+ try {
379+ // NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
380+ // so we don't need to do the UTF-8 workaround here.
381+ properties .load (Files .newInputStream (path ));
382+ } catch (IOException e ) {
383+ // TODO: Better error handling
384+ return Optional .empty ();
385+ }
371386 }
372387
373388 return Optional .of (properties );
374389 }
375390
376- private static void saveConfigProperties (Path path , Properties properties ) {
391+ private static void tryUpdateConfigPropertiesFile (Path path , Properties properties ) {
377392 try {
378- properties .store (Files .newOutputStream (path ), null );
393+ if (properties .isEmpty ()) {
394+ // Delete the file or don't create it if there are no changed configs
395+ if (Files .exists (path )) {
396+ Files .delete (path );
397+ }
398+
399+ return ;
400+ }
401+
402+ try (OutputStream out = Files .newOutputStream (path )) {
403+ properties .store (out , null );
404+ }
379405 } catch (IOException e ) {
380406 // TODO: Better error handling
381407 }
@@ -425,6 +451,34 @@ public static void queueShaderPackOptionsFromProfile(Profile profile) {
425451 getShaderPackOptionQueue ().putAll (profile .optionValues );
426452 }
427453
454+ public static void queueShaderPackOptionsFromProperties (Properties properties ) {
455+ queueDefaultShaderPackOptionValues ();
456+
457+ properties .stringPropertyNames ().forEach (key ->
458+ getShaderPackOptionQueue ().put (key , properties .getProperty (key )));
459+ }
460+
461+ // Used in favor of resetShaderPackOptions as the aforementioned requires the pack to be reloaded
462+ public static void queueDefaultShaderPackOptionValues () {
463+ clearShaderPackOptionQueue ();
464+
465+ getCurrentPack ().ifPresent (pack -> {
466+ OptionSet options = pack .getShaderPackOptions ().getOptionSet ();
467+ OptionValues values = pack .getShaderPackOptions ().getOptionValues ();
468+
469+ options .getStringOptions ().forEach ((key , mOpt ) -> {
470+ if (values .getStringValue (key ).isPresent ()) {
471+ getShaderPackOptionQueue ().put (key , mOpt .getOption ().getDefaultValue ());
472+ }
473+ });
474+ options .getBooleanOptions ().forEach ((key , mOpt ) -> {
475+ if (values .getBooleanValue (key ) != OptionalBoolean .DEFAULT ) {
476+ getShaderPackOptionQueue ().put (key , Boolean .toString (mOpt .getOption ().getDefaultValue ()));
477+ }
478+ });
479+ });
480+ }
481+
428482 public static void clearShaderPackOptionQueue () {
429483 getShaderPackOptionQueue ().clear ();
430484 }
0 commit comments