Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 28.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using Microsoft.Xna.Framework;
  9. using osu.Configuration;
  10. using osu.Constants;
  11. using osu.GameModes.Play;
  12. using osu.GameplayElements.Beatmaps;
  13. using osu.GameplayElements.Scoring.Processors;
  14. using osu.Graphics.Notifications;
  15. using osu.Helpers;
  16. using osu.Online;
  17. using osu.Online.Drawable;
  18. using osu_common;
  19. using osu_common.Bancho;
  20. using osu_common.Bancho.Objects;
  21. using osu_common.Helpers;
  22. using SevenZip.Compression.LZMA;
  23.  
  24. namespace osu.GameplayElements.Scoring
  25. {
  26.     internal class Score : bSerializable, IComparable<Score>, ICloneable
  27.     {
  28.         internal virtual bool AllowFrameSkipping => true;
  29.  
  30.         internal virtual float Accuracy => TotalHits > 0 ? (float)(Count50 * 50 + Count100 * 100 + Count300 * 300) / (TotalHits * 300) : 1;
  31.  
  32.         internal string LocalScoreChecksum => CryptoHelper.GetMd5String(MaxCombo + @"osu" + PlayerName + FileChecksum + TotalScore + Rank);
  33.  
  34.         internal virtual int TotalHits => Count50 + Count100 + Count300 + CountMiss;
  35.  
  36.         internal virtual int TotalSuccessfulHits => Count50 + Count100 + Count300;
  37.  
  38.         internal string ReplayString
  39.         {
  40.             get
  41.             {
  42.                 StringBuilder replayData = new StringBuilder();
  43.  
  44.                 if (Replay != null)
  45.                 {
  46.                     bReplayFrame lastF = new bReplayFrame(0, 0, 0, pButtonState.None);
  47.                     foreach (bReplayFrame f in Replay)
  48.                     {
  49.                         replayData.AppendFormat(@"{0}|{1}|{2}|{3},",
  50.                             f.time - lastF.time, (f.mouseX).ToString(GameBase.nfi),
  51.                             (f.mouseY).ToString(GameBase.nfi),
  52.                             (int)f.buttonState);
  53.                         lastF = f;
  54.                     }
  55.                 }
  56.                 replayData.AppendFormat(@"{0}|{1}|{2}|{3},", -12345, 0, 0, Seed);
  57.                 return replayData.ToString();
  58.             }
  59.         }
  60.  
  61.         internal virtual Ranks Rank
  62.         {
  63.             get
  64.             {
  65.                 float ratio300 = (float)Count300 / TotalHits;
  66.                 float ratio50 = (float)Count50 / TotalHits;
  67.                 if (!Pass) //something has gone wrong.
  68.                     return Ranks.F;
  69.                 if (ratio300 == 1)
  70.                     return ModManager.CheckActive(EnabledMods, Mods.Hidden) ||
  71.                            ModManager.CheckActive(EnabledMods, Mods.Flashlight)
  72.                         ? Ranks.XH
  73.                         : Ranks.X;
  74.                 if (ratio300 > 0.9 && ratio50 <= 0.01 && CountMiss == 0)
  75.                     return ModManager.CheckActive(EnabledMods, Mods.Hidden) ||
  76.                            ModManager.CheckActive(EnabledMods, Mods.Flashlight)
  77.                         ? Ranks.SH
  78.                         : Ranks.S;
  79.                 if ((ratio300 > 0.8 && CountMiss == 0) || (ratio300 > 0.9))
  80.                     return Ranks.A;
  81.                 if ((ratio300 > 0.7 && CountMiss == 0) || (ratio300 > 0.8))
  82.                     return Ranks.B;
  83.                 if (ratio300 > 0.6)
  84.                     return Ranks.C;
  85.                 return Ranks.D;
  86.             }
  87.         }
  88.  
  89.         internal string GhostFilename => ReplayFilename.Replace(@".osr", @".osg");
  90.  
  91.         internal string ReplayFilename
  92.         {
  93.             get
  94.             {
  95.                 long dateConverted;
  96.                 try
  97.                 {
  98.                     dateConverted = Date.ToFileTimeUtc();
  99.                 }
  100.                 catch
  101.                 {
  102.                     dateConverted = DateTime.Now.ToFileTimeUtc();
  103.                 }
  104.                 return Path.Combine(ScoreManager.ReplayCachePath, $@"{FileChecksum}-{dateConverted}.osr");
  105.             }
  106.         }
  107.  
  108.         internal bool LocalReplayPresent => File.Exists(ReplayFilename);
  109.  
  110.         internal ushort Count100;
  111.         internal ushort Count300;
  112.         internal ushort Count50;
  113.         internal ushort CountGeki;
  114.         internal ushort CountKatu;
  115.         internal ushort CountMiss;
  116.         internal DateTime Date;
  117.         internal Obfuscated<Mods> EnabledMods = Mods.None;
  118.         internal PlayModes PlayMode;
  119.         internal string FileChecksum = string.Empty;
  120.         internal List<Vector2> HpGraph = new List<Vector2>();
  121.         internal bool IsOnline;
  122.         internal int MaxCombo;
  123.         internal long OnlineId;
  124.         internal int OnlineRank;
  125.         internal bool Pass;
  126.         internal bool Exit;
  127.         internal int FailTime;
  128.         internal bool Perfect;
  129.         internal string PlayerName;
  130.         internal string HpGraphString;
  131.         internal byte[] ReplayCompressed;
  132.         internal List<bReplayFrame> Replay;
  133.         internal List<int> HitErrors = new List<int>();
  134.         internal List<int> SpinningRates = new List<int>();
  135.         internal List<bool> SectionResults = new List<bool>();
  136.         internal List<bScoreFrame> Frames = new List<bScoreFrame>();
  137.  
  138.         private int totalScore;
  139.         internal virtual int TotalScore
  140.         {
  141.             get
  142.             {
  143.                 return Processor?.TotalScore ?? totalScore;
  144.             }
  145.  
  146.             set
  147.             {
  148.                 Debug.Assert(Processor == null, "TotalScore cannot be modified when a ScoreProcessor is attached to a Score.");
  149.                 totalScore = value;
  150.             }
  151.         }
  152.  
  153.         //todo: move this clusterfuck to ScoreMania
  154.         internal double TotalScoreDouble;
  155.  
  156.         internal User User;
  157.         internal int Version = General.VERSION;
  158.         internal double CurrentHp;
  159.         internal ushort CurrentCombo;
  160.         internal string SubmissionResponseString;
  161.         internal MemoryStream ExtraData;
  162.         internal int Seed;
  163.  
  164.         internal ScoreProcessor Processor;
  165.  
  166.         internal Beatmap Beatmap;
  167.  
  168.         private bool allowSubmission = true;
  169.         internal bool AllowSubmission
  170.         {
  171.             get
  172.             {
  173.                 return
  174.                     Processor == null && //score submission should not be allowed with a new score processing algorithm for now.
  175.                     allowSubmission;
  176.             }
  177.         }
  178.  
  179.         internal ScoreSubmissionStatus SubmissionStatus { get; private set; }
  180.         internal bool HasOnlineReplay { get; private set; }
  181.         private string visualSettingsString => $@"{Beatmap.DimLevel}:{Beatmap.DisableSamples}:{Beatmap.DisableSkin}:{Beatmap.DisableStoryboard}:{Beatmap.BackgroundVisible}";
  182.  
  183.         private string onlineFormatted
  184.         {
  185.             get
  186.             {
  187.                 string specialFlag = string.Empty;
  188.                 for (int i = 0; i < (int)Player.flag; i++)
  189.                     specialFlag += ' ';
  190.                 Player.flag = 0;
  191.  
  192.                 string player = PlayerName;
  193.  
  194. #if DEBUG
  195.                 if (player != ConfigManager.sUsername)
  196.                     player += @"/" + ConfigManager.sUsername;
  197. #endif
  198.  
  199.                 return
  200.                     string.Format(@"{0}:{1}:{2}:{3}:{4}:{5}:{6}:{7}:{8}:{9}:{10}:{11}:{12}:{13}:{14}:{15}:{16:yyMMddHHmmss}:{17}", FileChecksum,
  201.                         player + ((BanchoClient.Permission & Permissions.Supporter) > 0 ? " " : ""), onlineScoreChecksum, Count300, Count100, Count50,
  202.                         CountGeki, CountKatu, CountMiss, TotalScore, MaxCombo, Perfect, Rank, (int)(Mods)EnabledMods, Pass, (int)PlayMode, Date.ToUniversalTime(),
  203.                         General.VERSION + specialFlag);
  204.             }
  205.         }
  206.  
  207.         private string onlineScoreChecksum
  208.         {
  209.             get
  210.             {
  211.                 string checkString =
  212.                     string.Format(@"chickenmcnuggets{0}o15{1}{2}smustard{3}{4}uu{5}{6}{7}{8}{9}{10}{11}" + (char)0x51 + @"{12}{13}{15}{14:yyMMddHHmmss}{16}", Count100 + Count300, Count50,
  213.                         CountGeki, CountKatu, CountMiss, FileChecksum, MaxCombo, Perfect,
  214.                         PlayerName, TotalScore, Rank, (int)(Mods)EnabledMods, Pass, (int)PlayMode, Date.ToUniversalTime(), General.VERSION, GameBase.ClientHash);
  215.                 string md5 = CryptoHelper.GetMd5String(checkString);
  216.                 if (md5.Length != 32)
  217.                     throw new Exception(@"checksum failure");
  218.                 return md5;
  219.             }
  220.         }
  221.  
  222.         internal Score()
  223.         {
  224.             Replay = new List<bReplayFrame>();
  225.             Date = DateTime.Now;
  226.         }
  227.  
  228.         internal Score(Beatmap beatmap, string playerName)
  229.         {
  230.             if (beatmap != null)
  231.                 FileChecksum = beatmap.BeatmapChecksum;
  232.             Beatmap = beatmap;
  233.             PlayerName = playerName ?? string.Empty;
  234.             Date = DateTime.Now;
  235.             Pass = false;
  236.             Perfect = false;
  237.             Replay = new List<bReplayFrame>();
  238.         }
  239.  
  240.         internal Score(string input, Beatmap beatmap)
  241.         {
  242.             User = new User();
  243.             IsOnline = true;
  244.  
  245.             if (beatmap != null)
  246.             {
  247.                 Beatmap = beatmap;
  248.                 FileChecksum = beatmap.BeatmapChecksum;
  249.             }
  250.  
  251.             Perfect = false;
  252.             Replay = new List<bReplayFrame>();
  253.  
  254.             string[] line = input.Split('|');
  255.  
  256.             int i = 0;
  257.  
  258.             Pass = true;
  259.             OnlineId = Convert.ToInt64(line[i++]);
  260.             PlayerName = line[i++];
  261.             User.Name = PlayerName;
  262.             TotalScore = Convert.ToInt32(line[i++]);
  263.             MaxCombo = Convert.ToUInt16(line[i++]);
  264.             Count50 = Convert.ToUInt16(line[i++]);
  265.             Count100 = Convert.ToUInt16(line[i++]);
  266.             Count300 = Convert.ToUInt16(line[i++]);
  267.             CountMiss = Convert.ToUInt16(line[i++]);
  268.             CountKatu = Convert.ToUInt16(line[i++]);
  269.             CountGeki = Convert.ToUInt16(line[i++]);
  270.             Perfect = line[i++] == @"1";
  271.             EnabledMods = (Mods)Convert.ToInt32(line[i++]);
  272.             User.Id = Convert.ToInt32(line[i++]);
  273.             if (line[i].Length > 0)
  274.                 OnlineRank = Convert.ToInt32(line[i]);
  275.             i++;
  276.             try
  277.             {
  278.                 Date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(int.Parse(line[i++]));
  279.                 if (Date < new DateTime(1990, 5, 31) /*|| date > DateTime.Now*/)
  280.                     Date = DateTime.Now; // when trying to export a corrupted replay file
  281.             }
  282.             catch
  283.             {
  284.                 Date = DateTime.Now;
  285.             }
  286.             HasOnlineReplay = line[i++] == @"1";
  287.         }
  288.  
  289.         internal Score(bScoreFrame sf, string name)
  290.         {
  291.             Pass = sf.pass;
  292.             PlayerName = name;
  293.             Count300 = sf.count300;
  294.             Count100 = sf.count100;
  295.             Count50 = sf.count50;
  296.             CountGeki = sf.countGeki;
  297.             CountKatu = sf.countKatu;
  298.             CountMiss = sf.countMiss;
  299.             TotalScore = sf.totalScore;
  300.             MaxCombo = sf.maxCombo;
  301.             Perfect = sf.perfect;
  302.             CurrentHp = sf.currentHp;
  303.             CurrentCombo = sf.currentCombo;
  304.             EnabledMods = ModManager.ModStatus; //todo: wtf check this?
  305.             Date = DateTime.Now;
  306.         }
  307.  
  308.         public void ReadFromStream(SerializationReader sr)
  309.         {
  310.             if (sr.BaseStream.Position == 1)
  311.                 ReadHeaderFromStream(sr);
  312.  
  313.             ReplayCompressed = sr.ReadByteArray();
  314.  
  315.             if (Version >= 20140721)
  316.                 OnlineId = sr.ReadInt64();
  317.             else if (Version >= General.VERSION_FIRST_OSZ2)
  318.                 OnlineId = sr.ReadInt32();
  319.  
  320.             ReadModSpecificData(sr);
  321.         }
  322.  
  323.         public void ReadHeaderFromStream(SerializationReader sr)
  324.         {
  325.             Pass = true;
  326.             Version = sr.ReadInt32();
  327.             FileChecksum = sr.ReadString();
  328.             PlayerName = sr.ReadString();
  329.             var localScoreChecksum = sr.ReadString();
  330.             Count300 = sr.ReadUInt16();
  331.             Count100 = sr.ReadUInt16();
  332.             Count50 = sr.ReadUInt16();
  333.             CountGeki = sr.ReadUInt16();
  334.             CountKatu = sr.ReadUInt16();
  335.             CountMiss = sr.ReadUInt16();
  336.             TotalScore = sr.ReadInt32();
  337.             MaxCombo = sr.ReadUInt16();
  338.             Perfect = sr.ReadBoolean();
  339.             EnabledMods = (Mods)sr.ReadInt32();
  340.             HpGraphString = sr.ReadString();
  341.             Date = sr.ReadDateTime();
  342.         }
  343.  
  344.         public void WriteToStream(SerializationWriter sw)
  345.         {
  346.             Pass = true;
  347.             sw.Write((byte)PlayMode); //Note that this is written out but not read back in.  ScoreFactory needs it.
  348.             sw.Write(General.VERSION);
  349.             sw.Write(FileChecksum);
  350.             sw.Write(PlayerName);
  351.             sw.Write(LocalScoreChecksum);
  352.             sw.Write(Count300);
  353.             sw.Write(Count100);
  354.             sw.Write(Count50);
  355.             sw.Write(CountGeki);
  356.             sw.Write(CountKatu);
  357.             sw.Write(CountMiss);
  358.             sw.Write(TotalScore);
  359.             sw.Write((ushort)MaxCombo);
  360.             sw.Write(Perfect);
  361.             sw.Write((int)(Mods)EnabledMods);
  362.             sw.Write(GetGraphFormatted());
  363.             sw.Write(Date);
  364.             sw.WriteByteArray(ReplayCompressed);
  365.             sw.Write(OnlineId);
  366.             WriteModSpecificData(sw);
  367.         }
  368.  
  369.         public int CompareTo(Score other)
  370.         {
  371.             int comparison = other.TotalScore.CompareTo(TotalScore);
  372.             return comparison == 0 ? Date.CompareTo(other.Date) : comparison;
  373.         }
  374.  
  375.         #region ICloneable Members
  376.  
  377.         public object Clone()
  378.         {
  379.             return MemberwiseClone();
  380.         }
  381.  
  382.         #endregion
  383.  
  384.         internal void InvalidateSubmission()
  385.         {
  386.             allowSubmission = false;
  387.         }
  388.  
  389.         internal string GetGraphFormatted()
  390.         {
  391.             if (!string.IsNullOrEmpty(HpGraphString))
  392.                 return HpGraphString;
  393.  
  394.             StringBuilder graphData = new StringBuilder();
  395.             float last = 0;
  396.  
  397.             if (HpGraph == null)
  398.                 return null;
  399.  
  400.             for (int i = 0; i < HpGraph.Count; i++)
  401.             {
  402.                 Vector2 v = HpGraph[i];
  403.  
  404.                 if (v.X - last > 2000 || i == HpGraph.Count - 1 || i == 0)
  405.                 {
  406.                     last = v.X;
  407.                     graphData.AppendFormat(@"{0}|{1},", Math.Round(v.X, 2).ToString(GameBase.nfi),
  408.                         Math.Round(v.Y, 2).ToString(GameBase.nfi));
  409.                 }
  410.             }
  411.             HpGraph.Clear();
  412.             HpGraph = null;
  413.  
  414.             HpGraphString = graphData.ToString();
  415.             return HpGraphString;
  416.         }
  417.  
  418.         internal void Submit()
  419.         {
  420.             if (SubmissionStatus > ScoreSubmissionStatus.NotSubmitted)
  421.                 return;
  422.             SubmissionStatus = ScoreSubmissionStatus.Submitting;
  423.  
  424.             if (!GameBase.HasLogin)
  425.                 return;
  426.             BackgroundWorker b = new BackgroundWorker();
  427.             b.DoWork += submit;
  428.             b.RunWorkerAsync();
  429.         }
  430.  
  431.         internal void GetReplayData()
  432.         {
  433.             pWebRequest dnr = new pWebRequest(string.Format(General.WEB_ROOT + @"/web/osu-getreplay.php?c={0}&m={1}&u={2}&h={3}",
  434.                 OnlineId, (int)PlayMode, ConfigManager.sUsername, ConfigManager.sPassword));
  435.             dnr.Finished += dnr_onFinish;
  436.             dnr.Perform();
  437.         }
  438.  
  439.         internal void ReadReplayData()
  440.         {
  441.             if (ReplayCompressed == null || ReplayCompressed.Length == 0) return;
  442.  
  443.             if (Replay == null)
  444.                 Replay = new List<bReplayFrame>();
  445.             else
  446.                 Replay.Clear();
  447.  
  448.             try
  449.             {
  450.                 ReadReplayData(new ASCIIEncoding().GetString(SevenZipHelper.Decompress(ReplayCompressed)));
  451.             }
  452.             catch (Exception)
  453.             {
  454.                 Replay.Clear();
  455.             }
  456.  
  457. #if DEBUG
  458.             StreamWriter w = File.CreateText(@"replay.txt");
  459.             foreach (bReplayFrame f in Replay)
  460.                 w.WriteLine(@"{0} {1} {2} {3} {4} {5} {6}", f.time, f.mouseX, f.mouseY, f.mouseLeft1, f.mouseLeft2,
  461.                     f.mouseRight1, f.mouseRight2);
  462.             w.Close();
  463. #endif
  464.         }
  465.  
  466.         internal void ReadReplayData(string replayData)
  467.         {
  468.             if (replayData.Length > 0)
  469.             {
  470.                 string[] replaylines = replayData.Split(',');
  471.  
  472.                 bReplayFrame lastF;
  473.                 if (Replay.Count > 0)
  474.                     lastF = Replay[Replay.Count - 1];
  475.                 else
  476.                     lastF = new bReplayFrame(0, 0, 0, pButtonState.None);
  477.  
  478.                 foreach (string replayline in replaylines)
  479.                 {
  480.                     if (replayline.Length == 0)
  481.                         continue;
  482.  
  483.                     string[] data = replayline.Split('|');
  484.  
  485.                     if (data.Length < 4)
  486.                         continue;
  487.                     if (data[0] == @"-12345")
  488.                     {
  489.                         Seed = int.Parse(data[3]);
  490.                         continue;
  491.                     }
  492.  
  493.                     pButtonState buttons = (pButtonState)Enum.Parse(typeof(pButtonState), data[3]);
  494.  
  495.                     bReplayFrame newF = new bReplayFrame(int.Parse(data[0]) + lastF.time,
  496.                         float.Parse(data[1], GameBase.nfi),
  497.                         float.Parse(data[2], GameBase.nfi),
  498.                         buttons);
  499.                     Replay.Add(newF);
  500.  
  501.                     lastF = newF;
  502.                 }
  503.             }
  504.         }
  505.  
  506.         internal void ReadGraphData(string graphData)
  507.         {
  508.             HpGraph = new List<Vector2>();
  509.  
  510.             if (graphData.Length > 0)
  511.             {
  512.                 string[] graphlines = graphData.Split(',');
  513.  
  514.                 foreach (string graphline in graphlines)
  515.                 {
  516.                     if (graphline.Length == 0)
  517.                         continue;
  518.  
  519.                     string[] data = graphline.Split('|');
  520.                     HpGraph.Add(
  521.                         new Vector2((float)decimal.Parse(data[0], GameBase.nfi),
  522.                             (float)decimal.Parse(data[1], GameBase.nfi)));
  523.                 }
  524.             }
  525.         }
  526.  
  527.         internal void DisposeHpGraph()
  528.         {
  529.             GetGraphFormatted();
  530.  
  531.             if (HpGraph != null)
  532.             {
  533.                 HpGraph.Clear();
  534.                 HpGraph = null;
  535.             }
  536.         }
  537.  
  538.         internal virtual void Reset(bool storeStatistics = false)
  539.         {
  540.             if (Processor != null)
  541.                 Processor.Reset(storeStatistics);
  542.             else
  543.             {
  544.                 TotalScore = 0;
  545.                 TotalScoreDouble = 0;
  546.             }
  547.  
  548.             CurrentCombo = 0;
  549.             MaxCombo = 0;
  550.             CountMiss = 0;
  551.             Count50 = 0;
  552.             Count100 = 0;
  553.             Count300 = 0;
  554.             CountGeki = 0;
  555.             CountMiss = 0;
  556.             CountKatu = 0;
  557.         }
  558.  
  559.         internal void ClearReplayData()
  560.         {
  561.             Replay = null;
  562.             ReplayCompressed = null;
  563.             HpGraphString = null;
  564.         }
  565.  
  566.         internal void LoadLocalData()
  567.         {
  568.             if (!LocalReplayPresent)
  569.                 return;
  570.  
  571.             Score inScore = ScoreManager.ReadReplayFromFile(ReplayFilename, false);
  572.  
  573.             if (inScore != null)
  574.             {
  575.                 ReplayCompressed = inScore.ReplayCompressed;
  576.                 HpGraphString = inScore.HpGraphString;
  577.             }
  578.         }
  579.  
  580.         internal void PurgeReplay()
  581.         {
  582.             File.Delete(ReplayFilename);
  583.             File.Delete(GhostFilename);
  584.         }
  585.  
  586.         protected virtual void ReadModSpecificData(SerializationReader sr)
  587.         {
  588.         }
  589.  
  590.         protected virtual void WriteModSpecificData(SerializationWriter sw)
  591.         {
  592.         }
  593.  
  594.         private void dnr_onFinish(pWebRequest r, Exception e)
  595.         {
  596.             ReplayCompressed = r.ResponseData;
  597.             ReadReplayData();
  598.             if (GotReplayData != null)
  599.                 GotReplayData(this);
  600.         }
  601.  
  602.         private void submit(object sender, DoWorkEventArgs e)
  603.         {
  604. #if ARCADE
  605.     //Don't submit scores yet, no matter what.
  606.             return;
  607. #endif
  608.  
  609. #if NO_SCORE_SUBMIT
  610.             NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_SubmissionDisabled));
  611.             return;
  612. #endif
  613.  
  614.             GameBase.User.spriteInfo.Text = LocalisationManager.GetString(OsuString.Score_SubmittingScore);
  615.             try
  616.             {
  617.                 byte[] zipped = new byte[0];
  618.  
  619.                 if (Pass)
  620.                 {
  621.                     if (BeatmapManager.Current.onlinePersonalScore != null &&
  622.                         TotalScore > BeatmapManager.Current.onlinePersonalScore.TotalScore)
  623.                         BeatmapManager.Current.Scores.Clear();
  624.                     //We should re-retrieve online scores no matter what, otherwise when clicking 'retry' the personal score won't be updated.
  625.  
  626.                     ReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(ReplayString));
  627.  
  628. #if DEBUG
  629.                     if (ReplayCompressed.Length < 100)
  630.                         LoadLocalData();
  631. #endif
  632.  
  633.                     zipped = ReplayCompressed;
  634.                 }
  635.  
  636.                 pWebRequest req = new pWebRequest(General.WEB_ROOT + @"/web/osu-submit-modular.php");
  637.                 req.AddFile(@"score", zipped);
  638.  
  639.                 string iv = null;
  640.  
  641. #if SUBMISSION_DEBUG
  642.                 File.AppendAllText(@"DEBUG.txt", @"Debug at " + DateTime.Now + @"\n");
  643. #endif
  644.  
  645.                 if (Pass)
  646.                 {
  647.                     Process[] procs = GameBase.Processes;
  648.                     GameBase.Processes = null;
  649.  
  650.                     if (procs == null || procs.Length == 0)
  651.                         procs = Process.GetProcesses();
  652.                     StringBuilder b = new StringBuilder();
  653.                     foreach (Process p in procs)
  654.                     {
  655.                         string filename = string.Empty;
  656.                         try
  657.                         {
  658.                             filename = p.MainModule.FileName;
  659.                             FileInfo fi = new FileInfo(filename);
  660.                             if (fi != null)
  661.                                 filename = CryptoHelper.GetMd5String(fi.Length.ToString()) + @" " + filename;
  662.                         }
  663.                         catch
  664.                         {
  665.                         }
  666.                         b.AppendLine(filename + @" | " + p.ProcessName + @" (" + p.MainWindowTitle + @")");
  667.                     }
  668.  
  669. #if SUBMISSION_DEBUG
  670.                     File.AppendAllText(@"DEBUG.txt", @"Running Processes:\n" + b + @"\n\n");
  671. #endif
  672.                     req.AddParameter(@"pl", CryptoHelper.EncryptString(b.ToString(), Secrets.GetScoreSubmissionKey(), ref iv));
  673.                 }
  674.                 else
  675.                 {
  676.                     req.AddParameter(@"x", Exit ? @"1" : @"0");
  677.                     req.AddParameter(@"ft", FailTime.ToString());
  678.                 }
  679.  
  680. #if SUBMISSION_DEBUG
  681.                 File.AppendAllText(@"DEBUG.txt", @"\n1:" + onlineFormatted + @"\n");
  682.                 File.AppendAllText(@"DEBUG.txt", @"\n2:" + GameBase.clientHash + @"\n");
  683.                 File.AppendAllText(@"DEBUG.txt", @"\n3:" + iv + @"\n");
  684. #endif
  685.  
  686.                 req.AddParameter(@"score", CryptoHelper.EncryptString(onlineFormatted, Secrets.GetScoreSubmissionKey(), ref iv));
  687.                 req.AddParameter(@"fs", CryptoHelper.EncryptString(visualSettingsString, Secrets.GetScoreSubmissionKey(), ref iv));
  688.                 req.AddParameter(@"c1", GameBase.CreateUniqueId());
  689.                 req.AddParameter(@"pass", ConfigManager.sPassword);
  690.                 req.AddParameter(@"osuver", General.VERSION.ToString());
  691.                 req.AddParameter(@"s", CryptoHelper.EncryptString(GameBase.ClientHash, Secrets.GetScoreSubmissionKey(), ref iv));
  692.  
  693.                 try
  694.                 {
  695.                     if (Pass && ExtraData != null)
  696.                         req.AddFile(@"i", ExtraData.ToArray());
  697.                     else
  698.                         req.AddParameter(@"i", string.Empty);
  699.                 }
  700.                 catch
  701.                 {
  702.                 }
  703.  
  704.                 GameBase.ChangeAllowance++;
  705.  
  706.                 ExtraData = null;
  707.  
  708.                 req.AddParameter(@"iv", iv);
  709.  
  710.                 int retryCount = Pass ? 10 : 2;
  711.                 int retryDelay = 7500;
  712.  
  713.                 bool didError = false;
  714.  
  715.                 while (retryCount-- > 0)
  716.                 {
  717.                     try
  718.                     {
  719.                         req.BlockingPerform();
  720.                         SubmissionResponseString = req.ResponseString;
  721.  
  722. #if SUBMISSION_DEBUG
  723.                         Debug.Print(SubmissionResponseString);
  724.                         File.AppendAllText(@"DEBUG.txt", @"\nres:" + SubmissionResponseString + @"\n\n\n\n\n-------------------\n\n\n\n");
  725. #endif
  726.  
  727.                         if (SubmissionResponseString.Contains(@"error:"))
  728.                         {
  729.                             switch (SubmissionResponseString.Replace(@"error: ", string.Empty))
  730.                             {
  731.                                 case @"reset":
  732.                                     BanchoClient.HandlePasswordReset();
  733.                                     break;
  734.                                 case @"verify":
  735.                                     BanchoClient.RequireVerification();
  736.                                     break;
  737.                                 case @"nouser":
  738.                                     NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorNoUser));
  739.                                     break;
  740.                                 case @"pass":
  741.                                     NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorPassword));
  742.                                     break;
  743.                                 case @"inactive":
  744.                                 case @"ban":
  745.                                     NotificationManager.ShowMessage("ERROR: Your account is no longer active.  Please send an email to accounts@ppy.sh if you think this is a mistake.");
  746.                                     break;
  747.                                 case @"beatmap":
  748.                                     if (Beatmap != null && Beatmap.SubmissionStatus > osu_common.SubmissionStatus.Pending)
  749.                                     {
  750.                                         NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorBeatmap));
  751.                                         Beatmap.SubmissionStatus = osu_common.SubmissionStatus.Unknown;
  752.                                     }
  753.                                     break;
  754.                                 case @"disabled":
  755.                                     NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorDisabled));
  756.                                     break;
  757.                                 case @"oldver":
  758.                                     NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorVersion));
  759.                                     GameBase.CheckForUpdates(true);
  760.                                     break;
  761.                                 case @"no":
  762.                                     break;
  763.                             }
  764.                             didError = true;
  765.                         }
  766.                         break;
  767.                     }
  768.                     catch
  769.                     {
  770.                     }
  771.  
  772.                     if (retryDelay >= 60000)
  773.                         NotificationManager.ShowMessage(string.Format(LocalisationManager.GetString(OsuString.Score_SubmissionFailed), (retryDelay / 60000)));
  774.                     Thread.Sleep(retryDelay);
  775.                     retryDelay *= 2;
  776.                 }
  777.  
  778.                 if (didError)
  779.                 {
  780.                     SubmissionStatus = ScoreSubmissionStatus.Complete;
  781.                     return;
  782.                 }
  783.             }
  784.             catch (Exception ex)
  785.             {
  786.             }
  787.  
  788.             if (SubmissionComplete != null)
  789.                 SubmissionComplete(this);
  790.  
  791.             SubmissionStatus = ScoreSubmissionStatus.Complete;
  792.  
  793.             if (!Pass)
  794.                 GameBase.User.Refresh();
  795.         }
  796.  
  797.         internal event GotReplayDataHandler GotReplayData;
  798.  
  799.         internal event GotReplayDataHandler SubmissionComplete;
  800.     }
  801.  
  802.     internal enum ScoreSubmissionStatus
  803.     {
  804.         NotSubmitted,
  805.         Submitting,
  806.         Complete
  807.     }
  808. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement