Skip to content

Commit 3a95cbf

Browse files
author
Joe Thaler
committed
Initial import
0 parents  commit 3a95cbf

24 files changed

+2521
-0
lines changed

LS1APISpec/LS1APISpec.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Text;
6+
7+
namespace libLS1APISpec
8+
{
9+
public class LS1APISpec : LS1APISpecObject
10+
{
11+
public string Description { get; set; }
12+
public ObservableCollection<LS1Event> Events { get; private set; }
13+
public ObservableCollection<LS1Type> Types { get; private set; }
14+
public ObservableCollection<LS1TopLevelObject> TopLevelObjects { get; private set; }
15+
public ObservableCollection<LS1Command> Commands { get; private set; }
16+
17+
public static LS1APISpec GetFromFile(string filename)
18+
{
19+
JObject jo = Utils.JObjectFromFile(filename);
20+
if (jo == null)
21+
return null;
22+
23+
LS1APISpec spec = new LS1APISpec();
24+
if (!spec.FromJObject(jo))
25+
return null;
26+
27+
return spec;
28+
}
29+
30+
public void WriteToFile(string filename)
31+
{
32+
JObject jo = GetJObject();
33+
if (jo == null)
34+
return;
35+
Utils.WriteAllTextSafe(filename, jo.ToString(), Encoding.UTF8);
36+
}
37+
38+
public override bool FromJObject(JObject jo)
39+
{
40+
Name = jo.GetValueString("name");
41+
Description = jo.GetValueString("description");
42+
43+
Commands = jo.ToObservableCollection<LS1Command>("commands");
44+
Events = jo.ToObservableCollection<LS1Event>("events");
45+
Types = jo.ToObservableCollection<LS1Type>("types");
46+
TopLevelObjects = jo.ToObservableCollection<LS1TopLevelObject>("topLevelObjects");
47+
return true;
48+
}
49+
50+
public override JObject GetJObject()
51+
{
52+
JObject jo = new JObject();
53+
jo.Add("name", Name);
54+
jo.Add("description", Description);
55+
jo.AddObject("commands", Commands);
56+
jo.AddObject("events", Events);
57+
jo.AddObject("types", Types);
58+
jo.AddObject("topLevelObjects", TopLevelObjects);
59+
return jo;
60+
}
61+
}
62+
}

LS1APISpec/LS1APISpecObject.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace libLS1APISpec
7+
{
8+
public abstract class LS1APISpecObject
9+
{
10+
public abstract bool FromJObject(JObject jo);
11+
public abstract JObject GetJObject();
12+
13+
public string Name { get; set; }
14+
15+
public static JObject GetJObject(LS1APISpecObject item)
16+
{
17+
if (item == null)
18+
return null;
19+
return item.GetJObject();
20+
}
21+
22+
public static T FromJObject<T>(JObject obj) where T : LS1APISpecObject, new()
23+
{
24+
if (obj == null)
25+
return null;
26+
T item = new T();
27+
item.FromJObject(obj);
28+
return item;
29+
}
30+
31+
public static T FromJObject<T>(JObject parentObject, string name) where T : LS1APISpecObject, new()
32+
{
33+
if (parentObject == null)
34+
return null;
35+
36+
JObject obj = parentObject.GetValueJObject(name);
37+
if (obj == null)
38+
return null;
39+
40+
T item = new T();
41+
item.FromJObject(obj);
42+
return item;
43+
}
44+
}
45+
}

LS1APISpec/LS1Command.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Text;
6+
7+
namespace libLS1APISpec
8+
{
9+
public class LS1CommandForm : LS1APISpecObject
10+
{
11+
public uint MinimumBuild { get; set; }
12+
public string Description { get; set; }
13+
14+
public ObservableCollection<LS1Parameter> Parameters { get; set; }
15+
16+
public ObservableCollection<LS1Example> Examples { get; set; }
17+
18+
public override bool FromJObject(JObject jo)
19+
{
20+
MinimumBuild = jo.GetValueUInt("minimumBuild");
21+
Description = jo.GetValueString("description");
22+
Parameters = jo.ToObservableCollection<LS1Parameter>("parameters");
23+
24+
Examples = jo.ToObservableCollection<LS1Example>("examples");
25+
26+
return true;
27+
}
28+
29+
public override JObject GetJObject()
30+
{
31+
JObject jo = new JObject();
32+
33+
34+
if (MinimumBuild > 0)
35+
jo.Add("minimumBuild", MinimumBuild);
36+
37+
if (!string.IsNullOrEmpty(Description))
38+
jo.Add("description", Description);
39+
40+
if (Parameters != null && Parameters.Count > 0)
41+
jo.Add("parameters", Parameters);
42+
43+
if (Examples!=null)
44+
jo.AddObject("examples", Examples);
45+
return jo;
46+
}
47+
}
48+
49+
public class LS1Command : LS1APISpecObject
50+
{
51+
public ObservableCollection<LS1CommandForm> Forms { get; private set; }
52+
53+
public string Category { get; set; }
54+
public bool Restricted { get; set; }
55+
56+
public override bool FromJObject(JObject jo)
57+
{
58+
Name = jo.GetValueString("name");
59+
Forms = jo.ToObservableCollection<LS1CommandForm>("forms");
60+
Category = jo.GetValueString("category");
61+
Restricted = jo.GetValueBool("restricted");
62+
return true;
63+
}
64+
65+
public override JObject GetJObject()
66+
{
67+
JObject jo = new JObject();
68+
69+
if (!string.IsNullOrEmpty(Name))
70+
jo.Add("name", Name);
71+
72+
if (!string.IsNullOrEmpty(Category))
73+
jo.Add("category", Category);
74+
75+
if (Restricted)
76+
jo.Add("restricted", Restricted);
77+
78+
if (Forms != null && Forms.Count > 0)
79+
jo.Add("forms", Forms);
80+
return jo;
81+
}
82+
}
83+
}

