示例#1
0
        private static Tutorial Load(XElement element)
        {
            Type   t;
            string type = element.Name.ToString().ToLowerInvariant();

            try
            {
                // Get the type of a specified class.
                t = Type.GetType("Barotrauma.Tutorials." + type + "", false, true);
                if (t == null)
                {
                    DebugConsole.ThrowError("Could not find tutorial type \"" + type + "\"");
                    return(null);
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Could not find tutorial type \"" + type + "\"", e);
                return(null);
            }

            ConstructorInfo constructor;

            try
            {
                if (!t.IsSubclassOf(typeof(Tutorial)))
                {
                    return(null);
                }
                constructor = t.GetConstructor(new Type[] { typeof(XElement) });
                if (constructor == null)
                {
                    DebugConsole.ThrowError("Could not find the constructor of tutorial type \"" + type + "\"");
                    return(null);
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Could not find the constructor of tutorial type \"" + type + "\"", e);
                return(null);
            }
            Tutorial tutorial = null;

            try
            {
                object component = constructor.Invoke(new object[] { element });
                tutorial = (Tutorial)component;
            }
            catch (TargetInvocationException e)
            {
                DebugConsole.ThrowError("Error while loading tutorial of the type " + t + ".", e.InnerException);
            }

            return(tutorial);
        }
示例#2
0
        public static void Init()
        {
            Tutorials = new List <Tutorial>();
            foreach (string file in GameMain.Instance.GetFilesOfType(ContentType.Tutorials))
            {
                XDocument doc = XMLExtensions.TryLoadXml(file);
                if (doc?.Root == null)
                {
                    continue;
                }

                foreach (XElement element in doc.Root.Elements())
                {
                    Tutorial newTutorial = Load(element);
                    if (newTutorial != null)
                    {
                        Tutorials.Add(newTutorial);
                    }
                }
            }
        }