Skip to content

Commit 64f11c8

Browse files
committed
Add a way to fetch missing map data based on mapId too
Still very much WIP and UI blocking.
1 parent 965fe09 commit 64f11c8

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

App/Misc/Helpers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,14 @@ public static void SetClipboardText(string text)
7777
}
7878
catch { }
7979
}
80+
public static string GetClipboardText()
81+
{
82+
try
83+
{
84+
return System.Windows.Forms.Clipboard.GetText();
85+
}
86+
catch { }
87+
return "";
88+
}
8089
}
8190
}

App/SidePanelActionsHandler.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
using System.IO;
55
using System.Linq;
66
using System.Threading;
7+
using System.Windows.Forms;
78
using App.Interfaces;
89
using App.Misc;
910
using App.Models;
1011
using App.Presenters.Controls;
1112
using App.Presenters.Forms;
1213
using CollectionManager.DataTypes;
1314
using CollectionManager.Enums;
15+
using CollectionManager.Interfaces;
1416
using CollectionManager.Modules.CollectionsManager;
1517
using CollectionManager.Modules.FileIO;
1618
using CollectionManagerExtensionsDll.DataTypes;
1719
using CollectionManagerExtensionsDll.Modules.API;
1820
using CollectionManagerExtensionsDll.Modules.API.osu;
1921
using CollectionManagerExtensionsDll.Modules.CollectionApiGenerator;
22+
using CollectionManagerExtensionsDll.Modules.TextProcessor;
2023
using GuiComponents.Interfaces;
2124
using NAudio.Codecs;
2225

@@ -73,9 +76,30 @@ private void BindMainFormActions()
7376

7477
private void GetMissingMapData()
7578
{
79+
//var test = Helpers.GetClipboardText();
80+
//var p = new TextProcessor();
81+
//var output = p.ParseLines(test.Split('\n').ToList());
82+
//foreach (var o in output)
83+
//{
84+
// var collection = new Collection(Initalizer.OsuFileIo.LoadedMaps) { Name = o.Key };
85+
// foreach (var mapResult in o.Value)
86+
// {
87+
// if (mapResult.IdType == TextProcessor.MapIdType.Map)
88+
// collection.AddBeatmapByMapId(mapResult.Id);
89+
// }
90+
// _collectionEditor.EditCollection(
91+
// CollectionEditArgs.AddCollections(
92+
// new Collections
93+
// {
94+
// collection
95+
// }));
96+
//}
97+
//TODO: UI for text parser and map data getter
98+
7699
if (BeatmapData == null)
77-
BeatmapData = new BeatmapData("abcd", Initalizer.OsuFileIo.LoadedMaps);
100+
BeatmapData = new BeatmapData("SNIP", Initalizer.OsuFileIo.LoadedMaps);
78101
var mapsWithMissingData = new Beatmaps();
102+
79103

80104
foreach (var collection in Initalizer.LoadedCollections)
81105
{
@@ -84,21 +108,29 @@ private void GetMissingMapData()
84108
mapsWithMissingData.Add(beatmap);
85109
}
86110
}
87-
var mapHashes = mapsWithMissingData.Where(m => !string.IsNullOrWhiteSpace(m.Md5)).Select(m => m.Md5).Distinct();
111+
var maps = mapsWithMissingData.Where(m => !string.IsNullOrWhiteSpace(m.Md5)).Distinct();
88112
List<Beatmap> fetchedBeatmaps = new List<Beatmap>();
89-
foreach (var mapHash in mapHashes)
113+
foreach (var map in maps)
90114
{
91-
var map = BeatmapData.GetBeatmapFromHash(mapHash, PlayMode.Osu);
92-
if (map != null)
115+
Beatmap downloadedBeatmap = null;
116+
if (map.MapId > 0)
117+
downloadedBeatmap = BeatmapData.GetBeatmapFromId(map.MapId, PlayMode.Osu);
118+
else
119+
if (!map.Md5.Contains("|"))
120+
downloadedBeatmap = BeatmapData.GetBeatmapFromHash(map.Md5, PlayMode.Osu);
121+
122+
if (downloadedBeatmap != null)
93123
{
94-
fetchedBeatmaps.Add(map);
124+
fetchedBeatmaps.Add(downloadedBeatmap);
95125
}
96126
}
97127
foreach (var collection in Initalizer.LoadedCollections)
98128
{
99129
foreach (var fetchedBeatmap in fetchedBeatmaps)
100130
{
101-
collection.ReplaceBeatmap(fetchedBeatmap.Md5, fetchedBeatmap);//TODO: this is really inefficient
131+
//TODO: this is really inefficient
132+
collection.ReplaceBeatmap(fetchedBeatmap.Md5, fetchedBeatmap);
133+
collection.ReplaceBeatmap(fetchedBeatmap.MapId, fetchedBeatmap);
102134
}
103135
}
104136
}

CollectionManagerDll/DataTypes/Beatmap.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ public override bool Equals(object obj)
458458
public override string ToString()
459459
{
460460
if (string.IsNullOrEmpty(Artist) && string.IsNullOrEmpty(Title))
461-
return Md5;
461+
{
462+
if(string.IsNullOrEmpty(Md5))
463+
return Md5;
464+
return "mapId: "+MapId;
465+
}
462466
var baseStr = Artist + " - " + Title;
463467
return baseStr;
464468
}

CollectionManagerDll/DataTypes/Collection.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Linq;
45
using CollectionManager.Annotations;
56
using CollectionManager.Exceptions;
67
using CollectionManager.Modules.FileIO.OsuDb;
@@ -121,8 +122,11 @@ public void AddBeatmap(Beatmap map)
121122

