mirror of
https://github.com/osukey/osukey.git
synced 2025-08-08 00:53:56 +09:00
Move RealmRulesetStore
initial work off incorrect thread
This commit is contained in:
@ -102,75 +102,78 @@ namespace osu.Game.Stores
|
|||||||
|
|
||||||
private void addMissingRulesets()
|
private void addMissingRulesets()
|
||||||
{
|
{
|
||||||
realmFactory.Context.Write(realm =>
|
using (var context = realmFactory.CreateContext())
|
||||||
{
|
{
|
||||||
var rulesets = realm.All<RealmRuleset>();
|
context.Write(realm =>
|
||||||
|
|
||||||
List<Ruleset> instances = loadedAssemblies.Values
|
|
||||||
.Select(r => Activator.CreateInstance(r) as Ruleset)
|
|
||||||
.Where(r => r != null)
|
|
||||||
.Select(r => r.AsNonNull())
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// add all legacy rulesets first to ensure they have exclusive choice of primary key.
|
|
||||||
foreach (var r in instances.Where(r => r is ILegacyRuleset))
|
|
||||||
{
|
{
|
||||||
if (realm.All<RealmRuleset>().FirstOrDefault(rr => rr.OnlineID == r.RulesetInfo.ID) == null)
|
var rulesets = realm.All<RealmRuleset>();
|
||||||
realm.Add(new RealmRuleset(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.ID));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add any other rulesets which have assemblies present but are not yet in the database.
|
List<Ruleset> instances = loadedAssemblies.Values
|
||||||
foreach (var r in instances.Where(r => !(r is ILegacyRuleset)))
|
.Select(r => Activator.CreateInstance(r) as Ruleset)
|
||||||
{
|
.Where(r => r != null)
|
||||||
if (rulesets.FirstOrDefault(ri => ri.InstantiationInfo.Equals(r.RulesetInfo.InstantiationInfo, StringComparison.Ordinal)) == null)
|
.Select(r => r.AsNonNull())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// add all legacy rulesets first to ensure they have exclusive choice of primary key.
|
||||||
|
foreach (var r in instances.Where(r => r is ILegacyRuleset))
|
||||||
{
|
{
|
||||||
var existingSameShortName = rulesets.FirstOrDefault(ri => ri.ShortName == r.RulesetInfo.ShortName);
|
if (realm.All<RealmRuleset>().FirstOrDefault(rr => rr.OnlineID == r.RulesetInfo.ID) == null)
|
||||||
|
|
||||||
if (existingSameShortName != null)
|
|
||||||
{
|
|
||||||
// even if a matching InstantiationInfo was not found, there may be an existing ruleset with the same ShortName.
|
|
||||||
// this generally means the user or ruleset provider has renamed their dll but the underlying ruleset is *likely* the same one.
|
|
||||||
// in such cases, update the instantiation info of the existing entry to point to the new one.
|
|
||||||
existingSameShortName.InstantiationInfo = r.RulesetInfo.InstantiationInfo;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
realm.Add(new RealmRuleset(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.ID));
|
realm.Add(new RealmRuleset(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.ID));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
List<RealmRuleset> detachedRulesets = new List<RealmRuleset>();
|
// add any other rulesets which have assemblies present but are not yet in the database.
|
||||||
|
foreach (var r in instances.Where(r => !(r is ILegacyRuleset)))
|
||||||
// perform a consistency check and detach final rulesets from realm for cross-thread runtime usage.
|
|
||||||
foreach (var r in rulesets)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var type = Type.GetType(r.InstantiationInfo);
|
if (rulesets.FirstOrDefault(ri => ri.InstantiationInfo.Equals(r.RulesetInfo.InstantiationInfo, StringComparison.Ordinal)) == null)
|
||||||
|
{
|
||||||
|
var existingSameShortName = rulesets.FirstOrDefault(ri => ri.ShortName == r.RulesetInfo.ShortName);
|
||||||
|
|
||||||
if (type == null)
|
if (existingSameShortName != null)
|
||||||
throw new InvalidOperationException(@"Type resolution failure.");
|
{
|
||||||
|
// even if a matching InstantiationInfo was not found, there may be an existing ruleset with the same ShortName.
|
||||||
var rInstance = (Activator.CreateInstance(type) as Ruleset)?.RulesetInfo;
|
// this generally means the user or ruleset provider has renamed their dll but the underlying ruleset is *likely* the same one.
|
||||||
|
// in such cases, update the instantiation info of the existing entry to point to the new one.
|
||||||
if (rInstance == null)
|
existingSameShortName.InstantiationInfo = r.RulesetInfo.InstantiationInfo;
|
||||||
throw new InvalidOperationException(@"Instantiation failure.");
|
}
|
||||||
|
else
|
||||||
r.Name = rInstance.Name;
|
realm.Add(new RealmRuleset(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.ID));
|
||||||
r.ShortName = rInstance.ShortName;
|
}
|
||||||
r.InstantiationInfo = rInstance.InstantiationInfo;
|
|
||||||
r.Available = true;
|
|
||||||
|
|
||||||
detachedRulesets.Add(r.Clone());
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
List<RealmRuleset> detachedRulesets = new List<RealmRuleset>();
|
||||||
|
|
||||||
|
// perform a consistency check and detach final rulesets from realm for cross-thread runtime usage.
|
||||||
|
foreach (var r in rulesets)
|
||||||
{
|
{
|
||||||
r.Available = false;
|
try
|
||||||
Logger.Log($"Could not load ruleset {r}: {ex.Message}");
|
{
|
||||||
}
|
var type = Type.GetType(r.InstantiationInfo);
|
||||||
}
|
|
||||||
|
|
||||||
availableRulesets.AddRange(detachedRulesets);
|
if (type == null)
|
||||||
});
|
throw new InvalidOperationException(@"Type resolution failure.");
|
||||||
|
|
||||||
|
var rInstance = (Activator.CreateInstance(type) as Ruleset)?.RulesetInfo;
|
||||||
|
|
||||||
|
if (rInstance == null)
|
||||||
|
throw new InvalidOperationException(@"Instantiation failure.");
|
||||||
|
|
||||||
|
r.Name = rInstance.Name;
|
||||||
|
r.ShortName = rInstance.ShortName;
|
||||||
|
r.InstantiationInfo = rInstance.InstantiationInfo;
|
||||||
|
r.Available = true;
|
||||||
|
|
||||||
|
detachedRulesets.Add(r.Clone());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
r.Available = false;
|
||||||
|
Logger.Log($"Could not load ruleset {r}: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
availableRulesets.AddRange(detachedRulesets);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadFromAppDomain()
|
private void loadFromAppDomain()
|
||||||
|
Reference in New Issue
Block a user