예제 #1
0
        public static PageGenerator FromXml(XDocument xml)
        {
            var pagegen = new PageGenerator();

            foreach (XAttribute attr in xml.Root.Attributes())
            {
                pagegen._params.Add("$" + attr.Name.LocalName, attr.Value);
            }
            foreach (XNode node in xml.Root.Nodes())
            {
                if (node is XText)
                {
                    pagegen.Blocks.Add(new TextBlock((node as XText).Value));
                }
                else if (node is XElement)
                {
                    XElement xe = node as XElement;

                    // try to instantiate the PageSegment
                    try
                    {
                        // find the type
                        string xmlns    = xe.Name.NamespaceName;
                        var    xs       = xmlns.SplitAt(";asm=");
                        string typename = xs.Item1 + "." + xe.Name.LocalName;
                        if (xs.Item2.Length > 0)
                        {
                            typename += ", " + xs.Item2;
                        }
                        Type type = Type.GetType(typename);

                        // try to instantiate it
                        PageBlock obj = Activator.CreateInstance(type) as PageBlock;
                        foreach (XAttribute xa in xe.Attributes())
                        {
                            PropertyInfo property = type.GetProperty(xa.Name.LocalName);
                            string       value    = xa.Value;
                            if (value.StartsWith("$"))
                            {
                                value = pagegen._params.GetValueOrDefault(xa.Value);
                            }
                            if (property != null)
                            {
                                // try and set it
                                if (property.PropertyType == typeof(int))
                                {
                                    property.SetValue(obj, int.Parse(value), null);
                                }
                                else if (property.PropertyType == typeof(bool))
                                {
                                    property.SetValue(obj, bool.Parse(value), null);
                                }
                                else
                                {
                                    property.SetValue(obj, value, null);
                                }
                            }
                            else
                            {
                                // ignore it?
                            }
                        }
                        pagegen.Blocks.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        pagegen.Blocks.Add(new UnknownXNodeBlock(node));
                        pagegen.Blocks.Add(new TextBlock("<!-- " + ex.ToString() + " -->"));
                    }
                }
                else
                {
                    // ignore it
                }
            }
            return(pagegen);
        }
예제 #2
0
        public static string Execute(string profilePath)
        {
            XDocument xml = XDocument.Load(profilePath);

            DataStore data = new DataStore();

            DataStore.LoadRewriter("playerpka.dict", data.IdRewriter);
            DataStore.LoadRewriter("mapakas.dict", data.MapRewriter);

            // load additional conformance rules
            foreach (XElement xe in xml.Root.Elements("Map"))
            {
                string target = xe.Attribute("To").Value;
                if (xe.Attribute("Map") != null)
                {
                    data.MapRewriter[xe.Attribute("Map").Value] = target;
                }
                if (xe.Attribute("Player") != null)
                {
                    data.IdRewriter[xe.Attribute("Player").Value] = target;
                }
                if (xe.Attribute("Team") != null)
                {
                    data.IdRewriter[xe.Attribute("Team").Value] = target;
                }
            }

            // read participant lists if they exist
            foreach (XElement xe in xml.Root.Elements("Participants"))
            {
                string page = xe.Attribute("Page").Value;
                data.AccumulateParticipants(page);
            }

            // process pages
            foreach (XElement xe in xml.Root.Elements("Matches"))
            {
                string page = xe.Attribute("Page").Value;

                // read placements map
                var placementMap = new Dictionary <string, Placement>();
                foreach (XElement xeMap in xe.Elements("Map"))
                {
                    string finish = xeMap.Attribute("Finish").Value;
                    string pbg    = xeMap.Attribute("Placement").Value;
                    string points = xeMap.Attribute("Points").Value;
                    var    sort   = xeMap.Attribute("Sort");

                    if (sort == null)
                    {
                        placementMap[finish] = new Placement(pbg, points);
                    }
                    else
                    {
                        placementMap[finish] = new Placement(pbg, points, int.Parse(sort.Value));
                    }
                }

                // process data
                try
                {
                    data.PlacementMap = placementMap;
                    data.Accumulate(page);
                }
                catch
                {
                    // just swallow errors for now
                }
            }

            // emit
            string        pageGenTemplate = xml.Root.Element("PageGenTemplate").Attribute("Template").Value;
            PageGenerator pagegen         = PageGenerator.FromXml(XDocument.Load("pages/" + pageGenTemplate));

            return(pagegen.Emit(data));
        }