-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathUnityVersion.cs
More file actions
57 lines (49 loc) · 2.03 KB
/
UnityVersion.cs
File metadata and controls
57 lines (49 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
namespace UnityLauncherPro
{
public class UnityVersion
{
public string Version { get; set; }
public UnityVersionStream Stream { get; set; }
public DateTime ReleaseDate { get; set; }
public string directURL { get; set; } = null; // if found from unofficial releases list
public static UnityVersion FromJson(string json)
{
var values = ParseJsonToDictionary(json);
return new UnityVersion
{
Version = values.ContainsKey("version") ? values["version"] : null,
Stream = ParseStream(values.ContainsKey("stream") ? values["stream"] : null),
ReleaseDate = DateTime.TryParse(values.ContainsKey("releaseDate") ? values["releaseDate"] : null, out var date)
? date
: default
};
}
public string ToJson()
{
return $"{{ \"version\": \"{Version}\", \"stream\": \"{Stream}\", \"releaseDate\": \"{ReleaseDate:yyyy-MM-ddTHH:mm:ss}\" }}";
}
private static Dictionary<string, string> ParseJsonToDictionary(string json)
{
var result = new Dictionary<string, string>();
json = json.Trim(new char[] { '{', '}', ' ' });
var keyValuePairs = json.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var pair in keyValuePairs)
{
var keyValue = pair.Split(new[] { ':' }, 2);
if (keyValue.Length == 2)
{
var key = keyValue[0].Trim(new char[] { ' ', '"' });
var value = keyValue[1].Trim(new char[] { ' ', '"' });
result[key] = value;
}
}
return result;
}
private static UnityVersionStream ParseStream(string stream)
{
return Enum.TryParse(stream, true, out UnityVersionStream result) ? result : UnityVersionStream.Tech;
}
}
}