Exemplo n.º 1
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());
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        private string CreateAddSubtypesBody(PersistentClassMapping mapping)
        {
            StringBuilder sb = new StringBuilder();

            PersistentSubclass[] subclasses = mapping.Subclasses.Where(sc => sc.IsEnabled).ToArray();
            for (int i = 0; i < subclasses.Length - 1; ++i)
            {
                PersistentSubclass subclass = mapping.Subclasses[i];
                if (subclass.IsEnabled)
                {
                    sb.Append(BR + TAB3 + TAB);
                    sb.AppendFormat(AddSubtypeTemplate, subclass.TypeName, subclass.PersistentTag + SubclassOffset);
                }
            }

            if (subclasses.Length > 0)
            {
                if (mapping.Subclasses[subclasses.Length - 1].IsEnabled)
                {
                    PersistentSubclass subclass = mapping.Subclasses[subclasses.Length - 1];
                    sb.Append(BR + TAB3 + TAB);
                    sb.AppendFormat(AddSubtypeTemplate, subclass.TypeName, subclass.PersistentTag + SubclassOffset);
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 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)
                {
                    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());
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
0
        private string CreateTypeModelCreatorBody(PersistentClassMapping[] mappings)
        {
            StringBuilder sb = new StringBuilder();

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

                if (!mapping.IsEnabled)
                {
                    continue;
                }
                string endOfLine = string.Empty;
                if (mapping.Subclasses != null && mapping.Subclasses.Where(s => s.IsEnabled).Count() > 0)
                {
                    endOfLine = CreateAddSubtypesBody(mapping);
                }

                bool hasSurrogate = false;
                Type mappingType  = Type.GetType(mapping.MappedAssemblyQualifiedName);
                if (mappingType == null)
                {
                    Debug.LogWarning("Type " + mapping.MappedAssemblyQualifiedName + " was not found");
                }
                else
                {
                    if (GetSurrogateType(mappingType) != null)
                    {
                        endOfLine   += string.Format(SetSerializationSurrogate, PreparePersistentTypeName(mapping.PersistentTypeName));
                        hasSurrogate = true;
                    }
                }

                endOfLine += SEMICOLON + BR + TAB3;

                if (hasSurrogate)
                {
                    sb.AppendFormat(AddTypeTemplate, PrepareMappedTypeName(mapping.MappedTypeName), "false", endOfLine);
                }
                else if (mappingType != null)
                {
                    if (mappingType.IsSubclassOf(typeof(UnityObject)) || mappingType == typeof(UnityObject) || mappingType == typeof(UnityEventBase))
                    {
                        sb.AppendFormat(AddTypeTemplate, PreparePersistentTypeName(mapping.PersistentTypeName), "true", endOfLine);
                    }
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 7
0
        /// <summary>
        /// Generate C# code of persistent class using persistent class mapping
        /// </summary>
        /// <param name="mapping"></param>
        /// <returns></returns>
        public string CreatePersistentClass(PersistentClassMapping mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }
            string usings        = CreateUsings(mapping);
            string ns            = mapping.PersistentNamespace;
            string className     = PreparePersistentTypeName(mapping.PersistentTypeName);
            string baseClassName = mapping.PersistentBaseTypeName != null?
                                   PreparePersistentTypeName(mapping.PersistentBaseTypeName) : null;

            string body = mapping.IsEnabled ? CreatePersistentClassBody(mapping) : string.Empty;

            return(string.Format(PersistentClassTemplate, usings, ns, className, baseClassName, body));
        }
Exemplo n.º 8
0
        private string CreateTypeMapBody(PersistentClassMapping[] mappings)
        {
            StringBuilder sb = new StringBuilder();

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

                if (!mapping.IsEnabled)
                {
                    continue;
                }

                Type mappingType = Type.GetType(mapping.MappedAssemblyQualifiedName);
                if (mappingType == null)
                {
                    Debug.LogWarning("Type " + mapping.MappedAssemblyQualifiedName + " was not found");
                    continue;
                }

                string mappedTypeName = mappingType.Name;
                if (mappedTypeName == "Object")
                {
                    mappedTypeName = "UnityObject";
                }
                sb.AppendFormat(AddToPersistentTypeTemplate, mappedTypeName, mapping.PersistentTypeName);
                sb.Append(TAB3);
                sb.AppendFormat(AddToUnityTypeTemplate, mapping.PersistentTypeName, mappedTypeName);
                sb.Append(TAB3);
                sb.AppendFormat(AddToGuidTemplate, mapping.PersistentTypeName, mapping.PersistentTypeGUID);
                sb.Append(TAB3);
                sb.AppendFormat(AddToGuidTemplate, mappedTypeName, mapping.MappedTypeGUID);
                sb.Append(TAB3);
                sb.AppendFormat(AddToTypeTemplate, mapping.PersistentTypeGUID, mapping.PersistentTypeName);
                sb.Append(TAB3);
                sb.AppendFormat(AddToTypeTemplate, mapping.MappedTypeGUID, mappedTypeName);
                sb.Append(TAB3);
            }
            sb.Append(BR);
            return(sb.ToString());
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
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());
        }
Exemplo n.º 11
0
        private string CreateTypeModelCreatorBody(PersistentClassMapping[] mappings)
        {
            StringBuilder sb = new StringBuilder();

            for (int m = 0; m < mappings.Length; ++m)
            {
                PersistentClassMapping mapping = mappings[m];
                if (mapping == null)
                {
                    continue;
                }
                string endOfLine = string.Empty;
                if (mapping.Subclasses != null && mapping.Subclasses.Length > 0)
                {
                    endOfLine = CreateAddSubtypesBody(mapping);
                }

                Type mappingType = Type.GetType(mapping.MappedAssemblyQualifiedName);
                if (mappingType == null)
                {
                    Debug.LogWarning("Type " + mapping.MappedAssemblyQualifiedName + " was not found");
                }
                else
                {
                    if (GetSurrogateType(mappingType) != null)
                    {
                        endOfLine += string.Format(SetSerializationSurrogate, PreparePersistentTypeName(mapping.PersistentTypeName));
                    }
                }


                endOfLine += SEMICOLON + BR + TAB3 + TAB;
                sb.AppendFormat(AddTypeTemplate, PreparePersistentTypeName(mapping.PersistentTypeName), endOfLine);
            }

            return(sb.ToString());
        }
Exemplo n.º 12
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());
        }
Exemplo n.º 13
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());
        }
Exemplo n.º 14
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());
        }