TerrainFilter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.CodeDom.Compiler;
  30. using System.Collections.Generic;
  31. using libTerrain;
  32. using Microsoft.CSharp;
  33. using Microsoft.JScript;
  34. namespace OpenSim.Region.Terrain
  35. {
  36. public interface ITerrainFilter
  37. {
  38. void Filter(Channel heightmap, string[] args);
  39. string Register();
  40. string Help();
  41. }
  42. public class TestFilter : ITerrainFilter
  43. {
  44. public void Filter(Channel heightmap, string[] args)
  45. {
  46. Console.WriteLine("Hello world");
  47. }
  48. public string Register()
  49. {
  50. return "demofilter";
  51. }
  52. public string Help()
  53. {
  54. return "demofilter - Does nothing";
  55. }
  56. }
  57. public class FilterHost
  58. {
  59. public Dictionary<string, ITerrainFilter> filters = new Dictionary<string, ITerrainFilter>();
  60. private void LoadFilter(CodeDomProvider compiler, string filename)
  61. {
  62. CompilerParameters compilerParams = new CompilerParameters();
  63. CompilerResults compilerResults;
  64. compilerParams.GenerateExecutable = false;
  65. compilerParams.GenerateInMemory = true;
  66. compilerParams.IncludeDebugInformation = false;
  67. compilerParams.ReferencedAssemblies.Add("libTerrain-BSD.dll");
  68. compilerParams.ReferencedAssemblies.Add("OpenSim.Terrain.BasicTerrain.dll");
  69. compilerParams.ReferencedAssemblies.Add("System.dll");
  70. compilerResults = compiler.CompileAssemblyFromFile(compilerParams, filename);
  71. if (compilerResults.Errors.Count > 0)
  72. {
  73. Console.WriteLine("Compile errors:");
  74. foreach (CompilerError error in compilerResults.Errors)
  75. {
  76. Console.WriteLine(error.Line.ToString() + ": " + error.ErrorText.ToString());
  77. }
  78. }
  79. else
  80. {
  81. foreach (Type pluginType in compilerResults.CompiledAssembly.GetExportedTypes())
  82. {
  83. Type testInterface = pluginType.GetInterface("ITerrainFilter",true);
  84. if (testInterface != null)
  85. {
  86. ITerrainFilter filter = (ITerrainFilter)compilerResults.CompiledAssembly.CreateInstance(pluginType.ToString());
  87. string filterName = filter.Register();
  88. Console.WriteLine("Plugin: " + filterName + " loaded.");
  89. if (!filters.ContainsKey(filterName))
  90. {
  91. filters.Add(filterName, filter);
  92. }
  93. else
  94. {
  95. filters[filterName] = filter;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. public void LoadFilterCSharp(string filename)
  102. {
  103. CSharpCodeProvider compiler = new CSharpCodeProvider();
  104. LoadFilter(compiler, filename);
  105. }
  106. public void LoadFilterJScript(string filename)
  107. {
  108. JScriptCodeProvider compiler = new JScriptCodeProvider();
  109. LoadFilter(compiler, filename);
  110. }
  111. }
  112. }