Skip to content

Commit

Permalink
add .osu support to load
Browse files Browse the repository at this point in the history
  • Loading branch information
Avibah committed Mar 9, 2024
1 parent fc33659 commit 9e157a5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Editor/New SSQE/GUI/GuiWindowMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public override void OnButtonClicked(int id)
var dialog = new OpenFileDialog()
{
Title = "Select Map File",
Filter = "Text Documents (*.txt;*.sspm)|*.txt;*.sspm",
Filter = "Beatmap Files (*.txt;*.sspm;*.osu)|*.txt;*.sspm;*.osu",
};

if (Settings.settings["defaultPath"] != "")
Expand Down
78 changes: 78 additions & 0 deletions Editor/New SSQE/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,12 @@ public bool LoadMap(string pathOrData, bool file = false, bool autosave = false)
file = false;
FileName = null;
}
else if (file && Path.GetExtension(pathOrData) == ".osu")
{
pathOrData = RunOSUImport(pathOrData);
file = false;
FileName = null;
}
if (pathOrData == "")
return false;

Expand Down Expand Up @@ -2596,5 +2602,77 @@ void SetField(string field, dynamic value)

return mapData;
}

public string RunOSUImport(string path)
{
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ".";

string data = File.ReadAllText(path);
string id = "";
string mapData = "";

string[] split = data.Split("\n");

bool timing = false;
bool hitObj = false;

for (int i = 0; i < split.Length; i++)
{
try
{
string line = split[i].Trim();
string[] subsplit = line.Split(":");
string[] set = line.Split(",");

if (!timing && !hitObj && subsplit.FirstOrDefault() == "AudioFilename")
{
string idPath = subsplit[1].Trim();
id = Path.GetFileNameWithoutExtension(idPath);

File.Copy($"{Path.GetDirectoryName(path)}\\{idPath}", $"cached/{id}.asset", true);
}

if (timing && !string.IsNullOrWhiteSpace(line) && set.Length > 1)
{
bool canParse = double.TryParse(set[0], NumberStyles.Any, culture, out double time);
canParse &= float.TryParse(set[1], NumberStyles.Any, culture, out float bpm);

if (canParse)
{
bool inhereted = set.Length > 6 ? set[6] == "1" : bpm > 0;

bpm = (float)Math.Abs(Math.Round(60000 / bpm, 3));

if (bpm > 0 && inhereted)
TimingPoints.Add(new(bpm, (long)time));
}
}

if (hitObj && !string.IsNullOrWhiteSpace(line) && set.Length > 3)
{
bool canParse = float.TryParse(set[0], NumberStyles.Any, culture, out float x);
canParse &= float.TryParse(set[1], NumberStyles.Any, culture, out float y);
canParse &= double.TryParse(set[2], NumberStyles.Any, culture, out double time);
canParse &= int.TryParse(set[3], NumberStyles.Any, culture, out int type);

if (canParse)
{
x = 5 - x / 64;
y = 4 - y / 64;

if ((type & 1) != 0)
mapData += $",{Math.Round(x, 2)}|{Math.Round(y, 2)}|{(long)time}";
}
}

timing = line != "[HitObjects]" && (timing || line == "[TimingPoints]");
hitObj = line != "[TimingPoints]" && (hitObj || line == "[HitObjects]");
}
catch { }
}

return id + mapData;
}
}
}

0 comments on commit 9e157a5

Please sign in to comment.