OpenSim.Modules.DataValue/src/DataValue.cs

227 lines
6.9 KiB
C#

using log4net;
using Mono.Addins;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
[assembly: Addin("DataValueModule", "0.1")]
[assembly: AddinDependency("OpenSim.Region.Framework", OpenSim.VersionInfo.VersionNumber)]
namespace OpenSim.Modules.DataValue
{
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DataValueModule")]
public class DataValue : INonSharedRegionModule
{
#region Region Module
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Scene m_scene = null;
private IConfig m_config = null;
private bool m_enabled = true;
private string m_dataValueDirectory = "./ScriptDataValue";
private IScriptModuleComms m_scriptModule;
public string Name
{
get { return "DataValueModule"; }
}
public Type ReplaceableInterface
{
get { return null; }
}
public void AddRegion(Scene scene)
{
}
public void Close()
{
}
public void Initialise(IConfigSource source)
{
try
{
m_config = source.Configs["XEngine"];
if (m_config != null)
{
m_dataValueDirectory = m_config.GetString("DataValueStorageDirectory", m_dataValueDirectory);
m_enabled = m_config.GetBoolean("EnabledDataStorage", m_enabled);
m_log.Info("[" + Name + "]: Data storage = " + m_dataValueDirectory);
}
else
{
m_log.Error("[" + Name + "]: Cant find config.");
}
}
catch (Exception e)
{
m_log.ErrorFormat("[" + Name + "]: initialization error: {0}", e.Message);
return;
}
if (m_enabled)
{
m_log.Info("[" + Name + "]: module is enabled");
}
else
{
m_log.Info("[" + Name + "]: module is disabled");
}
}
public void RegionLoaded(Scene scene)
{
if (m_enabled)
{
m_log.Info("[" + Name + "]: Load region " + scene.Name);
m_scene = scene;
m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>();
if (m_scriptModule == null)
{
m_log.ErrorFormat("[" + Name + "]: Failed to load IScriptModuleComms!");
m_enabled = false;
return;
}
try
{
m_scriptModule.RegisterScriptInvocation(this, "osGetDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osSetDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osDeleteDataValue");
m_scriptModule.RegisterScriptInvocation(this, "osCheckDataValue");
}
catch (Exception e)
{
m_log.WarnFormat("[" + Name + "]: script method registration failed; {0}", e.Message);
m_enabled = false;
}
}
}
public void RemoveRegion(Scene scene)
{
}
#endregion
#region Script Funktions
private string getFilePath(UUID host, string index, bool privat)
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(host);
if (_host != null)
{
string _nameSpace = _host.GroupID.ToString().Trim().ToUpper().Replace("-", "");
if(privat == true)
_nameSpace = _host.OwnerID.ToString().Trim().ToUpper().Replace("-", "");
if (!Directory.Exists(m_dataValueDirectory))
Directory.CreateDirectory(m_dataValueDirectory);
if (!Directory.Exists(m_dataValueDirectory + "/" + _nameSpace))
Directory.CreateDirectory(m_dataValueDirectory + "/" + _nameSpace);
string _storageKey = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCIIEncoding.ASCII.GetBytes(index.Trim().ToUpper()))).Replace("-", "");
return m_dataValueDirectory + "/" + _nameSpace + "/" + _storageKey + ".gz";
}
return null;
}
[ScriptInvocation]
public string osGetDataValue(UUID hostID, UUID scriptID, string key)
{
return osGetDataValue(hostID, scriptID, key, false);
}
[ScriptInvocation]
public string osGetDataValue(UUID hostID, UUID scriptID, string key, bool personal)
{
string _filePath = getFilePath(hostID, key, personal);
FileInfo file = new FileInfo(_filePath);
if (file.Exists)
return Compress.Unzip(File.ReadAllBytes(file.FullName));
return "";
}
[ScriptInvocation]
public void osSetDataValue(UUID hostID, UUID scriptID, string key, string value)
{
osSetDataValue(hostID, scriptID, key, value, false);
}
[ScriptInvocation]
public void osSetDataValue(UUID hostID, UUID scriptID, string key, string value, bool personal)
{
string _filePath = getFilePath(hostID, key, personal);
FileInfo file = new FileInfo(_filePath);
File.WriteAllBytes(file.FullName, Compress.Zip(value));
}
[ScriptInvocation]
public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, string value)
{
osDeleteDataValue(hostID, scriptID, key, false);
}
[ScriptInvocation]
public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, bool personal)
{
string _filePath = getFilePath(hostID, key, personal);
FileInfo file = new FileInfo(_filePath);
if (file.Exists)
file.Delete();
}
[ScriptInvocation]
public bool osCheckDataValue(UUID hostID, UUID scriptID, string key, string value)
{
return osCheckDataValue(hostID, scriptID, key, false);
}
[ScriptInvocation]
public bool osCheckDataValue(UUID hostID, UUID scriptID, string key, bool personal)
{
string _filePath = getFilePath(hostID, key, personal);
FileInfo file = new FileInfo(_filePath);
if (file.Exists)
return true;
return false;
}
#endregion
}
}