122123
public void AddBeatmap(BeatmapExtension map)
123124
{
125+
if (string.IsNullOrEmpty(map.Md5))
126+
map.Md5 = "semiRandomHash:" + map.MapId + "|" + map.MapSetId;
124127
if (_beatmapHashes.Contains(map.Md5))
125128
return;
129+
126130
_beatmapHashes.Add(map.Md5);
127131
ProcessNewlyAddedMap(map);
128132
}
@@ -135,6 +139,13 @@ public void AddBeatmapByHash(string hash)
135139
this.AddBeatmap(new BeatmapExtension() { Md5 = hash });
136140
}
137141

142+
public void AddBeatmapByMapId(int mapId)
143+
{
144+
if (AllBeatmaps().Any(m=>m.MapId==mapId))
145+
return;
146+
this.AddBeatmap(new BeatmapExtension() { MapId = mapId});
147+
}
148+
138149
private void ProcessAdditionalProps(BeatmapExtension src, BeatmapExtension dest)
139150
{
140151
dest.UserComment = src.UserComment;
@@ -182,6 +193,13 @@ public void ReplaceBeatmap(string hash, Beatmap newBeatmap)
182193
if (RemoveBeatmap(hash))
183194
AddBeatmap(newBeatmap);
184195
}
196+
public void ReplaceBeatmap(int mapId, Beatmap newBeatmap)
197+
{
198+
var map = AllBeatmaps().FirstOrDefault(m => m.MapId == mapId);
199+
200+
if (map != null && RemoveBeatmap(map.Md5))
201+
AddBeatmap(newBeatmap);
202+
}
185203
public bool RemoveBeatmap(string hash)
186204
{
187205
if (_beatmapHashes.Contains(hash))

CollectionManagerExtensionsDll/Modules/API/osu/OsuApi.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public Beatmap GetBeatmap(string hash, PlayMode? gamemode = null)
135135
{
136136
var link = GetBeatmapsURL + "?k=" + ApiKey + "&h=" + hash;
137137
if (gamemode.HasValue)
138-
link += "&m=" + (int) gamemode;
138+
link += "&m=" + (int)gamemode;
139139

140140
return GetBeatmapResult(link);
141141
}
@@ -207,7 +207,15 @@ private BeatmapExtension GetBeatmapResult(string url)
207207
beatmap.ArtistRoman = json["artist"].ToString();
208208
beatmap.TitleRoman = json["title"].ToString();
209209
beatmap.Creator = json["creator"].ToString();
210-
beatmap.ModPpStars.Add(PlayMode.Osu, new Dictionary<int, double>()
210+
beatmap.CircleSize = Convert.ToSingle(json["diff_size"].ToString(), CultureInfo.InvariantCulture);
211+
beatmap.OverallDifficulty = Convert.ToSingle(json["diff_overall"].ToString(),CultureInfo.InvariantCulture);
212+
beatmap.ApproachRate = Convert.ToSingle(json["diff_approach"].ToString(), CultureInfo.InvariantCulture);
213+
beatmap.HpDrainRate = Convert.ToSingle(json["diff_drain"].ToString(), CultureInfo.InvariantCulture);
214+
beatmap.PlayMode = (PlayMode)Convert.ToUInt32(json["mode"].ToString(), CultureInfo.InvariantCulture);
215+
//beatmap.bpm
216+
beatmap.Source = json["source"].ToString();
217+
beatmap.Tags = json["tags"].ToString();
218+
beatmap.ModPpStars.Add(beatmap.PlayMode, new Dictionary<int, double>()
211219
{
212220
{ 0, Math.Round(double.Parse(json["difficultyrating"].ToString(), CultureInfo.InvariantCulture), 2) }
213221
});

CollectionManagerExtensionsDll/Modules/TextProcessor/TextProcessor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class TextProcessor
1212
private static Regex beatmapIdRegex = new Regex(@"(?:https:\/\/osu.ppy.sh\/b\/)([\d]{0,10})", RegexOptions.Compiled);
1313

1414
private bool LineIsCollectionName(string line) =>
15-
line.Count(l => l == ' ' || l == '\t') < 3; //arbitary number that works 100% out of 50% times!
15+
!string.IsNullOrWhiteSpace(line) &&
16+
line.Count(l => l == ' ' || l == '\t' || l == '\'' || l == '!') < 2 && //arbitary number that works 100% out of 50% times!
17+
!line.Contains("osu.ppy.sh/");
1618
public enum MapIdType { Map, Set, None }
1719

1820
public class TextProcessorLinkResult
@@ -26,6 +28,7 @@ public Dictionary<string, List<TextProcessorLinkResult>> ParseLines(List<string>
2628
{
2729
var ret = new Dictionary<string, List<TextProcessorLinkResult>>();
2830
var currentCollectionName = "ParsedText";
31+
ret[currentCollectionName] = new List<TextProcessorLinkResult>();
2932
foreach (var line in lines)
3033
{
3134
if (LineIsCollectionName(line.Trim()))
@@ -37,7 +40,8 @@ public Dictionary<string, List<TextProcessorLinkResult>> ParseLines(List<string>
3740
else
3841
ret[currentCollectionName].Add(ExtractMapLink(line));
3942
}
40-
43+
if (!ret["ParsedText"].Any())
44+
ret.Remove("ParsedText");
4145
return ret;
4246
}
4347

0 commit comments

Comments
 (0)