protected ParserBase(string simfilePath) { _simfilePath = simfilePath; _parentFolderPath = FileLoader.GetParentFolder(simfilePath); simfile = new Simfile(); }
public Notechart(Simfile simfile) { Simfile = simfile; simfile.AddNotechart(this); }
private void bW_SubmitScores_DoWork(object sender, DoWorkEventArgs e) { bW_SubmitScores.ReportProgress(1); // Downloading score manifest... string manifestResponseString = GetResponseAsString("api/manifest"); List<string> guids = JsonConvert.DeserializeObject<List<string>>(manifestResponseString); bW_SubmitScores.ReportProgress(2, guids.Count); // Found n scores. bW_SubmitScores.ReportProgress(3); // Downloading simfile catalog... WebClient webClient = new WebClient(); webClient.DownloadFile(BuildURI("catalog.json.zip"), "catalog.json.zip"); bW_SubmitScores.ReportProgress(4); // Decompressing catalog... using (var unzip = new Internals.Unzip("catalog.json.zip")) { unzip.Extract(@"catalog.json", "catalog.json"); } StreamReader catalogfile = File.OpenText("catalog.json"); bW_SubmitScores.ReportProgress(6); // Serializing JSON Data... JsonSerializer serializer = new JsonSerializer(); List<Simfile> catalog = (List<Simfile>)serializer.Deserialize(catalogfile, typeof(List<Simfile>)); File.Delete("catalog.json.zip"); bW_SubmitScores.ReportProgress(5); // Validating catalog checksum... string cataloghash = GetSHA(File.OpenText("catalog.json")).ToUpper(); if (cataloghash != GetResponseAsString("api/catalog/sha").ToUpper()) { bW_SubmitScores.ReportProgress(18); // Simfile catalog mismatch. bW_SubmitScores.CancelAsync(); } bW_SubmitScores.ReportProgress(7, catalog.Count); // Loaded N Simfile hashes. //Start iterating scores. bW_SubmitScores.ReportProgress(8); // Loading config file... // Load App parameters from config file. XmlDocument config = new XmlDocument(); config.Load("config.xml"); XmlNode app = config.SelectSingleNode("//app"); // See if there's a LocalProfile to use. bW_SubmitScores.ReportProgress(9); // Checking for local profile... string PlayerGuid = ""; if (File.Exists(app.Attributes["datadir"].Value + "/Save/LocalProfiles/" + app.Attributes["localprofile"].Value + "/Stats.xml")) { XmlDocument profilestats = new XmlDocument(); profilestats.Load(app.Attributes["datadir"].Value + "/Save/LocalProfiles/" + app.Attributes["localprofile"].Value + "/Stats.xml"); PlayerGuid = profilestats.SelectSingleNode("//Stats/GeneralData/Guid").InnerText; } // Count scores in Upload folder. bW_SubmitScores.ReportProgress(10); // Examining uploads folder... string[] uploads = Directory.GetFiles(app.Attributes["datadir"].Value + "/Save/Upload"); bW_SubmitScores.ReportProgress(11, uploads.Length); // Found N Scores //Parse scores. var dict = new Dictionary<int, Dictionary<string, string>> { }; int i = 0; foreach (string upload in uploads) { XmlDocument uploadxml = new XmlDocument(); uploadxml.Load(upload); // If the Guid of this upload is already in the database for this user, skip this file. if (guids.Contains(uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText)) { bW_SubmitScores.ReportProgress(15, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } // If we're skipping fails and this is a fail, skip this file. string HighScore = uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Grade").InnerText; if (HighScore == "Failed" && app.Attributes["uploadfails"].Value == "0") { bW_SubmitScores.ReportProgress(12, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } string xPlayerGuid = uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/PlayerGuid").InnerText; if (PlayerGuid == "" || PlayerGuid == xPlayerGuid) { // Check the hash of the file in the song dir against the catalog. try { string[] stepchart = Directory.GetFiles(app.Attributes["sm5dir"].Value + "\\" + uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/Song").Attributes["Dir"].Value, "*.sm"); if (stepchart.Length == 0) { // No .sm, look for a .dwi stepchart = Directory.GetFiles(app.Attributes["sm5dir"].Value + "\\" + uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/Song").Attributes["Dir"].Value, "*.dwi"); if (stepchart.Length == 0) { // No .dwi either, can't hash this, skip it. bW_SubmitScores.ReportProgress(13, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } } // At this point we should be able to hash whatever stepchart we ended up with in index 0. string simfilesha = GetSHA(File.OpenText(stepchart[0])).ToUpper(); string chartID = ""; try { Simfile simfile = catalog.Find(x => x.SHA256.Contains(simfilesha)); int simfile_id = simfile.ID; string difficulty = uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/Steps").Attributes["Difficulty"].Value.ToUpper(); string stepstype = uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/Steps").Attributes["StepsType"].Value.ToUpper(); Chart chart = simfile.Catalog.Find( x => (x.Difficulty.ToUpper() == difficulty && x.StepsType.ToUpper() == stepstype) ); chartID = chart.ID.ToString(); } // If this errors out it'll be immediate on the assignment of the simfile var because nothing in the Xenon DB has the same hash as this simfile. catch (ArgumentNullException error) { bW_SubmitScores.ReportProgress(14, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } catch (NullReferenceException error) { bW_SubmitScores.ReportProgress(14, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } // At this point all we have left are files which... // * Are not already in the scores table for this user... // * and have a matching hash in the simfile manifest... // * and have a corresponding chart ID to use. // We can, finally, begin uploading a new score. Dictionary<string, string> score = new Dictionary<string, string> { { "chart_id", chartID }, { "W1", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/W1").InnerText }, { "W2", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/W2").InnerText }, { "W3", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/W3").InnerText }, { "W4", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/W4").InnerText }, { "W5", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/W5").InnerText }, { "Miss", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/Miss").InnerText }, { "Held", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/HoldNoteScores/Held").InnerText }, { "LetGo", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/HoldNoteScores/LetGo").InnerText }, { "x_MachineGuid", uploadxml.SelectSingleNode("//Stats/MachineGuid").InnerText }, { "x_Grade", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Grade").InnerText }, { "x_Score", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Score").InnerText }, { "x_PercentDP", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/PercentDP").InnerText }, { "x_MaxCombo", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/MaxCombo").InnerText }, { "x_StageAward", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/StageAward").InnerText }, { "x_PeakComboAward", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/PeakComboAward").InnerText }, { "x_Modifiers", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Modifiers").InnerText }, { "x_DateTime", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/DateTime").InnerText }, { "x_PlayerGuid", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/PlayerGuid").InnerText }, { "x_HitMine", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/HitMine").InnerText }, { "x_AvoidMine", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/AvoidMine").InnerText }, { "x_CheckpointMiss", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/CheckpointMiss").InnerText }, { "x_CheckpointHit", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/TapNoteScores/CheckpointHit").InnerText }, { "x_MissedHold", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/HoldNoteScores/MissedHold").InnerText }, { "x_Stream", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Stream").InnerText }, { "x_Voltage", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Voltage").InnerText }, { "x_Air", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Air").InnerText }, { "x_Freeze", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Freeze").InnerText }, { "x_Chaos", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Chaos").InnerText }, { "x_Notes", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Notes").InnerText }, { "x_TapsAndHolds", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/TapsAndHolds").InnerText }, { "x_Jumps", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Jumps").InnerText }, { "x_Holds", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Holds").InnerText }, { "x_Mines", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Mines").InnerText }, { "x_Hands", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Hands").InnerText }, { "x_Rolls", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Rolls").InnerText }, { "x_Lifts", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Lifts").InnerText }, { "x_Fakes", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/RadarValues/Fakes").InnerText }, { "x_Disqualified", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Disqualified").InnerText }, { "x_Pad", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Pad").InnerText }, { "x_StageGuid", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/StageGuid").InnerText }, { "x_Guid", uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText } }; // Add this score to the dictionary we'll serialize and send. dict.Add(i, score); i++; // Report the upload. bW_SubmitScores.ReportProgress(16, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); } catch (DirectoryNotFoundException error) { bW_SubmitScores.ReportProgress(13, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } catch (FileNotFoundException error) { bW_SubmitScores.ReportProgress(13, uploadxml.SelectSingleNode("//Stats/RecentSongScores/HighScoreForASongAndSteps/HighScore/Guid").InnerText); continue; } } } //We've iterated all scores now, let's send the final json dict. int scorecount = dict.Count; string json = JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented); config.Load("config.xml"); XmlNode auth = config.SelectSingleNode("//auth"); // Build the web client... WebRequest request = WebRequest.Create(BuildURI("api/scores")); request.Method = "PUT"; request.Headers.Add("Authorization: Bearer " + auth.Attributes["access_token"].Value); request.ContentType = "application/json"; ASCIIEncoding encoding = new ASCIIEncoding(); Byte[] bytes = encoding.GetBytes(json); Stream stream = request.GetRequestStream(); // Send the PUT Request. stream.Write(bytes, 0, bytes.Length); stream.Close(); var response = (HttpWebResponse)request.GetResponse(); if ((int)response.StatusCode == 200) { bW_SubmitScores.ReportProgress(17, scorecount); } }
public Notechart(Simfile simfile) { Simfile = simfile; simfile.Notecharts.Add(this); }