Esempio n. 1
0
        public BeatmapSettings ReadBeatmapSettings(string beatmapSettingsFilename)
        {
            string file = beatmapSettingsFilename;

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("Beatmap file not found");
            }

            string str;

            using (StreamReader sr = new StreamReader(file))
                str = sr.ReadToEnd();

            LogHelper.Log($"BeatmapReader: Parsing Beatmap '{beatmapSettingsFilename}'");

            JObject obj = JObject.Parse(str);

            BeatmapSettings settings =
                JsonConvert.DeserializeObject <BeatmapSettings>(obj["Settings"].ToString());

            Console.WriteLine(settings.General.BackgroundFileName);

            return(settings);
        }
Esempio n. 2
0
        public HitObjectContainer ReadHitObjects(string beatmapName, BeatmapSettings beatmapSettings)
        {
            HitObjectContainer hitObjectContainer = new HitObjectContainer(beatmapSettings.Difficulty.KeyAmount);

            LogHelper.Log($"BeatmapReader: Reading Beatmap Hit Objects '{beatmapName}'");
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapName,
                                       ProcessorSettings.HitObjectsFolder, beatmapSettings.HitObjectsFilename + "_" + beatmapSettings.Metadata.Version);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("hitobjects file not found");
            }

            Console.WriteLine(path);

            // Reading HitObjects file, which contains all the objects used in the beatmap
            // HitObjects file uses this format: "Column Position" for a Click, and "Column Position EndPosition" for a Hold
            using (StreamReader sr = new StreamReader(path))
            {
                LogHelper.Log($"BeatmapReader: Found Beatmap Hit Objects file '{beatmapSettings.HitObjectsFilename}'");

                string   full  = sr.ReadToEnd();
                string[] lines = full.Split('\n');
                foreach (var line in lines)
                {
                    // If the line is empty or contains only whitespaces, skip it
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    // Split the line, remove all whitespaces
                    string[] tokens = Array.ConvertAll(line.Split(' '), p => p.Trim());

                    // If length is 3, the object is a 'Hold', else it's 'Click'
                    HitObject ho;
                    if (tokens.Length == 2)
                    {
                        ho = new NoteClick(int.Parse(tokens[0]), int.Parse(tokens[1]));
                    }
                    else if (tokens.Length == 3)
                    {
                        ho = new NoteHold(int.Parse(tokens[0]), int.Parse(tokens[1]), int.Parse(tokens[2]));
                    }
                    else
                    {
                        throw new Exception("Unknown note type");
                    }

                    hitObjectContainer.Add(ho);
                }
            }
            LogHelper.Log($"BeatmapReader: Successfully Read Beatmap Hit Objects. Total Hit Objects count: {hitObjectContainer.Count}");

            return(hitObjectContainer);
        }
Esempio n. 3
0
        private void ProcessDatabase(GraphicsDevice graphicsDevice)
        {
            // TODO: Encapsulate work with the data base
            var dbConnection = new SQLiteConnection($"Data Source={ProcessorSettings.DatabaseName};Version=3;");

            dbConnection.Open();

            var dbCommand = dbConnection.CreateCommand();

            dbCommand.CommandText = "SELECT * FROM beatmaps";

            var dbDataReader = dbCommand.ExecuteReader();

            BeatmapReader bmReader = new BeatmapReader(ProcessorSettings);

            // Read all entries while there is any
            while (dbDataReader.Read())
            {
                // There are 22 values (from 0 to 21)
                // 0-3 values are information about beatmap location
                string beatmapFolder   = dbDataReader.GetString(0);
                string beatmapFilename = dbDataReader.GetString(1);
                string beatmapName     = dbDataReader.GetString(2);
                string beatmapVersion  = dbDataReader.GetString(3);

                // 4-17 values are beatmap settings data
                var bmSettingsGeneral = new BeatmapSettingsGeneral(
                    dbDataReader.GetString(4),
                    dbDataReader.GetInt32(5),
                    dbDataReader.GetString(6));
                var bmSettingsEditor   = new BeatmapSettingsEditor(dbDataReader.GetInt32(7));
                var bmSettingsMetadata = new BeatmapSettingsMetadata(dbDataReader.GetString(8),
                                                                     dbDataReader.GetString(9), dbDataReader.GetString(10), dbDataReader.GetString(11),
                                                                     dbDataReader.GetString(12), dbDataReader.GetInt32(13), dbDataReader.GetInt32(14));
                var bmSettingsDifficutly = new BeatmapSettingsDifficulty(dbDataReader.GetFloat(15),
                                                                         dbDataReader.GetFloat(16), dbDataReader.GetInt32(17));
                var bmSettings = new BeatmapSettings(bmSettingsGeneral, bmSettingsEditor, bmSettingsMetadata, bmSettingsDifficutly);

                // 20th - hit object count, 21st - the first beats per minute value
                var hitObjectCount = dbDataReader.GetInt32(20);
                var bpm            = dbDataReader.GetDouble(21);

                var bg    = bmReader.LoadBeatmapBackgroundTexture(graphicsDevice, bmSettings, GetBeatmapNameWithoutVersion(beatmapName));
                var music = bmReader.LoadBeatmapMusicTrack(bmSettings, GetBeatmapNameWithoutVersion(beatmapName));

                var entry = new BeatmapProcessorContainerEntry(bmSettings,
                                                               bg, music, beatmapFolder, beatmapFilename, beatmapName,
                                                               beatmapVersion, hitObjectCount, bpm);

                _beatmapSettingsList.Add(entry);
            }

            dbConnection.Close();
        }
