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 ;
2223import net .coderbot .iris .pipeline .*;
2324import net .coderbot .iris .pipeline .newshader .NewWorldRenderingPipeline ;
2425import net .coderbot .iris .shaderpack .DimensionId ;
26+ import net .coderbot .iris .shaderpack .OptionalBoolean ;
2527import net .coderbot .iris .shaderpack .ProgramSet ;
2628import net .coderbot .iris .shaderpack .ShaderPack ;
29+ import net .coderbot .iris .shaderpack .option .OptionSet ;
2730import net .coderbot .iris .shaderpack .option .Profile ;
2831import net .coderbot .iris .shaderpack .discovery .ShaderpackDirectoryManager ;
32+ import net .coderbot .iris .shaderpack .option .values .MutableOptionValues ;
33+ import net .coderbot .iris .shaderpack .option .values .OptionValues ;
2934import net .fabricmc .loader .api .ModContainer ;
3035import net .irisshaders .iris .api .v0 .IrisApi ;
3136import net .minecraft .ChatFormatting ;
@@ -69,6 +74,9 @@ public class Iris implements ClientModInitializer {
6974 private static KeyMapping shaderpackScreenKeybind ;
7075
7176 private static final Map <String , String > shaderPackOptionQueue = new HashMap <>();
77+ // Flag variable used when reloading
78+ // Used in favor of queueDefaultShaderPackOptionValues() for resetting as the
79+ // behavior is more concrete and therefore is more likely to repair a user's issues
7280 private static boolean resetShaderPackOptions = false ;
7381
7482 private static String IRIS_VERSION ;
@@ -277,7 +285,7 @@ private static boolean loadExternalShaderpack(String name) {
277285 return false ;
278286 }
279287
280- Map <String , String > changedConfigs = loadConfigProperties (shaderPackConfigTxt )
288+ Map <String , String > changedConfigs = tryReadConfigProperties (shaderPackConfigTxt )
281289 .map (properties -> (Map <String , String >) (Map ) properties )
282290 .orElse (new HashMap <>());
283291
@@ -289,12 +297,17 @@ private static boolean loadExternalShaderpack(String name) {
289297 }
290298 resetShaderPackOptions = false ;
291299
292- Properties configsToSave = new Properties ();
293- configsToSave .putAll (changedConfigs );
294- saveConfigProperties (shaderPackConfigTxt , configsToSave );
295-
296300 try {
297301 currentPack = new ShaderPack (shaderPackPath , changedConfigs );
302+
303+ MutableOptionValues changedConfigsValues = currentPack .getShaderPackOptions ().getOptionValues ().mutableCopy ();
304+
305+ // Store changed values from those currently in use by the shader pack
306+ Properties configsToSave = new Properties ();
307+ changedConfigsValues .getBooleanValues ().forEach ((k , v ) -> configsToSave .setProperty (k , Boolean .toString (v )));
308+ changedConfigsValues .getStringValues ().forEach (configsToSave ::setProperty );
309+
310+ tryUpdateConfigPropertiesFile (shaderPackConfigTxt , configsToSave );
298311 } catch (Exception e ) {
299312 logger .error ("Failed to load the shaderpack \" {}\" !" , name );
300313 logger .catching (e );
@@ -359,24 +372,37 @@ private static void setShadersDisabled() {
359372 logger .info ("Shaders are disabled" );
360373 }
361374
362- private static Optional <Properties > loadConfigProperties (Path path ) {
375+ private static Optional <Properties > tryReadConfigProperties (Path path ) {
363376 Properties properties = new Properties ();
364377
365- try {
366- // NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
367- // so we don't need to do the UTF-8 workaround here.
368- properties .load (Files .newInputStream (path ));
369- } catch (IOException e ) {
370- // TODO: Better error handling
371- return Optional .empty ();
378+ if (Files .exists (path )) {
379+ try {
380+ // NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
381+ // so we don't need to do the UTF-8 workaround here.
382+ properties .load (Files .newInputStream (path ));
383+ } catch (IOException e ) {
384+ // TODO: Better error handling
385+ return Optional .empty ();
386+ }
372387 }
373388
374389 return Optional .of (properties );
375390 }
376391
377- private static void saveConfigProperties (Path path , Properties properties ) {
392+ private static void tryUpdateConfigPropertiesFile (Path path , Properties properties ) {
378393 try {
379- properties .store (Files .newOutputStream (path ), null );
394+ if (properties .isEmpty ()) {
395+ // Delete the file or don't create it if there are no changed configs
396+ if (Files .exists (path )) {
397+ Files .delete (path );
398+ }
399+
400+ return ;
401+ }
402+
403+ try (OutputStream out = Files .newOutputStream (path )) {
404+ properties .store (out , null );
405+ }
380406 } catch (IOException e ) {
381407 // TODO: Better error handling
382408 }
@@ -426,6 +452,34 @@ public static void queueShaderPackOptionsFromProfile(Profile profile) {
426452 getShaderPackOptionQueue ().putAll (profile .optionValues );
427453 }
428454
455+ public static void queueShaderPackOptionsFromProperties (Properties properties ) {
456+ queueDefaultShaderPackOptionValues ();
457+
458+ properties .stringPropertyNames ().forEach (key ->
459+ getShaderPackOptionQueue ().put (key , properties .getProperty (key )));
460+ }
461+
462+ // Used in favor of resetShaderPackOptions as the aforementioned requires the pack to be reloaded
463+ public static void queueDefaultShaderPackOptionValues () {
464+ clearShaderPackOptionQueue ();
465+
466+ getCurrentPack ().ifPresent (pack -> {
467+ OptionSet options = pack .getShaderPackOptions ().getOptionSet ();
468+ OptionValues values = pack .getShaderPackOptions ().getOptionValues ();
469+
470+ options .getStringOptions ().forEach ((key , mOpt ) -> {
471+ if (values .getStringValue (key ).isPresent ()) {
472+ getShaderPackOptionQueue ().put (key , mOpt .getOption ().getDefaultValue ());
473+ }
474+ });
475+ options .getBooleanOptions ().forEach ((key , mOpt ) -> {
476+ if (values .getBooleanValue (key ) != OptionalBoolean .DEFAULT ) {
477+ getShaderPackOptionQueue ().put (key , Boolean .toString (mOpt .getOption ().getDefaultValue ()));
478+ }
479+ });
480+ });
481+ }
482+
429483 public static void clearShaderPackOptionQueue () {
430484 getShaderPackOptionQueue ().clear ();
431485 }
0 commit comments