ServerUtils.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 OpenSimulator 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.IO;
  29. using System.Reflection;
  30. using System.Xml;
  31. using System.Xml.Serialization;
  32. using System.Text;
  33. using log4net;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Server.Base
  36. {
  37. public static class ServerUtils
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public static string SLAssetTypeToContentType(int assetType)
  41. {
  42. switch (assetType)
  43. {
  44. case 0:
  45. return "image/jp2";
  46. case 1:
  47. return "application/ogg";
  48. case 2:
  49. return "application/x-metaverse-callingcard";
  50. case 3:
  51. return "application/x-metaverse-landmark";
  52. case 5:
  53. return "application/x-metaverse-clothing";
  54. case 6:
  55. return "application/x-metaverse-primitive";
  56. case 7:
  57. return "application/x-metaverse-notecard";
  58. case 8:
  59. return "application/x-metaverse-folder";
  60. case 10:
  61. return "application/x-metaverse-lsl";
  62. case 11:
  63. return "application/x-metaverse-lso";
  64. case 12:
  65. return "image/tga";
  66. case 13:
  67. return "application/x-metaverse-bodypart";
  68. case 17:
  69. return "audio/x-wav";
  70. case 19:
  71. return "image/jpeg";
  72. case 20:
  73. return "application/x-metaverse-animation";
  74. case 21:
  75. return "application/x-metaverse-gesture";
  76. case 22:
  77. return "application/x-metaverse-simstate";
  78. default:
  79. return "application/octet-stream";
  80. }
  81. }
  82. public static byte[] SerializeResult(XmlSerializer xs, object data)
  83. {
  84. MemoryStream ms = new MemoryStream();
  85. XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
  86. xw.Formatting = Formatting.Indented;
  87. xs.Serialize(xw, data);
  88. xw.Flush();
  89. ms.Seek(0, SeekOrigin.Begin);
  90. byte[] ret = ms.GetBuffer();
  91. Array.Resize(ref ret, (int)ms.Length);
  92. return ret;
  93. }
  94. public static T LoadPlugin<T>(string dllName, Object[] args) where T:class
  95. {
  96. string[] parts = dllName.Split(new char[] {':'});
  97. dllName = parts[0];
  98. string className = String.Empty;
  99. if (parts.Length > 1)
  100. className = parts[1];
  101. return LoadPlugin<T>(dllName, className, args);
  102. }
  103. public static T LoadPlugin<T>(string dllName, string className, Object[] args) where T:class
  104. {
  105. string interfaceName = typeof(T).ToString();
  106. try
  107. {
  108. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  109. foreach (Type pluginType in pluginAssembly.GetTypes())
  110. {
  111. if (pluginType.IsPublic)
  112. {
  113. if (className != String.Empty &&
  114. pluginType.ToString() !=
  115. pluginType.Namespace + "." + className)
  116. continue;
  117. Type typeInterface =
  118. pluginType.GetInterface(interfaceName, true);
  119. if (typeInterface != null)
  120. {
  121. T plug = null;
  122. try
  123. {
  124. plug = (T)Activator.CreateInstance(pluginType,
  125. args);
  126. }
  127. catch (Exception e)
  128. {
  129. m_log.ErrorFormat("Error loading plugin from {0}, exception {1}", dllName, e.InnerException);
  130. }
  131. return plug;
  132. }
  133. }
  134. }
  135. return null;
  136. }
  137. catch (Exception e)
  138. {
  139. m_log.ErrorFormat("Error loading plugin from {0}, exception {1}", dllName, e);
  140. return null;
  141. }
  142. }
  143. }
  144. }