Skip to content

Commit a6d29c8

Browse files
author
magiclu550
committed
[proxy] a event reflect handler
1 parent 817c08b commit a6d29c8

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

jsmod2/Lib.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,19 @@ public static string[] getArray(string s)
117117

118118
public static Vector getVector(string s)
119119
{
120-
121-
string[] xyz = s.Split(',');
122-
float x = getDouble(xyz[0]);
123-
float y = getDouble(xyz[1]);
124-
float z = getDouble(xyz[2]);
125-
return new Vector(x,y,z);
120+
121+
try
122+
{
123+
string[] xyz = s.Split(',');
124+
float x = getDouble(xyz[0]);
125+
float y = getDouble(xyz[1]);
126+
float z = getDouble(xyz[2]);
127+
return new Vector(x,y,z);
128+
}
129+
catch (Exception e)
130+
{
131+
return null;
132+
}
126133
}
127134

128135
public static float getDouble(string s)

jsmod2/NetworkHandler.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static NetworkHandler()
7979
handlers.Add(133,new HandleDoorSetOpen());
8080
handlers.Add(143,new HandleGeneratorSetTimeLeft());
8181
handlers.Add(131,new HandleGeneratorUnlock());
82+
handlers.Add(180,new EventHandler());
83+
handlers.Add(181,new EventHandler());
8284
}
8385
public static void handleJsmod2(int id, String json,Dictionary<string,string> mapper,TcpClient client)
8486
{
@@ -147,13 +149,100 @@ public static JsonSetting[] getOne(string id,object val,IdMapping mapping)
147149
{
148150
return new[] {new JsonSetting(Lib.getInt(id), val, mapping)};
149151
}
152+
153+
public static object getTypeValue(string val)
154+
{
155+
int i1;
156+
bool b = int.TryParse(val,out i1);
157+
if (b)
158+
{
159+
return i1;
160+
}
161+
162+
bool b1;
163+
b = bool.TryParse(val, out b1);
164+
if (b)
165+
{
166+
return b1;
167+
}
168+
169+
float f1;
170+
b = float.TryParse(val, out f1);
171+
if (b)
172+
{
173+
return f1;
174+
}
175+
176+
Vector vector = Lib.getVector(val);
177+
178+
if (vector != null)
179+
{
180+
return vector;
181+
}
182+
183+
char c1;
184+
185+
b = char.TryParse(val, out c1);
186+
if (b)
187+
{
188+
return c1;
189+
}
190+
191+
return val;
192+
193+
}
150194
}
151195

152196
public interface Handler
153197
{
154198
JsonSetting[] handle(object api,Dictionary<string,string> mapper);
155199
}
156200

201+
public class EventHandler : Handler
202+
{
203+
public JsonSetting[] handle(object api, Dictionary<string, string> mapper)
204+
{
205+
Type type = api.GetType();
206+
if (mapper["id"].Equals("180"))//180 Get
207+
{
208+
object obj = type.GetField(mapper["field"]).GetValue(api);
209+
Type returnType = obj.GetType();
210+
bool isCommonType = returnType == typeof(string) || returnType == typeof(bool) ||
211+
returnType == typeof(float)
212+
|| returnType == typeof(double) || returnType == typeof(String) ||
213+
returnType == typeof(Vector)
214+
|| returnType == typeof(int) || returnType == typeof(long) || returnType == typeof(char)
215+
|| returnType == typeof(byte) || returnType == typeof(short);
216+
if (isCommonType)
217+
{
218+
return Utils.getOne(mapper["id"], obj, null);
219+
}
220+
if (returnType == typeof(List<Vector>))
221+
{
222+
List<Vector> vectors = (List<Vector>) obj;
223+
JsonSetting[] settings = new JsonSetting[vectors.Count];
224+
for (int i = 0; i < settings.Length; i++)
225+
{
226+
settings[i] = new JsonSetting(Lib.getInt(mapper["id"]),vectors[i],null);
227+
}
228+
229+
return settings;
230+
}
231+
return null;
232+
}
233+
234+
string fieldName = mapper["field"];
235+
string val = mapper[fieldName];
236+
object result = Utils.getTypeValue(val);
237+
if (mapper.ContainsKey("apiId"))
238+
{
239+
result = ProxyHandler.handler.apiMapping[val];
240+
}
241+
type.GetField(fieldName).SetValue(api,result);
242+
return null;
243+
}
244+
}
245+
157246
public class HandleGeneratorGetEngaged : Handler
158247
{
159248
public JsonSetting[] handle(object api, Dictionary<string, string> mapper)

0 commit comments

Comments
 (0)