コード例 #1
0
        void LoadElement(XmlElement node, IReadOnlyFileSystem fileSystem)
        {
            switch (node.Name)
            {
            case "Options":
                ProjectTemplateImpl.WarnObsoleteNode(node, "Options are no longer supported, use properties instead.");
                break;

            case "CreateActions":
                LoadCreateActions(node);
                break;

            case "PreCreateActions":
                LoadPreCreateActions(node);
                break;

            case "ProjectItems":
                LoadProjectItems(node);
                break;

            case "Files":
                LoadFiles(node, fileSystem);
                break;

            case "Imports":
                LoadImports(node);
                break;

            case "PropertyGroup":
                LoadPropertyGroup(node);
                break;

            case "Include":
                TemplateLoadException.AssertAttributeExists(node, "src");
                FileName includeFileName = FileName.Create(node.GetAttribute("src"));
                try {
                    XmlDocument doc = new XmlDocument();
                    using (var stream = fileSystem.OpenRead(includeFileName)) {
                        doc.Load(stream);
                    }
                    doc.DocumentElement.SetAttribute("fileName", includeFileName);
                    var fileSystemForInclude = new ReadOnlyChrootFileSystem(fileSystem, includeFileName.GetParentDirectory());
                    if (doc.DocumentElement.Name == "Include")
                    {
                        LoadElementChildren(doc.DocumentElement, fileSystemForInclude);
                    }
                    else
                    {
                        LoadElement(doc.DocumentElement, fileSystemForInclude);
                    }
                } catch (XmlException ex) {
                    throw new TemplateLoadException("Error loading include file " + includeFileName, ex);
                }
                break;

            default:
                throw new TemplateLoadException("Unknown node in <Project>: " + node.Name);
            }
        }
コード例 #2
0
 /// <summary>
 ///     Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
 /// </summary>
 /// <param name="fs">The filesystem.</param>
 /// <param name="path">The path of the file to open for reading.</param>
 /// <returns>A string containing all lines of the file.</returns>
 /// <remarks>
 ///     This method attempts to automatically detect the encoding of a file based on the presence of byte order marks.
 ///     Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.
 /// </remarks>
 public static string ReadAllText(this IReadOnlyFileSystem fs, UPath path)
 {
     var stream = fs.OpenRead(path);
     {
         using (var reader = new StreamReader(stream))
         {
             return(reader.ReadToEnd());
         }
     }
 }
コード例 #3
0
        /// <summary>
        ///     Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        /// <param name="fs">The filesystem.</param>
        /// <param name="path">The path of the file to open for reading.</param>
        /// <returns>A byte array containing the contents of the file.</returns>
        public static byte[] ReadAllBytes(this IReadOnlyFileSystem fs, UPath path)
        {
            var memstream = new MemoryStream();

            using (var stream = fs.OpenRead(path))
            {
                stream.CopyTo(memstream);
            }
            return(memstream.ToArray());
        }
コード例 #4
0
        public bool TryLoad(IReadOnlyFileSystem fileSystem, string path, out ISkybox skybox, out string[] imageFileRelativePaths, out ErrorInfo error)
        {
            dynamic mainFile;

            using (var reader = trwFactory.JsonReader(fileSystem.OpenRead(path)))
                mainFile = reader.ReadAsDynamic();
            imageFileRelativePaths = new string[]
            {
                mainFile.Right,
                mainFile.Left,
                mainFile.Top,
                mainFile.Bottom,
                mainFile.Back,
                mainFile.Front
            };
            var folderPath = Path.Combine(Path.GetDirectoryName(path));
            var images     = new IImage[6];

            for (var i = 0; i < 6; i++)
            {
                var relPath = imageFileRelativePaths[i];
                using (var stream = fileSystem.OpenRead(Path.Combine(folderPath, relPath)))
                    if (!imageLoader.TryLoad(stream, out images[i], out error))
                    {
                        skybox = null;
                        return(false);
                    }
            }

            var width = images[0].Size.Width;

            if (images.Any(x => x.Size != new IntSize2(width, width)))
            {
                error  = new ErrorInfo("Skybox images are not of equal size");
                skybox = null;
                return(false);
            }
            skybox = new Skybox(ResourceVolatility.Immutable, width, images);
            return(true);
        }
