Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RuntimeUnityEditor.Core/RuntimeUnityEditor.Core.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows\ChangeHistory\IChange.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Clipboard\ClipboardWindow.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Inspector\Entries\Contents\CacheEntryBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Inspector\Entries\Contents\Fake\ArrayCacheEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Inspector\Entries\Contents\Fake\CallbackCacheEntey.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Inspector\Entries\Contents\Fake\IFakeCacheEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\Inspector\Entries\Contents\IL2CPP\IL2CPPEventCacheEntry.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using RuntimeUnityEditor.Core.ChangeHistory;

namespace RuntimeUnityEditor.Core.Inspector.Entries
{
/// <summary>
/// Represents a cache entry for an item inside an array of any rank.
/// </summary>
public class ArrayCacheEntry : CacheEntryBase, IFakeCacheEntry
{
private Type _type;
private readonly Array _array;
private readonly int[] _indices;

/// <inheritdoc />
public ArrayCacheEntry(Array container, int flatIndex) : this(container, GetIndices(container, flatIndex)) { }

private ArrayCacheEntry(Array container, int[] indices) : base(GetArrayItemName(indices), $"Item contained inside of an array.\n\nIndex: [{FormatIndices(indices)}]\n\nArray type: {container.GetType().FullDescription()}", null)
{
_array = container;
_indices = indices;
}

/// <summary>
/// Convert a flat row-major index into per-dimension indices.
/// </summary>
internal static int[] GetIndices(Array array, int flatIndex)
{
var indices = new int[array.Rank];
for (var dim = array.Rank - 1; dim >= 0; dim--)
{
var length = array.GetLength(dim);
indices[dim] = array.GetLowerBound(dim) + flatIndex % length;
flatIndex /= length;
}
return indices;
}

internal static string GetArrayItemName(int[] indices)
{
return indices.Length == 1
? ReadonlyListCacheEntry.GetListItemName(indices[0])
: "Index: [" + FormatIndices(indices) + "]";
}

private static string FormatIndices(int[] indices)
{
return string.Join(", ", indices.Select(x => x.ToString()).ToArray());
}

/// <inheritdoc />
public override object GetValueToCache()
{
return _array.GetValue(_indices);
}

/// <inheritdoc />
protected override bool OnSetValue(object newValue)
{
var oldValue = _array.GetValue(_indices);
Change.WithUndo($"{{0}}[{FormatIndices(_indices)}] = {{1}}", _array, newValue, (arr, o) => arr.SetValue(o, _indices), oldValue: oldValue);
_type = null;
return true;
}

/// <inheritdoc />
public override Type Type()
{
return _type ?? (_type = GetValue()?.GetType() ?? _array.GetType().GetElementType());
}

/// <inheritdoc />
public override MemberInfo MemberInfo => null;

/// <inheritdoc />
public override bool CanSetValue()
{
return true;
}
}
}
5 changes: 5 additions & 0 deletions RuntimeUnityEditor.Core/Windows/Inspector/MemberCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ CallbackCacheEntry GetExportTexEntry(Texture texture)
{
// Prevent the list overloads from listing subcomponents
}
else if (objectToOpen is Array arr)
{
for (var i = 0; i < arr.Length; i++)
fieldCache.Add(new ArrayCacheEntry(arr, i));
}
else if (objectToOpen is IList list)
{
for (var i = 0; i < list.Count; i++)
Expand Down
Loading