241 |
|
|
242 |
private void CreateLoader(string xml_file) |
private void CreateLoader(string xml_file) |
243 |
{ |
{ |
244 |
|
xmltv_logger.Verbose.Debug.WriteLine("Creating loader handle"); |
245 |
gInstance = new XMLTVRuntimeInstance(); |
gInstance = new XMLTVRuntimeInstance(); |
|
bool bound_to_ctor = false; |
|
246 |
object raw_instance = null; |
object raw_instance = null; |
247 |
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
248 |
|
CultureInfo culture = CultureInfo.CurrentCulture; |
249 |
Assembly asm = Assembly.GetExecutingAssembly(); |
Assembly asm = Assembly.GetExecutingAssembly(); |
250 |
var types = asm.GetTypes(); |
var types = asm.GetTypes(); |
251 |
|
Type handler_type = null; |
252 |
foreach (var type in types) |
foreach (var type in types) |
253 |
{ |
{ |
254 |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
255 |
{ |
{ |
256 |
xmltv_logger.Verbose.Debug.WriteLine("Type: '{0}' Base: '{1}'", type.Name, type.BaseType == null ? "none" : type.BaseType.Name); |
//xmltv_logger.Verbose.Debug.WriteLine("Type: '{0}' Base: '{1}'", type.Name, type.BaseType == null ? "none" : type.BaseType.Name); |
|
|
|
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
|
|
CultureInfo culture = CultureInfo.CurrentCulture; |
|
257 |
object[] args = new object[] { xml_file, gInstance }; |
object[] args = new object[] { xml_file, gInstance }; |
258 |
try |
var iface = type.GetInterface("IXMLTVHandler", true); |
259 |
|
if (iface != null) |
260 |
{ |
{ |
261 |
raw_instance = Activator.CreateInstance(type, flags, null, args, culture); |
var handler_prop = type.GetProperty("Handler"); |
262 |
bound_to_ctor = true; |
if (handler_prop != null) |
263 |
} |
{ |
264 |
catch (Exception ex) |
var ctors = type.GetConstructors(flags); |
265 |
{ |
bool has_string_ctor = false; |
266 |
Debug.WriteLine(ex.ToString()); |
foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 1 && ctor.GetParameters()[0].ParameterType == typeof(string)) { has_string_ctor = true; } } |
267 |
|
if (!has_string_ctor) { continue; } |
268 |
|
raw_instance = Activator.CreateInstance(type, flags, null, new object[] { xml_file}, culture); |
269 |
|
if (raw_instance != null) |
270 |
|
{ |
271 |
|
object handler_value = handler_prop.GetValue(raw_instance, null); |
272 |
|
if (handler_value != null && handler_value.ToString() == xml_file) |
273 |
|
{ |
274 |
|
handler_type = type; |
275 |
|
break; |
276 |
|
} |
277 |
|
} |
278 |
|
} |
279 |
} |
} |
280 |
} |
} |
281 |
} |
} |
282 |
if (!bound_to_ctor) { throw new Exception("Unable to find a compatible XMLTV Data Loader."); } |
if (handler_type == null) { throw new Exception("Unable to find a compatible XMLTV Data Loader."); } |
283 |
|
xmltv_logger.Verbose.Debug.WriteLine("Created loader handle"); |
284 |
|
raw_instance = Activator.CreateInstance(handler_type, flags, null, new object[] {xml_file , gInstance }, culture); |
285 |
if (raw_instance == null) { throw new NullReferenceException("Found a compatible XMLTV Data Loader, but it returned invalid data."); } |
if (raw_instance == null) { throw new NullReferenceException("Found a compatible XMLTV Data Loader, but it returned invalid data."); } |
286 |
IGetInstance<XMLTVRuntimeInstance> getter_instance = (raw_instance as IGetInstance<XMLTVRuntimeInstance>); |
IGetInstance<XMLTVRuntimeInstance> getter_instance = (raw_instance as IGetInstance<XMLTVRuntimeInstance>); |
287 |
if (getter_instance != null) { gInstance = getter_instance.GetInstance(); } |
if (getter_instance != null) { gInstance = getter_instance.GetInstance(); } |
327 |
foreach (var type in types) |
foreach (var type in types) |
328 |
{ |
{ |
329 |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
330 |
{ |
{ |
331 |
try |
var iface = type.GetInterface("IXMLTVHandler", true); |
332 |
|
if (iface != null) |
333 |
{ |
{ |
334 |
var handler_prop = type.GetProperty("Handler"); |
var handler_prop = type.GetProperty("Handler"); |
335 |
if (handler_prop != null) |
if (handler_prop != null) |
336 |
{ |
{ |
337 |
|
var ctors = type.GetConstructors(flags); |
338 |
|
bool has_default_ctor = false; |
339 |
|
foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } } |
340 |
|
if (!has_default_ctor) { continue; } |
341 |
raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture); |
raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture); |
342 |
if (raw_instance != null) |
if (raw_instance != null) |
343 |
{ |
{ |
347 |
handler_type = type; |
handler_type = type; |
348 |
break; |
break; |
349 |
} |
} |
350 |
} |
} |
351 |
} |
} |
352 |
} |
} |
|
catch (Exception) { } |
|
353 |
} |
} |
354 |
} |
} |
355 |
if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); } |
if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); } |
374 |
{ |
{ |
375 |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
if (type.BaseType != null && type.BaseType == typeof(XMLTVBase<XMLTVRuntimeInstance>)) |
376 |
{ |
{ |
377 |
try |
var iface = type.GetInterface("IXMLTVHandler", true); |
378 |
|
if (iface != null) |
379 |
{ |
{ |
380 |
var handler_prop = type.GetProperty("Handler"); |
var handler_prop = type.GetProperty("Handler"); |
381 |
if (handler_prop != null) |
if (handler_prop != null) |
382 |
{ |
{ |
383 |
|
var ctors = type.GetConstructors(flags); |
384 |
|
bool has_default_ctor = false; |
385 |
|
foreach (var ctor in ctors) { if (ctor.GetParameters().Count() == 0) { has_default_ctor = true; } } |
386 |
|
if (!has_default_ctor) { continue; } |
387 |
raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture); |
raw_instance = Activator.CreateInstance(type, flags, null, new object[0], culture); |
388 |
if (raw_instance != null) |
if (raw_instance != null) |
389 |
{ |
{ |
396 |
} |
} |
397 |
} |
} |
398 |
} |
} |
|
catch (Exception) { } |
|
399 |
} |
} |
400 |
} |
} |
401 |
if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); } |
if (handler_type == null) { throw new Exception("Unable to find a compatible handler to parse document root."); } |