World.Scripting.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Reflection;
  6. using OpenSim.Framework;
  7. using OpenSim.Framework.Interfaces;
  8. using OpenSim.Framework.Types;
  9. using libsecondlife;
  10. namespace OpenSim.world
  11. {
  12. public partial class World
  13. {
  14. private Dictionary<string, IScriptEngine> scriptEngines = new Dictionary<string, IScriptEngine>();
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. private void LoadScriptEngines()
  19. {
  20. this.LoadScriptPlugins();
  21. }
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public void LoadScriptPlugins()
  26. {
  27. string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ScriptEngines");
  28. string[] pluginFiles = Directory.GetFiles(path, "*.dll");
  29. for (int i = 0; i < pluginFiles.Length; i++)
  30. {
  31. this.AddPlugin(pluginFiles[i]);
  32. }
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="FileName"></param>
  38. private void AddPlugin(string FileName)
  39. {
  40. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  41. foreach (Type pluginType in pluginAssembly.GetTypes())
  42. {
  43. if (pluginType.IsPublic)
  44. {
  45. if (!pluginType.IsAbstract)
  46. {
  47. Type typeInterface = pluginType.GetInterface("IScriptEngine", true);
  48. if (typeInterface != null)
  49. {
  50. IScriptEngine plug = (IScriptEngine)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  51. plug.Init(this);
  52. this.scriptEngines.Add(plug.GetName(), plug);
  53. }
  54. typeInterface = null;
  55. }
  56. }
  57. }
  58. pluginAssembly = null;
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. /// <param name="scriptType"></param>
  64. /// <param name="scriptName"></param>
  65. /// <param name="script"></param>
  66. /// <param name="ent"></param>
  67. public void LoadScript(string scriptType, string scriptName, string script, Entity ent)
  68. {
  69. if(this.scriptEngines.ContainsKey(scriptType))
  70. {
  71. this.scriptEngines[scriptType].LoadScript(script, scriptName, ent.localid);
  72. }
  73. }
  74. #region IScriptAPI Methods
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. /// <param name="localID"></param>
  79. /// <returns></returns>
  80. public OSVector3 GetEntityPosition(uint localID)
  81. {
  82. OSVector3 res = new OSVector3();
  83. // Console.WriteLine("script- getting entity " + localID + " position");
  84. foreach (Entity entity in this.Entities.Values)
  85. {
  86. if (entity.localid == localID)
  87. {
  88. res.X = entity.Pos.X;
  89. res.Y = entity.Pos.Y;
  90. res.Z = entity.Pos.Z;
  91. }
  92. }
  93. return res;
  94. }
  95. /// <summary>
  96. ///
  97. /// </summary>
  98. /// <param name="localID"></param>
  99. /// <param name="x"></param>
  100. /// <param name="y"></param>
  101. /// <param name="z"></param>
  102. public void SetEntityPosition(uint localID, float x , float y, float z)
  103. {
  104. foreach (Entity entity in this.Entities.Values)
  105. {
  106. if (entity.localid == localID && entity is Primitive)
  107. {
  108. LLVector3 pos = entity.Pos;
  109. pos.X = x;
  110. pos.Y = y;
  111. Primitive prim = entity as Primitive;
  112. // Of course, we really should have asked the physEngine if this is possible, and if not, returned false.
  113. //prim.UpdatePosition(pos);
  114. // Console.WriteLine("script- setting entity " + localID + " positon");
  115. }
  116. }
  117. }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. /// <returns></returns>
  122. public uint GetRandomAvatarID()
  123. {
  124. //Console.WriteLine("script- getting random avatar id");
  125. uint res = 0;
  126. foreach (Entity entity in this.Entities.Values)
  127. {
  128. if (entity is Avatar)
  129. {
  130. res = entity.localid;
  131. }
  132. }
  133. return res;
  134. }
  135. #endregion
  136. }
  137. }