Example #1
0
        public static void LoadObjectBuilders(MyDefinitionSet set)
        {
            var definitionFactory  = MyDefinitionFactory.Get();
            var definitionHandlers = definitionFactory.DefinitionHandlers;
            var currentObset       = m_currentObset;

            for (var index1 = 0; index1 < definitionHandlers.Count; ++index1)
            {
                var handler           = definitionHandlers[index1];
                var objectBuilderType = ((MyDefinitionTypeAttribute)m_handlerAttributeField.GetValue(handler)).ObjectBuilderType;
                if (handler.HasBeforeLoad)
                {
                    handler.BeforeLoad(set);
                }
                MyConcurrentSortedDictionary <IApplicationPackage, MyConcurrentList <MyObjectBuilder_DefinitionBase> > modBuilders;
                if (m_modObjectBuilders.TryGetValue(objectBuilderType, out modBuilders))
                {
                    MergeBuilders(handler, modBuilders, m_currentObset);
                    ResolveInheritance(handler, m_currentObset);
                    for (var index2 = 0; index2 < m_currentObset.Count; ++index2)
                    {
                        var builder = currentObset[index2];
                        if (!builder.Abstract)
                        {
                            Loaded.AddOrReplaceDefinition(builder);
                        }
                    }

                    m_currentObset.Clear();
                }
            }
        }
        public T Translate <T>(T input, bool forceCopy = false) where T : MyObjectBuilder_DefinitionBase
        {
            var translatedId = new MyDefinitionId(input.Id.TypeId, input.Id.SubtypeIdAttribute + SubtypeSuffix);
            var translated   = DestinationSet.GetDefinition <T>(translatedId);

            if (translated != null && translated.GetType() == input.GetType())
            {
                return(translated);
            }

            var args = new object[] { input, input };

            foreach (var translator in Translators)
            {
                if (translator.GetParameters()[0].ParameterType.IsInstanceOfType(input))
                {
                    translator.Invoke(this, args);
                }
            }

            var output = (T)args[1];

            if (output == input && forceCopy)
            {
                output = (T)MyObjectBuilderSerializer.Clone(input);
            }
            if (output != input)
            {
                foreach (var translator in ConditionalTranslators)
                {
                    if (translator.GetParameters()[0].ParameterType.IsInstanceOfType(input))
                    {
                        translator.Invoke(this, new object[] { output });
                    }
                }

                output.Id = translatedId;
                DestinationSet.AddOrReplaceDefinition(output);
                Console.WriteLine("Translated " + input.GetType().Name + " " + input.Id + " to " + output.Id);
            }

            return(output);
        }