Skip to content

Commit

Permalink
Fix slider reset tooltip priority, fix setting boolean options to def…
Browse files Browse the repository at this point in the history
…ault, only create text file when options are changed and exclude default options from text file
  • Loading branch information
FoundationGames committed Jan 17, 2022
1 parent c129f94 commit 9747638
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
51 changes: 35 additions & 16 deletions src/main/java/net/coderbot/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.coderbot.iris.shaderpack.option.OptionSet;
import net.coderbot.iris.shaderpack.option.Profile;
import net.coderbot.iris.shaderpack.discovery.ShaderpackDirectoryManager;
import net.coderbot.iris.shaderpack.option.values.MutableOptionValues;
import net.coderbot.iris.shaderpack.option.values.OptionValues;
import net.fabricmc.loader.api.ModContainer;
import net.irisshaders.iris.api.v0.IrisApi;
Expand Down Expand Up @@ -284,7 +285,7 @@ private static boolean loadExternalShaderpack(String name) {
return false;
}

Map<String, String> changedConfigs = loadConfigProperties(shaderPackConfigTxt)
Map<String, String> changedConfigs = tryReadConfigProperties(shaderPackConfigTxt)
.map(properties -> (Map<String, String>) (Map) properties)
.orElse(new HashMap<>());

Expand All @@ -296,12 +297,17 @@ private static boolean loadExternalShaderpack(String name) {
}
resetShaderPackOptions = false;

Properties configsToSave = new Properties();
configsToSave.putAll(changedConfigs);
saveConfigProperties(shaderPackConfigTxt, configsToSave);

try {
currentPack = new ShaderPack(shaderPackPath, changedConfigs);

MutableOptionValues changedConfigsValues = currentPack.getShaderPackOptions().getOptionValues().mutableCopy();

// Store changed values from those currently in use by the shader pack
Properties configsToSave = new Properties();
changedConfigsValues.getBooleanValues().forEach((k, v) -> configsToSave.setProperty(k, Boolean.toString(v)));
changedConfigsValues.getStringValues().forEach(configsToSave::setProperty);

tryUpdateConfigPropertiesFile(shaderPackConfigTxt, configsToSave);
} catch (Exception e) {
logger.error("Failed to load the shaderpack \"{}\"!", name);
logger.catching(e);
Expand Down Expand Up @@ -366,24 +372,37 @@ private static void setShadersDisabled() {
logger.info("Shaders are disabled");
}

private static Optional<Properties> loadConfigProperties(Path path) {
private static Optional<Properties> tryReadConfigProperties(Path path) {
Properties properties = new Properties();

try {
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
// so we don't need to do the UTF-8 workaround here.
properties.load(Files.newInputStream(path));
} catch (IOException e) {
// TODO: Better error handling
return Optional.empty();
if (Files.exists(path)) {
try {
// NB: config properties are specified to be encoded with ISO-8859-1 by OptiFine,
// so we don't need to do the UTF-8 workaround here.
properties.load(Files.newInputStream(path));
} catch (IOException e) {
// TODO: Better error handling
return Optional.empty();
}
}

return Optional.of(properties);
}

private static void saveConfigProperties(Path path, Properties properties) {
try (OutputStream out = Files.newOutputStream(path)) {
properties.store(out, null);
private static void tryUpdateConfigPropertiesFile(Path path, Properties properties) {
try {
if (properties.isEmpty()) {
// Delete the file or don't create it if there are no changed configs
if (Files.exists(path)) {
Files.delete(path);
}

return;
}

try (OutputStream out = Files.newOutputStream(path)) {
properties.store(out, null);
}
} catch (IOException e) {
// TODO: Better error handling
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean applyPreviousValue() {

@Override
public boolean applyOriginalValue() {
this.value = this.appliedValue;
this.value = this.option.getDefaultValue();
this.queue();

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public void render(PoseStack poseStack, int x, int y, int width, int height, int
this.renderSlider(poseStack, x, y, width, height, mouseX, mouseY, tickDelta);
}

if (!this.screen.isDisplayingComment()) {
renderTooltip(poseStack, this.unmodifiedLabel, mouseX, mouseY, hovered);
} else if (Screen.hasShiftDown()) {
if (Screen.hasShiftDown()) {
renderTooltip(poseStack, SET_TO_DEFAULT, mouseX, mouseY, hovered);
} else if (!this.screen.isDisplayingComment()) {
renderTooltip(poseStack, this.unmodifiedLabel, mouseX, mouseY, hovered);
}

if (this.mouseDown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public class MutableOptionValues implements OptionValues {
private final Map<String, Boolean> booleanValues;
private final Map<String, String> stringValues;

MutableOptionValues(OptionSet options, Map<String, Boolean> flippedBooleanValues, Map<String, String> stringValues) {
MutableOptionValues(OptionSet options, Map<String, Boolean> booleanValues, Map<String, String> stringValues) {
Map<String, String> values = new HashMap<>();

booleanValues.forEach((k, v) -> values.put(k, Boolean.toString(v)));
values.putAll(stringValues);

this.options = options;
this.booleanValues = flippedBooleanValues;
this.stringValues = stringValues;
this.booleanValues = new HashMap<>();
this.stringValues = new HashMap<>();

this.addAll(values);
}

public MutableOptionValues(OptionSet options, Map<String, String> values) {
Expand Down

0 comments on commit 9747638

Please sign in to comment.