Skip to content

Commit

Permalink
Fix AOT issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Delsin-Yu committed May 18, 2024
1 parent 9f80c4f commit a1843cc
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 61 deletions.
9 changes: 2 additions & 7 deletions JsonPowerInspector/Scripts/InspectionSessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private async GDTask<bool> MountJsonObjectAsync(JsonObject jsonObject)

foreach (var propertyInfo in _mainObjectDefinition.Properties)
{
var inspectorForProperty = Utils.CreateInspectorForProperty(propertyInfo, InspectorSpawner);
var inspectorForProperty = Utils.CreateInspectorForProperty(propertyInfo, InspectorSpawner, false);
_inspectorRoot.Add(inspectorForProperty);
_container.AddChild((Control)inspectorForProperty);
}
Expand Down Expand Up @@ -237,10 +237,6 @@ private void ResetControls()
_inspectorRoot.Clear();
}

private readonly JsonSerializerOptions _options = new() { WriteIndented = true, };

[UnconditionalSuppressMessage("Warning", "IL3050")]
[UnconditionalSuppressMessage("Warning", "IL2026")]
public async GDTask Save()
{
PickPath:
Expand All @@ -254,8 +250,7 @@ public async GDTask Save()
string jsonString;
try
{
if(!_options.IsReadOnly) _options.MakeReadOnly(true);
jsonString = _editingJsonObject.ToJsonString(_options);
jsonString = _editingJsonObject.ToJsonString(new() { WriteIndented = true });
}
catch (Exception e)
{
Expand Down
28 changes: 16 additions & 12 deletions JsonPowerInspector/Scripts/InspectorSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,25 @@ InspectionSessionController currentSession
_dropdownInspector = dropdownInspector;
}

public StringInspector Create(StringPropertyInfo stringPropertyInfo) => Print<StringInspector, StringPropertyInfo>(_stringInspector, stringPropertyInfo);
public NumberInspector Create(NumberPropertyInfo numberPropertyInfo) => Print<NumberInspector, NumberPropertyInfo>(_numberInspector, numberPropertyInfo);
public ObjectInspector Create(ObjectPropertyInfo objectPropertyInfo) => Print<ObjectInspector, ObjectPropertyInfo>(_objectInspector, objectPropertyInfo);
public BooleanInspector Create(BooleanPropertyInfo booleanPropertyInfo) => Print<BooleanInspector, BooleanPropertyInfo>(_booleanInspector, booleanPropertyInfo);
public ArrayInspector Create(ArrayPropertyInfo arrayPropertyInfo) => Print<ArrayInspector, ArrayPropertyInfo>(_arrayInspector, arrayPropertyInfo);
public DictionaryInspector Create(DictionaryPropertyInfo dictionaryPropertyInfo) => Print<DictionaryInspector, DictionaryPropertyInfo>(_dictionaryInspector, dictionaryPropertyInfo);
public EnumInspector Create(EnumPropertyInfo enumPropertyInfo) => Print<EnumInspector, EnumPropertyInfo>(_enumInspector, enumPropertyInfo);
public DropdownInspector Create(DropdownPropertyInfo dropdownPropertyInfo) => Print<DropdownInspector, DropdownPropertyInfo>(_dropdownInspector, dropdownPropertyInfo);

private TInspector Print<TInspector, TPropertyInfo>(PackedScene inspectorPrefab, TPropertyInfo propertyInfo)
where TInspector : BasePropertyInspector<TPropertyInfo>
public StringInspector Create(StringPropertyInfo stringPropertyInfo, bool affectMainObject) => Print<StringInspector, StringPropertyInfo>(_stringInspector, stringPropertyInfo, affectMainObject);
public NumberInspector Create(NumberPropertyInfo numberPropertyInfo, bool affectMainObject) => Print<NumberInspector, NumberPropertyInfo>(_numberInspector, numberPropertyInfo, affectMainObject);
public ObjectInspector Create(ObjectPropertyInfo objectPropertyInfo, bool affectMainObject) => Print<ObjectInspector, ObjectPropertyInfo>(_objectInspector, objectPropertyInfo, affectMainObject);
public BooleanInspector Create(BooleanPropertyInfo booleanPropertyInfo, bool affectMainObject) => Print<BooleanInspector, BooleanPropertyInfo>(_booleanInspector, booleanPropertyInfo, affectMainObject);
public ArrayInspector Create(ArrayPropertyInfo arrayPropertyInfo, bool affectMainObject) => Print<ArrayInspector, ArrayPropertyInfo>(_arrayInspector, arrayPropertyInfo, affectMainObject);
public DictionaryInspector Create(DictionaryPropertyInfo dictionaryPropertyInfo, bool affectMainObject) => Print<DictionaryInspector, DictionaryPropertyInfo>(_dictionaryInspector, dictionaryPropertyInfo, affectMainObject);
public EnumInspector Create(EnumPropertyInfo enumPropertyInfo, bool affectMainObject) => Print<EnumInspector, EnumPropertyInfo>(_enumInspector, enumPropertyInfo, affectMainObject);
public DropdownInspector Create(DropdownPropertyInfo dropdownPropertyInfo, bool affectMainObject) => Print<DropdownInspector, DropdownPropertyInfo>(_dropdownInspector, dropdownPropertyInfo, affectMainObject);

private TInspector Print<TInspector, TPropertyInfo>(
PackedScene inspectorPrefab,
TPropertyInfo propertyInfo,
bool affectMainObject
)
where TInspector : BasePropertyInspector<TPropertyInfo>
where TPropertyInfo : BaseObjectPropertyInfo
{
var instance = inspectorPrefab.Instantiate<TInspector>();
instance.Initialize(propertyInfo, _currentSession);
instance.Initialize(propertyInfo, _currentSession, affectMainObject);
return instance;
}
}
32 changes: 21 additions & 11 deletions JsonPowerInspector/Scripts/Inspectors/BasePropertyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IPropertyInspector
string DisplayName { set; }
string BackingPropertyName { set; }
void BindJsonNode(JsonNode parent, string propertyName);
event Action<object> ValueChanged;
event Action<JsonValue> ValueChanged;
}

