public override ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            XmlDocument xmlDoc = LoadXmlScheme(schemeName); // Create an XML document object

            if (xmlDoc == null)
            {
                return(null);
            }
            XmlNode     root     = xmlDoc.GetElementsByTagName("dict")[0];
            XmlNodeList children = root.ChildNodes;

            uint[] colorTable = new uint[ColorTableSize];
            uint?  fgColor = null, bgColor = null, cursorColor = null;
            int    colorsFound = 0;
            bool   success     = false;

            foreach (var tableEntry in children.OfType <XmlNode>().Where(_ => _.Name == "key"))
            {
                uint    rgb        = 0;
                int     index      = -1;
                XmlNode components = tableEntry.NextSibling;
                success = ParseRgbFromXml(components, ref rgb);
                if (!success)
                {
                    break;
                }
                else if (tableEntry.InnerText == ForegroundKey)
                {
                    fgColor = rgb;
                }
                else if (tableEntry.InnerText == BackgroundKey)
                {
                    bgColor = rgb;
                }
                else if (tableEntry.InnerText == CursorKey)
                {
                    cursorColor = rgb;
                }
                else if (-1 != (index = Array.IndexOf(PListColorNames, tableEntry.InnerText)))
                {
                    colorTable[index] = rgb; colorsFound++;
                }
            }
            if (colorsFound < ColorTableSize)
            {
                if (reportErrors)
                {
                    Console.WriteLine(Resources.InvalidNumberOfColors);
                }
                success = false;
            }
            if (!success)
            {
                return(null);
            }

            var consoleAttributes = new ConsoleAttributes(bgColor, fgColor, null, null, cursorColor);

            return(new ColorScheme(ExtractSchemeName(schemeName), colorTable, consoleAttributes));
        }
