Example #1
0
        private static void BindImplementation <T>(
            T value, Type defaultType)
            where T : class, IMemenimScriptBindable
        {
            var targetType = value?.GetType() ?? typeof(T);

            targetType = MemenimScriptUtils.GetBaseType(targetType);

            if (defaultType == targetType)
            {
                defaultType = null;
            }

            if (defaultType == null)
            {
                BindImplementationInternal(value);

                return;
            }

            if (!targetType.IsAssignableFrom(defaultType))
            {
                throw new ArgumentException(
                          $"{nameof(defaultType)} must be derived from the <{targetType.Name}> class",
                          nameof(defaultType));
            }

            var defaultValue = (T)Activator.CreateInstance(
                defaultType, true);

            BindImplementationInternal(value, defaultValue);
        }
Example #2
0
        private static bool TryBindImplementation(
            Type targetType, Type defaultType = null)
        {
            targetType = MemenimScriptUtils.GetBaseType(targetType);

            if (defaultType == targetType)
            {
                defaultType = null;
            }

            if (!typeof(IMemenimScriptBindable).IsAssignableFrom(targetType))
            {
                return(false);
            }

            if (defaultType != null && !targetType.IsAssignableFrom(defaultType))
            {
                return(false);
            }

            var result = TryGetImplementation(
                targetType, defaultType);

            if (!result.IsSuccess)
            {
                return(false);
            }

            var reference = MemenimScriptUtils.GetBindReference(targetType);

            reference.Value = result.Implementation;

            return(true);
        }
Example #3
0
        private static void BindAll()
        {
            foreach (var field in typeof(MemenimScript).GetFields(BindingFlags.Static
                                                                  | BindingFlags.Public
                                                                  | BindingFlags.NonPublic))
            {
                var type = field.FieldType;

                if (!typeof(IMemenimScriptBindable).IsAssignableFrom(type))
                {
                    continue;
                }

                var target = MemenimScriptUtils.GetBindTarget(type);

                if (target == MemenimScriptBindTargetType.Unknown)
                {
                    continue;
                }

                var reference = new MemenimScriptBindReference(field);
                var value     = reference.Value;

                BindReferenceMap[target] = reference;

                BindImplementation(value, MemenimScriptUtils.GetBaseType(value));
            }

            BindSettingsImplementation(ref _settings);
        }
        internal MemenimScriptBindReference(FieldInfo field)
        {
            _field = field;

            Type   = field.FieldType;
            Target = MemenimScriptUtils.GetBindTarget(
                field.FieldType);
        }
Example #5
0
        private static T GetImplementation <T>(
            ref T value)
            where T : class, IMemenimScriptBindable
        {
            if (value != null)
            {
                return(value);
            }

            var notImplementedType =
                MemenimScriptUtils.GetNotImplementedType(typeof(T));

            value = (T)Activator.CreateInstance(
                notImplementedType, true);

            return(value);
        }
        private IMemenimScriptBindable GetValue()
        {
            object value = _field
                           .GetValue(null);

            if (value != null)
            {
                return((IMemenimScriptBindable)value);
            }

            var notImplementedType = MemenimScriptUtils
                                     .GetNotImplementedType(Target);

            value = (IMemenimScriptBindable)Activator.CreateInstance(
                notImplementedType, true);

            return((IMemenimScriptBindable)value);
        }
Example #7
0
        public static void BindImplementation <T>(
            MemenimScriptBindTargetType target, T value)
            where T : class
        {
            if (target == MemenimScriptBindTargetType.Unknown)
            {
                return;
            }

            if (value == null)
            {
                return;
            }

            if (!(value is IMemenimScriptBindable))
            {
                throw new ArgumentException(
                          $"{nameof(value)} must implement the {nameof(IMemenimScriptBindable)} interface",
                          nameof(value));
            }

            Type baseType = MemenimScriptUtils.GetBaseType(target);

            if (baseType == null)
            {
                return;
            }

            if (!baseType.IsAssignableFrom(value.GetType()))
            {
                throw new ArgumentException(
                          $"{nameof(value)} must be derived from the target[{target}] base class (in this case - {baseType.Name})",
                          nameof(value));
            }

            var reference = MemenimScriptUtils.GetBindReference(target);

            reference.Value = (IMemenimScriptBindable)value;
        }
Example #8
0
        private static void BindImplementationInternal <T>(
            T value, T defaultValue = null)
            where T : class, IMemenimScriptBindable
        {
            var targetType  = value?.GetType() ?? typeof(T);
            var defaultType = defaultValue?.GetType();

            targetType = MemenimScriptUtils.GetBaseType(targetType);

            if (defaultType == targetType)
            {
                defaultType = null;
            }

            if (TryBindImplementation(targetType, defaultType))
            {
                return;
            }

            var reference = MemenimScriptUtils.GetBindReference(value);

            reference.Value = defaultValue;
        }
Example #9
0
        private static (bool IsSuccess, IMemenimScriptBindable Implementation) TryGetImplementation(
            Type targetType, Type defaultType = null)
        {
            targetType = MemenimScriptUtils.GetBaseType(targetType);

            if (defaultType == targetType)
            {
                defaultType = null;
            }

            if (!typeof(IMemenimScriptBindable).IsAssignableFrom(targetType))
            {
                return(false, null);
            }

            if (defaultType != null && !targetType.IsAssignableFrom(defaultType))
            {
                return(false, null);
            }

            var notImplementedType = MemenimScriptUtils.GetNotImplementedType(targetType);
            var assemblies         = new List <Assembly>
            {
                Assembly.GetEntryAssembly(),
                Assembly.GetCallingAssembly(),
                Assembly.GetExecutingAssembly()
            };

            assemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies());

            foreach (var assembly in assemblies)
            {
                if (assembly == null)
                {
                    continue;
                }

                var types = assembly.GetTypes()
                            .Where(type => type.IsClass &&
                                   (targetType != null && targetType.IsAssignableFrom(type)) &&
                                   (type != targetType) &&
                                   (type != notImplementedType) &&
                                   (type != defaultType))
                            .ToArray();

                if (types.Length == 0)
                {
                    continue;
                }

                foreach (var type in types)
                {
                    IMemenimScriptBindable target;

                    try
                    {
                        target = (IMemenimScriptBindable)Activator.CreateInstance(
                            type, true);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    if (target == null)
                    {
                        continue;
                    }

                    return(true, target);
                }
            }

            return(false, null);
        }