public abstract partial class BasePropertyInspector<TPropertyInfo> : Control, IPropertyInspector where TPropertyInfo : BaseObjectPropertyInfo
Expand All @@ -32,19 +32,21 @@ public string BackingPropertyName
set => _jsonPropertyName = value;
}

public event Action<object> ValueChanged;
public event Action<JsonValue> ValueChanged;
protected TPropertyInfo PropertyInfo { get; private set; }
protected InspectionSessionController CurrentSession { get; private set; }

private JsonNode _parent;
private string _jsonPropertyName;
private string _displayName;
private bool _affectMainObject;

public void Initialize(TPropertyInfo propertyInfo, InspectionSessionController currentSession)
public void Initialize(TPropertyInfo propertyInfo, InspectionSessionController currentSession, bool affectMainObject)
{
CurrentSession = currentSession;
DisplayName = propertyInfo.DisplayName;
PropertyInfo = propertyInfo;
_affectMainObject = affectMainObject;
OnInitialize(propertyInfo);
}

Expand Down Expand Up @@ -82,17 +84,25 @@ protected void SetBackingNode(JsonNode jsonNode)
default:
throw new InvalidOperationException(_parent.GetType().Name);
}
CurrentSession.MarkChanged();
if(_affectMainObject) CurrentSession.MarkChanged();
}

[UnconditionalSuppressMessage("Warning", "IL3050")]
[UnconditionalSuppressMessage("Warning", "IL2026")]
protected void ReplaceValue<TValue>(TValue value)

protected void ReplaceValue(JsonValue value)
{
var node = GetBackingNode();
if (node is not JsonValue jsonValue) throw new InvalidOperationException($"{node.GetValueKind()} is not JsonValue!");
CurrentSession.MarkChanged();
jsonValue.ReplaceWith(value);
if (node is not JsonValue) throw new InvalidOperationException($"{node.GetValueKind()} is not JsonValue!");
switch (node.Parent)
{
case JsonObject jsonObject:
jsonObject[node.GetPropertyName()] = value;
break;
case JsonArray jsonArray:
jsonArray[node.GetElementIndex()] = value;
break;
default:
throw new InvalidOperationException(node.Parent?.GetType().Name);
}
if(_affectMainObject) CurrentSession.MarkChanged();
ValueChanged?.Invoke(value);
}

