Skip to content

Commit

Permalink
Added support for getting osu's game mode state.
Browse files Browse the repository at this point in the history
Also changed mode and status to be enums.
  • Loading branch information
Wibble199 committed Sep 22, 2019
1 parent db9f169 commit b294195
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
28 changes: 12 additions & 16 deletions Project-Aurora/Project-Aurora/Profiles/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,28 @@ internal long GetLong(string Name)
return -1;
}

internal T GetEnum<T>(string Name)
internal T GetEnum<T>(string Name) where T : struct
{
Newtonsoft.Json.Linq.JToken value;

if (_ParsedData.TryGetValue(Name, out value) && !String.IsNullOrWhiteSpace(value.ToString()))
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description.ToLowerInvariant().Equals(value.ToString().ToLowerInvariant()))
return (T)field.GetValue(null);
}

if (field.Name.ToLowerInvariant().Equals(value.ToString().ToLowerInvariant()))
return (T)field.GetValue(null);
}
// Attempt to parse it by name or number
if (Enum.TryParse<T>(value.ToString(), true, out var val))
return val;

return (T)Enum.Parse(typeof(T), "Undefined", true);
// If that wasn't successful, try by DescriptionAttribute
foreach (var field in type.GetFields())
if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute
&& attribute.Description.ToLowerInvariant().Equals(value.ToString().ToLowerInvariant()))
return (T)field.GetValue(null);
}
else
return (T)Enum.Parse(typeof(T), "Undefined", true);

// If there is an "undefined" enum value, return that else just do the default(T).
return Enum.TryParse<T>("Undefined", true, out var u) ? u : default(T);
}

internal bool GetBool(string Name)
Expand Down
21 changes: 21 additions & 0 deletions Project-Aurora/Project-Aurora/Profiles/Osu/GSI/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Aurora.Profiles.Osu.GSI {
public enum OsuStatus {
NoFoundProcess = 1,
Unkonwn = 2,
SelectSong = 4,
Playing = 8,
Editing = 16,
Rank = 32,
MatchSetup = 64,
Lobby = 128,
Idle = 256
}

public enum OsuPlayMode {
Unknown = -1,
Osu = 0,
Taiko = 1,
CatchTheBeat = 2,
Mania = 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal ProviderNode(string json) : base(json) {

public class GameNode : Node<GameNode> {

public string Status;
public OsuStatus StatusEnum;
public string Status => StatusEnum.ToString(); // Only here for legacy reasons - don't wanna break any profiles that may now depend on this
public OsuPlayMode PlayMode;
public float HP;
public float Accuracy;
public int Combo;
Expand All @@ -39,7 +41,8 @@ public class GameNode : Node<GameNode> {
public int CountMiss;

internal GameNode(string json) : base(json) {
Status = GetString("status");
StatusEnum = GetEnum<OsuStatus>("status");
PlayMode = GetEnum<OsuPlayMode>("playMode");
HP = GetFloat("hp");
Accuracy = GetFloat("accuracy");
Combo = GetInt("combo");
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions Project-Aurora/Project-Aurora/Project-Aurora.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
<DependentUpon>Control_FieldPresenter.xaml</DependentUpon>
</Compile>
<Compile Include="Devices\UnifiedHID\RoccatTyon.cs" />
<Compile Include="Profiles\Osu\GSI\Enums.cs" />
<Compile Include="Profiles\Skype\Control_Skype.xaml.cs">
<DependentUpon>Control_Skype.xaml</DependentUpon>
</Compile>
Expand Down

0 comments on commit b294195

Please sign in to comment.