// ReSharper disable once RedundantUsingDirective
using System.Linq;
namespace SRF
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Helpers;
using Service;
using UnityEngine;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field)]
public sealed class RequiredFieldAttribute : Attribute
{
private bool _autoCreate;
private bool _autoSearch;
private bool _editorOnly = true;
public RequiredFieldAttribute(bool autoSearch)
{
AutoSearch = autoSearch;
}
public RequiredFieldAttribute() {}
public bool AutoSearch
{
get { return _autoSearch; }
set { _autoSearch = value; }
}
public bool AutoCreate
{
get { return _autoCreate; }
set { _autoCreate = value; }
}
[Obsolete]
public bool EditorOnly
{
get { return _editorOnly; }
set { _editorOnly = value; }
}
}
///
/// Add to a field to attempt to use SRServiceManager to get an instance of the field type
///
[AttributeUsage(AttributeTargets.Field)]
public class ImportAttribute : Attribute
{
public readonly Type Service;
public ImportAttribute() {}
public ImportAttribute(Type serviceType)
{
Service = serviceType;
}
}
public abstract class SRMonoBehaviourEx : SRMonoBehaviour
{
private static Dictionary> _checkedFields;
private static void CheckFields(SRMonoBehaviourEx instance, bool justSet = false)
{
if (_checkedFields == null)
{
_checkedFields = new Dictionary>();
}
var t = instance.GetType();
IList cache;
if (!_checkedFields.TryGetValue(instance.GetType(), out cache))
{
cache = ScanType(t);
_checkedFields.Add(t, cache);
}
PopulateObject(cache, instance, justSet);
}
private static void PopulateObject(IList cache, SRMonoBehaviourEx instance, bool justSet)
{
for (var i = 0; i < cache.Count; i++)
{
var f = cache[i];
if (!EqualityComparer