From 3e16a0fbdd9477f24a93e0e70311bfca7995e339 Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Tue, 9 Aug 2011 00:12:41 +0100
Subject: factor out common notecard caching code from 3 methods.
---
.../Shared/Api/Implementation/OSSL_Api.cs | 167 +++++++++------------
1 file changed, 74 insertions(+), 93 deletions(-)
(limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Implementation')
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
index 32ad21f..154179c 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
@@ -1733,7 +1733,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
SaveNotecard(notecardName, notecardData.ToString());
}
- protected void SaveNotecard(string notecardName, string notecardData)
+ ///
+ /// Save a notecard to prim inventory.
+ ///
+ ///
+ ///
+ /// Prim inventory item created.
+ protected TaskInventoryItem SaveNotecard(string notecardName, string notecardData)
{
// Create new asset
AssetBase asset = new AssetBase(UUID.Random(), notecardName, (sbyte)AssetType.Notecard, m_host.OwnerID.ToString());
@@ -1770,6 +1776,66 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
taskItem.AssetID = asset.FullID;
m_host.Inventory.AddInventoryItem(taskItem, false);
+
+ return taskItem;
+ }
+
+ ///
+ /// Load the notecard data found at the given prim inventory item name or asset uuid.
+ ///
+ ///
+ /// The text loaded. Null if no notecard was found.
+ protected string LoadNotecard(string notecardNameOrUuid)
+ {
+ UUID assetID = CacheNotecard(notecardNameOrUuid);
+ StringBuilder notecardData = new StringBuilder();
+
+ for (int count = 0; count < NotecardCache.GetLines(assetID); count++)
+ notecardData.Append(NotecardCache.GetLine(assetID, count, 255) + "\n");
+
+ return notecardData.ToString();
+ }
+
+ ///
+ /// Cache a notecard's contents.
+ ///
+ ///
+ ///
+ /// The asset id of the notecard, which is used for retrieving the cached data.
+ /// UUID.Zero if no asset could be found.
+ ///
+ protected UUID CacheNotecard(string notecardNameOrUuid)
+ {
+ UUID assetID = UUID.Zero;
+ StringBuilder notecardData = new StringBuilder();
+
+ if (!UUID.TryParse(notecardNameOrUuid, out assetID))
+ {
+ foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
+ {
+ if (item.Type == 7 && item.Name == notecardNameOrUuid)
+ {
+ assetID = item.AssetID;
+ }
+ }
+ }
+
+ if (assetID == UUID.Zero)
+ return UUID.Zero;
+
+ if (!NotecardCache.IsCached(assetID))
+ {
+ AssetBase a = World.AssetService.Get(assetID.ToString());
+
+ if (a == null)
+ return UUID.Zero;
+
+ System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
+ string data = enc.GetString(a.Data);
+ NotecardCache.Cache(assetID, data);
+ };
+
+ return assetID;
}
///
@@ -1790,18 +1856,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecardLine");
m_host.AddScriptLPS(1);
- UUID assetID = UUID.Zero;
-
- if (!UUID.TryParse(name, out assetID))
- {
- foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
- {
- if (item.Type == 7 && item.Name == name)
- {
- assetID = item.AssetID;
- }
- }
- }
+ UUID assetID = CacheNotecard(name);
if (assetID == UUID.Zero)
{
@@ -1809,22 +1864,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return "ERROR!";
}
- if (!NotecardCache.IsCached(assetID))
- {
- AssetBase a = World.AssetService.Get(assetID.ToString());
- if (a != null)
- {
- System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
- string data = enc.GetString(a.Data);
- NotecardCache.Cache(assetID, data);
- }
- else
- {
- OSSLShoutError("Notecard '" + name + "' could not be found.");
- return "ERROR!";
- }
- };
-
return NotecardCache.GetLine(assetID, line, 255);
}
@@ -1845,48 +1884,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecard");
m_host.AddScriptLPS(1);
- UUID assetID = UUID.Zero;
- string NotecardData = "";
-
- if (!UUID.TryParse(name, out assetID))
- {
- foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
- {
- if (item.Type == 7 && item.Name == name)
- {
- assetID = item.AssetID;
- }
- }
- }
+ string text = LoadNotecard(name);
- if (assetID == UUID.Zero)
+ if (text == null)
{
OSSLShoutError("Notecard '" + name + "' could not be found.");
return "ERROR!";
}
-
- if (!NotecardCache.IsCached(assetID))
- {
- AssetBase a = World.AssetService.Get(assetID.ToString());
- if (a != null)
- {
- System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
- string data = enc.GetString(a.Data);
- NotecardCache.Cache(assetID, data);
- }
- else
- {
- OSSLShoutError("Notecard '" + name + "' could not be found.");
- return "ERROR!";
- }
- };
-
- for (int count = 0; count < NotecardCache.GetLines(assetID); count++)
+ else
{
- NotecardData += NotecardCache.GetLine(assetID, count, 255) + "\n";
+ return text;
}
-
- return NotecardData;
}
///
@@ -1906,18 +1914,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNumberOfNotecardLines");
m_host.AddScriptLPS(1);
- UUID assetID = UUID.Zero;
-
- if (!UUID.TryParse(name, out assetID))
- {
- foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
- {
- if (item.Type == 7 && item.Name == name)
- {
- assetID = item.AssetID;
- }
- }
- }
+ UUID assetID = CacheNotecard(name);
if (assetID == UUID.Zero)
{
@@ -1925,22 +1922,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return -1;
}
- if (!NotecardCache.IsCached(assetID))
- {
- AssetBase a = World.AssetService.Get(assetID.ToString());
- if (a != null)
- {
- System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
- string data = enc.GetString(a.Data);
- NotecardCache.Cache(assetID, data);
- }
- else
- {
- OSSLShoutError("Notecard '" + name + "' could not be found.");
- return -1;
- }
- };
-
return NotecardCache.GetLines(assetID);
}
@@ -2412,7 +2393,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
}
}
});
-
+
return result;
}
--
cgit v1.1