Scene.cs 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372
  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.Collections.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Xml;
  33. using System.Threading;
  34. using System.Timers;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Imaging;
  37. using OpenMetaverse.Packets;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Communications;
  40. using OpenSim.Framework.Communications.Cache;
  41. using OpenSim.Framework.Servers;
  42. using OpenSim.Region.Environment.Interfaces;
  43. using OpenSim.Region.Environment.Modules.World.Archiver;
  44. using OpenSim.Region.Environment.Modules.World.Serialiser;
  45. using OpenSim.Region.Environment.Modules.World.Terrain;
  46. using OpenSim.Region.Environment.Scenes.Scripting;
  47. using OpenSim.Region.Physics.Manager;
  48. using Nini.Config;
  49. using Caps = OpenSim.Framework.Communications.Capabilities.Caps;
  50. using Image = System.Drawing.Image;
  51. using TPFlags = OpenSim.Framework.Constants.TeleportFlags;
  52. using Timer = System.Timers.Timer;
  53. namespace OpenSim.Region.Environment.Scenes
  54. {
  55. public delegate bool FilterAvatarList(ScenePresence avatar);
  56. public partial class Scene : SceneBase
  57. {
  58. public delegate void SynchronizeSceneHandler(Scene scene);
  59. public SynchronizeSceneHandler SynchronizeScene = null;
  60. public int splitID = 0;
  61. #region Fields
  62. protected Timer m_restartWaitTimer = new Timer();
  63. protected SimStatsReporter m_statsReporter;
  64. protected List<RegionInfo> m_regionRestartNotifyList = new List<RegionInfo>();
  65. protected List<RegionInfo> m_neighbours = new List<RegionInfo>();
  66. public InnerScene m_innerScene;
  67. private int m_timePhase = 24;
  68. /// <summary>
  69. /// Are we applying physics to any of the prims in this scene?
  70. /// </summary>
  71. public bool m_physicalPrim;
  72. public float m_maxNonphys = 65536;
  73. public float m_maxPhys = 10;
  74. public bool m_seeIntoRegionFromNeighbor;
  75. public int MaxUndoCount = 5;
  76. private int m_RestartTimerCounter;
  77. private readonly Timer m_restartTimer = new Timer(15000); // Wait before firing
  78. private int m_incrementsof15seconds = 0;
  79. private volatile bool m_backingup = false;
  80. private Dictionary<UUID, ReturnInfo> m_returns = new Dictionary<UUID, ReturnInfo>();
  81. protected string m_simulatorVersion = "OpenSimulator Server";
  82. protected ModuleLoader m_moduleLoader;
  83. protected StorageManager m_storageManager;
  84. protected AgentCircuitManager m_authenticateHandler;
  85. public CommunicationsManager CommsManager;
  86. protected SceneCommunicationService m_sceneGridService;
  87. public SceneCommunicationService SceneGridService
  88. {
  89. get { return m_sceneGridService; }
  90. }
  91. /// <summary>
  92. /// Each agent has its own capabilities handler.
  93. /// </summary>
  94. protected Dictionary<UUID, Caps> m_capsHandlers = new Dictionary<UUID, Caps>();
  95. protected BaseHttpServer m_httpListener;
  96. protected Dictionary<string, IRegionModule> m_modules = new Dictionary<string, IRegionModule>();
  97. public Dictionary<string, IRegionModule> Modules
  98. {
  99. get { return m_modules; }
  100. }
  101. protected Dictionary<Type, List<object> > ModuleInterfaces = new Dictionary<Type, List<object> >();
  102. protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
  103. protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
  104. //API module interfaces
  105. public IXfer XferManager;
  106. protected IHttpRequests m_httpRequestModule;
  107. protected IXMLRPC m_xmlrpcModule;
  108. protected IWorldComm m_worldCommModule;
  109. protected IAvatarFactory m_AvatarFactory;
  110. protected IConfigSource m_config;
  111. protected IRegionArchiver m_archiver;
  112. protected IRegionSerialiser m_serialiser;
  113. // Central Update Loop
  114. protected int m_fps = 10;
  115. protected int m_frame = 0;
  116. protected float m_timespan = 0.089f;
  117. protected DateTime m_lastupdate = DateTime.Now;
  118. protected float m_timedilation = 1.0f;
  119. private int m_update_physics = 1;
  120. private int m_update_entitymovement = 1;
  121. private int m_update_entities = 1; // Run through all objects checking for updates
  122. private int m_update_entitiesquick = 200; // Run through objects that have scheduled updates checking for updates
  123. private int m_update_presences = 1; // Update scene presence movements
  124. private int m_update_events = 1;
  125. private int m_update_backup = 200;
  126. private int m_update_terrain = 50;
  127. private int m_update_land = 1;
  128. private int frameMS = 0;
  129. private int physicsMS2 = 0;
  130. private int physicsMS = 0;
  131. private int otherMS = 0;
  132. private bool m_physics_enabled = true;
  133. private bool m_scripts_enabled = true;
  134. private string m_defaultScriptEngine;
  135. private int m_LastLogin = 0;
  136. private Thread HeartbeatThread;
  137. private volatile bool shuttingdown = false;
  138. private object m_deleting_scene_object = new object();
  139. #endregion
  140. #region Properties
  141. public AgentCircuitManager AuthenticateHandler
  142. {
  143. get { return m_authenticateHandler; }
  144. }
  145. // an instance to the physics plugin's Scene object.
  146. public PhysicsScene PhysicsScene
  147. {
  148. set { m_innerScene.PhysicsScene = value; }
  149. get { return (m_innerScene.PhysicsScene); }
  150. }
  151. // This gets locked so things stay thread safe.
  152. public object SyncRoot
  153. {
  154. get { return m_innerScene.m_syncRoot; }
  155. }
  156. public float TimeDilation
  157. {
  158. get { return m_timedilation; }
  159. }
  160. /// <summary>
  161. /// This is for llGetRegionFPS
  162. /// </summary>
  163. public float SimulatorFPS
  164. {
  165. get { return m_statsReporter.getLastReportedSimFPS(); }
  166. }
  167. public int TimePhase
  168. {
  169. get { return m_timePhase; }
  170. }
  171. public string DefaultScriptEngine
  172. {
  173. get { return m_defaultScriptEngine; }
  174. }
  175. // Local reference to the objects in the scene (which are held in innerScene)
  176. // public Dictionary<UUID, SceneObjectGroup> Objects
  177. // {
  178. // get { return m_innerScene.SceneObjects; }
  179. // }
  180. // Reference to all of the agents in the scene (root and child)
  181. protected Dictionary<UUID, ScenePresence> m_scenePresences
  182. {
  183. get { return m_innerScene.ScenePresences; }
  184. set { m_innerScene.ScenePresences = value; }
  185. }
  186. // protected Dictionary<UUID, SceneObjectGroup> m_sceneObjects
  187. // {
  188. // get { return m_innerScene.SceneObjects; }
  189. // set { m_innerScene.SceneObjects = value; }
  190. // }
  191. /// <summary>
  192. /// The dictionary of all entities in this scene. The contents of this dictionary may be changed at any time.
  193. /// If you wish to add or remove entities, please use the appropriate method for that entity rather than
  194. /// editing this dictionary directly.
  195. ///
  196. /// If you want a list of entities where the list itself is guaranteed not to change, please use
  197. /// GetEntities()
  198. /// </summary>
  199. public Dictionary<UUID, EntityBase> Entities
  200. {
  201. get { return m_innerScene.Entities; }
  202. set { m_innerScene.Entities = value; }
  203. }
  204. public Dictionary<UUID, ScenePresence> m_restorePresences
  205. {
  206. get { return m_innerScene.RestorePresences; }
  207. set { m_innerScene.RestorePresences = value; }
  208. }
  209. public int objectCapacity = 45000;
  210. #endregion
  211. #region Constructors
  212. public Scene(RegionInfo regInfo, AgentCircuitManager authen,
  213. CommunicationsManager commsMan, SceneCommunicationService sceneGridService,
  214. AssetCache assetCach, StorageManager storeManager, BaseHttpServer httpServer,
  215. ModuleLoader moduleLoader, bool dumpAssetsToFile, bool physicalPrim,
  216. bool SeeIntoRegionFromNeighbor, IConfigSource config, string simulatorVersion)
  217. {
  218. m_config = config;
  219. m_moduleLoader = moduleLoader;
  220. m_authenticateHandler = authen;
  221. CommsManager = commsMan;
  222. m_sceneGridService = sceneGridService;
  223. m_storageManager = storeManager;
  224. AssetCache = assetCach;
  225. m_regInfo = regInfo;
  226. m_regionHandle = m_regInfo.RegionHandle;
  227. m_regionName = m_regInfo.RegionName;
  228. m_datastore = m_regInfo.DataStore;
  229. m_physicalPrim = physicalPrim;
  230. m_seeIntoRegionFromNeighbor = SeeIntoRegionFromNeighbor;
  231. m_eventManager = new EventManager();
  232. m_externalChecks = new SceneExternalChecks(this);
  233. m_asyncSceneObjectDeleter = new AsyncSceneObjectGroupDeleter(this);
  234. // Load region settings
  235. m_regInfo.RegionSettings = m_storageManager.DataStore.LoadRegionSettings(m_regInfo.RegionID);
  236. if (m_storageManager.EstateDataStore != null)
  237. m_regInfo.EstateSettings = m_storageManager.EstateDataStore.LoadEstateSettings(m_regInfo.RegionID);
  238. //Bind Storage Manager functions to some land manager functions for this scene
  239. EventManager.OnLandObjectAdded +=
  240. new EventManager.LandObjectAdded(m_storageManager.DataStore.StoreLandObject);
  241. EventManager.OnLandObjectRemoved +=
  242. new EventManager.LandObjectRemoved(m_storageManager.DataStore.RemoveLandObject);
  243. m_innerScene = new InnerScene(this, m_regInfo);
  244. // If the Inner scene has an Unrecoverable error, restart this sim.
  245. // Currently the only thing that causes it to happen is two kinds of specific
  246. // Physics based crashes.
  247. //
  248. // Out of memory
  249. // Operating system has killed the plugin
  250. m_innerScene.UnRecoverableError += RestartNow;
  251. RegisterDefaultSceneEvents();
  252. m_httpListener = httpServer;
  253. m_dumpAssetsToFile = dumpAssetsToFile;
  254. m_scripts_enabled = !RegionInfo.RegionSettings.DisableScripts;
  255. m_physics_enabled = !RegionInfo.RegionSettings.DisablePhysics;
  256. m_statsReporter = new SimStatsReporter(this);
  257. m_statsReporter.OnSendStatsResult += SendSimStatsPackets;
  258. m_statsReporter.SetObjectCapacity(objectCapacity);
  259. m_simulatorVersion = simulatorVersion
  260. + " ChilTasks:" + m_seeIntoRegionFromNeighbor.ToString()
  261. + " PhysPrim:" + m_physicalPrim.ToString();
  262. try
  263. {
  264. IConfig startupConfig = m_config.Configs["Startup"];
  265. m_maxNonphys = startupConfig.GetFloat("NonPhysicalPrimMax", 65536.0f);
  266. m_maxPhys = startupConfig.GetFloat("PhysicalPrimMax", 10.0f);
  267. m_defaultScriptEngine = startupConfig.GetString("DefaultScriptEngine", "DotNetEngine");
  268. }
  269. catch
  270. {
  271. m_log.Warn("[SCENE]: Failed to load StartupConfig");
  272. }
  273. }
  274. #endregion
  275. #region Startup / Close Methods
  276. protected virtual void RegisterDefaultSceneEvents()
  277. {
  278. m_eventManager.OnPermissionError += SendPermissionAlert;
  279. }
  280. public override string GetSimulatorVersion()
  281. {
  282. return m_simulatorVersion;
  283. }
  284. /// <summary>
  285. /// Another region is up. Gets called from Grid Comms:
  286. /// (OGS1 -> LocalBackEnd -> RegionListened -> SceneCommunicationService)
  287. /// We have to tell all our ScenePresences about it, and add it to the
  288. /// neighbor list.
  289. ///
  290. /// We only add it to the neighbor list if it's within 1 region from here.
  291. /// Agents may have draw distance values that cross two regions though, so
  292. /// we add it to the notify list regardless of distance. We'll check
  293. /// the agent's draw distance before notifying them though.
  294. /// </summary>
  295. /// <param name="otherRegion">RegionInfo handle for the new region.</param>
  296. /// <returns>True after all operations complete, throws exceptions otherwise.</returns>
  297. public override bool OtherRegionUp(RegionInfo otherRegion)
  298. {
  299. if (RegionInfo.RegionHandle != otherRegion.RegionHandle)
  300. {
  301. for (int i = 0; i < m_neighbours.Count; i++)
  302. {
  303. // The purpose of this loop is to re-update the known neighbors
  304. // when another region comes up on top of another one.
  305. // The latest region in that location ends up in the
  306. // 'known neighbors list'
  307. // Additionally, the commFailTF property gets reset to false.
  308. if (m_neighbours[i].RegionHandle == otherRegion.RegionHandle)
  309. {
  310. lock (m_neighbours)
  311. {
  312. m_neighbours[i] = otherRegion;
  313. }
  314. }
  315. }
  316. // If the value isn't in the neighbours, add it.
  317. // If the RegionInfo isn't exact but is for the same XY World location,
  318. // then the above loop will fix that.
  319. if (!(CheckNeighborRegion(otherRegion)))
  320. {
  321. lock (m_neighbours)
  322. {
  323. m_neighbours.Add(otherRegion);
  324. //m_log.Info("[UP]: " + otherRegion.RegionHandle.ToString());
  325. }
  326. }
  327. // If these are cast to INT because long + negative values + abs returns invalid data
  328. int resultX = Math.Abs((int)otherRegion.RegionLocX - (int)RegionInfo.RegionLocX);
  329. int resultY = Math.Abs((int)otherRegion.RegionLocY - (int)RegionInfo.RegionLocY);
  330. if (resultX <= 1 && resultY <= 1)
  331. {
  332. try
  333. {
  334. ForEachScenePresence(delegate(ScenePresence agent)
  335. {
  336. // If agent is a root agent.
  337. if (!agent.IsChildAgent)
  338. {
  339. //agent.ControllingClient.new
  340. //this.CommsManager.InterRegion.InformRegionOfChildAgent(otherRegion.RegionHandle, agent.ControllingClient.RequestClientInfo());
  341. InformClientOfNeighbor(agent, otherRegion);
  342. }
  343. }
  344. );
  345. }
  346. catch (NullReferenceException)
  347. {
  348. // This means that we're not booted up completely yet.
  349. // This shouldn't happen too often anymore.
  350. m_log.Error("[SCENE]: Couldn't inform client of regionup because we got a null reference exception");
  351. }
  352. }
  353. else
  354. {
  355. m_log.Info("[INTERGRID]: Got notice about far away Region: " + otherRegion.RegionName.ToString() +
  356. " at (" + otherRegion.RegionLocX.ToString() + ", " +
  357. otherRegion.RegionLocY.ToString() + ")");
  358. }
  359. }
  360. return true;
  361. }
  362. public void AddNeighborRegion(RegionInfo region)
  363. {
  364. lock (m_neighbours)
  365. {
  366. if (!CheckNeighborRegion(region))
  367. {
  368. m_neighbours.Add(region);
  369. }
  370. }
  371. }
  372. public bool CheckNeighborRegion(RegionInfo region)
  373. {
  374. bool found = false;
  375. lock (m_neighbours)
  376. {
  377. foreach (RegionInfo reg in m_neighbours)
  378. {
  379. if (reg.RegionHandle == region.RegionHandle)
  380. {
  381. found = true;
  382. break;
  383. }
  384. }
  385. }
  386. return found;
  387. }
  388. /// <summary>
  389. /// Given float seconds, this will restart the region.
  390. /// </summary>
  391. /// <param name="seconds">float indicating duration before restart.</param>
  392. public virtual void Restart(float seconds)
  393. {
  394. // notifications are done in 15 second increments
  395. // so .. if the number of seconds is less then 15 seconds, it's not really a restart request
  396. // It's a 'Cancel restart' request.
  397. // RestartNow() does immediate restarting.
  398. if (seconds < 15)
  399. {
  400. m_restartTimer.Stop();
  401. SendGeneralAlert("Restart Aborted");
  402. }
  403. else
  404. {
  405. // Now we figure out what to set the timer to that does the notifications and calls, RestartNow()
  406. m_restartTimer.Interval = 15000;
  407. m_incrementsof15seconds = (int)seconds / 15;
  408. m_RestartTimerCounter = 0;
  409. m_restartTimer.AutoReset = true;
  410. m_restartTimer.Elapsed += new ElapsedEventHandler(RestartTimer_Elapsed);
  411. m_log.Error("[REGION]: Restarting Region in " + (seconds / 60) + " minutes");
  412. m_restartTimer.Start();
  413. SendRegionMessageFromEstateTools(UUID.Random(), UUID.Random(), String.Empty, RegionInfo.RegionName + ": Restarting in 2 Minutes");
  414. //SendGeneralAlert(RegionInfo.RegionName + ": Restarting in 2 Minutes");
  415. }
  416. }
  417. // The Restart timer has occured.
  418. // We have to figure out if this is a notification or if the number of seconds specified in Restart
  419. // have elapsed.
  420. // If they have elapsed, call RestartNow()
  421. public void RestartTimer_Elapsed(object sender, ElapsedEventArgs e)
  422. {
  423. m_RestartTimerCounter++;
  424. if (m_RestartTimerCounter <= m_incrementsof15seconds)
  425. {
  426. if (m_RestartTimerCounter == 4 || m_RestartTimerCounter == 6 || m_RestartTimerCounter == 7)
  427. SendRegionMessageFromEstateTools(UUID.Random(), UUID.Random(), String.Empty, RegionInfo.RegionName + ": Restarting in " +
  428. ((8 - m_RestartTimerCounter) * 15) + " seconds");
  429. // SendGeneralAlert(RegionInfo.RegionName + ": Restarting in " + ((8 - m_RestartTimerCounter)*15) +
  430. //" seconds");
  431. }
  432. else
  433. {
  434. m_restartTimer.Stop();
  435. m_restartTimer.AutoReset = false;
  436. RestartNow();
  437. }
  438. }
  439. // This causes the region to restart immediatley.
  440. public void RestartNow()
  441. {
  442. if (PhysicsScene != null)
  443. {
  444. PhysicsScene.Dispose();
  445. }
  446. m_log.Error("[REGION]: Closing");
  447. Close();
  448. m_log.Error("[REGION]: Firing Region Restart Message");
  449. base.Restart(0);
  450. }
  451. // This is a helper function that notifies root agents in this region that a new sim near them has come up
  452. // This is in the form of a timer because when an instance of OpenSim.exe is started,
  453. // Even though the sims initialize, they don't listen until 'all of the sims are initialized'
  454. // If we tell an agent about a sim that's not listening yet, the agent will not be able to connect to it.
  455. // subsequently the agent will never see the region come back online.
  456. public void RestartNotifyWaitElapsed(object sender, ElapsedEventArgs e)
  457. {
  458. m_restartWaitTimer.Stop();
  459. lock (m_regionRestartNotifyList)
  460. {
  461. foreach (RegionInfo region in m_regionRestartNotifyList)
  462. {
  463. try
  464. {
  465. ForEachScenePresence(delegate(ScenePresence agent)
  466. {
  467. // If agent is a root agent.
  468. if (!agent.IsChildAgent)
  469. {
  470. //agent.ControllingClient.new
  471. //this.CommsManager.InterRegion.InformRegionOfChildAgent(otherRegion.RegionHandle, agent.ControllingClient.RequestClientInfo());
  472. InformClientOfNeighbor(agent, region);
  473. }
  474. }
  475. );
  476. }
  477. catch (NullReferenceException)
  478. {
  479. // This means that we're not booted up completely yet.
  480. // This shouldn't happen too often anymore.
  481. }
  482. }
  483. // Reset list to nothing.
  484. m_regionRestartNotifyList.Clear();
  485. }
  486. }
  487. public void SetSceneCoreDebug(bool ScriptEngine, bool CollisionEvents, bool PhysicsEngine)
  488. {
  489. if (m_scripts_enabled != !ScriptEngine)
  490. {
  491. // Tedd! Here's the method to disable the scripting engine!
  492. if (ScriptEngine)
  493. {
  494. m_log.Info("Stopping all Scripts in Scene");
  495. lock (Entities)
  496. {
  497. foreach (EntityBase ent in Entities.Values)
  498. {
  499. if (ent is SceneObjectGroup)
  500. {
  501. ((SceneObjectGroup)ent).RemoveScriptInstances();
  502. }
  503. }
  504. }
  505. }
  506. else
  507. {
  508. m_log.Info("Starting all Scripts in Scene");
  509. lock (Entities)
  510. {
  511. foreach (EntityBase ent in Entities.Values)
  512. {
  513. if (ent is SceneObjectGroup)
  514. {
  515. ((SceneObjectGroup)ent).CreateScriptInstances(0, false, DefaultScriptEngine, 0);
  516. }
  517. }
  518. }
  519. }
  520. m_scripts_enabled = !ScriptEngine;
  521. m_log.Info("[TOTEDD]: Here is the method to trigger disabling of the scripting engine");
  522. }
  523. if (m_physics_enabled != !PhysicsEngine)
  524. {
  525. m_physics_enabled = !PhysicsEngine;
  526. }
  527. }
  528. public int GetInaccurateNeighborCount()
  529. {
  530. lock (m_neighbours)
  531. {
  532. return m_neighbours.Count;
  533. }
  534. }
  535. // This is the method that shuts down the scene.
  536. public override void Close()
  537. {
  538. m_log.InfoFormat("[SCENE]: Closing down the single simulator: {0}", RegionInfo.RegionName);
  539. // Kick all ROOT agents with the message, 'The simulator is going down'
  540. ForEachScenePresence(delegate(ScenePresence avatar)
  541. {
  542. if (avatar.KnownChildRegions.Contains(RegionInfo.RegionHandle))
  543. avatar.KnownChildRegions.Remove(RegionInfo.RegionHandle);
  544. if (!avatar.IsChildAgent)
  545. avatar.ControllingClient.Kick("The simulator is going down.");
  546. avatar.ControllingClient.SendShutdownConnectionNotice();
  547. });
  548. // Wait here, or the kick messages won't actually get to the agents before the scene terminates.
  549. Thread.Sleep(500);
  550. // Stop all client threads.
  551. ForEachScenePresence(delegate(ScenePresence avatar) { avatar.ControllingClient.Close(true); });
  552. // Stop updating the scene objects and agents.
  553. //m_heartbeatTimer.Close();
  554. shuttingdown = true;
  555. // close the inner scene
  556. m_innerScene.Close();
  557. // De-register with region communications (events cleanup)
  558. UnRegisterRegionWithComms();
  559. // Shut down all non shared modules.
  560. foreach (IRegionModule module in Modules.Values)
  561. {
  562. if (!module.IsSharedModule)
  563. {
  564. module.Close();
  565. }
  566. }
  567. Modules.Clear();
  568. // call the base class Close method.
  569. base.Close();
  570. }
  571. /// <summary>
  572. /// Start the timer which triggers regular scene updates
  573. /// </summary>
  574. public void StartTimer()
  575. {
  576. //m_log.Debug("[SCENE]: Starting timer");
  577. //m_heartbeatTimer.Enabled = true;
  578. //m_heartbeatTimer.Interval = (int)(m_timespan * 1000);
  579. //m_heartbeatTimer.Elapsed += new ElapsedEventHandler(Heartbeat);
  580. HeartbeatThread = new Thread(new ParameterizedThreadStart(Heartbeat));
  581. HeartbeatThread.SetApartmentState(ApartmentState.MTA);
  582. HeartbeatThread.Name = "Heartbeat";
  583. HeartbeatThread.Priority = ThreadPriority.AboveNormal;
  584. ThreadTracker.Add(HeartbeatThread);
  585. HeartbeatThread.Start();
  586. }
  587. /// <summary>
  588. /// Sets up references to modules required by the scene
  589. /// </summary>
  590. public void SetModuleInterfaces()
  591. {
  592. m_httpRequestModule = RequestModuleInterface<IHttpRequests>();
  593. m_xmlrpcModule = RequestModuleInterface<IXMLRPC>();
  594. m_worldCommModule = RequestModuleInterface<IWorldComm>();
  595. XferManager = RequestModuleInterface<IXfer>();
  596. m_AvatarFactory = RequestModuleInterface<IAvatarFactory>();
  597. m_archiver = RequestModuleInterface<IRegionArchiver>();
  598. m_serialiser = RequestModuleInterface<IRegionSerialiser>();
  599. }
  600. #endregion
  601. #region Update Methods
  602. /// <summary>
  603. /// Performs per-frame updates regularly
  604. /// </summary>
  605. /// <param name="sender"></param>
  606. /// <param name="e"></param>
  607. private void Heartbeat(object sender)
  608. {
  609. Update();
  610. }
  611. /// <summary>
  612. /// Performs per-frame updates on the scene, this should be the central scene loop
  613. /// </summary>
  614. public override void Update()
  615. {
  616. int maintc = 0;
  617. while (!shuttingdown)
  618. {
  619. maintc = System.Environment.TickCount;
  620. TimeSpan SinceLastFrame = DateTime.Now - m_lastupdate;
  621. // Aquire a lock so only one update call happens at once
  622. //updateLock.WaitOne();
  623. float physicsFPS = 0;
  624. //m_log.Info("sadfadf" + m_neighbours.Count.ToString());
  625. int agentsInScene = m_innerScene.GetRootAgentCount() + m_innerScene.GetChildAgentCount();
  626. if (agentsInScene > 21)
  627. {
  628. if (m_update_entities == 1)
  629. {
  630. m_update_entities = 5;
  631. m_statsReporter.SetUpdateMS(6000);
  632. }
  633. }
  634. else
  635. {
  636. if (m_update_entities == 5)
  637. {
  638. m_update_entities = 1;
  639. m_statsReporter.SetUpdateMS(3000);
  640. }
  641. }
  642. frameMS = System.Environment.TickCount;
  643. try
  644. {
  645. // Increment the frame counter
  646. m_frame++;
  647. // Loop it
  648. if (m_frame == Int32.MaxValue)
  649. m_frame = 0;
  650. physicsMS2 = System.Environment.TickCount;
  651. if ((m_frame % m_update_physics == 0) && m_physics_enabled)
  652. m_innerScene.UpdatePreparePhysics();
  653. physicsMS2 = System.Environment.TickCount - physicsMS2;
  654. if (m_frame % m_update_entitymovement == 0)
  655. m_innerScene.UpdateEntityMovement();
  656. physicsMS = System.Environment.TickCount;
  657. if ((m_frame % m_update_physics == 0) && m_physics_enabled)
  658. physicsFPS = m_innerScene.UpdatePhysics(
  659. Math.Max(SinceLastFrame.TotalSeconds, m_timespan)
  660. );
  661. if (m_frame % m_update_physics == 0 && SynchronizeScene != null)
  662. SynchronizeScene(this);
  663. physicsMS = System.Environment.TickCount - physicsMS;
  664. physicsMS += physicsMS2;
  665. otherMS = System.Environment.TickCount;
  666. // run through all entities looking for updates (slow)
  667. if (m_frame % m_update_entities == 0)
  668. m_innerScene.UpdateEntities();
  669. // run through entities that have scheduled themselves for
  670. // updates looking for updates(faster)
  671. if (m_frame % m_update_entitiesquick == 0)
  672. m_innerScene.ProcessUpdates();
  673. // Run through scenepresences looking for updates
  674. if (m_frame % m_update_presences == 0)
  675. m_innerScene.UpdatePresences();
  676. // Delete temp-on-rez stuff
  677. if (m_frame % m_update_backup == 0)
  678. CleanTempObjects();
  679. if (Region_Status != RegionStatus.SlaveScene)
  680. {
  681. if (m_frame % m_update_events == 0)
  682. UpdateEvents();
  683. if (m_frame % m_update_backup == 0)
  684. {
  685. UpdateStorageBackup();
  686. }
  687. if (m_frame % m_update_terrain == 0)
  688. UpdateTerrain();
  689. if (m_frame % m_update_land == 0)
  690. UpdateLand();
  691. otherMS = System.Environment.TickCount - otherMS;
  692. // if (m_frame%m_update_avatars == 0)
  693. // UpdateInWorldTime();
  694. m_statsReporter.AddPhysicsFPS(physicsFPS);
  695. m_statsReporter.AddTimeDilation(m_timedilation);
  696. m_statsReporter.AddFPS(1);
  697. m_statsReporter.AddInPackets(0);
  698. m_statsReporter.SetRootAgents(m_innerScene.GetRootAgentCount());
  699. m_statsReporter.SetChildAgents(m_innerScene.GetChildAgentCount());
  700. m_statsReporter.SetObjects(m_innerScene.GetTotalObjectsCount());
  701. m_statsReporter.SetActiveObjects(m_innerScene.GetActiveObjectsCount());
  702. frameMS = System.Environment.TickCount - frameMS;
  703. m_statsReporter.addFrameMS(frameMS);
  704. m_statsReporter.addPhysicsMS(physicsMS);
  705. m_statsReporter.addOtherMS(otherMS);
  706. m_statsReporter.SetActiveScripts(m_innerScene.GetActiveScriptsCount());
  707. m_statsReporter.addScriptLines(m_innerScene.GetScriptLPS());
  708. }
  709. }
  710. catch (NotImplementedException)
  711. {
  712. throw;
  713. }
  714. catch (AccessViolationException e)
  715. {
  716. m_log.Error("[Scene]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  717. }
  718. catch (NullReferenceException e)
  719. {
  720. m_log.Error("[Scene]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  721. }
  722. catch (InvalidOperationException e)
  723. {
  724. m_log.Error("[Scene]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  725. }
  726. catch (Exception e)
  727. {
  728. m_log.Error("[Scene]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  729. }
  730. finally
  731. {
  732. //updateLock.ReleaseMutex();
  733. // Get actual time dilation
  734. float tmpval = (m_timespan / (float)SinceLastFrame.TotalSeconds);
  735. // If actual time dilation is greater then one, we're catching up, so subtract
  736. // the amount that's greater then 1 from the time dilation
  737. if (tmpval > 1.0)
  738. {
  739. tmpval = tmpval - (tmpval - 1.0f);
  740. }
  741. m_timedilation = tmpval;
  742. m_lastupdate = DateTime.Now;
  743. }
  744. maintc = System.Environment.TickCount - maintc;
  745. maintc = (int)(m_timespan * 1000) - maintc;
  746. if ((maintc < (m_timespan * 1000)) && maintc > 0)
  747. Thread.Sleep(maintc);
  748. }
  749. }
  750. private void SendSimStatsPackets(SimStats stats)
  751. {
  752. List<ScenePresence> StatSendAgents = GetScenePresences();
  753. foreach (ScenePresence agent in StatSendAgents)
  754. {
  755. if (!agent.IsChildAgent)
  756. {
  757. agent.ControllingClient.SendSimStats(stats);
  758. }
  759. }
  760. }
  761. private void UpdateLand()
  762. {
  763. if (LandChannel != null)
  764. {
  765. if (LandChannel.IsLandPrimCountTainted())
  766. {
  767. EventManager.TriggerParcelPrimCountUpdate();
  768. }
  769. }
  770. }
  771. private void UpdateTerrain()
  772. {
  773. EventManager.TriggerTerrainTick();
  774. }
  775. private void UpdateStorageBackup()
  776. {
  777. if (!m_backingup)
  778. {
  779. m_backingup = true;
  780. Thread backupthread = new Thread(Backup);
  781. backupthread.Name = "BackupWriter";
  782. backupthread.IsBackground = true;
  783. backupthread.Start();
  784. }
  785. }
  786. private void UpdateEvents()
  787. {
  788. m_eventManager.TriggerOnFrame();
  789. }
  790. /// <summary>
  791. /// Perform delegate action on all clients subscribing to updates from this region.
  792. /// </summary>
  793. /// <returns></returns>
  794. internal void Broadcast(Action<IClientAPI> whatToDo)
  795. {
  796. ForEachScenePresence(delegate(ScenePresence presence) { whatToDo(presence.ControllingClient); });
  797. }
  798. /// <summary>
  799. /// Backup the scene. This acts as the main method of the backup thread.
  800. /// </summary>
  801. /// <returns></returns>
  802. public void Backup()
  803. {
  804. m_returns.Clear();
  805. EventManager.TriggerOnBackup(m_storageManager.DataStore);
  806. m_backingup = false;
  807. foreach (KeyValuePair<UUID, ReturnInfo> ret in m_returns)
  808. {
  809. UUID transaction = UUID.Random();
  810. GridInstantMessage msg = new GridInstantMessage();
  811. msg.fromAgentID = new Guid(UUID.Zero.ToString()); // From server
  812. msg.fromAgentSession = new Guid(transaction.ToString());
  813. msg.toAgentID = new Guid(ret.Key.ToString());
  814. msg.imSessionID = new Guid(transaction.ToString());
  815. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  816. msg.fromAgentName = "Server";
  817. msg.dialog = (byte)19; // Object msg
  818. msg.fromGroup = false;
  819. msg.offline = (byte)1;
  820. msg.ParentEstateID = RegionInfo.EstateSettings.ParentEstateID;
  821. msg.Position = Vector3.Zero;
  822. msg.RegionID = RegionInfo.RegionID.Guid;
  823. msg.binaryBucket = new byte[0];
  824. if (ret.Value.count > 1)
  825. msg.message = string.Format("Your {0} objects were returned from {1} in region {2} due to parcel auto return", ret.Value.count, ret.Value.location.ToString(), RegionInfo.RegionName);
  826. else
  827. msg.message = string.Format("Your object {0} was returned from {1} in region {2} due to parcel auto return", ret.Value.objectName, ret.Value.location.ToString(), RegionInfo.RegionName);
  828. TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  829. }
  830. }
  831. public void AddReturn(UUID agentID, string objectName, Vector3 location)
  832. {
  833. if (m_returns.ContainsKey(agentID))
  834. {
  835. ReturnInfo info = m_returns[agentID];
  836. info.count++;
  837. m_returns[agentID] = info;
  838. }
  839. else
  840. {
  841. ReturnInfo info = new ReturnInfo();
  842. info.count = 1;
  843. info.objectName = objectName;
  844. info.location = location;
  845. m_returns[agentID] = info;
  846. }
  847. }
  848. #endregion
  849. #region Load Terrain
  850. public void ExportWorldMap(string fileName)
  851. {
  852. List<MapBlockData> mapBlocks =
  853. m_sceneGridService.RequestNeighbourMapBlocks((int)(RegionInfo.RegionLocX - 9),
  854. (int)(RegionInfo.RegionLocY - 9),
  855. (int)(RegionInfo.RegionLocX + 9),
  856. (int)(RegionInfo.RegionLocY + 9));
  857. List<AssetBase> textures = new List<AssetBase>();
  858. List<Image> bitImages = new List<Image>();
  859. foreach (MapBlockData mapBlock in mapBlocks)
  860. {
  861. AssetBase texAsset = AssetCache.GetAsset(mapBlock.MapImageId, true);
  862. if (texAsset != null)
  863. {
  864. textures.Add(texAsset);
  865. }
  866. else
  867. {
  868. texAsset = AssetCache.GetAsset(mapBlock.MapImageId, true);
  869. if (texAsset != null)
  870. {
  871. textures.Add(texAsset);
  872. }
  873. }
  874. }
  875. foreach (AssetBase asset in textures)
  876. {
  877. ManagedImage managedImage;
  878. Image image;
  879. if (OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
  880. bitImages.Add(image);
  881. }
  882. Bitmap mapTexture = new Bitmap(2560, 2560);
  883. Graphics g = Graphics.FromImage(mapTexture);
  884. SolidBrush sea = new SolidBrush(Color.DarkBlue);
  885. g.FillRectangle(sea, 0, 0, 2560, 2560);
  886. for (int i = 0; i < mapBlocks.Count; i++)
  887. {
  888. ushort x = (ushort)((mapBlocks[i].X - RegionInfo.RegionLocX) + 10);
  889. ushort y = (ushort)((mapBlocks[i].Y - RegionInfo.RegionLocY) + 10);
  890. g.DrawImage(bitImages[i], (x * 128), (y * 128), 128, 128);
  891. }
  892. mapTexture.Save(fileName, ImageFormat.Jpeg);
  893. }
  894. public void SaveTerrain()
  895. {
  896. m_storageManager.DataStore.StoreTerrain(Heightmap.GetDoubles(), RegionInfo.RegionID);
  897. }
  898. /// <summary>
  899. /// Loads the World heightmap
  900. /// </summary>
  901. ///
  902. public override void LoadWorldMap()
  903. {
  904. try
  905. {
  906. double[,] map = m_storageManager.DataStore.LoadTerrain(RegionInfo.RegionID);
  907. if (map == null)
  908. {
  909. m_log.Info("[TERRAIN]: No default terrain. Generating a new terrain.");
  910. Heightmap = new TerrainChannel();
  911. m_storageManager.DataStore.StoreTerrain(Heightmap.GetDoubles(), RegionInfo.RegionID);
  912. }
  913. else
  914. {
  915. Heightmap = new TerrainChannel(map);
  916. }
  917. }
  918. catch (Exception e)
  919. {
  920. m_log.Warn("[terrain]: Scene.cs: LoadWorldMap() - Failed with exception " + e.ToString());
  921. }
  922. }
  923. /// <summary>
  924. /// Register this region with a grid service
  925. /// </summary>
  926. /// <exception cref="System.Exception">Thrown if registration of the region itself fails.</exception>
  927. public void RegisterRegionWithGrid()
  928. {
  929. RegisterCommsEvents();
  930. // These two 'commands' *must be* next to each other or sim rebooting fails.
  931. m_sceneGridService.RegisterRegion(RegionInfo);
  932. m_sceneGridService.InformNeighborsThatRegionisUp(RegionInfo);
  933. Dictionary<string, string> dGridSettings = m_sceneGridService.GetGridSettings();
  934. if (dGridSettings.ContainsKey("allow_forceful_banlines"))
  935. {
  936. if (dGridSettings["allow_forceful_banlines"] != "TRUE")
  937. {
  938. m_log.Info("[GRID]: Grid is disabling forceful parcel banlists");
  939. EventManager.TriggerSetAllowForcefulBan(false);
  940. }
  941. else
  942. {
  943. m_log.Info("[GRID]: Grid is allowing forceful parcel banlists");
  944. EventManager.TriggerSetAllowForcefulBan(true);
  945. }
  946. }
  947. }
  948. /// <summary>
  949. ///
  950. /// </summary>
  951. public void CreateTerrainTexture(bool temporary)
  952. {
  953. //create a texture asset of the terrain
  954. IMapImageGenerator terrain = RequestModuleInterface<IMapImageGenerator>();
  955. // Cannot create a map for a nonexistant heightmap yet.
  956. if (Heightmap == null)
  957. return;
  958. if (terrain == null)
  959. {
  960. #region Fallback default maptile generation
  961. int tc = System.Environment.TickCount;
  962. m_log.Info("[MAPTILE]: Generating Maptile Step 1: Terrain");
  963. Bitmap mapbmp = new Bitmap(256, 256);
  964. double[,] hm = Heightmap.GetDoubles();
  965. bool ShadowDebugContinue = true;
  966. //Color prim = Color.FromArgb(120, 120, 120);
  967. //Vector3 RayEnd = new Vector3(0, 0, 0);
  968. //Vector3 RayStart = new Vector3(0, 0, 0);
  969. //Vector3 direction = new Vector3(0, 0, -1);
  970. //Vector3 AXOrigin = new Vector3();
  971. //Vector3 AXdirection = new Vector3();
  972. //Ray testRay = new Ray();
  973. //EntityIntersection rt = new EntityIntersection();
  974. bool terraincorruptedwarningsaid = false;
  975. float low = 255;
  976. float high = 0;
  977. for (int x = 0; x < 256; x++)
  978. {
  979. for (int y = 0; y < 256; y++)
  980. {
  981. float hmval = (float)hm[x, y];
  982. if (hmval < low)
  983. low = hmval;
  984. if (hmval > high)
  985. high = hmval;
  986. }
  987. }
  988. float mid = (high + low) * 0.5f;
  989. // temporary initializer
  990. float hfvalue = (float)m_regInfo.RegionSettings.WaterHeight;
  991. float hfvaluecompare = hfvalue;
  992. float hfdiff = hfvalue;
  993. int hfdiffi = 0;
  994. for (int x = 0; x < 256; x++)
  995. {
  996. //int tc = System.Environment.TickCount;
  997. for (int y = 0; y < 256; y++)
  998. {
  999. //RayEnd = new Vector3(x, y, 0);
  1000. //RayStart = new Vector3(x, y, 255);
  1001. //direction = Vector3.Norm(RayEnd - RayStart);
  1002. //AXOrigin = new Vector3(RayStart.X, RayStart.Y, RayStart.Z);
  1003. //AXdirection = new Vector3(direction.X, direction.Y, direction.Z);
  1004. //testRay = new Ray(AXOrigin, AXdirection);
  1005. //rt = m_innerScene.GetClosestIntersectingPrim(testRay);
  1006. //if (rt.HitTF)
  1007. //{
  1008. //mapbmp.SetPixel(x, y, prim);
  1009. //}
  1010. //else
  1011. //{
  1012. //float tmpval = (float)hm[x, y];
  1013. float heightvalue = (float)hm[x, y];
  1014. if (heightvalue > (float)m_regInfo.RegionSettings.WaterHeight)
  1015. {
  1016. // scale height value
  1017. heightvalue = low + mid * (heightvalue - low) / mid;
  1018. if (heightvalue > 255)
  1019. heightvalue = 255;
  1020. if (heightvalue < 0)
  1021. heightvalue = 0;
  1022. if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
  1023. heightvalue = 0;
  1024. try
  1025. {
  1026. Color green = Color.FromArgb((int)heightvalue, 100, (int)heightvalue);
  1027. // Y flip the cordinates
  1028. mapbmp.SetPixel(x, (256 - y) - 1, green);
  1029. //X
  1030. // .
  1031. //
  1032. // Shade the terrain for shadows
  1033. if ((x - 1 > 0) && (y - 1 > 0))
  1034. {
  1035. hfvalue = (float)hm[x, y];
  1036. hfvaluecompare = (float)hm[x - 1, y - 1];
  1037. if (Single.IsInfinity(hfvalue) || Single.IsNaN(hfvalue))
  1038. hfvalue = 0;
  1039. if (Single.IsInfinity(hfvaluecompare) || Single.IsNaN(hfvaluecompare))
  1040. hfvaluecompare = 0;
  1041. hfdiff = hfvaluecompare - hfvalue;
  1042. if (hfdiff > 0.3f)
  1043. {
  1044. }
  1045. else if (hfdiff < -0.3f)
  1046. {
  1047. // We have to desaturate and blacken the land at the same time
  1048. // we use floats, colors use bytes, so shrink are space down to
  1049. // 0-255
  1050. try
  1051. {
  1052. hfdiffi = Math.Abs((int)((hfdiff * 4) + (hfdiff * 0.5))) + 1;
  1053. if (hfdiff % 1 != 0)
  1054. {
  1055. hfdiffi = hfdiffi + Math.Abs((int)(((hfdiff % 1) * 0.5f) * 10f) - 1);
  1056. }
  1057. }
  1058. catch (System.OverflowException)
  1059. {
  1060. m_log.Debug("[MAPTILE]: Shadow failed at value: " + hfdiff.ToString());
  1061. ShadowDebugContinue = false;
  1062. }
  1063. if (ShadowDebugContinue)
  1064. {
  1065. if ((256 - y) - 1 > 0)
  1066. {
  1067. Color Shade = mapbmp.GetPixel(x - 1, (256 - y) - 1);
  1068. int r = Shade.R;
  1069. int g = Shade.G;
  1070. int b = Shade.B;
  1071. Shade = Color.FromArgb((r - hfdiffi > 0) ? r - hfdiffi : 0, (g - hfdiffi > 0) ? g - hfdiffi : 0, (b - hfdiffi > 0) ? b - hfdiffi : 0);
  1072. //Console.WriteLine("d:" + hfdiff.ToString() + ", i:" + hfdiffi + ", pos: " + x + "," + y + " - R:" + Shade.R.ToString() + ", G:" + Shade.G.ToString() + ", B:" + Shade.G.ToString());
  1073. mapbmp.SetPixel(x - 1, (256 - y) - 1, Shade);
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. catch (System.ArgumentException)
  1080. {
  1081. if (!terraincorruptedwarningsaid)
  1082. {
  1083. m_log.WarnFormat("[MAPIMAGE]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", RegionInfo.RegionName);
  1084. terraincorruptedwarningsaid = true;
  1085. }
  1086. Color black = Color.Black;
  1087. mapbmp.SetPixel(x, (256 - y) - 1, black);
  1088. }
  1089. }
  1090. else
  1091. {
  1092. // Y flip the cordinates
  1093. heightvalue = (float)m_regInfo.RegionSettings.WaterHeight - heightvalue;
  1094. if (heightvalue > 19)
  1095. heightvalue = 19;
  1096. if (heightvalue < 0)
  1097. heightvalue = 0;
  1098. heightvalue = 100 - (heightvalue * 100) / 19;
  1099. if (heightvalue > 255)
  1100. heightvalue = 255;
  1101. if (heightvalue < 0)
  1102. heightvalue = 0;
  1103. if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
  1104. heightvalue = 0;
  1105. try
  1106. {
  1107. Color water = Color.FromArgb((int)heightvalue, (int)heightvalue, 255);
  1108. mapbmp.SetPixel(x, (256 - y) - 1, water);
  1109. }
  1110. catch (System.ArgumentException)
  1111. {
  1112. if (!terraincorruptedwarningsaid)
  1113. {
  1114. m_log.WarnFormat("[MAPIMAGE]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", RegionInfo.RegionName);
  1115. terraincorruptedwarningsaid = true;
  1116. }
  1117. Color black = Color.Black;
  1118. mapbmp.SetPixel(x, (256 - y) - 1, black);
  1119. }
  1120. }
  1121. }
  1122. //}
  1123. //tc = System.Environment.TickCount - tc;
  1124. //m_log.Info("[MAPTILE]: Completed One row in " + tc + " ms");
  1125. }
  1126. m_log.Info("[MAPTILE]: Generating Maptile Step 1: Done in " + (System.Environment.TickCount - tc) + " ms");
  1127. bool drawPrimVolume = true;
  1128. try
  1129. {
  1130. IConfig startupConfig = m_config.Configs["Startup"];
  1131. drawPrimVolume = startupConfig.GetBoolean("DrawPrimOnMapTile", true);
  1132. }
  1133. catch
  1134. {
  1135. m_log.Warn("[MAPTILE]: Failed to load StartupConfig");
  1136. }
  1137. if (drawPrimVolume)
  1138. {
  1139. tc = System.Environment.TickCount;
  1140. m_log.Info("[MAPTILE]: Generating Maptile Step 2: Object Volume Profile");
  1141. List<EntityBase> objs = GetEntities();
  1142. lock (objs)
  1143. {
  1144. foreach (EntityBase obj in objs)
  1145. {
  1146. // Only draw the contents of SceneObjectGroup
  1147. if (obj is SceneObjectGroup)
  1148. {
  1149. SceneObjectGroup mapdot = (SceneObjectGroup)obj;
  1150. Color mapdotspot = Color.Gray; // Default color when prim color is white
  1151. // Loop over prim in group
  1152. foreach (SceneObjectPart part in mapdot.Children.Values)
  1153. {
  1154. if (part == null)
  1155. continue;
  1156. // Draw if the object is at least 1 meter wide in any direction
  1157. if (part.Scale.X > 1f || part.Scale.Y > 1f || part.Scale.Z > 1f)
  1158. {
  1159. // Try to get the RGBA of the default texture entry..
  1160. //
  1161. try
  1162. {
  1163. if (part == null)
  1164. continue;
  1165. if (part.Shape == null)
  1166. continue;
  1167. if (part.Shape.PCode == (byte)PCode.Tree || part.Shape.PCode == (byte)PCode.NewTree)
  1168. continue; // eliminates trees from this since we don't really have a good tree representation
  1169. // if you want tree blocks on the map comment the above line and uncomment the below line
  1170. //mapdotspot = Color.PaleGreen;
  1171. if (part.Shape.Textures == null)
  1172. continue;
  1173. if (part.Shape.Textures.DefaultTexture == null)
  1174. continue;
  1175. Color4 texcolor = part.Shape.Textures.DefaultTexture.RGBA;
  1176. // Not sure why some of these are null, oh well.
  1177. int colorr = 255 - (int)(texcolor.R * 255f);
  1178. int colorg = 255 - (int)(texcolor.G * 255f);
  1179. int colorb = 255 - (int)(texcolor.B * 255f);
  1180. if (!(colorr == 255 && colorg == 255 && colorb == 255))
  1181. {
  1182. //Try to set the map spot color
  1183. try
  1184. {
  1185. // If the color gets goofy somehow, skip it *shakes fist at Color4
  1186. mapdotspot = Color.FromArgb(colorr, colorg, colorb);
  1187. }
  1188. catch (ArgumentException)
  1189. {
  1190. }
  1191. }
  1192. }
  1193. catch (IndexOutOfRangeException)
  1194. {
  1195. // Windows Array
  1196. }
  1197. catch (ArgumentOutOfRangeException)
  1198. {
  1199. // Mono Array
  1200. }
  1201. Vector3 pos = part.GetWorldPosition();
  1202. // skip prim outside of retion
  1203. if (pos.X < 0f || pos.X > 256f || pos.Y < 0f || pos.Y > 256f)
  1204. continue;
  1205. // skip prim in non-finite position
  1206. if (Single.IsNaN(pos.X) || Single.IsNaN(pos.Y) || Single.IsInfinity(pos.X)
  1207. || Single.IsInfinity(pos.Y))
  1208. continue;
  1209. // Figure out if object is under 256m above the height of the terrain
  1210. bool isBelow256AboveTerrain = false;
  1211. try
  1212. {
  1213. isBelow256AboveTerrain = (pos.Z < ((float)hm[(int)pos.X, (int)pos.Y] + 256f));
  1214. }
  1215. catch (Exception)
  1216. {
  1217. }
  1218. if (isBelow256AboveTerrain)
  1219. {
  1220. // Translate scale by rotation so scale is represented properly when object is rotated
  1221. Vector3 scale = part.Shape.Scale;
  1222. Quaternion rot = part.GetWorldRotation();
  1223. scale *= rot;
  1224. // negative scales don't work in this situation
  1225. scale.X = Math.Abs(scale.X);
  1226. scale.Y = Math.Abs(scale.Y);
  1227. scale.Z = Math.Abs(scale.Z);
  1228. // This scaling isn't very accurate and doesn't take into account the face rotation :P
  1229. int mapdrawstartX = (int)(pos.X - scale.X);
  1230. int mapdrawstartY = (int)(pos.Y - scale.Y);
  1231. int mapdrawendX = (int)(pos.X + scale.X);
  1232. int mapdrawendY = (int)(pos.Y + scale.Y);
  1233. // If object is beyond the edge of the map, don't draw it to avoid errors
  1234. if (mapdrawstartX < 0 || mapdrawstartX > 255 || mapdrawendX < 0 || mapdrawendX > 255
  1235. || mapdrawstartY < 0 || mapdrawstartY > 255 || mapdrawendY < 0
  1236. || mapdrawendY > 255)
  1237. continue;
  1238. int wy = 0;
  1239. bool breakYN = false; // If we run into an error drawing, break out of the
  1240. // loop so we don't lag to death on error handling
  1241. for (int wx = mapdrawstartX; wx < mapdrawendX; wx++)
  1242. {
  1243. for (wy = mapdrawstartY; wy < mapdrawendY; wy++)
  1244. {
  1245. //m_log.InfoFormat("[MAPDEBUG]: {0},{1}({2})", wx, (255 - wy),wy);
  1246. try
  1247. {
  1248. // Remember, flip the y!
  1249. mapbmp.SetPixel(wx, (255 - wy), mapdotspot);
  1250. }
  1251. catch (ArgumentException)
  1252. {
  1253. breakYN = true;
  1254. }
  1255. if (breakYN)
  1256. break;
  1257. }
  1258. if (breakYN)
  1259. break;
  1260. }
  1261. } // Object is within 256m Z of terrain
  1262. } // object is at least a meter wide
  1263. } // loop over group children
  1264. } // entitybase is sceneobject group
  1265. } // foreach loop over entities
  1266. } // lock entities objs
  1267. m_log.Info("[MAPTILE]: Generating Maptile Step 2: Done in " + (System.Environment.TickCount - tc) + " ms");
  1268. } // end if drawPrimOnMaptle
  1269. byte[] data;
  1270. try
  1271. {
  1272. data = OpenJPEG.EncodeFromImage(mapbmp, false);
  1273. }
  1274. catch (Exception)
  1275. {
  1276. return;
  1277. }
  1278. LazySaveGeneratedMaptile(data,temporary);
  1279. #endregion
  1280. }
  1281. else
  1282. {
  1283. // Use the module to generate the maptile.
  1284. byte[] data = terrain.WriteJpeg2000Image("defaultstripe.png");
  1285. if (data != null)
  1286. {
  1287. LazySaveGeneratedMaptile(data,temporary);
  1288. }
  1289. }
  1290. }
  1291. public void LazySaveGeneratedMaptile(byte[] data, bool temporary)
  1292. {
  1293. // Overwrites the local Asset cache with new maptile data
  1294. // Assets are single write, this causes the asset server to ignore this update,
  1295. // but the local asset cache does not
  1296. // this is on purpose! The net result of this is the region always has the most up to date
  1297. // map tile while protecting the (grid) asset database from bloat caused by a new asset each
  1298. // time a mapimage is generated!
  1299. UUID lastMapRegionUUID = m_regInfo.lastMapUUID;
  1300. int lastMapRefresh = 0;
  1301. int twoDays = 172800;
  1302. int RefreshSeconds = twoDays;
  1303. try
  1304. {
  1305. lastMapRefresh = Convert.ToInt32(m_regInfo.lastMapRefresh);
  1306. }
  1307. catch (ArgumentException)
  1308. {
  1309. }
  1310. catch (FormatException)
  1311. {
  1312. }
  1313. catch (OverflowException)
  1314. {
  1315. }
  1316. UUID TerrainImageUUID = UUID.Random();
  1317. if (lastMapRegionUUID == UUID.Zero || (lastMapRefresh + RefreshSeconds) < Util.UnixTimeSinceEpoch())
  1318. {
  1319. m_regInfo.SaveLastMapUUID(TerrainImageUUID);
  1320. m_log.Warn("[MAPTILE]: STORING MAPTILE IMAGE");
  1321. }
  1322. else
  1323. {
  1324. TerrainImageUUID = lastMapRegionUUID;
  1325. m_log.Warn("[MAPTILE]: REUSING OLD MAPTILE IMAGE ID");
  1326. }
  1327. m_regInfo.RegionSettings.TerrainImageID = TerrainImageUUID;
  1328. AssetBase asset = new AssetBase();
  1329. asset.FullID = m_regInfo.RegionSettings.TerrainImageID;
  1330. asset.Data = data;
  1331. asset.Name = "terrainImage_" + m_regInfo.RegionID.ToString() + "_" + lastMapRefresh.ToString();
  1332. asset.Description = RegionInfo.RegionName;
  1333. asset.Type = 0;
  1334. asset.Temporary = temporary;
  1335. AssetCache.AddAsset(asset);
  1336. }
  1337. #endregion
  1338. #region Load Land
  1339. public void loadAllLandObjectsFromStorage(UUID regionID)
  1340. {
  1341. m_log.Info("[SCENE]: Loading land objects from storage");
  1342. List<LandData> landData = m_storageManager.DataStore.LoadLandObjects(regionID);
  1343. if (LandChannel != null)
  1344. {
  1345. if (landData.Count == 0)
  1346. {
  1347. EventManager.TriggerNoticeNoLandDataFromStorage();
  1348. }
  1349. else
  1350. {
  1351. EventManager.TriggerIncomingLandDataFromStorage(landData);
  1352. }
  1353. }
  1354. else
  1355. {
  1356. m_log.Error("[SCENE]: Land Channel is not defined. Cannot load from storage!");
  1357. }
  1358. }
  1359. #endregion
  1360. #region Primitives Methods
  1361. /// <summary>
  1362. /// Loads the World's objects
  1363. /// </summary>
  1364. public virtual void LoadPrimsFromStorage(UUID regionID)
  1365. {
  1366. m_log.Info("[SCENE]: Loading objects from datastore");
  1367. List<SceneObjectGroup> PrimsFromDB = m_storageManager.DataStore.LoadObjects(regionID);
  1368. foreach (SceneObjectGroup group in PrimsFromDB)
  1369. {
  1370. if (group.RootPart == null)
  1371. {
  1372. m_log.ErrorFormat("[SCENE] Found a SceneObjectGroup with m_rootPart == null and {0} children",
  1373. group.Children == null ? 0 : group.Children.Count);
  1374. }
  1375. AddRestoredSceneObject(group, true, true);
  1376. SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  1377. rootPart.ObjectFlags &= ~(uint)PrimFlags.Scripted;
  1378. rootPart.TrimPermissions();
  1379. group.CheckSculptAndLoad();
  1380. //rootPart.DoPhysicsPropertyUpdate(UsePhysics, true);
  1381. }
  1382. m_log.Info("[SCENE]: Loaded " + PrimsFromDB.Count.ToString() + " SceneObject(s)");
  1383. }
  1384. public Vector3 GetNewRezLocation(Vector3 RayStart, Vector3 RayEnd, UUID RayTargetID, Quaternion rot, byte bypassRayCast, byte RayEndIsIntersection, bool frontFacesOnly, Vector3 scale, bool FaceCenter)
  1385. {
  1386. Vector3 pos = Vector3.Zero;
  1387. if (RayEndIsIntersection == (byte)1)
  1388. {
  1389. pos = RayEnd;
  1390. return pos;
  1391. }
  1392. if (RayTargetID != UUID.Zero)
  1393. {
  1394. SceneObjectPart target = GetSceneObjectPart(RayTargetID);
  1395. Vector3 direction = Vector3.Normalize(RayEnd - RayStart);
  1396. Vector3 AXOrigin = new Vector3(RayStart.X, RayStart.Y, RayStart.Z);
  1397. Vector3 AXdirection = new Vector3(direction.X, direction.Y, direction.Z);
  1398. if (target != null)
  1399. {
  1400. pos = target.AbsolutePosition;
  1401. //m_log.Info("[OBJECT_REZ]: TargetPos: " + pos.ToString() + ", RayStart: " + RayStart.ToString() + ", RayEnd: " + RayEnd.ToString() + ", Volume: " + Util.GetDistanceTo(RayStart,RayEnd).ToString() + ", mag1: " + Util.GetMagnitude(RayStart).ToString() + ", mag2: " + Util.GetMagnitude(RayEnd).ToString());
  1402. // TODO: Raytrace better here
  1403. //EntityIntersection ei = m_innerScene.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection));
  1404. Ray NewRay = new Ray(AXOrigin, AXdirection);
  1405. // Ray Trace against target here
  1406. EntityIntersection ei = target.TestIntersectionOBB(NewRay, Quaternion.Identity, frontFacesOnly, FaceCenter);
  1407. // Un-comment out the following line to Get Raytrace results printed to the console.
  1408. // m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  1409. float ScaleOffset = 0.5f;
  1410. // If we hit something
  1411. if (ei.HitTF)
  1412. {
  1413. Vector3 scaleComponent = new Vector3(ei.AAfaceNormal.X, ei.AAfaceNormal.Y, ei.AAfaceNormal.Z);
  1414. if (scaleComponent.X != 0) ScaleOffset = scale.X;
  1415. if (scaleComponent.Y != 0) ScaleOffset = scale.Y;
  1416. if (scaleComponent.Z != 0) ScaleOffset = scale.Z;
  1417. ScaleOffset = Math.Abs(ScaleOffset);
  1418. Vector3 intersectionpoint = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  1419. Vector3 normal = new Vector3(ei.normal.X, ei.normal.Y, ei.normal.Z);
  1420. // Set the position to the intersection point
  1421. Vector3 offset = (normal * (ScaleOffset / 2f));
  1422. pos = (intersectionpoint + offset);
  1423. // Un-offset the prim (it gets offset later by the consumer method)
  1424. pos.Z -= 0.25F;
  1425. }
  1426. return pos;
  1427. }
  1428. else
  1429. {
  1430. // We don't have a target here, so we're going to raytrace all the objects in the scene.
  1431. EntityIntersection ei = m_innerScene.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection), true, false);
  1432. // Un-comment the following line to print the raytrace results to the console.
  1433. //m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  1434. if (ei.HitTF)
  1435. {
  1436. pos = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  1437. } else
  1438. {
  1439. // fall back to our stupid functionality
  1440. pos = RayEnd;
  1441. }
  1442. return pos;
  1443. }
  1444. }
  1445. else
  1446. {
  1447. // fall back to our stupid functionality
  1448. pos = RayEnd;
  1449. return pos;
  1450. }
  1451. }
  1452. public virtual void AddNewPrim(UUID ownerID, Vector3 RayEnd, Quaternion rot, PrimitiveBaseShape shape,
  1453. byte bypassRaycast, Vector3 RayStart, UUID RayTargetID,
  1454. byte RayEndIsIntersection)
  1455. {
  1456. Vector3 pos = GetNewRezLocation(RayStart, RayEnd, RayTargetID, rot, bypassRaycast, RayEndIsIntersection, true, new Vector3(0.5f, 0.5f, 0.5f), false);
  1457. if (ExternalChecks.ExternalChecksCanRezObject(1, ownerID, pos))
  1458. {
  1459. // rez ON the ground, not IN the ground
  1460. pos.Z += 0.25F;
  1461. AddNewPrim(ownerID, pos, rot, shape);
  1462. }
  1463. }
  1464. public virtual SceneObjectGroup AddNewPrim(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  1465. {
  1466. //m_log.DebugFormat(
  1467. // "[SCENE]: Scene.AddNewPrim() called for agent {0} in {1}", ownerID, RegionInfo.RegionName);
  1468. SceneObjectGroup sceneObject = new SceneObjectGroup(ownerID, pos, rot, shape);
  1469. SceneObjectPart rootPart = sceneObject.GetChildPart(sceneObject.UUID);
  1470. // if grass or tree, make phantom
  1471. //rootPart.TrimPermissions();
  1472. if ((rootPart.Shape.PCode == (byte)PCode.Grass)
  1473. || (rootPart.Shape.PCode == (byte)PCode.Tree) || (rootPart.Shape.PCode == (byte)PCode.NewTree))
  1474. {
  1475. rootPart.AddFlag(PrimFlags.Phantom);
  1476. //rootPart.ObjectFlags += (uint)PrimFlags.Phantom;
  1477. if (rootPart.Shape.PCode != (byte)PCode.Grass)
  1478. AdaptTree(ref shape);
  1479. }
  1480. AddNewSceneObject(sceneObject, true);
  1481. return sceneObject;
  1482. }
  1483. void AdaptTree(ref PrimitiveBaseShape tree)
  1484. {
  1485. // Tree size has to be adapted depending on its type
  1486. switch ((Tree)tree.State)
  1487. {
  1488. case Tree.Cypress1:
  1489. case Tree.Cypress2:
  1490. tree.Scale = new Vector3(4, 4, 10);
  1491. break;
  1492. // case... other tree types
  1493. // tree.Scale = new Vector3(?, ?, ?);
  1494. // break;
  1495. default:
  1496. tree.Scale = new Vector3(4, 4, 4);
  1497. break;
  1498. }
  1499. }
  1500. public SceneObjectGroup AddTree(UUID uuid, Vector3 scale, Quaternion rotation, Vector3 position,
  1501. Tree treeType, bool newTree)
  1502. {
  1503. PrimitiveBaseShape treeShape = new PrimitiveBaseShape();
  1504. treeShape.PathCurve = 16;
  1505. treeShape.PathEnd = 49900;
  1506. treeShape.PCode = newTree ? (byte)PCode.NewTree : (byte)PCode.Tree;
  1507. treeShape.Scale = scale;
  1508. treeShape.State = (byte)treeType;
  1509. return AddNewPrim(uuid, position, rotation, treeShape);
  1510. }
  1511. /// <summary>
  1512. /// Add an object into the scene that has come from storage
  1513. /// </summary>
  1514. /// <param name="sceneObject"></param>
  1515. /// <param name="attachToBackup">
  1516. /// If true, changes to the object will be reflected in its persisted data
  1517. /// If false, the persisted data will not be changed even if the object in the scene is changed
  1518. /// </param>
  1519. /// <param name="alreadyPersisted">
  1520. /// If true, we won't persist this object until it changes
  1521. /// If false, we'll persist this object immediately
  1522. /// </param>
  1523. /// <returns>
  1524. /// true if the object was added, false if an object with the same uuid was already in the scene
  1525. /// </returns>
  1526. public bool AddRestoredSceneObject(
  1527. SceneObjectGroup sceneObject, bool attachToBackup, bool alreadyPersisted)
  1528. {
  1529. return m_innerScene.AddRestoredSceneObject(sceneObject, attachToBackup, alreadyPersisted);
  1530. }
  1531. /// <summary>
  1532. /// Add a newly created object to the scene
  1533. /// </summary>
  1534. /// <param name="sceneObject"></param>
  1535. /// <param name="attachToBackup">
  1536. /// If true, the object is made persistent into the scene.
  1537. /// If false, the object will not persist over server restarts
  1538. /// </param>
  1539. public bool AddNewSceneObject(SceneObjectGroup sceneObject, bool attachToBackup)
  1540. {
  1541. return m_innerScene.AddNewSceneObject(sceneObject, attachToBackup);
  1542. }
  1543. /// <summary>
  1544. /// Delete every object from the scene
  1545. /// </summary>
  1546. public void DeleteAllSceneObjects()
  1547. {
  1548. lock (Entities)
  1549. {
  1550. ICollection<EntityBase> entities = new List<EntityBase>(Entities.Values);
  1551. foreach (EntityBase e in entities)
  1552. {
  1553. if (e is SceneObjectGroup)
  1554. DeleteSceneObject((SceneObjectGroup)e, false);
  1555. }
  1556. }
  1557. }
  1558. /// <summary>
  1559. /// Delete the given object from the scene.
  1560. /// </summary>
  1561. /// <param name="group"></param>
  1562. public void DeleteSceneObject(SceneObjectGroup group, bool silent)
  1563. {
  1564. //SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  1565. // Serialise calls to RemoveScriptInstances to avoid
  1566. // deadlocking on m_parts inside SceneObjectGroup
  1567. lock (m_deleting_scene_object)
  1568. {
  1569. group.RemoveScriptInstances();
  1570. }
  1571. foreach (SceneObjectPart part in group.Children.Values)
  1572. {
  1573. if (part.PhysActor != null)
  1574. {
  1575. PhysicsScene.RemovePrim(part.PhysActor);
  1576. part.PhysActor = null;
  1577. }
  1578. }
  1579. // if (rootPart.PhysActor != null)
  1580. // {
  1581. // PhysicsScene.RemovePrim(rootPart.PhysActor);
  1582. // rootPart.PhysActor = null;
  1583. // }
  1584. if (UnlinkSceneObject(group.UUID, false))
  1585. {
  1586. EventManager.TriggerObjectBeingRemovedFromScene(group);
  1587. EventManager.TriggerParcelPrimCountTainted();
  1588. }
  1589. group.DeleteGroup(silent);
  1590. }
  1591. /// <summary>
  1592. /// Unlink the given object from the scene. Unlike delete, this just removes the record of the object - the
  1593. /// object itself is not destroyed.
  1594. /// </summary>
  1595. /// <param name="uuid"></param>
  1596. /// <returns>true if the object was in the scene, false if it was not</returns>
  1597. public bool UnlinkSceneObject(UUID uuid, bool resultOfLinkingObjects)
  1598. {
  1599. if (m_innerScene.DeleteSceneObject(uuid, resultOfLinkingObjects))
  1600. {
  1601. if (!resultOfLinkingObjects)
  1602. m_storageManager.DataStore.RemoveObject(uuid,
  1603. m_regInfo.RegionID);
  1604. return true;
  1605. }
  1606. return false;
  1607. }
  1608. public void LoadPrimsFromXml(string fileName, bool newIdsFlag, Vector3 loadOffset)
  1609. {
  1610. m_log.InfoFormat("[SCENE]: Loading prims in xml format to region {0} from {1}", RegionInfo.RegionName, fileName);
  1611. m_serialiser.LoadPrimsFromXml(this, fileName, newIdsFlag, loadOffset);
  1612. }
  1613. public void SavePrimsToXml(string fileName)
  1614. {
  1615. m_log.InfoFormat("[SCENE]: Saving prims in xml format for region {0} to {1}", RegionInfo.RegionName, fileName);
  1616. m_serialiser.SavePrimsToXml(this, fileName);
  1617. }
  1618. public void LoadPrimsFromXml2(string fileName)
  1619. {
  1620. m_log.InfoFormat("[SCENE]: Loading prims in xml2 format to region {0} from {1}", RegionInfo.RegionName, fileName);
  1621. m_serialiser.LoadPrimsFromXml2(this, fileName);
  1622. }
  1623. public void LoadPrimsFromXml2(TextReader reader, bool startScripts)
  1624. {
  1625. m_log.InfoFormat("[SCENE]: Loading prims in xml2 format to region {0} from stream", RegionInfo.RegionName);
  1626. m_serialiser.LoadPrimsFromXml2(this, reader, startScripts);
  1627. }
  1628. public void SavePrimsToXml2(string fileName)
  1629. {
  1630. m_log.InfoFormat("[SCENE]: Saving prims in xml2 format for region {0} to {1}", RegionInfo.RegionName, fileName);
  1631. m_serialiser.SavePrimsToXml2(this, fileName);
  1632. }
  1633. public void SavePrimsToXml2(TextWriter stream, Vector3 min, Vector3 max)
  1634. {
  1635. m_log.InfoFormat("[SCENE]: Saving prims in xml2 format for region {0} to stream", RegionInfo.RegionName);
  1636. m_serialiser.SavePrimsToXml2(this, stream, min, max);
  1637. }
  1638. public void SaveNamedPrimsToXml2(string primName, string fileName)
  1639. {
  1640. m_log.InfoFormat(
  1641. "[SCENE]: Saving prims with name {0} in xml2 format for region {1} to {2}", primName, RegionInfo.RegionName, fileName);
  1642. List<EntityBase> entityList = GetEntities();
  1643. List<EntityBase> primList = new List<EntityBase>();
  1644. foreach (EntityBase ent in entityList)
  1645. {
  1646. if (ent is SceneObjectGroup)
  1647. {
  1648. if (ent.Name == primName)
  1649. {
  1650. primList.Add(ent);
  1651. }
  1652. }
  1653. }
  1654. m_serialiser.SavePrimListToXml2(primList, fileName);
  1655. }
  1656. /// <summary>
  1657. /// Load a prim archive into the scene. This loads both prims and their assets.
  1658. /// </summary>
  1659. /// <param name="filePath"></param>
  1660. public void LoadPrimsFromArchive(string filePath)
  1661. {
  1662. m_log.InfoFormat("[SCENE]: Loading archive to region {0} from {1}", RegionInfo.RegionName, filePath);
  1663. m_archiver.DearchiveRegion(filePath);
  1664. }
  1665. /// <summary>
  1666. /// Save the prims in the scene to an archive. This saves both prims and their assets.
  1667. /// </summary>
  1668. /// <param name="filePath"></param>
  1669. public void SavePrimsToArchive(string filePath)
  1670. {
  1671. m_log.InfoFormat("[SCENE]: Writing archive for region {0} to {1}", RegionInfo.RegionName, filePath);
  1672. m_archiver.ArchiveRegion(filePath);
  1673. }
  1674. /// <summary>
  1675. /// Move the given scene object into a new region depending on which region its absolute position has moved
  1676. /// into.
  1677. ///
  1678. /// This method locates the new region handle and offsets the prim position for the new region
  1679. /// </summary>
  1680. /// <param name="attemptedPosition">the attempted out of region position of the scene object</param>
  1681. /// <param name="grp">the scene object that we're crossing</param>
  1682. public void CrossPrimGroupIntoNewRegion(Vector3 attemptedPosition, SceneObjectGroup grp, bool silent)
  1683. {
  1684. if (grp == null)
  1685. return;
  1686. if (grp.RootPart == null)
  1687. return;
  1688. if (grp.RootPart.DIE_AT_EDGE)
  1689. {
  1690. // We remove the object here
  1691. try
  1692. {
  1693. DeleteSceneObject(grp, false);
  1694. }
  1695. catch (Exception)
  1696. {
  1697. m_log.Warn("[DATABASE]: exception when trying to remove the prim that crossed the border.");
  1698. }
  1699. return;
  1700. }
  1701. int thisx = (int)RegionInfo.RegionLocX;
  1702. int thisy = (int)RegionInfo.RegionLocY;
  1703. ulong newRegionHandle = 0;
  1704. Vector3 pos = attemptedPosition;
  1705. if (attemptedPosition.X > Constants.RegionSize + 0.1f)
  1706. {
  1707. pos.X = ((pos.X - Constants.RegionSize));
  1708. newRegionHandle
  1709. = Util.UIntsToLong((uint)((thisx + 1) * Constants.RegionSize), (uint)(thisy * Constants.RegionSize));
  1710. // x + 1
  1711. }
  1712. else if (attemptedPosition.X < -0.1f)
  1713. {
  1714. pos.X = ((pos.X + Constants.RegionSize));
  1715. newRegionHandle
  1716. = Util.UIntsToLong((uint)((thisx - 1) * Constants.RegionSize), (uint)(thisy * Constants.RegionSize));
  1717. // x - 1
  1718. }
  1719. if (attemptedPosition.Y > Constants.RegionSize + 0.1f)
  1720. {
  1721. pos.Y = ((pos.Y - Constants.RegionSize));
  1722. newRegionHandle
  1723. = Util.UIntsToLong((uint)(thisx * Constants.RegionSize), (uint)((thisy + 1) * Constants.RegionSize));
  1724. // y + 1
  1725. }
  1726. else if (attemptedPosition.Y < -0.1f)
  1727. {
  1728. pos.Y = ((pos.Y + Constants.RegionSize));
  1729. newRegionHandle
  1730. = Util.UIntsToLong((uint)(thisx * Constants.RegionSize), (uint)((thisy - 1) * Constants.RegionSize));
  1731. // y - 1
  1732. }
  1733. // Offset the positions for the new region across the border
  1734. Vector3 oldGroupPosition = grp.RootPart.GroupPosition;
  1735. grp.OffsetForNewRegion(pos);
  1736. // If we fail to cross the border, then reset the position of the scene object on that border.
  1737. if (!CrossPrimGroupIntoNewRegion(newRegionHandle, grp, silent))
  1738. {
  1739. grp.OffsetForNewRegion(oldGroupPosition);
  1740. }
  1741. }
  1742. /// <summary>
  1743. /// Move the given scene object into a new region
  1744. /// </summary>
  1745. /// <param name="newRegionHandle"></param>
  1746. /// <param name="grp">Scene Object Group that we're crossing</param>
  1747. /// <returns>
  1748. /// true if the crossing itself was successful, false on failure
  1749. /// FIMXE: we still return true if the crossing object was not successfully deleted from the originating region
  1750. /// </returns>
  1751. public bool CrossPrimGroupIntoNewRegion(ulong newRegionHandle, SceneObjectGroup grp, bool silent)
  1752. {
  1753. bool successYN = false;
  1754. grp.RootPart.UpdateFlag = 0;
  1755. int primcrossingXMLmethod = 0;
  1756. if (newRegionHandle != 0)
  1757. {
  1758. successYN
  1759. = m_sceneGridService.PrimCrossToNeighboringRegion(
  1760. newRegionHandle, grp.UUID, m_serialiser.SaveGroupToXml2(grp), primcrossingXMLmethod);
  1761. if (successYN)
  1762. {
  1763. // We remove the object here
  1764. try
  1765. {
  1766. DeleteSceneObject(grp, silent);
  1767. }
  1768. catch (Exception e)
  1769. {
  1770. m_log.ErrorFormat(
  1771. "[INTERREGION]: Exception deleting the old object left behind on a border crossing for {0}, {1}",
  1772. grp, e);
  1773. }
  1774. }
  1775. else
  1776. {
  1777. if (grp.RootPart != null)
  1778. {
  1779. if (grp.RootPart.PhysActor != null)
  1780. {
  1781. grp.RootPart.PhysActor.CrossingFailure();
  1782. }
  1783. }
  1784. m_log.ErrorFormat("[INTERREGION]: Prim crossing failed for {0}", grp);
  1785. }
  1786. }
  1787. else
  1788. {
  1789. m_log.Error("[INTERREGION]: region handle was unexpectedly 0 in Scene.CrossPrimGroupIntoNewRegion()");
  1790. }
  1791. return successYN;
  1792. }
  1793. /// <summary>
  1794. /// Handle a scene object that is crossing into this region from another.
  1795. /// </summary>
  1796. /// <param name="regionHandle"></param>
  1797. /// <param name="primID"></param>
  1798. /// <param name="objXMLData"></param>
  1799. /// <param name="XMLMethod"></param>
  1800. /// <returns></returns>
  1801. public bool IncomingInterRegionPrimGroup(ulong regionHandle, UUID primID, string objXMLData, int XMLMethod)
  1802. {
  1803. m_log.Warn("[INTERREGION]: A new prim arrived from a neighbor");
  1804. if (XMLMethod == 0)
  1805. {
  1806. SceneObjectGroup sceneObject = m_serialiser.DeserializeGroupFromXml2(objXMLData);
  1807. AddRestoredSceneObject(sceneObject, true, false);
  1808. SceneObjectPart RootPrim = GetSceneObjectPart(primID);
  1809. if (RootPrim != null)
  1810. {
  1811. if (m_regInfo.EstateSettings.IsBanned(RootPrim.OwnerID))
  1812. {
  1813. SceneObjectGroup grp = RootPrim.ParentGroup;
  1814. if (grp != null)
  1815. {
  1816. DeleteSceneObject(grp, false);
  1817. }
  1818. m_log.Info("[INTERREGION]: Denied prim crossing for banned avatar");
  1819. return false;
  1820. }
  1821. if (RootPrim.Shape.PCode == (byte)PCode.Prim)
  1822. {
  1823. SceneObjectGroup grp = RootPrim.ParentGroup;
  1824. if (grp != null)
  1825. {
  1826. if (RootPrim.Shape.State != 0)
  1827. {
  1828. // Never persist
  1829. m_log.DebugFormat("[ATTACHMENT]: Received attachment {0}, inworld asset id {1}", grp.RootPart.LastOwnerID.ToString(), grp.UUID.ToString());
  1830. grp.DetachFromBackup();
  1831. // Attachment
  1832. ScenePresence sp = GetScenePresence(grp.OwnerID);
  1833. if (sp != null)
  1834. {
  1835. // hack assetID until we get assetID into the XML format.
  1836. // LastOwnerID is used for group deeding, so when you do stuff
  1837. // with the deeded object, it goes back to them
  1838. grp.SetFromAssetID(grp.RootPart.LastOwnerID);
  1839. m_log.DebugFormat("[ATTACHMENT]: Attach to avatar {0}", sp.UUID.ToString());
  1840. AttachObject(sp.ControllingClient, grp.LocalId, (uint)0, grp.GroupRotation, grp.AbsolutePosition, false);
  1841. }
  1842. else
  1843. {
  1844. // Remove, then add, to ensure the expire
  1845. // time is refreshed. Wouldn't do to
  1846. // have it poof before the avatar gets
  1847. // there.
  1848. //
  1849. RootPrim.RemFlag(PrimFlags.TemporaryOnRez);
  1850. RootPrim.AddFlag(PrimFlags.TemporaryOnRez);
  1851. }
  1852. }
  1853. }
  1854. }
  1855. }
  1856. return true;
  1857. }
  1858. else
  1859. {
  1860. return false;
  1861. }
  1862. }
  1863. #endregion
  1864. #region Add/Remove Avatar Methods
  1865. public override void AddNewClient(IClientAPI client, bool child)
  1866. {
  1867. SubscribeToClientEvents(client);
  1868. ScenePresence presence;
  1869. if (m_restorePresences.ContainsKey(client.AgentId))
  1870. {
  1871. m_log.DebugFormat("[SCENE]: Restoring agent {0} {1} in {2}", client.Name, client.AgentId, RegionInfo.RegionName);
  1872. presence = m_restorePresences[client.AgentId];
  1873. m_restorePresences.Remove(client.AgentId);
  1874. // This is one of two paths to create avatars that are
  1875. // used. This tends to get called more in standalone
  1876. // than grid, not really sure why, but as such needs
  1877. // an explicity appearance lookup here.
  1878. AvatarAppearance appearance = null;
  1879. GetAvatarAppearance(client, out appearance);
  1880. presence.Appearance = appearance;
  1881. presence.initializeScenePresence(client, RegionInfo, this);
  1882. m_innerScene.AddScenePresence(presence);
  1883. lock (m_restorePresences)
  1884. {
  1885. Monitor.PulseAll(m_restorePresences);
  1886. }
  1887. }
  1888. else
  1889. {
  1890. m_log.DebugFormat(
  1891. "[SCENE]: Adding new {0} agent {1} {2} in {3}",
  1892. (child ? "child" : "root"), client.Name, client.AgentId, RegionInfo.RegionName);
  1893. CommsManager.UserProfileCacheService.AddNewUser(client.AgentId);
  1894. CreateAndAddScenePresence(client, child);
  1895. }
  1896. m_LastLogin = System.Environment.TickCount;
  1897. EventManager.TriggerOnNewClient(client);
  1898. }
  1899. protected virtual void SubscribeToClientEvents(IClientAPI client)
  1900. {
  1901. client.OnRegionHandShakeReply += SendLayerData;
  1902. client.OnAddPrim += AddNewPrim;
  1903. client.OnUpdatePrimGroupPosition += m_innerScene.UpdatePrimPosition;
  1904. client.OnUpdatePrimSinglePosition += m_innerScene.UpdatePrimSinglePosition;
  1905. client.OnUpdatePrimGroupRotation += m_innerScene.UpdatePrimRotation;
  1906. client.OnUpdatePrimGroupMouseRotation += m_innerScene.UpdatePrimRotation;
  1907. client.OnUpdatePrimSingleRotation += m_innerScene.UpdatePrimSingleRotation;
  1908. client.OnUpdatePrimScale += m_innerScene.UpdatePrimScale;
  1909. client.OnUpdatePrimGroupScale += m_innerScene.UpdatePrimGroupScale;
  1910. client.OnUpdateExtraParams += m_innerScene.UpdateExtraParam;
  1911. client.OnUpdatePrimShape += m_innerScene.UpdatePrimShape;
  1912. //client.OnRequestMapBlocks += RequestMapBlocks; // handled in a module now.
  1913. client.OnUpdatePrimTexture += m_innerScene.UpdatePrimTexture;
  1914. client.OnTeleportLocationRequest += RequestTeleportLocation;
  1915. client.OnTeleportLandmarkRequest += RequestTeleportLandmark;
  1916. client.OnObjectSelect += SelectPrim;
  1917. client.OnObjectDeselect += DeselectPrim;
  1918. client.OnGrabUpdate += m_innerScene.MoveObject;
  1919. client.OnDeRezObject += DeRezObject;
  1920. client.OnRezObject += RezObject;
  1921. client.OnRezSingleAttachmentFromInv += RezSingleAttachment;
  1922. client.OnDetachAttachmentIntoInv += DetachSingleAttachmentToInv;
  1923. client.OnObjectAttach += m_innerScene.AttachObject;
  1924. client.OnObjectDetach += m_innerScene.DetachObject;
  1925. client.OnObjectDrop += m_innerScene.DropObject;
  1926. client.OnNameFromUUIDRequest += CommsManager.HandleUUIDNameRequest;
  1927. client.OnObjectDescription += m_innerScene.PrimDescription;
  1928. client.OnObjectName += m_innerScene.PrimName;
  1929. client.OnObjectClickAction += m_innerScene.PrimClickAction;
  1930. client.OnObjectMaterial += m_innerScene.PrimMaterial;
  1931. client.OnLinkObjects += m_innerScene.LinkObjects;
  1932. client.OnDelinkObjects += m_innerScene.DelinkObjects;
  1933. client.OnObjectDuplicate += m_innerScene.DuplicateObject;
  1934. client.OnObjectDuplicateOnRay += doObjectDuplicateOnRay;
  1935. client.OnUpdatePrimFlags += m_innerScene.UpdatePrimFlags;
  1936. client.OnRequestObjectPropertiesFamily += m_innerScene.RequestObjectPropertiesFamily;
  1937. client.OnRequestGodlikePowers += handleRequestGodlikePowers;
  1938. client.OnGodKickUser += HandleGodlikeKickUser;
  1939. client.OnObjectPermissions += HandleObjectPermissionsUpdate;
  1940. client.OnCreateNewInventoryItem += CreateNewInventoryItem;
  1941. client.OnCreateNewInventoryFolder += CommsManager.UserProfileCacheService.HandleCreateInventoryFolder;
  1942. client.OnUpdateInventoryFolder += CommsManager.UserProfileCacheService.HandleUpdateInventoryFolder;
  1943. client.OnMoveInventoryFolder += CommsManager.UserProfileCacheService.HandleMoveInventoryFolder;
  1944. client.OnFetchInventoryDescendents += CommsManager.UserProfileCacheService.HandleFetchInventoryDescendents;
  1945. client.OnPurgeInventoryDescendents += CommsManager.UserProfileCacheService.HandlePurgeInventoryDescendents;
  1946. client.OnFetchInventory += CommsManager.UserProfileCacheService.HandleFetchInventory;
  1947. client.OnUpdateInventoryItem += UpdateInventoryItemAsset;
  1948. client.OnCopyInventoryItem += CopyInventoryItem;
  1949. client.OnMoveInventoryItem += MoveInventoryItem;
  1950. client.OnRemoveInventoryItem += RemoveInventoryItem;
  1951. client.OnRemoveInventoryFolder += RemoveInventoryFolder;
  1952. client.OnRezScript += RezScript;
  1953. client.OnRequestTaskInventory += RequestTaskInventory;
  1954. client.OnRemoveTaskItem += RemoveTaskInventory;
  1955. client.OnUpdateTaskInventory += UpdateTaskInventory;
  1956. client.OnMoveTaskItem += ClientMoveTaskInventoryItem;
  1957. client.OnGrabObject += ProcessObjectGrab;
  1958. client.OnDeGrabObject += ProcessObjectDeGrab;
  1959. client.OnMoneyTransferRequest += ProcessMoneyTransferRequest;
  1960. client.OnParcelBuy += ProcessParcelBuy;
  1961. client.OnAvatarPickerRequest += ProcessAvatarPickerRequest;
  1962. client.OnObjectIncludeInSearch += m_innerScene.MakeObjectSearchable;
  1963. client.OnTeleportHomeRequest += TeleportClientHome;
  1964. client.OnSetStartLocationRequest += SetHomeRezPoint;
  1965. client.OnUndo += m_innerScene.HandleUndo;
  1966. client.OnObjectGroupRequest += m_innerScene.HandleObjectGroupUpdate;
  1967. client.OnParcelReturnObjectsRequest += LandChannel.ReturnObjectsInParcel;
  1968. client.OnParcelSetOtherCleanTime += LandChannel.SetParcelOtherCleanTime;
  1969. client.OnObjectSaleInfo += ObjectSaleInfo;
  1970. client.OnScriptReset += ProcessScriptReset;
  1971. client.OnGetScriptRunning += GetScriptRunning;
  1972. client.OnSetScriptRunning += SetScriptRunning;
  1973. client.OnRegionHandleRequest += RegionHandleRequest;
  1974. client.OnUnackedTerrain += TerrainUnAcked;
  1975. //Gesture
  1976. client.OnActivateGesture += ActivateGesture;
  1977. client.OnDeactivateGesture += DeactivateGesture;
  1978. client.OnObjectOwner += ObjectOwner;
  1979. // EventManager.TriggerOnNewClient(client);
  1980. }
  1981. // Gesture
  1982. public virtual void ActivateGesture(IClientAPI client, UUID assetId, UUID gestureId)
  1983. {
  1984. // UserProfileCacheService User = CommsManager.SecureInventoryService.UpdateItem(gestureid, agentID);
  1985. CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(client.AgentId);
  1986. if (userInfo != null)
  1987. {
  1988. InventoryItemBase item = userInfo.RootFolder.FindItem(gestureId);
  1989. if (item != null)
  1990. {
  1991. item.Flags = 1;
  1992. userInfo.UpdateItem(item);
  1993. }
  1994. else m_log.Error("Unable to find gesture");
  1995. }
  1996. else m_log.Error("Gesture : Unable to find user ");
  1997. m_log.DebugFormat("Asset : {0} gesture :{1}", gestureId.ToString(), assetId.ToString());
  1998. }
  1999. public virtual void DeactivateGesture(IClientAPI client, UUID gestureId)
  2000. {
  2001. // UserProfileCacheService User = CommsManager.SecureInventoryService.UpdateItem(gestureid, agentID);
  2002. CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(client.AgentId);
  2003. if (userInfo != null)
  2004. {
  2005. InventoryItemBase item = userInfo.RootFolder.FindItem(gestureId);
  2006. if (item != null)
  2007. {
  2008. item.Flags = 0;
  2009. userInfo.UpdateItem(item);
  2010. }
  2011. else m_log.Error("Unable to find gesture");
  2012. }
  2013. else m_log.Error("Gesture : Unable to find user ");
  2014. m_log.DebugFormat("gesture : {0} ", gestureId.ToString());
  2015. }
  2016. /// <summary>
  2017. /// Teleport an avatar to their home region
  2018. /// </summary>
  2019. /// <param name="agentId"></param>
  2020. /// <param name="client"></param>
  2021. public virtual void TeleportClientHome(UUID agentId, IClientAPI client)
  2022. {
  2023. UserProfileData UserProfile = CommsManager.UserService.GetUserProfile(agentId);
  2024. if (UserProfile != null)
  2025. {
  2026. RegionInfo regionInfo = CommsManager.GridService.RequestNeighbourInfo(UserProfile.HomeRegionID);
  2027. if (regionInfo == null)
  2028. {
  2029. regionInfo = CommsManager.GridService.RequestNeighbourInfo(UserProfile.HomeRegion);
  2030. UserProfile.HomeRegionID = regionInfo.RegionID;
  2031. CommsManager.UserService.UpdateUserProfile(UserProfile);
  2032. }
  2033. if (regionInfo == null)
  2034. {
  2035. // can't find the Home region: Tell viewer and abort
  2036. client.SendTeleportFailed("Your home-region could not be found.");
  2037. return;
  2038. }
  2039. RequestTeleportLocation(
  2040. client, regionInfo.RegionHandle, UserProfile.HomeLocation, UserProfile.HomeLookAt,
  2041. (uint)(TPFlags.SetLastToTarget | TPFlags.ViaHome));
  2042. }
  2043. }
  2044. public void doObjectDuplicateOnRay(uint localID, uint dupeFlags, UUID AgentID, UUID GroupID,
  2045. UUID RayTargetObj, Vector3 RayEnd, Vector3 RayStart,
  2046. bool BypassRaycast, bool RayEndIsIntersection, bool CopyCenters, bool CopyRotates)
  2047. {
  2048. Vector3 pos;
  2049. const bool frontFacesOnly = true;
  2050. //m_log.Info("HITTARGET: " + RayTargetObj.ToString() + ", COPYTARGET: " + localID.ToString());
  2051. SceneObjectPart target = GetSceneObjectPart(localID);
  2052. SceneObjectPart target2 = GetSceneObjectPart(RayTargetObj);
  2053. if (target != null && target2 != null)
  2054. {
  2055. Vector3 direction = Vector3.Normalize(RayEnd - RayStart);
  2056. Vector3 AXOrigin = new Vector3(RayStart.X, RayStart.Y, RayStart.Z);
  2057. Vector3 AXdirection = new Vector3(direction.X, direction.Y, direction.Z);
  2058. if (target2.ParentGroup != null)
  2059. {
  2060. pos = target2.AbsolutePosition;
  2061. //m_log.Info("[OBJECT_REZ]: TargetPos: " + pos.ToString() + ", RayStart: " + RayStart.ToString() + ", RayEnd: " + RayEnd.ToString() + ", Volume: " + Util.GetDistanceTo(RayStart,RayEnd).ToString() + ", mag1: " + Util.GetMagnitude(RayStart).ToString() + ", mag2: " + Util.GetMagnitude(RayEnd).ToString());
  2062. // TODO: Raytrace better here
  2063. //EntityIntersection ei = m_innerScene.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection));
  2064. Ray NewRay = new Ray(AXOrigin, AXdirection);
  2065. // Ray Trace against target here
  2066. EntityIntersection ei = target2.TestIntersectionOBB(NewRay, Quaternion.Identity, frontFacesOnly, CopyCenters);
  2067. // Un-comment out the following line to Get Raytrace results printed to the console.
  2068. //m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  2069. float ScaleOffset = 0.5f;
  2070. // If we hit something
  2071. if (ei.HitTF)
  2072. {
  2073. Vector3 scale = target.Scale;
  2074. Vector3 scaleComponent = new Vector3(ei.AAfaceNormal.X, ei.AAfaceNormal.Y, ei.AAfaceNormal.Z);
  2075. if (scaleComponent.X != 0) ScaleOffset = scale.X;
  2076. if (scaleComponent.Y != 0) ScaleOffset = scale.Y;
  2077. if (scaleComponent.Z != 0) ScaleOffset = scale.Z;
  2078. ScaleOffset = Math.Abs(ScaleOffset);
  2079. Vector3 intersectionpoint = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  2080. Vector3 normal = new Vector3(ei.normal.X, ei.normal.Y, ei.normal.Z);
  2081. Vector3 offset = normal * (ScaleOffset / 2f);
  2082. pos = intersectionpoint + offset;
  2083. // stick in offset format from the original prim
  2084. pos = pos - target.ParentGroup.AbsolutePosition;
  2085. if (CopyRotates)
  2086. {
  2087. Quaternion worldRot = target2.GetWorldRotation();
  2088. // SceneObjectGroup obj = m_innerScene.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID, worldRot);
  2089. m_innerScene.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID, worldRot);
  2090. //obj.Rotation = worldRot;
  2091. //obj.UpdateGroupRotation(worldRot);
  2092. }
  2093. else
  2094. {
  2095. m_innerScene.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID);
  2096. }
  2097. }
  2098. return;
  2099. }
  2100. return;
  2101. }
  2102. }
  2103. public virtual void SetHomeRezPoint(IClientAPI remoteClient, ulong regionHandle, Vector3 position, Vector3 lookAt, uint flags)
  2104. {
  2105. UserProfileData UserProfile = CommsManager.UserService.GetUserProfile(remoteClient.AgentId);
  2106. if (UserProfile != null)
  2107. {
  2108. // I know I'm ignoring the regionHandle provided by the teleport location request.
  2109. // reusing the TeleportLocationRequest delegate, so regionHandle isn't valid
  2110. UserProfile.HomeRegionID = RegionInfo.RegionID;
  2111. // TODO: The next line can be removed, as soon as only homeRegionID based UserServers are around.
  2112. // TODO: The HomeRegion property can be removed then, too
  2113. UserProfile.HomeRegion = RegionInfo.RegionHandle;
  2114. UserProfile.HomeLocation = position;
  2115. UserProfile.HomeLookAt = lookAt;
  2116. CommsManager.UserService.UpdateUserProfile(UserProfile);
  2117. // FUBAR ALERT: this needs to be "Home position set." so the viewer saves a home-screenshot.
  2118. remoteClient.SendAgentAlertMessage("Home position set.",false);
  2119. }
  2120. else
  2121. {
  2122. remoteClient.SendAgentAlertMessage("Set Home request Failed",false);
  2123. }
  2124. }
  2125. /// <summary>
  2126. /// Create a scene presence and add it to this scene.
  2127. /// </summary>
  2128. /// <param name="client"></param>
  2129. /// <param name="child"></param>
  2130. /// <returns></returns>
  2131. protected virtual ScenePresence CreateAndAddScenePresence(IClientAPI client, bool child)
  2132. {
  2133. AvatarAppearance appearance = null;
  2134. GetAvatarAppearance(client, out appearance);
  2135. ScenePresence avatar = m_innerScene.CreateAndAddScenePresence(client, child, appearance);
  2136. return avatar;
  2137. }
  2138. /// <summary>
  2139. /// Get the avatar apperance for the given client.
  2140. /// </summary>
  2141. /// <param name="client"></param>
  2142. /// <param name="appearance"></param>
  2143. public void GetAvatarAppearance(IClientAPI client, out AvatarAppearance appearance)
  2144. {
  2145. appearance = new AvatarAppearance();
  2146. try
  2147. {
  2148. if (m_AvatarFactory != null)
  2149. {
  2150. if (m_AvatarFactory.TryGetAvatarAppearance(client.AgentId, out appearance))
  2151. return;
  2152. }
  2153. }
  2154. catch (Exception e)
  2155. {
  2156. m_log.ErrorFormat("[APPEARANCE]: Problem fetching appearance for avatar {0}, {1}",
  2157. client.Name, e);
  2158. }
  2159. m_log.Warn("[APPEARANCE]: Appearance not found, returning default");
  2160. }
  2161. /// <summary>
  2162. /// Remove the given client from the scene.
  2163. /// </summary>
  2164. /// <param name="agentID"></param>
  2165. public override void RemoveClient(UUID agentID)
  2166. {
  2167. bool childagentYN = false;
  2168. ScenePresence avatar = GetScenePresence(agentID);
  2169. if (avatar != null)
  2170. {
  2171. childagentYN = avatar.IsChildAgent;
  2172. }
  2173. try
  2174. {
  2175. m_log.DebugFormat(
  2176. "[SCENE]: Removing {0} agent {1} from region {2}",
  2177. (childagentYN ? "child" : "root"), agentID, RegionInfo.RegionName);
  2178. if (avatar.IsChildAgent)
  2179. {
  2180. m_innerScene.removeUserCount(false);
  2181. }
  2182. else
  2183. {
  2184. m_innerScene.removeUserCount(true);
  2185. m_sceneGridService.LogOffUser(agentID, RegionInfo.RegionID, RegionInfo.RegionHandle, avatar.AbsolutePosition, avatar.Lookat);
  2186. List<ulong> childknownRegions = new List<ulong>();
  2187. List<ulong> ckn = avatar.GetKnownRegionList();
  2188. for (int i = 0; i < ckn.Count; i++)
  2189. {
  2190. childknownRegions.Add(ckn[i]);
  2191. }
  2192. m_sceneGridService.SendCloseChildAgentConnections(agentID, childknownRegions);
  2193. RemoveCapsHandler(agentID);
  2194. CommsManager.UserProfileCacheService.RemoveUser(agentID);
  2195. }
  2196. m_eventManager.TriggerClientClosed(agentID);
  2197. }
  2198. catch (NullReferenceException)
  2199. {
  2200. // We don't know which count to remove it from
  2201. // Avatar is already disposed :/
  2202. }
  2203. m_eventManager.TriggerOnRemovePresence(agentID);
  2204. Broadcast(delegate(IClientAPI client)
  2205. {
  2206. try
  2207. {
  2208. client.SendKillObject(avatar.RegionHandle, avatar.LocalId);
  2209. }
  2210. catch (NullReferenceException)
  2211. {
  2212. //We can safely ignore null reference exceptions. It means the avatar are dead and cleaned up anyway.
  2213. }
  2214. });
  2215. ForEachScenePresence(
  2216. delegate(ScenePresence presence) { presence.CoarseLocationChange(); });
  2217. IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
  2218. if (agentTransactions != null)
  2219. {
  2220. agentTransactions.RemoveAgentAssetTransactions(agentID);
  2221. }
  2222. m_innerScene.RemoveScenePresence(agentID);
  2223. try
  2224. {
  2225. avatar.Close();
  2226. }
  2227. catch (NullReferenceException)
  2228. {
  2229. //We can safely ignore null reference exceptions. It means the avatar are dead and cleaned up anyway.
  2230. }
  2231. catch (Exception e)
  2232. {
  2233. m_log.Error("[SCENE] Scene.cs:RemoveClient exception: " + e.ToString());
  2234. }
  2235. // Remove client agent from profile, so new logins will work
  2236. if (!childagentYN)
  2237. {
  2238. m_sceneGridService.ClearUserAgent(agentID);
  2239. }
  2240. //m_log.InfoFormat("[SCENE] Memory pre GC {0}", System.GC.GetTotalMemory(false));
  2241. //m_log.InfoFormat("[SCENE] Memory post GC {0}", System.GC.GetTotalMemory(true));
  2242. }
  2243. public void HandleRemoveKnownRegionsFromAvatar(UUID avatarID, List<ulong> regionslst)
  2244. {
  2245. ScenePresence av = GetScenePresence(avatarID);
  2246. if (av != null)
  2247. {
  2248. lock (av)
  2249. {
  2250. for (int i = 0; i < regionslst.Count; i++)
  2251. {
  2252. av.KnownChildRegions.Remove(regionslst[i]);
  2253. }
  2254. }
  2255. }
  2256. }
  2257. public override void CloseAllAgents(uint circuitcode)
  2258. {
  2259. // Called by ClientView to kill all circuit codes
  2260. ClientManager.CloseAllAgents(circuitcode);
  2261. }
  2262. public void NotifyMyCoarseLocationChange()
  2263. {
  2264. ForEachScenePresence(delegate(ScenePresence presence) { presence.CoarseLocationChange(); });
  2265. }
  2266. #endregion
  2267. #region Entities
  2268. public void SendKillObject(uint localID)
  2269. {
  2270. SceneObjectPart part = GetSceneObjectPart(localID);
  2271. if (part != null) // It is a prim
  2272. {
  2273. if (part.ParentGroup != null && part.ParentGroup.RootPart != null) // Valid
  2274. {
  2275. if (part.ParentGroup.RootPart != part) // Child part
  2276. return;
  2277. }
  2278. }
  2279. Broadcast(delegate(IClientAPI client) { client.SendKillObject(m_regionHandle, localID); });
  2280. }
  2281. #endregion
  2282. #region RegionComms
  2283. /// <summary>
  2284. /// Register the methods that should be invoked when this scene receives various incoming events
  2285. /// </summary>
  2286. public void RegisterCommsEvents()
  2287. {
  2288. m_sceneGridService.OnExpectUser += NewUserConnection;
  2289. m_sceneGridService.OnAvatarCrossingIntoRegion += AgentCrossing;
  2290. m_sceneGridService.OnCloseAgentConnection += CloseConnection;
  2291. m_sceneGridService.OnRegionUp += OtherRegionUp;
  2292. m_sceneGridService.OnChildAgentUpdate += IncomingChildAgentDataUpdate;
  2293. m_sceneGridService.OnExpectPrim += IncomingInterRegionPrimGroup;
  2294. m_sceneGridService.OnRemoveKnownRegionFromAvatar += HandleRemoveKnownRegionsFromAvatar;
  2295. m_sceneGridService.OnLogOffUser += HandleLogOffUserFromGrid;
  2296. m_sceneGridService.KiPrimitive += SendKillObject;
  2297. m_sceneGridService.OnGetLandData += GetLandData;
  2298. }
  2299. /// <summary>
  2300. ///
  2301. /// </summary>
  2302. public void UnRegisterRegionWithComms()
  2303. {
  2304. m_sceneGridService.KiPrimitive -= SendKillObject;
  2305. m_sceneGridService.OnLogOffUser -= HandleLogOffUserFromGrid;
  2306. m_sceneGridService.OnRemoveKnownRegionFromAvatar -= HandleRemoveKnownRegionsFromAvatar;
  2307. m_sceneGridService.OnExpectPrim -= IncomingInterRegionPrimGroup;
  2308. m_sceneGridService.OnChildAgentUpdate -= IncomingChildAgentDataUpdate;
  2309. m_sceneGridService.OnRegionUp -= OtherRegionUp;
  2310. m_sceneGridService.OnExpectUser -= NewUserConnection;
  2311. m_sceneGridService.OnAvatarCrossingIntoRegion -= AgentCrossing;
  2312. m_sceneGridService.OnCloseAgentConnection -= CloseConnection;
  2313. m_sceneGridService.OnGetLandData -= GetLandData;
  2314. m_sceneGridService.Close();
  2315. }
  2316. /// <summary>
  2317. /// Do the work necessary to initiate a new user connection for a particular scene.
  2318. /// At the moment, this consists of setting up the caps infrastructure
  2319. /// </summary>
  2320. /// <param name="regionHandle"></param>
  2321. /// <param name="agent"></param>
  2322. public void NewUserConnection(ulong regionHandle, AgentCircuitData agent)
  2323. {
  2324. if (regionHandle == m_regInfo.RegionHandle)
  2325. {
  2326. if (m_regInfo.EstateSettings.IsBanned(agent.AgentID))
  2327. {
  2328. m_log.WarnFormat(
  2329. "[CONNECTION DEBUGGING]: Denied access to: {0} [{1}] at {2} because the user is on the region banlist",
  2330. agent.AgentID, regionHandle, RegionInfo.RegionName);
  2331. }
  2332. capsPaths[agent.AgentID] = agent.CapsPath;
  2333. if (!agent.child)
  2334. {
  2335. AddCapsHandler(agent.AgentID);
  2336. // Honor parcel landing type and position.
  2337. ILandObject land = LandChannel.GetLandObject(agent.startpos.X, agent.startpos.Y);
  2338. if (land != null)
  2339. {
  2340. if (land.landData.LandingType == (byte)1 && land.landData.UserLocation != Vector3.Zero)
  2341. {
  2342. agent.startpos = land.landData.UserLocation;
  2343. }
  2344. }
  2345. }
  2346. m_authenticateHandler.AddNewCircuit(agent.circuitcode, agent);
  2347. // rewrite session_id
  2348. CachedUserInfo userinfo = CommsManager.UserProfileCacheService.GetUserDetails(agent.AgentID);
  2349. if (userinfo != null)
  2350. {
  2351. userinfo.SessionID = agent.SessionID;
  2352. }
  2353. else
  2354. {
  2355. m_log.WarnFormat("[USERINFO CACHE]: We couldn't find a User Info record for {0}. This is usually an indication that the UUID we're looking up is invalid", agent.AgentID);
  2356. }
  2357. }
  2358. else
  2359. {
  2360. m_log.WarnFormat(
  2361. "[CONNECTION DEBUGGING]: Skipping this region for welcoming avatar {0} [{1}] at {2}",
  2362. agent.AgentID, regionHandle, RegionInfo.RegionName);
  2363. }
  2364. }
  2365. public void UpdateCircuitData(AgentCircuitData data)
  2366. {
  2367. m_authenticateHandler.UpdateAgentData(data);
  2368. }
  2369. public bool ChangeCircuitCode(uint oldcc, uint newcc)
  2370. {
  2371. return m_authenticateHandler.TryChangeCiruitCode(oldcc, newcc);
  2372. }
  2373. protected void HandleLogOffUserFromGrid(ulong regionHandle, UUID AvatarID, UUID RegionSecret, string message)
  2374. {
  2375. if (RegionInfo.RegionHandle == regionHandle)
  2376. {
  2377. ScenePresence loggingOffUser = null;
  2378. loggingOffUser = GetScenePresence(AvatarID);
  2379. if (loggingOffUser != null)
  2380. {
  2381. if (RegionSecret == loggingOffUser.ControllingClient.SecureSessionId)
  2382. {
  2383. loggingOffUser.ControllingClient.Kick(message);
  2384. // Give them a second to receive the message!
  2385. System.Threading.Thread.Sleep(1000);
  2386. loggingOffUser.ControllingClient.Close(true);
  2387. }
  2388. else
  2389. {
  2390. m_log.Info("[USERLOGOFF]: System sending the LogOff user message failed to sucessfully authenticate");
  2391. }
  2392. }
  2393. else
  2394. {
  2395. m_log.InfoFormat("[USERLOGOFF]: Got a logoff request for {0} but the user isn't here. The user might already have been logged out", AvatarID.ToString());
  2396. }
  2397. }
  2398. }
  2399. /// <summary>
  2400. /// Add a caps handler for the given agent. If the CAPS handler already exists for this agent,
  2401. /// then it is replaced by a new CAPS handler.
  2402. ///
  2403. /// FIXME: On login this is called twice, once for the login and once when the connection is made.
  2404. /// This is somewhat innefficient and should be fixed. The initial login creation is necessary
  2405. /// since the client asks for capabilities immediately after being informed of the seed.
  2406. /// </summary>
  2407. /// <param name="agentId"></param>
  2408. /// <param name="capsObjectPath"></param>
  2409. public void AddCapsHandler(UUID agentId)
  2410. {
  2411. if (RegionInfo.EstateSettings.IsBanned(agentId))
  2412. return;
  2413. String capsObjectPath = GetCapsPath(agentId);
  2414. m_log.DebugFormat(
  2415. "[CAPS]: Setting up CAPS handler for root agent {0} in {1}",
  2416. agentId, RegionInfo.RegionName);
  2417. Caps cap =
  2418. new Caps(AssetCache, m_httpListener, m_regInfo.ExternalHostName, m_httpListener.Port,
  2419. capsObjectPath, agentId, m_dumpAssetsToFile, RegionInfo.RegionName);
  2420. cap.RegisterHandlers();
  2421. EventManager.TriggerOnRegisterCaps(agentId, cap);
  2422. cap.AddNewInventoryItem = AddUploadedInventoryItem;
  2423. cap.ItemUpdatedCall = CapsUpdateInventoryItemAsset;
  2424. cap.TaskScriptUpdatedCall = CapsUpdateTaskInventoryScriptAsset;
  2425. cap.CAPSFetchInventoryDescendents = CommsManager.UserProfileCacheService.HandleFetchInventoryDescendentsCAPS;
  2426. cap.GetClient = m_innerScene.GetControllingClient;
  2427. m_capsHandlers[agentId] = cap;
  2428. }
  2429. public Caps GetCapsHandlerForUser(UUID agentId)
  2430. {
  2431. lock (m_capsHandlers)
  2432. {
  2433. if (m_capsHandlers.ContainsKey(agentId))
  2434. {
  2435. return m_capsHandlers[agentId];
  2436. }
  2437. }
  2438. return null;
  2439. }
  2440. /// <summary>
  2441. /// Remove the caps handler for a given agent.
  2442. /// </summary>
  2443. /// <param name="agentId"></param>
  2444. public void RemoveCapsHandler(UUID agentId)
  2445. {
  2446. lock (m_capsHandlers)
  2447. {
  2448. if (m_capsHandlers.ContainsKey(agentId))
  2449. {
  2450. m_log.DebugFormat(
  2451. "[CAPS]: Removing CAPS handler for root agent {0} in {1}",
  2452. agentId, RegionInfo.RegionName);
  2453. m_capsHandlers[agentId].DeregisterHandlers();
  2454. EventManager.TriggerOnDeregisterCaps(agentId, m_capsHandlers[agentId]);
  2455. m_capsHandlers.Remove(agentId);
  2456. }
  2457. else
  2458. {
  2459. m_log.WarnFormat(
  2460. "[CAPS]: Received request to remove CAPS handler for root agent {0} in {1}, but no such CAPS handler found!",
  2461. agentId, RegionInfo.RegionName);
  2462. }
  2463. }
  2464. }
  2465. /// <summary>
  2466. ///
  2467. /// </summary>
  2468. /// <param name="regionHandle"></param>
  2469. /// <param name="agentID"></param>
  2470. /// <param name="position"></param>
  2471. /// <param name="isFlying"></param>
  2472. public virtual void AgentCrossing(ulong regionHandle, UUID agentID, Vector3 position, bool isFlying)
  2473. {
  2474. if (regionHandle == m_regInfo.RegionHandle)
  2475. {
  2476. lock (m_scenePresences)
  2477. {
  2478. if (m_scenePresences.ContainsKey(agentID))
  2479. {
  2480. try
  2481. {
  2482. m_scenePresences[agentID].MakeRootAgent(position, isFlying);
  2483. }
  2484. catch (Exception e)
  2485. {
  2486. m_log.Info("[SCENE]: Unable to do Agent Crossing.");
  2487. m_log.Debug("[SCENE]: " + e.ToString());
  2488. }
  2489. //m_innerScene.SwapRootChildAgent(false);
  2490. }
  2491. }
  2492. }
  2493. }
  2494. public virtual bool IncomingChildAgentDataUpdate(ulong regionHandle, ChildAgentDataUpdate cAgentData)
  2495. {
  2496. ScenePresence childAgentUpdate = GetScenePresence(new UUID(cAgentData.AgentID));
  2497. if (childAgentUpdate != null)
  2498. {
  2499. // I can't imagine *yet* why we would get an update if the agent is a root agent..
  2500. // however to avoid a race condition crossing borders..
  2501. if (childAgentUpdate.IsChildAgent)
  2502. {
  2503. uint rRegionX = (uint)(cAgentData.regionHandle >> 40);
  2504. uint rRegionY = (((uint)(cAgentData.regionHandle)) >> 8);
  2505. uint tRegionX = RegionInfo.RegionLocX;
  2506. uint tRegionY = RegionInfo.RegionLocY;
  2507. //Send Data to ScenePresence
  2508. childAgentUpdate.ChildAgentDataUpdate(cAgentData, tRegionX, tRegionY, rRegionX, rRegionY);
  2509. // Not Implemented:
  2510. //TODO: Do we need to pass the message on to one of our neighbors?
  2511. }
  2512. return true;
  2513. }
  2514. return false;
  2515. }
  2516. /// <summary>
  2517. /// Tell a single agent to disconnect from the region.
  2518. /// </summary>
  2519. /// <param name="regionHandle"></param>
  2520. /// <param name="agentID"></param>
  2521. public bool CloseConnection(ulong regionHandle, UUID agentID)
  2522. {
  2523. if (regionHandle == m_regionHandle)
  2524. {
  2525. ScenePresence presence = m_innerScene.GetScenePresence(agentID);
  2526. if (presence != null)
  2527. {
  2528. // Nothing is removed here, so down count it as such
  2529. // if (presence.IsChildAgent)
  2530. // {
  2531. // m_innerScene.removeUserCount(false);
  2532. // }
  2533. // else
  2534. // {
  2535. // m_innerScene.removeUserCount(true);
  2536. // }
  2537. // Tell a single agent to disconnect from the region.
  2538. presence.ControllingClient.SendShutdownConnectionNotice();
  2539. presence.ControllingClient.Close(true);
  2540. }
  2541. }
  2542. return true;
  2543. }
  2544. /// <summary>
  2545. /// Tell neighboring regions about this agent
  2546. /// When the regions respond with a true value,
  2547. /// tell the agents about the region.
  2548. ///
  2549. /// We have to tell the regions about the agents first otherwise it'll deny them access
  2550. ///
  2551. /// </summary>
  2552. /// <param name="presence"></param>
  2553. public void InformClientOfNeighbours(ScenePresence presence)
  2554. {
  2555. m_sceneGridService.EnableNeighbourChildAgents(presence, m_neighbours);
  2556. }
  2557. /// <summary>
  2558. /// Tell a neighboring region about this agent
  2559. /// </summary>
  2560. /// <param name="presence"></param>
  2561. /// <param name="region"></param>
  2562. public void InformClientOfNeighbor(ScenePresence presence, RegionInfo region)
  2563. {
  2564. m_sceneGridService.InformNeighborChildAgent(presence, region, m_neighbours);
  2565. }
  2566. /// <summary>
  2567. /// Requests information about this region from gridcomms
  2568. /// </summary>
  2569. /// <param name="regionHandle"></param>
  2570. /// <returns></returns>
  2571. public RegionInfo RequestNeighbouringRegionInfo(ulong regionHandle)
  2572. {
  2573. return m_sceneGridService.RequestNeighbouringRegionInfo(regionHandle);
  2574. }
  2575. /// <summary>
  2576. /// Requests textures for map from minimum region to maximum region in world cordinates
  2577. /// </summary>
  2578. /// <param name="remoteClient"></param>
  2579. /// <param name="minX"></param>
  2580. /// <param name="minY"></param>
  2581. /// <param name="maxX"></param>
  2582. /// <param name="maxY"></param>
  2583. public void RequestMapBlocks(IClientAPI remoteClient, int minX, int minY, int maxX, int maxY)
  2584. {
  2585. m_log.InfoFormat("[MAPBLOCK]: {0}-{1}, {2}-{3}", minX, minY, maxX, maxY);
  2586. m_sceneGridService.RequestMapBlocks(remoteClient, minX, minY, maxX, maxY);
  2587. }
  2588. /// <summary>
  2589. /// Tries to teleport agent to other region.
  2590. /// </summary>
  2591. /// <param name="remoteClient"></param>
  2592. /// <param name="regionName"></param>
  2593. /// <param name="position"></param>
  2594. /// <param name="lookAt"></param>
  2595. /// <param name="teleportFlags"></param>
  2596. public void RequestTeleportLocation(IClientAPI remoteClient, string regionName, Vector3 position,
  2597. Vector3 lookat, uint teleportFlags)
  2598. {
  2599. RegionInfo regionInfo = m_sceneGridService.RequestClosestRegion(regionName);
  2600. if (regionInfo == null)
  2601. {
  2602. // can't find the region: Tell viewer and abort
  2603. remoteClient.SendTeleportFailed("The region '" + regionName + "' could not be found.");
  2604. return;
  2605. }
  2606. RequestTeleportLocation(remoteClient, regionInfo.RegionHandle, position, lookat, teleportFlags);
  2607. }
  2608. /// <summary>
  2609. /// Tries to teleport agent to other region.
  2610. /// </summary>
  2611. /// <param name="remoteClient"></param>
  2612. /// <param name="regionHandle"></param>
  2613. /// <param name="position"></param>
  2614. /// <param name="lookAt"></param>
  2615. /// <param name="teleportFlags"></param>
  2616. public void RequestTeleportLocation(IClientAPI remoteClient, ulong regionHandle, Vector3 position,
  2617. Vector3 lookAt, uint teleportFlags)
  2618. {
  2619. lock (m_scenePresences)
  2620. {
  2621. if (m_scenePresences.ContainsKey(remoteClient.AgentId))
  2622. {
  2623. m_sceneGridService.RequestTeleportToLocation(m_scenePresences[remoteClient.AgentId], regionHandle,
  2624. position, lookAt, teleportFlags);
  2625. }
  2626. }
  2627. }
  2628. /// <summary>
  2629. /// Tries to teleport agent to landmark.
  2630. /// </summary>
  2631. /// <param name="remoteClient"></param>
  2632. /// <param name="regionHandle"></param>
  2633. /// <param name="position"></param>
  2634. public void RequestTeleportLandmark(IClientAPI remoteClient, UUID regionID, Vector3 position)
  2635. {
  2636. RegionInfo info = CommsManager.GridService.RequestNeighbourInfo(regionID);
  2637. if (info == null)
  2638. {
  2639. // can't find the region: Tell viewer and abort
  2640. remoteClient.SendTeleportFailed("The teleport destination could not be found.");
  2641. return;
  2642. }
  2643. lock (m_scenePresences)
  2644. {
  2645. if (m_scenePresences.ContainsKey(remoteClient.AgentId))
  2646. {
  2647. m_sceneGridService.RequestTeleportToLocation(m_scenePresences[remoteClient.AgentId], info.RegionHandle,
  2648. position, Vector3.Zero, (uint)(TPFlags.SetLastToTarget | TPFlags.ViaLandmark));
  2649. }
  2650. }
  2651. }
  2652. /// <summary>
  2653. /// Agent is crossing the border into a neighbouring region. Tell the neighbour about it!
  2654. /// </summary>
  2655. /// <param name="regionHandle"></param>
  2656. /// <param name="agentID"></param>
  2657. /// <param name="position"></param>
  2658. /// <param name="isFlying"></param>
  2659. /// <returns></returns>
  2660. public bool InformNeighbourOfCrossing(ulong regionHandle, UUID agentID, Vector3 position, bool isFlying)
  2661. {
  2662. return m_sceneGridService.CrossToNeighbouringRegion(regionHandle, agentID, position, isFlying);
  2663. }
  2664. public void SendOutChildAgentUpdates(ChildAgentDataUpdate cadu, ScenePresence presence)
  2665. {
  2666. m_sceneGridService.SendChildAgentDataUpdate(cadu, presence);
  2667. }
  2668. #endregion
  2669. #region Module Methods
  2670. /// <summary>
  2671. ///
  2672. /// </summary>
  2673. /// <param name="name"></param>
  2674. /// <param name="module"></param>
  2675. public void AddModule(string name, IRegionModule module)
  2676. {
  2677. if (!Modules.ContainsKey(name))
  2678. {
  2679. Modules.Add(name, module);
  2680. }
  2681. }
  2682. public void RegisterModuleCommander(string name, ICommander commander)
  2683. {
  2684. lock (m_moduleCommanders)
  2685. {
  2686. m_moduleCommanders.Add(name, commander);
  2687. }
  2688. }
  2689. public ICommander GetCommander(string name)
  2690. {
  2691. lock (m_moduleCommanders)
  2692. {
  2693. return m_moduleCommanders[name];
  2694. }
  2695. }
  2696. public Dictionary<string, ICommander> GetCommanders()
  2697. {
  2698. return m_moduleCommanders;
  2699. }
  2700. /// <summary>
  2701. /// Register an interface to a region module. This allows module methods to be called directly as
  2702. /// well as via events. If there is already a module registered for this interface, it is not replaced
  2703. /// (is this the best behaviour?)
  2704. /// </summary>
  2705. /// <param name="mod"></param>
  2706. public void RegisterModuleInterface<M>(M mod)
  2707. {
  2708. if (!ModuleInterfaces.ContainsKey(typeof(M)))
  2709. {
  2710. List<Object> l = new List<Object>();
  2711. l.Add(mod);
  2712. ModuleInterfaces.Add(typeof(M), l);
  2713. }
  2714. }
  2715. public void StackModuleInterface<M>(M mod)
  2716. {
  2717. List<Object> l;
  2718. if (ModuleInterfaces.ContainsKey(typeof(M)))
  2719. l = ModuleInterfaces[typeof(M)];
  2720. else
  2721. l = new List<Object>();
  2722. if (l.Contains(mod))
  2723. return;
  2724. l.Add(mod);
  2725. ModuleInterfaces[typeof(M)] = l;
  2726. }
  2727. /// <summary>
  2728. /// For the given interface, retrieve the region module which implements it.
  2729. /// </summary>
  2730. /// <returns>null if there is no module implementing that interface</returns>
  2731. public override T RequestModuleInterface<T>()
  2732. {
  2733. if (ModuleInterfaces.ContainsKey(typeof(T)))
  2734. {
  2735. return (T)ModuleInterfaces[typeof(T)][0];
  2736. }
  2737. else
  2738. {
  2739. return default(T);
  2740. }
  2741. }
  2742. public override T[] RequestModuleInterfaces<T>()
  2743. {
  2744. if (ModuleInterfaces.ContainsKey(typeof(T)))
  2745. {
  2746. List<T> ret = new List<T>();
  2747. foreach (Object o in ModuleInterfaces[typeof(T)])
  2748. ret.Add((T)o);
  2749. return ret.ToArray();
  2750. }
  2751. else
  2752. {
  2753. return new T[] { default(T) };
  2754. }
  2755. }
  2756. public void SetObjectCapacity(int objects)
  2757. {
  2758. if (m_statsReporter != null)
  2759. {
  2760. m_statsReporter.SetObjectCapacity(objects);
  2761. }
  2762. objectCapacity = objects;
  2763. }
  2764. public List<FriendListItem> GetFriendList(UUID avatarID)
  2765. {
  2766. return CommsManager.GetUserFriendList(avatarID);
  2767. }
  2768. public Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos(List<UUID> uuids)
  2769. {
  2770. return CommsManager.GetFriendRegionInfos(uuids);
  2771. }
  2772. public List<UUID> InformFriendsInOtherRegion(UUID agentId, ulong destRegionHandle, List<UUID> friends, bool online)
  2773. {
  2774. return CommsManager.InformFriendsInOtherRegion(agentId, destRegionHandle, friends, online);
  2775. }
  2776. public bool TriggerTerminateFriend(ulong regionHandle, UUID agentID, UUID exFriendID)
  2777. {
  2778. return CommsManager.TriggerTerminateFriend(regionHandle, agentID, exFriendID);
  2779. }
  2780. #endregion
  2781. #region Other Methods
  2782. /// <summary>
  2783. ///
  2784. /// </summary>
  2785. /// <param name="phase"></param>
  2786. public void SetTimePhase(int phase)
  2787. {
  2788. m_timePhase = phase;
  2789. }
  2790. /// <summary>
  2791. ///
  2792. /// </summary>
  2793. /// <param name="avatarID"></param>
  2794. /// <param name="objectName"></param>
  2795. /// <param name="objectID"></param>
  2796. /// <param name="ownerID"></param>
  2797. /// <param name="groupOwned"></param>
  2798. /// <param name="message"></param>
  2799. /// <param name="url"></param>
  2800. public void SendUrlToUser(UUID avatarID, string objectName, UUID objectID, UUID ownerID, bool groupOwned,
  2801. string message, string url)
  2802. {
  2803. lock (m_scenePresences)
  2804. {
  2805. if (m_scenePresences.ContainsKey(avatarID))
  2806. {
  2807. m_scenePresences[avatarID].ControllingClient.SendLoadURL(objectName, objectID, ownerID, groupOwned,
  2808. message, url);
  2809. }
  2810. }
  2811. }
  2812. public void SendDialogToUser(UUID avatarID, string objectName, UUID objectID, UUID ownerID, string message, UUID TextureID, int ch, string[] buttonlabels)
  2813. {
  2814. lock (m_scenePresences)
  2815. {
  2816. if (m_scenePresences.ContainsKey(avatarID))
  2817. {
  2818. m_scenePresences[avatarID].ControllingClient.SendDialog(
  2819. objectName, objectID, ownerID, message, TextureID, ch, buttonlabels);
  2820. }
  2821. }
  2822. }
  2823. /// <summary>
  2824. ///
  2825. /// </summary>
  2826. /// <param name="url"></param>
  2827. /// <param name="type"></param>
  2828. /// <param name="body"></param>
  2829. /// <returns></returns>
  2830. public UUID MakeHttpRequest(string url, string type, string body)
  2831. {
  2832. if (m_httpRequestModule != null)
  2833. {
  2834. return m_httpRequestModule.MakeHttpRequest(url, type, body);
  2835. }
  2836. return UUID.Zero;
  2837. }
  2838. /// <summary>
  2839. /// This method is a way for the Friends Module to create an instant
  2840. /// message to the avatar and for Instant Messages that travel across
  2841. /// gridcomms to make it to the Instant Message Module.
  2842. ///
  2843. /// Friendship establishment and groups are unfortunately tied with instant messaging and
  2844. /// there's no way to separate them completely.
  2845. /// </summary>
  2846. /// <param name="message">object containing the instant message data</param>
  2847. /// <returns>void</returns>
  2848. public void TriggerGridInstantMessage(GridInstantMessage message, InstantMessageReceiver options)
  2849. {
  2850. m_eventManager.TriggerGridInstantMessage(message, options);
  2851. }
  2852. public virtual void StoreAddFriendship(UUID ownerID, UUID friendID, uint perms)
  2853. {
  2854. // TODO: m_sceneGridService.DoStuff;
  2855. m_sceneGridService.AddNewUserFriend(ownerID, friendID, perms);
  2856. }
  2857. public virtual void StoreUpdateFriendship(UUID ownerID, UUID friendID, uint perms)
  2858. {
  2859. // TODO: m_sceneGridService.DoStuff;
  2860. m_sceneGridService.UpdateUserFriendPerms(ownerID, friendID, perms);
  2861. }
  2862. public virtual void StoreRemoveFriendship(UUID ownerID, UUID ExfriendID)
  2863. {
  2864. // TODO: m_sceneGridService.DoStuff;
  2865. m_sceneGridService.RemoveUserFriend(ownerID, ExfriendID);
  2866. }
  2867. public virtual List<FriendListItem> StoreGetFriendsForUser(UUID ownerID)
  2868. {
  2869. // TODO: m_sceneGridService.DoStuff;
  2870. return m_sceneGridService.GetUserFriendList(ownerID);
  2871. }
  2872. public void AddPacketStats(int inPackets, int outPackets, int unAckedBytes)
  2873. {
  2874. m_statsReporter.AddInPackets(inPackets);
  2875. m_statsReporter.AddOutPackets(outPackets);
  2876. m_statsReporter.AddunAckedBytes(unAckedBytes);
  2877. }
  2878. public void AddAgentTime(int ms)
  2879. {
  2880. m_statsReporter.addFrameMS(ms);
  2881. m_statsReporter.addAgentMS(ms);
  2882. }
  2883. public void AddAgentUpdates(int count)
  2884. {
  2885. m_statsReporter.AddAgentUpdates(count);
  2886. }
  2887. public void AddPendingDownloads(int count)
  2888. {
  2889. m_statsReporter.addPendingDownload(count);
  2890. }
  2891. #endregion
  2892. #region Console Commands
  2893. #region Alert Methods
  2894. private void SendPermissionAlert(UUID user, string reason)
  2895. {
  2896. SendAlertToUser(user, reason, false);
  2897. }
  2898. /// <summary>
  2899. /// Send an alert messages to all avatars in this scene.
  2900. /// </summary>
  2901. /// <param name="message"></param>
  2902. public void SendGeneralAlert(string message)
  2903. {
  2904. List<ScenePresence> presenceList = GetScenePresences();
  2905. foreach (ScenePresence presence in presenceList)
  2906. {
  2907. if (!presence.IsChildAgent)
  2908. presence.ControllingClient.SendAlertMessage(message);
  2909. }
  2910. }
  2911. /// <summary>
  2912. /// Send an alert message to a particular agent.
  2913. /// </summary>
  2914. /// <param name="agentID"></param>
  2915. /// <param name="message"></param>
  2916. /// <param name="modal"></param>
  2917. public void SendAlertToUser(UUID agentID, string message, bool modal)
  2918. {
  2919. lock (m_scenePresences)
  2920. {
  2921. if (m_scenePresences.ContainsKey(agentID))
  2922. {
  2923. m_scenePresences[agentID].ControllingClient.SendAgentAlertMessage(message, modal);
  2924. }
  2925. }
  2926. }
  2927. /// <summary>
  2928. /// Handle a request for admin rights
  2929. /// </summary>
  2930. /// <param name="agentID"></param>
  2931. /// <param name="sessionID"></param>
  2932. /// <param name="token"></param>
  2933. /// <param name="controllingClient"></param>
  2934. public void handleRequestGodlikePowers(UUID agentID, UUID sessionID, UUID token, bool godLike,
  2935. IClientAPI controllingClient)
  2936. {
  2937. lock (m_scenePresences)
  2938. {
  2939. // User needs to be logged into this sim
  2940. if (m_scenePresences.ContainsKey(agentID))
  2941. {
  2942. if (godLike == false)
  2943. {
  2944. m_scenePresences[agentID].GrantGodlikePowers(agentID, sessionID, token, godLike);
  2945. return;
  2946. }
  2947. // First check that this is the sim owner
  2948. if (ExternalChecks.ExternalChecksCanBeGodLike(agentID))
  2949. {
  2950. // Next we check for spoofing.....
  2951. UUID testSessionID = m_scenePresences[agentID].ControllingClient.SessionId;
  2952. if (sessionID == testSessionID)
  2953. {
  2954. if (sessionID == controllingClient.SessionId)
  2955. {
  2956. //m_log.Info("godlike: " + godLike.ToString());
  2957. m_scenePresences[agentID].GrantGodlikePowers(agentID, testSessionID, token, godLike);
  2958. }
  2959. }
  2960. }
  2961. else
  2962. {
  2963. m_scenePresences[agentID].ControllingClient.SendAgentAlertMessage("Request for god powers denied", false);
  2964. }
  2965. }
  2966. }
  2967. }
  2968. /// <summary>
  2969. /// Sends a Big Blue Box message on the upper right of the screen to the client
  2970. /// for all agents in the region
  2971. /// </summary>
  2972. /// <param name="FromAvatarID">The person sending the message</param>
  2973. /// <param name="fromSessionID">The session of the person sending the message</param>
  2974. /// <param name="FromAvatarName">The name of the person doing the sending</param>
  2975. /// <param name="Message">The Message being sent to the user</param>
  2976. public void SendRegionMessageFromEstateTools(UUID FromAvatarID, UUID fromSessionID, String FromAvatarName, String Message)
  2977. {
  2978. List<ScenePresence> presenceList = GetScenePresences();
  2979. foreach (ScenePresence presence in presenceList)
  2980. {
  2981. if (!presence.IsChildAgent)
  2982. presence.ControllingClient.SendBlueBoxMessage(FromAvatarID, FromAvatarName, Message);
  2983. }
  2984. }
  2985. /// <summary>
  2986. /// Sends a Big Blue Box message on the upper right of the screen to the client
  2987. /// for all agents in the estate
  2988. /// </summary>
  2989. /// <param name="FromAvatarID">The person sending the message</param>
  2990. /// <param name="fromSessionID">The session of the person sending the message</param>
  2991. /// <param name="FromAvatarName">The name of the person doing the sending</param>
  2992. /// <param name="Message">The Message being sent to the user</param>
  2993. public void SendEstateMessageFromEstateTools(UUID FromAvatarID, UUID fromSessionID, String FromAvatarName, String Message)
  2994. {
  2995. ClientManager.ForEachClient(delegate(IClientAPI controller)
  2996. {
  2997. controller.SendBlueBoxMessage(FromAvatarID, FromAvatarName, Message);
  2998. }
  2999. );
  3000. }
  3001. /// <summary>
  3002. /// Kicks User specified from the simulator. This logs them off of the grid
  3003. /// If the client gets the UUID: 44e87126e7944ded05b37c42da3d5cdb it assumes
  3004. /// that you're kicking it even if the avatar's UUID isn't the UUID that the
  3005. /// agent is assigned
  3006. /// </summary>
  3007. /// <param name="godID">The person doing the kicking</param>
  3008. /// <param name="sessionID">The session of the person doing the kicking</param>
  3009. /// <param name="agentID">the person that is being kicked</param>
  3010. /// <param name="kickflags">This isn't used apparently</param>
  3011. /// <param name="reason">The message to send to the user after it's been turned into a field</param>
  3012. public void HandleGodlikeKickUser(UUID godID, UUID sessionID, UUID agentID, uint kickflags, byte[] reason)
  3013. {
  3014. // For some reason the client sends this seemingly hard coded UUID for kicking everyone. Dun-know.
  3015. UUID kickUserID = new UUID("44e87126e7944ded05b37c42da3d5cdb");
  3016. lock (m_scenePresences)
  3017. {
  3018. if (m_scenePresences.ContainsKey(agentID) || agentID == kickUserID)
  3019. {
  3020. if (ExternalChecks.ExternalChecksCanBeGodLike(godID))
  3021. {
  3022. if (agentID == kickUserID)
  3023. {
  3024. ClientManager.ForEachClient(delegate(IClientAPI controller)
  3025. {
  3026. if (controller.AgentId != godID)
  3027. controller.Kick(Utils.BytesToString(reason));
  3028. }
  3029. );
  3030. // This is a bit crude. It seems the client will be null before it actually stops the thread
  3031. // The thread will kill itself eventually :/
  3032. // Is there another way to make sure *all* clients get this 'inter region' message?
  3033. ClientManager.ForEachClient(delegate(IClientAPI controller)
  3034. {
  3035. ScenePresence p = GetScenePresence(controller.AgentId);
  3036. bool childagent = p != null && p.IsChildAgent;
  3037. if (controller.AgentId != godID && !childagent)
  3038. // Do we really want to kick the initiator of this madness?
  3039. {
  3040. controller.Close(true);
  3041. }
  3042. }
  3043. );
  3044. }
  3045. else
  3046. {
  3047. m_innerScene.removeUserCount(!m_scenePresences[agentID].IsChildAgent);
  3048. m_scenePresences[agentID].ControllingClient.Kick(Utils.BytesToString(reason));
  3049. m_scenePresences[agentID].ControllingClient.Close(true);
  3050. }
  3051. }
  3052. else
  3053. {
  3054. if (m_scenePresences.ContainsKey(godID))
  3055. m_scenePresences[godID].ControllingClient.SendAgentAlertMessage("Kick request denied", false);
  3056. }
  3057. }
  3058. }
  3059. }
  3060. public void HandleObjectPermissionsUpdate(IClientAPI controller, UUID agentID, UUID sessionID, byte field, uint localId, uint mask, byte set)
  3061. {
  3062. // Check for spoofing.. since this is permissions we're talking about here!
  3063. if ((controller.SessionId == sessionID) && (controller.AgentId == agentID))
  3064. {
  3065. // Tell the object to do permission update
  3066. if (localId != 0)
  3067. {
  3068. SceneObjectGroup chObjectGroup = GetGroupByPrim(localId);
  3069. if (chObjectGroup != null)
  3070. {
  3071. chObjectGroup.UpdatePermissions(agentID, field, localId, mask, set);
  3072. }
  3073. }
  3074. }
  3075. }
  3076. /// <summary>
  3077. ///
  3078. /// </summary>
  3079. /// <param name="firstName"></param>
  3080. /// <param name="lastName"></param>
  3081. /// <param name="message"></param>
  3082. /// <param name="modal"></param>
  3083. public void SendAlertToUser(string firstName, string lastName, string message, bool modal)
  3084. {
  3085. List<ScenePresence> presenceList = GetScenePresences();
  3086. foreach (ScenePresence presence in presenceList)
  3087. {
  3088. if (presence.Firstname == firstName && presence.Lastname == lastName)
  3089. {
  3090. presence.ControllingClient.SendAgentAlertMessage(message, modal);
  3091. break;
  3092. }
  3093. }
  3094. }
  3095. /// <summary>
  3096. /// Handle an alert command from the console.
  3097. /// FIXME: Command parsing code really shouldn't be in this core Scene class.
  3098. /// </summary>
  3099. /// <param name="commandParams"></param>
  3100. public void HandleAlertCommand(string[] commandParams)
  3101. {
  3102. if (commandParams[0] == "general")
  3103. {
  3104. string message = CombineParams(commandParams, 1);
  3105. SendGeneralAlert(message);
  3106. }
  3107. else
  3108. {
  3109. string message = CombineParams(commandParams, 2);
  3110. SendAlertToUser(commandParams[0], commandParams[1], message, false);
  3111. }
  3112. }
  3113. private string CombineParams(string[] commandParams, int pos)
  3114. {
  3115. string result = String.Empty;
  3116. for (int i = pos; i < commandParams.Length; i++)
  3117. {
  3118. result += commandParams[i] + " ";
  3119. }
  3120. return result;
  3121. }
  3122. #endregion
  3123. /// <summary>
  3124. /// Causes all clients to get a full object update on all of the objects in the scene.
  3125. /// </summary>
  3126. public void ForceClientUpdate()
  3127. {
  3128. List<EntityBase> EntityList = GetEntities();
  3129. foreach (EntityBase ent in EntityList)
  3130. {
  3131. if (ent is SceneObjectGroup)
  3132. {
  3133. ((SceneObjectGroup)ent).ScheduleGroupForFullUpdate();
  3134. }
  3135. }
  3136. }
  3137. /// <summary>
  3138. /// This is currently only used for scale (to scale to MegaPrim size)
  3139. /// There is a console command that calls this in OpenSimMain
  3140. /// </summary>
  3141. /// <param name="cmdparams"></param>
  3142. public void HandleEditCommand(string[] cmdparams)
  3143. {
  3144. Console.WriteLine("Searching for Primitive: '" + cmdparams[0] + "'");
  3145. List<EntityBase> EntityList = GetEntities();
  3146. foreach (EntityBase ent in EntityList)
  3147. {
  3148. if (ent is SceneObjectGroup)
  3149. {
  3150. SceneObjectPart part = ((SceneObjectGroup)ent).GetChildPart(((SceneObjectGroup)ent).UUID);
  3151. if (part != null)
  3152. {
  3153. if (part.Name == cmdparams[0])
  3154. {
  3155. part.Resize(
  3156. new Vector3(Convert.ToSingle(cmdparams[1]), Convert.ToSingle(cmdparams[2]),
  3157. Convert.ToSingle(cmdparams[3])));
  3158. Console.WriteLine("Edited scale of Primitive: " + part.Name);
  3159. }
  3160. }
  3161. }
  3162. }
  3163. }
  3164. /// <summary>
  3165. /// Shows various details about the sim based on the parameters supplied by the console command in openSimMain.
  3166. /// </summary>
  3167. /// <param name="showParams">What to show</param>
  3168. public void Show(string[] showParams)
  3169. {
  3170. switch (showParams[0])
  3171. {
  3172. case "users":
  3173. m_log.Error("Current Region: " + RegionInfo.RegionName);
  3174. m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16}{5,-16}{6,-16}", "Firstname", "Lastname",
  3175. "Agent ID", "Session ID", "Circuit", "IP", "World");
  3176. foreach (ScenePresence scenePresence in GetAvatars())
  3177. {
  3178. m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}{6,-16}",
  3179. scenePresence.Firstname,
  3180. scenePresence.Lastname,
  3181. scenePresence.UUID,
  3182. scenePresence.ControllingClient.AgentId,
  3183. "Unknown",
  3184. "Unknown",
  3185. RegionInfo.RegionName);
  3186. }
  3187. break;
  3188. case "modules":
  3189. m_log.Error("The currently loaded modules in " + RegionInfo.RegionName + " are:");
  3190. foreach (IRegionModule module in Modules.Values)
  3191. {
  3192. if (!module.IsSharedModule)
  3193. {
  3194. m_log.Error("Region Module: " + module.Name);
  3195. }
  3196. }
  3197. break;
  3198. }
  3199. }
  3200. #endregion
  3201. #region Script Handling Methods
  3202. /// <summary>
  3203. /// Console command handler to send script command to script engine.
  3204. /// </summary>
  3205. /// <param name="args"></param>
  3206. public void SendCommandToPlugins(string[] args)
  3207. {
  3208. m_eventManager.TriggerOnPluginConsole(args);
  3209. }
  3210. public double GetLandHeight(int x, int y)
  3211. {
  3212. return Heightmap[x, y];
  3213. }
  3214. public UUID GetLandOwner(float x, float y)
  3215. {
  3216. ILandObject land = LandChannel.GetLandObject(x, y);
  3217. if (land == null)
  3218. {
  3219. return UUID.Zero;
  3220. }
  3221. else
  3222. {
  3223. return land.landData.OwnerID;
  3224. }
  3225. }
  3226. public LandData GetLandData(float x, float y)
  3227. {
  3228. return LandChannel.GetLandObject(x, y).landData;
  3229. }
  3230. public LandData GetLandData(uint x, uint y)
  3231. {
  3232. m_log.DebugFormat("[SCENE] returning land for {0},{1}", x, y);
  3233. return LandChannel.GetLandObject((int)x, (int)y).landData;
  3234. }
  3235. public void SetLandMusicURL(float x, float y, string url)
  3236. {
  3237. ILandObject land = LandChannel.GetLandObject(x, y);
  3238. if (land == null)
  3239. {
  3240. return;
  3241. }
  3242. else
  3243. {
  3244. land.landData.MusicURL = url;
  3245. land.sendLandUpdateToAvatarsOverMe();
  3246. return;
  3247. }
  3248. }
  3249. public void SetLandMediaURL(float x, float y, string url)
  3250. {
  3251. ILandObject land = LandChannel.GetLandObject(x, y);
  3252. if (land == null)
  3253. {
  3254. return;
  3255. }
  3256. else
  3257. {
  3258. land.landData.MediaURL = url;
  3259. land.sendLandUpdateToAvatarsOverMe();
  3260. return;
  3261. }
  3262. }
  3263. public RegionInfo RequestClosestRegion(string name)
  3264. {
  3265. return m_sceneGridService.RequestClosestRegion(name);
  3266. }
  3267. #endregion
  3268. #region Script Engine
  3269. private List<ScriptEngineInterface> ScriptEngines = new List<ScriptEngineInterface>();
  3270. private bool m_dumpAssetsToFile;
  3271. /// <summary>
  3272. ///
  3273. /// </summary>
  3274. /// <param name="scriptEngine"></param>
  3275. public void AddScriptEngine(ScriptEngineInterface scriptEngine)
  3276. {
  3277. ScriptEngines.Add(scriptEngine);
  3278. scriptEngine.InitializeEngine(this);
  3279. }
  3280. public void TriggerObjectChanged(uint localID, uint change)
  3281. {
  3282. m_eventManager.TriggerOnScriptChangedEvent(localID, change);
  3283. }
  3284. public void TriggerAtTargetEvent(uint localID, uint handle, Vector3 targetpos, Vector3 currentpos)
  3285. {
  3286. m_eventManager.TriggerAtTargetEvent(localID, handle, targetpos, currentpos);
  3287. }
  3288. public void TriggerNotAtTargetEvent(uint localID)
  3289. {
  3290. m_eventManager.TriggerNotAtTargetEvent(localID);
  3291. }
  3292. private bool ScriptDanger(SceneObjectPart part,Vector3 pos)
  3293. {
  3294. ILandObject parcel = LandChannel.GetLandObject(pos.X, pos.Y);
  3295. if (part != null)
  3296. {
  3297. if (parcel != null)
  3298. {
  3299. if ((parcel.landData.Flags & (uint)Parcel.ParcelFlags.AllowOtherScripts) != 0)
  3300. {
  3301. return true;
  3302. }
  3303. else if ((parcel.landData.Flags & (uint)Parcel.ParcelFlags.AllowGroupScripts) != 0)
  3304. {
  3305. if (part.OwnerID == parcel.landData.OwnerID || (parcel.landData.IsGroupOwned && part.GroupID == parcel.landData.GroupID) || ExternalChecks.ExternalChecksCanBeGodLike(part.OwnerID))
  3306. {
  3307. return true;
  3308. }
  3309. else
  3310. {
  3311. return false;
  3312. }
  3313. }
  3314. else
  3315. {
  3316. if (part.OwnerID == parcel.landData.OwnerID)
  3317. {
  3318. return true;
  3319. }
  3320. else
  3321. {
  3322. return false;
  3323. }
  3324. }
  3325. }
  3326. else
  3327. {
  3328. if (pos.X > 0f && pos.X < Constants.RegionSize && pos.Y > 0f && pos.Y < Constants.RegionSize)
  3329. {
  3330. // The only time parcel != null when an object is inside a region is when
  3331. // there is nothing behind the landchannel. IE, no land plugin loaded.
  3332. return true;
  3333. }
  3334. else
  3335. {
  3336. // The object is outside of this region. Stop piping events to it.
  3337. return false;
  3338. }
  3339. }
  3340. }
  3341. else
  3342. {
  3343. return false;
  3344. }
  3345. }
  3346. public bool ScriptDanger(uint localID, Vector3 pos)
  3347. {
  3348. SceneObjectPart part = GetSceneObjectPart(localID);
  3349. if (part != null)
  3350. {
  3351. return ScriptDanger(part, pos);
  3352. }
  3353. else
  3354. {
  3355. return false;
  3356. }
  3357. }
  3358. public bool PipeEventsForScript(uint localID)
  3359. {
  3360. SceneObjectPart part = GetSceneObjectPart(localID);
  3361. if (part != null)
  3362. {
  3363. // Changed so that child prims of attachments return ScriptDanger for their parent, so that
  3364. // their scripts will actually run.
  3365. // -- Leaf, Tue Aug 12 14:17:05 EDT 2008
  3366. SceneObjectPart parent = part.ParentGroup.RootPart;
  3367. if (parent != null && parent.IsAttachment)
  3368. return ScriptDanger(parent, parent.GetWorldPosition());
  3369. else
  3370. return ScriptDanger(part, part.GetWorldPosition());
  3371. }
  3372. else
  3373. {
  3374. return false;
  3375. }
  3376. }
  3377. #endregion
  3378. #region InnerScene wrapper methods
  3379. /// <summary>
  3380. ///
  3381. /// </summary>
  3382. /// <param name="localID"></param>
  3383. /// <returns></returns>
  3384. public UUID ConvertLocalIDToFullID(uint localID)
  3385. {
  3386. return m_innerScene.ConvertLocalIDToFullID(localID);
  3387. }
  3388. public void SwapRootAgentCount(bool rootChildChildRootTF)
  3389. {
  3390. m_innerScene.SwapRootChildAgent(rootChildChildRootTF);
  3391. }
  3392. public void AddPhysicalPrim(int num)
  3393. {
  3394. m_innerScene.AddPhysicalPrim(num);
  3395. }
  3396. public void RemovePhysicalPrim(int num)
  3397. {
  3398. m_innerScene.RemovePhysicalPrim(num);
  3399. }
  3400. //The idea is to have a group of method that return a list of avatars meeting some requirement
  3401. // ie it could be all m_scenePresences within a certain range of the calling prim/avatar.
  3402. /// <summary>
  3403. /// Return a list of all avatars in this region.
  3404. /// This list is a new object, so it can be iterated over without locking.
  3405. /// </summary>
  3406. /// <returns></returns>
  3407. public List<ScenePresence> GetAvatars()
  3408. {
  3409. return m_innerScene.GetAvatars();
  3410. }
  3411. /// <summary>
  3412. /// Return a list of all ScenePresences in this region. This returns child agents as well as root agents.
  3413. /// This list is a new object, so it can be iterated over without locking.
  3414. /// </summary>
  3415. /// <returns></returns>
  3416. public List<ScenePresence> GetScenePresences()
  3417. {
  3418. return m_innerScene.GetScenePresences();
  3419. }
  3420. /// <summary>
  3421. /// Request a filtered list of ScenePresences in this region.
  3422. /// This list is a new object, so it can be iterated over without locking.
  3423. /// </summary>
  3424. /// <param name="filter"></param>
  3425. /// <returns></returns>
  3426. public List<ScenePresence> GetScenePresences(FilterAvatarList filter)
  3427. {
  3428. return m_innerScene.GetScenePresences(filter);
  3429. }
  3430. /// <summary>
  3431. /// Request a scene presence by UUID
  3432. /// </summary>
  3433. /// <param name="avatarID"></param>
  3434. /// <returns></returns>
  3435. public ScenePresence GetScenePresence(UUID avatarID)
  3436. {
  3437. return m_innerScene.GetScenePresence(avatarID);
  3438. }
  3439. /// <summary>
  3440. /// Request an Avatar's Child Status - used by ClientView when a 'kick everyone' or 'estate message' occurs
  3441. /// </summary>
  3442. /// <param name="avatarID">AvatarID to lookup</param>
  3443. /// <returns></returns>
  3444. public override bool PresenceChildStatus(UUID avatarID)
  3445. {
  3446. ScenePresence cp = GetScenePresence(avatarID);
  3447. return cp.IsChildAgent;
  3448. }
  3449. /// <summary>
  3450. ///
  3451. /// </summary>
  3452. /// <param name="action"></param>
  3453. public void ForEachScenePresence(Action<ScenePresence> action)
  3454. {
  3455. // We don't want to try to send messages if there are no avatars.
  3456. if (m_scenePresences != null)
  3457. {
  3458. try
  3459. {
  3460. List<ScenePresence> presenceList = GetScenePresences();
  3461. foreach (ScenePresence presence in presenceList)
  3462. {
  3463. action(presence);
  3464. }
  3465. }
  3466. catch (Exception e)
  3467. {
  3468. m_log.Info("[BUG]: " + e.ToString());
  3469. }
  3470. }
  3471. }
  3472. /// <summary>
  3473. ///
  3474. /// </summary>
  3475. /// <param name="action"></param>
  3476. // public void ForEachObject(Action<SceneObjectGroup> action)
  3477. // {
  3478. // List<SceneObjectGroup> presenceList;
  3479. //
  3480. // lock (m_sceneObjects)
  3481. // {
  3482. // presenceList = new List<SceneObjectGroup>(m_sceneObjects.Values);
  3483. // }
  3484. //
  3485. // foreach (SceneObjectGroup presence in presenceList)
  3486. // {
  3487. // action(presence);
  3488. // }
  3489. // }
  3490. /// <summary>
  3491. /// Get a named prim contained in this scene (will return the first
  3492. /// found, if there are more than one prim with the same name)
  3493. /// </summary>
  3494. /// <param name="name"></param>
  3495. /// <returns></returns>
  3496. public SceneObjectPart GetSceneObjectPart(string name)
  3497. {
  3498. return m_innerScene.GetSceneObjectPart(name);
  3499. }
  3500. /// <summary>
  3501. /// Get a prim via its local id
  3502. /// </summary>
  3503. /// <param name="localID"></param>
  3504. /// <returns></returns>
  3505. public SceneObjectPart GetSceneObjectPart(uint localID)
  3506. {
  3507. return m_innerScene.GetSceneObjectPart(localID);
  3508. }
  3509. /// <summary>
  3510. /// Get a prim via its UUID
  3511. /// </summary>
  3512. /// <param name="fullID"></param>
  3513. /// <returns></returns>
  3514. public SceneObjectPart GetSceneObjectPart(UUID fullID)
  3515. {
  3516. return m_innerScene.GetSceneObjectPart(fullID);
  3517. }
  3518. internal bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
  3519. {
  3520. return m_innerScene.TryGetAvatar(avatarId, out avatar);
  3521. }
  3522. internal bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  3523. {
  3524. return m_innerScene.TryGetAvatarByName(avatarName, out avatar);
  3525. }
  3526. internal void ForEachClient(Action<IClientAPI> action)
  3527. {
  3528. m_innerScene.ForEachClient(action);
  3529. }
  3530. /// <summary>
  3531. /// Returns a list of the entities in the scene. This is a new list so operations perform on the list itself
  3532. /// will not affect the original list of objects in the scene.
  3533. /// </summary>
  3534. /// <returns></returns>
  3535. public List<EntityBase> GetEntities()
  3536. {
  3537. return m_innerScene.GetEntities();
  3538. }
  3539. #endregion
  3540. #region BaseHTTPServer wrapper methods
  3541. public bool AddHTTPHandler(string method, GenericHTTPMethod handler)
  3542. {
  3543. return m_httpListener.AddHTTPHandler(method, handler);
  3544. }
  3545. public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
  3546. {
  3547. return m_httpListener.AddXmlRPCHandler(method, handler);
  3548. }
  3549. public void AddStreamHandler(IRequestHandler handler)
  3550. {
  3551. m_httpListener.AddStreamHandler(handler);
  3552. }
  3553. public bool AddLLSDHandler(string path, LLSDMethod handler)
  3554. {
  3555. return m_httpListener.AddLLSDHandler(path, handler);
  3556. }
  3557. public void RemoveStreamHandler(string httpMethod, string path)
  3558. {
  3559. m_httpListener.RemoveStreamHandler(httpMethod, path);
  3560. }
  3561. public void RemoveHTTPHandler(string httpMethod, string path)
  3562. {
  3563. m_httpListener.RemoveHTTPHandler(httpMethod, path);
  3564. }
  3565. public bool RemoveLLSDHandler(string path, LLSDMethod handler)
  3566. {
  3567. return m_httpListener.RemoveLLSDHandler(path, handler);
  3568. }
  3569. #endregion
  3570. #region Avatar Appearance Default
  3571. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  3572. {
  3573. visualParams = GetDefaultVisualParams();
  3574. wearables = AvatarWearable.DefaultWearables;
  3575. }
  3576. private static byte[] GetDefaultVisualParams()
  3577. {
  3578. byte[] visualParams;
  3579. visualParams = new byte[218];
  3580. for (int i = 0; i < 218; i++)
  3581. {
  3582. visualParams[i] = 100;
  3583. }
  3584. return visualParams;
  3585. }
  3586. #endregion
  3587. public void ParcelMediaSetTime(float time)
  3588. {
  3589. //should be doing this by parcel, but as its only for testing
  3590. // The use of Thread.Sleep here causes the following compiler error under mono 1.2.4
  3591. // OpenSim/Region/Environment/Scenes/Scene.cs(3675,17): error CS0103: The name `Thread' does not exist
  3592. // in the context of `<>c__CompilerGenerated17'
  3593. // MW said it was okay to comment the body of this method out for now since the code is experimental
  3594. // and will be replaced anyway
  3595. // ForEachClient(delegate(IClientAPI client)
  3596. // {
  3597. // client.SendParcelMediaCommand((uint)(2), ParcelMediaCommandEnum.Pause, 0);
  3598. // Thread.Sleep(10);
  3599. // client.SendParcelMediaCommand((uint)(64), ParcelMediaCommandEnum.Time, time);
  3600. // Thread.Sleep(200);
  3601. // client.SendParcelMediaCommand((uint)(4), ParcelMediaCommandEnum.Play, 0);
  3602. // });
  3603. }
  3604. public void RegionHandleRequest(IClientAPI client, UUID regionID)
  3605. {
  3606. RegionInfo info;
  3607. if (regionID == RegionInfo.RegionID)
  3608. info = RegionInfo;
  3609. else
  3610. info = CommsManager.GridService.RequestNeighbourInfo(regionID);
  3611. if (info != null)
  3612. client.SendRegionHandle(regionID, info.RegionHandle);
  3613. }
  3614. public void TerrainUnAcked(IClientAPI client, int patchX, int patchY)
  3615. {
  3616. //Console.WriteLine("Terrain packet unacked, resending patch: " + patchX + " , " + patchY);
  3617. client.SendLayerData(patchX, patchY, Heightmap.GetFloatsSerialised());
  3618. }
  3619. public void SetRootAgentScene(UUID agentID)
  3620. {
  3621. IInventoryTransferModule inv = RequestModuleInterface<IInventoryTransferModule>();
  3622. if (inv == null)
  3623. return;
  3624. inv.SetRootAgentScene(agentID, this);
  3625. }
  3626. public bool NeedSceneCacheClear(UUID agentID)
  3627. {
  3628. IInventoryTransferModule inv = RequestModuleInterface<IInventoryTransferModule>();
  3629. if (inv == null)
  3630. return true;
  3631. return inv.NeedSceneCacheClear(agentID, this);
  3632. }
  3633. public void ObjectSaleInfo(IClientAPI client, UUID agentID, UUID sessionID, uint localID, byte saleType, int salePrice)
  3634. {
  3635. SceneObjectPart part = GetSceneObjectPart(localID);
  3636. if (part == null || part.ParentGroup == null)
  3637. return;
  3638. if (part.ParentGroup.RootPart == null)
  3639. return;
  3640. part = part.ParentGroup.RootPart;
  3641. part.ObjectSaleType = saleType;
  3642. part.SalePrice = salePrice;
  3643. part.ParentGroup.HasGroupChanged = true;
  3644. part.GetProperties(client);
  3645. }
  3646. public bool PerformObjectBuy(IClientAPI remoteClient, UUID categoryID,
  3647. uint localID, byte saleType)
  3648. {
  3649. SceneObjectPart part = GetSceneObjectPart(localID);
  3650. if (part == null)
  3651. return false;
  3652. if (part.ParentGroup == null)
  3653. return false;
  3654. SceneObjectGroup group = part.ParentGroup;
  3655. switch (saleType)
  3656. {
  3657. case 1: // Sell as original (in-place sale)
  3658. uint effectivePerms=group.GetEffectivePermissions();
  3659. if ((effectivePerms & (uint)PermissionMask.Transfer) == 0)
  3660. {
  3661. remoteClient.SendAgentAlertMessage("This item doesn't appear to be for sale", false);
  3662. return false;
  3663. }
  3664. group.SetOwnerId(remoteClient.AgentId);
  3665. group.SetRootPartOwner(part, remoteClient.AgentId,
  3666. remoteClient.ActiveGroupId);
  3667. List<SceneObjectPart> partList =
  3668. new List<SceneObjectPart>(group.Children.Values);
  3669. if (ExternalChecks.ExternalChecksPropagatePermissions())
  3670. {
  3671. foreach (SceneObjectPart child in partList)
  3672. {
  3673. child.ChangeInventoryOwner(remoteClient.AgentId);
  3674. child.ApplyNextOwnerPermissions();
  3675. }
  3676. }
  3677. part.ObjectSaleType = 0;
  3678. part.SalePrice = 10;
  3679. group.HasGroupChanged = true;
  3680. part.GetProperties(remoteClient);
  3681. part.ScheduleFullUpdate();
  3682. break;
  3683. case 2: // Sell a copy
  3684. string sceneObjectXml = group.ToXmlString();
  3685. CachedUserInfo userInfo =
  3686. CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);
  3687. if (userInfo != null)
  3688. {
  3689. uint perms=group.GetEffectivePermissions();
  3690. if ((perms & (uint)PermissionMask.Transfer) == 0)
  3691. {
  3692. remoteClient.SendAgentAlertMessage("This item doesn't appear to be for sale", false);
  3693. return false;
  3694. }
  3695. AssetBase asset = CreateAsset(
  3696. group.GetPartName(localID),
  3697. group.GetPartDescription(localID),
  3698. (sbyte)AssetType.Object,
  3699. Utils.StringToBytes(sceneObjectXml));
  3700. AssetCache.AddAsset(asset);
  3701. InventoryItemBase item = new InventoryItemBase();
  3702. item.Creator = part.CreatorID;
  3703. item.ID = UUID.Random();
  3704. item.Owner = remoteClient.AgentId;
  3705. item.AssetID = asset.FullID;
  3706. item.Description = asset.Description;
  3707. item.Name = asset.Name;
  3708. item.AssetType = asset.Type;
  3709. item.InvType = (int)InventoryType.Object;
  3710. item.Folder = categoryID;
  3711. uint nextPerms=(perms & 7) << 13;
  3712. if ((nextPerms & (uint)PermissionMask.Copy) == 0)
  3713. perms &= ~(uint)PermissionMask.Copy;
  3714. if ((nextPerms & (uint)PermissionMask.Transfer) == 0)
  3715. perms &= ~(uint)PermissionMask.Transfer;
  3716. if ((nextPerms & (uint)PermissionMask.Modify) == 0)
  3717. perms &= ~(uint)PermissionMask.Modify;
  3718. item.BasePermissions = perms & part.NextOwnerMask;
  3719. item.CurrentPermissions = perms & part.NextOwnerMask;
  3720. item.NextPermissions = part.NextOwnerMask;
  3721. item.EveryOnePermissions = part.EveryoneMask &
  3722. part.NextOwnerMask;
  3723. item.CurrentPermissions |= 8; // Slam!
  3724. item.CreationDate = Util.UnixTimeSinceEpoch();
  3725. userInfo.AddItem(item);
  3726. remoteClient.SendInventoryItemCreateUpdate(item);
  3727. }
  3728. else
  3729. {
  3730. remoteClient.SendAgentAlertMessage("Cannot buy now. Your inventory is unavailable", false);
  3731. return false;
  3732. }
  3733. break;
  3734. case 3: // Sell contents
  3735. List<UUID> invList = part.GetInventoryList();
  3736. bool okToSell = true;
  3737. foreach (UUID invID in invList)
  3738. {
  3739. TaskInventoryItem item = part.GetInventoryItem(invID);
  3740. if ((item.CurrentPermissions &
  3741. (uint)PermissionMask.Transfer) == 0)
  3742. {
  3743. okToSell = false;
  3744. break;
  3745. }
  3746. }
  3747. if (!okToSell)
  3748. {
  3749. remoteClient.SendAgentAlertMessage("This item's inventory doesn't appear to be for sale", false);
  3750. return false;
  3751. }
  3752. if (invList.Count > 0)
  3753. MoveTaskInventoryItems(remoteClient.AgentId, part.Name,
  3754. part, invList);
  3755. break;
  3756. }
  3757. return true;
  3758. }
  3759. public void CleanTempObjects()
  3760. {
  3761. List<EntityBase> objs = GetEntities();
  3762. foreach (EntityBase obj in objs)
  3763. {
  3764. if (obj is SceneObjectGroup)
  3765. {
  3766. SceneObjectGroup grp = (SceneObjectGroup)obj;
  3767. if (grp.RootPart != null)
  3768. {
  3769. if ((grp.RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  3770. {
  3771. if (grp.RootPart.Expires <= DateTime.Now)
  3772. DeleteSceneObject(grp, false);
  3773. }
  3774. }
  3775. }
  3776. }
  3777. }
  3778. public void DeleteFromStorage(UUID uuid)
  3779. {
  3780. m_storageManager.DataStore.RemoveObject(uuid, m_regInfo.RegionID);
  3781. }
  3782. public int GetHealth()
  3783. {
  3784. int health=1; // Start at 1, means we're up
  3785. // A login in the last 4 mins? We can't be doing too badly
  3786. //
  3787. if ((System.Environment.TickCount - m_LastLogin) < 240000)
  3788. health++;
  3789. return 0;
  3790. }
  3791. }
  3792. }