コード例 #1
0
        /// <summary>
        /// Generate method which will extract and populate context with dependencies (referenced unity objects)
        /// </summary>
        /// <param name="mapping"></param>
        /// <returns></returns>
        private string CreateDepsFromMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled)
                {
                    continue;
                }
                if (prop.HasDependenciesOrIsDependencyItself)
                {
                    if (prop.UseSurrogate)
                    {
                        sb.Append(TAB);
                        sb.AppendFormat("AddSurrogateDeps(uo.{0}, context);", prop.MappedName);
                        sb.Append(BR + TAB2);
                    }
                    if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) || prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)))
                    {
                        sb.Append(TAB);
                        sb.AppendFormat("AddDep(uo.{0}, context);", prop.MappedName);
                        sb.Append(BR + TAB2);
                    }
                }
            }
            return(sb.ToString());
        }
コード例 #2
0
        private string CreateReadMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled)
                {
                    continue;
                }

                sb.Append(TAB);


                if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) || prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)))
                {
                    //generate code which will convert unity object to identifier
                    sb.AppendFormat("{0} = ToId(uo.{1});", prop.PersistentName, prop.MappedName);
                }
                else
                {
                    sb.AppendFormat("{0} = uo.{1};", prop.PersistentName, prop.MappedName);
                }

                sb.Append(BR + TAB2);
            }

            return(sb.ToString());
        }
コード例 #3
0
        private string CreateWriteMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled)
                {
                    continue;
                }

                sb.Append(TAB);

                if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) || prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)))
                {
                    //generate code which will convert identifier to unity object

                    Type mappedType = prop.MappedType.IsArray ? prop.MappedType.GetElementType() : prop.MappedType;
                    sb.AppendFormat("uo.{0} = FromId<{2}>({1});", prop.MappedName, prop.PersistentName, PrepareMappedTypeName(mappedType.Name));
                }
                else
                {
                    sb.AppendFormat("uo.{0} = {1};", prop.MappedName, prop.PersistentName);
                }

                sb.Append(BR + TAB2);
            }

            return(sb.ToString());
        }
コード例 #4
0
        private string CreateReadMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled || prop.HasPropertyInTemplate)
                {
                    continue;
                }

                sb.Append(TAB);

                string get = "uo.{1}";
                if (prop.IsNonPublic)
                {
                    get = "GetPrivate<" + prop.MappedTypeName + ">(uo, \"{1}\")";
                }


                if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) ||
                    prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)) ||
                    IsGenericList(prop.MappedType) && prop.MappedType.GetGenericArguments()[0].IsSubclassOf(typeof(UnityObject)))
                {
                    //generate code which will convert unity object to identifier
                    sb.AppendFormat("{0} = ToID(" + get + ");", prop.PersistentName, prop.MappedName);
                }
                else
                {
                    if (prop.UseSurrogate)
                    {
                        if (IsGenericList(prop.MappedType))
                        {
                            sb.AppendFormat("{0} = Assign(" + get + ", v_ => ({2})v_);", prop.PersistentName, prop.MappedName, "Persistent" + prop.MappedType.GetGenericArguments()[0].Name);
                        }
                        else if (prop.MappedType.IsArray)
                        {
                            sb.AppendFormat("{0} = Assign(" + get + ", v_ => ({2})v_);", prop.PersistentName, prop.MappedName, "Persistent" + prop.MappedType.GetElementType().Name);
                        }
                        else
                        {
                            sb.AppendFormat("{0} = " + get + ";", prop.PersistentName, prop.MappedName);
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0} = " + get + ";", prop.PersistentName, prop.MappedName);
                    }
                }

                sb.Append(BR + TAB2);
            }

            return(sb.ToString());
        }
コード例 #5
0
        private string CreateNamespaceDefinitions(PersistentClassMapping[] mappings)
        {
            StringBuilder sb = new StringBuilder();

            HashSet <string> nsHs = new HashSet <string>();

            for (int m = 0; m < mappings.Length; ++m)
            {
                PersistentClassMapping mapping = mappings[m];
                if (!mapping.IsEnabled)
                {
                    continue;
                }

                if (!nsHs.Contains(mapping.PersistentNamespace))
                {
                    nsHs.Add(mapping.PersistentNamespace);
                }

                for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
                {
                    PersistentPropertyMapping pMapping = mapping.PropertyMappings[i];
                    if (!pMapping.IsEnabled || pMapping.HasPropertyInTemplate)
                    {
                        continue;
                    }
                    if (!nsHs.Contains(pMapping.PersistentNamespace))
                    {
                        nsHs.Add(pMapping.PersistentNamespace);
                    }
                }
            }

            foreach (string ns in nsHs)
            {
                sb.AppendFormat(NamespaceDefinitionTemplate, ns);
                sb.Append(BR);
            }

            return(sb.ToString());
        }
