Skip to content

Commit

Permalink
downgrade realm, set realm to readonly, and add try catch for import
Browse files Browse the repository at this point in the history
  • Loading branch information
hrfarmer committed Oct 24, 2024
1 parent d3262a8 commit 51ce354
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 66 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"graceful-fs": "^4.2.11",
"lucide-solid": "^0.452.0",
"node-addon-api": "^8.2.1",
"realm": "^12.13.1",
"realm": "~12.6.2",
"remixicon": "^4.3.0",
"sharp": "^0.33.5",
"solid-focus-trap": "^0.1.7",
Expand Down
134 changes: 76 additions & 58 deletions src/main/lib/osu-file-parser/OsuParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ export class OsuParser {
): DirParseResult {
const currentDir = databasePath.replaceAll("\\", "/");

const realm = await Realm.open({ path: currentDir + "/client.realm" });
const realm = await Realm.open({
path: currentDir + "/client.realm",
readOnly: true,
schemaVersion: 23,
});
const beatmapSets = realm.objects<BeatmapSet>("BeatmapSet");

const songTable = new Map<ResourceID, Song>();
Expand All @@ -143,67 +147,81 @@ export class OsuParser {

let i = 0;
for (const beatmapSet of beatmapSets) {
const beatmaps = removeUnoriginalBeatmaps(beatmapSet.Beatmaps);

for (const beatmap of beatmaps) {
const song: Song = {
audio: "",
osuFile: "",
path: "",
ctime: "",
dateAdded: beatmapSet.DateAdded,
title: beatmap.Metadata.Title,
artist: beatmap.Metadata.Artist,
creator: beatmap.Metadata.Author.Username,
bpm: [],
duration: beatmap.Length,
diffs: [beatmap.DifficultyName],
};

song.osuFile =
currentDir +
"/files/" +
beatmap.Hash[0] +
"/" +
beatmap.Hash.substring(0, 2) +
"/" +
beatmap.Hash;

const songHash = beatmapSet.Files.find(
(file) => file.Filename === beatmap.Metadata.AudioFile,
)?.File.Hash as string; // the mp3 should exist, will check if its possible for it to not in lazer

song.audio =
currentDir + "/files/" + songHash[0] + "/" + songHash.substring(0, 2) + "/" + songHash;

/* Note: in lots of places throughout the application, it relies on the song.path parameter, which in the
try {
const beatmaps = removeUnoriginalBeatmaps(beatmapSet.Beatmaps);

for (const beatmap of beatmaps) {
try {
const song: Song = {
audio: "",
osuFile: "",
path: "",
ctime: "",
dateAdded: beatmapSet.DateAdded,
title: beatmap.Metadata.Title,
artist: beatmap.Metadata.Artist,
creator: beatmap.Metadata.Author.Username,
bpm: [],
duration: beatmap.Length,
diffs: [beatmap.DifficultyName],
};

song.osuFile =
currentDir +
"/files/" +
beatmap.Hash[0] +
"/" +
beatmap.Hash.substring(0, 2) +
"/" +
beatmap.Hash;

const songHash = beatmapSet.Files.find(
(file) => file.Filename === beatmap.Metadata.AudioFile,
)?.File.Hash as string; // the mp3 should exist, will check if its possible for it to not in lazer

song.audio =
currentDir +
"/files/" +
songHash[0] +
"/" +
songHash.substring(0, 2) +
"/" +
songHash;

/* Note: in lots of places throughout the application, it relies on the song.path parameter, which in the
stable parser is the path of the folder that holds all the files. This folder doesn't exist in lazer's
file structure, so for now I'm just passing the audio location as the path parameter. In initial testing
this doesn't seem to break anything but just leaving this note in case it does */
song.path = song.audio;

if (beatmap.Metadata.BackgroundFile) {
const bgHash = beatmapSet.Files.find(
(file) => file.Filename === beatmap.Metadata.BackgroundFile,
)?.File.Hash as string;

song.bg =
currentDir + "/files/" + bgHash[0] + "/" + bgHash.substring(0, 2) + "/" + bgHash;
}

song.beatmapSetID = beatmapSet.OnlineID;

songTable.set(song.audio, song);
audioTable.set(song.audio, {
songID: song.audio,
path: song.audio,
ctime: String(beatmapSet.DateAdded),
});

if (update) {
update(i + 1, beatmapSets.length, song.title);
i++;
song.path = song.audio;

if (beatmap.Metadata.BackgroundFile) {
const bgHash = beatmapSet.Files.find(
(file) => file.Filename === beatmap.Metadata.BackgroundFile,
)?.File.Hash as string;

song.bg =
currentDir + "/files/" + bgHash[0] + "/" + bgHash.substring(0, 2) + "/" + bgHash;
}

song.beatmapSetID = beatmapSet.OnlineID;

songTable.set(song.audio, song);
audioTable.set(song.audio, {
songID: song.audio,
path: song.audio,
ctime: String(beatmapSet.DateAdded),
});

if (update) {
update(i + 1, beatmapSets.length, song.title);
i++;
}
} catch (err) {
console.error("Error while parsing beatmap: ", err);
}
}
} catch (err) {
console.error("Error while parsing beatmapset: ", err);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/router/dir-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ Router.respond("dir::autoGetOsuDirs", () => {
});
}

if (process.env.HOME != undefined && fs.existsSync(path.join(process.env.HOME, "osu"))) {
dirs.push({ version: "lazer", path: path.join(process.env.HOME, "osu") });
const homePath = process.env.XDG_DATA_HOME ?? `${process.env.HOME}/.local/share`;

if (homePath != undefined && fs.existsSync(path.join(homePath, "osu"))) {
dirs.push({ version: "lazer", path: path.join(homePath, "osu") });
}

if (dirs.length > 0) {
Expand Down

0 comments on commit 51ce354

Please sign in to comment.