2006-11-01 00:14:43 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2006-12-19 23:13:04 +00:00
|
|
|
|
2006-11-01 00:14:43 +00:00
|
|
|
using IA_SimpleInventory;
|
|
|
|
|
|
|
|
|
|
using libsecondlife;
|
|
|
|
|
using libsecondlife.AssetSystem;
|
|
|
|
|
|
|
|
|
|
namespace IA_TestAsyncImage
|
|
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
class TestAsync
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
private SecondLife _Client;
|
|
|
|
|
private ManualResetEvent ConnectedSignal = new ManualResetEvent(false);
|
|
|
|
|
|
2006-11-01 00:14:43 +00:00
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
private Queue<LLUUID> TextureQueue = new Queue<LLUUID>();
|
2006-11-01 00:14:43 +00:00
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
private string OutputDirectory = "IA_TestAsyncImages";
|
2006-11-01 00:14:43 +00:00
|
|
|
|
|
|
|
|
[STAThread]
|
2006-12-19 23:13:04 +00:00
|
|
|
static void Main(string[] args)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
|
|
|
|
TestAsync app = new TestAsync();
|
|
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
app._Client.Objects.OnNewPrim += new ObjectManager.NewPrimCallback(app.Objects_OnNewPrim);
|
|
|
|
|
app._Client.Objects.OnNewAvatar += new ObjectManager.NewAvatarCallback(app.Objects_OnNewAvatar);
|
2006-11-01 00:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Connect(args[0], args[1], args[2]);
|
2006-12-19 23:13:04 +00:00
|
|
|
if (app.ConnectedSignal.WaitOne(TimeSpan.FromMinutes(1), false))
|
|
|
|
|
{
|
|
|
|
|
app.doStuff();
|
|
|
|
|
app.Disconnect();
|
|
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
Console.WriteLine("Done...");
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
public TestAsync()
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
try
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client = new SecondLife();
|
|
|
|
|
_Client.Images = new ImageManager(_Client, ImageManager.CacheTypes.Disk, OutputDirectory);
|
|
|
|
|
_Client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
// Error initializing the client
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-11-02 17:59:30 +00:00
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
private void Objects_OnNewAvatar(Simulator simulator, Avatar avatar, ulong regionHandle, ushort timeDilation)
|
|
|
|
|
{
|
|
|
|
|
if (avatar.FirstLifeImage != null)
|
|
|
|
|
{
|
|
|
|
|
if (_Client.Images.isCachedImage(avatar.FirstLifeImage) == false)
|
2006-11-02 17:59:30 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client.Images.RequestImageAsync(avatar.FirstLifeImage);
|
2006-11-02 17:59:30 +00:00
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
2006-12-19 23:13:04 +00:00
|
|
|
|
|
|
|
|
if (avatar.ProfileImage != null)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
if (_Client.Images.isCachedImage(avatar.FirstLifeImage) == false)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client.Images.RequestImageAsync(avatar.ProfileImage);
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
2006-12-19 23:13:04 +00:00
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
if (avatar.Textures != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (TextureEntryFace tef in avatar.Textures.FaceTextures.Values)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
if (_Client.Images.isCachedImage(tef.TextureID) == false)
|
2006-11-02 17:59:30 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client.Images.RequestImageAsync(tef.TextureID);
|
2006-11-02 17:59:30 +00:00
|
|
|
}
|
2006-12-19 23:13:04 +00:00
|
|
|
else
|
2006-11-02 17:59:30 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
Console.WriteLine("Already cached: " + tef.TextureID);
|
2006-11-02 17:59:30 +00:00
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Objects_OnNewPrim(Simulator simulator, PrimObject prim, ulong regionHandle, ushort timeDilation)
|
|
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
if ((prim.Textures.DefaultTexture != null) && (prim.Textures.DefaultTexture.TextureID != null))
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
if (_Client.Images.isCachedImage(prim.Textures.DefaultTexture.TextureID) == false)
|
|
|
|
|
{
|
|
|
|
|
_Client.Images.RequestImageAsync(prim.Textures.DefaultTexture.TextureID);
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
Console.WriteLine("Already cached: " + prim.Textures.DefaultTexture.TextureID);
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-12-19 23:13:04 +00:00
|
|
|
|
|
|
|
|
if (prim.Textures.FaceTextures != null)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
foreach (TextureEntryFace tef in prim.Textures.FaceTextures.Values)
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
if (_Client.Images.isCachedImage(tef.TextureID) == false)
|
2006-11-02 17:59:30 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client.Images.RequestImageAsync(tef.TextureID);
|
2006-11-02 17:59:30 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
Console.WriteLine("Already cached: " + tef.TextureID);
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-03 20:42:37 +00:00
|
|
|
private void NewImageRetrievedCallBack( LLUUID ImageID, byte[] data, bool wasCached, string statusMsg )
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
|
|
|
|
if (wasCached)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Cache ( " + data.Length + "): " + ImageID);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-11-03 01:02:08 +00:00
|
|
|
if (data == null)
|
|
|
|
|
{
|
2006-11-03 20:42:37 +00:00
|
|
|
Console.WriteLine("Image Data is null (" + statusMsg + "): " + ImageID);
|
2006-11-03 01:02:08 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Finished ( " + data.Length + "): " + ImageID);
|
2006-11-01 00:14:43 +00:00
|
|
|
|
2006-11-03 01:02:08 +00:00
|
|
|
String filename = Path.Combine(OutputDirectory, ImageID.ToStringHyphenated()) + ".tif";
|
2006-11-01 00:14:43 +00:00
|
|
|
|
2006-11-03 01:02:08 +00:00
|
|
|
TiffJob tj = new TiffJob(filename, data);
|
|
|
|
|
Thread t = new Thread(tj.RunMe);
|
|
|
|
|
t.Start();
|
|
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-19 23:13:04 +00:00
|
|
|
protected void doStuff()
|
2006-11-01 00:14:43 +00:00
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
|
|
|
|
|
_Client.Images.OnImageRetrieved += new ImageRetrievedCallback(NewImageRetrievedCallBack);
|
2006-11-01 00:14:43 +00:00
|
|
|
|
|
|
|
|
while (TextureQueue.Count > 0)
|
|
|
|
|
{
|
2006-12-19 23:13:04 +00:00
|
|
|
_Client.Images.RequestImageAsync(TextureQueue.Dequeue());
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Press any key to stop.");
|
|
|
|
|
Console.Read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected class TiffJob
|
|
|
|
|
{
|
|
|
|
|
string filename;
|
|
|
|
|
byte[] j2cdata;
|
|
|
|
|
|
|
|
|
|
public TiffJob(string path, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
filename = path;
|
|
|
|
|
j2cdata = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunMe()
|
|
|
|
|
{
|
2006-11-05 09:21:06 +00:00
|
|
|
File.WriteAllBytes(filename, JasperWrapper.jasper_decode_j2c_to_tiff(j2cdata));
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-12-19 23:13:04 +00:00
|
|
|
|
|
|
|
|
void Network_OnConnected(object sender)
|
|
|
|
|
{
|
|
|
|
|
ConnectedSignal.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool Connect(string FirstName, string LastName, string Password)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Attempting to connect and login to SecondLife.");
|
|
|
|
|
|
|
|
|
|
// Setup Login to Second Life
|
|
|
|
|
Dictionary<string, object> loginReply = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
// Login
|
|
|
|
|
if (!_Client.Network.Login(FirstName, LastName, Password, "createnotecard", "static.sprocket@gmail.com"))
|
|
|
|
|
{
|
|
|
|
|
// Login failed
|
|
|
|
|
Console.WriteLine("Error logging in: " + _Client.Network.LoginError);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Login was successful
|
|
|
|
|
Console.WriteLine("Login was successful.");
|
|
|
|
|
Console.WriteLine("AgentID: " + _Client.Network.AgentID);
|
|
|
|
|
Console.WriteLine("SessionID: " + _Client.Network.SessionID);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void Disconnect()
|
|
|
|
|
{
|
|
|
|
|
// Logout of Second Life
|
|
|
|
|
Console.WriteLine("Request logout");
|
|
|
|
|
_Client.Network.Logout();
|
|
|
|
|
}
|
2006-11-01 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
}
|