Esempio n. 4
0
        /// <summary>
        /// Write a beatmap to a file using JSON format
        /// </summary>
        public void WriteToFile(Beatmap bm, string beatmapName, string beatmapVersion)
        {
            // TODO: Encapsulate "Maps", maybe got to create a ContentMap class
            string file = Path.Combine(ProcessorSettings.BeatmapsFolder, beatmapName, beatmapName + " " + beatmapVersion + ProcessorSettings.BeatmapFileExtension);

            BeatmapSettings bms = bm.Settings;

            string str = JsonConvert.SerializeObject(bms, Formatting.Indented);

            if (!Directory.Exists(Path.Combine(ProcessorSettings.BeatmapsFolder, beatmapName)))
            {
                Directory.CreateDirectory(Path.Combine(ProcessorSettings.BeatmapsFolder, beatmapName));
            }

            using (StreamWriter sw = new StreamWriter(file))
                sw.WriteLine(str);

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapName,
                                       bms.TimingPointsFilename);

            // Write all the timing points
            using (StreamWriter sw = new StreamWriter(Path.Combine(ProcessorSettings.TimingPointsFolder, path + beatmapVersion)))
            {
                foreach (var timingPoint in bm.TimingPoints)
                {
                    sw.WriteLine(
                        $"{timingPoint.Position} {timingPoint.MsPerBeat} {timingPoint.TimeSignature} {timingPoint.HitSoundVolume} {timingPoint.IsResetMetronome}");
                }
            }
            path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapName,
                                bms.HitObjectsFilename);
            // Write all the hit objects
            using (StreamWriter sw = new StreamWriter(Path.Combine(ProcessorSettings.HitObjectsFolder, path + beatmapVersion)))
            {
                foreach (var hitObject in bm.HitObjects)
                {
                    if (hitObject is NoteClick)
                    {
                        var noteClick = hitObject as NoteClick;
                        sw.WriteLine($"{noteClick.Column} {noteClick.Position}");
                    }
                    else if (hitObject is NoteHold)
                    {
                        var noteHold = hitObject as NoteHold;
                        sw.WriteLine($"{noteHold.Column} {noteHold.Position} {noteHold.EndPosition}");
                    }
                    else
                    {
                        throw new Exception("Unknown note type");
                    }
                }
            }
        }
Esempio n. 5
0
 public Beatmap(
     BeatmapSettings settings,
     Texture2D backgroundTexture,
     Music music,
     TimingPointContainer timingPoints,
     HitObjectContainer hitObjects)
 {
     Settings          = settings;
     BackgroundTexture = backgroundTexture;
     Music             = music;
     TimingPoints      = timingPoints;
     HitObjects        = hitObjects;
 }
Esempio n. 6
0
        public Texture2D LoadBeatmapBackgroundTexture(GraphicsDevice graphicsDevice, BeatmapSettings settings, string beatmapName)
        {
            Texture2D background;

            Console.WriteLine($"Loading BG: {settings.General.BackgroundFileName}, ID: {settings.Metadata.BeatmapId}");
            using (FileStream fs =
                       new FileStream(
                           Path.Combine(ProcessorSettings.BeatmapsFolder, beatmapName, settings.General.BackgroundFileName),
                           FileMode.Open))
            {
                background = Texture2D.FromStream(graphicsDevice, fs);
            }

            return(background);
        }