Exemple #2
0
        public override ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            XmlDocument xmlDoc = LoadJsonFile(schemeName);

            if (xmlDoc == null)
            {
                return(null);
            }

            try
            {
                XmlNode     root       = xmlDoc.DocumentElement;
                XmlNodeList children   = root.ChildNodes;
                uint[]      colorTable = new uint[ColorTableSize];;
                for (int i = 0; i < ColorTableSize; i++)
                {
                    string name = ConcfgColorNames[i];
                    var    node = children.OfType <XmlNode>().Where(n => n.Name == name).Single();
                    colorTable[i] = ParseColor(node.InnerText);
                }


                uint?popupForeground = null;
                uint?popupBackground = null;

                var popupNode = children.OfType <XmlNode>().Where(n => n.Name == "popup_colors").SingleOrDefault();
                if (popupNode != null)
                {
                    var parts = popupNode.InnerText.Split(',');
                    if (parts.Length == 2)
                    {
                        var foregroundIndex = (ConcfgColorNames as IList <string>).IndexOf(parts[0]);
                        var backgroundIndex = (ConcfgColorNames as IList <string>).IndexOf(parts[1]);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            popupForeground = colorTable[foregroundIndex];
                            popupBackground = colorTable[backgroundIndex];
                        }
                    }
                }

                uint?screenForeground = null;
                uint?screenBackground = null;

                var screenNode = children.OfType <XmlNode>().Where(n => n.Name == "screen_colors").SingleOrDefault();
                if (screenNode != null)
                {
                    var parts = screenNode.InnerText.Split(',');
                    if (parts.Length == 2)
                    {
                        var foregroundIndex = (ConcfgColorNames as IList <string>).IndexOf(parts[0]);
                        var backgroundIndex = (ConcfgColorNames as IList <string>).IndexOf(parts[1]);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            screenForeground = colorTable[foregroundIndex];
                            screenBackground = colorTable[backgroundIndex];
                        }
                    }
                }

                var consoleAttributes = new ConsoleAttributes(screenBackground, screenForeground, popupBackground, popupForeground, null);
                return(new ColorScheme(ExtractSchemeName(schemeName), colorTable, consoleAttributes));
            }
            catch (Exception /*e*/)
            {
                if (reportErrors)
                {
                    Console.WriteLine("failed to load json scheme");
                }

                return(null);
            }
        }
        public override ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            bool success = true;

            string filename = FindIniScheme(schemeName);

            if (filename == null)
            {
                return(null);
            }

            string[] tableStrings         = new string[ColorTableSize];
            uint[]   colorTable           = null;
            uint?    foregroundColor      = null;
            uint?    backgroundColor      = null;
            uint?    popupForegroundColor = null;
            uint?    popupBackgroundColor = null;
            uint?    cursorColor          = null;

            for (int i = 0; i < ColorTableSize; i++)
            {
                string        name   = ColorNames[i];
                StringBuilder buffer = new StringBuilder(512);
                GetPrivateProfileString("table", name, null, buffer, 512, filename);

                tableStrings[i] = buffer.ToString();
                if (tableStrings[i].Length <= 0)
                {
                    success = false;
                    if (reportErrors)
                    {
                        Console.WriteLine(string.Format(Resources.IniParseError, filename, name, tableStrings[i]));
                    }
                    break;
                }
            }

            if (success)
            {
                try
                {
                    colorTable = new uint[ColorTableSize];
                    for (int i = 0; i < ColorTableSize; i++)
                    {
                        colorTable[i] = ParseColor(tableStrings[i]);
                    }

                    if (ReadAttributes("popup", out var foreground, out var background, out var cursor))
                    {
                        var foregroundIndex = (ColorNames as IList <string>).IndexOf(foreground);
                        var backgroundIndex = (ColorNames as IList <string>).IndexOf(background);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            popupForegroundColor = colorTable[foregroundIndex];
                            popupBackgroundColor = colorTable[backgroundIndex];
                        }
                    }

                    if (ReadAttributes("screen", out foreground, out background, out cursor))
                    {
                        var foregroundIndex = (ColorNames as IList <string>).IndexOf(foreground);
                        var backgroundIndex = (ColorNames as IList <string>).IndexOf(background);
                        if (foregroundIndex != -1)
                        {
                            foregroundColor = colorTable[foregroundIndex];
                        }
                        else
                        {
                            foregroundColor = ParseColor(foreground);
                        }
                        if (backgroundIndex != -1)
                        {
                            backgroundColor = colorTable[backgroundIndex];
                        }
                        else
                        {
                            backgroundColor = ParseColor(background);
                        }
                        if (cursor.Length > 1)
                        {
                            cursorColor = ParseColor(cursor);
                        }
                    }
                }
                catch (Exception /*e*/)
                {
                    if (reportErrors)
                    {
                        Console.WriteLine(string.Format(Resources.IniLoadError, filename));
                    }

                    colorTable = null;
                }
            }

            if (colorTable != null)
            {
                var consoleAttributes = new ConsoleAttributes(backgroundColor, foregroundColor, popupBackgroundColor, popupForegroundColor, cursorColor);
                return(new ColorScheme(ExtractSchemeName(schemeName), colorTable, consoleAttributes));
            }
            else
            {
                return(null);
            }

            bool ReadAttributes(string section, out string foreground, out string background, out string cursor)
            {
                foreground = null;
                background = null;
                cursor     = null;

                StringBuilder buffer = new StringBuilder(512);

                GetPrivateProfileString(section, "FOREGROUND", null, buffer, 512, filename);
                foreground = buffer.ToString();
                if (foreground.Length <= 1)
                {
                    return(false);
                }


                buffer = new StringBuilder(512);
                GetPrivateProfileString(section, "BACKGROUND", null, buffer, 512, filename);
                background = buffer.ToString();
                if (background.Length <= 1)
                {
                    return(false);
                }

                buffer = new StringBuilder(512);
                GetPrivateProfileString(section, "CURSOR", null, buffer, 512, filename);
                cursor = buffer.ToString();

                return(true);
            }
        }