Skip to content

Commit

Permalink
Fixed auto value string enum conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandoun committed Aug 4, 2022
1 parent 6c7c368 commit 772f8b8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void Scenario1 () {
Task.Factory.StartNew(async () => {
//attaching the logger
Logger.LogLevel = LogLevel.Critical;
Logger.LogLevel = LogLevel.Verbose;
Logger.OnNewLogMessage((date, msg) => {
Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}");
});
Expand Down
2 changes: 2 additions & 0 deletions Examples/TestRegisters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public enum CurrentState {
[Register(50)]
public CurrentState TestEnum { get; private set; }

[Register(100)]
public TimeSpan TsTest2 { get; private set; }

}
}
8 changes: 7 additions & 1 deletion MewtocolNet/Mewtocol/Subregisters/Register.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ public string GetStartingMemoryArea () {
public string GetValueString () {

if (enumType != null && this is NRegister<int> intEnumReg) {

var dict = new Dictionary<int, string>();

foreach (var name in Enum.GetNames(enumType)) {
dict.Add((int)Enum.Parse(enumType, name), name);
int enumKey = (int)Enum.Parse(enumType, name);
if(!dict.ContainsKey(enumKey)) {
dict.Add(enumKey, name);
}
}

if(dict.ContainsKey(intEnumReg.Value)) {
return $"{intEnumReg.Value} ({dict[intEnumReg.Value]})";
} else {
return $"{intEnumReg.Value} (Missing Enum)";
}

}
if (this is NRegister<short> shortReg) {
return $"{shortReg.Value}{(isUsedBitwise ? $" [{shortReg.GetBitwise().ToBitString()}]" : "")}";
Expand Down

0 comments on commit 772f8b8

Please sign in to comment.