diff options
Diffstat (limited to 'OpenSim/OpenSim.Terrain.BasicTerrain/TerrainFilter.cs')
-rw-r--r-- | OpenSim/OpenSim.Terrain.BasicTerrain/TerrainFilter.cs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/OpenSim/OpenSim.Terrain.BasicTerrain/TerrainFilter.cs b/OpenSim/OpenSim.Terrain.BasicTerrain/TerrainFilter.cs new file mode 100644 index 0000000..6a83c8a --- /dev/null +++ b/OpenSim/OpenSim.Terrain.BasicTerrain/TerrainFilter.cs | |||
@@ -0,0 +1,97 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | |||
5 | using System.CodeDom.Compiler; | ||
6 | using System.CodeDom; | ||
7 | using Microsoft.CSharp; | ||
8 | using Microsoft.JScript; | ||
9 | |||
10 | using libTerrain; | ||
11 | |||
12 | namespace OpenSim.Terrain | ||
13 | { | ||
14 | public interface ITerrainFilter | ||
15 | { | ||
16 | void Filter(Channel heightmap, string[] args); | ||
17 | string Register(); | ||
18 | } | ||
19 | |||
20 | public class TestFilter : ITerrainFilter | ||
21 | { | ||
22 | public void Filter(Channel heightmap, string[] args) | ||
23 | { | ||
24 | Console.WriteLine("Hello world"); | ||
25 | } | ||
26 | |||
27 | public string Register() | ||
28 | { | ||
29 | return "demofilter"; | ||
30 | } | ||
31 | } | ||
32 | |||
33 | public class FilterHost | ||
34 | { | ||
35 | public Dictionary<string, ITerrainFilter> filters = new Dictionary<string, ITerrainFilter>(); | ||
36 | |||
37 | private void LoadFilter(ICodeCompiler compiler, string filename) | ||
38 | { | ||
39 | CompilerParameters compilerParams = new CompilerParameters(); | ||
40 | CompilerResults compilerResults; | ||
41 | compilerParams.GenerateExecutable = false; | ||
42 | compilerParams.GenerateInMemory = true; | ||
43 | compilerParams.IncludeDebugInformation = false; | ||
44 | compilerParams.ReferencedAssemblies.Add("libTerrain-BSD.dll"); | ||
45 | compilerParams.ReferencedAssemblies.Add("OpenSim.Terrain.BasicTerrain.dll"); | ||
46 | compilerParams.ReferencedAssemblies.Add("System.dll"); | ||
47 | |||
48 | compilerResults = compiler.CompileAssemblyFromFile(compilerParams, filename); | ||
49 | |||
50 | if (compilerResults.Errors.Count > 0) | ||
51 | { | ||
52 | Console.WriteLine("Compile errors:"); | ||
53 | foreach (CompilerError error in compilerResults.Errors) | ||
54 | { | ||
55 | Console.WriteLine(error.Line.ToString() + ": " + error.ErrorText.ToString()); | ||
56 | } | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | foreach (Type pluginType in compilerResults.CompiledAssembly.GetExportedTypes()) | ||
61 | { | ||
62 | Type testInterface = pluginType.GetInterface("ITerrainFilter",true); | ||
63 | |||
64 | if (testInterface != null) | ||
65 | { | ||
66 | ITerrainFilter filter = (ITerrainFilter)compilerResults.CompiledAssembly.CreateInstance(pluginType.ToString()); | ||
67 | |||
68 | string filterName = filter.Register(); | ||
69 | Console.WriteLine("Plugin: " + filterName + " loaded."); | ||
70 | |||
71 | if (!filters.ContainsKey(filterName)) | ||
72 | { | ||
73 | filters.Add(filterName, filter); | ||
74 | } | ||
75 | else | ||
76 | { | ||
77 | filters[filterName] = filter; | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | } | ||
84 | |||
85 | public void LoadFilterCSharp(string filename) | ||
86 | { | ||
87 | CSharpCodeProvider compiler = new CSharpCodeProvider(); | ||
88 | LoadFilter(compiler.CreateCompiler(), filename); | ||
89 | } | ||
90 | |||
91 | public void LoadFilterJScript(string filename) | ||
92 | { | ||
93 | JScriptCodeProvider compiler = new JScriptCodeProvider(); | ||
94 | LoadFilter(compiler.CreateCompiler(), filename); | ||
95 | } | ||
96 | } | ||
97 | } | ||