LS1APISpec/LS1Event.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Text;
6+
7+
namespace libLS1APISpec
8+
{
9+
public class LS1EventContext : LS1APISpecObject
10+
{
11+
public string Type { get; set; }
12+
public string Description { get; set; }
13+
14+
15+
public override bool FromJObject(JObject jo)
16+
{
17+
Type = jo.GetValueString("type");
18+
Description = jo.GetValueString("description");
19+
return true;
20+
}
21+
22+
public override JObject GetJObject()
23+
{
24+
JObject jo = new JObject();
25+
jo.Add("type", Type);
26+
jo.Add("description", Description);
27+
return jo;
28+
}
29+
30+
}
31+
32+
public class LS1Event : LS1APISpecObject
33+
{
34+
public string Description { get; set; }
35+
36+
public ObservableCollection<LS1Parameter> Parameters { get; private set; }
37+
38+
public LS1EventContext Context { get; set; }
39+
40+
public bool Restricted { get; set; }
41+
public uint MinimumBuild { get; set; }
42+
public string Category { get; set; }
43+
44+
public override bool FromJObject(JObject jo)
45+
{
46+
Name = jo.GetValueString("name");
47+
Description = jo.GetValueString("description");
48+
49+
Parameters = jo.ToObservableCollection<LS1Parameter>("parameters");
50+
Context = LS1EventContext.FromJObject<LS1EventContext>(jo, "context");
51+
Restricted = jo.GetValueBool("restricted");
52+
MinimumBuild = jo.GetValueUInt("minimumBuild");
53+
Category = jo.GetValueString("category");
54+
return true;
55+
}
56+
57+
public override JObject GetJObject()
58+
{
59+
JObject jo = new JObject();
60+
61+
jo.Add("name", Name);
62+
jo.Add("description", Description);
63+
if (Parameters!=null && Parameters.Count>0)
64+
jo.Add("parameters", Parameters);
65+
66+
if (Context!=null)
67+
jo.Add("context", Context);
68+
69+
if (Restricted)
70+
jo.Add("restricted", Restricted);
71+
72+
if (MinimumBuild>0)
73+
jo.Add("minimumBuild", MinimumBuild);
74+
75+
if (!string.IsNullOrEmpty(Category))
76+
jo.Add("category", Category);
77+
return jo;
78+
}
79+
}
80+
}

LS1APISpec/LS1Example.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Text;
6+
7+
namespace libLS1APISpec
8+
{
9+
public class LS1Example : LS1APISpecObject
10+
{
11+
public string Description { get; set; }
12+
13+
public ObservableCollection<string> Code { get; private set; }
14+
15+
public ObservableCollection<string> Result { get; private set; }
16+
17+
ObservableCollection<string> GetOneOrMoreStrings(JObject jo, string name)
18+
{
19+
JArray ja = jo.GetValueJArray(name);
20+
if (ja != null)
21+
{
22+
// todo
23+
ObservableCollection<string> res = new ObservableCollection<string>();
24+
for (int i = 0; i < ja.Count; i++)
25+
{
26+
res.Add(ja.GetStringAt(i));
27+
}
28+
return res;
29+
30+
}
31+
else
32+
{
33+
string code = jo.GetValueString("code");
34+
if (!string.IsNullOrEmpty(code))
35+
{
36+
ObservableCollection<string> res = new ObservableCollection<string>();
37+
res.Add(code);
38+
return res;
39+
}
40+
}
41+
42+
return null;
43+
}
44+
45+
JToken StringsToJSON(ObservableCollection<string> list)
46+
{
47+
if (list == null || list.Count==0)
48+
return null;
49+
50+
if (list.Count == 1)
51+
return list[0];
52+
53+
JArray ja = new JArray();
54+
foreach (string s in list)
55+
ja.Add(s);
56+
return ja;
57+
}
58+
59+
public override bool FromJObject(JObject jo)
60+
{
61+
Name = jo.GetValueString("name");
62+
63+
Description = jo.GetValueString("description");
64+
65+
Code = GetOneOrMoreStrings(jo,"code");
66+
Result = GetOneOrMoreStrings(jo,"result");
67+
68+
return true;
69+
}
70+
71+
public override JObject GetJObject()
72+
{
73+
JObject jo = new JObject();
74+
75+
if (!string.IsNullOrEmpty(Name))
76+
jo.Add("name", Name);
77+
78+
if (!string.IsNullOrEmpty(Description))
79+
jo.Add("description", Description);
80+
81+
JToken strings = StringsToJSON(Code);
82+
if (strings != null)
83+
jo.Add("code", strings);
84+
85+
strings = StringsToJSON(Result);
86+
if (strings != null)
87+
jo.Add("result", strings);
88+
89+
return jo;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)