Exemplo n.º 1
0
    public ContextSwitchInterpreter(Type type, ScriptEngine engine)
    {
        this.Engine = engine;
        engine.GetPlugin <ContextSwitchesPlugin> ().AddInterToType(type, this);
        //if (ScriptEngine.AnalyzeDebug)
        //    Debug.Log ("Context switch " + type);
        contextType = type;
        if (type == typeof(GameObject))
        {
            return;
        }
        var props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
        var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);

        foreach (var prop in props)
        {
            //Debug.Log (prop.Name);
            if (prop.PropertyType != typeof(string) && (prop.PropertyType.IsClass || (prop.PropertyType.IsValueType && !prop.PropertyType.IsEnum &&
                                                                                      prop.PropertyType != typeof(bool) && prop.PropertyType != typeof(float) && prop.PropertyType != typeof(int))) &&
                !(prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(List <>)))
            {
                ContextPropertySwitchInterpreter inter = new ContextPropertySwitchInterpreter(prop.Name, prop.PropertyType, engine);
                contextSwitches.Add(NameTranslator.ScriptNameFromCSharp(prop.Name), inter);

                inter.Engine = Engine;
                //if (ScriptEngine.AnalyzeDebug)
                //    Debug.Log ("SubContext " + NameTranslator.ScriptNameFromCSharp (prop.Name));
            }
            else
            {
                ContextPropertyInterpreter inter = new ContextPropertyInterpreter(prop.Name, prop.PropertyType, Engine);
                inter.Engine = Engine;
                //if (ScriptEngine.AnalyzeDebug)
                //    Debug.Log (inter.Engine);
                properties.Add(NameTranslator.ScriptNameFromCSharp(prop.Name), inter);
                //if (ScriptEngine.AnalyzeDebug)
                //    Debug.Log (NameTranslator.ScriptNameFromCSharp (prop.Name));
            }
        }

        foreach (var method in methods)
        {
            if (method.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length > 0)
            {
                continue;
            }
            // if (ScriptEngine.AnalyzeDebug)
            //     Debug.Log ("Context method " + method.Name);
            ContextFunctionCallInterpreter inter = new ContextFunctionCallInterpreter(method, Engine);
            try
            {
                functions.Add(NameTranslator.ScriptNameFromCSharp(method.Name), inter);
            }
            catch (ArgumentException e)
            {
                Debug.LogWarningFormat("{0} has a duplicate function named {1}", type, method.Name);
            }
        }
    }
    //	static Dictionary<Type, ContextPropertySwitchInterpreter> switchesByType = new Dictionary<Type, ContextPropertySwitchInterpreter> ();
    //
    //	public static ContextPropertySwitchInterpreter GetSwitch (Type t)
    //	{
    //		ContextPropertySwitchInterpreter inter = null;
    //		if (!switchesByType.TryGetValue (t, out inter))
    //		{
    //
    //
    //		}
    //		return inter;
    //	}

    public ContextPropertySwitchInterpreter(string propName, Type type, ScriptEngine engine) : base(propName, type, engine)
    {
        engine.GetPlugin <ContextSwitchesPlugin> ().AddInterToType(type, this);
        this.propName = propName;
        propType      = type;
        this.Engine   = engine;
        // if (ScriptEngine.AnalyzeDebug)
        //      Debug.Log ("Context switch " + type);
        var props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        // if (ScriptEngine.AnalyzeDebug)
        //     Debug.Log ("Methods count " + methods.Length);
        var thisKey = new PropKey(type, propName);

        if (!allPropSwitches.ContainsKey(thisKey))
        {
            allPropSwitches.Add(thisKey, this);
        }
        if (typeof(MonoBehaviour).IsAssignableFrom(type) && type != typeof(MonoBehaviour))
        {
            //if (ScriptEngine.AnalyzeDebug)
            //    Debug.Log ("It's a component! " + type);
            foreach (var prop in props)
            {
                //Debug.Log (prop.Name);
                if (prop.PropertyType != typeof(string) && (prop.PropertyType.IsClass || (prop.PropertyType.IsValueType && !prop.PropertyType.IsEnum &&
                                                                                          prop.PropertyType != typeof(bool) && prop.PropertyType != typeof(float) && prop.PropertyType != typeof(int))))
                {
                    var key = new PropKey(prop.PropertyType, prop.Name);
                    if (allPropSwitches.ContainsKey(key))
                    {
                        contextSwitches.Add(NameTranslator.ScriptNameFromCSharp(prop.Name), allPropSwitches [key]);
                    }
                    else
                    {
                        ContextPropertySwitchInterpreter inter = new ContextPropertySwitchInterpreter(prop.Name, prop.PropertyType, engine);
                        contextSwitches.Add(NameTranslator.ScriptNameFromCSharp(prop.Name), inter);

                        inter.Engine = Engine;
                    }

                    // if (ScriptEngine.AnalyzeDebug)
                    //      Debug.Log ("SubContext " + NameTranslator.ScriptNameFromCSharp (prop.Name));
                }
                else
                {
                    ContextPropertyInterpreter inter = new ContextPropertyInterpreter(prop.Name, prop.PropertyType, engine);
                    inter.Engine = Engine;
                    //  if (ScriptEngine.AnalyzeDebug)
                    //      Debug.Log (inter.Engine);
                    properties.Add(NameTranslator.ScriptNameFromCSharp(prop.Name), inter);
                    //   if (ScriptEngine.AnalyzeDebug)
                    //      Debug.Log (NameTranslator.ScriptNameFromCSharp (prop.Name));
                }
            }

            foreach (var method in methods)
            {
                if (method.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length > 0)
                {
                    continue;
                }
                // if (ScriptEngine.AnalyzeDebug)
                //    Debug.Log ("Context method " + method.Name);
                ContextFunctionCallInterpreter inter = new ContextFunctionCallInterpreter(method, Engine);
                functions.Add(NameTranslator.ScriptNameFromCSharp(method.Name), inter);
            }
        }
    }