Ejemplo n.º 1
0
        /// <summary>
        /// Serializes object to isolated storage file.
        /// </summary>
        /// <param name="fileName">file name.</param>
        /// <param name="obj">Object to serialize.</param>
        public static void Serialize(string subDirectory, string fileName, Object obj)
        {
            // Open isolated storage.
            using (var storageManager = new IsolatedStorageManager())
            {
                fileName = Prepare(storageManager, subDirectory, fileName);

                // Open file from storage.
                using (Stream stream = storageManager.OpenFile(fileName, IO.OpenFileMode.Create))
                {
                    XFile file = XFile.Create(fileName);

                    XNode fileNode = file;

                    var node = new XNode(file, "Serializator");
                    ((XNode)file).Nodes.Add(node);

                    // Create serializer for type.
                    var serializer = new XSerializer(node);
                    serializer.Serialize("Data", obj);

                    file.WriteBinary(stream);
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                XFile file = null;
                using (Stream stream = new FileStream(args[0], FileMode.Open))
                {
                    file = XFile.Create(stream, args[0]);
                }

                Parse(file);
            }
        }
Ejemplo n.º 3
0
        public void Register(string path)
        {
            using (Stream stream = File.Open(path, FileMode.Open))
            {
                ZipFile  zf    = new ZipFile(stream);
                ZipEntry entry = zf.GetEntry("Definition.xml");

                if (entry != null)
                {
                    using (Stream zipStream = zf.GetInputStream(entry))
                    {
                        XNode node = XFile.Create(zipStream, entry.Name);

                        if (node.Tag != "Template")
                        {
                            return;
                        }

                        foreach (var cn in node.Nodes)
                        {
                            if (cn.Tag == "Properties")
                            {
                                string name = cn.Attribute("Name");
                                string guid = cn.Attribute("Guid");

                                string shortPath = path;

                                int maxLength = 34;

                                if (shortPath.Length > maxLength)
                                {
                                    string fname = Path.GetFileName(shortPath);
                                    string dir   = Path.GetDirectoryName(shortPath);

                                    int len = maxLength - fname.Length - 4;

                                    if (len > 0)
                                    {
                                        shortPath = dir.Substring(0, len) + "/../" + fname;
                                    }
                                    else
                                    {
                                        shortPath = "../" + fname.Substring(fname.Length - (maxLength - 3));
                                    }
                                }

                                InvalidateTemplate(path);

                                Templates.Add(new Template()
                                {
                                    Name      = name,
                                    Path      = path,
                                    Guid      = guid,
                                    ShortPath = shortPath.Replace('\\', '/')
                                });
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Load(Stream stream)
        {
            foreach (var tileset in _tilesets)
            {
                tileset.Item2.Dispose();
            }
            _tilesets.Clear();
            _layers.Clear();

            byte[] buffer = new byte[4096];     // 4K is optimum


            ZipFile zf = new ZipFile(stream);

            foreach (ZipEntry zipEntry in zf)
            {
                if (!zipEntry.IsFile)
                {
                    continue;           // Ignore directories
                }

                String entryFileName = zipEntry.Name;
                // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
                // Optionally match entrynames against a selection list here to skip as desired.
                // The unpacked length is available in the zipEntry.Size property.


                Stream zipStream = zf.GetInputStream(zipEntry);

                MemoryStream fileStream = new MemoryStream();
                StreamUtils.Copy(zipStream, fileStream, buffer);
                zipStream.Close();

                fileStream = new MemoryStream(fileStream.GetBuffer());

                string dir  = Path.GetDirectoryName(entryFileName);
                string file = Path.GetFileNameWithoutExtension(entryFileName);
                string ext  = Path.GetExtension(entryFileName).ToLowerInvariant();

                if (entryFileName == "Definition.xml")
                {
                    ParseDefinition(XFile.Create(fileStream, entryFileName));
                    continue;
                }

                switch (dir)
                {
                case "Tilesets":
                {
                    if (ext == ".png")
                    {
                        Texture2D texture = Texture2D.FromStream(AppMain.Current.GraphicsDevice, fileStream);
                        _tilesets.Add(new Tuple <string, Texture2D>(file, texture));
                    }
                    break;
                }
                }

                fileStream.Close();
            }
        }