コード例 #6
0
        /// <summary>
        /// Generate method which will extract and populate context with dependencies (referenced unity objects)
        /// </summary>
        /// <param name="mapping"></param>
        /// <returns></returns>
        private string CreateDepsFromMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled || prop.HasPropertyInTemplate)
                {
                    continue;
                }
                if (prop.HasDependenciesOrIsDependencyItself)
                {
                    string get = "uo.{0}";
                    if (prop.IsNonPublic)
                    {
                        get = "GetPrivate<" + prop.MappedTypeName + ">(uo, \"{0}\")";
                    }

                    if (prop.UseSurrogate)
                    {
                        sb.Append(TAB);
                        sb.AppendFormat("AddSurrogateDeps(" + get + ", v_ => ({1})v_, context);", prop.MappedName, "Persistent" + prop.PersistentTypeName);
                        sb.Append(BR + TAB2);
                    }
                    if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) ||
                        prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)) ||
                        IsGenericList(prop.MappedType) && prop.MappedType.GetGenericArguments()[0].IsSubclassOf(typeof(UnityObject)))
                    {
                        sb.Append(TAB);
                        sb.AppendFormat("AddDep(" + get + ", context);", prop.MappedName);
                        sb.Append(BR + TAB2);
                    }
                }
            }
            return(sb.ToString());
        }
コード例 #7
0
        private string CreateUsings(params PersistentClassMapping[] mappings)
        {
            StringBuilder    sb         = new StringBuilder();
            HashSet <string> namespaces = new HashSet <string>();

            for (int m = 0; m < mappings.Length; ++m)
            {
                PersistentClassMapping mapping = mappings[m];
                if (mapping == null)
                {
                    continue;
                }

                for (int i = 0; i < DefaultNamespaces.Length; ++i)
                {
                    namespaces.Add(DefaultNamespaces[i]);
                }

                if (!namespaces.Contains(mapping.MappedNamespace))
                {
                    namespaces.Add(mapping.MappedNamespace);
                }

                if (!namespaces.Contains(mapping.PersistentNamespace))
                {
                    namespaces.Add(mapping.PersistentNamespace);
                }

                if (!namespaces.Contains(mapping.PersistentBaseNamespace))
                {
                    namespaces.Add(mapping.PersistentBaseNamespace);
                }

                if (mapping.IsEnabled)
                {
                    for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
                    {
                        PersistentPropertyMapping propertyMapping = mapping.PropertyMappings[i];
                        if (!propertyMapping.IsEnabled)
                        {
                            continue;
                        }
                        if (!namespaces.Contains(propertyMapping.MappedNamespace))
                        {
                            namespaces.Add(propertyMapping.MappedNamespace);
                        }

                        Type type            = propertyMapping.MappedType;
                        Type replacementType = GetReplacementType(type);
                        if (replacementType != null)
                        {
                            if (!namespaces.Contains(replacementType.Namespace))
                            {
                                namespaces.Add(replacementType.Namespace);
                            }
                        }
                        else
                        {
                            if (!type.FullName.Contains("System"))
                            {
                                if (!namespaces.Contains(propertyMapping.PersistentNamespace))
                                {
                                    namespaces.Add(propertyMapping.PersistentNamespace);
                                }
                            }
                        }
                    }
                }
            }
            foreach (string ns in namespaces)
            {
                sb.Append("using " + ns + ";" + BR);
            }

            return(sb.ToString());
        }
コード例 #8
0
        private string CreatePersistentClassBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled)
                {
                    continue;
                }

                string typeName;
                Type   repacementType = GetReplacementType(prop.MappedType);
                if (repacementType != null)
                {
                    string primitiveTypeName;
                    if (m_primitiveNames.TryGetValue(repacementType, out primitiveTypeName))
                    {
                        typeName = primitiveTypeName;
                    }
                    else
                    {
                        typeName = PrepareMappedTypeName(repacementType.Name);
                    }
                }
                else
                {
                    string primitiveTypeName;
                    if (m_primitiveNames.TryGetValue(prop.MappedType, out primitiveTypeName))
                    {
                        typeName = primitiveTypeName;
                    }
                    else
                    {
                        typeName = PreparePersistentTypeName(prop.PersistentTypeName);
                    }
                }

                sb.AppendFormat(
                    FieldTemplate, i + AutoFieldTagOffset,
                    typeName,
                    prop.PersistentName);
            }

            string readMethodBody        = CreateReadMethodBody(mapping);
            string writeMethodBody       = CreateWriteMethodBody(mapping);
            string getDepsMethodBody     = CreateDepsMethodBody(mapping);
            string getDepsFromMethodBody = CreateDepsFromMethodBody(mapping);

            string mappedTypeName = PrepareMappedTypeName(mapping.MappedTypeName);

            if (mappedTypeName == "Object")
            {
                mappedTypeName = "UnityObject";
            }

            if (!string.IsNullOrEmpty(readMethodBody))
            {
                sb.AppendFormat(ReadFromMethodTemplate, readMethodBody, mappedTypeName);
            }
            if (!string.IsNullOrEmpty(writeMethodBody))
            {
                sb.Append(BR + TAB2);
                sb.AppendFormat(WriteToMethodTemplate, writeMethodBody, mappedTypeName);
            }
            if (!string.IsNullOrEmpty(getDepsMethodBody))
            {
                sb.Append(BR + TAB2);
                sb.AppendFormat(GetDepsMethodTemplate, getDepsMethodBody);
            }
            if (!string.IsNullOrEmpty(getDepsFromMethodBody))
            {
                sb.Append(BR + TAB2);
                sb.AppendFormat(GetDepsFromMethodTemplate, getDepsFromMethodBody, mappedTypeName);
            }

            Type mappingType = Type.GetType(mapping.MappedAssemblyQualifiedName);

            if (mappingType.GetConstructor(Type.EmptyTypes) != null || mappingType.IsValueType)
            {
                sb.Append(BR + TAB2);
                sb.AppendFormat(ImplicitOperatorsTemplate, mappedTypeName, PreparePersistentTypeName(mapping.PersistentTypeName));
            }

            return(sb.ToString());
        }
