diff --git a/src/Compress.cs b/src/Compress.cs new file mode 100644 index 0000000..5ef02ae --- /dev/null +++ b/src/Compress.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OpenSim.Modules.DataValue +{ + class Compress + { + public static void CopyTo(Stream src, Stream dest) + { + byte[] bytes = new byte[4096]; + + int cnt; + + while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) + { + dest.Write(bytes, 0, cnt); + } + } + + public static byte[] Zip(string str) + { + var bytes = Encoding.UTF8.GetBytes(str); + + using (var msi = new MemoryStream(bytes)) + using (var mso = new MemoryStream()) + { + using (var gs = new GZipStream(mso, CompressionMode.Compress)) + { + CopyTo(msi, gs); + } + + return mso.ToArray(); + } + } + + public static string Unzip(byte[] bytes) + { + using (var msi = new MemoryStream(bytes)) + using (var mso = new MemoryStream()) + { + using (var gs = new GZipStream(msi, CompressionMode.Decompress)) + { + CopyTo(gs, mso); + } + + return Encoding.UTF8.GetString(mso.ToArray()); + } + } + } +} diff --git a/src/DataValue.cs b/src/DataValue.cs new file mode 100644 index 0000000..6d9a73f --- /dev/null +++ b/src/DataValue.cs @@ -0,0 +1,211 @@ +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")] + + 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 + { + if ((m_config = source.Configs["XEngine"]) == null) + { + // There is no configuration, the module is disabled + // m_log.InfoFormat("[JsonStoreScripts] no configuration info"); + return; + } + + m_dataValueDirectory = m_config.GetString("DataValueStorageDirectory", m_dataValueDirectory); + m_enabled = m_config.GetBoolean("Enabled", m_enabled); + } + catch (Exception e) + { + m_log.ErrorFormat("[" + Name + "]: initialization error: {0}", e.Message); + return; + } + + if (m_enabled) + m_log.DebugFormat("[" + Name + "]: module is enabled"); + } + + public void RegionLoaded(Scene scene) + { + if (m_enabled) + { + m_scene = scene; + m_scriptModule = m_scene.RequestModuleInterface(); + if (m_scriptModule == null) + { + m_log.ErrorFormat("[" + Name + "]: Failed to load IScriptModuleComms!"); + m_enabled = false; + return; + } + + try + { + m_scriptModule.RegisterScriptInvocations(this); + } + 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 + + } +}