source: tags/1.2.0/AI Template/CevoDotNet/AIPlugin.cs

Last change on this file was 191, checked in by chronos, 4 years ago
  • Added: AI Template directory with manual for development of custom AI from original game.
File size: 1.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
5namespace Loader
6{
7 class AIPlugin
8 {
9 static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
10
11 Object instance = null;
12 MethodInfo initializeMethod = null;
13 MethodInfo callMethod = null;
14
15 public AIPlugin(string assemblyPath)
16 {
17 try
18 {
19 Type type = null;
20 if (!typeCache.TryGetValue(assemblyPath, out type))
21 {
22 Assembly assembly = Assembly.LoadFile(assemblyPath);
23 type = assembly.GetType("AI.Plugin");
24 typeCache[assemblyPath] = type;
25 }
26 initializeMethod = type.GetMethod("Initialize");
27 callMethod = type.GetMethod("Call");
28 instance = Activator.CreateInstance(type);
29 }
30 catch (Exception)
31 {
32 initializeMethod = null;
33 callMethod = null;
34 instance = null;
35 }
36 }
37
38 public Object Initialize(params Object[] args)
39 {
40 if (initializeMethod == null || instance == null)
41 return null;
42 else
43 {
44 try
45 {
46 return initializeMethod.Invoke(instance, args);
47 }
48 catch (Exception)
49 {
50 return null;
51 }
52 }
53 }
54
55 public Object Call(params Object[] args)
56 {
57 if (callMethod == null || instance == null)
58 return null;
59 else
60 {
61 try
62 {
63 return callMethod.Invoke(instance, args);
64 }
65 catch (Exception)
66 {
67 return null;
68 }
69 }
70 }
71 }
72}
Note: See TracBrowser for help on using the repository browser.