Esempio n. 7
0
        public TimingPointContainer ReadTimingPoints(string beatmapName, BeatmapSettings beatmapSettings)
        {
            TimingPointContainer timingPointContainer = new TimingPointContainer();

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapName,
                                       ProcessorSettings.TimingPointsFolder, beatmapSettings.TimingPointsFilename + "_" + beatmapSettings.Metadata.Version);

            Console.WriteLine(path);

            LogHelper.Log($"BeatmapReader: Reading Beatmap Timing Points '{beatmapName}'");

            // Reading TimingPoints file, which contains all the timing points used in the beatmap
            using (StreamReader sr = new StreamReader(path))
            {
                LogHelper.Log($"BeatmapReader: Found Beatmap Timing Points file '{beatmapSettings.TimingPointsFilename}'");
                string   full  = sr.ReadToEnd();
                string[] lines = full.Split('\n');
                foreach (var line in lines)
                {
                    // If the line is empty or contains only whitespaces, skip it
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    // Split the line, remove all whitespaces
                    string[] tokens = Array.ConvertAll(line.Split(' '), p => p.Trim());

                    // First is TP position
                    // Second is Ms per beat
                    // Third is time signature (n/4, where n is the number)
                    // Fourth is hitsound volume
                    // Fifth determines if metronome must be reset on the start of the timing point
                    TimingPoint tp = new TimingPoint(
                        int.Parse(tokens[0]),
                        double.Parse(tokens[1]),
                        int.Parse(tokens[2]),
                        int.Parse(tokens[3]),
                        bool.Parse(tokens[4]));

                    timingPointContainer.Add(tp);
                }
            }
            LogHelper.Log($"BeatmapReader: Successfully Read Beatmap Timing Points. Total Timing Point count: {timingPointContainer.Count}");

            return(timingPointContainer);
        }
 public BeatmapProcessorContainerEntry(
     BeatmapSettings beatmapSettings,
     Texture2D backgroundTexture,
     Music music,
     string beatmapFolder,
     string beatmapFilename,
     string beatmapName,
     string beatmapVersion,
     int hitObjectCount,
     double beatsPerMinute)
 {
     BeatmapSettings   = beatmapSettings;
     BackgroundTexture = backgroundTexture;
     Music             = music;
     BeatmapFilename   = beatmapFilename;
     BeatmapFolder     = beatmapFolder;
     BeatmapName       = beatmapName;
     BeatmapVersion    = beatmapVersion;
     HitObjectCount    = hitObjectCount;
     BeatsPerMinute    = beatsPerMinute;
 }
Esempio n. 9
0
        /// <summary>
        /// Load Beatmap from a JSON file with file path and filename specified
        /// </summary>
        /// <returns>Beatmap</returns>
        public Beatmap ReadBeatmap(GraphicsDevice graphicsDevice, string beatmapName, string versionName)
        {
            LogHelper.Log($"BeatmapReader: Load Beatmap from file '{beatmapName}'");

            var absPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder,
                                       beatmapName, beatmapName + " [" + versionName + "]" + ProcessorSettings.BeatmapFileExtension);

            BeatmapSettings settings = ReadBeatmapSettings(absPath);

            if (string.IsNullOrEmpty(settings.HitObjectsFilename))
            {
                throw new FileLoadException("HitObjectsFilename is null");
            }
            if (string.IsNullOrEmpty(settings.TimingPointsFilename))
            {
                throw new FileLoadException("TimingPointsFilename is null");
            }
            //if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapSettingsFilename, settings.HitObjectsFilename)))
            //    throw new FileNotFoundException("HitObjects file not found");
            //if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProcessorSettings.BeatmapsFolder, beatmapSettingsFilename, settings.TimingPointsFilename)))
            //    throw new FileNotFoundException("TimingPoints file not found");

            LogHelper.Log("BeatmapReader: Sucessfully Read Beatmap Settings");

            var timingPoints = ReadTimingPoints(beatmapName, settings);
            var hitObjects   = ReadHitObjects(beatmapName, settings);


            LogHelper.Log($"BeatmapReader: Successfully Read Beatmap '{beatmapName}'");

            Beatmap bm = new Beatmap(settings,
                                     LoadBeatmapBackgroundTexture(graphicsDevice, settings, beatmapName),
                                     LoadBeatmapMusicTrack(settings, beatmapName),
                                     timingPoints, hitObjects);

            return(bm);
        }
Esempio n. 10
0
 public Music LoadBeatmapMusicTrack(BeatmapSettings settings, string beatmapName)
 {
     return(new Music(Path.Combine(ProcessorSettings.BeatmapsFolder, beatmapName, settings.General.AudioFileName)));
 }