Exemplo n.º 1
0
        public static bool IsLineValid(string line, FileSections currentSection)
        {
            switch (currentSection)
            {
            case FileSections.Format:
                return(line.ToLower().Contains("osu file format v"));

            case FileSections.General:
            case FileSections.Editor:
            case FileSections.Metadata:
            case FileSections.Difficulty:
            case FileSections.Fonts:
            case FileSections.Mania:
                return(line.Contains(":"));

            case FileSections.Events:
            case FileSections.TimingPoints:
            case FileSections.HitObjects:
                return(line.Contains(","));

            case FileSections.Colours:
            case FileSections.CatchTheBeat:
                return(line.Contains(',') && line.Contains(':'));

            default: return(false);
            }
        }
        /// <summary>
        /// Parses .osu file.
        /// </summary>
        /// <param name="lines">Array of text lines containing beatmap data.</param>
        /// <returns>A usable beatmap.</returns>
        public static Beatmap Decode(IEnumerable <string> lines)
        {
            Beatmap        = new Beatmap();
            currentSection = FileSections.Format;
            sbLines.Clear();

            foreach (var line in lines)
            {
                if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//"))
                {
                    if (ParseHelper.GetCurrentSection(line) != FileSections.None)
                    {
                        currentSection = ParseHelper.GetCurrentSection(line);
                    }
                    else if (ParseHelper.IsLineValid(line, currentSection))
                    {
                        ParseLine(line);
                    }
                }
            }

            Beatmap.EventsSection.Storyboard = StoryboardDecoder.Decode(sbLines.ToArray());

            Beatmap.GeneralSection.CirclesCount  = Beatmap.HitObjects.Count(c => c is HitCircle || c is TaikoHit || c is ManiaNote || c is CatchFruit);
            Beatmap.GeneralSection.SlidersCount  = Beatmap.HitObjects.Count(c => c is Slider || c is TaikoDrumroll || c is ManiaHoldNote || c is CatchJuiceStream);
            Beatmap.GeneralSection.SpinnersCount = Beatmap.HitObjects.Count(c => c is Spinner || c is TaikoSpinner || c is CatchBananaRain);

            Beatmap.GeneralSection.Length = Beatmap.HitObjects.Any() ? Beatmap.HitObjects.Last().EndTime : 0;

            return(Beatmap);
        }
Exemplo n.º 3
0
        public static FileSections GetCurrentSection(string line)
        {
            FileSections parsedSection = FileSections.None;

            Enum.TryParse(line.Trim(new char[] { '[', ']' }), true, out parsedSection);
            return(parsedSection);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parses skin.ini file.
        /// </summary>
        /// <param name="lines">Array of text lines containing skin.ini data.</param>
        /// <returns>A usable skin.</returns>
        public static Skin Decode(IEnumerable <string> lines)
        {
            skin           = new Skin();
            currentSection = FileSections.General;

            foreach (var line in lines)
            {
                string currentLine = line;

                if (currentLine.Contains("//"))
                {
                    currentLine = currentLine.Remove(currentLine.IndexOf("//"), currentLine.Length - currentLine.IndexOf("//"));
                }

                if (!string.IsNullOrWhiteSpace(currentLine))
                {
                    if (ParseHelper.GetCurrentSection(currentLine) != FileSections.None)
                    {
                        currentSection = ParseHelper.GetCurrentSection(currentLine);
                    }
                    else if (ParseHelper.IsLineValid(currentLine, currentSection))
                    {
                        ParseLine(currentLine);
                    }
                }
            }

            return(skin);
        }
Exemplo n.º 5
0
        public async Task <FileOperationStatus> AddFileAsync(string sectionName, string filename, Stream content)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(FileOperationStatus.BadParameters);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(FileOperationStatus.NotFound);
            }
            if (content == null || !content.CanRead)
            {
                return(FileOperationStatus.BadParameters);
            }

            if (filename.Contains("_CHANGED_"))
            {
                return(FileOperationStatus.BadParameters);
            }
            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            if (File.Exists(filePath))
            {
                return(FileOperationStatus.Conflict);
            }

            try
            {
                using (var file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    await content.CopyToAsync(file).ConfigureAwait(false);

                    await file.FlushAsync();
                }
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }

            if (FilesPreprocessingEnabled)
            {
                return(FilePreprocessController.EnqueueFile(filePath)
                    ? FileOperationStatus.JobQueued
                    : FileOperationStatus.Ok);
            }
            return(FileOperationStatus.Ok);
        }
Exemplo n.º 6
0
        public Stream GetFileStream(string sectionName, string filename)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(null);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(null);
            }

            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            return(File.Exists(filePath) ? new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) : null);
        }
Exemplo n.º 7
0
        public bool FileExists(string sectionName, string filename)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(false);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(false);
            }

            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            return(File.Exists(filePath));
        }
Exemplo n.º 8
0
 private FileSection ExtractFileSection(string localPath)
 {
     if (localPath == null)
     {
         return(null);
     }
     try
     {
         var sectionName    = localPath.Trim('/');
         var slashIndex     = sectionName.IndexOf("/", StringComparison.Ordinal);
         var lastSlashIndex = sectionName.LastIndexOf("/", StringComparison.Ordinal);
         sectionName = sectionName.Substring(slashIndex + 1, lastSlashIndex - slashIndex - 1);
         LogDebug($"Section requested: {sectionName}");
         return(FileSections.FirstOrDefault(s => s.Folder == sectionName));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Exemplo n.º 9
0
        public FileOperationStatus DeleteFile(string sectionName, string filename)
        {
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(FileOperationStatus.NotFound);
            }

            if (filename.Contains("_CHANGED_"))
            {
                return(FileOperationStatus.BadParameters);
            }
            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            if (!File.Exists(filePath))
            {
                LogDebug($"File {filePath} does not exist and cannot be deleted");
                return(FileOperationStatus.NotFound);
            }

            var compressed = IoHelper.LoadAllChanged(filePath);

            try
            {
                File.Delete(filePath);
                compressed.ForEach(File.Delete);
                return(FileOperationStatus.Ok);
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }
        }
 public FileStreamHelperModel(IEnumerable <FileMultipartSection> fileSections)
 {
     FileSections = fileSections ?? throw new ArgumentNullException(nameof(fileSections));
     FileCount    = FileSections.Count();
 }