using OpenMetaverse; using OpenSim.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OpenSim.Modules.Appearance2Avatar.helper { public class NotecardCache { protected class Notecard { public string[] text; public DateTime lastRef; } private static Dictionary m_Notecards = new Dictionary(); public static void Cache(UUID assetID, byte[] text) { CheckCache(); lock (m_Notecards) { if (m_Notecards.ContainsKey(assetID)) return; Notecard nc = new Notecard(); nc.lastRef = DateTime.Now; nc.text = SLUtil.ParseNotecardToArray(text); m_Notecards[assetID] = nc; } } public static bool IsCached(UUID assetID) { lock (m_Notecards) { return m_Notecards.ContainsKey(assetID); } } public static int GetLines(UUID assetID) { if (!IsCached(assetID)) return -1; lock (m_Notecards) { m_Notecards[assetID].lastRef = DateTime.Now; return m_Notecards[assetID].text.Length; } } /// /// Get a notecard line. /// /// /// Lines start at index 0 /// public static string GetLine(UUID assetID, int lineNumber) { if (lineNumber < 0) return ""; string data; if (!IsCached(assetID)) return ""; lock (m_Notecards) { m_Notecards[assetID].lastRef = DateTime.Now; if (lineNumber >= m_Notecards[assetID].text.Length) return "\n\n\n"; data = m_Notecards[assetID].text[lineNumber]; return data; } } /// /// Get a notecard line. /// /// /// Lines start at index 0 /// /// Maximum length of the returned line. /// /// /// If the line length is longer than , /// the return string will be truncated. /// public static string GetLine(UUID assetID, int lineNumber, int maxLength) { string line = GetLine(assetID, lineNumber); if (line.Length > maxLength) line = line.Substring(0, maxLength); return line; } public static void CheckCache() { lock (m_Notecards) { foreach (UUID key in new List(m_Notecards.Keys)) { Notecard nc = m_Notecards[key]; if (nc.lastRef.AddSeconds(30) < DateTime.Now) m_Notecards.Remove(key); } } } } }