Skip to content

Commit

Permalink
Merge pull request karaoke-dev#1874 from andy840119/should-be-able-to…
Browse files Browse the repository at this point in the history
…-get-generator-config-just-by-type

Should be able to get generator config just by type.
  • Loading branch information
andy840119 authored Mar 11, 2023
2 parents 7a76251 + 85b1111 commit c1eb702
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,58 @@ protected override void InitialiseDefaults()
base.InitialiseDefaults();

// Beatmap page
SetDefault(KaraokeRulesetEditGeneratorSetting.BeatmapPageGeneratorConfig, CreateDefaultConfig<PageGeneratorConfig>());
SetDefault<PageGeneratorConfig>();

// Language detection
SetDefault(KaraokeRulesetEditGeneratorSetting.ReferenceLyricDetectorConfig, CreateDefaultConfig<ReferenceLyricDetectorConfig>());
SetDefault<ReferenceLyricDetectorConfig>();

// Language detection
SetDefault(KaraokeRulesetEditGeneratorSetting.LanguageDetectorConfig, CreateDefaultConfig<LanguageDetectorConfig>());
SetDefault<LanguageDetectorConfig>();

// Note generator
SetDefault(KaraokeRulesetEditGeneratorSetting.NoteGeneratorConfig, CreateDefaultConfig<NoteGeneratorConfig>());
SetDefault<NoteGeneratorConfig>();

// Romaji generator
SetDefault(KaraokeRulesetEditGeneratorSetting.JaRomajiTagGeneratorConfig, CreateDefaultConfig<JaRomajiTagGeneratorConfig>());
SetDefault<JaRomajiTagGeneratorConfig>();

// Ruby generator
SetDefault(KaraokeRulesetEditGeneratorSetting.JaRubyTagGeneratorConfig, CreateDefaultConfig<JaRubyTagGeneratorConfig>());
SetDefault<JaRubyTagGeneratorConfig>();

// Time tag generator
SetDefault(KaraokeRulesetEditGeneratorSetting.JaTimeTagGeneratorConfig, CreateDefaultConfig<JaTimeTagGeneratorConfig>());
SetDefault(KaraokeRulesetEditGeneratorSetting.ZhTimeTagGeneratorConfig, CreateDefaultConfig<ZhTimeTagGeneratorConfig>());
SetDefault<JaTimeTagGeneratorConfig>();
SetDefault<ZhTimeTagGeneratorConfig>();
}

protected void SetDefault<T>() where T : GeneratorConfig, new()
{
var defaultValue = CreateDefaultConfig<T>();
var setting = GetSettingByType<T>();

SetDefault(setting, defaultValue);
}

protected static T CreateDefaultConfig<T>() where T : GeneratorConfig, new() => new();

