Scene.cs 203 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972
  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 copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Timers;
  35. using System.Xml;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Packets;
  39. using OpenMetaverse.Imaging;
  40. using OpenSim.Framework;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Framework.Communications;
  43. using OpenSim.Framework.Communications.Cache;
  44. using OpenSim.Framework.Communications.Clients;
  45. using OpenSim.Framework.Console;
  46. using OpenSim.Region.Framework.Interfaces;
  47. using OpenSim.Region.Framework.Scenes.Scripting;
  48. using OpenSim.Region.Framework.Scenes.Serialization;
  49. using OpenSim.Region.Physics.Manager;
  50. using Timer=System.Timers.Timer;
  51. using TPFlags = OpenSim.Framework.Constants.TeleportFlags;
  52. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  53. namespace OpenSim.Region.Framework.Scenes
  54. {
  55. public delegate bool FilterAvatarList(ScenePresence avatar);
  56. public partial class Scene : SceneBase
  57. {
  58. public enum UpdatePrioritizationSchemes {
  59. Time = 0,
  60. Distance = 1,
  61. SimpleAngularDistance = 2,
  62. FrontBack = 3,
  63. }
  64. public delegate void SynchronizeSceneHandler(Scene scene);
  65. public SynchronizeSceneHandler SynchronizeScene = null;
  66. /* Used by the loadbalancer plugin on GForge */
  67. protected int m_splitRegionID = 0;
  68. public int SplitRegionID
  69. {
  70. get { return m_splitRegionID; }
  71. set { m_splitRegionID = value; }
  72. }
  73. private const long DEFAULT_MIN_TIME_FOR_PERSISTENCE = 60L;
  74. private const long DEFAULT_MAX_TIME_FOR_PERSISTENCE = 600L;
  75. #region Fields
  76. protected Timer m_restartWaitTimer = new Timer();
  77. public SimStatsReporter StatsReporter;
  78. protected List<RegionInfo> m_regionRestartNotifyList = new List<RegionInfo>();
  79. protected List<RegionInfo> m_neighbours = new List<RegionInfo>();
  80. private volatile int m_bordersLocked = 0;
  81. public bool BordersLocked
  82. {
  83. get { return m_bordersLocked == 1; }
  84. set
  85. {
  86. if (value == true)
  87. m_bordersLocked = 1;
  88. else
  89. m_bordersLocked = 0;
  90. }
  91. }
  92. public List<Border> NorthBorders = new List<Border>();
  93. public List<Border> EastBorders = new List<Border>();
  94. public List<Border> SouthBorders = new List<Border>();
  95. public List<Border> WestBorders = new List<Border>();
  96. /// <value>
  97. /// The scene graph for this scene
  98. /// </value>
  99. /// TODO: Possibly stop other classes being able to manipulate this directly.
  100. private SceneGraph m_sceneGraph;
  101. /// <summary>
  102. /// Are we applying physics to any of the prims in this scene?
  103. /// </summary>
  104. public bool m_physicalPrim;
  105. public float m_maxNonphys = 256;
  106. public float m_maxPhys = 10;
  107. public bool m_clampPrimSize;
  108. public bool m_trustBinaries;
  109. public bool m_allowScriptCrossings;
  110. public bool m_useFlySlow;
  111. public bool m_usePreJump;
  112. public bool m_seeIntoRegionFromNeighbor;
  113. // TODO: need to figure out how allow client agents but deny
  114. // root agents when ACL denies access to root agent
  115. public bool m_strictAccessControl = true;
  116. public int MaxUndoCount = 5;
  117. private int m_RestartTimerCounter;
  118. private readonly Timer m_restartTimer = new Timer(15000); // Wait before firing
  119. private int m_incrementsof15seconds;
  120. private volatile bool m_backingup;
  121. private bool m_useAsyncWhenPossible;
  122. private Dictionary<UUID, ReturnInfo> m_returns = new Dictionary<UUID, ReturnInfo>();
  123. private Dictionary<UUID, SceneObjectGroup> m_groupsWithTargets = new Dictionary<UUID, SceneObjectGroup>();
  124. protected string m_simulatorVersion = "OpenSimulator Server";
  125. protected ModuleLoader m_moduleLoader;
  126. protected StorageManager m_storageManager;
  127. protected AgentCircuitManager m_authenticateHandler;
  128. public CommunicationsManager CommsManager;
  129. protected SceneCommunicationService m_sceneGridService;
  130. public bool loginsdisabled = true;
  131. public new float TimeDilation
  132. {
  133. get { return m_sceneGraph.PhysicsScene.TimeDilation; }
  134. }
  135. public SceneCommunicationService SceneGridService
  136. {
  137. get { return m_sceneGridService; }
  138. }
  139. public IXfer XferManager;
  140. protected IAssetService m_AssetService;
  141. protected IAuthorizationService m_AuthorizationService;
  142. private Object m_heartbeatLock = new Object();
  143. public IAssetService AssetService
  144. {
  145. get
  146. {
  147. if (m_AssetService == null)
  148. {
  149. m_AssetService = RequestModuleInterface<IAssetService>();
  150. if (m_AssetService == null)
  151. {
  152. throw new Exception("No IAssetService available.");
  153. }
  154. }
  155. return m_AssetService;
  156. }
  157. }
  158. public IAuthorizationService AuthorizationService
  159. {
  160. get
  161. {
  162. if (m_AuthorizationService == null)
  163. {
  164. m_AuthorizationService = RequestModuleInterface<IAuthorizationService>();
  165. if (m_AuthorizationService == null)
  166. {
  167. // don't throw an exception if no authorization service is set for the time being
  168. m_log.InfoFormat("[SCENE]: No Authorization service is configured");
  169. }
  170. }
  171. return m_AuthorizationService;
  172. }
  173. }
  174. protected IInventoryService m_InventoryService;
  175. public IInventoryService InventoryService
  176. {
  177. get
  178. {
  179. if (m_InventoryService == null)
  180. {
  181. m_InventoryService = RequestModuleInterface<IInventoryService>();
  182. if (m_InventoryService == null)
  183. {
  184. throw new Exception("No IInventoryService available. This could happen if the config_include folder doesn't exist or if the OpenSim.ini [Architecture] section isn't set. Please also check that you have the correct version of your inventory service dll. Sometimes old versions of this dll will still exist. Do a clean checkout and re-create the opensim.ini from the opensim.ini.example.");
  185. }
  186. }
  187. return m_InventoryService;
  188. }
  189. }
  190. protected IGridService m_GridService;
  191. public IGridService GridService
  192. {
  193. get
  194. {
  195. if (m_GridService == null)
  196. {
  197. m_GridService = RequestModuleInterface<IGridService>();
  198. if (m_GridService == null)
  199. {
  200. throw new Exception("No IGridService available. This could happen if the config_include folder doesn't exist or if the OpenSim.ini [Architecture] section isn't set. Please also check that you have the correct version of your inventory service dll. Sometimes old versions of this dll will still exist. Do a clean checkout and re-create the opensim.ini from the opensim.ini.example.");
  201. }
  202. }
  203. return m_GridService;
  204. }
  205. }
  206. protected IXMLRPC m_xmlrpcModule;
  207. protected IWorldComm m_worldCommModule;
  208. protected IAvatarFactory m_AvatarFactory;
  209. public IAvatarFactory AvatarFactory
  210. {
  211. get { return m_AvatarFactory; }
  212. }
  213. protected IConfigSource m_config;
  214. protected IRegionSerialiserModule m_serialiser;
  215. protected IInterregionCommsOut m_interregionCommsOut;
  216. protected IInterregionCommsIn m_interregionCommsIn;
  217. protected IDialogModule m_dialogModule;
  218. protected ITeleportModule m_teleportModule;
  219. protected ICapabilitiesModule m_capsModule;
  220. public ICapabilitiesModule CapsModule
  221. {
  222. get { return m_capsModule; }
  223. }
  224. protected override IConfigSource GetConfig()
  225. {
  226. return m_config;
  227. }
  228. // Central Update Loop
  229. protected int m_fps = 10;
  230. protected uint m_frame;
  231. protected float m_timespan = 0.089f;
  232. protected DateTime m_lastupdate = DateTime.UtcNow;
  233. private int m_update_physics = 1;
  234. private int m_update_entitymovement = 1;
  235. private int m_update_objects = 1; // Update objects which have scheduled themselves for updates
  236. private int m_update_presences = 1; // Update scene presence movements
  237. private int m_update_events = 1;
  238. private int m_update_backup = 200;
  239. private int m_update_terrain = 50;
  240. private int m_update_land = 1;
  241. private int frameMS;
  242. private int physicsMS2;
  243. private int physicsMS;
  244. private int otherMS;
  245. private int tempOnRezMS;
  246. private int eventMS;
  247. private int backupMS;
  248. private int terrainMS;
  249. private int landMS;
  250. private int lastCompletedFrame;
  251. public int MonitorFrameTime { get { return frameMS; } }
  252. public int MonitorPhysicsUpdateTime { get { return physicsMS; } }
  253. public int MonitorPhysicsSyncTime { get { return physicsMS2; } }
  254. public int MonitorOtherTime { get { return otherMS; } }
  255. public int MonitorTempOnRezTime { get { return tempOnRezMS; } }
  256. public int MonitorEventTime { get { return eventMS; } } // This may need to be divided into each event?
  257. public int MonitorBackupTime { get { return backupMS; } }
  258. public int MonitorTerrainTime { get { return terrainMS; } }
  259. public int MonitorLandTime { get { return landMS; } }
  260. public int MonitorLastFrameTick { get { return lastCompletedFrame; } }
  261. private bool m_physics_enabled = true;
  262. private bool m_scripts_enabled = true;
  263. private string m_defaultScriptEngine;
  264. private int m_LastLogin;
  265. private Thread HeartbeatThread;
  266. private volatile bool shuttingdown;
  267. private int m_lastUpdate = Environment.TickCount;
  268. private bool m_firstHeartbeat = true;
  269. private UpdatePrioritizationSchemes m_update_prioritization_scheme = UpdatePrioritizationSchemes.Time;
  270. private bool m_reprioritization_enabled = true;
  271. private double m_reprioritization_interval = 5000.0;
  272. private double m_root_reprioritization_distance = 10.0;
  273. private double m_child_reprioritization_distance = 20.0;
  274. private object m_deleting_scene_object = new object();
  275. // the minimum time that must elapse before a changed object will be considered for persisted
  276. public long m_dontPersistBefore = DEFAULT_MIN_TIME_FOR_PERSISTENCE * 10000000L;
  277. // the maximum time that must elapse before a changed object will be considered for persisted
  278. public long m_persistAfter = DEFAULT_MAX_TIME_FOR_PERSISTENCE * 10000000L;
  279. #endregion
  280. #region Properties
  281. public UpdatePrioritizationSchemes UpdatePrioritizationScheme { get { return this.m_update_prioritization_scheme; } }
  282. public bool IsReprioritizationEnabled { get { return m_reprioritization_enabled; } }
  283. public double ReprioritizationInterval { get { return m_reprioritization_interval; } }
  284. public double RootReprioritizationDistance { get { return m_root_reprioritization_distance; } }
  285. public double ChildReprioritizationDistance { get { return m_child_reprioritization_distance; } }
  286. public AgentCircuitManager AuthenticateHandler
  287. {
  288. get { return m_authenticateHandler; }
  289. }
  290. public SceneGraph SceneContents
  291. {
  292. get { return m_sceneGraph; }
  293. }
  294. // an instance to the physics plugin's Scene object.
  295. public PhysicsScene PhysicsScene
  296. {
  297. get { return m_sceneGraph.PhysicsScene; }
  298. set
  299. {
  300. // If we're not doing the initial set
  301. // Then we've got to remove the previous
  302. // event handler
  303. if (PhysicsScene != null && PhysicsScene.SupportsNINJAJoints)
  304. {
  305. PhysicsScene.OnJointMoved -= jointMoved;
  306. PhysicsScene.OnJointDeactivated -= jointDeactivated;
  307. PhysicsScene.OnJointErrorMessage -= jointErrorMessage;
  308. }
  309. m_sceneGraph.PhysicsScene = value;
  310. if (PhysicsScene != null && m_sceneGraph.PhysicsScene.SupportsNINJAJoints)
  311. {
  312. // register event handlers to respond to joint movement/deactivation
  313. PhysicsScene.OnJointMoved += jointMoved;
  314. PhysicsScene.OnJointDeactivated += jointDeactivated;
  315. PhysicsScene.OnJointErrorMessage += jointErrorMessage;
  316. }
  317. }
  318. }
  319. // This gets locked so things stay thread safe.
  320. public object SyncRoot
  321. {
  322. get { return m_sceneGraph.m_syncRoot; }
  323. }
  324. /// <summary>
  325. /// This is for llGetRegionFPS
  326. /// </summary>
  327. public float SimulatorFPS
  328. {
  329. get { return StatsReporter.getLastReportedSimFPS(); }
  330. }
  331. public string DefaultScriptEngine
  332. {
  333. get { return m_defaultScriptEngine; }
  334. }
  335. public EntityManager Entities
  336. {
  337. get { return m_sceneGraph.Entities; }
  338. }
  339. public Dictionary<UUID, ScenePresence> m_restorePresences
  340. {
  341. get { return m_sceneGraph.RestorePresences; }
  342. set { m_sceneGraph.RestorePresences = value; }
  343. }
  344. public int objectCapacity = 45000;
  345. #endregion
  346. #region BinaryStats
  347. public class StatLogger
  348. {
  349. public DateTime StartTime;
  350. public string Path;
  351. public System.IO.BinaryWriter Log;
  352. }
  353. static StatLogger m_statLog = null;
  354. static TimeSpan m_statLogPeriod = TimeSpan.FromSeconds(300);
  355. static string m_statsDir = String.Empty;
  356. static Object m_statLockObject = new Object();
  357. private void LogSimStats(SimStats stats)
  358. {
  359. SimStatsPacket pack = new SimStatsPacket();
  360. pack.Region = new SimStatsPacket.RegionBlock();
  361. pack.Region.RegionX = stats.RegionX;
  362. pack.Region.RegionY = stats.RegionY;
  363. pack.Region.RegionFlags = stats.RegionFlags;
  364. pack.Region.ObjectCapacity = stats.ObjectCapacity;
  365. //pack.Region = //stats.RegionBlock;
  366. pack.Stat = stats.StatsBlock;
  367. pack.Header.Reliable = false;
  368. // note that we are inside the reporter lock when called
  369. DateTime now = DateTime.Now;
  370. // hide some time information into the packet
  371. pack.Header.Sequence = (uint)now.Ticks;
  372. lock (m_statLockObject) // m_statLog is shared so make sure there is only executer here
  373. {
  374. try
  375. {
  376. if (m_statLog == null || now > m_statLog.StartTime + m_statLogPeriod)
  377. {
  378. // First log file or time has expired, start writing to a new log file
  379. if (m_statLog != null && m_statLog.Log != null)
  380. {
  381. m_statLog.Log.Close();
  382. }
  383. m_statLog = new StatLogger();
  384. m_statLog.StartTime = now;
  385. m_statLog.Path = (m_statsDir.Length > 0 ? m_statsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "")
  386. + String.Format("stats-{0}.log", now.ToString("yyyyMMddHHmmss"));
  387. m_statLog.Log = new BinaryWriter(File.Open(m_statLog.Path, FileMode.Append, FileAccess.Write));
  388. }
  389. // Write the serialized data to disk
  390. if (m_statLog != null && m_statLog.Log != null)
  391. m_statLog.Log.Write(pack.ToBytes());
  392. }
  393. catch (Exception ex)
  394. {
  395. m_log.Error("statistics gathering failed: " + ex.Message, ex);
  396. if (m_statLog != null && m_statLog.Log != null)
  397. {
  398. m_statLog.Log.Close();
  399. }
  400. m_statLog = null;
  401. }
  402. }
  403. return;
  404. }
  405. #endregion
  406. #region Constructors
  407. public Scene(RegionInfo regInfo, AgentCircuitManager authen,
  408. CommunicationsManager commsMan, SceneCommunicationService sceneGridService,
  409. StorageManager storeManager,
  410. ModuleLoader moduleLoader, bool dumpAssetsToFile, bool physicalPrim,
  411. bool SeeIntoRegionFromNeighbor, IConfigSource config, string simulatorVersion)
  412. {
  413. m_config = config;
  414. Random random = new Random();
  415. BordersLocked = true;
  416. Border northBorder = new Border();
  417. northBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, (int)Constants.RegionSize); //<---
  418. northBorder.CrossDirection = Cardinals.N;
  419. NorthBorders.Add(northBorder);
  420. Border southBorder = new Border();
  421. southBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, 0); //--->
  422. southBorder.CrossDirection = Cardinals.S;
  423. SouthBorders.Add(southBorder);
  424. Border eastBorder = new Border();
  425. eastBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, (int)Constants.RegionSize); //<---
  426. eastBorder.CrossDirection = Cardinals.E;
  427. EastBorders.Add(eastBorder);
  428. Border westBorder = new Border();
  429. westBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, 0); //--->
  430. westBorder.CrossDirection = Cardinals.W;
  431. WestBorders.Add(westBorder);
  432. BordersLocked = false;
  433. m_lastAllocatedLocalId = (uint)(random.NextDouble() * (double)(uint.MaxValue/2))+(uint)(uint.MaxValue/4);
  434. m_moduleLoader = moduleLoader;
  435. m_authenticateHandler = authen;
  436. CommsManager = commsMan;
  437. m_sceneGridService = sceneGridService;
  438. m_storageManager = storeManager;
  439. m_regInfo = regInfo;
  440. m_regionHandle = m_regInfo.RegionHandle;
  441. m_regionName = m_regInfo.RegionName;
  442. m_datastore = m_regInfo.DataStore;
  443. m_physicalPrim = physicalPrim;
  444. m_seeIntoRegionFromNeighbor = SeeIntoRegionFromNeighbor;
  445. m_eventManager = new EventManager();
  446. m_permissions = new ScenePermissions(this);
  447. m_asyncSceneObjectDeleter = new AsyncSceneObjectGroupDeleter(this);
  448. m_asyncSceneObjectDeleter.Enabled = true;
  449. // Load region settings
  450. m_regInfo.RegionSettings = m_storageManager.DataStore.LoadRegionSettings(m_regInfo.RegionID);
  451. if (m_storageManager.EstateDataStore != null)
  452. {
  453. m_regInfo.EstateSettings = m_storageManager.EstateDataStore.LoadEstateSettings(m_regInfo.RegionID);
  454. }
  455. //Bind Storage Manager functions to some land manager functions for this scene
  456. EventManager.OnLandObjectAdded +=
  457. new EventManager.LandObjectAdded(m_storageManager.DataStore.StoreLandObject);
  458. EventManager.OnLandObjectRemoved +=
  459. new EventManager.LandObjectRemoved(m_storageManager.DataStore.RemoveLandObject);
  460. m_sceneGraph = new SceneGraph(this, m_regInfo);
  461. // If the scene graph has an Unrecoverable error, restart this sim.
  462. // Currently the only thing that causes it to happen is two kinds of specific
  463. // Physics based crashes.
  464. //
  465. // Out of memory
  466. // Operating system has killed the plugin
  467. m_sceneGraph.UnRecoverableError += RestartNow;
  468. RegisterDefaultSceneEvents();
  469. DumpAssetsToFile = dumpAssetsToFile;
  470. m_scripts_enabled = !RegionInfo.RegionSettings.DisableScripts;
  471. m_physics_enabled = !RegionInfo.RegionSettings.DisablePhysics;
  472. StatsReporter = new SimStatsReporter(this);
  473. StatsReporter.OnSendStatsResult += SendSimStatsPackets;
  474. StatsReporter.OnStatsIncorrect += m_sceneGraph.RecalculateStats;
  475. StatsReporter.SetObjectCapacity(objectCapacity);
  476. m_simulatorVersion = simulatorVersion
  477. + " (OS " + Util.GetOperatingSystemInformation() + ")"
  478. + " ChilTasks:" + m_seeIntoRegionFromNeighbor.ToString()
  479. + " PhysPrim:" + m_physicalPrim.ToString();
  480. try
  481. {
  482. // Region config overrides global config
  483. //
  484. IConfig startupConfig = m_config.Configs["Startup"];
  485. // Should we try to run loops synchronously or asynchronously?
  486. m_useAsyncWhenPossible = startupConfig.GetBoolean("use_async_when_possible", false);
  487. //Animation states
  488. m_useFlySlow = startupConfig.GetBoolean("enableflyslow", false);
  489. // TODO: Change default to true once the feature is supported
  490. m_usePreJump = startupConfig.GetBoolean("enableprejump", false);
  491. m_maxNonphys = startupConfig.GetFloat("NonPhysicalPrimMax", m_maxNonphys);
  492. if (RegionInfo.NonphysPrimMax > 0)
  493. {
  494. m_maxNonphys = RegionInfo.NonphysPrimMax;
  495. }
  496. m_maxPhys = startupConfig.GetFloat("PhysicalPrimMax", m_maxPhys);
  497. if (RegionInfo.PhysPrimMax > 0)
  498. {
  499. m_maxPhys = RegionInfo.PhysPrimMax;
  500. }
  501. // Here, if clamping is requested in either global or
  502. // local config, it will be used
  503. //
  504. m_clampPrimSize = startupConfig.GetBoolean("ClampPrimSize", m_clampPrimSize);
  505. if (RegionInfo.ClampPrimSize)
  506. {
  507. m_clampPrimSize = true;
  508. }
  509. m_trustBinaries = startupConfig.GetBoolean("TrustBinaries", m_trustBinaries);
  510. m_allowScriptCrossings = startupConfig.GetBoolean("AllowScriptCrossing", m_allowScriptCrossings);
  511. m_dontPersistBefore =
  512. startupConfig.GetLong("MinimumTimeBeforePersistenceConsidered", DEFAULT_MIN_TIME_FOR_PERSISTENCE);
  513. m_dontPersistBefore *= 10000000;
  514. m_persistAfter =
  515. startupConfig.GetLong("MaximumTimeBeforePersistenceConsidered", DEFAULT_MAX_TIME_FOR_PERSISTENCE);
  516. m_persistAfter *= 10000000;
  517. m_defaultScriptEngine = startupConfig.GetString("DefaultScriptEngine", "DotNetEngine");
  518. IConfig packetConfig = m_config.Configs["PacketPool"];
  519. if (packetConfig != null)
  520. {
  521. PacketPool.Instance.RecyclePackets = packetConfig.GetBoolean("RecyclePackets", true);
  522. PacketPool.Instance.RecycleDataBlocks = packetConfig.GetBoolean("RecycleDataBlocks", true);
  523. }
  524. m_strictAccessControl = startupConfig.GetBoolean("StrictAccessControl", m_strictAccessControl);
  525. IConfig interest_management_config = m_config.Configs["InterestManagement"];
  526. if (interest_management_config != null)
  527. {
  528. string update_prioritization_scheme = interest_management_config.GetString("UpdatePrioritizationScheme", "Time").Trim().ToLower();
  529. switch (update_prioritization_scheme)
  530. {
  531. case "time":
  532. m_update_prioritization_scheme = UpdatePrioritizationSchemes.Time;
  533. break;
  534. case "distance":
  535. m_update_prioritization_scheme = UpdatePrioritizationSchemes.Distance;
  536. break;
  537. case "simpleangulardistance":
  538. m_update_prioritization_scheme = UpdatePrioritizationSchemes.SimpleAngularDistance;
  539. break;
  540. case "frontback":
  541. m_update_prioritization_scheme = UpdatePrioritizationSchemes.FrontBack;
  542. break;
  543. default:
  544. m_log.Warn("[SCENE]: UpdatePrioritizationScheme was not recognized, setting to default settomg of Time");
  545. m_update_prioritization_scheme = UpdatePrioritizationSchemes.Time;
  546. break;
  547. }
  548. m_reprioritization_enabled = interest_management_config.GetBoolean("ReprioritizationEnabled", true);
  549. m_reprioritization_interval = interest_management_config.GetDouble("ReprioritizationInterval", 5000.0);
  550. m_root_reprioritization_distance = interest_management_config.GetDouble("RootReprioritizationDistance", 10.0);
  551. m_child_reprioritization_distance = interest_management_config.GetDouble("ChildReprioritizationDistance", 20.0);
  552. }
  553. m_log.Info("[SCENE]: Using the " + m_update_prioritization_scheme + " prioritization scheme");
  554. #region BinaryStats
  555. try
  556. {
  557. IConfig statConfig = m_config.Configs["Statistics.Binary"];
  558. if (statConfig.Contains("enabled") && statConfig.GetBoolean("enabled"))
  559. {
  560. if (statConfig.Contains("collect_region_stats"))
  561. {
  562. if (statConfig.GetBoolean("collect_region_stats"))
  563. {
  564. // if enabled, add us to the event. If not enabled, I won't get called
  565. StatsReporter.OnSendStatsResult += LogSimStats;
  566. }
  567. }
  568. if (statConfig.Contains("region_stats_period_seconds"))
  569. {
  570. m_statLogPeriod = TimeSpan.FromSeconds(statConfig.GetInt("region_stats_period_seconds"));
  571. }
  572. if (statConfig.Contains("stats_dir"))
  573. {
  574. m_statsDir = statConfig.GetString("stats_dir");
  575. }
  576. }
  577. }
  578. catch
  579. {
  580. // if it doesn't work, we don't collect anything
  581. }
  582. #endregion BinaryStats
  583. }
  584. catch
  585. {
  586. m_log.Warn("[SCENE]: Failed to load StartupConfig");
  587. }
  588. }
  589. /// <summary>
  590. /// Mock constructor for scene group persistency unit tests.
  591. /// SceneObjectGroup RegionId property is delegated to Scene.
  592. /// </summary>
  593. /// <param name="regInfo"></param>
  594. public Scene(RegionInfo regInfo)
  595. {
  596. BordersLocked = true;
  597. Border northBorder = new Border();
  598. northBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, (int)Constants.RegionSize); //<---
  599. northBorder.CrossDirection = Cardinals.N;
  600. NorthBorders.Add(northBorder);
  601. Border southBorder = new Border();
  602. southBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue,0); //--->
  603. southBorder.CrossDirection = Cardinals.S;
  604. SouthBorders.Add(southBorder);
  605. Border eastBorder = new Border();
  606. eastBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue, (int)Constants.RegionSize); //<---
  607. eastBorder.CrossDirection = Cardinals.E;
  608. EastBorders.Add(eastBorder);
  609. Border westBorder = new Border();
  610. westBorder.BorderLine = new Vector3(float.MinValue, float.MaxValue,0); //--->
  611. westBorder.CrossDirection = Cardinals.W;
  612. WestBorders.Add(westBorder);
  613. BordersLocked = false;
  614. m_regInfo = regInfo;
  615. m_eventManager = new EventManager();
  616. }
  617. #endregion
  618. #region Startup / Close Methods
  619. public bool ShuttingDown
  620. {
  621. get { return shuttingdown; }
  622. }
  623. /// <value>
  624. /// The scene graph for this scene
  625. /// </value>
  626. /// TODO: Possibly stop other classes being able to manipulate this directly.
  627. public SceneGraph SceneGraph
  628. {
  629. get { return m_sceneGraph; }
  630. }
  631. protected virtual void RegisterDefaultSceneEvents()
  632. {
  633. IDialogModule dm = RequestModuleInterface<IDialogModule>();
  634. if (dm != null)
  635. m_eventManager.OnPermissionError += dm.SendAlertToUser;
  636. }
  637. public override string GetSimulatorVersion()
  638. {
  639. return m_simulatorVersion;
  640. }
  641. /// <summary>
  642. /// Another region is up.
  643. ///
  644. /// We only add it to the neighbor list if it's within 1 region from here.
  645. /// Agents may have draw distance values that cross two regions though, so
  646. /// we add it to the notify list regardless of distance. We'll check
  647. /// the agent's draw distance before notifying them though.
  648. /// </summary>
  649. /// <param name="otherRegion">RegionInfo handle for the new region.</param>
  650. /// <returns>True after all operations complete, throws exceptions otherwise.</returns>
  651. public override void OtherRegionUp(GridRegion otherRegion)
  652. {
  653. uint xcell = (uint)((int)otherRegion.RegionLocX / (int)Constants.RegionSize);
  654. uint ycell = (uint)((int)otherRegion.RegionLocY / (int)Constants.RegionSize);
  655. m_log.InfoFormat("[SCENE]: (on region {0}): Region {1} up in coords {2}-{3}",
  656. RegionInfo.RegionName, otherRegion.RegionName, xcell, ycell);
  657. if (RegionInfo.RegionHandle != otherRegion.RegionHandle)
  658. {
  659. // If these are cast to INT because long + negative values + abs returns invalid data
  660. int resultX = Math.Abs((int)xcell - (int)RegionInfo.RegionLocX);
  661. int resultY = Math.Abs((int)ycell - (int)RegionInfo.RegionLocY);
  662. if (resultX <= 1 && resultY <= 1)
  663. {
  664. // Let the grid service module know, so this can be cached
  665. m_eventManager.TriggerOnRegionUp(otherRegion);
  666. RegionInfo regInfo = new RegionInfo(xcell, ycell, otherRegion.InternalEndPoint, otherRegion.ExternalHostName);
  667. regInfo.RegionID = otherRegion.RegionID;
  668. regInfo.RegionName = otherRegion.RegionName;
  669. regInfo.ScopeID = otherRegion.ScopeID;
  670. regInfo.ExternalHostName = otherRegion.ExternalHostName;
  671. try
  672. {
  673. ForEachScenePresence(delegate(ScenePresence agent)
  674. {
  675. // If agent is a root agent.
  676. if (!agent.IsChildAgent)
  677. {
  678. //agent.ControllingClient.new
  679. //this.CommsManager.InterRegion.InformRegionOfChildAgent(otherRegion.RegionHandle, agent.ControllingClient.RequestClientInfo());
  680. List<ulong> old = new List<ulong>();
  681. old.Add(otherRegion.RegionHandle);
  682. agent.DropOldNeighbours(old);
  683. InformClientOfNeighbor(agent, regInfo);
  684. }
  685. }
  686. );
  687. }
  688. catch (NullReferenceException)
  689. {
  690. // This means that we're not booted up completely yet.
  691. // This shouldn't happen too often anymore.
  692. m_log.Error("[SCENE]: Couldn't inform client of regionup because we got a null reference exception");
  693. }
  694. }
  695. else
  696. {
  697. m_log.Info("[INTERGRID]: Got notice about far away Region: " + otherRegion.RegionName.ToString() +
  698. " at (" + otherRegion.RegionLocX.ToString() + ", " +
  699. otherRegion.RegionLocY.ToString() + ")");
  700. }
  701. }
  702. }
  703. public void AddNeighborRegion(RegionInfo region)
  704. {
  705. lock (m_neighbours)
  706. {
  707. if (!CheckNeighborRegion(region))
  708. {
  709. m_neighbours.Add(region);
  710. }
  711. }
  712. }
  713. public bool CheckNeighborRegion(RegionInfo region)
  714. {
  715. bool found = false;
  716. lock (m_neighbours)
  717. {
  718. foreach (RegionInfo reg in m_neighbours)
  719. {
  720. if (reg.RegionHandle == region.RegionHandle)
  721. {
  722. found = true;
  723. break;
  724. }
  725. }
  726. }
  727. return found;
  728. }
  729. // Alias IncomingHelloNeighbour OtherRegionUp, for now
  730. public GridRegion IncomingHelloNeighbour(RegionInfo neighbour)
  731. {
  732. OtherRegionUp(new GridRegion(neighbour));
  733. return new GridRegion(RegionInfo);
  734. }
  735. /// <summary>
  736. /// Given float seconds, this will restart the region.
  737. /// </summary>
  738. /// <param name="seconds">float indicating duration before restart.</param>
  739. public virtual void Restart(float seconds)
  740. {
  741. // notifications are done in 15 second increments
  742. // so .. if the number of seconds is less then 15 seconds, it's not really a restart request
  743. // It's a 'Cancel restart' request.
  744. // RestartNow() does immediate restarting.
  745. if (seconds < 15)
  746. {
  747. m_restartTimer.Stop();
  748. m_dialogModule.SendGeneralAlert("Restart Aborted");
  749. }
  750. else
  751. {
  752. // Now we figure out what to set the timer to that does the notifications and calls, RestartNow()
  753. m_restartTimer.Interval = 15000;
  754. m_incrementsof15seconds = (int)seconds / 15;
  755. m_RestartTimerCounter = 0;
  756. m_restartTimer.AutoReset = true;
  757. m_restartTimer.Elapsed += new ElapsedEventHandler(RestartTimer_Elapsed);
  758. m_log.Info("[REGION]: Restarting Region in " + (seconds / 60) + " minutes");
  759. m_restartTimer.Start();
  760. m_dialogModule.SendNotificationToUsersInRegion(
  761. UUID.Random(), String.Empty, RegionInfo.RegionName + ": Restarting in 2 Minutes");
  762. }
  763. }
  764. // The Restart timer has occured.
  765. // We have to figure out if this is a notification or if the number of seconds specified in Restart
  766. // have elapsed.
  767. // If they have elapsed, call RestartNow()
  768. public void RestartTimer_Elapsed(object sender, ElapsedEventArgs e)
  769. {
  770. m_RestartTimerCounter++;
  771. if (m_RestartTimerCounter <= m_incrementsof15seconds)
  772. {
  773. if (m_RestartTimerCounter == 4 || m_RestartTimerCounter == 6 || m_RestartTimerCounter == 7)
  774. m_dialogModule.SendNotificationToUsersInRegion(
  775. UUID.Random(),
  776. String.Empty,
  777. RegionInfo.RegionName + ": Restarting in " + ((8 - m_RestartTimerCounter) * 15) + " seconds");
  778. }
  779. else
  780. {
  781. m_restartTimer.Stop();
  782. m_restartTimer.AutoReset = false;
  783. RestartNow();
  784. }
  785. }
  786. // This causes the region to restart immediatley.
  787. public void RestartNow()
  788. {
  789. if (PhysicsScene != null)
  790. {
  791. PhysicsScene.Dispose();
  792. }
  793. m_log.Error("[REGION]: Closing");
  794. Close();
  795. m_log.Error("[REGION]: Firing Region Restart Message");
  796. base.Restart(0);
  797. }
  798. // This is a helper function that notifies root agents in this region that a new sim near them has come up
  799. // This is in the form of a timer because when an instance of OpenSim.exe is started,
  800. // Even though the sims initialize, they don't listen until 'all of the sims are initialized'
  801. // If we tell an agent about a sim that's not listening yet, the agent will not be able to connect to it.
  802. // subsequently the agent will never see the region come back online.
  803. public void RestartNotifyWaitElapsed(object sender, ElapsedEventArgs e)
  804. {
  805. m_restartWaitTimer.Stop();
  806. lock (m_regionRestartNotifyList)
  807. {
  808. foreach (RegionInfo region in m_regionRestartNotifyList)
  809. {
  810. try
  811. {
  812. ForEachScenePresence(delegate(ScenePresence agent)
  813. {
  814. // If agent is a root agent.
  815. if (!agent.IsChildAgent)
  816. {
  817. //agent.ControllingClient.new
  818. //this.CommsManager.InterRegion.InformRegionOfChildAgent(otherRegion.RegionHandle, agent.ControllingClient.RequestClientInfo());
  819. InformClientOfNeighbor(agent, region);
  820. }
  821. }
  822. );
  823. }
  824. catch (NullReferenceException)
  825. {
  826. // This means that we're not booted up completely yet.
  827. // This shouldn't happen too often anymore.
  828. }
  829. }
  830. // Reset list to nothing.
  831. m_regionRestartNotifyList.Clear();
  832. }
  833. }
  834. public void SetSceneCoreDebug(bool ScriptEngine, bool CollisionEvents, bool PhysicsEngine)
  835. {
  836. if (m_scripts_enabled != !ScriptEngine)
  837. {
  838. // Tedd! Here's the method to disable the scripting engine!
  839. if (ScriptEngine)
  840. {
  841. m_log.Info("Stopping all Scripts in Scene");
  842. foreach (EntityBase ent in Entities)
  843. {
  844. if (ent is SceneObjectGroup)
  845. {
  846. ((SceneObjectGroup) ent).RemoveScriptInstances();
  847. }
  848. }
  849. }
  850. else
  851. {
  852. m_log.Info("Starting all Scripts in Scene");
  853. lock (Entities)
  854. {
  855. foreach (EntityBase ent in Entities)
  856. {
  857. if (ent is SceneObjectGroup)
  858. {
  859. ((SceneObjectGroup)ent).CreateScriptInstances(0, false, DefaultScriptEngine, 0);
  860. }
  861. }
  862. }
  863. }
  864. m_scripts_enabled = !ScriptEngine;
  865. m_log.Info("[TOTEDD]: Here is the method to trigger disabling of the scripting engine");
  866. }
  867. if (m_physics_enabled != !PhysicsEngine)
  868. {
  869. m_physics_enabled = !PhysicsEngine;
  870. }
  871. }
  872. public int GetInaccurateNeighborCount()
  873. {
  874. lock (m_neighbours)
  875. {
  876. return m_neighbours.Count;
  877. }
  878. }
  879. // This is the method that shuts down the scene.
  880. public override void Close()
  881. {
  882. m_log.InfoFormat("[SCENE]: Closing down the single simulator: {0}", RegionInfo.RegionName);
  883. m_restartTimer.Stop();
  884. m_restartTimer.Close();
  885. // Kick all ROOT agents with the message, 'The simulator is going down'
  886. ForEachScenePresence(delegate(ScenePresence avatar)
  887. {
  888. if (avatar.KnownChildRegionHandles.Contains(RegionInfo.RegionHandle))
  889. avatar.KnownChildRegionHandles.Remove(RegionInfo.RegionHandle);
  890. if (!avatar.IsChildAgent)
  891. avatar.ControllingClient.Kick("The simulator is going down.");
  892. avatar.ControllingClient.SendShutdownConnectionNotice();
  893. });
  894. // Wait here, or the kick messages won't actually get to the agents before the scene terminates.
  895. Thread.Sleep(500);
  896. // Stop all client threads.
  897. ForEachScenePresence(delegate(ScenePresence avatar) { avatar.ControllingClient.Close(); });
  898. // Stop updating the scene objects and agents.
  899. //m_heartbeatTimer.Close();
  900. shuttingdown = true;
  901. m_log.Debug("[SCENE]: Persisting changed objects");
  902. List<EntityBase> entities = GetEntities();
  903. foreach (EntityBase entity in entities)
  904. {
  905. if (!entity.IsDeleted && entity is SceneObjectGroup && ((SceneObjectGroup)entity).HasGroupChanged)
  906. {
  907. ((SceneObjectGroup)entity).ProcessBackup(m_storageManager.DataStore, false);
  908. }
  909. }
  910. m_sceneGraph.Close();
  911. // De-register with region communications (events cleanup)
  912. UnRegisterRegionWithComms();
  913. // call the base class Close method.
  914. base.Close();
  915. }
  916. /// <summary>
  917. /// Start the timer which triggers regular scene updates
  918. /// </summary>
  919. public void StartTimer()
  920. {
  921. //m_log.Debug("[SCENE]: Starting timer");
  922. //m_heartbeatTimer.Enabled = true;
  923. //m_heartbeatTimer.Interval = (int)(m_timespan * 1000);
  924. //m_heartbeatTimer.Elapsed += new ElapsedEventHandler(Heartbeat);
  925. if (HeartbeatThread != null)
  926. {
  927. HeartbeatThread.Abort();
  928. HeartbeatThread = null;
  929. }
  930. m_lastUpdate = Environment.TickCount;
  931. HeartbeatThread = Watchdog.StartThread(Heartbeat, "Heartbeat for region " + RegionInfo.RegionName, ThreadPriority.Normal, false);
  932. }
  933. /// <summary>
  934. /// Sets up references to modules required by the scene
  935. /// </summary>
  936. public void SetModuleInterfaces()
  937. {
  938. m_xmlrpcModule = RequestModuleInterface<IXMLRPC>();
  939. m_worldCommModule = RequestModuleInterface<IWorldComm>();
  940. XferManager = RequestModuleInterface<IXfer>();
  941. m_AvatarFactory = RequestModuleInterface<IAvatarFactory>();
  942. m_serialiser = RequestModuleInterface<IRegionSerialiserModule>();
  943. m_interregionCommsOut = RequestModuleInterface<IInterregionCommsOut>();
  944. m_interregionCommsIn = RequestModuleInterface<IInterregionCommsIn>();
  945. m_dialogModule = RequestModuleInterface<IDialogModule>();
  946. m_capsModule = RequestModuleInterface<ICapabilitiesModule>();
  947. m_teleportModule = RequestModuleInterface<ITeleportModule>();
  948. }
  949. #endregion
  950. #region Update Methods
  951. /// <summary>
  952. /// Performs per-frame updates regularly
  953. /// </summary>
  954. private void Heartbeat()
  955. {
  956. if (!Monitor.TryEnter(m_heartbeatLock))
  957. {
  958. Watchdog.RemoveThread();
  959. return;
  960. }
  961. try
  962. {
  963. Update();
  964. m_lastUpdate = Environment.TickCount;
  965. m_firstHeartbeat = false;
  966. }
  967. catch (ThreadAbortException)
  968. {
  969. }
  970. finally
  971. {
  972. Monitor.Pulse(m_heartbeatLock);
  973. Monitor.Exit(m_heartbeatLock);
  974. }
  975. Watchdog.RemoveThread();
  976. }
  977. /// <summary>
  978. /// Performs per-frame updates on the scene, this should be the central scene loop
  979. /// </summary>
  980. public override void Update()
  981. {
  982. float physicsFPS;
  983. int maintc;
  984. while (!shuttingdown)
  985. {
  986. TimeSpan SinceLastFrame = DateTime.UtcNow - m_lastupdate;
  987. physicsFPS = 0f;
  988. maintc = otherMS = Environment.TickCount;
  989. int tmpFrameMS = maintc;
  990. // Increment the frame counter
  991. ++m_frame;
  992. try
  993. {
  994. // Check if any objects have reached their targets
  995. CheckAtTargets();
  996. // Update SceneObjectGroups that have scheduled themselves for updates
  997. // Objects queue their updates onto all scene presences
  998. if (m_frame % m_update_objects == 0)
  999. m_sceneGraph.UpdateObjectGroups();
  1000. // Run through all ScenePresences looking for updates
  1001. // Presence updates and queued object updates for each presence are sent to clients
  1002. if (m_frame % m_update_presences == 0)
  1003. m_sceneGraph.UpdatePresences();
  1004. int TempPhysicsMS2 = Environment.TickCount;
  1005. if ((m_frame % m_update_physics == 0) && m_physics_enabled)
  1006. m_sceneGraph.UpdatePreparePhysics();
  1007. TempPhysicsMS2 = Environment.TickCount - TempPhysicsMS2;
  1008. physicsMS2 = TempPhysicsMS2;
  1009. if (m_frame % m_update_entitymovement == 0)
  1010. m_sceneGraph.UpdateScenePresenceMovement();
  1011. int TempPhysicsMS = Environment.TickCount;
  1012. if (m_frame % m_update_physics == 0)
  1013. {
  1014. if (m_physics_enabled)
  1015. physicsFPS = m_sceneGraph.UpdatePhysics(Math.Max(SinceLastFrame.TotalSeconds, m_timespan));
  1016. if (SynchronizeScene != null)
  1017. SynchronizeScene(this);
  1018. }
  1019. TempPhysicsMS = Environment.TickCount - TempPhysicsMS;
  1020. physicsMS = TempPhysicsMS;
  1021. // Delete temp-on-rez stuff
  1022. if (m_frame % m_update_backup == 0)
  1023. {
  1024. int tozMS = Environment.TickCount;
  1025. CleanTempObjects();
  1026. tozMS -= Environment.TickCount;
  1027. tempOnRezMS = tozMS;
  1028. }
  1029. if (RegionStatus != RegionStatus.SlaveScene)
  1030. {
  1031. if (m_frame % m_update_events == 0)
  1032. {
  1033. int evMS = Environment.TickCount;
  1034. UpdateEvents();
  1035. evMS -= Environment.TickCount;
  1036. eventMS = evMS;
  1037. }
  1038. if (m_frame % m_update_backup == 0)
  1039. {
  1040. int backMS = Environment.TickCount;
  1041. UpdateStorageBackup();
  1042. backMS -= Environment.TickCount;
  1043. backupMS = backMS;
  1044. }
  1045. if (m_frame % m_update_terrain == 0)
  1046. {
  1047. int terMS = Environment.TickCount;
  1048. UpdateTerrain();
  1049. terMS -= Environment.TickCount;
  1050. terrainMS = terMS;
  1051. }
  1052. if (m_frame % m_update_land == 0)
  1053. {
  1054. int ldMS = Environment.TickCount;
  1055. UpdateLand();
  1056. ldMS -= Environment.TickCount;
  1057. landMS = ldMS;
  1058. }
  1059. int tickCount = Environment.TickCount;
  1060. otherMS = tickCount - otherMS;
  1061. tmpFrameMS -= tickCount;
  1062. frameMS = tmpFrameMS;
  1063. lastCompletedFrame = tickCount;
  1064. // if (m_frame%m_update_avatars == 0)
  1065. // UpdateInWorldTime();
  1066. StatsReporter.AddPhysicsFPS(physicsFPS);
  1067. StatsReporter.AddTimeDilation(TimeDilation);
  1068. StatsReporter.AddFPS(1);
  1069. StatsReporter.SetRootAgents(m_sceneGraph.GetRootAgentCount());
  1070. StatsReporter.SetChildAgents(m_sceneGraph.GetChildAgentCount());
  1071. StatsReporter.SetObjects(m_sceneGraph.GetTotalObjectsCount());
  1072. StatsReporter.SetActiveObjects(m_sceneGraph.GetActiveObjectsCount());
  1073. StatsReporter.addFrameMS(frameMS);
  1074. StatsReporter.addPhysicsMS(physicsMS + physicsMS2);
  1075. StatsReporter.addOtherMS(otherMS);
  1076. StatsReporter.SetActiveScripts(m_sceneGraph.GetActiveScriptsCount());
  1077. StatsReporter.addScriptLines(m_sceneGraph.GetScriptLPS());
  1078. }
  1079. if (loginsdisabled && m_frame > 20)
  1080. {
  1081. // In 99.9% of cases it is a bad idea to manually force garbage collection. However,
  1082. // this is a rare case where we know we have just went through a long cycle of heap
  1083. // allocations, and there is no more work to be done until someone logs in
  1084. GC.Collect();
  1085. m_log.DebugFormat("[REGION]: Enabling logins for {0}", RegionInfo.RegionName);
  1086. loginsdisabled = false;
  1087. }
  1088. }
  1089. catch (NotImplementedException)
  1090. {
  1091. throw;
  1092. }
  1093. catch (AccessViolationException e)
  1094. {
  1095. m_log.Error("[REGION]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  1096. }
  1097. //catch (NullReferenceException e)
  1098. //{
  1099. // m_log.Error("[REGION]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  1100. //}
  1101. catch (InvalidOperationException e)
  1102. {
  1103. m_log.Error("[REGION]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  1104. }
  1105. catch (Exception e)
  1106. {
  1107. m_log.Error("[REGION]: Failed with exception " + e.ToString() + " On Region: " + RegionInfo.RegionName);
  1108. }
  1109. finally
  1110. {
  1111. m_lastupdate = DateTime.UtcNow;
  1112. }
  1113. maintc = Environment.TickCount - maintc;
  1114. maintc = (int)(m_timespan * 1000) - maintc;
  1115. if ((maintc < (m_timespan * 1000)) && maintc > 0)
  1116. Thread.Sleep(maintc);
  1117. // Tell the watchdog that this thread is still alive
  1118. Watchdog.UpdateThread();
  1119. }
  1120. }
  1121. public void AddGroupTarget(SceneObjectGroup grp)
  1122. {
  1123. lock (m_groupsWithTargets)
  1124. m_groupsWithTargets[grp.UUID] = grp;
  1125. }
  1126. public void RemoveGroupTarget(SceneObjectGroup grp)
  1127. {
  1128. lock (m_groupsWithTargets)
  1129. m_groupsWithTargets.Remove(grp.UUID);
  1130. }
  1131. private void CheckAtTargets()
  1132. {
  1133. lock (m_groupsWithTargets)
  1134. {
  1135. foreach (SceneObjectGroup entry in m_groupsWithTargets.Values)
  1136. {
  1137. entry.checkAtTargets();
  1138. }
  1139. }
  1140. }
  1141. /// <summary>
  1142. /// Send out simstats data to all clients
  1143. /// </summary>
  1144. /// <param name="stats">Stats on the Simulator's performance</param>
  1145. private void SendSimStatsPackets(SimStats stats)
  1146. {
  1147. ForEachScenePresence(
  1148. delegate(ScenePresence agent)
  1149. {
  1150. if (!agent.IsChildAgent)
  1151. agent.ControllingClient.SendSimStats(stats);
  1152. }
  1153. );
  1154. }
  1155. /// <summary>
  1156. /// Recount SceneObjectPart in parcel aabb
  1157. /// </summary>
  1158. private void UpdateLand()
  1159. {
  1160. if (LandChannel != null)
  1161. {
  1162. if (LandChannel.IsLandPrimCountTainted())
  1163. {
  1164. EventManager.TriggerParcelPrimCountUpdate();
  1165. }
  1166. }
  1167. }
  1168. /// <summary>
  1169. /// Update the terrain if it needs to be updated.
  1170. /// </summary>
  1171. private void UpdateTerrain()
  1172. {
  1173. EventManager.TriggerTerrainTick();
  1174. }
  1175. /// <summary>
  1176. /// Back up queued up changes
  1177. /// </summary>
  1178. private void UpdateStorageBackup()
  1179. {
  1180. if (!m_backingup)
  1181. {
  1182. m_backingup = true;
  1183. Util.FireAndForget(BackupWaitCallback);
  1184. }
  1185. }
  1186. /// <summary>
  1187. /// Sends out the OnFrame event to the modules
  1188. /// </summary>
  1189. private void UpdateEvents()
  1190. {
  1191. m_eventManager.TriggerOnFrame();
  1192. }
  1193. /// <summary>
  1194. /// Wrapper for Backup() that can be called with Util.FireAndForget()
  1195. /// </summary>
  1196. private void BackupWaitCallback(object o)
  1197. {
  1198. Backup();
  1199. }
  1200. /// <summary>
  1201. /// Backup the scene. This acts as the main method of the backup thread.
  1202. /// </summary>
  1203. /// <returns></returns>
  1204. public void Backup()
  1205. {
  1206. lock (m_returns)
  1207. {
  1208. EventManager.TriggerOnBackup(m_storageManager.DataStore);
  1209. m_backingup = false;
  1210. foreach (KeyValuePair<UUID, ReturnInfo> ret in m_returns)
  1211. {
  1212. UUID transaction = UUID.Random();
  1213. GridInstantMessage msg = new GridInstantMessage();
  1214. msg.fromAgentID = new Guid(UUID.Zero.ToString()); // From server
  1215. msg.toAgentID = new Guid(ret.Key.ToString());
  1216. msg.imSessionID = new Guid(transaction.ToString());
  1217. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  1218. msg.fromAgentName = "Server";
  1219. msg.dialog = (byte)19; // Object msg
  1220. msg.fromGroup = false;
  1221. msg.offline = (byte)1;
  1222. msg.ParentEstateID = RegionInfo.EstateSettings.ParentEstateID;
  1223. msg.Position = Vector3.Zero;
  1224. msg.RegionID = RegionInfo.RegionID.Guid;
  1225. msg.binaryBucket = new byte[0];
  1226. if (ret.Value.count > 1)
  1227. msg.message = string.Format("Your {0} objects were returned from {1} in region {2} due to {3}", ret.Value.count, ret.Value.location.ToString(), RegionInfo.RegionName, ret.Value.reason);
  1228. else
  1229. msg.message = string.Format("Your object {0} was returned from {1} in region {2} due to {3}", ret.Value.objectName, ret.Value.location.ToString(), RegionInfo.RegionName, ret.Value.reason);
  1230. IMessageTransferModule tr = RequestModuleInterface<IMessageTransferModule>();
  1231. if (tr != null)
  1232. tr.SendInstantMessage(msg, delegate(bool success) {});
  1233. }
  1234. m_returns.Clear();
  1235. }
  1236. }
  1237. /// <summary>
  1238. /// Synchronous force backup. For deletes and links/unlinks
  1239. /// </summary>
  1240. /// <param name="group">Object to be backed up</param>
  1241. public void ForceSceneObjectBackup(SceneObjectGroup group)
  1242. {
  1243. if (group != null)
  1244. {
  1245. group.ProcessBackup(m_storageManager.DataStore, true);
  1246. }
  1247. }
  1248. /// <summary>
  1249. /// Return object to avatar Message
  1250. /// </summary>
  1251. /// <param name="agentID">Avatar Unique Id</param>
  1252. /// <param name="objectName">Name of object returned</param>
  1253. /// <param name="location">Location of object returned</param>
  1254. /// <param name="reason">Reasion for object return</param>
  1255. public void AddReturn(UUID agentID, string objectName, Vector3 location, string reason)
  1256. {
  1257. lock (m_returns)
  1258. {
  1259. if (m_returns.ContainsKey(agentID))
  1260. {
  1261. ReturnInfo info = m_returns[agentID];
  1262. info.count++;
  1263. m_returns[agentID] = info;
  1264. }
  1265. else
  1266. {
  1267. ReturnInfo info = new ReturnInfo();
  1268. info.count = 1;
  1269. info.objectName = objectName;
  1270. info.location = location;
  1271. info.reason = reason;
  1272. m_returns[agentID] = info;
  1273. }
  1274. }
  1275. }
  1276. #endregion
  1277. #region Load Terrain
  1278. /// <summary>
  1279. /// Store the terrain in the persistant data store
  1280. /// </summary>
  1281. public void SaveTerrain()
  1282. {
  1283. m_storageManager.DataStore.StoreTerrain(Heightmap.GetDoubles(), RegionInfo.RegionID);
  1284. }
  1285. /// <summary>
  1286. /// Loads the World heightmap
  1287. /// </summary>
  1288. public override void LoadWorldMap()
  1289. {
  1290. try
  1291. {
  1292. double[,] map = m_storageManager.DataStore.LoadTerrain(RegionInfo.RegionID);
  1293. if (map == null)
  1294. {
  1295. m_log.Info("[TERRAIN]: No default terrain. Generating a new terrain.");
  1296. Heightmap = new TerrainChannel();
  1297. m_storageManager.DataStore.StoreTerrain(Heightmap.GetDoubles(), RegionInfo.RegionID);
  1298. }
  1299. else
  1300. {
  1301. Heightmap = new TerrainChannel(map);
  1302. }
  1303. }
  1304. catch (IOException e)
  1305. {
  1306. m_log.Warn("[TERRAIN]: Scene.cs: LoadWorldMap() - Failed with exception " + e.ToString() + " Regenerating");
  1307. // Non standard region size. If there's an old terrain in the database, it might read past the buffer
  1308. #pragma warning disable 0162
  1309. if ((int)Constants.RegionSize != 256)
  1310. {
  1311. Heightmap = new TerrainChannel();
  1312. m_storageManager.DataStore.StoreTerrain(Heightmap.GetDoubles(), RegionInfo.RegionID);
  1313. }
  1314. }
  1315. catch (Exception e)
  1316. {
  1317. m_log.Warn("[TERRAIN]: Scene.cs: LoadWorldMap() - Failed with exception " + e.ToString());
  1318. }
  1319. }
  1320. /// <summary>
  1321. /// Register this region with a grid service
  1322. /// </summary>
  1323. /// <exception cref="System.Exception">Thrown if registration of the region itself fails.</exception>
  1324. public void RegisterRegionWithGrid()
  1325. {
  1326. RegisterCommsEvents();
  1327. // These two 'commands' *must be* next to each other or sim rebooting fails.
  1328. //m_sceneGridService.RegisterRegion(m_interregionCommsOut, RegionInfo);
  1329. GridRegion region = new GridRegion(RegionInfo);
  1330. bool success = GridService.RegisterRegion(RegionInfo.ScopeID, region);
  1331. if (!success)
  1332. throw new Exception("Can't register with grid");
  1333. m_sceneGridService.SetScene(this);
  1334. m_sceneGridService.InformNeighborsThatRegionisUp(RequestModuleInterface<INeighbourService>(), RegionInfo);
  1335. //Dictionary<string, string> dGridSettings = m_sceneGridService.GetGridSettings();
  1336. //if (dGridSettings.ContainsKey("allow_forceful_banlines"))
  1337. //{
  1338. // if (dGridSettings["allow_forceful_banlines"] != "TRUE")
  1339. // {
  1340. // m_log.Info("[GRID]: Grid is disabling forceful parcel banlists");
  1341. // EventManager.TriggerSetAllowForcefulBan(false);
  1342. // }
  1343. // else
  1344. // {
  1345. // m_log.Info("[GRID]: Grid is allowing forceful parcel banlists");
  1346. // EventManager.TriggerSetAllowForcefulBan(true);
  1347. // }
  1348. //}
  1349. }
  1350. /// <summary>
  1351. /// Create a terrain texture for this scene
  1352. /// </summary>
  1353. public void CreateTerrainTexture(bool temporary)
  1354. {
  1355. //create a texture asset of the terrain
  1356. IMapImageGenerator terrain = RequestModuleInterface<IMapImageGenerator>();
  1357. // Cannot create a map for a nonexistant heightmap yet.
  1358. if (Heightmap == null)
  1359. return;
  1360. if (terrain == null)
  1361. return;
  1362. byte[] data = terrain.WriteJpeg2000Image("defaultstripe.png");
  1363. if (data != null)
  1364. {
  1365. IWorldMapModule mapModule = RequestModuleInterface<IWorldMapModule>();
  1366. if (mapModule != null)
  1367. mapModule.LazySaveGeneratedMaptile(data, temporary);
  1368. }
  1369. }
  1370. #endregion
  1371. #region Load Land
  1372. /// <summary>
  1373. /// Loads all Parcel data from the datastore for region identified by regionID
  1374. /// </summary>
  1375. /// <param name="regionID">Unique Identifier of the Region to load parcel data for</param>
  1376. public void loadAllLandObjectsFromStorage(UUID regionID)
  1377. {
  1378. m_log.Info("[SCENE]: Loading land objects from storage");
  1379. List<LandData> landData = m_storageManager.DataStore.LoadLandObjects(regionID);
  1380. if (LandChannel != null)
  1381. {
  1382. if (landData.Count == 0)
  1383. {
  1384. EventManager.TriggerNoticeNoLandDataFromStorage();
  1385. }
  1386. else
  1387. {
  1388. EventManager.TriggerIncomingLandDataFromStorage(landData);
  1389. }
  1390. }
  1391. else
  1392. {
  1393. m_log.Error("[SCENE]: Land Channel is not defined. Cannot load from storage!");
  1394. }
  1395. }
  1396. #endregion
  1397. #region Primitives Methods
  1398. /// <summary>
  1399. /// Loads the World's objects
  1400. /// </summary>
  1401. public virtual void LoadPrimsFromStorage(UUID regionID)
  1402. {
  1403. m_log.Info("[SCENE]: Loading objects from datastore");
  1404. List<SceneObjectGroup> PrimsFromDB = m_storageManager.DataStore.LoadObjects(regionID);
  1405. m_log.Info("[SCENE]: Loaded " + PrimsFromDB.Count + " objects from the datastore");
  1406. foreach (SceneObjectGroup group in PrimsFromDB)
  1407. {
  1408. if (group.RootPart == null)
  1409. {
  1410. m_log.ErrorFormat("[SCENE] Found a SceneObjectGroup with m_rootPart == null and {0} children",
  1411. group.Children == null ? 0 : group.Children.Count);
  1412. }
  1413. AddRestoredSceneObject(group, true, true);
  1414. SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  1415. rootPart.ObjectFlags &= ~(uint)PrimFlags.Scripted;
  1416. rootPart.TrimPermissions();
  1417. group.CheckSculptAndLoad();
  1418. //rootPart.DoPhysicsPropertyUpdate(UsePhysics, true);
  1419. }
  1420. m_log.Info("[SCENE]: Loaded " + PrimsFromDB.Count.ToString() + " SceneObject(s)");
  1421. }
  1422. /// <summary>
  1423. /// Gets a new rez location based on the raycast and the size of the object that is being rezzed.
  1424. /// </summary>
  1425. /// <param name="RayStart"></param>
  1426. /// <param name="RayEnd"></param>
  1427. /// <param name="RayTargetID"></param>
  1428. /// <param name="rot"></param>
  1429. /// <param name="bypassRayCast"></param>
  1430. /// <param name="RayEndIsIntersection"></param>
  1431. /// <param name="frontFacesOnly"></param>
  1432. /// <param name="scale"></param>
  1433. /// <param name="FaceCenter"></param>
  1434. /// <returns></returns>
  1435. public Vector3 GetNewRezLocation(Vector3 RayStart, Vector3 RayEnd, UUID RayTargetID, Quaternion rot, byte bypassRayCast, byte RayEndIsIntersection, bool frontFacesOnly, Vector3 scale, bool FaceCenter)
  1436. {
  1437. Vector3 pos = Vector3.Zero;
  1438. if (RayEndIsIntersection == (byte)1)
  1439. {
  1440. pos = RayEnd;
  1441. return pos;
  1442. }
  1443. if (RayTargetID != UUID.Zero)
  1444. {
  1445. SceneObjectPart target = GetSceneObjectPart(RayTargetID);
  1446. Vector3 direction = Vector3.Normalize(RayEnd - RayStart);
  1447. Vector3 AXOrigin = new Vector3(RayStart.X, RayStart.Y, RayStart.Z);
  1448. Vector3 AXdirection = new Vector3(direction.X, direction.Y, direction.Z);
  1449. if (target != null)
  1450. {
  1451. pos = target.AbsolutePosition;
  1452. //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());
  1453. // TODO: Raytrace better here
  1454. //EntityIntersection ei = m_sceneGraph.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection));
  1455. Ray NewRay = new Ray(AXOrigin, AXdirection);
  1456. // Ray Trace against target here
  1457. EntityIntersection ei = target.TestIntersectionOBB(NewRay, Quaternion.Identity, frontFacesOnly, FaceCenter);
  1458. // Un-comment out the following line to Get Raytrace results printed to the console.
  1459. // m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  1460. float ScaleOffset = 0.5f;
  1461. // If we hit something
  1462. if (ei.HitTF)
  1463. {
  1464. Vector3 scaleComponent = new Vector3(ei.AAfaceNormal.X, ei.AAfaceNormal.Y, ei.AAfaceNormal.Z);
  1465. if (scaleComponent.X != 0) ScaleOffset = scale.X;
  1466. if (scaleComponent.Y != 0) ScaleOffset = scale.Y;
  1467. if (scaleComponent.Z != 0) ScaleOffset = scale.Z;
  1468. ScaleOffset = Math.Abs(ScaleOffset);
  1469. Vector3 intersectionpoint = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  1470. Vector3 normal = new Vector3(ei.normal.X, ei.normal.Y, ei.normal.Z);
  1471. // Set the position to the intersection point
  1472. Vector3 offset = (normal * (ScaleOffset / 2f));
  1473. pos = (intersectionpoint + offset);
  1474. //Seems to make no sense to do this as this call is used for rezzing from inventory as well, and with inventory items their size is not always 0.5f
  1475. //And in cases when we weren't rezzing from inventory we were re-adding the 0.25 straight after calling this method
  1476. // Un-offset the prim (it gets offset later by the consumer method)
  1477. //pos.Z -= 0.25F;
  1478. }
  1479. return pos;
  1480. }
  1481. else
  1482. {
  1483. // We don't have a target here, so we're going to raytrace all the objects in the scene.
  1484. EntityIntersection ei = m_sceneGraph.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection), true, false);
  1485. // Un-comment the following line to print the raytrace results to the console.
  1486. //m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  1487. if (ei.HitTF)
  1488. {
  1489. pos = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  1490. } else
  1491. {
  1492. // fall back to our stupid functionality
  1493. pos = RayEnd;
  1494. }
  1495. return pos;
  1496. }
  1497. }
  1498. else
  1499. {
  1500. // fall back to our stupid functionality
  1501. pos = RayEnd;
  1502. //increase height so its above the ground.
  1503. //should be getting the normal of the ground at the rez point and using that?
  1504. pos.Z += scale.Z / 2f;
  1505. return pos;
  1506. }
  1507. }
  1508. /// <summary>
  1509. /// Create a New SceneObjectGroup/Part by raycasting
  1510. /// </summary>
  1511. /// <param name="ownerID"></param>
  1512. /// <param name="groupID"></param>
  1513. /// <param name="RayEnd"></param>
  1514. /// <param name="rot"></param>
  1515. /// <param name="shape"></param>
  1516. /// <param name="bypassRaycast"></param>
  1517. /// <param name="RayStart"></param>
  1518. /// <param name="RayTargetID"></param>
  1519. /// <param name="RayEndIsIntersection"></param>
  1520. public virtual void AddNewPrim(UUID ownerID, UUID groupID, Vector3 RayEnd, Quaternion rot, PrimitiveBaseShape shape,
  1521. byte bypassRaycast, Vector3 RayStart, UUID RayTargetID,
  1522. byte RayEndIsIntersection)
  1523. {
  1524. Vector3 pos = GetNewRezLocation(RayStart, RayEnd, RayTargetID, rot, bypassRaycast, RayEndIsIntersection, true, new Vector3(0.5f, 0.5f, 0.5f), false);
  1525. if (Permissions.CanRezObject(1, ownerID, pos))
  1526. {
  1527. // rez ON the ground, not IN the ground
  1528. // pos.Z += 0.25F; The rez point should now be correct so that its not in the ground
  1529. AddNewPrim(ownerID, groupID, pos, rot, shape);
  1530. }
  1531. }
  1532. public virtual SceneObjectGroup AddNewPrim(
  1533. UUID ownerID, UUID groupID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  1534. {
  1535. //m_log.DebugFormat(
  1536. // "[SCENE]: Scene.AddNewPrim() pcode {0} called for {1} in {2}", shape.PCode, ownerID, RegionInfo.RegionName);
  1537. // If an entity creator has been registered for this prim type then use that
  1538. if (m_entityCreators.ContainsKey((PCode)shape.PCode))
  1539. return m_entityCreators[(PCode)shape.PCode].CreateEntity(ownerID, groupID, pos, rot, shape);
  1540. // Otherwise, use this default creation code;
  1541. SceneObjectGroup sceneObject = new SceneObjectGroup(ownerID, pos, rot, shape);
  1542. AddNewSceneObject(sceneObject, true);
  1543. sceneObject.SetGroup(groupID, null);
  1544. return sceneObject;
  1545. }
  1546. /// <summary>
  1547. /// Add an object into the scene that has come from storage
  1548. /// </summary>
  1549. ///
  1550. /// <param name="sceneObject"></param>
  1551. /// <param name="attachToBackup">
  1552. /// If true, changes to the object will be reflected in its persisted data
  1553. /// If false, the persisted data will not be changed even if the object in the scene is changed
  1554. /// </param>
  1555. /// <param name="alreadyPersisted">
  1556. /// If true, we won't persist this object until it changes
  1557. /// If false, we'll persist this object immediately
  1558. /// </param>
  1559. /// <returns>
  1560. /// true if the object was added, false if an object with the same uuid was already in the scene
  1561. /// </returns>
  1562. public bool AddRestoredSceneObject(
  1563. SceneObjectGroup sceneObject, bool attachToBackup, bool alreadyPersisted)
  1564. {
  1565. return m_sceneGraph.AddRestoredSceneObject(sceneObject, attachToBackup, alreadyPersisted);
  1566. }
  1567. /// <summary>
  1568. /// Add a newly created object to the scene
  1569. /// </summary>
  1570. /// <param name="sceneObject"></param>
  1571. /// <param name="attachToBackup">
  1572. /// If true, the object is made persistent into the scene.
  1573. /// If false, the object will not persist over server restarts
  1574. /// </param>
  1575. public bool AddNewSceneObject(SceneObjectGroup sceneObject, bool attachToBackup)
  1576. {
  1577. return m_sceneGraph.AddNewSceneObject(sceneObject, attachToBackup);
  1578. }
  1579. /// <summary>
  1580. /// Delete every object from the scene
  1581. /// </summary>
  1582. public void DeleteAllSceneObjects()
  1583. {
  1584. lock (Entities)
  1585. {
  1586. ICollection<EntityBase> entities = new List<EntityBase>(Entities);
  1587. foreach (EntityBase e in entities)
  1588. {
  1589. if (e is SceneObjectGroup)
  1590. DeleteSceneObject((SceneObjectGroup)e, false);
  1591. }
  1592. }
  1593. }
  1594. /// <summary>
  1595. /// Synchronously delete the given object from the scene.
  1596. /// </summary>
  1597. /// <param name="group">Object Id</param>
  1598. /// <param name="silent">Suppress broadcasting changes to other clients.</param>
  1599. public void DeleteSceneObject(SceneObjectGroup group, bool silent)
  1600. {
  1601. //SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  1602. // Serialise calls to RemoveScriptInstances to avoid
  1603. // deadlocking on m_parts inside SceneObjectGroup
  1604. lock (m_deleting_scene_object)
  1605. {
  1606. group.RemoveScriptInstances();
  1607. }
  1608. foreach (SceneObjectPart part in group.Children.Values)
  1609. {
  1610. if (part.IsJoint() && ((part.ObjectFlags&(uint)PrimFlags.Physics) != 0))
  1611. {
  1612. PhysicsScene.RequestJointDeletion(part.Name); // FIXME: what if the name changed?
  1613. }
  1614. else if (part.PhysActor != null)
  1615. {
  1616. PhysicsScene.RemovePrim(part.PhysActor);
  1617. part.PhysActor = null;
  1618. }
  1619. }
  1620. // if (rootPart.PhysActor != null)
  1621. // {
  1622. // PhysicsScene.RemovePrim(rootPart.PhysActor);
  1623. // rootPart.PhysActor = null;
  1624. // }
  1625. if (UnlinkSceneObject(group.UUID, false))
  1626. {
  1627. EventManager.TriggerObjectBeingRemovedFromScene(group);
  1628. EventManager.TriggerParcelPrimCountTainted();
  1629. }
  1630. group.DeleteGroup(silent);
  1631. }
  1632. /// <summary>
  1633. /// Unlink the given object from the scene. Unlike delete, this just removes the record of the object - the
  1634. /// object itself is not destroyed.
  1635. /// </summary>
  1636. /// <param name="uuid">Id of object.</param>
  1637. /// <returns>true if the object was in the scene, false if it was not</returns>
  1638. /// <param name="softDelete">If true, only deletes from scene, but keeps object in database.</param>
  1639. public bool UnlinkSceneObject(UUID uuid, bool softDelete)
  1640. {
  1641. if (m_sceneGraph.DeleteSceneObject(uuid, softDelete))
  1642. {
  1643. if (!softDelete)
  1644. {
  1645. m_storageManager.DataStore.RemoveObject(uuid,
  1646. m_regInfo.RegionID);
  1647. }
  1648. return true;
  1649. }
  1650. return false;
  1651. }
  1652. /// <summary>
  1653. /// Move the given scene object into a new region depending on which region its absolute position has moved
  1654. /// into.
  1655. ///
  1656. /// This method locates the new region handle and offsets the prim position for the new region
  1657. /// </summary>
  1658. /// <param name="attemptedPosition">the attempted out of region position of the scene object</param>
  1659. /// <param name="grp">the scene object that we're crossing</param>
  1660. public void CrossPrimGroupIntoNewRegion(Vector3 attemptedPosition, SceneObjectGroup grp, bool silent)
  1661. {
  1662. if (grp == null)
  1663. return;
  1664. if (grp.IsDeleted)
  1665. return;
  1666. if (grp.RootPart.DIE_AT_EDGE)
  1667. {
  1668. // We remove the object here
  1669. try
  1670. {
  1671. DeleteSceneObject(grp, false);
  1672. }
  1673. catch (Exception)
  1674. {
  1675. m_log.Warn("[DATABASE]: exception when trying to remove the prim that crossed the border.");
  1676. }
  1677. return;
  1678. }
  1679. int thisx = (int)RegionInfo.RegionLocX;
  1680. int thisy = (int)RegionInfo.RegionLocY;
  1681. Vector3 EastCross = new Vector3(0.1f,0,0);
  1682. Vector3 WestCross = new Vector3(-0.1f, 0, 0);
  1683. Vector3 NorthCross = new Vector3(0, 0.1f, 0);
  1684. Vector3 SouthCross = new Vector3(0, -0.1f, 0);
  1685. // use this if no borders were crossed!
  1686. ulong newRegionHandle
  1687. = Util.UIntsToLong((uint)((thisx) * Constants.RegionSize),
  1688. (uint)((thisy) * Constants.RegionSize));
  1689. Vector3 pos = attemptedPosition;
  1690. int changeX = 1;
  1691. int changeY = 1;
  1692. if (TestBorderCross(attemptedPosition + WestCross, Cardinals.W))
  1693. {
  1694. if (TestBorderCross(attemptedPosition + SouthCross, Cardinals.S))
  1695. {
  1696. Border crossedBorderx = GetCrossedBorder(attemptedPosition + WestCross, Cardinals.W);
  1697. if (crossedBorderx.BorderLine.Z > 0)
  1698. {
  1699. pos.X = ((pos.X + crossedBorderx.BorderLine.Z));
  1700. changeX = (int)(crossedBorderx.BorderLine.Z /(int) Constants.RegionSize);
  1701. }
  1702. else
  1703. pos.X = ((pos.X + Constants.RegionSize));
  1704. Border crossedBordery = GetCrossedBorder(attemptedPosition + SouthCross, Cardinals.S);
  1705. //(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize)
  1706. if (crossedBordery.BorderLine.Z > 0)
  1707. {
  1708. pos.Y = ((pos.Y + crossedBordery.BorderLine.Z));
  1709. changeY = (int)(crossedBordery.BorderLine.Z / (int)Constants.RegionSize);
  1710. }
  1711. else
  1712. pos.Y = ((pos.Y + Constants.RegionSize));
  1713. newRegionHandle
  1714. = Util.UIntsToLong((uint)((thisx - changeX) * Constants.RegionSize),
  1715. (uint)((thisy - changeY) * Constants.RegionSize));
  1716. // x - 1
  1717. // y - 1
  1718. }
  1719. else if (TestBorderCross(attemptedPosition + NorthCross, Cardinals.N))
  1720. {
  1721. Border crossedBorderx = GetCrossedBorder(attemptedPosition + WestCross, Cardinals.W);
  1722. if (crossedBorderx.BorderLine.Z > 0)
  1723. {
  1724. pos.X = ((pos.X + crossedBorderx.BorderLine.Z));
  1725. changeX = (int)(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize);
  1726. }
  1727. else
  1728. pos.X = ((pos.X + Constants.RegionSize));
  1729. Border crossedBordery = GetCrossedBorder(attemptedPosition + SouthCross, Cardinals.S);
  1730. //(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize)
  1731. if (crossedBordery.BorderLine.Z > 0)
  1732. {
  1733. pos.Y = ((pos.Y + crossedBordery.BorderLine.Z));
  1734. changeY = (int)(crossedBordery.BorderLine.Z / (int)Constants.RegionSize);
  1735. }
  1736. else
  1737. pos.Y = ((pos.Y + Constants.RegionSize));
  1738. newRegionHandle
  1739. = Util.UIntsToLong((uint)((thisx - changeX) * Constants.RegionSize),
  1740. (uint)((thisy + changeY) * Constants.RegionSize));
  1741. // x - 1
  1742. // y + 1
  1743. }
  1744. else
  1745. {
  1746. Border crossedBorderx = GetCrossedBorder(attemptedPosition + WestCross, Cardinals.W);
  1747. if (crossedBorderx.BorderLine.Z > 0)
  1748. {
  1749. pos.X = ((pos.X + crossedBorderx.BorderLine.Z));
  1750. changeX = (int)(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize);
  1751. }
  1752. else
  1753. pos.X = ((pos.X + Constants.RegionSize));
  1754. newRegionHandle
  1755. = Util.UIntsToLong((uint)((thisx - changeX) * Constants.RegionSize),
  1756. (uint) (thisy*Constants.RegionSize));
  1757. // x - 1
  1758. }
  1759. }
  1760. else if (TestBorderCross(attemptedPosition + EastCross, Cardinals.E))
  1761. {
  1762. if (TestBorderCross(attemptedPosition + SouthCross, Cardinals.S))
  1763. {
  1764. pos.X = ((pos.X - Constants.RegionSize));
  1765. Border crossedBordery = GetCrossedBorder(attemptedPosition + SouthCross, Cardinals.S);
  1766. //(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize)
  1767. if (crossedBordery.BorderLine.Z > 0)
  1768. {
  1769. pos.Y = ((pos.Y + crossedBordery.BorderLine.Z));
  1770. changeY = (int)(crossedBordery.BorderLine.Z / (int)Constants.RegionSize);
  1771. }
  1772. else
  1773. pos.Y = ((pos.Y + Constants.RegionSize));
  1774. newRegionHandle
  1775. = Util.UIntsToLong((uint)((thisx + changeX) * Constants.RegionSize),
  1776. (uint)((thisy - changeY) * Constants.RegionSize));
  1777. // x + 1
  1778. // y - 1
  1779. }
  1780. else if (TestBorderCross(attemptedPosition + NorthCross, Cardinals.N))
  1781. {
  1782. pos.X = ((pos.X - Constants.RegionSize));
  1783. pos.Y = ((pos.Y - Constants.RegionSize));
  1784. newRegionHandle
  1785. = Util.UIntsToLong((uint)((thisx + changeX) * Constants.RegionSize),
  1786. (uint)((thisy + changeY) * Constants.RegionSize));
  1787. // x + 1
  1788. // y + 1
  1789. }
  1790. else
  1791. {
  1792. pos.X = ((pos.X - Constants.RegionSize));
  1793. newRegionHandle
  1794. = Util.UIntsToLong((uint)((thisx + changeX) * Constants.RegionSize),
  1795. (uint) (thisy*Constants.RegionSize));
  1796. // x + 1
  1797. }
  1798. }
  1799. else if (TestBorderCross(attemptedPosition + SouthCross, Cardinals.S))
  1800. {
  1801. Border crossedBordery = GetCrossedBorder(attemptedPosition + SouthCross, Cardinals.S);
  1802. //(crossedBorderx.BorderLine.Z / (int)Constants.RegionSize)
  1803. if (crossedBordery.BorderLine.Z > 0)
  1804. {
  1805. pos.Y = ((pos.Y + crossedBordery.BorderLine.Z));
  1806. changeY = (int)(crossedBordery.BorderLine.Z / (int)Constants.RegionSize);
  1807. }
  1808. else
  1809. pos.Y = ((pos.Y + Constants.RegionSize));
  1810. newRegionHandle
  1811. = Util.UIntsToLong((uint)(thisx * Constants.RegionSize), (uint)((thisy - changeY) * Constants.RegionSize));
  1812. // y - 1
  1813. }
  1814. else if (TestBorderCross(attemptedPosition + NorthCross, Cardinals.N))
  1815. {
  1816. pos.Y = ((pos.Y - Constants.RegionSize));
  1817. newRegionHandle
  1818. = Util.UIntsToLong((uint)(thisx * Constants.RegionSize), (uint)((thisy + changeY) * Constants.RegionSize));
  1819. // y + 1
  1820. }
  1821. // Offset the positions for the new region across the border
  1822. Vector3 oldGroupPosition = grp.RootPart.GroupPosition;
  1823. grp.OffsetForNewRegion(pos);
  1824. // If we fail to cross the border, then reset the position of the scene object on that border.
  1825. if (!CrossPrimGroupIntoNewRegion(newRegionHandle, grp, silent))
  1826. {
  1827. grp.OffsetForNewRegion(oldGroupPosition);
  1828. grp.ScheduleGroupForFullUpdate();
  1829. }
  1830. }
  1831. public Border GetCrossedBorder(Vector3 position, Cardinals gridline)
  1832. {
  1833. if (BordersLocked)
  1834. {
  1835. switch (gridline)
  1836. {
  1837. case Cardinals.N:
  1838. lock (NorthBorders)
  1839. {
  1840. foreach (Border b in NorthBorders)
  1841. {
  1842. if (b.TestCross(position))
  1843. return b;
  1844. }
  1845. }
  1846. break;
  1847. case Cardinals.S:
  1848. lock (SouthBorders)
  1849. {
  1850. foreach (Border b in SouthBorders)
  1851. {
  1852. if (b.TestCross(position))
  1853. return b;
  1854. }
  1855. }
  1856. break;
  1857. case Cardinals.E:
  1858. lock (EastBorders)
  1859. {
  1860. foreach (Border b in EastBorders)
  1861. {
  1862. if (b.TestCross(position))
  1863. return b;
  1864. }
  1865. }
  1866. break;
  1867. case Cardinals.W:
  1868. lock (WestBorders)
  1869. {
  1870. foreach (Border b in WestBorders)
  1871. {
  1872. if (b.TestCross(position))
  1873. return b;
  1874. }
  1875. }
  1876. break;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. switch (gridline)
  1882. {
  1883. case Cardinals.N:
  1884. foreach (Border b in NorthBorders)
  1885. {
  1886. if (b.TestCross(position))
  1887. return b;
  1888. }
  1889. break;
  1890. case Cardinals.S:
  1891. foreach (Border b in SouthBorders)
  1892. {
  1893. if (b.TestCross(position))
  1894. return b;
  1895. }
  1896. break;
  1897. case Cardinals.E:
  1898. foreach (Border b in EastBorders)
  1899. {
  1900. if (b.TestCross(position))
  1901. return b;
  1902. }
  1903. break;
  1904. case Cardinals.W:
  1905. foreach (Border b in WestBorders)
  1906. {
  1907. if (b.TestCross(position))
  1908. return b;
  1909. }
  1910. break;
  1911. }
  1912. }
  1913. return null;
  1914. }
  1915. public bool TestBorderCross(Vector3 position, Cardinals border)
  1916. {
  1917. if (BordersLocked)
  1918. {
  1919. switch (border)
  1920. {
  1921. case Cardinals.N:
  1922. lock (NorthBorders)
  1923. {
  1924. foreach (Border b in NorthBorders)
  1925. {
  1926. if (b.TestCross(position))
  1927. return true;
  1928. }
  1929. }
  1930. break;
  1931. case Cardinals.E:
  1932. lock (EastBorders)
  1933. {
  1934. foreach (Border b in EastBorders)
  1935. {
  1936. if (b.TestCross(position))
  1937. return true;
  1938. }
  1939. }
  1940. break;
  1941. case Cardinals.S:
  1942. lock (SouthBorders)
  1943. {
  1944. foreach (Border b in SouthBorders)
  1945. {
  1946. if (b.TestCross(position))
  1947. return true;
  1948. }
  1949. }
  1950. break;
  1951. case Cardinals.W:
  1952. lock (WestBorders)
  1953. {
  1954. foreach (Border b in WestBorders)
  1955. {
  1956. if (b.TestCross(position))
  1957. return true;
  1958. }
  1959. }
  1960. break;
  1961. }
  1962. }
  1963. else
  1964. {
  1965. switch (border)
  1966. {
  1967. case Cardinals.N:
  1968. foreach (Border b in NorthBorders)
  1969. {
  1970. if (b.TestCross(position))
  1971. return true;
  1972. }
  1973. break;
  1974. case Cardinals.E:
  1975. foreach (Border b in EastBorders)
  1976. {
  1977. if (b.TestCross(position))
  1978. return true;
  1979. }
  1980. break;
  1981. case Cardinals.S:
  1982. foreach (Border b in SouthBorders)
  1983. {
  1984. if (b.TestCross(position))
  1985. return true;
  1986. }
  1987. break;
  1988. case Cardinals.W:
  1989. foreach (Border b in WestBorders)
  1990. {
  1991. if (b.TestCross(position))
  1992. return true;
  1993. }
  1994. break;
  1995. }
  1996. }
  1997. return false;
  1998. }
  1999. /// <summary>
  2000. /// Move the given scene object into a new region
  2001. /// </summary>
  2002. /// <param name="newRegionHandle"></param>
  2003. /// <param name="grp">Scene Object Group that we're crossing</param>
  2004. /// <returns>
  2005. /// true if the crossing itself was successful, false on failure
  2006. /// FIMXE: we still return true if the crossing object was not successfully deleted from the originating region
  2007. /// </returns>
  2008. public bool CrossPrimGroupIntoNewRegion(ulong newRegionHandle, SceneObjectGroup grp, bool silent)
  2009. {
  2010. //m_log.Debug(" >>> CrossPrimGroupIntoNewRegion <<<");
  2011. bool successYN = false;
  2012. grp.RootPart.UpdateFlag = 0;
  2013. //int primcrossingXMLmethod = 0;
  2014. if (newRegionHandle != 0)
  2015. {
  2016. //string objectState = grp.GetStateSnapshot();
  2017. //successYN
  2018. // = m_sceneGridService.PrimCrossToNeighboringRegion(
  2019. // newRegionHandle, grp.UUID, m_serialiser.SaveGroupToXml2(grp), primcrossingXMLmethod);
  2020. //if (successYN && (objectState != "") && m_allowScriptCrossings)
  2021. //{
  2022. // successYN = m_sceneGridService.PrimCrossToNeighboringRegion(
  2023. // newRegionHandle, grp.UUID, objectState, 100);
  2024. //}
  2025. // And the new channel...
  2026. if (m_interregionCommsOut != null)
  2027. successYN = m_interregionCommsOut.SendCreateObject(newRegionHandle, grp, true);
  2028. if (successYN)
  2029. {
  2030. // We remove the object here
  2031. try
  2032. {
  2033. DeleteSceneObject(grp, silent);
  2034. }
  2035. catch (Exception e)
  2036. {
  2037. m_log.ErrorFormat(
  2038. "[INTERREGION]: Exception deleting the old object left behind on a border crossing for {0}, {1}",
  2039. grp, e);
  2040. }
  2041. }
  2042. else
  2043. {
  2044. if (!grp.IsDeleted)
  2045. {
  2046. if (grp.RootPart.PhysActor != null)
  2047. {
  2048. grp.RootPart.PhysActor.CrossingFailure();
  2049. }
  2050. }
  2051. m_log.ErrorFormat("[INTERREGION]: Prim crossing failed for {0}", grp);
  2052. }
  2053. }
  2054. else
  2055. {
  2056. m_log.Error("[INTERREGION]: region handle was unexpectedly 0 in Scene.CrossPrimGroupIntoNewRegion()");
  2057. }
  2058. return successYN;
  2059. }
  2060. /// <summary>
  2061. /// Handle a scene object that is crossing into this region from another.
  2062. /// NOTE: Unused as of 2009-02-09. Soon to be deleted.
  2063. /// </summary>
  2064. /// <param name="regionHandle"></param>
  2065. /// <param name="primID"></param>
  2066. /// <param name="objXMLData"></param>
  2067. /// <param name="XMLMethod"></param>
  2068. /// <returns></returns>
  2069. public bool IncomingInterRegionPrimGroup(UUID primID, string objXMLData, int XMLMethod)
  2070. {
  2071. if (XMLMethod == 0)
  2072. {
  2073. m_log.DebugFormat("[INTERREGION]: A new prim {0} arrived from a neighbor", primID);
  2074. SceneObjectGroup sceneObject = m_serialiser.DeserializeGroupFromXml2(objXMLData);
  2075. if (sceneObject.IsAttachment)
  2076. sceneObject.RootPart.ObjectFlags |= (uint)PrimFlags.Phantom;
  2077. return AddSceneObject(sceneObject);
  2078. }
  2079. else if ((XMLMethod == 100) && m_allowScriptCrossings)
  2080. {
  2081. m_log.Warn("[INTERREGION]: Prim state data arrived from a neighbor");
  2082. XmlDocument doc = new XmlDocument();
  2083. doc.LoadXml(objXMLData);
  2084. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  2085. if (rootL.Count == 1)
  2086. {
  2087. XmlNode rootNode = rootL[0];
  2088. if (rootNode != null)
  2089. {
  2090. XmlNodeList partL = rootNode.ChildNodes;
  2091. foreach (XmlNode part in partL)
  2092. {
  2093. XmlNodeList nodeL = part.ChildNodes;
  2094. switch (part.Name)
  2095. {
  2096. case "Assemblies":
  2097. foreach (XmlNode asm in nodeL)
  2098. {
  2099. string fn = asm.Attributes.GetNamedItem("Filename").Value;
  2100. Byte[] filedata = Convert.FromBase64String(asm.InnerText);
  2101. string path = Path.Combine("ScriptEngines", RegionInfo.RegionID.ToString());
  2102. path = Path.Combine(path, fn);
  2103. if (!File.Exists(path))
  2104. {
  2105. FileStream fs = File.Create(path);
  2106. fs.Write(filedata, 0, filedata.Length);
  2107. fs.Close();
  2108. }
  2109. }
  2110. break;
  2111. case "ScriptStates":
  2112. foreach (XmlNode st in nodeL)
  2113. {
  2114. string id = st.Attributes.GetNamedItem("UUID").Value;
  2115. UUID uuid = new UUID(id);
  2116. XmlNode state = st.ChildNodes[0];
  2117. XmlDocument sdoc = new XmlDocument();
  2118. XmlNode sxmlnode = sdoc.CreateNode(
  2119. XmlNodeType.XmlDeclaration,
  2120. "", "");
  2121. sdoc.AppendChild(sxmlnode);
  2122. XmlNode newnode = sdoc.ImportNode(state, true);
  2123. sdoc.AppendChild(newnode);
  2124. string spath = Path.Combine("ScriptEngines", RegionInfo.RegionID.ToString());
  2125. spath = Path.Combine(spath, uuid.ToString());
  2126. FileStream sfs = File.Create(spath + ".state");
  2127. ASCIIEncoding enc = new ASCIIEncoding();
  2128. Byte[] buf = enc.GetBytes(sdoc.InnerXml);
  2129. sfs.Write(buf, 0, buf.Length);
  2130. sfs.Close();
  2131. }
  2132. break;
  2133. }
  2134. }
  2135. }
  2136. }
  2137. SceneObjectPart RootPrim = GetSceneObjectPart(primID);
  2138. RootPrim.ParentGroup.CreateScriptInstances(0, false, DefaultScriptEngine, 1);
  2139. return true;
  2140. }
  2141. return true;
  2142. }
  2143. public bool IncomingCreateObject(ISceneObject sog)
  2144. {
  2145. //m_log.Debug(" >>> IncomingCreateObject <<< " + ((SceneObjectGroup)sog).AbsolutePosition + " deleted? " + ((SceneObjectGroup)sog).IsDeleted);
  2146. SceneObjectGroup newObject;
  2147. try
  2148. {
  2149. newObject = (SceneObjectGroup)sog;
  2150. }
  2151. catch (Exception e)
  2152. {
  2153. m_log.WarnFormat("[SCENE]: Problem casting object: {0}", e.Message);
  2154. return false;
  2155. }
  2156. if (!AddSceneObject(newObject))
  2157. {
  2158. m_log.DebugFormat("[SCENE]: Problem adding scene object {0} in {1} ", sog.UUID, RegionInfo.RegionName);
  2159. return false;
  2160. }
  2161. newObject.RootPart.ParentGroup.CreateScriptInstances(0, false, DefaultScriptEngine, 1);
  2162. return true;
  2163. }
  2164. /// <summary>
  2165. /// Attachment rezzing
  2166. /// </summary>
  2167. /// <param name="userID">Agent Unique ID</param>
  2168. /// <param name="itemID">Object ID</param>
  2169. /// <returns>False</returns>
  2170. public virtual bool IncomingCreateObject(UUID userID, UUID itemID)
  2171. {
  2172. ScenePresence sp = GetScenePresence(userID);
  2173. if (sp != null)
  2174. {
  2175. uint attPt = (uint)sp.Appearance.GetAttachpoint(itemID);
  2176. m_sceneGraph.RezSingleAttachment(sp.ControllingClient, itemID, attPt);
  2177. }
  2178. return false;
  2179. }
  2180. /// <summary>
  2181. /// Adds a Scene Object group to the Scene.
  2182. /// Verifies that the creator of the object is not banned from the simulator.
  2183. /// Checks if the item is an Attachment
  2184. /// </summary>
  2185. /// <param name="sceneObject"></param>
  2186. /// <returns>True if the SceneObjectGroup was added, False if it was not</returns>
  2187. public bool AddSceneObject(SceneObjectGroup sceneObject)
  2188. {
  2189. // If the user is banned, we won't let any of their objects
  2190. // enter. Period.
  2191. //
  2192. if (m_regInfo.EstateSettings.IsBanned(sceneObject.OwnerID))
  2193. {
  2194. m_log.Info("[INTERREGION]: Denied prim crossing for " +
  2195. "banned avatar");
  2196. return false;
  2197. }
  2198. // Force allocation of new LocalId
  2199. //
  2200. foreach (SceneObjectPart p in sceneObject.Children.Values)
  2201. p.LocalId = 0;
  2202. if ((sceneObject.RootPart.Shape.PCode == (byte)PCode.Prim) && (sceneObject.RootPart.Shape.State != 0)) // Attachment
  2203. {
  2204. sceneObject.RootPart.AddFlag(PrimFlags.TemporaryOnRez);
  2205. sceneObject.RootPart.AddFlag(PrimFlags.Phantom);
  2206. AddRestoredSceneObject(sceneObject, false, false);
  2207. // Handle attachment special case
  2208. SceneObjectPart RootPrim = sceneObject.RootPart;
  2209. // Fix up attachment Parent Local ID
  2210. ScenePresence sp = GetScenePresence(sceneObject.OwnerID);
  2211. //uint parentLocalID = 0;
  2212. if (sp != null)
  2213. {
  2214. //parentLocalID = sp.LocalId;
  2215. //sceneObject.RootPart.IsAttachment = true;
  2216. //sceneObject.RootPart.SetParentLocalId(parentLocalID);
  2217. SceneObjectGroup grp = sceneObject;
  2218. //RootPrim.SetParentLocalId(parentLocalID);
  2219. m_log.DebugFormat("[ATTACHMENT]: Received " +
  2220. "attachment {0}, inworld asset id {1}",
  2221. //grp.RootPart.LastOwnerID.ToString(),
  2222. grp.GetFromItemID(),
  2223. grp.UUID.ToString());
  2224. //grp.SetFromAssetID(grp.RootPart.LastOwnerID);
  2225. m_log.DebugFormat("[ATTACHMENT]: Attach " +
  2226. "to avatar {0} at position {1}",
  2227. sp.UUID.ToString(), grp.AbsolutePosition);
  2228. AttachObject(sp.ControllingClient,
  2229. grp.LocalId, (uint)0,
  2230. grp.GroupRotation,
  2231. grp.AbsolutePosition, false);
  2232. RootPrim.RemFlag(PrimFlags.TemporaryOnRez);
  2233. grp.SendGroupFullUpdate();
  2234. }
  2235. else
  2236. {
  2237. RootPrim.RemFlag(PrimFlags.TemporaryOnRez);
  2238. RootPrim.AddFlag(PrimFlags.TemporaryOnRez);
  2239. }
  2240. }
  2241. else
  2242. {
  2243. AddRestoredSceneObject(sceneObject, true, false);
  2244. if (!Permissions.CanObjectEntry(sceneObject.UUID,
  2245. true, sceneObject.AbsolutePosition))
  2246. {
  2247. // Deny non attachments based on parcel settings
  2248. //
  2249. m_log.Info("[INTERREGION]: Denied prim crossing " +
  2250. "because of parcel settings");
  2251. DeleteSceneObject(sceneObject, false);
  2252. return false;
  2253. }
  2254. }
  2255. return true;
  2256. }
  2257. #endregion
  2258. #region Add/Remove Avatar Methods
  2259. /// <summary>
  2260. /// Adding a New Client and Create a Presence for it.
  2261. /// </summary>
  2262. /// <param name="client"></param>
  2263. public override void AddNewClient(IClientAPI client)
  2264. {
  2265. m_clientManager.Add(client);
  2266. CheckHeartbeat();
  2267. SubscribeToClientEvents(client);
  2268. ScenePresence presence;
  2269. if (m_restorePresences.ContainsKey(client.AgentId))
  2270. {
  2271. m_log.DebugFormat("[SCENE]: Restoring agent {0} {1} in {2}", client.Name, client.AgentId, RegionInfo.RegionName);
  2272. presence = m_restorePresences[client.AgentId];
  2273. m_restorePresences.Remove(client.AgentId);
  2274. // This is one of two paths to create avatars that are
  2275. // used. This tends to get called more in standalone
  2276. // than grid, not really sure why, but as such needs
  2277. // an explicity appearance lookup here.
  2278. AvatarAppearance appearance = null;
  2279. GetAvatarAppearance(client, out appearance);
  2280. presence.Appearance = appearance;
  2281. presence.initializeScenePresence(client, RegionInfo, this);
  2282. m_sceneGraph.AddScenePresence(presence);
  2283. lock (m_restorePresences)
  2284. {
  2285. Monitor.PulseAll(m_restorePresences);
  2286. }
  2287. }
  2288. else
  2289. {
  2290. AgentCircuitData aCircuit = m_authenticateHandler.GetAgentCircuitData(client.CircuitCode);
  2291. m_log.Debug("[Scene] Adding new agent " + client.Name + " to scene " + RegionInfo.RegionName);
  2292. /*
  2293. string logMsg = string.Format("[SCENE]: Adding new {0} agent for {1} in {2}",
  2294. ((aCircuit.child == true) ? "child" : "root"), client.Name,
  2295. RegionInfo.RegionName);
  2296. m_log.Debug(logMsg);
  2297. */
  2298. CommsManager.UserProfileCacheService.AddNewUser(client.AgentId);
  2299. ScenePresence sp = CreateAndAddScenePresence(client);
  2300. // HERE!!! Do the initial attachments right here
  2301. // first agent upon login is a root agent by design.
  2302. // All other AddNewClient calls find aCircuit.child to be true
  2303. if (aCircuit == null || aCircuit.child == false)
  2304. {
  2305. sp.IsChildAgent = false;
  2306. Util.FireAndForget(delegate(object o) { sp.RezAttachments(); });
  2307. }
  2308. }
  2309. m_LastLogin = Environment.TickCount;
  2310. EventManager.TriggerOnNewClient(client);
  2311. }
  2312. /// <summary>
  2313. /// Register for events from the client
  2314. /// </summary>
  2315. /// <param name="client">The IClientAPI of the connected client</param>
  2316. public virtual void SubscribeToClientEvents(IClientAPI client)
  2317. {
  2318. SubscribeToClientTerrainEvents(client);
  2319. SubscribeToClientPrimEvents(client);
  2320. SubscribeToClientPrimRezEvents(client);
  2321. SubscribeToClientInventoryEvents(client);
  2322. SubscribeToClientAttachmentEvents(client);
  2323. SubscribeToClientTeleportEvents(client);
  2324. SubscribeToClientScriptEvents(client);
  2325. SubscribeToClientParcelEvents(client);
  2326. SubscribeToClientGridEvents(client);
  2327. SubscribeToClientGodEvents(client);
  2328. SubscribeToClientNetworkEvents(client);
  2329. // EventManager.TriggerOnNewClient(client);
  2330. }
  2331. public virtual void SubscribeToClientTerrainEvents(IClientAPI client)
  2332. {
  2333. client.OnRegionHandShakeReply += SendLayerData;
  2334. client.OnUnackedTerrain += TerrainUnAcked;
  2335. }
  2336. public virtual void SubscribeToClientPrimEvents(IClientAPI client)
  2337. {
  2338. client.OnUpdatePrimGroupPosition += m_sceneGraph.UpdatePrimPosition;
  2339. client.OnUpdatePrimSinglePosition += m_sceneGraph.UpdatePrimSinglePosition;
  2340. client.OnUpdatePrimGroupRotation += m_sceneGraph.UpdatePrimRotation;
  2341. client.OnUpdatePrimGroupMouseRotation += m_sceneGraph.UpdatePrimRotation;
  2342. client.OnUpdatePrimSingleRotation += m_sceneGraph.UpdatePrimSingleRotation;
  2343. client.OnUpdatePrimSingleRotationPosition += m_sceneGraph.UpdatePrimSingleRotationPosition;
  2344. client.OnUpdatePrimScale += m_sceneGraph.UpdatePrimScale;
  2345. client.OnUpdatePrimGroupScale += m_sceneGraph.UpdatePrimGroupScale;
  2346. client.OnUpdateExtraParams += m_sceneGraph.UpdateExtraParam;
  2347. client.OnUpdatePrimShape += m_sceneGraph.UpdatePrimShape;
  2348. client.OnUpdatePrimTexture += m_sceneGraph.UpdatePrimTexture;
  2349. client.OnObjectRequest += RequestPrim;
  2350. client.OnObjectSelect += SelectPrim;
  2351. client.OnObjectDeselect += DeselectPrim;
  2352. client.OnGrabUpdate += m_sceneGraph.MoveObject;
  2353. client.OnSpinStart += m_sceneGraph.SpinStart;
  2354. client.OnSpinUpdate += m_sceneGraph.SpinObject;
  2355. client.OnDeRezObject += DeRezObject;
  2356. client.OnObjectName += m_sceneGraph.PrimName;
  2357. client.OnObjectClickAction += m_sceneGraph.PrimClickAction;
  2358. client.OnObjectMaterial += m_sceneGraph.PrimMaterial;
  2359. client.OnLinkObjects += m_sceneGraph.LinkObjects;
  2360. client.OnDelinkObjects += m_sceneGraph.DelinkObjects;
  2361. client.OnObjectDuplicate += m_sceneGraph.DuplicateObject;
  2362. client.OnObjectDuplicateOnRay += doObjectDuplicateOnRay;
  2363. client.OnUpdatePrimFlags += m_sceneGraph.UpdatePrimFlags;
  2364. client.OnRequestObjectPropertiesFamily += m_sceneGraph.RequestObjectPropertiesFamily;
  2365. client.OnObjectPermissions += HandleObjectPermissionsUpdate;
  2366. client.OnGrabObject += ProcessObjectGrab;
  2367. client.OnDeGrabObject += ProcessObjectDeGrab;
  2368. client.OnUndo += m_sceneGraph.HandleUndo;
  2369. client.OnObjectDescription += m_sceneGraph.PrimDescription;
  2370. client.OnObjectDrop += m_sceneGraph.DropObject;
  2371. client.OnObjectSaleInfo += ObjectSaleInfo;
  2372. client.OnObjectIncludeInSearch += m_sceneGraph.MakeObjectSearchable;
  2373. client.OnObjectOwner += ObjectOwner;
  2374. }
  2375. public virtual void SubscribeToClientPrimRezEvents(IClientAPI client)
  2376. {
  2377. client.OnAddPrim += AddNewPrim;
  2378. client.OnRezObject += RezObject;
  2379. }
  2380. public virtual void SubscribeToClientInventoryEvents(IClientAPI client)
  2381. {
  2382. client.OnCreateNewInventoryItem += CreateNewInventoryItem;
  2383. client.OnCreateNewInventoryFolder += HandleCreateInventoryFolder;
  2384. client.OnUpdateInventoryFolder += HandleUpdateInventoryFolder;
  2385. client.OnMoveInventoryFolder += HandleMoveInventoryFolder; // 2; //!!
  2386. client.OnFetchInventoryDescendents += HandleFetchInventoryDescendents;
  2387. client.OnPurgeInventoryDescendents += HandlePurgeInventoryDescendents; // 2; //!!
  2388. client.OnFetchInventory += HandleFetchInventory;
  2389. client.OnUpdateInventoryItem += UpdateInventoryItemAsset;
  2390. client.OnCopyInventoryItem += CopyInventoryItem;
  2391. client.OnMoveInventoryItem += MoveInventoryItem;
  2392. client.OnRemoveInventoryItem += RemoveInventoryItem;
  2393. client.OnRemoveInventoryFolder += RemoveInventoryFolder;
  2394. client.OnRezScript += RezScript;
  2395. client.OnRequestTaskInventory += RequestTaskInventory;
  2396. client.OnRemoveTaskItem += RemoveTaskInventory;
  2397. client.OnUpdateTaskInventory += UpdateTaskInventory;
  2398. client.OnMoveTaskItem += ClientMoveTaskInventoryItem;
  2399. }
  2400. public virtual void SubscribeToClientAttachmentEvents(IClientAPI client)
  2401. {
  2402. client.OnRezSingleAttachmentFromInv += RezSingleAttachment;
  2403. client.OnRezMultipleAttachmentsFromInv += RezMultipleAttachments;
  2404. client.OnDetachAttachmentIntoInv += DetachSingleAttachmentToInv;
  2405. client.OnObjectAttach += m_sceneGraph.AttachObject;
  2406. client.OnObjectDetach += m_sceneGraph.DetachObject;
  2407. }
  2408. public virtual void SubscribeToClientTeleportEvents(IClientAPI client)
  2409. {
  2410. client.OnTeleportLocationRequest += RequestTeleportLocation;
  2411. client.OnTeleportLandmarkRequest += RequestTeleportLandmark;
  2412. client.OnTeleportHomeRequest += TeleportClientHome;
  2413. }
  2414. public virtual void SubscribeToClientScriptEvents(IClientAPI client)
  2415. {
  2416. client.OnScriptReset += ProcessScriptReset;
  2417. client.OnGetScriptRunning += GetScriptRunning;
  2418. client.OnSetScriptRunning += SetScriptRunning;
  2419. }
  2420. public virtual void SubscribeToClientParcelEvents(IClientAPI client)
  2421. {
  2422. client.OnObjectGroupRequest += m_sceneGraph.HandleObjectGroupUpdate;
  2423. client.OnParcelReturnObjectsRequest += LandChannel.ReturnObjectsInParcel;
  2424. client.OnParcelSetOtherCleanTime += LandChannel.SetParcelOtherCleanTime;
  2425. client.OnParcelBuy += ProcessParcelBuy;
  2426. }
  2427. public virtual void SubscribeToClientGridEvents(IClientAPI client)
  2428. {
  2429. client.OnNameFromUUIDRequest += CommsManager.HandleUUIDNameRequest;
  2430. client.OnMoneyTransferRequest += ProcessMoneyTransferRequest;
  2431. client.OnAvatarPickerRequest += ProcessAvatarPickerRequest;
  2432. client.OnSetStartLocationRequest += SetHomeRezPoint;
  2433. client.OnRegionHandleRequest += RegionHandleRequest;
  2434. }
  2435. public virtual void SubscribeToClientGodEvents(IClientAPI client)
  2436. {
  2437. IGodsModule godsModule = RequestModuleInterface<IGodsModule>();
  2438. client.OnGodKickUser += godsModule.KickUser;
  2439. client.OnRequestGodlikePowers += godsModule.RequestGodlikePowers;
  2440. }
  2441. public virtual void SubscribeToClientNetworkEvents(IClientAPI client)
  2442. {
  2443. client.OnNetworkStatsUpdate += StatsReporter.AddPacketsStats;
  2444. client.OnViewerEffect += ProcessViewerEffect;
  2445. }
  2446. protected virtual void UnsubscribeToClientEvents(IClientAPI client)
  2447. {
  2448. }
  2449. /// <summary>
  2450. /// Register for events from the client
  2451. /// </summary>
  2452. /// <param name="client">The IClientAPI of the connected client</param>
  2453. public virtual void UnSubscribeToClientEvents(IClientAPI client)
  2454. {
  2455. UnSubscribeToClientTerrainEvents(client);
  2456. UnSubscribeToClientPrimEvents(client);
  2457. UnSubscribeToClientPrimRezEvents(client);
  2458. UnSubscribeToClientInventoryEvents(client);
  2459. UnSubscribeToClientAttachmentEvents(client);
  2460. UnSubscribeToClientTeleportEvents(client);
  2461. UnSubscribeToClientScriptEvents(client);
  2462. UnSubscribeToClientParcelEvents(client);
  2463. UnSubscribeToClientGridEvents(client);
  2464. UnSubscribeToClientGodEvents(client);
  2465. UnSubscribeToClientNetworkEvents(client);
  2466. // EventManager.TriggerOnNewClient(client);
  2467. }
  2468. public virtual void UnSubscribeToClientTerrainEvents(IClientAPI client)
  2469. {
  2470. client.OnRegionHandShakeReply -= SendLayerData;
  2471. client.OnUnackedTerrain -= TerrainUnAcked;
  2472. }
  2473. public virtual void UnSubscribeToClientPrimEvents(IClientAPI client)
  2474. {
  2475. client.OnUpdatePrimGroupPosition -= m_sceneGraph.UpdatePrimPosition;
  2476. client.OnUpdatePrimSinglePosition -= m_sceneGraph.UpdatePrimSinglePosition;
  2477. client.OnUpdatePrimGroupRotation -= m_sceneGraph.UpdatePrimRotation;
  2478. client.OnUpdatePrimGroupMouseRotation -= m_sceneGraph.UpdatePrimRotation;
  2479. client.OnUpdatePrimSingleRotation -= m_sceneGraph.UpdatePrimSingleRotation;
  2480. client.OnUpdatePrimSingleRotationPosition -= m_sceneGraph.UpdatePrimSingleRotationPosition;
  2481. client.OnUpdatePrimScale -= m_sceneGraph.UpdatePrimScale;
  2482. client.OnUpdatePrimGroupScale -= m_sceneGraph.UpdatePrimGroupScale;
  2483. client.OnUpdateExtraParams -= m_sceneGraph.UpdateExtraParam;
  2484. client.OnUpdatePrimShape -= m_sceneGraph.UpdatePrimShape;
  2485. client.OnUpdatePrimTexture -= m_sceneGraph.UpdatePrimTexture;
  2486. client.OnObjectRequest -= RequestPrim;
  2487. client.OnObjectSelect -= SelectPrim;
  2488. client.OnObjectDeselect -= DeselectPrim;
  2489. client.OnGrabUpdate -= m_sceneGraph.MoveObject;
  2490. client.OnSpinStart -= m_sceneGraph.SpinStart;
  2491. client.OnSpinUpdate -= m_sceneGraph.SpinObject;
  2492. client.OnDeRezObject -= DeRezObject;
  2493. client.OnObjectName -= m_sceneGraph.PrimName;
  2494. client.OnObjectClickAction -= m_sceneGraph.PrimClickAction;
  2495. client.OnObjectMaterial -= m_sceneGraph.PrimMaterial;
  2496. client.OnLinkObjects -= m_sceneGraph.LinkObjects;
  2497. client.OnDelinkObjects -= m_sceneGraph.DelinkObjects;
  2498. client.OnObjectDuplicate -= m_sceneGraph.DuplicateObject;
  2499. client.OnObjectDuplicateOnRay -= doObjectDuplicateOnRay;
  2500. client.OnUpdatePrimFlags -= m_sceneGraph.UpdatePrimFlags;
  2501. client.OnRequestObjectPropertiesFamily -= m_sceneGraph.RequestObjectPropertiesFamily;
  2502. client.OnObjectPermissions -= HandleObjectPermissionsUpdate;
  2503. client.OnGrabObject -= ProcessObjectGrab;
  2504. client.OnDeGrabObject -= ProcessObjectDeGrab;
  2505. client.OnUndo -= m_sceneGraph.HandleUndo;
  2506. client.OnObjectDescription -= m_sceneGraph.PrimDescription;
  2507. client.OnObjectDrop -= m_sceneGraph.DropObject;
  2508. client.OnObjectSaleInfo -= ObjectSaleInfo;
  2509. client.OnObjectIncludeInSearch -= m_sceneGraph.MakeObjectSearchable;
  2510. client.OnObjectOwner -= ObjectOwner;
  2511. }
  2512. public virtual void UnSubscribeToClientPrimRezEvents(IClientAPI client)
  2513. {
  2514. client.OnAddPrim -= AddNewPrim;
  2515. client.OnRezObject -= RezObject;
  2516. }
  2517. public virtual void UnSubscribeToClientInventoryEvents(IClientAPI client)
  2518. {
  2519. client.OnCreateNewInventoryItem -= CreateNewInventoryItem;
  2520. client.OnCreateNewInventoryFolder -= HandleCreateInventoryFolder;
  2521. client.OnUpdateInventoryFolder -= HandleUpdateInventoryFolder;
  2522. client.OnMoveInventoryFolder -= HandleMoveInventoryFolder; // 2; //!!
  2523. client.OnFetchInventoryDescendents -= HandleFetchInventoryDescendents;
  2524. client.OnPurgeInventoryDescendents -= HandlePurgeInventoryDescendents; // 2; //!!
  2525. client.OnFetchInventory -= HandleFetchInventory;
  2526. client.OnUpdateInventoryItem -= UpdateInventoryItemAsset;
  2527. client.OnCopyInventoryItem -= CopyInventoryItem;
  2528. client.OnMoveInventoryItem -= MoveInventoryItem;
  2529. client.OnRemoveInventoryItem -= RemoveInventoryItem;
  2530. client.OnRemoveInventoryFolder -= RemoveInventoryFolder;
  2531. client.OnRezScript -= RezScript;
  2532. client.OnRequestTaskInventory -= RequestTaskInventory;
  2533. client.OnRemoveTaskItem -= RemoveTaskInventory;
  2534. client.OnUpdateTaskInventory -= UpdateTaskInventory;
  2535. client.OnMoveTaskItem -= ClientMoveTaskInventoryItem;
  2536. }
  2537. public virtual void UnSubscribeToClientAttachmentEvents(IClientAPI client)
  2538. {
  2539. client.OnRezSingleAttachmentFromInv -= RezSingleAttachment;
  2540. client.OnRezMultipleAttachmentsFromInv -= RezMultipleAttachments;
  2541. client.OnDetachAttachmentIntoInv -= DetachSingleAttachmentToInv;
  2542. client.OnObjectAttach -= m_sceneGraph.AttachObject;
  2543. client.OnObjectDetach -= m_sceneGraph.DetachObject;
  2544. }
  2545. public virtual void UnSubscribeToClientTeleportEvents(IClientAPI client)
  2546. {
  2547. client.OnTeleportLocationRequest -= RequestTeleportLocation;
  2548. client.OnTeleportLandmarkRequest -= RequestTeleportLandmark;
  2549. client.OnTeleportHomeRequest -= TeleportClientHome;
  2550. }
  2551. public virtual void UnSubscribeToClientScriptEvents(IClientAPI client)
  2552. {
  2553. client.OnScriptReset -= ProcessScriptReset;
  2554. client.OnGetScriptRunning -= GetScriptRunning;
  2555. client.OnSetScriptRunning -= SetScriptRunning;
  2556. }
  2557. public virtual void UnSubscribeToClientParcelEvents(IClientAPI client)
  2558. {
  2559. client.OnObjectGroupRequest -= m_sceneGraph.HandleObjectGroupUpdate;
  2560. client.OnParcelReturnObjectsRequest -= LandChannel.ReturnObjectsInParcel;
  2561. client.OnParcelSetOtherCleanTime -= LandChannel.SetParcelOtherCleanTime;
  2562. client.OnParcelBuy -= ProcessParcelBuy;
  2563. }
  2564. public virtual void UnSubscribeToClientGridEvents(IClientAPI client)
  2565. {
  2566. client.OnNameFromUUIDRequest -= CommsManager.HandleUUIDNameRequest;
  2567. client.OnMoneyTransferRequest -= ProcessMoneyTransferRequest;
  2568. client.OnAvatarPickerRequest -= ProcessAvatarPickerRequest;
  2569. client.OnSetStartLocationRequest -= SetHomeRezPoint;
  2570. client.OnRegionHandleRequest -= RegionHandleRequest;
  2571. }
  2572. public virtual void UnSubscribeToClientGodEvents(IClientAPI client)
  2573. {
  2574. IGodsModule godsModule = RequestModuleInterface<IGodsModule>();
  2575. client.OnGodKickUser -= godsModule.KickUser;
  2576. client.OnRequestGodlikePowers -= godsModule.RequestGodlikePowers;
  2577. }
  2578. public virtual void UnSubscribeToClientNetworkEvents(IClientAPI client)
  2579. {
  2580. client.OnNetworkStatsUpdate -= StatsReporter.AddPacketsStats;
  2581. client.OnViewerEffect -= ProcessViewerEffect;
  2582. }
  2583. /// <summary>
  2584. /// Teleport an avatar to their home region
  2585. /// </summary>
  2586. /// <param name="agentId">The avatar's Unique ID</param>
  2587. /// <param name="client">The IClientAPI for the client</param>
  2588. public virtual void TeleportClientHome(UUID agentId, IClientAPI client)
  2589. {
  2590. UserProfileData UserProfile = CommsManager.UserService.GetUserProfile(agentId);
  2591. if (UserProfile != null)
  2592. {
  2593. GridRegion regionInfo = GridService.GetRegionByUUID(UUID.Zero, UserProfile.HomeRegionID);
  2594. if (regionInfo == null)
  2595. {
  2596. uint x = 0, y = 0;
  2597. Utils.LongToUInts(UserProfile.HomeRegion, out x, out y);
  2598. regionInfo = GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  2599. if (regionInfo != null) // home region can be away temporarily, too
  2600. {
  2601. UserProfile.HomeRegionID = regionInfo.RegionID;
  2602. CommsManager.UserService.UpdateUserProfile(UserProfile);
  2603. }
  2604. }
  2605. if (regionInfo == null)
  2606. {
  2607. // can't find the Home region: Tell viewer and abort
  2608. client.SendTeleportFailed("Your home-region could not be found.");
  2609. return;
  2610. }
  2611. RequestTeleportLocation(
  2612. client, regionInfo.RegionHandle, UserProfile.HomeLocation, UserProfile.HomeLookAt,
  2613. (uint)(TPFlags.SetLastToTarget | TPFlags.ViaHome));
  2614. }
  2615. }
  2616. /// <summary>
  2617. /// Duplicates object specified by localID at position raycasted against RayTargetObject using
  2618. /// RayEnd and RayStart to determine what the angle of the ray is
  2619. /// </summary>
  2620. /// <param name="localID">ID of object to duplicate</param>
  2621. /// <param name="dupeFlags"></param>
  2622. /// <param name="AgentID">Agent doing the duplication</param>
  2623. /// <param name="GroupID">Group of new object</param>
  2624. /// <param name="RayTargetObj">The target of the Ray</param>
  2625. /// <param name="RayEnd">The ending of the ray (farthest away point)</param>
  2626. /// <param name="RayStart">The Beginning of the ray (closest point)</param>
  2627. /// <param name="BypassRaycast">Bool to bypass raycasting</param>
  2628. /// <param name="RayEndIsIntersection">The End specified is the place to add the object</param>
  2629. /// <param name="CopyCenters">Position the object at the center of the face that it's colliding with</param>
  2630. /// <param name="CopyRotates">Rotate the object the same as the localID object</param>
  2631. public void doObjectDuplicateOnRay(uint localID, uint dupeFlags, UUID AgentID, UUID GroupID,
  2632. UUID RayTargetObj, Vector3 RayEnd, Vector3 RayStart,
  2633. bool BypassRaycast, bool RayEndIsIntersection, bool CopyCenters, bool CopyRotates)
  2634. {
  2635. Vector3 pos;
  2636. const bool frontFacesOnly = true;
  2637. //m_log.Info("HITTARGET: " + RayTargetObj.ToString() + ", COPYTARGET: " + localID.ToString());
  2638. SceneObjectPart target = GetSceneObjectPart(localID);
  2639. SceneObjectPart target2 = GetSceneObjectPart(RayTargetObj);
  2640. if (target != null && target2 != null)
  2641. {
  2642. Vector3 direction = Vector3.Normalize(RayEnd - RayStart);
  2643. Vector3 AXOrigin = new Vector3(RayStart.X, RayStart.Y, RayStart.Z);
  2644. Vector3 AXdirection = new Vector3(direction.X, direction.Y, direction.Z);
  2645. if (target2.ParentGroup != null)
  2646. {
  2647. pos = target2.AbsolutePosition;
  2648. //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());
  2649. // TODO: Raytrace better here
  2650. //EntityIntersection ei = m_sceneGraph.GetClosestIntersectingPrim(new Ray(AXOrigin, AXdirection));
  2651. Ray NewRay = new Ray(AXOrigin, AXdirection);
  2652. // Ray Trace against target here
  2653. EntityIntersection ei = target2.TestIntersectionOBB(NewRay, Quaternion.Identity, frontFacesOnly, CopyCenters);
  2654. // Un-comment out the following line to Get Raytrace results printed to the console.
  2655. //m_log.Info("[RAYTRACERESULTS]: Hit:" + ei.HitTF.ToString() + " Point: " + ei.ipoint.ToString() + " Normal: " + ei.normal.ToString());
  2656. float ScaleOffset = 0.5f;
  2657. // If we hit something
  2658. if (ei.HitTF)
  2659. {
  2660. Vector3 scale = target.Scale;
  2661. Vector3 scaleComponent = new Vector3(ei.AAfaceNormal.X, ei.AAfaceNormal.Y, ei.AAfaceNormal.Z);
  2662. if (scaleComponent.X != 0) ScaleOffset = scale.X;
  2663. if (scaleComponent.Y != 0) ScaleOffset = scale.Y;
  2664. if (scaleComponent.Z != 0) ScaleOffset = scale.Z;
  2665. ScaleOffset = Math.Abs(ScaleOffset);
  2666. Vector3 intersectionpoint = new Vector3(ei.ipoint.X, ei.ipoint.Y, ei.ipoint.Z);
  2667. Vector3 normal = new Vector3(ei.normal.X, ei.normal.Y, ei.normal.Z);
  2668. Vector3 offset = normal * (ScaleOffset / 2f);
  2669. pos = intersectionpoint + offset;
  2670. // stick in offset format from the original prim
  2671. pos = pos - target.ParentGroup.AbsolutePosition;
  2672. if (CopyRotates)
  2673. {
  2674. Quaternion worldRot = target2.GetWorldRotation();
  2675. // SceneObjectGroup obj = m_sceneGraph.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID, worldRot);
  2676. m_sceneGraph.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID, worldRot);
  2677. //obj.Rotation = worldRot;
  2678. //obj.UpdateGroupRotationR(worldRot);
  2679. }
  2680. else
  2681. {
  2682. m_sceneGraph.DuplicateObject(localID, pos, target.GetEffectiveObjectFlags(), AgentID, GroupID);
  2683. }
  2684. }
  2685. return;
  2686. }
  2687. return;
  2688. }
  2689. }
  2690. /// <summary>
  2691. /// Sets the Home Point. The GridService uses this to know where to put a user when they log-in
  2692. /// </summary>
  2693. /// <param name="remoteClient"></param>
  2694. /// <param name="regionHandle"></param>
  2695. /// <param name="position"></param>
  2696. /// <param name="lookAt"></param>
  2697. /// <param name="flags"></param>
  2698. public virtual void SetHomeRezPoint(IClientAPI remoteClient, ulong regionHandle, Vector3 position, Vector3 lookAt, uint flags)
  2699. {
  2700. UserProfileData UserProfile = CommsManager.UserService.GetUserProfile(remoteClient.AgentId);
  2701. if (UserProfile != null)
  2702. {
  2703. // I know I'm ignoring the regionHandle provided by the teleport location request.
  2704. // reusing the TeleportLocationRequest delegate, so regionHandle isn't valid
  2705. UserProfile.HomeRegionID = RegionInfo.RegionID;
  2706. // TODO: The next line can be removed, as soon as only homeRegionID based UserServers are around.
  2707. // TODO: The HomeRegion property can be removed then, too
  2708. UserProfile.HomeRegion = RegionInfo.RegionHandle;
  2709. UserProfile.HomeLocation = position;
  2710. UserProfile.HomeLookAt = lookAt;
  2711. CommsManager.UserService.UpdateUserProfile(UserProfile);
  2712. // FUBAR ALERT: this needs to be "Home position set." so the viewer saves a home-screenshot.
  2713. m_dialogModule.SendAlertToUser(remoteClient, "Home position set.");
  2714. }
  2715. else
  2716. {
  2717. m_dialogModule.SendAlertToUser(remoteClient, "Set Home request Failed.");
  2718. }
  2719. }
  2720. /// <summary>
  2721. /// Create a child agent scene presence and add it to this scene.
  2722. /// </summary>
  2723. /// <param name="client"></param>
  2724. /// <returns></returns>
  2725. protected virtual ScenePresence CreateAndAddScenePresence(IClientAPI client)
  2726. {
  2727. CheckHeartbeat();
  2728. AvatarAppearance appearance = null;
  2729. GetAvatarAppearance(client, out appearance);
  2730. ScenePresence avatar = m_sceneGraph.CreateAndAddChildScenePresence(client, appearance);
  2731. //avatar.KnownRegions = GetChildrenSeeds(avatar.UUID);
  2732. m_eventManager.TriggerOnNewPresence(avatar);
  2733. return avatar;
  2734. }
  2735. /// <summary>
  2736. /// Get the avatar apperance for the given client.
  2737. /// </summary>
  2738. /// <param name="client"></param>
  2739. /// <param name="appearance"></param>
  2740. public void GetAvatarAppearance(IClientAPI client, out AvatarAppearance appearance)
  2741. {
  2742. AgentCircuitData aCircuit = m_authenticateHandler.GetAgentCircuitData(client.CircuitCode);
  2743. if (aCircuit == null)
  2744. {
  2745. m_log.DebugFormat("[APPEARANCE] Client did not supply a circuit. Non-Linden? Creating default appearance.");
  2746. appearance = new AvatarAppearance(client.AgentId);
  2747. return;
  2748. }
  2749. appearance = aCircuit.Appearance;
  2750. if (appearance == null)
  2751. {
  2752. m_log.DebugFormat("[APPEARANCE]: Appearance not found in {0}, returning default", RegionInfo.RegionName);
  2753. appearance = new AvatarAppearance(client.AgentId);
  2754. }
  2755. }
  2756. /// <summary>
  2757. /// Remove the given client from the scene.
  2758. /// </summary>
  2759. /// <param name="agentID"></param>
  2760. public override void RemoveClient(UUID agentID)
  2761. {
  2762. CheckHeartbeat();
  2763. bool childagentYN = false;
  2764. ScenePresence avatar = GetScenePresence(agentID);
  2765. if (avatar != null)
  2766. {
  2767. childagentYN = avatar.IsChildAgent;
  2768. if (avatar.ParentID != 0)
  2769. {
  2770. avatar.StandUp();
  2771. }
  2772. try
  2773. {
  2774. m_log.DebugFormat(
  2775. "[SCENE]: Removing {0} agent {1} from region {2}",
  2776. (childagentYN ? "child" : "root"), agentID, RegionInfo.RegionName);
  2777. m_sceneGraph.removeUserCount(!childagentYN);
  2778. CapsModule.RemoveCapsHandler(agentID);
  2779. if (avatar.Scene.NeedSceneCacheClear(avatar.UUID))
  2780. {
  2781. CommsManager.UserProfileCacheService.RemoveUser(agentID);
  2782. }
  2783. if (!avatar.IsChildAgent)
  2784. {
  2785. m_sceneGridService.LogOffUser(agentID, RegionInfo.RegionID, RegionInfo.RegionHandle, avatar.AbsolutePosition, avatar.Lookat);
  2786. //List<ulong> childknownRegions = new List<ulong>();
  2787. //List<ulong> ckn = avatar.KnownChildRegionHandles;
  2788. //for (int i = 0; i < ckn.Count; i++)
  2789. //{
  2790. // childknownRegions.Add(ckn[i]);
  2791. //}
  2792. List<ulong> regions = new List<ulong>(avatar.KnownChildRegionHandles);
  2793. regions.Remove(RegionInfo.RegionHandle);
  2794. m_sceneGridService.SendCloseChildAgentConnections(agentID, regions);
  2795. }
  2796. m_eventManager.TriggerClientClosed(agentID, this);
  2797. }
  2798. catch (NullReferenceException)
  2799. {
  2800. // We don't know which count to remove it from
  2801. // Avatar is already disposed :/
  2802. }
  2803. m_eventManager.TriggerOnRemovePresence(agentID);
  2804. ForEachClient(
  2805. delegate(IClientAPI client)
  2806. {
  2807. //We can safely ignore null reference exceptions. It means the avatar is dead and cleaned up anyway
  2808. try { client.SendKillObject(avatar.RegionHandle, avatar.LocalId); }
  2809. catch (NullReferenceException) { }
  2810. });
  2811. ForEachScenePresence(
  2812. delegate(ScenePresence presence) { presence.CoarseLocationChange(); });
  2813. IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
  2814. if (agentTransactions != null)
  2815. {
  2816. agentTransactions.RemoveAgentAssetTransactions(agentID);
  2817. }
  2818. // Remove the avatar from the scene
  2819. m_sceneGraph.RemoveScenePresence(agentID);
  2820. m_clientManager.Remove(agentID);
  2821. try
  2822. {
  2823. avatar.Close();
  2824. }
  2825. catch (NullReferenceException)
  2826. {
  2827. //We can safely ignore null reference exceptions. It means the avatar are dead and cleaned up anyway.
  2828. }
  2829. catch (Exception e)
  2830. {
  2831. m_log.Error("[SCENE] Scene.cs:RemoveClient exception: " + e.ToString());
  2832. }
  2833. // Remove client agent from profile, so new logins will work
  2834. if (!childagentYN)
  2835. {
  2836. m_sceneGridService.ClearUserAgent(agentID);
  2837. }
  2838. m_authenticateHandler.RemoveCircuit(avatar.ControllingClient.CircuitCode);
  2839. //m_log.InfoFormat("[SCENE] Memory pre GC {0}", System.GC.GetTotalMemory(false));
  2840. //m_log.InfoFormat("[SCENE] Memory post GC {0}", System.GC.GetTotalMemory(true));
  2841. }
  2842. }
  2843. /// <summary>
  2844. /// Removes region from an avatar's known region list. This coincides with child agents. For each child agent, there will be a known region entry.
  2845. ///
  2846. /// </summary>
  2847. /// <param name="avatarID"></param>
  2848. /// <param name="regionslst"></param>
  2849. public void HandleRemoveKnownRegionsFromAvatar(UUID avatarID, List<ulong> regionslst)
  2850. {
  2851. ScenePresence av = GetScenePresence(avatarID);
  2852. if (av != null)
  2853. {
  2854. lock (av)
  2855. {
  2856. for (int i = 0; i < regionslst.Count; i++)
  2857. {
  2858. av.KnownChildRegionHandles.Remove(regionslst[i]);
  2859. }
  2860. }
  2861. }
  2862. }
  2863. /// <summary>
  2864. /// Inform all other ScenePresences on this Scene that someone else has changed position on the minimap.
  2865. /// </summary>
  2866. public void NotifyMyCoarseLocationChange()
  2867. {
  2868. ForEachScenePresence(delegate(ScenePresence presence) { presence.CoarseLocationChange(); });
  2869. }
  2870. #endregion
  2871. #region Entities
  2872. public void SendKillObject(uint localID)
  2873. {
  2874. SceneObjectPart part = GetSceneObjectPart(localID);
  2875. if (part != null) // It is a prim
  2876. {
  2877. if (part.ParentGroup != null && !part.ParentGroup.IsDeleted) // Valid
  2878. {
  2879. if (part.ParentGroup.RootPart != part) // Child part
  2880. return;
  2881. }
  2882. }
  2883. ForEachClient(delegate(IClientAPI client) { client.SendKillObject(m_regionHandle, localID); });
  2884. }
  2885. #endregion
  2886. #region RegionComms
  2887. /// <summary>
  2888. /// Register the methods that should be invoked when this scene receives various incoming events
  2889. /// </summary>
  2890. public void RegisterCommsEvents()
  2891. {
  2892. m_sceneGridService.OnExpectUser += HandleNewUserConnection;
  2893. m_sceneGridService.OnAvatarCrossingIntoRegion += AgentCrossing;
  2894. m_sceneGridService.OnCloseAgentConnection += IncomingCloseAgent;
  2895. //m_eventManager.OnRegionUp += OtherRegionUp;
  2896. //m_sceneGridService.OnChildAgentUpdate += IncomingChildAgentDataUpdate;
  2897. m_sceneGridService.OnExpectPrim += IncomingInterRegionPrimGroup;
  2898. //m_sceneGridService.OnRemoveKnownRegionFromAvatar += HandleRemoveKnownRegionsFromAvatar;
  2899. m_sceneGridService.OnLogOffUser += HandleLogOffUserFromGrid;
  2900. m_sceneGridService.KiPrimitive += SendKillObject;
  2901. m_sceneGridService.OnGetLandData += GetLandData;
  2902. if (m_interregionCommsIn != null)
  2903. {
  2904. m_log.Debug("[SCENE]: Registering with InterregionCommsIn");
  2905. m_interregionCommsIn.OnChildAgentUpdate += IncomingChildAgentDataUpdate;
  2906. }
  2907. else
  2908. m_log.Debug("[SCENE]: Unable to register with InterregionCommsIn");
  2909. }
  2910. /// <summary>
  2911. /// Deregister this scene from receiving incoming region events
  2912. /// </summary>
  2913. public void UnRegisterRegionWithComms()
  2914. {
  2915. m_sceneGridService.KiPrimitive -= SendKillObject;
  2916. m_sceneGridService.OnLogOffUser -= HandleLogOffUserFromGrid;
  2917. //m_sceneGridService.OnRemoveKnownRegionFromAvatar -= HandleRemoveKnownRegionsFromAvatar;
  2918. m_sceneGridService.OnExpectPrim -= IncomingInterRegionPrimGroup;
  2919. //m_sceneGridService.OnChildAgentUpdate -= IncomingChildAgentDataUpdate;
  2920. //m_eventManager.OnRegionUp -= OtherRegionUp;
  2921. m_sceneGridService.OnExpectUser -= HandleNewUserConnection;
  2922. m_sceneGridService.OnAvatarCrossingIntoRegion -= AgentCrossing;
  2923. m_sceneGridService.OnCloseAgentConnection -= IncomingCloseAgent;
  2924. m_sceneGridService.OnGetLandData -= GetLandData;
  2925. if (m_interregionCommsIn != null)
  2926. m_interregionCommsIn.OnChildAgentUpdate -= IncomingChildAgentDataUpdate;
  2927. // this does nothing; should be removed
  2928. m_sceneGridService.Close();
  2929. if (!GridService.DeregisterRegion(m_regInfo.RegionID))
  2930. m_log.WarnFormat("[SCENE]: Deregister from grid failed for region {0}", m_regInfo.RegionName);
  2931. }
  2932. /// <summary>
  2933. /// A handler for the SceneCommunicationService event, to match that events return type of void.
  2934. /// Use NewUserConnection() directly if possible so the return type can refuse connections.
  2935. /// At the moment nothing actually seems to use this event,
  2936. /// as everything is switching to calling the NewUserConnection method directly.
  2937. /// </summary>
  2938. /// <param name="agent"></param>
  2939. public void HandleNewUserConnection(AgentCircuitData agent)
  2940. {
  2941. string reason;
  2942. NewUserConnection(agent, out reason);
  2943. }
  2944. /// <summary>
  2945. /// Do the work necessary to initiate a new user connection for a particular scene.
  2946. /// At the moment, this consists of setting up the caps infrastructure
  2947. /// The return bool should allow for connections to be refused, but as not all calling paths
  2948. /// take proper notice of it let, we allowed banned users in still.
  2949. /// </summary>
  2950. /// <param name="agent">CircuitData of the agent who is connecting</param>
  2951. /// <param name="reason">Outputs the reason for the false response on this string</param>
  2952. /// <returns>True if the region accepts this agent. False if it does not. False will
  2953. /// also return a reason.</returns>
  2954. public bool NewUserConnection(AgentCircuitData agent, out string reason)
  2955. {
  2956. if (loginsdisabled)
  2957. {
  2958. reason = "Logins Disabled";
  2959. return false;
  2960. }
  2961. // Don't disable this log message - it's too helpful
  2962. m_log.InfoFormat(
  2963. "[CONNECTION BEGIN]: Region {0} told of incoming {1} agent {2} {3} {4} (circuit code {5})",
  2964. RegionInfo.RegionName, (agent.child ? "child" : "root"), agent.firstname, agent.lastname,
  2965. agent.AgentID, agent.circuitcode);
  2966. reason = String.Empty;
  2967. if (!AuthenticateUser(agent, out reason))
  2968. return false;
  2969. if (!AuthorizeUser(agent, out reason))
  2970. return false;
  2971. m_log.InfoFormat(
  2972. "[CONNECTION BEGIN]: Region {0} authenticated and authorized incoming {1} agent {2} {3} {4} (circuit code {5})",
  2973. RegionInfo.RegionName, (agent.child ? "child" : "root"), agent.firstname, agent.lastname,
  2974. agent.AgentID, agent.circuitcode);
  2975. CapsModule.NewUserConnection(agent);
  2976. ScenePresence sp = m_sceneGraph.GetScenePresence(agent.AgentID);
  2977. if (sp != null)
  2978. {
  2979. m_log.DebugFormat(
  2980. "[SCENE]: Adjusting known seeds for existing agent {0} in {1}",
  2981. agent.AgentID, RegionInfo.RegionName);
  2982. sp.AdjustKnownSeeds();
  2983. return true;
  2984. }
  2985. CapsModule.AddCapsHandler(agent.AgentID);
  2986. if (!agent.child)
  2987. {
  2988. if (TestBorderCross(agent.startpos,Cardinals.E))
  2989. {
  2990. Border crossedBorder = GetCrossedBorder(agent.startpos, Cardinals.E);
  2991. agent.startpos.X = crossedBorder.BorderLine.Z - 1;
  2992. }
  2993. if (TestBorderCross(agent.startpos, Cardinals.N))
  2994. {
  2995. Border crossedBorder = GetCrossedBorder(agent.startpos, Cardinals.N);
  2996. agent.startpos.Y = crossedBorder.BorderLine.Z - 1;
  2997. }
  2998. //Mitigate http://opensimulator.org/mantis/view.php?id=3522
  2999. // Check if start position is outside of region
  3000. // If it is, check the Z start position also.. if not, leave it alone.
  3001. if (BordersLocked)
  3002. {
  3003. lock (EastBorders)
  3004. {
  3005. if (agent.startpos.X > EastBorders[0].BorderLine.Z)
  3006. {
  3007. m_log.Warn("FIX AGENT POSITION");
  3008. agent.startpos.X = EastBorders[0].BorderLine.Z * 0.5f;
  3009. if (agent.startpos.Z > 720)
  3010. agent.startpos.Z = 720;
  3011. }
  3012. }
  3013. lock (NorthBorders)
  3014. {
  3015. if (agent.startpos.Y > NorthBorders[0].BorderLine.Z)
  3016. {
  3017. m_log.Warn("FIX Agent POSITION");
  3018. agent.startpos.Y = NorthBorders[0].BorderLine.Z * 0.5f;
  3019. if (agent.startpos.Z > 720)
  3020. agent.startpos.Z = 720;
  3021. }
  3022. }
  3023. }
  3024. else
  3025. {
  3026. if (agent.startpos.X > EastBorders[0].BorderLine.Z)
  3027. {
  3028. m_log.Warn("FIX AGENT POSITION");
  3029. agent.startpos.X = EastBorders[0].BorderLine.Z * 0.5f;
  3030. if (agent.startpos.Z > 720)
  3031. agent.startpos.Z = 720;
  3032. }
  3033. if (agent.startpos.Y > NorthBorders[0].BorderLine.Z)
  3034. {
  3035. m_log.Warn("FIX Agent POSITION");
  3036. agent.startpos.Y = NorthBorders[0].BorderLine.Z * 0.5f;
  3037. if (agent.startpos.Z > 720)
  3038. agent.startpos.Z = 720;
  3039. }
  3040. }
  3041. // Honor parcel landing type and position.
  3042. ILandObject land = LandChannel.GetLandObject(agent.startpos.X, agent.startpos.Y);
  3043. if (land != null)
  3044. {
  3045. if (land.LandData.LandingType == (byte)1 && land.LandData.UserLocation != Vector3.Zero)
  3046. {
  3047. agent.startpos = land.LandData.UserLocation;
  3048. }
  3049. }
  3050. }
  3051. m_authenticateHandler.AddNewCircuit(agent.circuitcode, agent);
  3052. // rewrite session_id
  3053. CachedUserInfo userinfo = CommsManager.UserProfileCacheService.GetUserDetails(agent.AgentID);
  3054. if (userinfo != null)
  3055. {
  3056. userinfo.SessionID = agent.SessionID;
  3057. }
  3058. else
  3059. {
  3060. m_log.WarnFormat(
  3061. "[CONNECTION BEGIN]: 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);
  3062. }
  3063. return true;
  3064. }
  3065. /// <summary>
  3066. /// Verifies that the user has a session on the Grid
  3067. /// </summary>
  3068. /// <param name="agent">Circuit Data of the Agent we're verifying</param>
  3069. /// <param name="reason">Outputs the reason for the false response on this string</param>
  3070. /// <returns>True if the user has a session on the grid. False if it does not. False will
  3071. /// also return a reason.</returns>
  3072. public virtual bool AuthenticateUser(AgentCircuitData agent, out string reason)
  3073. {
  3074. reason = String.Empty;
  3075. bool result = CommsManager.UserService.VerifySession(agent.AgentID, agent.SessionID);
  3076. m_log.Debug("[CONNECTION BEGIN]: User authentication returned " + result);
  3077. if (!result)
  3078. reason = String.Format("Failed to authenticate user {0} {1}, access denied.", agent.firstname, agent.lastname);
  3079. return result;
  3080. }
  3081. /// <summary>
  3082. /// Verify if the user can connect to this region. Checks the banlist and ensures that the region is set for public access
  3083. /// </summary>
  3084. /// <param name="agent">The circuit data for the agent</param>
  3085. /// <param name="reason">outputs the reason to this string</param>
  3086. /// <returns>True if the region accepts this agent. False if it does not. False will
  3087. /// also return a reason.</returns>
  3088. protected virtual bool AuthorizeUser(AgentCircuitData agent, out string reason)
  3089. {
  3090. reason = String.Empty;
  3091. if (!m_strictAccessControl) return true;
  3092. if (Permissions.IsGod(agent.AgentID)) return true;
  3093. if (AuthorizationService != null)
  3094. {
  3095. if (!AuthorizationService.IsAuthorizedForRegion(agent.AgentID.ToString(), RegionInfo.RegionID.ToString(),out reason))
  3096. {
  3097. m_log.WarnFormat("[CONNECTION BEGIN]: Denied access to: {0} ({1} {2}) at {3} because the user does not have access to the region",
  3098. agent.AgentID, agent.firstname, agent.lastname, RegionInfo.RegionName);
  3099. //reason = String.Format("You are not currently on the access list for {0}",RegionInfo.RegionName);
  3100. return false;
  3101. }
  3102. }
  3103. if (m_regInfo.EstateSettings.IsBanned(agent.AgentID))
  3104. {
  3105. m_log.WarnFormat("[CONNECTION BEGIN]: Denied access to: {0} ({1} {2}) at {3} because the user is on the banlist",
  3106. agent.AgentID, agent.firstname, agent.lastname, RegionInfo.RegionName);
  3107. reason = String.Format("Denied access to region {0}: You have been banned from that region.",
  3108. RegionInfo.RegionName);
  3109. return false;
  3110. }
  3111. if (!m_regInfo.EstateSettings.PublicAccess &&
  3112. !m_regInfo.EstateSettings.HasAccess(agent.AgentID))
  3113. {
  3114. m_log.WarnFormat("[CONNECTION BEGIN]: Denied access to: {0} ({1} {2}) at {3} because the user does not have access to the estate",
  3115. agent.AgentID, agent.firstname, agent.lastname, RegionInfo.RegionName);
  3116. reason = String.Format("Denied access to private region {0}: You are not on the access list for that region.",
  3117. RegionInfo.RegionName);
  3118. return false;
  3119. }
  3120. // TODO: estate/region settings are not properly hooked up
  3121. // to ILandObject.isRestrictedFromLand()
  3122. // if (null != LandChannel)
  3123. // {
  3124. // // region seems to have local Id of 1
  3125. // ILandObject land = LandChannel.GetLandObject(1);
  3126. // if (null != land)
  3127. // {
  3128. // if (land.isBannedFromLand(agent.AgentID))
  3129. // {
  3130. // m_log.WarnFormat("[CONNECTION BEGIN]: Denied access to: {0} ({1} {2}) at {3} because the user has been banned from land",
  3131. // agent.AgentID, agent.firstname, agent.lastname, RegionInfo.RegionName);
  3132. // reason = String.Format("Denied access to private region {0}: You are banned from that region.",
  3133. // RegionInfo.RegionName);
  3134. // return false;
  3135. // }
  3136. // if (land.isRestrictedFromLand(agent.AgentID))
  3137. // {
  3138. // m_log.WarnFormat("[CONNECTION BEGIN]: Denied access to: {0} ({1} {2}) at {3} because the user does not have access to the region",
  3139. // agent.AgentID, agent.firstname, agent.lastname, RegionInfo.RegionName);
  3140. // reason = String.Format("Denied access to private region {0}: You are not on the access list for that region.",
  3141. // RegionInfo.RegionName);
  3142. // return false;
  3143. // }
  3144. // }
  3145. // }
  3146. return true;
  3147. }
  3148. /// <summary>
  3149. /// Update an AgentCircuitData object with new information
  3150. /// </summary>
  3151. /// <param name="data">Information to update the AgentCircuitData with</param>
  3152. public void UpdateCircuitData(AgentCircuitData data)
  3153. {
  3154. m_authenticateHandler.UpdateAgentData(data);
  3155. }
  3156. /// <summary>
  3157. /// Change the Circuit Code for the user's Circuit Data
  3158. /// </summary>
  3159. /// <param name="oldcc">The old Circuit Code. Must match a previous circuit code</param>
  3160. /// <param name="newcc">The new Circuit Code. Must not be an already existing circuit code</param>
  3161. /// <returns>True if we successfully changed it. False if we did not</returns>
  3162. public bool ChangeCircuitCode(uint oldcc, uint newcc)
  3163. {
  3164. return m_authenticateHandler.TryChangeCiruitCode(oldcc, newcc);
  3165. }
  3166. /// <summary>
  3167. /// The Grid has requested that we log-off a user. Log them off.
  3168. /// </summary>
  3169. /// <param name="AvatarID">Unique ID of the avatar to log-off</param>
  3170. /// <param name="RegionSecret">SecureSessionID of the user, or the RegionSecret text when logging on to the grid</param>
  3171. /// <param name="message">message to display to the user. Reason for being logged off</param>
  3172. public void HandleLogOffUserFromGrid(UUID AvatarID, UUID RegionSecret, string message)
  3173. {
  3174. ScenePresence loggingOffUser = null;
  3175. loggingOffUser = GetScenePresence(AvatarID);
  3176. if (loggingOffUser != null)
  3177. {
  3178. UUID localRegionSecret = UUID.Zero;
  3179. bool parsedsecret = UUID.TryParse(m_regInfo.regionSecret, out localRegionSecret);
  3180. // Region Secret is used here in case a new sessionid overwrites an old one on the user server.
  3181. // Will update the user server in a few revisions to use it.
  3182. if (RegionSecret == loggingOffUser.ControllingClient.SecureSessionId || (parsedsecret && RegionSecret == localRegionSecret))
  3183. {
  3184. m_sceneGridService.SendCloseChildAgentConnections(loggingOffUser.UUID, new List<ulong>(loggingOffUser.KnownRegions.Keys));
  3185. loggingOffUser.ControllingClient.Kick(message);
  3186. // Give them a second to receive the message!
  3187. Thread.Sleep(1000);
  3188. loggingOffUser.ControllingClient.Close();
  3189. }
  3190. else
  3191. {
  3192. m_log.Info("[USERLOGOFF]: System sending the LogOff user message failed to sucessfully authenticate");
  3193. }
  3194. }
  3195. else
  3196. {
  3197. 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());
  3198. }
  3199. }
  3200. /// <summary>
  3201. /// Triggered when an agent crosses into this sim. Also happens on initial login.
  3202. /// </summary>
  3203. /// <param name="agentID"></param>
  3204. /// <param name="position"></param>
  3205. /// <param name="isFlying"></param>
  3206. public virtual void AgentCrossing(UUID agentID, Vector3 position, bool isFlying)
  3207. {
  3208. ScenePresence presence;
  3209. m_sceneGraph.TryGetAvatar(agentID, out presence);
  3210. if (presence != null)
  3211. {
  3212. try
  3213. {
  3214. presence.MakeRootAgent(position, isFlying);
  3215. }
  3216. catch (Exception e)
  3217. {
  3218. m_log.ErrorFormat("[SCENE]: Unable to do agent crossing, exception {0}", e);
  3219. }
  3220. }
  3221. else
  3222. {
  3223. m_log.ErrorFormat(
  3224. "[SCENE]: Could not find presence for agent {0} crossing into scene {1}",
  3225. agentID, RegionInfo.RegionName);
  3226. }
  3227. }
  3228. /// <summary>
  3229. /// We've got an update about an agent that sees into this region,
  3230. /// send it to ScenePresence for processing It's the full data.
  3231. /// </summary>
  3232. /// <param name="cAgentData">Agent that contains all of the relevant things about an agent.
  3233. /// Appearance, animations, position, etc.</param>
  3234. /// <returns>true if we handled it.</returns>
  3235. public virtual bool IncomingChildAgentDataUpdate(AgentData cAgentData)
  3236. {
  3237. // m_log.DebugFormat(
  3238. // "[SCENE]: Incoming child agent update for {0} in {1}", cAgentData.AgentID, RegionInfo.RegionName);
  3239. // We have to wait until the viewer contacts this region after receiving EAC.
  3240. // That calls AddNewClient, which finally creates the ScenePresence
  3241. ScenePresence childAgentUpdate = WaitGetScenePresence(cAgentData.AgentID);
  3242. if (childAgentUpdate != null)
  3243. {
  3244. childAgentUpdate.ChildAgentDataUpdate(cAgentData);
  3245. return true;
  3246. }
  3247. return false;
  3248. }
  3249. /// <summary>
  3250. /// We've got an update about an agent that sees into this region,
  3251. /// send it to ScenePresence for processing It's only positional data
  3252. /// </summary>
  3253. /// <param name="cAgentData">AgentPosition that contains agent positional data so we can know what to send</param>
  3254. /// <returns>true if we handled it.</returns>
  3255. public virtual bool IncomingChildAgentDataUpdate(AgentPosition cAgentData)
  3256. {
  3257. //m_log.Debug(" XXX Scene IncomingChildAgentDataUpdate POSITION in " + RegionInfo.RegionName);
  3258. ScenePresence childAgentUpdate = GetScenePresence(cAgentData.AgentID);
  3259. if (childAgentUpdate != null)
  3260. {
  3261. // I can't imagine *yet* why we would get an update if the agent is a root agent..
  3262. // however to avoid a race condition crossing borders..
  3263. if (childAgentUpdate.IsChildAgent)
  3264. {
  3265. uint rRegionX = (uint)(cAgentData.RegionHandle >> 40);
  3266. uint rRegionY = (((uint)(cAgentData.RegionHandle)) >> 8);
  3267. uint tRegionX = RegionInfo.RegionLocX;
  3268. uint tRegionY = RegionInfo.RegionLocY;
  3269. //Send Data to ScenePresence
  3270. childAgentUpdate.ChildAgentDataUpdate(cAgentData, tRegionX, tRegionY, rRegionX, rRegionY);
  3271. // Not Implemented:
  3272. //TODO: Do we need to pass the message on to one of our neighbors?
  3273. }
  3274. return true;
  3275. }
  3276. return false;
  3277. }
  3278. protected virtual ScenePresence WaitGetScenePresence(UUID agentID)
  3279. {
  3280. int ntimes = 10;
  3281. ScenePresence childAgentUpdate = null;
  3282. while ((childAgentUpdate = GetScenePresence(agentID)) == null && (ntimes-- > 0))
  3283. Thread.Sleep(1000);
  3284. return childAgentUpdate;
  3285. }
  3286. public virtual bool IncomingRetrieveRootAgent(UUID id, out IAgentData agent)
  3287. {
  3288. agent = null;
  3289. ScenePresence sp = GetScenePresence(id);
  3290. if ((sp != null) && (!sp.IsChildAgent))
  3291. {
  3292. sp.IsChildAgent = true;
  3293. return sp.CopyAgent(out agent);
  3294. }
  3295. return false;
  3296. }
  3297. public virtual bool IncomingReleaseAgent(UUID id)
  3298. {
  3299. return m_sceneGridService.ReleaseAgent(id);
  3300. }
  3301. public void SendReleaseAgent(ulong regionHandle, UUID id, string uri)
  3302. {
  3303. m_interregionCommsOut.SendReleaseAgent(regionHandle, id, uri);
  3304. }
  3305. /// <summary>
  3306. /// Tell a single agent to disconnect from the region.
  3307. /// </summary>
  3308. /// <param name="regionHandle"></param>
  3309. /// <param name="agentID"></param>
  3310. public bool IncomingCloseAgent(UUID agentID)
  3311. {
  3312. //m_log.DebugFormat("[SCENE]: Processing incoming close agent for {0}", agentID);
  3313. ScenePresence presence = m_sceneGraph.GetScenePresence(agentID);
  3314. if (presence != null)
  3315. {
  3316. // Nothing is removed here, so down count it as such
  3317. if (presence.IsChildAgent)
  3318. {
  3319. m_sceneGraph.removeUserCount(false);
  3320. }
  3321. else
  3322. {
  3323. m_sceneGraph.removeUserCount(true);
  3324. }
  3325. // Don't do this to root agents on logout, it's not nice for the viewer
  3326. if (presence.IsChildAgent)
  3327. {
  3328. // Tell a single agent to disconnect from the region.
  3329. IEventQueue eq = RequestModuleInterface<IEventQueue>();
  3330. if (eq != null)
  3331. {
  3332. eq.DisableSimulator(RegionInfo.RegionHandle, agentID);
  3333. }
  3334. else
  3335. presence.ControllingClient.SendShutdownConnectionNotice();
  3336. }
  3337. presence.ControllingClient.Close();
  3338. return true;
  3339. }
  3340. // Agent not here
  3341. return false;
  3342. }
  3343. /// <summary>
  3344. /// Tell neighboring regions about this agent
  3345. /// When the regions respond with a true value,
  3346. /// tell the agents about the region.
  3347. ///
  3348. /// We have to tell the regions about the agents first otherwise it'll deny them access
  3349. ///
  3350. /// </summary>
  3351. /// <param name="presence"></param>
  3352. public void InformClientOfNeighbours(ScenePresence presence)
  3353. {
  3354. m_sceneGridService.EnableNeighbourChildAgents(presence, m_neighbours);
  3355. }
  3356. /// <summary>
  3357. /// Tell a neighboring region about this agent
  3358. /// </summary>
  3359. /// <param name="presence"></param>
  3360. /// <param name="region"></param>
  3361. public void InformClientOfNeighbor(ScenePresence presence, RegionInfo region)
  3362. {
  3363. m_sceneGridService.EnableNeighbourChildAgents(presence, m_neighbours);
  3364. }
  3365. /// <summary>
  3366. /// Tries to teleport agent to other region.
  3367. /// </summary>
  3368. /// <param name="remoteClient"></param>
  3369. /// <param name="regionName"></param>
  3370. /// <param name="position"></param>
  3371. /// <param name="lookAt"></param>
  3372. /// <param name="teleportFlags"></param>
  3373. public void RequestTeleportLocation(IClientAPI remoteClient, string regionName, Vector3 position,
  3374. Vector3 lookat, uint teleportFlags)
  3375. {
  3376. GridRegion regionInfo = GridService.GetRegionByName(UUID.Zero, regionName);
  3377. if (regionInfo == null)
  3378. {
  3379. // can't find the region: Tell viewer and abort
  3380. remoteClient.SendTeleportFailed("The region '" + regionName + "' could not be found.");
  3381. return;
  3382. }
  3383. RequestTeleportLocation(remoteClient, regionInfo.RegionHandle, position, lookat, teleportFlags);
  3384. }
  3385. /// <summary>
  3386. /// Tries to teleport agent to other region.
  3387. /// </summary>
  3388. /// <param name="remoteClient"></param>
  3389. /// <param name="regionHandle"></param>
  3390. /// <param name="position"></param>
  3391. /// <param name="lookAt"></param>
  3392. /// <param name="teleportFlags"></param>
  3393. public void RequestTeleportLocation(IClientAPI remoteClient, ulong regionHandle, Vector3 position,
  3394. Vector3 lookAt, uint teleportFlags)
  3395. {
  3396. ScenePresence sp;
  3397. m_sceneGraph.TryGetAvatar(remoteClient.AgentId, out sp);
  3398. if (sp != null)
  3399. {
  3400. uint regionX = m_regInfo.RegionLocX;
  3401. uint regionY = m_regInfo.RegionLocY;
  3402. Utils.LongToUInts(regionHandle, out regionX, out regionY);
  3403. int shiftx = (int) regionX - (int) m_regInfo.RegionLocX * (int)Constants.RegionSize;
  3404. int shifty = (int) regionY - (int) m_regInfo.RegionLocY * (int)Constants.RegionSize;
  3405. position.X += shiftx;
  3406. position.Y += shifty;
  3407. bool result = false;
  3408. if (TestBorderCross(position,Cardinals.N))
  3409. result = true;
  3410. if (TestBorderCross(position, Cardinals.S))
  3411. result = true;
  3412. if (TestBorderCross(position, Cardinals.E))
  3413. result = true;
  3414. if (TestBorderCross(position, Cardinals.W))
  3415. result = true;
  3416. // bordercross if position is outside of region
  3417. if (!result)
  3418. regionHandle = m_regInfo.RegionHandle;
  3419. else
  3420. {
  3421. // not in this region, undo the shift!
  3422. position.X -= shiftx;
  3423. position.Y -= shifty;
  3424. }
  3425. if (m_teleportModule != null)
  3426. {
  3427. m_teleportModule.RequestTeleportToLocation(sp, regionHandle,
  3428. position, lookAt, teleportFlags);
  3429. }
  3430. else
  3431. {
  3432. m_sceneGridService.RequestTeleportToLocation(sp, regionHandle,
  3433. position, lookAt, teleportFlags);
  3434. }
  3435. }
  3436. }
  3437. /// <summary>
  3438. /// Tries to teleport agent to landmark.
  3439. /// </summary>
  3440. /// <param name="remoteClient"></param>
  3441. /// <param name="regionHandle"></param>
  3442. /// <param name="position"></param>
  3443. public void RequestTeleportLandmark(IClientAPI remoteClient, UUID regionID, Vector3 position)
  3444. {
  3445. GridRegion info = GridService.GetRegionByUUID(UUID.Zero, regionID);
  3446. if (info == null)
  3447. {
  3448. // can't find the region: Tell viewer and abort
  3449. remoteClient.SendTeleportFailed("The teleport destination could not be found.");
  3450. return;
  3451. }
  3452. RequestTeleportLocation(remoteClient, info.RegionHandle, position, Vector3.Zero, (uint)(TPFlags.SetLastToTarget | TPFlags.ViaLandmark));
  3453. }
  3454. public void CrossAgentToNewRegion(ScenePresence agent, bool isFlying)
  3455. {
  3456. m_sceneGridService.CrossAgentToNewRegion(this, agent, isFlying);
  3457. }
  3458. public void SendOutChildAgentUpdates(AgentPosition cadu, ScenePresence presence)
  3459. {
  3460. m_sceneGridService.SendChildAgentDataUpdate(cadu, presence);
  3461. }
  3462. #endregion
  3463. #region Other Methods
  3464. public void SetObjectCapacity(int objects)
  3465. {
  3466. // Region specific config overrides global
  3467. //
  3468. if (RegionInfo.ObjectCapacity != 0)
  3469. objects = RegionInfo.ObjectCapacity;
  3470. if (StatsReporter != null)
  3471. {
  3472. StatsReporter.SetObjectCapacity(objects);
  3473. }
  3474. objectCapacity = objects;
  3475. }
  3476. public List<FriendListItem> GetFriendList(string id)
  3477. {
  3478. UUID avatarID;
  3479. if (!UUID.TryParse(id, out avatarID))
  3480. return new List<FriendListItem>();
  3481. return CommsManager.GetUserFriendList(avatarID);
  3482. }
  3483. public Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos(List<UUID> uuids)
  3484. {
  3485. return CommsManager.GetFriendRegionInfos(uuids);
  3486. }
  3487. public virtual void StoreAddFriendship(UUID ownerID, UUID friendID, uint perms)
  3488. {
  3489. m_sceneGridService.AddNewUserFriend(ownerID, friendID, perms);
  3490. }
  3491. public virtual void StoreUpdateFriendship(UUID ownerID, UUID friendID, uint perms)
  3492. {
  3493. m_sceneGridService.UpdateUserFriendPerms(ownerID, friendID, perms);
  3494. }
  3495. public virtual void StoreRemoveFriendship(UUID ownerID, UUID ExfriendID)
  3496. {
  3497. m_sceneGridService.RemoveUserFriend(ownerID, ExfriendID);
  3498. }
  3499. #endregion
  3500. public void HandleObjectPermissionsUpdate(IClientAPI controller, UUID agentID, UUID sessionID, byte field, uint localId, uint mask, byte set)
  3501. {
  3502. // Check for spoofing.. since this is permissions we're talking about here!
  3503. if ((controller.SessionId == sessionID) && (controller.AgentId == agentID))
  3504. {
  3505. // Tell the object to do permission update
  3506. if (localId != 0)
  3507. {
  3508. SceneObjectGroup chObjectGroup = GetGroupByPrim(localId);
  3509. if (chObjectGroup != null)
  3510. {
  3511. chObjectGroup.UpdatePermissions(agentID, field, localId, mask, set);
  3512. }
  3513. }
  3514. }
  3515. }
  3516. /// <summary>
  3517. /// Causes all clients to get a full object update on all of the objects in the scene.
  3518. /// </summary>
  3519. public void ForceClientUpdate()
  3520. {
  3521. List<EntityBase> EntityList = GetEntities();
  3522. foreach (EntityBase ent in EntityList)
  3523. {
  3524. if (ent is SceneObjectGroup)
  3525. {
  3526. ((SceneObjectGroup)ent).ScheduleGroupForFullUpdate();
  3527. }
  3528. }
  3529. }
  3530. /// <summary>
  3531. /// This is currently only used for scale (to scale to MegaPrim size)
  3532. /// There is a console command that calls this in OpenSimMain
  3533. /// </summary>
  3534. /// <param name="cmdparams"></param>
  3535. public void HandleEditCommand(string[] cmdparams)
  3536. {
  3537. m_log.Debug("Searching for Primitive: '" + cmdparams[2] + "'");
  3538. List<EntityBase> EntityList = GetEntities();
  3539. foreach (EntityBase ent in EntityList)
  3540. {
  3541. if (ent is SceneObjectGroup)
  3542. {
  3543. SceneObjectPart part = ((SceneObjectGroup)ent).GetChildPart(((SceneObjectGroup)ent).UUID);
  3544. if (part != null)
  3545. {
  3546. if (part.Name == cmdparams[2])
  3547. {
  3548. part.Resize(
  3549. new Vector3(Convert.ToSingle(cmdparams[3]), Convert.ToSingle(cmdparams[4]),
  3550. Convert.ToSingle(cmdparams[5])));
  3551. m_log.Debug("Edited scale of Primitive: " + part.Name);
  3552. }
  3553. }
  3554. }
  3555. }
  3556. }
  3557. public override void Show(string[] showParams)
  3558. {
  3559. base.Show(showParams);
  3560. switch (showParams[0])
  3561. {
  3562. case "users":
  3563. m_log.Error("Current Region: " + RegionInfo.RegionName);
  3564. m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16}{5,-16}{6,-16}", "Firstname", "Lastname",
  3565. "Agent ID", "Session ID", "Circuit", "IP", "World");
  3566. foreach (ScenePresence scenePresence in GetAvatars())
  3567. {
  3568. m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}{6,-16}",
  3569. scenePresence.Firstname,
  3570. scenePresence.Lastname,
  3571. scenePresence.UUID,
  3572. scenePresence.ControllingClient.AgentId,
  3573. "Unknown",
  3574. "Unknown",
  3575. RegionInfo.RegionName);
  3576. }
  3577. break;
  3578. }
  3579. }
  3580. #region Script Handling Methods
  3581. /// <summary>
  3582. /// Console command handler to send script command to script engine.
  3583. /// </summary>
  3584. /// <param name="args"></param>
  3585. public void SendCommandToPlugins(string[] args)
  3586. {
  3587. m_eventManager.TriggerOnPluginConsole(args);
  3588. }
  3589. public LandData GetLandData(float x, float y)
  3590. {
  3591. return LandChannel.GetLandObject(x, y).LandData;
  3592. }
  3593. public LandData GetLandData(uint x, uint y)
  3594. {
  3595. m_log.DebugFormat("[SCENE]: returning land for {0},{1}", x, y);
  3596. return LandChannel.GetLandObject((int)x, (int)y).LandData;
  3597. }
  3598. #endregion
  3599. #region Script Engine
  3600. private List<ScriptEngineInterface> ScriptEngines = new List<ScriptEngineInterface>();
  3601. public bool DumpAssetsToFile;
  3602. /// <summary>
  3603. ///
  3604. /// </summary>
  3605. /// <param name="scriptEngine"></param>
  3606. public void AddScriptEngine(ScriptEngineInterface scriptEngine)
  3607. {
  3608. ScriptEngines.Add(scriptEngine);
  3609. scriptEngine.InitializeEngine(this);
  3610. }
  3611. private bool ScriptDanger(SceneObjectPart part,Vector3 pos)
  3612. {
  3613. ILandObject parcel = LandChannel.GetLandObject(pos.X, pos.Y);
  3614. if (part != null)
  3615. {
  3616. if (parcel != null)
  3617. {
  3618. if ((parcel.LandData.Flags & (uint)ParcelFlags.AllowOtherScripts) != 0)
  3619. {
  3620. return true;
  3621. }
  3622. else if ((parcel.LandData.Flags & (uint)ParcelFlags.AllowGroupScripts) != 0)
  3623. {
  3624. if (part.OwnerID == parcel.LandData.OwnerID
  3625. || (parcel.LandData.IsGroupOwned && part.GroupID == parcel.LandData.GroupID)
  3626. || Permissions.IsGod(part.OwnerID))
  3627. {
  3628. return true;
  3629. }
  3630. else
  3631. {
  3632. return false;
  3633. }
  3634. }
  3635. else
  3636. {
  3637. if (part.OwnerID == parcel.LandData.OwnerID)
  3638. {
  3639. return true;
  3640. }
  3641. else
  3642. {
  3643. return false;
  3644. }
  3645. }
  3646. }
  3647. else
  3648. {
  3649. if (pos.X > 0f && pos.X < Constants.RegionSize && pos.Y > 0f && pos.Y < Constants.RegionSize)
  3650. {
  3651. // The only time parcel != null when an object is inside a region is when
  3652. // there is nothing behind the landchannel. IE, no land plugin loaded.
  3653. return true;
  3654. }
  3655. else
  3656. {
  3657. // The object is outside of this region. Stop piping events to it.
  3658. return false;
  3659. }
  3660. }
  3661. }
  3662. else
  3663. {
  3664. return false;
  3665. }
  3666. }
  3667. public bool ScriptDanger(uint localID, Vector3 pos)
  3668. {
  3669. SceneObjectPart part = GetSceneObjectPart(localID);
  3670. if (part != null)
  3671. {
  3672. return ScriptDanger(part, pos);
  3673. }
  3674. else
  3675. {
  3676. return false;
  3677. }
  3678. }
  3679. public bool PipeEventsForScript(uint localID)
  3680. {
  3681. SceneObjectPart part = GetSceneObjectPart(localID);
  3682. if (part != null)
  3683. {
  3684. // Changed so that child prims of attachments return ScriptDanger for their parent, so that
  3685. // their scripts will actually run.
  3686. // -- Leaf, Tue Aug 12 14:17:05 EDT 2008
  3687. SceneObjectPart parent = part.ParentGroup.RootPart;
  3688. if (parent != null && parent.IsAttachment)
  3689. return ScriptDanger(parent, parent.GetWorldPosition());
  3690. else
  3691. return ScriptDanger(part, part.GetWorldPosition());
  3692. }
  3693. else
  3694. {
  3695. return false;
  3696. }
  3697. }
  3698. #endregion
  3699. #region SceneGraph wrapper methods
  3700. /// <summary>
  3701. ///
  3702. /// </summary>
  3703. /// <param name="localID"></param>
  3704. /// <returns></returns>
  3705. public UUID ConvertLocalIDToFullID(uint localID)
  3706. {
  3707. return m_sceneGraph.ConvertLocalIDToFullID(localID);
  3708. }
  3709. public void SwapRootAgentCount(bool rootChildChildRootTF)
  3710. {
  3711. m_sceneGraph.SwapRootChildAgent(rootChildChildRootTF);
  3712. }
  3713. public void AddPhysicalPrim(int num)
  3714. {
  3715. m_sceneGraph.AddPhysicalPrim(num);
  3716. }
  3717. public void RemovePhysicalPrim(int num)
  3718. {
  3719. m_sceneGraph.RemovePhysicalPrim(num);
  3720. }
  3721. //The idea is to have a group of method that return a list of avatars meeting some requirement
  3722. // ie it could be all m_scenePresences within a certain range of the calling prim/avatar.
  3723. /// <summary>
  3724. /// Return a list of all avatars in this region.
  3725. /// This list is a new object, so it can be iterated over without locking.
  3726. /// </summary>
  3727. /// <returns></returns>
  3728. public List<ScenePresence> GetAvatars()
  3729. {
  3730. return m_sceneGraph.GetAvatars();
  3731. }
  3732. /// <summary>
  3733. /// Return a list of all ScenePresences in this region. This returns child agents as well as root agents.
  3734. /// This list is a new object, so it can be iterated over without locking.
  3735. /// </summary>
  3736. /// <returns></returns>
  3737. public ScenePresence[] GetScenePresences()
  3738. {
  3739. return m_sceneGraph.GetScenePresences();
  3740. }
  3741. /// <summary>
  3742. /// Request a filtered list of ScenePresences in this region.
  3743. /// This list is a new object, so it can be iterated over without locking.
  3744. /// </summary>
  3745. /// <param name="filter"></param>
  3746. /// <returns></returns>
  3747. public List<ScenePresence> GetScenePresences(FilterAvatarList filter)
  3748. {
  3749. return m_sceneGraph.GetScenePresences(filter);
  3750. }
  3751. /// <summary>
  3752. /// Request a scene presence by UUID
  3753. /// </summary>
  3754. /// <param name="avatarID"></param>
  3755. /// <returns></returns>
  3756. public ScenePresence GetScenePresence(UUID avatarID)
  3757. {
  3758. return m_sceneGraph.GetScenePresence(avatarID);
  3759. }
  3760. public override bool PresenceChildStatus(UUID avatarID)
  3761. {
  3762. ScenePresence cp = GetScenePresence(avatarID);
  3763. // FIXME: This is really crap - some logout code is relying on a NullReferenceException to halt its processing
  3764. // This needs to be fixed properly by cleaning up the logout code.
  3765. //if (cp != null)
  3766. // return cp.IsChildAgent;
  3767. //return false;
  3768. return cp.IsChildAgent;
  3769. }
  3770. /// <summary>
  3771. ///
  3772. /// </summary>
  3773. /// <param name="action"></param>
  3774. public void ForEachScenePresence(Action<ScenePresence> action)
  3775. {
  3776. // We don't want to try to send messages if there are no avatars.
  3777. if (m_sceneGraph != null)
  3778. {
  3779. try
  3780. {
  3781. ScenePresence[] presences = GetScenePresences();
  3782. for (int i = 0; i < presences.Length; i++)
  3783. action(presences[i]);
  3784. }
  3785. catch (Exception e)
  3786. {
  3787. m_log.Info("[BUG] in " + RegionInfo.RegionName + ": " + e.ToString());
  3788. m_log.Info("[BUG] Stack Trace: " + e.StackTrace);
  3789. }
  3790. }
  3791. }
  3792. /// <summary>
  3793. ///
  3794. /// </summary>
  3795. /// <param name="action"></param>
  3796. // public void ForEachObject(Action<SceneObjectGroup> action)
  3797. // {
  3798. // List<SceneObjectGroup> presenceList;
  3799. //
  3800. // lock (m_sceneObjects)
  3801. // {
  3802. // presenceList = new List<SceneObjectGroup>(m_sceneObjects.Values);
  3803. // }
  3804. //
  3805. // foreach (SceneObjectGroup presence in presenceList)
  3806. // {
  3807. // action(presence);
  3808. // }
  3809. // }
  3810. /// <summary>
  3811. /// Get a named prim contained in this scene (will return the first
  3812. /// found, if there are more than one prim with the same name)
  3813. /// </summary>
  3814. /// <param name="name"></param>
  3815. /// <returns></returns>
  3816. public SceneObjectPart GetSceneObjectPart(string name)
  3817. {
  3818. return m_sceneGraph.GetSceneObjectPart(name);
  3819. }
  3820. /// <summary>
  3821. /// Get a prim via its local id
  3822. /// </summary>
  3823. /// <param name="localID"></param>
  3824. /// <returns></returns>
  3825. public SceneObjectPart GetSceneObjectPart(uint localID)
  3826. {
  3827. return m_sceneGraph.GetSceneObjectPart(localID);
  3828. }
  3829. /// <summary>
  3830. /// Get a prim via its UUID
  3831. /// </summary>
  3832. /// <param name="fullID"></param>
  3833. /// <returns></returns>
  3834. public SceneObjectPart GetSceneObjectPart(UUID fullID)
  3835. {
  3836. return m_sceneGraph.GetSceneObjectPart(fullID);
  3837. }
  3838. /// <summary>
  3839. /// Get a scene object group that contains the prim with the given local id
  3840. /// </summary>
  3841. /// <param name="localID"></param>
  3842. /// <returns>null if no scene object group containing that prim is found</returns>
  3843. public SceneObjectGroup GetGroupByPrim(uint localID)
  3844. {
  3845. return m_sceneGraph.GetGroupByPrim(localID);
  3846. }
  3847. public bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
  3848. {
  3849. return m_sceneGraph.TryGetAvatar(avatarId, out avatar);
  3850. }
  3851. public bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  3852. {
  3853. return m_sceneGraph.TryGetAvatarByName(avatarName, out avatar);
  3854. }
  3855. public void ForEachClient(Action<IClientAPI> action)
  3856. {
  3857. ForEachClient(action, m_useAsyncWhenPossible);
  3858. }
  3859. public void ForEachClient(Action<IClientAPI> action, bool doAsynchronous)
  3860. {
  3861. // FIXME: Asynchronous iteration is disabled until we have a threading model that
  3862. // can support calling this function from an async packet handler without
  3863. // potentially deadlocking
  3864. m_clientManager.ForEachSync(action);
  3865. //if (doAsynchronous)
  3866. // m_clientManager.ForEach(action);
  3867. //else
  3868. // m_clientManager.ForEachSync(action);
  3869. }
  3870. public bool TryGetClient(UUID avatarID, out IClientAPI client)
  3871. {
  3872. return m_clientManager.TryGetValue(avatarID, out client);
  3873. }
  3874. public bool TryGetClient(System.Net.IPEndPoint remoteEndPoint, out IClientAPI client)
  3875. {
  3876. return m_clientManager.TryGetValue(remoteEndPoint, out client);
  3877. }
  3878. public void ForEachSOG(Action<SceneObjectGroup> action)
  3879. {
  3880. m_sceneGraph.ForEachSOG(action);
  3881. }
  3882. /// <summary>
  3883. /// Returns a list of the entities in the scene. This is a new list so operations perform on the list itself
  3884. /// will not affect the original list of objects in the scene.
  3885. /// </summary>
  3886. /// <returns></returns>
  3887. public List<EntityBase> GetEntities()
  3888. {
  3889. return m_sceneGraph.GetEntities();
  3890. }
  3891. #endregion
  3892. #region Avatar Appearance Default
  3893. public static void GetDefaultAvatarAppearance(out AvatarWearable[] wearables, out byte[] visualParams)
  3894. {
  3895. visualParams = AvatarAppearance.GetDefaultVisualParams();
  3896. wearables = AvatarWearable.DefaultWearables;
  3897. }
  3898. #endregion
  3899. public void RegionHandleRequest(IClientAPI client, UUID regionID)
  3900. {
  3901. ulong handle = 0;
  3902. if (regionID == RegionInfo.RegionID)
  3903. handle = RegionInfo.RegionHandle;
  3904. else
  3905. {
  3906. GridRegion r = GridService.GetRegionByUUID(UUID.Zero, regionID);
  3907. if (r != null)
  3908. handle = r.RegionHandle;
  3909. }
  3910. if (handle != 0)
  3911. client.SendRegionHandle(regionID, handle);
  3912. }
  3913. public void TerrainUnAcked(IClientAPI client, int patchX, int patchY)
  3914. {
  3915. //m_log.Debug("Terrain packet unacked, resending patch: " + patchX + " , " + patchY);
  3916. client.SendLayerData(patchX, patchY, Heightmap.GetFloatsSerialised());
  3917. }
  3918. public void SetRootAgentScene(UUID agentID)
  3919. {
  3920. IInventoryTransferModule inv = RequestModuleInterface<IInventoryTransferModule>();
  3921. if (inv == null)
  3922. return;
  3923. inv.SetRootAgentScene(agentID, this);
  3924. EventManager.TriggerSetRootAgentScene(agentID, this);
  3925. }
  3926. public bool NeedSceneCacheClear(UUID agentID)
  3927. {
  3928. IInventoryTransferModule inv = RequestModuleInterface<IInventoryTransferModule>();
  3929. if (inv == null)
  3930. return true;
  3931. return inv.NeedSceneCacheClear(agentID, this);
  3932. }
  3933. public void ObjectSaleInfo(IClientAPI client, UUID agentID, UUID sessionID, uint localID, byte saleType, int salePrice)
  3934. {
  3935. SceneObjectPart part = GetSceneObjectPart(localID);
  3936. if (part == null || part.ParentGroup == null)
  3937. return;
  3938. if (part.ParentGroup.IsDeleted)
  3939. return;
  3940. part = part.ParentGroup.RootPart;
  3941. part.ObjectSaleType = saleType;
  3942. part.SalePrice = salePrice;
  3943. part.ParentGroup.HasGroupChanged = true;
  3944. part.GetProperties(client);
  3945. }
  3946. public bool PerformObjectBuy(IClientAPI remoteClient, UUID categoryID,
  3947. uint localID, byte saleType)
  3948. {
  3949. SceneObjectPart part = GetSceneObjectPart(localID);
  3950. if (part == null)
  3951. return false;
  3952. if (part.ParentGroup == null)
  3953. return false;
  3954. SceneObjectGroup group = part.ParentGroup;
  3955. switch (saleType)
  3956. {
  3957. case 1: // Sell as original (in-place sale)
  3958. uint effectivePerms=group.GetEffectivePermissions();
  3959. if ((effectivePerms & (uint)PermissionMask.Transfer) == 0)
  3960. {
  3961. m_dialogModule.SendAlertToUser(remoteClient, "This item doesn't appear to be for sale");
  3962. return false;
  3963. }
  3964. group.SetOwnerId(remoteClient.AgentId);
  3965. group.SetRootPartOwner(part, remoteClient.AgentId,
  3966. remoteClient.ActiveGroupId);
  3967. List<SceneObjectPart> partList =
  3968. new List<SceneObjectPart>(group.Children.Values);
  3969. if (Permissions.PropagatePermissions())
  3970. {
  3971. foreach (SceneObjectPart child in partList)
  3972. {
  3973. child.Inventory.ChangeInventoryOwner(remoteClient.AgentId);
  3974. child.ApplyNextOwnerPermissions();
  3975. }
  3976. }
  3977. part.ObjectSaleType = 0;
  3978. part.SalePrice = 10;
  3979. group.HasGroupChanged = true;
  3980. part.GetProperties(remoteClient);
  3981. part.ScheduleFullUpdate();
  3982. break;
  3983. case 2: // Sell a copy
  3984. Vector3 inventoryStoredPosition = new Vector3
  3985. (((group.AbsolutePosition.X > (int)Constants.RegionSize)
  3986. ? 250
  3987. : group.AbsolutePosition.X)
  3988. ,
  3989. (group.AbsolutePosition.X > (int)Constants.RegionSize)
  3990. ? 250
  3991. : group.AbsolutePosition.X,
  3992. group.AbsolutePosition.Z);
  3993. Vector3 originalPosition = group.AbsolutePosition;
  3994. group.AbsolutePosition = inventoryStoredPosition;
  3995. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(group);
  3996. group.AbsolutePosition = originalPosition;
  3997. uint perms=group.GetEffectivePermissions();
  3998. if ((perms & (uint)PermissionMask.Transfer) == 0)
  3999. {
  4000. m_dialogModule.SendAlertToUser(remoteClient, "This item doesn't appear to be for sale");
  4001. return false;
  4002. }
  4003. AssetBase asset = CreateAsset(
  4004. group.GetPartName(localID),
  4005. group.GetPartDescription(localID),
  4006. (sbyte)AssetType.Object,
  4007. Utils.StringToBytes(sceneObjectXml));
  4008. AssetService.Store(asset);
  4009. InventoryItemBase item = new InventoryItemBase();
  4010. item.CreatorId = part.CreatorID.ToString();
  4011. item.ID = UUID.Random();
  4012. item.Owner = remoteClient.AgentId;
  4013. item.AssetID = asset.FullID;
  4014. item.Description = asset.Description;
  4015. item.Name = asset.Name;
  4016. item.AssetType = asset.Type;
  4017. item.InvType = (int)InventoryType.Object;
  4018. item.Folder = categoryID;
  4019. uint nextPerms=(perms & 7) << 13;
  4020. if ((nextPerms & (uint)PermissionMask.Copy) == 0)
  4021. perms &= ~(uint)PermissionMask.Copy;
  4022. if ((nextPerms & (uint)PermissionMask.Transfer) == 0)
  4023. perms &= ~(uint)PermissionMask.Transfer;
  4024. if ((nextPerms & (uint)PermissionMask.Modify) == 0)
  4025. perms &= ~(uint)PermissionMask.Modify;
  4026. item.BasePermissions = perms & part.NextOwnerMask;
  4027. item.CurrentPermissions = perms & part.NextOwnerMask;
  4028. item.NextPermissions = part.NextOwnerMask;
  4029. item.EveryOnePermissions = part.EveryoneMask &
  4030. part.NextOwnerMask;
  4031. item.GroupPermissions = part.GroupMask &
  4032. part.NextOwnerMask;
  4033. item.CurrentPermissions |= 8; // Slam!
  4034. item.CreationDate = Util.UnixTimeSinceEpoch();
  4035. if (InventoryService.AddItem(item))
  4036. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  4037. else
  4038. {
  4039. m_dialogModule.SendAlertToUser(remoteClient, "Cannot buy now. Your inventory is unavailable");
  4040. return false;
  4041. }
  4042. break;
  4043. case 3: // Sell contents
  4044. List<UUID> invList = part.Inventory.GetInventoryList();
  4045. bool okToSell = true;
  4046. foreach (UUID invID in invList)
  4047. {
  4048. TaskInventoryItem item1 = part.Inventory.GetInventoryItem(invID);
  4049. if ((item1.CurrentPermissions &
  4050. (uint)PermissionMask.Transfer) == 0)
  4051. {
  4052. okToSell = false;
  4053. break;
  4054. }
  4055. }
  4056. if (!okToSell)
  4057. {
  4058. m_dialogModule.SendAlertToUser(
  4059. remoteClient, "This item's inventory doesn't appear to be for sale");
  4060. return false;
  4061. }
  4062. if (invList.Count > 0)
  4063. MoveTaskInventoryItems(remoteClient.AgentId, part.Name,
  4064. part, invList);
  4065. break;
  4066. }
  4067. return true;
  4068. }
  4069. public void CleanTempObjects()
  4070. {
  4071. List<EntityBase> objs = GetEntities();
  4072. foreach (EntityBase obj in objs)
  4073. {
  4074. if (obj is SceneObjectGroup)
  4075. {
  4076. SceneObjectGroup grp = (SceneObjectGroup)obj;
  4077. if (!grp.IsDeleted)
  4078. {
  4079. if ((grp.RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  4080. {
  4081. if (grp.RootPart.Expires <= DateTime.Now)
  4082. DeleteSceneObject(grp, false);
  4083. }
  4084. }
  4085. }
  4086. }
  4087. }
  4088. public void DeleteFromStorage(UUID uuid)
  4089. {
  4090. m_storageManager.DataStore.RemoveObject(uuid, m_regInfo.RegionID);
  4091. }
  4092. public int GetHealth()
  4093. {
  4094. // Returns:
  4095. // 1 = sim is up and accepting http requests. The heartbeat has
  4096. // stopped and the sim is probably locked up, but a remote
  4097. // admin restart may succeed
  4098. //
  4099. // 2 = Sim is up and the heartbeat is running. The sim is likely
  4100. // usable for people within and logins _may_ work
  4101. //
  4102. // 3 = We have seen a new user enter within the past 4 minutes
  4103. // which can be seen as positive confirmation of sim health
  4104. //
  4105. int health=1; // Start at 1, means we're up
  4106. if ((Environment.TickCount - m_lastUpdate) < 1000)
  4107. health+=1;
  4108. else
  4109. return health;
  4110. // A login in the last 4 mins? We can't be doing too badly
  4111. //
  4112. if ((Environment.TickCount - m_LastLogin) < 240000)
  4113. health++;
  4114. else
  4115. return health;
  4116. CheckHeartbeat();
  4117. return health;
  4118. }
  4119. // This callback allows the PhysicsScene to call back to its caller (the SceneGraph) and
  4120. // update non-physical objects like the joint proxy objects that represent the position
  4121. // of the joints in the scene.
  4122. // This routine is normally called from within a lock (OdeLock) from within the OdePhysicsScene
  4123. // WARNING: be careful of deadlocks here if you manipulate the scene. Remember you are being called
  4124. // from within the OdePhysicsScene.
  4125. protected internal void jointMoved(PhysicsJoint joint)
  4126. {
  4127. // m_parentScene.PhysicsScene.DumpJointInfo(); // non-thread-locked version; we should already be in a lock (OdeLock) when this callback is invoked
  4128. SceneObjectPart jointProxyObject = GetSceneObjectPart(joint.ObjectNameInScene);
  4129. if (jointProxyObject == null)
  4130. {
  4131. jointErrorMessage(joint, "WARNING, joint proxy not found, name " + joint.ObjectNameInScene);
  4132. return;
  4133. }
  4134. // now update the joint proxy object in the scene to have the position of the joint as returned by the physics engine
  4135. SceneObjectPart trackedBody = GetSceneObjectPart(joint.TrackedBodyName); // FIXME: causes a sequential lookup
  4136. if (trackedBody == null) return; // the actor may have been deleted but the joint still lingers around a few frames waiting for deletion. during this time, trackedBody is NULL to prevent further motion of the joint proxy.
  4137. jointProxyObject.Velocity = trackedBody.Velocity;
  4138. jointProxyObject.AngularVelocity = trackedBody.AngularVelocity;
  4139. switch (joint.Type)
  4140. {
  4141. case PhysicsJointType.Ball:
  4142. {
  4143. Vector3 jointAnchor = PhysicsScene.GetJointAnchor(joint);
  4144. Vector3 proxyPos = new Vector3(jointAnchor.X, jointAnchor.Y, jointAnchor.Z);
  4145. jointProxyObject.ParentGroup.UpdateGroupPosition(proxyPos); // schedules the entire group for a terse update
  4146. }
  4147. break;
  4148. case PhysicsJointType.Hinge:
  4149. {
  4150. Vector3 jointAnchor = PhysicsScene.GetJointAnchor(joint);
  4151. // Normally, we would just ask the physics scene to return the axis for the joint.
  4152. // Unfortunately, ODE sometimes returns <0,0,0> for the joint axis, which should
  4153. // never occur. Therefore we cannot rely on ODE to always return a correct joint axis.
  4154. // Therefore the following call does not always work:
  4155. //PhysicsVector phyJointAxis = _PhyScene.GetJointAxis(joint);
  4156. // instead we compute the joint orientation by saving the original joint orientation
  4157. // relative to one of the jointed bodies, and applying this transformation
  4158. // to the current position of the jointed bodies (the tracked body) to compute the
  4159. // current joint orientation.
  4160. if (joint.TrackedBodyName == null)
  4161. {
  4162. jointErrorMessage(joint, "joint.TrackedBodyName is null, joint " + joint.ObjectNameInScene);
  4163. }
  4164. Vector3 proxyPos = new Vector3(jointAnchor.X, jointAnchor.Y, jointAnchor.Z);
  4165. Quaternion q = trackedBody.RotationOffset * joint.LocalRotation;
  4166. jointProxyObject.ParentGroup.UpdateGroupPosition(proxyPos); // schedules the entire group for a terse update
  4167. jointProxyObject.ParentGroup.UpdateGroupRotationR(q); // schedules the entire group for a terse update
  4168. }
  4169. break;
  4170. }
  4171. }
  4172. // This callback allows the PhysicsScene to call back to its caller (the SceneGraph) and
  4173. // update non-physical objects like the joint proxy objects that represent the position
  4174. // of the joints in the scene.
  4175. // This routine is normally called from within a lock (OdeLock) from within the OdePhysicsScene
  4176. // WARNING: be careful of deadlocks here if you manipulate the scene. Remember you are being called
  4177. // from within the OdePhysicsScene.
  4178. protected internal void jointDeactivated(PhysicsJoint joint)
  4179. {
  4180. //m_log.Debug("[NINJA] SceneGraph.jointDeactivated, joint:" + joint.ObjectNameInScene);
  4181. SceneObjectPart jointProxyObject = GetSceneObjectPart(joint.ObjectNameInScene);
  4182. if (jointProxyObject == null)
  4183. {
  4184. jointErrorMessage(joint, "WARNING, trying to deactivate (stop interpolation of) joint proxy, but not found, name " + joint.ObjectNameInScene);
  4185. return;
  4186. }
  4187. // turn the proxy non-physical, which also stops its client-side interpolation
  4188. bool wasUsingPhysics = ((jointProxyObject.ObjectFlags & (uint)PrimFlags.Physics) != 0);
  4189. if (wasUsingPhysics)
  4190. {
  4191. jointProxyObject.UpdatePrimFlags(false, false, true, false); // FIXME: possible deadlock here; check to make sure all the scene alterations set into motion here won't deadlock
  4192. }
  4193. }
  4194. // This callback allows the PhysicsScene to call back to its caller (the SceneGraph) and
  4195. // alert the user of errors by using the debug channel in the same way that scripts alert
  4196. // the user of compile errors.
  4197. // This routine is normally called from within a lock (OdeLock) from within the OdePhysicsScene
  4198. // WARNING: be careful of deadlocks here if you manipulate the scene. Remember you are being called
  4199. // from within the OdePhysicsScene.
  4200. public void jointErrorMessage(PhysicsJoint joint, string message)
  4201. {
  4202. if (joint != null)
  4203. {
  4204. if (joint.ErrorMessageCount > PhysicsJoint.maxErrorMessages)
  4205. return;
  4206. SceneObjectPart jointProxyObject = GetSceneObjectPart(joint.ObjectNameInScene);
  4207. if (jointProxyObject != null)
  4208. {
  4209. SimChat(Utils.StringToBytes("[NINJA]: " + message),
  4210. ChatTypeEnum.DebugChannel,
  4211. 2147483647,
  4212. jointProxyObject.AbsolutePosition,
  4213. jointProxyObject.Name,
  4214. jointProxyObject.UUID,
  4215. false);
  4216. joint.ErrorMessageCount++;
  4217. if (joint.ErrorMessageCount > PhysicsJoint.maxErrorMessages)
  4218. {
  4219. SimChat(Utils.StringToBytes("[NINJA]: Too many messages for this joint, suppressing further messages."),
  4220. ChatTypeEnum.DebugChannel,
  4221. 2147483647,
  4222. jointProxyObject.AbsolutePosition,
  4223. jointProxyObject.Name,
  4224. jointProxyObject.UUID,
  4225. false);
  4226. }
  4227. }
  4228. else
  4229. {
  4230. // couldn't find the joint proxy object; the error message is silently suppressed
  4231. }
  4232. }
  4233. }
  4234. public Scene ConsoleScene()
  4235. {
  4236. if (MainConsole.Instance == null)
  4237. return null;
  4238. if (MainConsole.Instance.ConsoleScene is Scene)
  4239. return (Scene)MainConsole.Instance.ConsoleScene;
  4240. return null;
  4241. }
  4242. public float GetGroundHeight(float x, float y)
  4243. {
  4244. if (x < 0)
  4245. x = 0;
  4246. if (x >= Heightmap.Width)
  4247. x = Heightmap.Width - 1;
  4248. if (y < 0)
  4249. y = 0;
  4250. if (y >= Heightmap.Height)
  4251. y = Heightmap.Height - 1;
  4252. Vector3 p0 = new Vector3(x, y, (float)Heightmap[(int)x, (int)y]);
  4253. Vector3 p1 = new Vector3(p0);
  4254. Vector3 p2 = new Vector3(p0);
  4255. p1.X += 1.0f;
  4256. if (p1.X < Heightmap.Width)
  4257. p1.Z = (float)Heightmap[(int)p1.X, (int)p1.Y];
  4258. p2.Y += 1.0f;
  4259. if (p2.Y < Heightmap.Height)
  4260. p2.Z = (float)Heightmap[(int)p2.X, (int)p2.Y];
  4261. Vector3 v0 = new Vector3(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
  4262. Vector3 v1 = new Vector3(p2.X - p0.X, p2.Y - p0.Y, p2.Z - p0.Z);
  4263. v0.Normalize();
  4264. v1.Normalize();
  4265. Vector3 vsn = new Vector3();
  4266. vsn.X = (v0.Y * v1.Z) - (v0.Z * v1.Y);
  4267. vsn.Y = (v0.Z * v1.X) - (v0.X * v1.Z);
  4268. vsn.Z = (v0.X * v1.Y) - (v0.Y * v1.X);
  4269. vsn.Normalize();
  4270. float xdiff = x - (float)((int)x);
  4271. float ydiff = y - (float)((int)y);
  4272. return (((vsn.X * xdiff) + (vsn.Y * ydiff)) / (-1 * vsn.Z)) + p0.Z;
  4273. }
  4274. private void CheckHeartbeat()
  4275. {
  4276. if (m_firstHeartbeat)
  4277. return;
  4278. if (System.Environment.TickCount - m_lastUpdate > 2000)
  4279. StartTimer();
  4280. }
  4281. }
  4282. }