diff --git a/JsonPowerInspector/Scripts/InspectionSessionController.cs b/JsonPowerInspector/Scripts/InspectionSessionController.cs index 5723bba..fcdeceb 100644 --- a/JsonPowerInspector/Scripts/InspectionSessionController.cs +++ b/JsonPowerInspector/Scripts/InspectionSessionController.cs @@ -196,7 +196,7 @@ private async GDTask 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); } @@ -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: @@ -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) { diff --git a/JsonPowerInspector/Scripts/InspectorSpawner.cs b/JsonPowerInspector/Scripts/InspectorSpawner.cs index 03e4349..4d99d75 100644 --- a/JsonPowerInspector/Scripts/InspectorSpawner.cs +++ b/JsonPowerInspector/Scripts/InspectorSpawner.cs @@ -38,21 +38,25 @@ InspectionSessionController currentSession _dropdownInspector = dropdownInspector; } - public StringInspector Create(StringPropertyInfo stringPropertyInfo) => Print(_stringInspector, stringPropertyInfo); - public NumberInspector Create(NumberPropertyInfo numberPropertyInfo) => Print(_numberInspector, numberPropertyInfo); - public ObjectInspector Create(ObjectPropertyInfo objectPropertyInfo) => Print(_objectInspector, objectPropertyInfo); - public BooleanInspector Create(BooleanPropertyInfo booleanPropertyInfo) => Print(_booleanInspector, booleanPropertyInfo); - public ArrayInspector Create(ArrayPropertyInfo arrayPropertyInfo) => Print(_arrayInspector, arrayPropertyInfo); - public DictionaryInspector Create(DictionaryPropertyInfo dictionaryPropertyInfo) => Print(_dictionaryInspector, dictionaryPropertyInfo); - public EnumInspector Create(EnumPropertyInfo enumPropertyInfo) => Print(_enumInspector, enumPropertyInfo); - public DropdownInspector Create(DropdownPropertyInfo dropdownPropertyInfo) => Print(_dropdownInspector, dropdownPropertyInfo); - - private TInspector Print(PackedScene inspectorPrefab, TPropertyInfo propertyInfo) - where TInspector : BasePropertyInspector + public StringInspector Create(StringPropertyInfo stringPropertyInfo, bool affectMainObject) => Print(_stringInspector, stringPropertyInfo, affectMainObject); + public NumberInspector Create(NumberPropertyInfo numberPropertyInfo, bool affectMainObject) => Print(_numberInspector, numberPropertyInfo, affectMainObject); + public ObjectInspector Create(ObjectPropertyInfo objectPropertyInfo, bool affectMainObject) => Print(_objectInspector, objectPropertyInfo, affectMainObject); + public BooleanInspector Create(BooleanPropertyInfo booleanPropertyInfo, bool affectMainObject) => Print(_booleanInspector, booleanPropertyInfo, affectMainObject); + public ArrayInspector Create(ArrayPropertyInfo arrayPropertyInfo, bool affectMainObject) => Print(_arrayInspector, arrayPropertyInfo, affectMainObject); + public DictionaryInspector Create(DictionaryPropertyInfo dictionaryPropertyInfo, bool affectMainObject) => Print(_dictionaryInspector, dictionaryPropertyInfo, affectMainObject); + public EnumInspector Create(EnumPropertyInfo enumPropertyInfo, bool affectMainObject) => Print(_enumInspector, enumPropertyInfo, affectMainObject); + public DropdownInspector Create(DropdownPropertyInfo dropdownPropertyInfo, bool affectMainObject) => Print(_dropdownInspector, dropdownPropertyInfo, affectMainObject); + + private TInspector Print( + PackedScene inspectorPrefab, + TPropertyInfo propertyInfo, + bool affectMainObject + ) + where TInspector : BasePropertyInspector where TPropertyInfo : BaseObjectPropertyInfo { var instance = inspectorPrefab.Instantiate(); - instance.Initialize(propertyInfo, _currentSession); + instance.Initialize(propertyInfo, _currentSession, affectMainObject); return instance; } } \ No newline at end of file diff --git a/JsonPowerInspector/Scripts/Inspectors/BasePropertyInspector.cs b/JsonPowerInspector/Scripts/Inspectors/BasePropertyInspector.cs index f30fd88..957f520 100644 --- a/JsonPowerInspector/Scripts/Inspectors/BasePropertyInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/BasePropertyInspector.cs @@ -11,7 +11,7 @@ public interface IPropertyInspector string DisplayName { set; } string BackingPropertyName { set; } void BindJsonNode(JsonNode parent, string propertyName); - event Action ValueChanged; + event Action ValueChanged; } public abstract partial class BasePropertyInspector : Control, IPropertyInspector where TPropertyInfo : BaseObjectPropertyInfo @@ -32,19 +32,21 @@ public string BackingPropertyName set => _jsonPropertyName = value; } - public event Action ValueChanged; + public event Action 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); } @@ -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 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); } diff --git a/JsonPowerInspector/Scripts/Inspectors/BooleanInspector.cs b/JsonPowerInspector/Scripts/Inspectors/BooleanInspector.cs index b85a83a..1cf51f3 100644 --- a/JsonPowerInspector/Scripts/Inspectors/BooleanInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/BooleanInspector.cs @@ -12,6 +12,6 @@ protected override void Bind(ref JsonNode node) { var jsonValue = node.AsValue(); _contentControl.ButtonPressed = jsonValue.GetValue(); - _contentControl.Toggled += ReplaceValue; + _contentControl.Toggled += value => ReplaceValue(JsonValue.Create(value)); } } \ No newline at end of file diff --git a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ArrayInspector.cs b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ArrayInspector.cs index b4163ef..7a74a88 100644 --- a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ArrayInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ArrayInspector.cs @@ -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(); diff --git a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/DictionaryInspector.cs b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/DictionaryInspector.cs index c7dd09f..3b4232f 100644 --- a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/DictionaryInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/DictionaryInspector.cs @@ -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(); diff --git a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/KeyInputWindow.cs b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/KeyInputWindow.cs index 8da054e..7d5b575 100644 --- a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/KeyInputWindow.cs +++ b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/KeyInputWindow.cs @@ -27,7 +27,8 @@ public override void _Ready() public GDTask<(bool, double)> ShowAsync(NumberPropertyInfo numberPropertyInfo, InspectorSpawner inspectorSpawner, HashSet bannedKey) => ShowAsync( numberPropertyInfo, - static (spawner, info) => spawner.Create(info), + static (spawner, info) => spawner.Create(info, false), + val => JsonValue.Create(val), inspectorSpawner, bannedKey, 0.0 @@ -36,7 +37,8 @@ public override void _Ready() public GDTask<(bool, string)> ShowAsync(StringPropertyInfo stringPropertyInfo, InspectorSpawner inspectorSpawner, HashSet bannedKey) => ShowAsync( stringPropertyInfo, - static (spawner, info) => spawner.Create(info), + static (spawner, info) => spawner.Create(info, false), + val => JsonValue.Create(val), inspectorSpawner, bannedKey, "" @@ -45,7 +47,8 @@ public override void _Ready() public GDTask<(bool, int)> ShowAsyncInt(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet bannedKey) => ShowAsync( dropdownPropertyInfo, - static (spawner, info) => spawner.Create(info), + static (spawner, info) => spawner.Create(info, false), + val => JsonValue.Create(val), inspectorSpawner, bannedKey, 0 @@ -54,7 +57,8 @@ public override void _Ready() public GDTask<(bool, double)> ShowAsyncFloat(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet bannedKey) => ShowAsync( dropdownPropertyInfo, - static (spawner, info) => spawner.Create(info), + static (spawner, info) => spawner.Create(info, false), + val => JsonValue.Create(val), inspectorSpawner, bannedKey, 0.0 @@ -63,7 +67,8 @@ public override void _Ready() public GDTask<(bool, string)> ShowAsyncString(DropdownPropertyInfo dropdownPropertyInfo, InspectorSpawner inspectorSpawner, HashSet bannedKey) => ShowAsync( dropdownPropertyInfo, - static (spawner, info) => spawner.Create(info), + static (spawner, info) => spawner.Create(info, false), + val => JsonValue.Create(val), inspectorSpawner, bannedKey, "" @@ -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 propertyInfo, Func inspectorFactory, + Func jsonValueFactory, InspectorSpawner spawner, IReadOnlySet bannedKey, TValue defaultValue @@ -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()); 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); diff --git a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ObjectInspector.cs b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ObjectInspector.cs index 9c82b1f..b52f527 100644 --- a/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ObjectInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/CollectionInspectors/ObjectInspector.cs @@ -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); diff --git a/JsonPowerInspector/Scripts/Inspectors/DropdownInspector.cs b/JsonPowerInspector/Scripts/Inspectors/DropdownInspector.cs index 04d8f87..6ad07cb 100644 --- a/JsonPowerInspector/Scripts/Inspectors/DropdownInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/DropdownInspector.cs @@ -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(); diff --git a/JsonPowerInspector/Scripts/Inspectors/EnumInspector.cs b/JsonPowerInspector/Scripts/Inspectors/EnumInspector.cs index 51baeae..4e12994 100644 --- a/JsonPowerInspector/Scripts/Inspectors/EnumInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/EnumInspector.cs @@ -31,6 +31,6 @@ protected override void Bind(ref JsonNode node) { var jsonValue = node.AsValue(); _contentControl.Selected = _selections.FindIndex(x => x.DeclareName == jsonValue.GetValue()); - _contentControl.ItemSelected += index => ReplaceValue(_selections[(int)index].DeclareName); + _contentControl.ItemSelected += index => ReplaceValue(JsonValue.Create(_selections[(int)index].DeclareName)); } } \ No newline at end of file diff --git a/JsonPowerInspector/Scripts/Inspectors/NumberInspector.cs b/JsonPowerInspector/Scripts/Inspectors/NumberInspector.cs index ba1dd04..fcbbb50 100644 --- a/JsonPowerInspector/Scripts/Inspectors/NumberInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/NumberInspector.cs @@ -60,6 +60,6 @@ protected override void Bind(ref JsonNode node) throw new InvalidOperationException(); } - _contentControl.ValueChanged += ReplaceValue; + _contentControl.ValueChanged += value => ReplaceValue(JsonValue.Create(value)); } } \ No newline at end of file diff --git a/JsonPowerInspector/Scripts/Inspectors/StringInspector.cs b/JsonPowerInspector/Scripts/Inspectors/StringInspector.cs index 1f09617..8c7ce95 100644 --- a/JsonPowerInspector/Scripts/Inspectors/StringInspector.cs +++ b/JsonPowerInspector/Scripts/Inspectors/StringInspector.cs @@ -12,6 +12,6 @@ protected override void Bind(ref JsonNode node) { var jsonValue = node.AsValue(); _contentControl.Text = jsonValue.GetValue(); - _contentControl.TextChanged += ReplaceValue; + _contentControl.TextChanged += value => ReplaceValue(JsonValue.Create(value)); } } \ No newline at end of file diff --git a/JsonPowerInspector/Scripts/Main.cs b/JsonPowerInspector/Scripts/Main.cs index 9e44469..bf314de 100644 --- a/JsonPowerInspector/Scripts/Main.cs +++ b/JsonPowerInspector/Scripts/Main.cs @@ -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; diff --git a/JsonPowerInspector/Scripts/Utils.cs b/JsonPowerInspector/Scripts/Utils.cs index 144962c..f45ba48 100644 --- a/JsonPowerInspector/Scripts/Utils.cs +++ b/JsonPowerInspector/Scripts/Utils.cs @@ -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() }; }