OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
DOExampleModule.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using log4net;
32 using Mono.Addins;
33 using Nini.Config;
34 using OpenMetaverse;
35 using OpenMetaverse.Packets;
36 using OpenMetaverse.StructuredData;
37 using OpenSim.Framework;
38 using OpenSim.Region.Framework;
39 using OpenSim.Region.CoreModules.Framework.DynamicAttributes.DAExampleModule;
40 using OpenSim.Region.Framework.Interfaces;
41 using OpenSim.Region.Framework.Scenes;
42 
43 namespace OpenSim.Region.Framework.DynamicAttributes.DOExampleModule
44 {
48  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DOExampleModule")]
50  {
51  public class MyObject
52  {
53  public int Moves { get; set; }
54 
55  public MyObject(int moves)
56  {
57  Moves = moves;
58  }
59  }
60 
61  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
62 
63  private static readonly bool ENABLED = false; // enable for testing
64 
65  private Scene m_scene;
66  private IDialogModule m_dialogMod;
67 
68  public string Name { get { return "DO"; } }
69  public Type ReplaceableInterface { get { return null; } }
70 
71  public void Initialise(IConfigSource source) {}
72 
73  public void AddRegion(Scene scene)
74  {
75  if (ENABLED)
76  {
77  m_scene = scene;
78  m_scene.EventManager.OnObjectAddedToScene += OnObjectAddedToScene;
79  m_scene.EventManager.OnSceneGroupMove += OnSceneGroupMove;
80  m_dialogMod = m_scene.RequestModuleInterface<IDialogModule>();
81  }
82  }
83 
84  public void RemoveRegion(Scene scene)
85  {
86  if (ENABLED)
87  {
88  m_scene.EventManager.OnSceneGroupMove -= OnSceneGroupMove;
89  }
90  }
91 
92  public void RegionLoaded(Scene scene) {}
93 
94  public void Close()
95  {
96  RemoveRegion(m_scene);
97  }
98 
99  private void OnObjectAddedToScene(SceneObjectGroup so)
100  {
101  SceneObjectPart rootPart = so.RootPart;
102 
103  OSDMap attrs;
104 
105  int movesSoFar = 0;
106 
107 // Console.WriteLine("Here for {0}", so.Name);
108 
110  {
111  movesSoFar = attrs["moves"].AsInteger();
112 
113  m_log.DebugFormat(
114  "[DO EXAMPLE MODULE]: Found saved moves {0} for {1} in {2}", movesSoFar, so.Name, m_scene.Name);
115  }
116 
117  rootPart.DynObjs.Add(DAExampleModule.Namespace, Name, new MyObject(movesSoFar));
118  }
119 
120  private bool OnSceneGroupMove(UUID groupId, Vector3 delta)
121  {
122  SceneObjectGroup so = m_scene.GetSceneObjectGroup(groupId);
123 
124  if (so == null)
125  return true;
126 
127  object rawObj = so.RootPart.DynObjs.Get(Name);
128 
129  if (rawObj != null)
130  {
131  MyObject myObj = (MyObject)rawObj;
132 
133  m_dialogMod.SendGeneralAlert(string.Format("{0} {1} moved {2} times", so.Name, so.UUID, ++myObj.Moves));
134  }
135 
136  return true;
137  }
138  }
139 }
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
bool TryGetStore(string ns, string storeName, out OSDMap store)
Definition: DAMap.cs:281
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
OpenMetaverse.StructuredData.OSDMap OSDMap
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Example module for experimenting with and demonstrating dynamic object ideas.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
Interactive OpenSim region server
Definition: OpenSim.cs:55
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
DAMap DynAttrs
Dynamic attributes can be created and deleted as required.