Ejemplo n.º 1
0
        private static object ParseExpression(JsonToken token, TokenStream ts)
        {
            if (token == null)
            {
                token = ts.GetNextToken();
            }

            switch (token.Type)
            {
            case JsonToken.TokenType.String:
                logger.Debug("Found String " + token.Value);
                return(token.Value);

            case JsonToken.TokenType.Number:
                logger.Debug("Found Number " + token.Value);
                return(token.Value);

            case JsonToken.TokenType.False:
                logger.Debug("Found False");
                return(false);

            case JsonToken.TokenType.True:
                logger.Debug("Found True");
                return(true);

            case JsonToken.TokenType.Null:
                logger.Debug("Found Null");
                return(null);

            case JsonToken.TokenType.ObjectStart:
                logger.Debug("Found ObjectStart");
                return(ParseObject(ts));

            case JsonToken.TokenType.ArrayStart:
                logger.Debug("Found ArrayStart");
                return(ParseArray(ts));

            case JsonToken.TokenType.Undefined:
            default:
                throw new ArgumentException("parse error");
            }
        }
Ejemplo n.º 2
0
        private static void LoadIfUnresolved(Type type)
        {
            if (DEPENDENCIES.ContainsKey(type))
            {
                return;
            }

            if (CURRENTLY_LOADING_DEPENDENCIES.Contains(type))
            {
                throw new AccessViolationException(
                          $"An attempt to load class that is being loaded. Please check that you don't have any dependency usage of class {type}" + " in its constructor");
            }

            if (type.IsSubclassOf(typeof(MonoBehaviour)) && type.GetCustomAttribute <InjectAttribute>() != null)
            {
                var instance = Object.FindObjectOfType(type);
                if (instance != null)
                {
                    Add(type, instance);
                }
                else
                {
                    UnityLogger.Error($"Failed to instantiate MonoBehaviour object of type {type}: no object found in the scene");
                }

                return;
            }

            if (Application.isEditor && !Application.isPlaying)
            {
                if (!DEPENDENCIES.ContainsKey(type))
                {
                    UnityLogger.Debug($"Initializing dependency {type} inside the editor");
                    InstantiateObject(type);
                }

                return;
            }

            if (!IS_INITIALIZED)
            {
                throw new AccessViolationException(
                          "Tried to resolve without Initializing " + typeof(DependencyProvider));
            }

            CURRENTLY_LOADING_DEPENDENCIES.Add(type);

            foreach (var injectableType in INJECTABLE_TYPES)
            {
                if (injectableType == type)
                {
                    try
                    {
                        InstantiateObject(type);
                    }
                    catch (Exception ex)
                    {
                        UnityLogger.Error("Tried to instantiate class [" + type + "], but failed because of: " + ex);
                    }
                }
                else if (type.IsAssignableFrom(injectableType))
                {
                    if (!DEPENDENCIES.ContainsKey(injectableType))
                    {
                        LoadIfUnresolved(injectableType);
                    }

                    foreach (var injectable in DEPENDENCIES[injectableType])
                    {
                        Add(type, injectable);
                    }
                }
            }

            CURRENTLY_LOADING_DEPENDENCIES.Remove(type);
        }