TerrainFilter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.CodeDom.Compiler;
  29. using System.Collections.Generic;
  30. using libTerrain;
  31. using Microsoft.CSharp;
  32. using Microsoft.JScript;
  33. namespace OpenSim.Region.Terrain
  34. {
  35. public interface ITerrainFilter
  36. {
  37. void Filter(Channel heightmap, string[] args);
  38. string Register();
  39. string Help();
  40. }
  41. public class TestFilter : ITerrainFilter
  42. {
  43. public void Filter(Channel heightmap, string[] args)
  44. {
  45. Console.WriteLine("Hello world");
  46. }
  47. public string Register()
  48. {
  49. return "demofilter";
  50. }
  51. public string Help()
  52. {
  53. return "demofilter - Does nothing";
  54. }
  55. }
  56. public class FilterHost
  57. {
  58. public Dictionary<string, ITerrainFilter> filters = new Dictionary<string, ITerrainFilter>();
  59. private void LoadFilter(CodeDomProvider compiler, string filename)
  60. {
  61. CompilerParameters compilerParams = new CompilerParameters();
  62. CompilerResults compilerResults;
  63. compilerParams.GenerateExecutable = false;
  64. compilerParams.GenerateInMemory = true;
  65. compilerParams.IncludeDebugInformation = false;
  66. compilerParams.ReferencedAssemblies.Add("OpenSim.Terrain.BasicTerrain.dll");
  67. compilerParams.ReferencedAssemblies.Add("System.dll");
  68. compilerResults = compiler.CompileAssemblyFromFile(compilerParams, filename);
  69. if (compilerResults.Errors.Count > 0)
  70. {
  71. Console.WriteLine("Compile errors:");
  72. foreach (CompilerError error in compilerResults.Errors)
  73. {
  74. Console.WriteLine(error.Line.ToString() + ": " + error.ErrorText.ToString());
  75. }
  76. }
  77. else
  78. {
  79. foreach (Type pluginType in compilerResults.CompiledAssembly.GetExportedTypes())
  80. {
  81. Type testInterface = pluginType.GetInterface("ITerrainFilter", true);
  82. if (testInterface != null)
  83. {
  84. ITerrainFilter filter =
  85. (ITerrainFilter) compilerResults.CompiledAssembly.CreateInstance(pluginType.ToString());
  86. string filterName = filter.Register();
  87. Console.WriteLine("Plugin: " + filterName + " loaded.");
  88. if (!filters.ContainsKey(filterName))
  89. {
  90. filters.Add(filterName, filter);
  91. }
  92. else
  93. {
  94. filters[filterName] = filter;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. public void LoadFilterCSharp(string filename)
  101. {
  102. CSharpCodeProvider compiler = new CSharpCodeProvider();
  103. LoadFilter(compiler, filename);
  104. }
  105. public void LoadFilterJScript(string filename)
  106. {
  107. JScriptCodeProvider compiler = new JScriptCodeProvider();
  108. LoadFilter(compiler, filename);
  109. }
  110. }
  111. }