WorldScripting.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. private void LoadScriptEngines()
  16. {
  17. this.LoadScriptPlugins();
  18. }
  19. public void LoadScriptPlugins()
  20. {
  21. string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ScriptEngines");
  22. string[] pluginFiles = Directory.GetFiles(path, "*.dll");
  23. for (int i = 0; i < pluginFiles.Length; i++)
  24. {
  25. this.AddPlugin(pluginFiles[i]);
  26. }
  27. }
  28. private void AddPlugin(string FileName)
  29. {
  30. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  31. foreach (Type pluginType in pluginAssembly.GetTypes())
  32. {
  33. if (pluginType.IsPublic)
  34. {
  35. if (!pluginType.IsAbstract)
  36. {
  37. Type typeInterface = pluginType.GetInterface("IScriptEngine", true);
  38. if (typeInterface != null)
  39. {
  40. IScriptEngine plug = (IScriptEngine)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  41. plug.Init(this);
  42. this.scriptEngines.Add(plug.GetName(), plug);
  43. }
  44. typeInterface = null;
  45. }
  46. }
  47. }
  48. pluginAssembly = null;
  49. }
  50. public void LoadScript(string scriptType, string scriptName, string script, Entity ent)
  51. {
  52. if(this.scriptEngines.ContainsKey(scriptType))
  53. {
  54. this.scriptEngines[scriptType].LoadScript(script, scriptName, ent.localid);
  55. }
  56. }
  57. #region IScriptAPI Methods
  58. public OSVector3 GetEntityPosition(uint localID)
  59. {
  60. OSVector3 res = new OSVector3();
  61. // Console.WriteLine("script- getting entity " + localID + " position");
  62. foreach (Entity entity in this.Entities.Values)
  63. {
  64. if (entity.localid == localID)
  65. {
  66. res.X = entity.Pos.X;
  67. res.Y = entity.Pos.Y;
  68. res.Z = entity.Pos.Z;
  69. }
  70. }
  71. return res;
  72. }
  73. public void SetEntityPosition(uint localID, float x , float y, float z)
  74. {
  75. foreach (Entity entity in this.Entities.Values)
  76. {
  77. if (entity.localid == localID && entity is Primitive)
  78. {
  79. LLVector3 pos = entity.Pos;
  80. pos.X = x;
  81. pos.Y = y;
  82. Primitive prim = entity as Primitive;
  83. // Of course, we really should have asked the physEngine if this is possible, and if not, returned false.
  84. prim.UpdatePosition(pos);
  85. // Console.WriteLine("script- setting entity " + localID + " positon");
  86. }
  87. }
  88. }
  89. public uint GetRandomAvatarID()
  90. {
  91. //Console.WriteLine("script- getting random avatar id");
  92. uint res = 0;
  93. foreach (Entity entity in this.Entities.Values)
  94. {
  95. if (entity is Avatar)
  96. {
  97. res = entity.localid;
  98. }
  99. }
  100. return res;
  101. }
  102. #endregion
  103. }
  104. }