IRegionModule

From OpenSimulator

Revision as of 17:26, 3 December 2010 by Justincc (Talk | contribs)

Jump to: navigation, search


Introduction

Region modules are .net/mono DLLs. During initialization of the simulator, the OpenSim bin directory (bin/) and the scriptengines (bin/ScriptEngines) directory are scanned for DLLs, in an attempt to load region modules stored there.

Region modules execute within the heart of the simulator and have access to all its facilties. Typically, region modules register for a number of events, e.g. chat messages, user logins, texture transfers, and take what ever steps are appropriate for the purposes of the module.

There are two types of region module.

  • Non-shared modules where a separate module is created for each region/scene
  • Shared region modules, where a single module is shared between all regions/scenes running on the same simulator.

Region Modules require a few basic things:

  • The Base Interface
  • Some callbacks for OpenSim events

The Base Interfaces

Region modules must implement INonSharedRegionModule or ISharedRegionModule as appropriate. However, both these interfaces extend IRegionBase, defined as follows.

public interface IRegionModuleBase
{
     string Name { get; }
     Type ReplaceableInterface { get; }
     void Initialise(IConfigSource source);
     void AddRegion(Scene scene);
     void RemoveRegion(Scene scene);
     void RegionLoaded(Scene scene);
     void Close();
}
Method Description
Name This name is shown when the console command "show modules" is run. For example, "Sim Chat Module" or "The Best Region Module Ever".
ReplaceableInterface If this is not null, then the module is not loaded if any other module implements the given interface. One use for this is to provide 'stub' functionality implementations that are only active if no other module is present
Initialise This method is called immediately after the region module has been loaded into the runtime, before it has been added to a scene or scenes. IConfigSource is a Nini class that contains the concatentation of config parameters from OpenSim.ini, OpenSimDefaults.ini and the appropriate ini files in bin/config-include
AddRegion This method is called when a region is added to the module. For shared modules this will happen multiple times (one for each module). For non-shared modules this will happen only once. The module can store the scene reference and use it later to reach and invoke OpenSim internals and interfaces.
RemoveRegion Called when a region is removed from a module. For shared modules this can happen multiple times. For non-shared region modules this will happen only once and should shortly be followed by a Close(). On simulator shutdown, this method will be called before Close(). RemoveRegion() can also be called if a region/scene is manually removed while the simulator is running.
RegionLoaded Called when all modules have been added for a particular scene/region. Since all other modules are now loaded, this gives the module an opportunity to obtain interfaces or subscribe to events on other modules. Called once for a non-shared region module and multiple times for shared region modules.
Close This method will be invoked when the sim is closing down.

INonSharedRegionModule itself contains no methods, being defined simply as

public interface INonSharedRegionModule : IRegionModuleBase
{
}

ISharedRegionModule has one additional method.

public interface ISharedRegionModule : IRegionModuleBase
{
    void PostInitialise();
}
Method Description
PostInitialise Called after Initialise() but before modules are added. This may be a bug - it might be that this should really be called after all the regions have been added.

Implementing a region interface doesn't give you much beyond a piece of loaded code. In order to do anything useful you'll need to use a combination of events and calls to OpenSim interfaces and internals.

Accessible Objects

Note: these are internal interfaces, and will change in the future, probably for the better. We expect these to stabilize over time, but for now this point in time snapshot is probably helpful.

In the Initialise routine you get access to the scene object for the region, from here you can spider down into the scene and get access to many other objects of interest.

  • scene.GetEntities() - returns a list of all the Entities in the environment. This will be a combined list of SceneObjectGroups (prim sets) and ScenePresences (avatars).
  • scene.GetAvatars() - get only the avatars in the scene (very handy for sending messages to clients)
  • scene.EventManager - this is the object from which you can register callbacks for scene events. Some examples provided in a little bit
  • scene.RegionInfo - properties about the region

Registering for Events

Taking the SunModule as an example we can see the following code:

In Initialise():

...
m_scene.EventManager.OnFrame += SunUpdate;
...

Pretty simple, we just got the EventManager and registered the SunUpdate method as a callback for the OnFrame event. OnFrame is triggered every time there is a render frame in opensim, which is about 20 times per second. If you are firing on the OnFrame event you need to do something small, or punt most of the time, as you'll negatively impact the performance of the system otherwise.

Now, for that function...

public void SunUpdate()
{
    // this code just means only do this on every 1000th frame, and don't do it if the sun is in a fixed possition
    if (((m_frame++%m_frame_mod) != 0) || !ready || sunFixed)
    {
        return;
    }
 
    GenSunPos();        // Generate shared values once
 
    List<ScenePresence> avatars = m_scene.GetAvatars();
    foreach (ScenePresence avatar in avatars)
    {
        if (!avatar.IsChildAgent)
            avatar.ControllingClient.SendSunPos(Position, Velocity, CurrentTime, SecondsPerSunCycle, SecondsPerYear, OrbitalPosition);
    }
 
    // set estate settings for region access to sun position
    m_scene.RegionInfo.RegionSettings.SunVector = Position;
}

SunUpdate() takes no parameter (some events may require them). It only fires every 1000th frame by default (m_frame_mod = 1000 in this module), so it doesn't take too many cycles.

In order for the sun position to change for the clients, they need to be told that it changes. This is done by getting a list of all the Avatars from the scene, then sending the Sun Position to each of them in turn. It is important to check to see if the avatar is a ChildAgent, otherwise you will generate zombies in opensim world.

Where to go from here

  • Getting Started with Region Modules -- the Hello World of OpenSim application development
  • Read the source for existing opensim core modules. These are in the OpenSim.Region.CoreModules and OpenSim.Region.OptionalModules projects.
  • Read the source code for the EventManager class in the OpenSim.Region.Framework.Scenes project. These list the many events that exist. Some modules may also export their own events (e.g. OnInventoryArchiveSaved in the InventoryArchiverModule at OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/).
  • Help write some examples here. OpenSim grows with your contributions.
Personal tools
General
About This Wiki