コード例 #5
0
 /// <summary>
 ///     Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
 /// </summary>
 /// <param name="fs">The filesystem.</param>
 /// <param name="path">The path of the file to open for reading.</param>
 /// <param name="encoding">The encoding to use to decode the text from <paramref name="path" />.</param>
 /// <returns>A string containing all lines of the file.</returns>
 public static string ReadAllText(this IReadOnlyFileSystem fs, UPath path, Encoding encoding)
 {
     if (encoding == null)
     {
         throw new ArgumentNullException(nameof(encoding));
     }
     var stream = fs.OpenRead(path);
     {
         using (var reader = new StreamReader(stream, encoding))
         {
             return(reader.ReadToEnd());
         }
     }
 }
コード例 #6
0
 /// <summary>
 ///     Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
 /// </summary>
 /// <param name="fs">The filesystem.</param>
 /// <param name="path">The path of the file to open for reading.</param>
 /// <returns>An array of strings containing all lines of the file.</returns>
 public static string[] ReadAllLines(this IReadOnlyFileSystem fs, UPath path)
 {
     var stream = fs.OpenRead(path);
     {
         using (var reader = new StreamReader(stream))
         {
             var    lines = new List <string>();
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 lines.Add(line);
             }
             return(lines.ToArray());
         }
     }
 }
コード例 #7
0
 /// <summary>
 ///     Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
 /// </summary>
 /// <param name="fs">The filesystem.</param>
 /// <param name="path">The path of the file to open for reading.</param>
 /// <param name="encoding">The encoding to use to decode the text from <paramref name="path" />.</param>
 /// <remarks>
 ///     This method attempts to automatically detect the encoding of a file based on the presence of byte order marks.
 ///     Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.
 /// </remarks>
 /// <returns>An array of strings containing all lines of the file.</returns>
 public static string[] ReadAllLines(this IReadOnlyFileSystem fs, UPath path, Encoding encoding)
 {
     if (encoding == null)
     {
         throw new ArgumentNullException(nameof(encoding));
     }
     var stream = fs.OpenRead(path);
     {
         using (var reader = new StreamReader(stream, encoding))
         {
             var    lines = new List <string>();
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 lines.Add(line);
             }
             return(lines.ToArray());
         }
     }
 }
コード例 #8
0
		void LoadElement(XmlElement node, IReadOnlyFileSystem fileSystem)
		{
			switch (node.Name) {
				case "Options":
					ProjectTemplateImpl.WarnObsoleteNode(node, "Options are no longer supported, use properties instead.");
					break;
				case "CreateActions":
					LoadCreateActions(node);
					break;
				case "PreCreateActions":
					LoadPreCreateActions(node);
					break;
				case "ProjectItems":
					LoadProjectItems(node);
					break;
				case "Files":
					LoadFiles(node, fileSystem);
					break;
				case "Imports":
					LoadImports(node);
					break;
				case "PropertyGroup":
					LoadPropertyGroup(node);
					break;
				case "Include":
					TemplateLoadException.AssertAttributeExists(node, "src");
					FileName includeFileName = FileName.Create(node.GetAttribute("src"));
					try {
						XmlDocument doc = new XmlDocument();
						using (var stream = fileSystem.OpenRead(includeFileName)) {
							doc.Load(stream);
						}
						doc.DocumentElement.SetAttribute("fileName", includeFileName);
						var fileSystemForInclude = new ReadOnlyChrootFileSystem(fileSystem, includeFileName.GetParentDirectory());
						if (doc.DocumentElement.Name == "Include") {
							LoadElementChildren(doc.DocumentElement, fileSystemForInclude);
						} else {
							LoadElement(doc.DocumentElement, fileSystemForInclude);
						}
					} catch (XmlException ex) {
						throw new TemplateLoadException("Error loading include file " + includeFileName, ex);
					}
					break;
				default:
					throw new TemplateLoadException("Unknown node in <Project>: " + node.Name);
			}
		}
コード例 #9
0
 public Stream OpenRead(FileName fileName)
 {
     return(fileSystem.OpenRead(basePath.Combine(fileName)));
 }