Fixes parameter inconsistency in IFieldWrapper (Fixes #4325) (#4766)

This commit is contained in:
sinus authored and LexManos committed 2018-05-17 04:15:57 -07:00
1 parent fc1877dd96
commit 3c4efa1ae7
5 files changed
+130 -51

No files matched your search

@@ -220,8 +220,12 @@ public class ConfigManager
{
if (!Modifier.isPublic(f.getModifiers()))
continue;
//Only the root class may have static fields. Otherwise category tree nodes of the same type would share the
//contained value messing up the sync
if (Modifier.isStatic(f.getModifiers()) != (instance == null))
continue;
if (f.isAnnotationPresent(Config.Ignore.class))
continue;
@@ -230,7 +234,7 @@ public class ConfigManager
if (ca != null)
comment = NEW_LINE.join(ca.value());
String langKey = modid + "." + (category.isEmpty() ? "" : category + ".") + f.getName().toLowerCase(Locale.ENGLISH);
String langKey = modid + "." + (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + f.getName().toLowerCase(Locale.ENGLISH);
LangKey la = f.getAnnotation(LangKey.class);
if (la != null)
langKey = la.value();
@@ -238,7 +242,7 @@ public class ConfigManager
boolean requiresMcRestart = f.isAnnotationPresent(Config.RequiresMcRestart.class);
boolean requiresWorldRestart = f.isAnnotationPresent(Config.RequiresWorldRestart.class);
if (FieldWrapper.hasWrapperFor(f)) //Access the field
if (FieldWrapper.hasWrapperFor(f)) //Wrappers exist for primitives, enums, maps and arrays
{
if (Strings.isNullOrEmpty(category))
throw new RuntimeException("An empty category may not contain anything but objects representing categories!");
@@ -248,9 +252,9 @@ public class ConfigManager
ITypeAdapter adapt = wrapper.getTypeAdapter();
Property.Type propType = adapt.getType();
for (String key : wrapper.getKeys())
for (String key : wrapper.getKeys()) //Iterate the fully qualified property names the field provides
{
String suffix = StringUtils.replaceOnce(key, wrapper.getCategory() + ".", "");
String suffix = StringUtils.replaceOnce(key, wrapper.getCategory() + Configuration.CATEGORY_SPLITTER, "");
boolean existed = exists(cfg, wrapper.getCategory(), suffix);
if (!existed || loading) //Creates keys in category specified by the wrapper if new ones are programaticaly added
@@ -263,7 +267,7 @@ public class ConfigManager
else
wrapper.setValue(key, adapt.getValue(property));
}
else //If the key is not new, sync according to shoudlReadFromVar()
else //If the key is not new, sync according to shouldReadFromVar()
{
Property property = property(cfg, wrapper.getCategory(), suffix, propType, adapt.isArrayAdapter());
Object propVal = adapt.getValue(property);
@@ -278,31 +282,32 @@ public class ConfigManager
ConfigCategory confCat = cfg.getCategory(wrapper.getCategory());
for (Property property : confCat.getOrderedValues())//Are new keys in the Configuration object?
for (Property property : confCat.getOrderedValues()) //Iterate the properties to check for new data from the config side
{
if (!wrapper.handlesKey(property.getName()))
String key = confCat.getQualifiedName() + Configuration.CATEGORY_SPLITTER + property.getName();
if (!wrapper.handlesKey(key))
continue;
if (loading || !wrapper.hasKey(property.getName()))
if (loading || !wrapper.hasKey(key))
{
Object value = wrapper.getTypeAdapter().getValue(property);
wrapper.setValue(confCat.getName() + "." + property.getName(), value);
wrapper.setValue(key, value);
}
}
if (loading) //Doing this after the loops. The wrapper should set cosmetic stuff.
if (loading)
wrapper.setupConfiguration(cfg, comment, langKey, requiresMcRestart, requiresWorldRestart);
}
catch (Exception e) //If anything goes wrong, add the errored field and class.
catch (Exception e)
{
String format = "Error syncing field '%s' of class '%s'!";
String error = String.format(format, f.getName(), cls.getName());
throw new RuntimeException(error, e);
}
}
else if (f.getType().getSuperclass() != null && f.getType().getSuperclass().equals(Object.class)) //Descend the object tree
{
else if (f.getType().getSuperclass() != null && f.getType().getSuperclass().equals(Object.class))
{ //If the field extends Object directly, descend the object tree and access the objects members
Object newInstance = null;
try
{
@@ -310,11 +315,11 @@ public class ConfigManager
}
catch (IllegalAccessException e)
{
//This should never happen. Previous checks should eliminate this.
throw new RuntimeException(e);
}
String sub = (category.isEmpty() ? "" : category + ".") + getName(f).toLowerCase(Locale.ENGLISH);
//Setup the sub category with its respective name, comment, language key, etc.
String sub = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + getName(f).toLowerCase(Locale.ENGLISH);
ConfigCategory confCat = cfg.getCategory(sub);
confCat.setComment(comment);
confCat.setLanguageKey(langKey);
@@ -4,12 +4,9 @@ import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.Set;
import com.google.common.collect.Lists;
@@ -68,6 +65,7 @@ public abstract class FieldWrapper implements IFieldWrapper
{
private Map<String, Object> theMap = null;
private Type mType;
private final String baseName;
ITypeAdapter adapter;
@SuppressWarnings("unchecked")
@@ -75,6 +73,8 @@ public abstract class FieldWrapper implements IFieldWrapper
{
super(category, field, instance);
this.baseName = (this.category == null) ? "" : (this.category + ".") + this.name.toLowerCase(Locale.ENGLISH) + ".";
try
{
theMap = (Map<String, Object>) field.get(instance);
@@ -123,7 +123,7 @@ public abstract class FieldWrapper implements IFieldWrapper
Iterator<String> it = keys.iterator();
for (int i = 0; i < keyArray.length; i++)
{
keyArray[i] = category + "." + name + "." + it.next();
keyArray[i] = this.baseName + it.next();
}
return keyArray;
@@ -132,28 +132,27 @@ public abstract class FieldWrapper implements IFieldWrapper
@Override
public Object getValue(String key)
{
return theMap.get(StringUtils.replaceOnce(key, category + "." + name + ".", ""));
return theMap.get(getSuffix(key));
}
@Override
public void setValue(String key, Object value)
{
String suffix = StringUtils.replaceOnce(key, category + "." + name + ".", "");
theMap.put(suffix, value);
theMap.put(getSuffix(key), value);
}
@Override
public boolean hasKey(String name)
public boolean hasKey(String key)
{
return theMap.containsKey(name);
return theMap.containsKey(getSuffix(key));
}
@Override
public boolean handlesKey(String name)
public boolean handlesKey(String key)
{
if (name == null)
if (key == null)
return false;
return name.startsWith(category + "." + name + ".");
return key.startsWith(this.baseName);
}
@Override
@@ -169,7 +168,17 @@ public abstract class FieldWrapper implements IFieldWrapper
@Override
public String getCategory()
{
return category + "." + name;
return (this.category == null) ? "" : (this.category + ".") + this.name.toLowerCase(Locale.ENGLISH);
}
/**
* Removes the {@code this.baseName} prefix from the key
* @param key the key to be edited
* @return the keys suffix
*/
private String getSuffix(String key)
{
return StringUtils.replaceOnce(key, this.baseName, "");
}
}
@@ -336,15 +345,15 @@ public abstract class FieldWrapper implements IFieldWrapper
}
@Override
public boolean hasKey(String name)
public boolean hasKey(String key)
{
return (this.category + "." + this.name).equals(name);
return (this.category + "." + this.name).equals(key);
}
@Override
public boolean handlesKey(String name)
public boolean handlesKey(String key)
{
return hasKey(name);
return hasKey(key);
}
@Override
@@ -361,7 +370,7 @@ public abstract class FieldWrapper implements IFieldWrapper
@Override
public String getCategory()
{
return category;
return this.category;
}
}
@@ -2,6 +2,8 @@ package net.minecraftforge.common.config;
/**
* The objects are expected to get their wrapped field, the owning class, instance and category string on initialization.
* In general: The key is the fully qualified property name, where each subcategory is appended with a dot.
* i.e: general.map.isPresent
*/
public interface IFieldWrapper
{
@@ -12,24 +14,46 @@ public interface IFieldWrapper
ITypeAdapter getTypeAdapter();
/**
* @param field the field about which to retrieve information
* @param instance The instance whose field shall be queried.
* @return a list of keys handled by this field
* @return a list of fully qualified property keys handled by this field
*/
String[] getKeys();
/**
* @param key the fully qualified property key
* @return the value the wrapped field associates with the given key
*/
Object getValue(String key);
/**
* @param key the fully qualified property key
* @param value the target value of the property associated with the key
*/
void setValue(String key, Object value);
boolean hasKey(String name);
boolean handlesKey(String name);
/**
* @param key a fully qualified property key
* @return true if the wrapped field contains a property associated with the given key
*/
boolean hasKey(String key);
/**
* @param key a fully qualified property key
* @return true if the wrapped field can save information associated with the given key, false otherwise
*/
boolean handlesKey(String key);
/**
* @param cfg The configuration object holding the properties
* @param desc The properties description
* @param langKey The languageKey of the property, used in GUI
* @param reqMCRestart True, if a change in this property requires a restart of Minecraft
* @param reqWorldRestart True, if the world needs to be reloaded after changes to this property
*/
void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart);
/**
* @return the category name in which the entries should be saved.
* i.e. general.map in the example above
* @return the category name in which the entries should be saved. This includes the parent categories
*/
String getCategory();
}
@@ -20,15 +20,33 @@ package net.minecraftforge.common.config;
import net.minecraftforge.common.config.Property.Type;
/**
* Abstracts the types of properties away. Higher level logic must prevent invalid data types.
*/
interface ITypeAdapter
{
/**
* Assigns the default value to the property
* @param property the property whose default value will be assigned
* @param value the default value
*/
void setDefaultValue(Property property, Object value);
/**
* Sets the properties value.
* @param property the property whose value will be set
* @param value the set value
*/
void setValue(Property property, Object value);
/**
* Retrieves the properties value
* @param prop the property whose value will be retrieved
* @return the properties value
*/
Object getValue(Property prop);
Type getType();
boolean isArrayAdapter();
}