This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OsuHelper.cs
105 lines (94 loc) · 3.81 KB
/
OsuHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.IO;
using CustomBeatmaps.Packages;
namespace CustomBeatmaps
{
public static class OsuHelper
{
public static bool GetOsuBeatmaps(string path, out string[] beatmapPaths)
{
path = GetOsuPath(path);
if (Directory.Exists(path))
{
List<string> beatmaps = new List<string>();
foreach (string osuProjectDir in Directory.EnumerateDirectories(path))
{
foreach (string file in Directory.EnumerateFiles(osuProjectDir))
{
if (file.EndsWith(".osu"))
{
beatmaps.Add(file);
}
}
}
double TimeSinceLastWrite(string filename)
{
return (DateTime.Now - File.GetLastWriteTime(filename)).TotalSeconds;
}
// Sort by newest access
beatmaps.Sort((left, right) => Math.Sign(TimeSinceLastWrite(left) - TimeSinceLastWrite(right)));
beatmapPaths = beatmaps.ToArray();
return true;
}
beatmapPaths = new string[0];
return false;
}
public static string GetOsuPath(string overridePath)
{
if (string.IsNullOrEmpty(overridePath))
{
return Path.GetFullPath(Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData).Replace('\\', '/'), "../Local/osu!/Songs"));
}
return overridePath;
}
private static string GetPackageNameFromOsu(string osuPath)
{
string title = "(undefined)";
foreach (string fpath in Directory.EnumerateFiles(osuPath))
{
if (fpath.EndsWith(".osu"))
{
string text = File.ReadAllText(fpath);
title = PackageGrabberUtils.GetBeatmapProp(text, "Title", fpath);
}
}
return $"LOCAL_{title}";
}
public static string CreateExportZipFile(string osuPath, string temporaryFolderLocation)
{
if (Directory.Exists(temporaryFolderLocation))
{
Directory.Delete(temporaryFolderLocation, true);
}
Directory.CreateDirectory(temporaryFolderLocation);
string packageName = GetPackageNameFromOsu(osuPath);
string filesLocation = $"{temporaryFolderLocation}/{packageName}";
Directory.CreateDirectory(filesLocation);
List<string> beatmaps = new List<string>();
// Copy over the files
foreach (string fpath in Directory.EnumerateFiles(osuPath))
{
string fname = Path.GetFileName(fpath);
File.Copy(fpath, $"{filesLocation}/{fname}");
if (fpath.EndsWith(".osu"))
{
beatmaps.Add(fpath);
}
}
// Create an info.json
CustomPackageInfo info =
OldModConverter.GeneratePackageInfoFor(packageName, beatmaps.ToArray(), packageName);
CustomPackageInfo.Save(info, $"{filesLocation}/info.json");
// Zip
string zipTarget = $"{packageName}.zip";
// Remove LOCAL_ just... to make it a bit more neat.
if (zipTarget.StartsWith("LOCAL_")) zipTarget = zipTarget.Substring("LOCAL_".Length);
System.IO.Compression.ZipFile.CreateFromDirectory(temporaryFolderLocation, zipTarget);
// Delete temporary directory afterwards
Directory.Delete(temporaryFolderLocation, true);
return zipTarget;
}
}
}