Ejemplo n.º 1
0
        public static T SaveableFromNode <T>(XmlNode subNode, object[] ctorArgs)
        {
            if (Scribe.mode != LoadSaveMode.LoadingVars)
            {
                Log.Error("Called SaveableFromNode(), but mode is " + Scribe.mode, false);
                return(default(T));
            }
            if (subNode == null)
            {
                return(default(T));
            }
            XmlAttribute xmlAttribute = subNode.Attributes["IsNull"];
            T            result;

            if (xmlAttribute != null && xmlAttribute.Value.ToLower() == "true")
            {
                result = default(T);
            }
            else
            {
                try
                {
                    XmlAttribute xmlAttribute2 = subNode.Attributes["Class"];
                    string       text          = (xmlAttribute2 == null) ? typeof(T).FullName : xmlAttribute2.Value;
                    Type         type          = BackCompatibility.GetBackCompatibleType(typeof(T), text, subNode);
                    if (type == null)
                    {
                        Type bestFallbackType = ScribeExtractor.GetBestFallbackType <T>(subNode);
                        Log.Error(string.Concat(new object[]
                        {
                            "Could not find class ",
                            text,
                            " while resolving node ",
                            subNode.Name,
                            ". Trying to use ",
                            bestFallbackType,
                            " instead. Full node: ",
                            subNode.OuterXml
                        }), false);
                        type = bestFallbackType;
                    }
                    if (type.IsAbstract)
                    {
                        throw new ArgumentException("Can't load abstract class " + type);
                    }
                    IExposable exposable = (IExposable)Activator.CreateInstance(type, ctorArgs);
                    bool       flag      = typeof(T).IsValueType || typeof(Name).IsAssignableFrom(typeof(T));
                    if (!flag)
                    {
                        Scribe.loader.crossRefs.RegisterForCrossRefResolve(exposable);
                    }
                    XmlNode    curXmlParent       = Scribe.loader.curXmlParent;
                    IExposable curParent          = Scribe.loader.curParent;
                    string     curPathRelToParent = Scribe.loader.curPathRelToParent;
                    Scribe.loader.curXmlParent       = subNode;
                    Scribe.loader.curParent          = exposable;
                    Scribe.loader.curPathRelToParent = null;
                    try
                    {
                        exposable.ExposeData();
                    }
                    finally
                    {
                        Scribe.loader.curXmlParent       = curXmlParent;
                        Scribe.loader.curParent          = curParent;
                        Scribe.loader.curPathRelToParent = curPathRelToParent;
                    }
                    if (!flag)
                    {
                        Scribe.loader.initer.RegisterForPostLoadInit(exposable);
                    }
                    result = (T)((object)exposable);
                }
                catch (Exception ex)
                {
                    result = default(T);
                    Log.Error(string.Concat(new object[]
                    {
                        "SaveableFromNode exception: ",
                        ex,
                        "\nSubnode:\n",
                        subNode.OuterXml
                    }), false);
                }
            }
            return(result);
        }