Esempio n. 1
0
        public static BooruPostList DoSearch(string Pattern, ServerBooru Booru)
        {
            string[] parts = SplitString(Pattern);
            if (parts.Length < 1)
            {
                using (DataTable postTable = Booru.DB.ExecuteTable(SQLStatements.GetPosts))
                    return(BooruPostList.FromTable(postTable));
            }

            //Get all posts
            //Perform all the special patterns
            //return the post ids

            List <string> tagSearchQueries = new List <string>();
            var           specialPatterns  = new List <SpecialPattern>();

            //Extract all the needed information
            for (int i = 0; i < parts.Length; i++)
            {
                string part   = parts[i];
                bool   negate = ExtractNegate(ref part);

                if (!IsSpecialPattern(part))
                {
                    DataRow  tagRow = Booru.DB.ExecuteRow(SQLStatements.GetTagByTagString, part);
                    BooruTag tag    = BooruTag.FromRow(tagRow);
                    if (tag != null)
                    {
                        tagSearchQueries.Add(string.Format("id {0} (SELECT post FROM post_tags WHERE tag = {1})", negate ? "NOT IN" : "IN", tag.ID));
                    }
                }
                else
                {
                    SpecialPattern sPattern = ExtractSpecialPattern(part);
                    sPattern.Negate = negate;
                    specialPatterns.Add(sPattern);
                }
            }

            string tagSearchQuery = tagSearchQueries.Count > 0 ?
                                    "SELECT * FROM posts WHERE " + string.Join(" AND ", tagSearchQueries) + " ORDER BY creationdate DESC"
                : SQLStatements.GetPosts;

            using (DataTable postTable = Booru.DB.ExecuteTable(tagSearchQuery))
            {
                BooruPostList postList = new BooruPostList();
                foreach (BooruPost post in BooruPostList.FromTable(postTable))
                {
                    if (DoSpecialPatternChecks(specialPatterns, post))
                    {
                        postList.Add(post);
                    }
                }
                return(postList);
            }
        }
        public SourceScore ReadSourceScore(Stream stream, string fileName, ReadSourceOptions sourceOptions)
        {
            var lines = ReadLines(stream);

            var c = 0;

            var offsetTimeSpan = TimeSpan.Parse(lines[c]);

            ++c;

            var leadTimeSpan = TimeSpan.Parse(lines[c]);

            ++c;

            var trackCount = Convert.ToInt32(lines[c]);

            ++c;

            var beatsPerMeasure = Convert.ToInt32(lines[c]);

            ++c;

            // splitter
            ++c;

            var notes = new List <SourceNote>();

            while (!lines[c].StartsWith("---"))
            {
                var line = lines[c];

                SourceNote[] sourceNotes;
                if (TapPattern.IsMatch(line))
                {
                    var tap = Tap.FromString(line);

                    var sourceNote = new SourceNote {
                        Type = NoteType.Tap
                    };
                    FillHeader(sourceNote, tap.Header);
                    sourceNote.Size = StrToSize(tap.Body.Size);

                    sourceNotes = new[] { sourceNote };
                }
                else if (HoldPairPattern.IsMatch(line))
                {
                    var holds = Hold.CreateHolds(line);

                    var sourceNote = new SourceNote {
                        Type = NoteType.Hold
                    };
                    FillHeader(sourceNote, holds[0].Header);
                    sourceNote.Size = StrToSize(holds[0].Body.Size);

                    var holdEnd = new SourceNote {
                        Type = NoteType.Hold
                    };
                    FillHeader(holdEnd, holds[1].Header);
                    holdEnd.Size           = sourceNote.Size;
                    holdEnd.FlickDirection = StrToDirection(holds[1].Body.Direction);

                    sourceNote.FollowingNotes = new[] { holdEnd };

                    sourceNotes = new[] { sourceNote };
                }
                else if (FlickPattern.IsMatch(line))
                {
                    var flick = Flick.FromString(line);

                    var sourceNote = new SourceNote {
                        Type = NoteType.Flick
                    };
                    FillHeader(sourceNote, flick.Header);
                    sourceNote.Size           = StrToSize(flick.Body.Size);
                    sourceNote.FlickDirection = StrToDirection(flick.Body.Direction);

                    sourceNotes = new[] { sourceNote };
                }
                else if (SlideSeriesPattern.IsMatch(line))
                {
                    var slides = Slide.CreateSlides(line);

                    var sourceNote = new SourceNote {
                        Type = NoteType.Slide
                    };
                    FillHeader(sourceNote, slides[0].Header);

                    var following = new List <SourceNote>();
                    for (var i = 1; i < slides.Count; ++i)
                    {
                        var nodeInSeries = new SourceNote {
                            Type = NoteType.Slide
                        };
                        FillHeader(nodeInSeries, slides[i].Header);

                        if (i == slides.Count - 1)
                        {
                            nodeInSeries.FlickDirection = StrToDirection(slides[i].Body.Direction);
                        }

                        following.Add(nodeInSeries);
                    }

                    sourceNote.FollowingNotes = following.ToArray();

                    sourceNotes = new[] { sourceNote };
                }
                else if (SpecialPattern.IsMatch(line))
                {
                    var special = Special.FromString(line);

                    var sourceNote = new SourceNote {
                        Type = NoteType.Special
                    };
                    FillHeader(sourceNote, special.Header);

                    sourceNotes = new[] { sourceNote };
                }
                else
                {
                    throw new FormatException("Error in simple format.");
                }

                notes.AddRange(sourceNotes);

                // next line
                ++c;
            }

            // Sort the added notes.
            notes.Sort((n1, n2) => n1.Ticks.CompareTo(n2.Ticks));

            // splitter
            ++c;

            var conductors = new List <Conductor>();

            for (; c < lines.Count; ++c)
            {
                var ss           = lines[c].Split(':');
                var measureIndex = Convert.ToInt32(ss[0]);
                var bpm          = Convert.ToDouble(ss[1]);
                var conductor    = new Conductor {
                    Measure              = measureIndex - 1,
                    Tempo                = bpm,
                    Ticks                = (measureIndex - 1) * beatsPerMeasure * NoteBase.TicksPerBeat,
                    SignatureNumerator   = beatsPerMeasure,
                    SignatureDenominator = beatsPerMeasure
                };
                conductors.Add(conductor);
            }

            conductors.Sort((n1, n2) => n1.Ticks.CompareTo(n2.Ticks));

            var score = new SourceScore();

            score.Conductors  = conductors.ToArray();
            score.Notes       = notes.ToArray();
            score.TrackCount  = trackCount;
            score.MusicOffset = offsetTimeSpan.TotalSeconds;
            return(score);

            void FillHeader(SourceNote note, NoteHeader header)
            {
                var fraction = (float)(header.Nominator - 1) / header.Denominator;

                note.Beat       = (int)(beatsPerMeasure * fraction);
                note.StartX     = header.Start - 1;
                note.EndX       = header.End - 1;
                note.Speed      = header.Speed;
                note.LeadTime   = leadTimeSpan.TotalSeconds;
                note.Measure    = header.Measure - 1;
                note.Ticks      = 60 * (long)(beatsPerMeasure * ((header.Measure - 1) + fraction) * NoteBase.TicksPerBeat);
                note.TrackIndex = (int)note.StartX;

                if (note.TrackIndex < 0 || note.TrackIndex >= trackCount)
                {
                    Debug.Print("Warning: Invalid track index \"{0}\", changing into range [0, {1}].", note.TrackIndex, trackCount - 1);

                    if (note.TrackIndex < 0)
                    {
                        note.TrackIndex = 0;
                    }
                    else if (note.TrackIndex >= trackCount)
                    {
                        note.TrackIndex = trackCount - 1;
                    }
                }
            }

            NoteSize StrToSize(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return(NoteSize.Small);
                }
                else
                {
                    switch (str)
                    {
                    case "small":
                        return(NoteSize.Small);

                    case "large":
                        return(NoteSize.Large);

                    default:
                        throw new ArgumentOutOfRangeException(nameof(str), str, null);
                    }
                }
            }

            FlickDirection StrToDirection(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return(FlickDirection.None);
                }
                else
                {
                    switch (str)
                    {
                    case "left":
                        return(FlickDirection.Left);

                    case "right":
                        return(FlickDirection.Right);

                    case "up":
                        return(FlickDirection.Up);

                    default:
                        throw new ArgumentOutOfRangeException(nameof(str), str, null);
                    }
                }
            }
        }