コード例 #9
0
        private string CreateWriteMethodBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mapping.PropertyMappings.Length; ++i)
            {
                PersistentPropertyMapping prop = mapping.PropertyMappings[i];
                if (!prop.IsEnabled || prop.HasPropertyInTemplate)
                {
                    continue;
                }

                sb.Append(TAB);

                string get = "uo.{0}";
                if (prop.IsNonPublic)
                {
                    get = "GetPrivate<" + prop.MappedTypeName + ">(uo, \"{0}\")";
                }

                if (prop.MappedType.IsSubclassOf(typeof(UnityObject)) ||
                    prop.MappedType.IsArray && prop.MappedType.GetElementType().IsSubclassOf(typeof(UnityObject)) ||
                    IsGenericList(prop.MappedType) && prop.MappedType.GetGenericArguments()[0].IsSubclassOf(typeof(UnityObject)))
                {
                    //generate code which will convert identifier to unity object

                    //Type mappedType = prop.MappedType.IsArray ? prop.MappedType.GetElementType() : prop.MappedType;
                    //sb.AppendFormat("uo.{0} = FromID<{2}>({1}, uo.{0});", prop.MappedName, prop.PersistentName, PrepareMappedTypeName(mappedType.Name));
                    if (prop.IsNonPublic)
                    {
                        sb.AppendFormat("SetPrivate(uo, \"{0}\", FromID({1}, " + get + "));", prop.MappedName, prop.PersistentName);
                    }
                    else
                    {
                        sb.AppendFormat("uo.{0} = FromID({1}, " + get + ");", prop.MappedName, prop.PersistentName);
                    }
                }
                else
                {
                    if (prop.UseSurrogate)
                    {
                        if (IsGenericList(prop.MappedType))
                        {
                            if (prop.IsNonPublic)
                            {
                                sb.AppendFormat("SetPrivate(uo, \"{0}\", Assign({1}, v_ => ({2})v_));", prop.MappedName, prop.PersistentName, prop.MappedType.GetGenericArguments()[0].Name);
                            }
                            else
                            {
                                sb.AppendFormat("uo.{0} = Assign({1}, v_ => ({2})v_);", prop.MappedName, prop.PersistentName, prop.MappedType.GetGenericArguments()[0].Name);
                            }
                        }
                        else if (prop.MappedType.IsArray)
                        {
                            if (prop.IsNonPublic)
                            {
                                sb.AppendFormat("SetPrivate(uo, \"{0}\", Assign({1}, v_ => ({2})v_));", prop.MappedName, prop.PersistentName, prop.MappedType.GetElementType().Name);
                            }
                            else
                            {
                                sb.AppendFormat("uo.{0} = Assign({1}, v_ => ({2})v_);", prop.MappedName, prop.PersistentName, prop.MappedType.GetElementType().Name);
                            }
                        }
                        else
                        {
                            if (prop.IsNonPublic)
                            {
                                sb.AppendFormat("SetPrivate(uo, \"{0}\", {1});", prop.MappedName, prop.PersistentName);
                            }
                            else
                            {
                                sb.AppendFormat("uo.{0} = {1};", prop.MappedName, prop.PersistentName);
                            }
                        }
                    }
                    else
                    {
                        if (prop.IsNonPublic)
                        {
                            sb.AppendFormat("SetPrivate(uo, \"{0}\", {1});", prop.MappedName, prop.PersistentName);
                        }
                        else
                        {
                            sb.AppendFormat("uo.{0} = {1};", prop.MappedName, prop.PersistentName);
                        }
                    }
                }

                sb.Append(BR + TAB2);
            }

            return(sb.ToString());
        }