protected static KaraokeRulesetEditGeneratorSetting GetSettingByType<TValue>() =>
typeof(TValue) switch
{
Type t when t == typeof(PageGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.BeatmapPageGeneratorConfig,
Type t when t == typeof(ReferenceLyricDetectorConfig) => KaraokeRulesetEditGeneratorSetting.ReferenceLyricDetectorConfig,
Type t when t == typeof(LanguageDetectorConfig) => KaraokeRulesetEditGeneratorSetting.LanguageDetectorConfig,
Type t when t == typeof(NoteGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.NoteGeneratorConfig,
Type t when t == typeof(JaRomajiTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaRomajiTagGeneratorConfig,
Type t when t == typeof(JaRubyTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaRubyTagGeneratorConfig,
Type t when t == typeof(JaTimeTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.JaTimeTagGeneratorConfig,
Type t when t == typeof(ZhTimeTagGeneratorConfig) => KaraokeRulesetEditGeneratorSetting.ZhTimeTagGeneratorConfig,
_ => throw new NotSupportedException()
};

public TValue Get<TValue>() where TValue : GeneratorConfig, new()
{
var lookup = GetSettingByType<TValue>();
return Get<TValue>(lookup);
}

public GeneratorConfig GetGeneratorConfig(KaraokeRulesetEditGeneratorSetting lookup)
{
if (!ConfigStore.TryGetValue(lookup, out IBindable? obj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void AutoGenerate()
}

private PageGeneratorConfig getGeneratorConfig()
=> generatorConfigManager.Get<PageGeneratorConfig>(KaraokeRulesetEditGeneratorSetting.BeatmapPageGeneratorConfig);
=> generatorConfigManager.Get<PageGeneratorConfig>();

public void Add(Page page)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ private PropertyDetector<Lyric, T> createLyricDetector<T>()
{
case Type t when t == typeof(Lyric):
var lyrics = beatmap.HitObjects.OfType<Lyric>().ToArray();
var referenceLyricDetectorConfig = generatorConfigManager.Get<ReferenceLyricDetectorConfig>(KaraokeRulesetEditGeneratorSetting.ReferenceLyricDetectorConfig);
var referenceLyricDetectorConfig = generatorConfigManager.Get<ReferenceLyricDetectorConfig>();
return (PropertyDetector<Lyric, T>)(object)new ReferenceLyricDetector(lyrics, referenceLyricDetectorConfig);

case Type t when t == typeof(CultureInfo):
var languageDetectorConfig = generatorConfigManager.Get<LanguageDetectorConfig>(KaraokeRulesetEditGeneratorSetting.LanguageDetectorConfig);
var languageDetectorConfig = generatorConfigManager.Get<LanguageDetectorConfig>();
return (PropertyDetector<Lyric, T>)(object)new LanguageDetector(languageDetectorConfig);

default:
Expand All @@ -228,7 +228,7 @@ private PropertyGenerator<Lyric, TProperty> createLyricGenerator<TProperty>()
return (PropertyGenerator<Lyric, TProperty>)(object)new TimeTagGeneratorSelector(generatorConfigManager);

case Type t when t == typeof(Note[]):
var config = generatorConfigManager.Get<NoteGeneratorConfig>(KaraokeRulesetEditGeneratorSetting.NoteGeneratorConfig);
var config = generatorConfigManager.Get<NoteGeneratorConfig>();
return (PropertyGenerator<Lyric, TProperty>)(object)new NoteGenerator(config);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,23 @@ protected void RegisterGenerator<TGenerator, TConfig>(CultureInfo info)
{
generator.Add(info, new Lazy<PropertyGenerator<Lyric, TProperty>>(() =>
{
var generatorSetting = GetGeneratorConfigSetting(info);
var config = generatorConfigManager.Get<TConfig>(generatorSetting);
if (Activator.CreateInstance(typeof(TGenerator), config) is not PropertyGenerator<Lyric, TProperty> generator)
var config = generatorConfigManager.Get<TConfig>();
if (Activator.CreateInstance(typeof(TGenerator), config) is not PropertyGenerator<Lyric, TProperty> propertyGenerator)
throw new InvalidCastException();

return generator;
return propertyGenerator;
}));
}

protected abstract KaraokeRulesetEditGeneratorSetting GetGeneratorConfigSetting(CultureInfo info);

protected override TProperty GenerateFromItem(Lyric item)
{
if (item.Language == null)
throw new GeneratorNotSupportedException();

if (!this.generator.TryGetValue(item.Language, out var generator))
if (!generator.TryGetValue(item.Language, out var propertyGenerator))
throw new GeneratorNotSupportedException();

return generator.Value.Generate(item);
return propertyGenerator.Value.Generate(item);
}

protected override LocalisableString? GetInvalidMessageFromItem(Lyric item)
Expand All @@ -58,10 +55,10 @@ protected override TProperty GenerateFromItem(Lyric item)
if (string.IsNullOrWhiteSpace(item.Text))
return "Should have the text in the lyric";

if (!this.generator.TryGetValue(item.Language, out var generator))
if (!generator.TryGetValue(item.Language, out var propertyGenerator))
return "Sorry, the language of lyric is not supported yet.";

return generator.Value.GetInvalidMessage(item);
return propertyGenerator.Value.GetInvalidMessage(item);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ public RomajiTagGeneratorSelector(KaraokeRulesetEditGeneratorConfigManager gener
RegisterGenerator<JaRomajiTagGenerator, JaRomajiTagGeneratorConfig>(new CultureInfo(17));
RegisterGenerator<JaRomajiTagGenerator, JaRomajiTagGeneratorConfig>(new CultureInfo(1041));
}

protected override KaraokeRulesetEditGeneratorSetting GetGeneratorConfigSetting(CultureInfo info)
=> KaraokeRulesetEditGeneratorSetting.JaRomajiTagGeneratorConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ public RubyTagGeneratorSelector(KaraokeRulesetEditGeneratorConfigManager generat
RegisterGenerator<JaRubyTagGenerator, JaRubyTagGeneratorConfig>(new CultureInfo(17));
RegisterGenerator<JaRubyTagGenerator, JaRubyTagGeneratorConfig>(new CultureInfo(1041));
}

protected override KaraokeRulesetEditGeneratorSetting GetGeneratorConfigSetting(CultureInfo info)
=> KaraokeRulesetEditGeneratorSetting.JaRubyTagGeneratorConfig;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Globalization;
using osu.Game.Rulesets.Karaoke.Configuration;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags.Ja;
Expand All @@ -19,14 +18,5 @@ public TimeTagGeneratorSelector(KaraokeRulesetEditGeneratorConfigManager generat
RegisterGenerator<JaTimeTagGenerator, JaTimeTagGeneratorConfig>(new CultureInfo(1041));
RegisterGenerator<ZhTimeTagGenerator, ZhTimeTagGeneratorConfig>(new CultureInfo(1028));
}

protected override KaraokeRulesetEditGeneratorSetting GetGeneratorConfigSetting(CultureInfo info) =>
info.LCID switch
{
17 => KaraokeRulesetEditGeneratorSetting.JaTimeTagGeneratorConfig,
1041 => KaraokeRulesetEditGeneratorSetting.JaTimeTagGeneratorConfig,
1028 => KaraokeRulesetEditGeneratorSetting.ZhTimeTagGeneratorConfig,
_ => throw new ArgumentOutOfRangeException(nameof(info))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public PageAutoGenerateButton()
}

private bool clearExistPagesAfterGenerated()
=> generatorConfigManager.Get<PageGeneratorConfig>(KaraokeRulesetEditGeneratorSetting.BeatmapPageGeneratorConfig).ClearExistPages.Value;
=> generatorConfigManager.Get<PageGeneratorConfig>().ClearExistPages.Value;
}
}
}

0 comments on commit c1eb702

Please sign in to comment.