Example #1
0
    public static Type GetTypeFromTypeStringWithErrors(this TypeResolverGroup typeResolverGroup, string typeString)
    {
        var type = typeResolverGroup.GetTypeFromTypeString(typeString);

        if (type == null)
        {
            Debug.LogError($"Unable to find type {typeString} in type resolver group");
        }
        return(type);
    }
Example #2
0
 public static void AddTypeInterfaces(this Type type, HashSet <string> interfaces,
                                      TypeResolverGroup typeResolverGroup)
 {
     if (type == null)
     {
         return;
     }
     foreach (var Interface in type.GetInterfaces())
     {
         if (typeResolverGroup.GetTypeFromTypeString(UdonTypeExporter.GenerateUdonName(Interface)) != null)
         {
             interfaces.Add(UdonTypeExporter.GetTypeFullName(Interface));
             Interface.AddTypeInterfaces(interfaces, typeResolverGroup);
         }
     }
 }
Example #3
0
    private void HandlePushEnumInstruction(string rawLine, string[] instructionArgs, Stack <object> stack,
                                           TypeResolverGroup typeResolver)
    {
        if (instructionArgs.Length != 3)
        {
            throw new InvalidOperationException(
                      $"PUSHENUM requires 2 arguments(EnumType, EnumValueName). EX: 'PUSHENUM UnityEngineFFTWindow Rectangular'");
        }
        var enumName      = instructionArgs[1];
        var enumValueName = instructionArgs[2];

        var enumType = typeResolver.GetTypeFromTypeString(enumName);

        if (enumType == null)
        {
            throw new InvalidOperationException(
                      $"Unable to find type '{enumName}'.");
        }
        object output = Enum.Parse(enumType, enumValueName);

        stack.Push(output);
    }
Example #4
0
    private void HandleConstructInstruction(string rawLine, string[] instructionArgs, Stack <object> stack,
                                            TypeResolverGroup typeResolver)
    {
        if (instructionArgs.Length != 3)
        {
            throw new InvalidOperationException(
                      $"CONSTRUCT instruction requires 2 arguments(TypeName, ArgumentCount). EX: 'CONSTRUCT UnityEngineVector2 2'");
        }
        var typeName = instructionArgs[1];
        var constructorArgumentcount = int.Parse(instructionArgs[2]);

        Stack <object> flipped = new Stack <object>();

        for (int i = 0; i < constructorArgumentcount; i++)
        {
            flipped.Push(stack.Pop());
        }

        object obj = Activator.CreateInstance(typeResolver.GetTypeFromTypeString(typeName), flipped.ToArray());

        stack.Push(obj);
    }
Example #5
0
    private static string RecurseInheritedType(Type originalType, Type type, TypeResolverGroup typeResolverGroup)
    {
        if (type == null || type == typeof(object))
        {
            return("");
        }
        if (originalType == type)
        {
            return(RecurseInheritedType(originalType, type.BaseType, typeResolverGroup));
        }

        if (typeResolverGroup.GetTypeFromTypeString(UdonTypeExporter.GenerateUdonName(type)) != null)
        {
            return(UdonTypeExporter.GetTypeFullName(type));
        }

        if (type.BaseType != null)
        {
            return(RecurseInheritedType(originalType, type.BaseType, typeResolverGroup));
        }

        return("");
    }