Expand Down
2 changes: 1 addition & 1 deletion JsonPowerInspector/Scripts/Inspectors/BooleanInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ protected override void Bind(ref JsonNode node)
{
var jsonValue = node.AsValue();
_contentControl.ButtonPressed = jsonValue.GetValue<bool>();
_contentControl.Toggled += ReplaceValue;
_contentControl.Toggled += value => ReplaceValue(JsonValue.Create(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private void BindArrayItem(InspectorSpawner spawner, int index, JsonNode node)
{
var inspector = Utils.CreateInspectorForProperty(
PropertyInfo.ArrayElementTypeInfo,
spawner
spawner,
true
);
inspector.BindJsonNode(node, index.ToString());
var arrayItem = _arrayElement.Instantiate<ArrayItem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ private void BindDictionaryItem(InspectorSpawner spawner, string key, JsonObject
{
var inspector = Utils.CreateInspectorForProperty(
PropertyInfo.ValueTypeInfo,
spawner
spawner,
true
);
inspector.BindJsonNode(jsonObject, key);
var dictionaryItem = _dictionaryElement.Instantiate<DictionaryItem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public override void _Ready()
public GDTask<(bool, double)> ShowAsync(NumberPropertyInfo numberPropertyInfo, InspectorSpawner inspectorSpawner, HashSet<double> bannedKey) =>
ShowAsync(
numberPropertyInfo,
static (spawner, info) => spawner.Create(info),
static (spawner, info) => spawner.Create(info, false),
val => JsonValue.Create(val),
inspectorSpawner,
bannedKey,
0.0
Expand All @@ -36,7 +37,8 @@ public override void _Ready()
public GDTask<(bool, string)> ShowAsync(StringPropertyInfo stringPropertyInfo, InspectorSpawner inspectorSpawner, HashSet<string> bannedKey) =>
ShowAsync(
stringPropertyInfo,
static (spawner, info) => spawner.Create(info),
static (spawner, info) => spawner.Create(info, false),
val => JsonValue.Create(val),
inspectorSpawner,
bannedKey,
""
Expand All @@ -45,7 +47,8 @@ public override void _Ready()
public GDTask<(bool, int)> ShowAsyncInt(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet<int> bannedKey) =>
ShowAsync(
dropdownPropertyInfo,
static (spawner, info) => spawner.Create(info),
static (spawner, info) => spawner.Create(info, false),
val => JsonValue.Create(val),
inspectorSpawner,
bannedKey,
0
Expand All @@ -54,7 +57,8 @@ public override void _Ready()
public GDTask<(bool, double)> ShowAsyncFloat(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet<double> bannedKey) =>
ShowAsync(
dropdownPropertyInfo,
static (spawner, info) => spawner.Create(info),
static (spawner, info) => spawner.Create(info, false),
val => JsonValue.Create(val),
inspectorSpawner,
bannedKey,
0.0
Expand All @@ -63,7 +67,8 @@ public override void _Ready()
public GDTask<(bool, string)> ShowAsyncString(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet<string> bannedKey) =>
ShowAsync(
dropdownPropertyInfo,
static (spawner, info) => spawner.Create(info),
static (spawner, info) => spawner.Create(info, false),
val => JsonValue.Create(val),
inspectorSpawner,
bannedKey,
""
Expand All @@ -75,11 +80,10 @@ public override void _Notification(int what)
_removeClicked = true;
}

[UnconditionalSuppressMessage("Warning", "IL3050")]
[UnconditionalSuppressMessage("Warning", "IL2026")]
private async GDTask<(bool, TValue)> ShowAsync<TPropertyInfo, TValue>(
TPropertyInfo propertyInfo,
Func<InspectorSpawner, TPropertyInfo, IPropertyInspector> inspectorFactory,
Func<TValue, JsonValue> jsonValueFactory,
InspectorSpawner spawner,
IReadOnlySet<TValue> bannedKey,
TValue defaultValue
Expand All @@ -88,13 +92,12 @@ TValue defaultValue
var inspector = inspectorFactory(spawner, propertyInfo);
inspector.ValueChanged += value =>
{
var typedValue = (TValue)value;
var addBtnDisabled = bannedKey.Contains(typedValue);
var addBtnDisabled = bannedKey.Contains(value.GetValue<TValue>());
UpdateAddButton(addBtnDisabled);
};
UpdateAddButton(bannedKey.Contains(defaultValue));
var emptyJsonObject = new JsonObject();
JsonNode jsonValueNode = JsonValue.Create(defaultValue);
JsonNode jsonValueNode = jsonValueFactory(defaultValue);
const string name = "TempJsonNode";
emptyJsonObject.Add(name, jsonValueNode);
_container.AddChild((Control)inspector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void BindObject(JsonNode node)
for (var index = 0; index < span.Length; index++)
{
var info = span[index];
var inspector = Utils.CreateInspectorForProperty(info, CurrentSession.InspectorSpawner);
var inspector = Utils.CreateInspectorForProperty(info, CurrentSession.InspectorSpawner, true);
AddChildNode(inspector, (Control)inspector);
var propertyName = jsonProperties[index].Key;
inspector.BindJsonNode(jsonObject, propertyName);
Expand Down
6 changes: 3 additions & 3 deletions JsonPowerInspector/Scripts/Inspectors/DropdownInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ private void ReplaceDropdownValue(long index)
switch (PropertyInfo.Kind)
{
case DropdownPropertyInfo.DropdownKind.Int:
ReplaceValue(int.Parse(stringValue));
ReplaceValue(JsonValue.Create(int.Parse(stringValue)));
break;
case DropdownPropertyInfo.DropdownKind.Float:
ReplaceValue(double.Parse(stringValue));
ReplaceValue(JsonValue.Create(double.Parse(stringValue)));
break;
case DropdownPropertyInfo.DropdownKind.String:
ReplaceValue(stringValue);
ReplaceValue(JsonValue.Create(stringValue));
break;
default:
throw new InvalidOperationException();
Expand Down
2 changes: 1 addition & 1 deletion JsonPowerInspector/Scripts/Inspectors/EnumInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ protected override void Bind(ref JsonNode node)
{
var jsonValue = node.AsValue();
_contentControl.Selected = _selections.FindIndex(x => x.DeclareName == jsonValue.GetValue<string>());
_contentControl.ItemSelected += index => ReplaceValue(_selections[(int)index].DeclareName);
_contentControl.ItemSelected += index => ReplaceValue(JsonValue.Create(_selections[(int)index].DeclareName));
}
}
2 changes: 1 addition & 1 deletion JsonPowerInspector/Scripts/Inspectors/NumberInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ protected override void Bind(ref JsonNode node)
throw new InvalidOperationException();
}

_contentControl.ValueChanged += ReplaceValue;
_contentControl.ValueChanged += value => ReplaceValue(JsonValue.Create(value));
}
}
2 changes: 1 addition & 1 deletion JsonPowerInspector/Scripts/Inspectors/StringInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ protected override void Bind(ref JsonNode node)
{
var jsonValue = node.AsValue();
_contentControl.Text = jsonValue.GetValue<string>();
_contentControl.TextChanged += ReplaceValue;
_contentControl.TextChanged += value => ReplaceValue(JsonValue.Create(value));
}
}
2 changes: 0 additions & 2 deletions JsonPowerInspector/Scripts/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public partial class Main : Control

private InspectionSessionController _currentFocusedSession;

[UnconditionalSuppressMessage("Warning", "IL3050")]
[UnconditionalSuppressMessage("Warning", "IL2026")]
public override void _Ready()
{
_versionInfo.Text = Version.Current;
Expand Down
18 changes: 9 additions & 9 deletions JsonPowerInspector/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace JsonPowerInspector;

public static class Utils
{
public static IPropertyInspector CreateInspectorForProperty(BaseObjectPropertyInfo propertyInfo, InspectorSpawner spawner)
public static IPropertyInspector CreateInspectorForProperty(BaseObjectPropertyInfo propertyInfo, InspectorSpawner spawner, bool affectMainObject)
{
return propertyInfo switch
{
StringPropertyInfo stringPropertyInfo => spawner.Create(stringPropertyInfo),
NumberPropertyInfo numberPropertyInfo => spawner.Create(numberPropertyInfo),
ObjectPropertyInfo objectPropertyInfo => spawner.Create(objectPropertyInfo),
BooleanPropertyInfo booleanPropertyInfo => spawner.Create(booleanPropertyInfo),
ArrayPropertyInfo arrayPropertyInfo => spawner.Create(arrayPropertyInfo),
DictionaryPropertyInfo dictionaryPropertyInfo => spawner.Create(dictionaryPropertyInfo),
EnumPropertyInfo enumPropertyInfo => spawner.Create(enumPropertyInfo),
DropdownPropertyInfo dropdownPropertyInfo => spawner.Create(dropdownPropertyInfo),
StringPropertyInfo stringPropertyInfo => spawner.Create(stringPropertyInfo, affectMainObject),
NumberPropertyInfo numberPropertyInfo => spawner.Create(numberPropertyInfo, affectMainObject),
ObjectPropertyInfo objectPropertyInfo => spawner.Create(objectPropertyInfo, affectMainObject),
BooleanPropertyInfo booleanPropertyInfo => spawner.Create(booleanPropertyInfo, affectMainObject),
ArrayPropertyInfo arrayPropertyInfo => spawner.Create(arrayPropertyInfo, affectMainObject),
DictionaryPropertyInfo dictionaryPropertyInfo => spawner.Create(dictionaryPropertyInfo, affectMainObject),
EnumPropertyInfo enumPropertyInfo => spawner.Create(enumPropertyInfo, affectMainObject),
DropdownPropertyInfo dropdownPropertyInfo => spawner.Create(dropdownPropertyInfo, affectMainObject),
_ => throw new InvalidOperationException()
};
}
Expand Down

0 comments on commit a1843cc

Please sign in to comment.