Something went wrong. Try again.
osu!lazer maps/scores/settings bidirectional diff/export/sync tool
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491module Osync.Realm
open Systemopen System.Runtime.InteropServicesopen Realms
let getOsuDataPath () = if RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then IO.Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "osu" ) else IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "share", "osu")
let findRealmPath (dataPath: string) : string = let clientRealm = IO.Path.Combine(dataPath, "client.realm")
let tryParseVersion (path: string) = let name = IO.Path.GetFileNameWithoutExtension(path) let suffix = name.Substring("client_".Length)
match Int32.TryParse(suffix) with | true, v -> Some v | _ -> None
let versioned = IO.Directory.GetFiles(dataPath, "client_*.realm") |> Array.choose (fun path -> tryParseVersion path |> Option.map (fun v -> (v, path))) |> Array.sortByDescending fst |> Array.tryHead |> Option.map snd
versioned |> Option.defaultValue clientRealm
let openRealm (dataPath: string) = let realmPath = findRealmPath dataPath let config = RealmConfiguration(realmPath) config.IsDynamic <- true Realm.GetInstance(config)
let openRealmAt (path: string) = let config = RealmConfiguration(path) config.IsDynamic <- true Realm.GetInstance(config)
// --- Lightweight reads (IDs only) ---
let beatmapSetIdsFrom (realm: Realm) : Set<int> = realm.DynamicApi.All("BeatmapSet") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending")) && obj.DynamicApi.Get<int>("OnlineID") > 0) |> Seq.map (fun obj -> obj.DynamicApi.Get<int>("OnlineID")) |> Set.ofSeq
let skinIdentifiersFrom (realm: Realm) : Map<string, string> = realm.DynamicApi.All("Skin") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending")) && not (obj.DynamicApi.Get<bool>("Protected"))) |> Seq.map (fun obj -> let hash = obj.DynamicApi.Get<string>("Hash") let name = obj.DynamicApi.Get<string>("Name") (hash, name)) |> Map.ofSeq
let scoreHashesFrom (realm: Realm) : Set<string> = realm.DynamicApi.All("Score") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending"))) |> Seq.map (fun obj -> obj.DynamicApi.Get<string>("Hash")) |> Set.ofSeq
// --- Intermediate types for full data transfer ---
type FileRef = { Hash: string; Filename: string }
type UserData = { OnlineID: int Username: string CountryCode: string }
type MetadataData = { Title: string TitleUnicode: string Artist: string ArtistUnicode: string Source: string Tags: string PreviewTime: int AudioFile: string BackgroundFile: string Author: UserData }
type DifficultyData = { DrainRate: float32 CircleSize: float32 OverallDifficulty: float32 ApproachRate: float32 SliderMultiplier: float SliderTickRate: float }
type BeatmapData = { Id: Guid DifficultyName: string RulesetShortName: string Status: int OnlineID: int Length: float BPM: float Hash: string StarRating: float MD5Hash: string OnlineMD5Hash: string Hidden: bool EndTimeObjectCount: int TotalObjectCount: int BeatDivisor: int EditorTimestamp: Nullable<float> LastLocalUpdate: Nullable<DateTimeOffset> LastOnlineUpdate: Nullable<DateTimeOffset> LastPlayed: Nullable<DateTimeOffset> Metadata: MetadataData Difficulty: DifficultyData Offset: float }
type BeatmapSetData = { Id: Guid OnlineID: int DateAdded: DateTimeOffset DateSubmitted: Nullable<DateTimeOffset> DateRanked: Nullable<DateTimeOffset> Status: int Hash: string Files: FileRef list Beatmaps: BeatmapData list }
type SkinData = { Id: Guid Name: string Creator: string InstantiationInfo: string Hash: string Files: FileRef list }
type ScoreData = { Id: Guid BeatmapHash: string ClientVersion: string RulesetShortName: string Hash: string TotalScore: int64 TotalScoreWithoutMods: int64 TotalScoreVersion: int LegacyTotalScore: Nullable<int64> BackgroundReprocessingFailed: bool MaxCombo: int Accuracy: float Date: DateTimeOffset PP: Nullable<float> OnlineID: int64 LegacyOnlineID: int64 Rank: int IsLegacyScore: bool Combo: int User: UserData ModsJson: string StatisticsJson: string MaximumStatisticsJson: string Files: FileRef list }
// --- Full reads ---
let private readFileRefs (obj: IRealmObjectBase) : FileRef list = obj.DynamicApi.GetList<IEmbeddedObject>("Files") |> Seq.map (fun f -> let file = f.DynamicApi.Get<IRealmObject>("File")
{ Hash = file.DynamicApi.Get<string>("Hash") Filename = f.DynamicApi.Get<string>("Filename") }) |> Seq.toList
let private readUser (obj: IEmbeddedObject) : UserData = { OnlineID = obj.DynamicApi.Get<int>("OnlineID") Username = obj.DynamicApi.Get<string>("Username") CountryCode = obj.DynamicApi.Get<string>("CountryCode") }
let private readMetadata (obj: IRealmObjectBase) : MetadataData = { Title = obj.DynamicApi.Get<string>("Title") TitleUnicode = obj.DynamicApi.Get<string>("TitleUnicode") Artist = obj.DynamicApi.Get<string>("Artist") ArtistUnicode = obj.DynamicApi.Get<string>("ArtistUnicode") Source = obj.DynamicApi.Get<string>("Source") Tags = obj.DynamicApi.Get<string>("Tags") PreviewTime = obj.DynamicApi.Get<int>("PreviewTime") AudioFile = obj.DynamicApi.Get<string>("AudioFile") BackgroundFile = obj.DynamicApi.Get<string>("BackgroundFile") Author = readUser (obj.DynamicApi.Get<IEmbeddedObject>("Author")) }
let private readDifficulty (obj: IEmbeddedObject) : DifficultyData = { DrainRate = obj.DynamicApi.Get<float32>("DrainRate") CircleSize = obj.DynamicApi.Get<float32>("CircleSize") OverallDifficulty = obj.DynamicApi.Get<float32>("OverallDifficulty") ApproachRate = obj.DynamicApi.Get<float32>("ApproachRate") SliderMultiplier = obj.DynamicApi.Get<float>("SliderMultiplier") SliderTickRate = obj.DynamicApi.Get<float>("SliderTickRate") }
let private readBeatmap (obj: IRealmObject) : BeatmapData = let ruleset = obj.DynamicApi.Get<IRealmObject>("Ruleset")
{ Id = obj.DynamicApi.Get<Guid>("ID") DifficultyName = obj.DynamicApi.Get<string>("DifficultyName") RulesetShortName = ruleset.DynamicApi.Get<string>("ShortName") Status = obj.DynamicApi.Get<int>("Status") OnlineID = obj.DynamicApi.Get<int>("OnlineID") Length = obj.DynamicApi.Get<float>("Length") BPM = obj.DynamicApi.Get<float>("BPM") Hash = obj.DynamicApi.Get<string>("Hash") StarRating = obj.DynamicApi.Get<float>("StarRating") MD5Hash = obj.DynamicApi.Get<string>("MD5Hash") OnlineMD5Hash = obj.DynamicApi.Get<string>("OnlineMD5Hash") Hidden = obj.DynamicApi.Get<bool>("Hidden") EndTimeObjectCount = obj.DynamicApi.Get<int>("EndTimeObjectCount") TotalObjectCount = obj.DynamicApi.Get<int>("TotalObjectCount") BeatDivisor = obj.DynamicApi.Get<int>("BeatDivisor") EditorTimestamp = obj.DynamicApi.Get<Nullable<float>>("EditorTimestamp") LastLocalUpdate = obj.DynamicApi.Get<Nullable<DateTimeOffset>>("LastLocalUpdate") LastOnlineUpdate = obj.DynamicApi.Get<Nullable<DateTimeOffset>>("LastOnlineUpdate") LastPlayed = obj.DynamicApi.Get<Nullable<DateTimeOffset>>("LastPlayed") Metadata = readMetadata (obj.DynamicApi.Get<IRealmObject>("Metadata")) Difficulty = readDifficulty (obj.DynamicApi.Get<IEmbeddedObject>("Difficulty")) Offset = let us = obj.DynamicApi.Get<IEmbeddedObject>("UserSettings") us.DynamicApi.Get<float>("Offset") }
let readBeatmapSets (realm: Realm) (onlineIds: Set<int>) : BeatmapSetData list = realm.DynamicApi.All("BeatmapSet") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending")) && Set.contains (obj.DynamicApi.Get<int>("OnlineID")) onlineIds) |> Seq.map (fun obj -> { Id = obj.DynamicApi.Get<Guid>("ID") OnlineID = obj.DynamicApi.Get<int>("OnlineID") DateAdded = obj.DynamicApi.Get<DateTimeOffset>("DateAdded") DateSubmitted = obj.DynamicApi.Get<Nullable<DateTimeOffset>>("DateSubmitted") DateRanked = obj.DynamicApi.Get<Nullable<DateTimeOffset>>("DateRanked") Status = obj.DynamicApi.Get<int>("Status") Hash = obj.DynamicApi.Get<string>("Hash") Files = readFileRefs obj Beatmaps = obj.DynamicApi.GetList<IRealmObject>("Beatmaps") |> Seq.map readBeatmap |> Seq.toList }) |> Seq.toList
let readSkins (realm: Realm) (hashes: Set<string>) : SkinData list = realm.DynamicApi.All("Skin") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending")) && not (obj.DynamicApi.Get<bool>("Protected")) && Set.contains (obj.DynamicApi.Get<string>("Hash")) hashes) |> Seq.map (fun obj -> { Id = obj.DynamicApi.Get<Guid>("ID") Name = obj.DynamicApi.Get<string>("Name") Creator = obj.DynamicApi.Get<string>("Creator") InstantiationInfo = obj.DynamicApi.Get<string>("InstantiationInfo") Hash = obj.DynamicApi.Get<string>("Hash") Files = readFileRefs obj }) |> Seq.toList
let readScores (realm: Realm) (hashes: Set<string>) : ScoreData list = realm.DynamicApi.All("Score") |> Seq.filter (fun obj -> not (obj.DynamicApi.Get<bool>("DeletePending")) && Set.contains (obj.DynamicApi.Get<string>("Hash")) hashes) |> Seq.map (fun obj -> let ruleset = obj.DynamicApi.Get<IRealmObject>("Ruleset") let user = obj.DynamicApi.Get<IEmbeddedObject>("User")
{ Id = obj.DynamicApi.Get<Guid>("ID") BeatmapHash = obj.DynamicApi.Get<string>("BeatmapHash") ClientVersion = obj.DynamicApi.Get<string>("ClientVersion") RulesetShortName = ruleset.DynamicApi.Get<string>("ShortName") Hash = obj.DynamicApi.Get<string>("Hash") TotalScore = obj.DynamicApi.Get<int64>("TotalScore") TotalScoreWithoutMods = obj.DynamicApi.Get<int64>("TotalScoreWithoutMods") TotalScoreVersion = obj.DynamicApi.Get<int>("TotalScoreVersion") LegacyTotalScore = obj.DynamicApi.Get<Nullable<int64>>("LegacyTotalScore") BackgroundReprocessingFailed = obj.DynamicApi.Get<bool>("BackgroundReprocessingFailed") MaxCombo = obj.DynamicApi.Get<int>("MaxCombo") Accuracy = obj.DynamicApi.Get<float>("Accuracy") Date = obj.DynamicApi.Get<DateTimeOffset>("Date") PP = obj.DynamicApi.Get<Nullable<float>>("PP") OnlineID = obj.DynamicApi.Get<int64>("OnlineID") LegacyOnlineID = obj.DynamicApi.Get<int64>("LegacyOnlineID") Rank = obj.DynamicApi.Get<int>("Rank") IsLegacyScore = obj.DynamicApi.Get<bool>("IsLegacyScore") Combo = obj.DynamicApi.Get<int>("Combo") User = readUser user ModsJson = obj.DynamicApi.Get<string>("Mods") StatisticsJson = obj.DynamicApi.Get<string>("Statistics") MaximumStatisticsJson = obj.DynamicApi.Get<string>("MaximumStatistics") Files = readFileRefs obj }) |> Seq.toList
// --- Writes ---
let private ensureRealmFile (realm: Realm) (hash: string) : IRealmObjectBase = match realm.DynamicApi.Find("File", hash) with | null -> realm.DynamicApi.CreateObject("File", hash) | existing -> existing
let private writeFileRefs (realm: Realm) (parent: IRealmObjectBase) (files: FileRef list) = let list = parent.DynamicApi.GetList<IEmbeddedObject>("Files")
for f in files do let realmFile = ensureRealmFile realm f.Hash let usage = realm.DynamicApi.AddEmbeddedObjectToList(list) usage.DynamicApi.Set("File", RealmValue.Object(realmFile)) usage.DynamicApi.Set("Filename", f.Filename)
let private writeUser (realm: Realm) (parent: IRealmObjectBase) (propName: string) (user: UserData) = let obj = realm.DynamicApi.CreateEmbeddedObjectForProperty(parent, propName) obj.DynamicApi.Set("OnlineID", user.OnlineID) obj.DynamicApi.Set("Username", user.Username) obj.DynamicApi.Set("CountryCode", user.CountryCode)
let private writeMetadata (realm: Realm) (parent: IRealmObjectBase) (m: MetadataData) = let obj = realm.DynamicApi.CreateObject("BeatmapMetadata") obj.DynamicApi.Set("Title", m.Title) obj.DynamicApi.Set("TitleUnicode", m.TitleUnicode) obj.DynamicApi.Set("Artist", m.Artist) obj.DynamicApi.Set("ArtistUnicode", m.ArtistUnicode) obj.DynamicApi.Set("Source", m.Source) obj.DynamicApi.Set("Tags", m.Tags) obj.DynamicApi.Set("PreviewTime", m.PreviewTime) obj.DynamicApi.Set("AudioFile", m.AudioFile) obj.DynamicApi.Set("BackgroundFile", m.BackgroundFile) writeUser realm obj "Author" m.Author parent.DynamicApi.Set("Metadata", RealmValue.Object(obj))
let private writeDifficulty (realm: Realm) (parent: IRealmObjectBase) (d: DifficultyData) = let obj = realm.DynamicApi.CreateEmbeddedObjectForProperty(parent, "Difficulty") obj.DynamicApi.Set("DrainRate", d.DrainRate) obj.DynamicApi.Set("CircleSize", d.CircleSize) obj.DynamicApi.Set("OverallDifficulty", d.OverallDifficulty) obj.DynamicApi.Set("ApproachRate", d.ApproachRate) obj.DynamicApi.Set("SliderMultiplier", d.SliderMultiplier) obj.DynamicApi.Set("SliderTickRate", d.SliderTickRate)
let private writeUserSettings (realm: Realm) (parent: IRealmObjectBase) (offset: float) = let obj = realm.DynamicApi.CreateEmbeddedObjectForProperty(parent, "UserSettings") obj.DynamicApi.Set("Offset", offset)
let private writeBeatmap (realm: Realm) (setObj: IRealmObjectBase) (b: BeatmapData) = let list = setObj.DynamicApi.GetList<IRealmObject>("Beatmaps") let obj = realm.DynamicApi.CreateObject("Beatmap", Nullable(b.Id)) obj.DynamicApi.Set("DifficultyName", b.DifficultyName) obj.DynamicApi.Set("Status", b.Status) obj.DynamicApi.Set("OnlineID", b.OnlineID) obj.DynamicApi.Set("Length", b.Length) obj.DynamicApi.Set("BPM", b.BPM) obj.DynamicApi.Set("Hash", b.Hash) obj.DynamicApi.Set("StarRating", b.StarRating) obj.DynamicApi.Set("MD5Hash", b.MD5Hash) obj.DynamicApi.Set("OnlineMD5Hash", b.OnlineMD5Hash) obj.DynamicApi.Set("Hidden", b.Hidden) obj.DynamicApi.Set("EndTimeObjectCount", b.EndTimeObjectCount) obj.DynamicApi.Set("TotalObjectCount", b.TotalObjectCount) obj.DynamicApi.Set("BeatDivisor", b.BeatDivisor)
if b.EditorTimestamp.HasValue then obj.DynamicApi.Set("EditorTimestamp", b.EditorTimestamp.Value)
if b.LastLocalUpdate.HasValue then obj.DynamicApi.Set("LastLocalUpdate", b.LastLocalUpdate.Value)
if b.LastOnlineUpdate.HasValue then obj.DynamicApi.Set("LastOnlineUpdate", b.LastOnlineUpdate.Value)
if b.LastPlayed.HasValue then obj.DynamicApi.Set("LastPlayed", b.LastPlayed.Value)
let ruleset = realm.DynamicApi.Find("Ruleset", b.RulesetShortName)
if ruleset <> null then obj.DynamicApi.Set("Ruleset", RealmValue.Object(ruleset))
obj.DynamicApi.Set("BeatmapSet", RealmValue.Object(setObj))
writeMetadata realm obj b.Metadata writeDifficulty realm obj b.Difficulty writeUserSettings realm obj b.Offset
list.Add(obj :?> IRealmObject)
let writeBeatmapSets (realm: Realm) (sets: BeatmapSetData list) = realm.Write(fun () -> for s in sets do let obj = realm.DynamicApi.CreateObject("BeatmapSet", Nullable(s.Id)) obj.DynamicApi.Set("OnlineID", s.OnlineID) obj.DynamicApi.Set("DateAdded", s.DateAdded)
if s.DateSubmitted.HasValue then obj.DynamicApi.Set("DateSubmitted", s.DateSubmitted.Value)
if s.DateRanked.HasValue then obj.DynamicApi.Set("DateRanked", s.DateRanked.Value)
obj.DynamicApi.Set("Status", s.Status) obj.DynamicApi.Set("Hash", s.Hash) obj.DynamicApi.Set("DeletePending", false) obj.DynamicApi.Set("Protected", false)
writeFileRefs realm obj s.Files
for b in s.Beatmaps do writeBeatmap realm obj b)
let writeSkins (realm: Realm) (skins: SkinData list) = realm.Write(fun () -> for s in skins do let obj = realm.DynamicApi.CreateObject("Skin", Nullable(s.Id)) obj.DynamicApi.Set("Name", s.Name) obj.DynamicApi.Set("Creator", s.Creator) obj.DynamicApi.Set("InstantiationInfo", s.InstantiationInfo) obj.DynamicApi.Set("Hash", s.Hash) obj.DynamicApi.Set("DeletePending", false) obj.DynamicApi.Set("Protected", false)
writeFileRefs realm obj s.Files)
let writeScores (realm: Realm) (scores: ScoreData list) = realm.Write(fun () -> let beatmapByHash = realm.DynamicApi.All("Beatmap") |> Seq.map (fun b -> b.DynamicApi.Get<string>("Hash"), b) |> dict
for s in scores do let obj = realm.DynamicApi.CreateObject("Score", Nullable(s.Id)) obj.DynamicApi.Set("BeatmapHash", s.BeatmapHash) obj.DynamicApi.Set("ClientVersion", s.ClientVersion) obj.DynamicApi.Set("Hash", s.Hash) obj.DynamicApi.Set("TotalScore", s.TotalScore) obj.DynamicApi.Set("TotalScoreWithoutMods", s.TotalScoreWithoutMods) obj.DynamicApi.Set("TotalScoreVersion", s.TotalScoreVersion)
if s.LegacyTotalScore.HasValue then obj.DynamicApi.Set("LegacyTotalScore", s.LegacyTotalScore.Value)
obj.DynamicApi.Set("BackgroundReprocessingFailed", s.BackgroundReprocessingFailed) obj.DynamicApi.Set("MaxCombo", s.MaxCombo) obj.DynamicApi.Set("Accuracy", s.Accuracy) obj.DynamicApi.Set("Date", s.Date)
if s.PP.HasValue then obj.DynamicApi.Set("PP", s.PP.Value)
obj.DynamicApi.Set("OnlineID", s.OnlineID) obj.DynamicApi.Set("LegacyOnlineID", s.LegacyOnlineID) obj.DynamicApi.Set("Rank", s.Rank) obj.DynamicApi.Set("IsLegacyScore", s.IsLegacyScore) obj.DynamicApi.Set("Combo", s.Combo) obj.DynamicApi.Set("Mods", s.ModsJson) obj.DynamicApi.Set("Statistics", s.StatisticsJson) obj.DynamicApi.Set("MaximumStatistics", s.MaximumStatisticsJson) obj.DynamicApi.Set("DeletePending", false)
let ruleset = realm.DynamicApi.Find("Ruleset", s.RulesetShortName)
if ruleset <> null then obj.DynamicApi.Set("Ruleset", RealmValue.Object(ruleset))
match beatmapByHash.TryGetValue(s.BeatmapHash) with | true, b -> obj.DynamicApi.Set("BeatmapInfo", RealmValue.Object(b)) | _ -> ()
writeUser realm obj "User" s.User writeFileRefs realm obj s.Files)
let collectFileHashes (sets: BeatmapSetData list) (skins: SkinData list) (scores: ScoreData list) : Set<string> = [ sets |> List.collect (fun s -> s.Files) skins |> List.collect (fun s -> s.Files) scores |> List.collect (fun s -> s.Files) ] |> List.concat |> List.map (fun f -> f.Hash) |> Set.ofList