Files
opengenesislinkserver/addons/NexTransit/NexTransitRegionModule.cs
T
OpenGenesis (MPL 2.0) 8e57845cf2 feat(gen1): three standalone city addons NexTraffic, NexTransit and NexSmartLight (#338)
* feat(gen1): deterministic separate traffic, transit and smart light domain model

* feat(gen1): deterministic separate traffic, transit and smart light domain model

* feat(gen1): deterministic separate traffic, transit and smart light domain model

* feat(gen1): implement independent simulator-wide NexTraffic region module

* feat(gen1): implement independent simulator-wide NexTransit region module

* feat(gen1): implement independent simulator-wide NexSmartLight region module

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* build(gen1): add independent opt-in city addon configs and deployment wrappers

* feat(gen1): inworld LSL traffic lights, transit departures and city street lamps

* feat(gen1): inworld LSL traffic lights, transit departures and city street lamps

* feat(gen1): inworld LSL traffic lights, transit departures and city street lamps

* fix(gen1): use portable OpenSim LSL syntax for lighting and traffic state

* fix(gen1): use portable OpenSim LSL syntax for lighting and traffic state

* test(gen1): regression for junction safety, timetable rollovers, DST lighting and plugin boundaries

* test(gen1): regression for junction safety, timetable rollovers, DST lighting and plugin boundaries

* test(gen1): regression for junction safety, timetable rollovers, DST lighting and plugin boundaries

* ci(gen1): build NexTraffic, NexTransit and NexSmartLight independently and regression-test models

* docs(gen1): deployment and precise MVP scope for NexTraffic

* docs(gen1): deployment and precise MVP scope for NexTransit

* docs(gen1): deployment and precise MVP scope for NexSmartLight

* fix(gen1): resolve city addon helper at repository root, not parent directory

* test(gen1): guard standalone addon builder repository root discovery
2026-10-28 19:46:02 +01:00

143 lines
5.6 KiB
C#

// SPDX-License-Identifier: MPL-2.0
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using log4net;
using Mono.Addins;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
[assembly: Addin("NexTransit", Namespace = "OpenGenesisLINK", Version = "0.1.0")]
[assembly: AddinDependency("::OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]
namespace OpenGenesisLINK.Addons.NexTransit
{
/// <summary>
/// Opt-in simulator-wide controller; broadcasts read-only state to
/// in-world listeners. Never rezzes, moves, or deletes resident objects.
/// </summary>
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "NexTransit")]
public sealed class NexTransitRegionModule : ISharedRegionModule
{
private sealed class RegionRuntime
{
public Scene Scene;
public EventManager.OnFrameDelegate OnFrame;
public long NextPublish;
}
private static readonly ILog Log = LogManager.GetLogger(typeof(NexTransitRegionModule));
private readonly ConcurrentDictionary<UUID, RegionRuntime> m_Regions = new();
private volatile bool m_Enabled;
private volatile bool m_Closing;
private int m_Channel;
private int m_RefreshSeconds;
private List<TransitRoute> m_Routes;
private TimeZoneInfo m_TimeZone;
private int m_MaxDepartures;
public string Name => "NexTransit";
public Type ReplaceableInterface => null;
public void Initialise(IConfigSource source)
{
IConfig cfg = source?.Configs["NexTransit"];
m_Enabled = cfg?.GetBoolean("Enabled", false) ?? false;
if (!m_Enabled) return;
try
{
m_Channel = cfg.GetInt("LSLChannel", -835422);
m_RefreshSeconds = Math.Clamp(
cfg.GetInt("RefreshSeconds", 30), 30, 300);
if (m_Channel >= 0 || m_Channel == -835311 ||
m_Channel == -835421 && "NexTransit" != "NexTraffic" ||
m_Channel == -835422 && "NexTransit" != "NexTransit" ||
m_Channel == -835423 && "NexTransit" != "NexSmartLight")
throw new FormatException("Use the dedicated negative LSL channel");
m_Routes = NexTransitModel.ParseRoutes(cfg.GetString(
"Routes", "101,Magdeburg,10,0,*;150,Jenseits,20,15,*"));
m_MaxDepartures = Math.Clamp(cfg.GetInt("MaxDepartures", 5), 1, 10);
m_TimeZone = TimeZoneInfo.FindSystemTimeZoneById(
cfg.GetString("TimeZone", "Europe/Berlin").Trim());
Log.InfoFormat("[NEXTRANSIT]: simulator-wide service enabled, channel={0}", m_Channel);
}
catch (Exception e)
{
Log.Error("[NEXTRANSIT]: invalid configuration; module disabled.", e);
m_Enabled = false;
}
}
public void PostInitialise() { }
public void AddRegion(Scene scene)
{
if (!m_Enabled || m_Closing || scene == null) return;
var runtime = new RegionRuntime { Scene = scene };
runtime.OnFrame = () => Publish(runtime);
m_Regions.TryAdd(scene.RegionInfo.RegionID, runtime);
}
public void RegionLoaded(Scene scene)
{
if (!m_Enabled || m_Closing || scene == null) return;
if (m_Regions.TryGetValue(scene.RegionInfo.RegionID, out RegionRuntime runtime))
{
scene.EventManager.OnFrame -= runtime.OnFrame;
scene.EventManager.OnFrame += runtime.OnFrame;
}
}
private void Publish(RegionRuntime state)
{
if (!m_Enabled || m_Closing) return;
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (now < state.NextPublish) return;
// Advance before any work so missing chat modules do not cause a
// per-frame spin or log flood. One publication per interval.
state.NextPublish = now + m_RefreshSeconds;
try
{
IWorldComm comm = state.Scene.RequestModuleInterface<IWorldComm>();
if (comm == null) return;
Vector3 center = new Vector3(
state.Scene.RegionInfo.RegionSizeX / 2f,
state.Scene.RegionInfo.RegionSizeY / 2f, 20f);
DateTime local = TimeZoneInfo.ConvertTime(
DateTimeOffset.UtcNow, m_TimeZone).DateTime;
List<Departure> departures = NexTransitModel.Upcoming(
local, m_Routes, state.Scene.RegionInfo.RegionName,
m_MaxDepartures);
comm.DeliverMessage(ChatTypeEnum.Region, m_Channel,
"NexTransit", UUID.Zero,
NexTransitModel.Wire(local, departures), center);
}
catch (Exception e)
{
Log.Warn("[NEXTRANSIT]: region state publication failed.", e);
}
}
public void RemoveRegion(Scene scene)
{
if (scene != null &&
m_Regions.TryRemove(scene.RegionInfo.RegionID, out RegionRuntime runtime))
scene.EventManager.OnFrame -= runtime.OnFrame;
}
public void Close()
{
m_Closing = true;
foreach (RegionRuntime runtime in m_Regions.Values)
runtime.Scene.EventManager.OnFrame -= runtime.OnFrame;
m_Regions.Clear();
m_Enabled = false;
}
}
}