Example #6
0
    public static TDType GetOrCreateTDType(TDType rootType, string fullName, TypeResolverGroup typeResolverGroup)
    {
        bool           containsGenericArguments = fullName.Contains("<");
        Queue <string> namespaces;
        string         genericArguments = null;

        if (containsGenericArguments) //Generic types
        {
            var match    = GenericsRegex.Match(fullName);
            var groups   = match.Groups;
            var baseType = groups["GenericBaseType"].ToString();
            genericArguments = groups["GenericArguments"].ToString();
            namespaces       = new Queue <string>(baseType.Split('.'));
        }
        else
        {
            namespaces = new Queue <string>(fullName.Split('.'));
        }

        var current = rootType;

        while (namespaces.Count > 0)
        {
            var name = namespaces.Dequeue();
            //Only the full string is "generic", so it must be the last thing in the queue.
            //IE. System.Collections.Generic isn't generic itself, but System.Collections.Generic.List is generic
            bool isGeneric = containsGenericArguments && namespaces.Count == 0;
            var  child     = current.Children.Find(x =>
                                                   x.TypeName == name && x.IsGeneric == isGeneric && genericArguments == x.InputGenericArguments);
            if (child != null)
            {
                //Go down tree
                current = child;
            }
            else
            {
                //Create an go down tree
                var type = new TDType
                {
                    NamespaceName         = current.FullName,
                    FullName              = (current.FullName != null ? current.FullName + "." : "") + name,
                    TypeName              = name,
                    InputGenericArguments = genericArguments
                };
                string attemptedUdonName =
                    GenerateUdonName(type.FullName, true); //Try to generate udon name and set it if it's correct.
                if (typeResolverGroup.GetTypeFromTypeString(attemptedUdonName) != null)
                {
                    type.UdonName = attemptedUdonName;
                }

                current.Children.Add(type);
                current           = type;
                current.IsGeneric = isGeneric;
            }
        }

        if (current.IsGeneric)
        {
            current.IsGeneric = true;
            if (!genericArguments.Contains("<"))
            {
                current.AddGenericArguments(rootType, typeResolverGroup, genericArguments.Replace(" ", "").Split(','));
            }
            else
            {
                //Only one thing contains a nested generic argument in Udon currently
                //and luckily it looks like "thing<other<something, else>>"
                //which means it's a single layer of nesting
                //So for now we can just pass "other<something, else>" to GetOrCreateTDType
                //In the future this might change?
                current.AddGenericArguments(rootType, typeResolverGroup, genericArguments);
            }
        }

        if (fullName.Contains("[]"))
        {
            //Add base type for arrays
            GetOrCreateTDType(rootType, fullName.Replace("[]", ""), typeResolverGroup);
        }

        return(current);
    }
Example #7
0
    private void HandlePushInstruction(string rawLine, string[] instructionArgs, Stack <object> stack,
                                       TypeResolverGroup typeResolver)
    {
        if (instructionArgs.Length < 3)
        {
            throw new InvalidOperationException(
                      $"PUSH instruction requires 2 arguments(TypeName, literal). EX: 'PUSH SystemSingle 5'");
        }
        var typeName = instructionArgs[1];
        var literal  = rawLine.Substring(rawLine.IndexOf(typeName) + typeName.Length + 1);

        object output = null;

        switch (typeName)
        {
        case "SystemSingle":
            output = Single.Parse(literal);
            break;

        case "SystemDouble":
            output = Double.Parse(literal);
            break;

        case "SystemInt64":
            output = Int64.Parse(literal);
            break;

        case "SystemInt32":
            output = Int32.Parse(literal);
            break;

        case "SystemInt16":
            output = Int16.Parse(literal);
            break;

        case "SystemUInt64":
            output = UInt64.Parse(literal);
            break;

        case "SystemUInt32":
            output = UInt32.Parse(literal);
            break;

        case "SystemUInt16":
            output = UInt16.Parse(literal);
            break;

        case "SystemString":
            output = literal.Trim('"');
            break;

        case "SystemBoolean":
            output = Boolean.Parse(literal);
            break;

        case "SystemChar":
            output = char.Parse(literal);
            break;

        case "SystemByte":
            output = byte.Parse(literal);
            break;

        case "SystemSByte":
            output = sbyte.Parse(literal);
            break;

        case "SystemType":
            output = typeResolver.GetTypeFromTypeString(literal);
            if (output == null)
            {
                throw new InvalidOperationException(
                          $"Unable to find type '{literal}'.");
            }
            break;

        default:
            throw new InvalidOperationException($"PUSH literal unsupported for type {typeName}");
        }

        stack.Push(output);
    }