//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
/** @addtogroup Serialization
* @{
*/
#pragma warning disable 649
///
/// Allows you to access meta-data about a managed list and its children. Similar to Reflection but simpler and faster.
///
public sealed class SerializableList : ScriptObject
{
private SerializableProperty.FieldType elementPropertyType;
private Type elementType;
private SerializableProperty parentProperty;
///
/// Type of serializable property used for the elements stored in the list.
///
public SerializableProperty.FieldType ElementPropertyType
{
get { return elementPropertyType; }
}
///
/// Type of elements stored in the list.
///
public Type ElementType
{
get { return elementType; }
}
///
/// Constructor for use by the runtime only.
///
/// C# type of the elements in the list.
/// Property used for retrieving this entry.
private SerializableList(Type elementType, SerializableProperty parentProperty)
{
this.parentProperty = parentProperty;
this.elementType = elementType;
elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
}
///
/// Returns a serializable property for a specific list element.
///
/// Index of the element in the list.
/// Serializable property that allows you to manipulate contents of the list entry.
public SerializableProperty GetProperty(int elementIdx)
{
SerializableProperty.Getter getter = () =>
{
IList list = parentProperty.GetValue();
if (list != null)
return list[elementIdx];
else
return null;
};
SerializableProperty.Setter setter = (object value) =>
{
IList list = parentProperty.GetValue();
if (list != null)
list[elementIdx] = value;
};
SerializableProperty property = Internal_CreateProperty(mCachedPtr);
property.Construct(ElementPropertyType, elementType, getter, setter);
return property;
}
///
/// Returns number of elements in the list.
///
/// Number of elements in the list.
public int GetLength()
{
IList list = parentProperty.GetValue();
if (list != null)
return list.Count;
else
return 0;
}
///
/// Uses the provided path elements to find an list element at the specified index, and returns a property
/// to the element, or to a child property of that element.
///
/// Path elements representing field names and keys to look for.
/// Index in the array to start the search at.
/// Property representing the final path element, or null if not found (list index is out of range, or
/// property with that path doesn't exist).
internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
{
int arrayIdx;
if (string.IsNullOrEmpty(pathElements[elementIdx].key))
arrayIdx = 0;
else
{
if (!int.TryParse(pathElements[elementIdx].key, out arrayIdx))
return null;
else
{
if (arrayIdx < 0 || arrayIdx >= GetLength())
return null;
}
}
SerializableProperty property = GetProperty(arrayIdx);
if (elementIdx == (pathElements.Length - 1))
return property;
return property.FindProperty(pathElements, elementIdx + 1);
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
}
/** @} */
}