public static void Save(FractronConfig conf)
        {
            //it is a bad idea to save a configuration that hasn't been validated
            if (!conf.Valid)
            {
                return;
            }

            FileStream fs = null;

            try{
                fs = File.Open(FractronConfigFileName, FileMode.Create, FileAccess.Write);
                TextWriter writer = new StreamWriter(fs);
                conf.WriteXml(writer);
            }
            catch (Exception ex)
            {
                throw(ex);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
        public FractronConfig(FractronConfig src)         //copies config from src
        {
            this.Valid            = src.Valid;
            this.EngineType       = src.EngineType;
            this.DeviceID         = src.DeviceID;
            this.AutoSizeRenderer = src.AutoSizeRenderer;
            this.CustomRes        = src.CustomRes;
            this.TargetQuality    = src.TargetQuality;
            this.RedrawQuality    = src.RedrawQuality;

            this.ImageDir           = src.ImageDir;
            this.PaletteDir         = src.PaletteDir;
            this.CurrentLibraryFile = src.CurrentLibraryFile;
        }
        internal static FractalList ReadFlameFile(string filename, FractronConfig conf)
        {
            FractalList newFractals = new FractalList();
            Fractal     newFractal;

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.CheckCharacters  = false;
            settings.CloseInput       = true;
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.IgnoreComments   = false;
            settings.IgnoreWhitespace = false;
            settings.ValidationType   = ValidationType.None;

            XmlReader reader = XmlReader.Create(filename, settings);

            XmlDocument doc = new XmlDocument();

            doc.Load(reader);
            reader.Close();

            XmlNode flamesNode = null;

            foreach (XmlNode node in doc.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    flamesNode = node;
                }
            }

            if (flamesNode == null)
            {
                return(newFractals);
            }

            foreach (XmlNode node in flamesNode)
            {
                if (node.Name == "flame")
                {
                    newFractal = readFlameNode(node, conf);
                    if (newFractal != null)
                    {
                        newFractals.Add(newFractal);
                    }
                }
            }

            return(newFractals);
        }
        public static FractronConfig Load()
        {
            FileStream     fs   = null;
            FractronConfig conf = new FractronConfig();

            try{
                fs = File.Open(FractronConfigFileName, FileMode.Open, FileAccess.Read, FileShare.None);
            }
            catch (DirectoryNotFoundException) {
                return(conf);
            }
            catch (FileNotFoundException) {
                return(conf);
            }

            try{
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.CheckCharacters  = false;
                settings.CloseInput       = true;
                settings.ConformanceLevel = ConformanceLevel.Fragment;
                settings.IgnoreComments   = false;
                settings.IgnoreWhitespace = false;
                settings.ValidationType   = ValidationType.None;

                XmlReader reader = XmlReader.Create(fs, settings);

                XmlDocument doc = new XmlDocument();

                doc.Load(reader);

                conf.LoadFromXmlDoc(doc);
            }
            finally{
                if (fs != null)
                {
                    fs.Close();
                }
            }
            conf.Valid = false;
            return(conf);
        }
        private static Palette readPaletteNode(XmlNode node, FractronConfig conf)
        {
            Palette result = null;

            if (node.Attributes["src"] != null)
            {
                string fullFileName = Path.Combine(conf.PaletteDir, node.Attributes["src"].Value);
                try{
                    result = new Palette(fullFileName);
                }catch (Exception) {
                    result = null;
                }
            }
            else if (node.Attributes["format"] != null &&
                     node.Attributes["format"].Value == "RGB")
            {
                try{
                    result = read1DPaletteNode(node);
                }catch (Exception) {
                    result = null;
                }
            }
            return(result);
        }
        private static Fractal readFlameNode(XmlNode flameNode, FractronConfig conf)
        {
            Fractal newFractal = new Fractal();
            Palette apoPalette = null;
            float   xSize      = 800.0f;
            float   ySize      = 600.0f;
            float   xCenter    = 0.0f;
            float   yCenter    = 0.0f;
            float   scale      = 100.0f;
            float   zoom       = 1.0f;
            float   rotate     = 0.0f;

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

            newFractal.OriginalXml = flameNode;

            foreach (XmlAttribute attr in flameNode.Attributes)
            {
                if (attr.Name == "name")
                {
                    newFractal.Name = attr.Value;
                }
                else if (attr.Name == "version")
                {
                    newFractal.Version = attr.Value;
                }
                else if (attr.Name == "size")
                {
                    try{
                        string[] xyStr = attr.Value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                        xSize = float.Parse(xyStr[0]);
                        ySize = float.Parse(xyStr[1]);
                    }catch (Exception) {}
                }
                else if (attr.Name == "center")
                {
                    try{
                        string[] xyStr = attr.Value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                        xCenter = float.Parse(xyStr[0]);
                        yCenter = float.Parse(xyStr[1]);
                    }catch (Exception) {}
                }
                else if (attr.Name == "scale")
                {
                    readFloatAttr(attr, ref scale);
                }
                else if (attr.Name == "zoom")
                {
                    readFloatAttr(attr, ref zoom);
                }
                else if (attr.Name == "rotate")
                {
                    readFloatAttr(attr, ref rotate);
                }
                else if (attr.Name == "background")
                {
                    try{
                        string[] rgbStr = attr.Value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                        newFractal.BackgroundColor.X = float.Parse(rgbStr[0]);
                        newFractal.BackgroundColor.Y = float.Parse(rgbStr[1]);
                        newFractal.BackgroundColor.Z = float.Parse(rgbStr[2]);
                        newFractal.BackgroundColor.W = 1.0f;
                    }catch (Exception) {}
                }
                else if (attr.Name == "brightness")
                {
                    readFloatAttr(attr, ref newFractal.Brightness);
                }
                else if (attr.Name == "gamma")
                {
                    readFloatAttr(attr, ref newFractal.Gamma);
                }
                else if (attr.Name == "vibrancy")
                {
                    readFloatAttr(attr, ref newFractal.Vibrancy);
                }
            }

            newFractal.SetCameraFromFlame(xSize, ySize, xCenter, yCenter, scale, zoom, rotate);

            foreach (XmlNode node in flameNode.ChildNodes)
            {
                if (node.Name == "xform")
                {
                    newFractal.Branches.Add(readBranchNode(node));
                }
                else if (node.Name == "palette")
                {
                    apoPalette = readPaletteNode(node, conf);
                }
                else if (node.Name == "color")                //handle the old style palettes
                {
                    if (apoPalette == null)
                    {
                        apoPalette = new Palette(256, 1, Palette.PaletteType.OneDimensional);
                        for (int i = 0; i < 256; i++)
                        {
                            apoPalette.SetPixel(i, 0, Color.White);
                        }
                    }

                    try{
                        int      index  = int.Parse(node.Attributes["index"].Value);
                        string[] rgbStr = node.Attributes["rgb"].Value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                        Color    c      = Color.FromArgb(
                            int.Parse(rgbStr[0]),
                            int.Parse(rgbStr[1]),
                            int.Parse(rgbStr[2]));
                        apoPalette.SetPixel(index, 0, c);
                    }
                    catch (Exception) {}
                }
            }

            if (apoPalette != null)
            {
                newFractal.Palette = apoPalette;
            }

            return(newFractal);
        }
Exemple #7
0
        public static void ReadFromFlameFile(string filename, FractronConfig conf)
        {
            FractalList newFractals = FlameFileIO.ReadFlameFile(filename, conf);

            fractals = newFractals;
        }