Пример #1
0
        /// <summary>
        /// Loads the block from a file of virtual file system.
        /// </summary>
        /// <param name="path">The virtual file path.</param>
        /// <param name="errorString">The information on an error.</param>
        /// <returns><see cref="NeoAxis.DataBlock"/> if the block has been loaded; otherwise, <b>null</b>.</returns>
        public static DataBlock LoadFromVirtualFile(string path, out string errorString)
        {
            errorString = null;

            if (!VirtualFile.Exists(path))
            {
                errorString = string.Format("File not found \"{0}\".", path);
                return(null);
            }

            try
            {
                byte[] data = VirtualFile.ReadAllBytes(path);

                string    error;
                DataBlock dataBlock = DataBlock.Parse(data, false, out error);
                if (dataBlock == null)
                {
                    errorString = string.Format("Parsing data block failed \"{0}\" ({1}).",
                                                path, error);
                }

                return(dataBlock);
            }
            catch (Exception)
            {
                errorString = string.Format("Reading file failed \"{0}\".", path);
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Loads the block from a file of virtual file system.
        /// </summary>
        /// <param name="path">The virtual file path.</param>
        /// <param name="errorString">The information on an error.</param>
        /// <param name="fileNotFound"><b>true</b> if file not found.</param>
        /// <returns><see cref="NeoAxis.TextBlock"/> if the block has been loaded; otherwise, <b>null</b>.</returns>
        public static TextBlock LoadFromVirtualFile(string path, out string errorString, out bool fileNotFound)
        {
            errorString  = null;
            fileNotFound = false;

            //!!!!имя файла в ошибке снаружи метода

            try
            {
                var bytes  = VirtualFile.ReadAllBytes(path);
                var stream = new MemoryVirtualFileStream(bytes);

                //using( Stream stream = VirtualFile.Open( path ) )
                //{
                using (StreamReader streamReader = new StreamReader(stream))
                {
                    string    error;
                    TextBlock textBlock = TextBlock.Parse(streamReader.ReadToEnd(), out error);
                    if (textBlock == null)
                    {
                        errorString = $"Unable to load file \'{path}\'. " + error;
                        return(null);
                    }
                    return(textBlock);
                }
                //}
            }
            catch (FileNotFoundException)
            {
                errorString  = $"Unable to load file \'{path}\'. File not found.";
                fileNotFound = true;
                return(null);
            }
            catch (Exception e)
            {
                errorString = $"Unable to load file \'{path}\'. " + e.Message;
                return(null);
            }
        }
Пример #3
0
        static bool GetInfoFromSourceFile(string sourceFileName, out string hash, out int sourceFileSize)
        {
            try
            {
                var data = VirtualFile.ReadAllBytes(sourceFileName);
                //!!!!optimization: faster method maybe
                //!!!!optimization: может один раз создавать для всего?
                using (var sha = new SHA256Managed())
                {
                    byte[] checksum = sha.ComputeHash(data);
                    hash = BitConverter.ToString(checksum).Replace("-", string.Empty);
                }

                sourceFileSize = (int)VirtualFile.GetLength(sourceFileName);
                return(true);
            }
            catch
            {
                hash           = "";
                sourceFileSize = 0;
                return(false);
            }
        }