Implement backwards compat for the enum changes
This commit is contained in:
parent
68cdc81c27
commit
0d52f59a9a
|
@ -28,7 +28,7 @@ import java.util.Set;
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public class Respackopts implements ClientModInitializer {
|
public class Respackopts implements ClientModInitializer {
|
||||||
public static final Integer META_VERSION = 1;
|
public static final Integer META_VERSION = 2;
|
||||||
public static final String ID = "respackopts";
|
public static final String ID = "respackopts";
|
||||||
public static final Identifier CONF_ID = new Identifier(ID, "conf.json");
|
public static final Identifier CONF_ID = new Identifier(ID, "conf.json");
|
||||||
public static final Logger LOGGER = LogManager.getFormatterLogger(ID);
|
public static final Logger LOGGER = LogManager.getFormatterLogger(ID);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package io.gitlab.jfronny.respackopts.data.entry;
|
package io.gitlab.jfronny.respackopts.data.entry;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.respackopts.data.RpoError;
|
||||||
|
|
||||||
public class ConfigBooleanEntry extends ConfigEntry<Boolean> {
|
public class ConfigBooleanEntry extends ConfigEntry<Boolean> {
|
||||||
public ConfigBooleanEntry(boolean v) {
|
public ConfigBooleanEntry(boolean v) {
|
||||||
setValue(v);
|
setValue(v);
|
||||||
|
@ -16,4 +18,12 @@ public class ConfigBooleanEntry extends ConfigEntry<Boolean> {
|
||||||
be.setDefault(getDefault());
|
be.setDefault(getDefault());
|
||||||
return be;
|
return be;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void buildShader(StringBuilder sb, String valueName) throws RpoError {
|
||||||
|
if (getValue()) {
|
||||||
|
sb.append("\n#define ");
|
||||||
|
sb.append(valueName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,7 @@ public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> void add(String name, ConfigEntry<T> val) {
|
public <T> void add(String name, ConfigEntry<T> val) {
|
||||||
|
val.setVersion(version);
|
||||||
super.getValue().put(name, val);
|
super.getValue().put(name, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,42 +79,10 @@ public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
|
||||||
return ImmutableMap.copyOf(super.getValue());
|
return ImmutableMap.copyOf(super.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildShader(StringBuilder sb, String valuePrefix) throws RpoError {
|
@Override
|
||||||
|
public void buildShader(StringBuilder sb, String valueName) throws RpoError {
|
||||||
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
|
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
|
||||||
if (e.getValue() instanceof ConfigNumericEntry n) {
|
e.getValue().buildShader(sb, valueName + "_" + e.getKey());
|
||||||
sb.append("\n#define ");
|
|
||||||
sb.append(valuePrefix);
|
|
||||||
sb.append(e.getKey());
|
|
||||||
sb.append(' ');
|
|
||||||
sb.append(n.getValue().toString());
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigBooleanEntry n) {
|
|
||||||
if (n.getValue()) {
|
|
||||||
sb.append("\n#define ");
|
|
||||||
sb.append(valuePrefix);
|
|
||||||
sb.append(e.getKey());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigEnumEntry n) {
|
|
||||||
sb.append("\n#define ");
|
|
||||||
sb.append(valuePrefix);
|
|
||||||
sb.append(e.getKey());
|
|
||||||
for (String e2 : n.values) {
|
|
||||||
if (e2.equals(n.getValue())) {
|
|
||||||
sb.append("\n#define ");
|
|
||||||
sb.append(valuePrefix);
|
|
||||||
sb.append(e.getKey());
|
|
||||||
sb.append('_');
|
|
||||||
sb.append(e2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (e.getValue() instanceof ConfigBranch n) {
|
|
||||||
n.buildShader(sb, valuePrefix + e.getKey() + "_");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new RpoError("Invalid type");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,4 +104,12 @@ public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
|
||||||
}
|
}
|
||||||
return branch;
|
return branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setVersion(int version) {
|
||||||
|
super.setVersion(version);
|
||||||
|
for (ConfigEntry<?> value : getValue().values()) {
|
||||||
|
value.setVersion(version);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
package io.gitlab.jfronny.respackopts.data.entry;
|
package io.gitlab.jfronny.respackopts.data.entry;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.respackopts.data.RpoError;
|
||||||
|
|
||||||
public abstract class ConfigEntry<T> {
|
public abstract class ConfigEntry<T> {
|
||||||
private T defaultValue;
|
private T defaultValue;
|
||||||
private T value;
|
private T value;
|
||||||
|
protected int version;
|
||||||
|
|
||||||
|
public void setVersion(int version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
public T getValue() {
|
public T getValue() {
|
||||||
if (value == null)
|
if (value == null)
|
||||||
|
@ -47,4 +54,6 @@ public abstract class ConfigEntry<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract ConfigEntry<T> clone();
|
public abstract ConfigEntry<T> clone();
|
||||||
|
|
||||||
|
public abstract void buildShader(StringBuilder sb, String valueName) throws RpoError;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package io.gitlab.jfronny.respackopts.data.entry;
|
package io.gitlab.jfronny.respackopts.data.entry;
|
||||||
|
|
||||||
import io.gitlab.jfronny.respackopts.Respackopts;
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||||
|
import io.gitlab.jfronny.respackopts.data.RpoError;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -81,4 +82,30 @@ public class ConfigEnumEntry extends ConfigEntry<String> {
|
||||||
e.setDefault(getDefault());
|
e.setDefault(getDefault());
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void buildShader(StringBuilder sb, String valueName) throws RpoError {
|
||||||
|
sb.append("\n#define ");
|
||||||
|
sb.append(valueName);
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(values.indexOf(getValue()));
|
||||||
|
for (int i = 0; i < values.size(); i++) {
|
||||||
|
String e2 = values.get(i);
|
||||||
|
if (version == 1) {
|
||||||
|
sb.append("\n#define ");
|
||||||
|
sb.append(valueName);
|
||||||
|
sb.append('_');
|
||||||
|
sb.append(e2);
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(i);
|
||||||
|
} else {
|
||||||
|
if (e2.equals(getValue())) {
|
||||||
|
sb.append("\n#define ");
|
||||||
|
sb.append(valueName);
|
||||||
|
sb.append('_');
|
||||||
|
sb.append(e2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package io.gitlab.jfronny.respackopts.data.entry;
|
package io.gitlab.jfronny.respackopts.data.entry;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.respackopts.data.RpoError;
|
||||||
|
|
||||||
public class ConfigNumericEntry extends ConfigEntry<Double> {
|
public class ConfigNumericEntry extends ConfigEntry<Double> {
|
||||||
public Double min;
|
public Double min;
|
||||||
public Double max;
|
public Double max;
|
||||||
|
@ -46,4 +48,12 @@ public class ConfigNumericEntry extends ConfigEntry<Double> {
|
||||||
ce.setDefault(getDefault());
|
ce.setDefault(getDefault());
|
||||||
return ce;
|
return ce;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void buildShader(StringBuilder sb, String valueName) throws RpoError {
|
||||||
|
sb.append("\n#define ");
|
||||||
|
sb.append(valueName);
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append(getValue().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class FrexCompat implements FrexInitializer {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (Map.Entry<String, ConfigBranch> e : Respackopts.CONFIG_BRANCH.entrySet()) {
|
for (Map.Entry<String, ConfigBranch> e : Respackopts.CONFIG_BRANCH.entrySet()) {
|
||||||
try {
|
try {
|
||||||
e.getValue().buildShader(sb, e.getKey() + "_");
|
e.getValue().buildShader(sb, e.getKey());
|
||||||
} catch (RpoError rpoError) {
|
} catch (RpoError rpoError) {
|
||||||
Respackopts.LOGGER.error("Could not build shader", rpoError);
|
Respackopts.LOGGER.error("Could not build shader", rpoError);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,14 @@ public class ResourcePackManagerMixin {
|
||||||
if (rpo$hasMetadata(v)) {
|
if (rpo$hasMetadata(v)) {
|
||||||
try {
|
try {
|
||||||
Respackmeta conf = Respackopts.GSON.fromJson(rpo$readMetadata(v, Respackopts.GSON), Respackmeta.class);
|
Respackmeta conf = Respackopts.GSON.fromJson(rpo$readMetadata(v, Respackopts.GSON), Respackmeta.class);
|
||||||
if (!Respackopts.META_VERSION.equals(conf.version)) {
|
if (Respackopts.META_VERSION < conf.version) {
|
||||||
Respackopts.LOGGER.error(s + " was not loaded as it specifies a different respackopts version than is installed");
|
Respackopts.LOGGER.error(s + " was not loaded as it specifies a newer respackopts version than is installed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (Respackopts.META_VERSION > conf.version) {
|
||||||
|
Respackopts.LOGGER.warn(s + " uses an outdated RPO format (" + conf.version + "). Although this is supported, using the latest version (" + Respackopts.META_VERSION + ") is recommended");
|
||||||
|
}
|
||||||
|
conf.conf.setVersion(conf.version);
|
||||||
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
|
if (!Respackopts.CONFIG_BRANCH.containsKey(conf.id))
|
||||||
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
|
Respackopts.CONFIG_BRANCH.put(conf.id, conf.conf);
|
||||||
else
|
else
|
||||||
|
@ -54,12 +58,12 @@ public class ResourcePackManagerMixin {
|
||||||
|
|
||||||
private JsonObject rpo$readMetadata(ResourcePackProfile v, Gson g) throws IOException {
|
private JsonObject rpo$readMetadata(ResourcePackProfile v, Gson g) throws IOException {
|
||||||
InputStream is = v.createResourcePack().open(ResourceType.CLIENT_RESOURCES, Respackopts.CONF_ID);
|
InputStream is = v.createResourcePack().open(ResourceType.CLIENT_RESOURCES, Respackopts.CONF_ID);
|
||||||
ByteArrayOutputStream bais = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int length;
|
int length;
|
||||||
while ((length = is.read(buffer)) != -1) {
|
while ((length = is.read(buffer)) != -1) {
|
||||||
bais.write(buffer, 0, length);
|
os.write(buffer, 0, length);
|
||||||
}
|
}
|
||||||
return g.fromJson(bais.toString(), JsonElement.class).getAsJsonObject();
|
return g.fromJson(os.toString(), JsonElement.class).getAsJsonObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user