factor out common notecard caching code from 3 methods.

bulletsim
Justin Clark-Casey (justincc) 2011-08-09 00:12:41 +01:00
parent 78d8ce3816
commit 3e16a0fbdd
1 changed files with 74 additions and 93 deletions

View File

@ -1733,7 +1733,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
SaveNotecard(notecardName, notecardData.ToString()); SaveNotecard(notecardName, notecardData.ToString());
} }
protected void SaveNotecard(string notecardName, string notecardData) /// <summary>
/// Save a notecard to prim inventory.
/// </summary>
/// <param name="notecardName"></param>
/// <param name="notecardData"></param>
/// <returns>Prim inventory item created.</returns>
protected TaskInventoryItem SaveNotecard(string notecardName, string notecardData)
{ {
// Create new asset // Create new asset
AssetBase asset = new AssetBase(UUID.Random(), notecardName, (sbyte)AssetType.Notecard, m_host.OwnerID.ToString()); 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; taskItem.AssetID = asset.FullID;
m_host.Inventory.AddInventoryItem(taskItem, false); m_host.Inventory.AddInventoryItem(taskItem, false);
return taskItem;
}
/// <summary>
/// Load the notecard data found at the given prim inventory item name or asset uuid.
/// </summary>
/// <param name="notecardNameOrUuid"></param>
/// <returns>The text loaded. Null if no notecard was found.</returns>
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();
}
/// <summary>
/// Cache a notecard's contents.
/// </summary>
/// <param name="notecardNameOrUuid"></param>
/// <returns>
/// The asset id of the notecard, which is used for retrieving the cached data.
/// UUID.Zero if no asset could be found.
/// </returns>
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;
} }
/// <summary> /// <summary>
@ -1790,18 +1856,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecardLine"); CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecardLine");
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
UUID assetID = UUID.Zero; UUID assetID = CacheNotecard(name);
if (!UUID.TryParse(name, out assetID))
{
foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
{
if (item.Type == 7 && item.Name == name)
{
assetID = item.AssetID;
}
}
}
if (assetID == UUID.Zero) if (assetID == UUID.Zero)
{ {
@ -1809,22 +1864,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return "ERROR!"; 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); return NotecardCache.GetLine(assetID, line, 255);
} }
@ -1845,48 +1884,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecard"); CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNotecard");
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
UUID assetID = UUID.Zero; string text = LoadNotecard(name);
string NotecardData = "";
if (!UUID.TryParse(name, out assetID)) if (text == null)
{
foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
{
if (item.Type == 7 && item.Name == name)
{
assetID = item.AssetID;
}
}
}
if (assetID == UUID.Zero)
{ {
OSSLShoutError("Notecard '" + name + "' could not be found."); OSSLShoutError("Notecard '" + name + "' could not be found.");
return "ERROR!"; 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 else
{ {
OSSLShoutError("Notecard '" + name + "' could not be found."); return text;
return "ERROR!";
} }
};
for (int count = 0; count < NotecardCache.GetLines(assetID); count++)
{
NotecardData += NotecardCache.GetLine(assetID, count, 255) + "\n";
}
return NotecardData;
} }
/// <summary> /// <summary>
@ -1906,18 +1914,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNumberOfNotecardLines"); CheckThreatLevel(ThreatLevel.VeryHigh, "osGetNumberOfNotecardLines");
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
UUID assetID = UUID.Zero; UUID assetID = CacheNotecard(name);
if (!UUID.TryParse(name, out assetID))
{
foreach (TaskInventoryItem item in m_host.TaskInventory.Values)
{
if (item.Type == 7 && item.Name == name)
{
assetID = item.AssetID;
}
}
}
if (assetID == UUID.Zero) if (assetID == UUID.Zero)
{ {
@ -1925,22 +1922,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
return -1; 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); return NotecardCache.GetLines(assetID);
} }