Пример #1
0
        public void PerformRead(Action <T> perform)
        {
            if (!IsManaged)
            {
                perform(data);
                return;
            }

            using (var realm = realmFactory.CreateContext())
                perform(realm.Find <T>(ID));
        }
Пример #2
0
        private void migrateSkins(DatabaseWriteUsage db)
        {
            // can be removed 20220530.
            var existingSkins = db.Context.SkinInfo
                                .Include(s => s.Files)
                                .ThenInclude(f => f.FileInfo)
                                .ToList();

            // previous entries in EF are removed post migration.
            if (!existingSkins.Any())
            {
                return;
            }

            var userSkinChoice = config.GetBindable <string>(OsuSetting.Skin);

            int.TryParse(userSkinChoice.Value, out int userSkinInt);

            switch (userSkinInt)
            {
            case EFSkinInfo.DEFAULT_SKIN:
                userSkinChoice.Value = SkinInfo.DEFAULT_SKIN.ToString();
                break;

            case EFSkinInfo.CLASSIC_SKIN:
                userSkinChoice.Value = SkinInfo.CLASSIC_SKIN.ToString();
                break;
            }

            using (var realm = realmContextFactory.CreateContext())
                using (var transaction = realm.BeginWrite())
                {
                    // only migrate data if the realm database is empty.
                    // note that this cannot be written as: `realm.All<SkinInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
                    if (!realm.All <SkinInfo>().Any(s => !s.Protected))
                    {
                        foreach (var skin in existingSkins)
                        {
                            var realmSkin = new SkinInfo
                            {
                                Name              = skin.Name,
                                Creator           = skin.Creator,
                                Hash              = skin.Hash,
                                Protected         = false,
                                InstantiationInfo = skin.InstantiationInfo,
                            };

                            foreach (var file in skin.Files)
                            {
                                var realmFile = realm.Find <RealmFile>(file.FileInfo.Hash);

                                if (realmFile == null)
                                {
                                    realm.Add(realmFile = new RealmFile {
                                        Hash = file.FileInfo.Hash
                                    });
                                }

                                realmSkin.Files.Add(new RealmNamedFileUsage(realmFile, file.Filename));
                            }

                            realm.Add(realmSkin);

                            if (skin.ID == userSkinInt)
                            {
                                userSkinChoice.Value = realmSkin.ID.ToString();
                            }
                        }
                    }

                    db.Context.RemoveRange(existingSkins);
                    // Intentionally don't clean up the files, so they don't get purged by EF.

                    transaction.Commit();
                }
        }