public static void SaveAssets(AssetManager assetManager, AssetType assetType, IList<UUID> assets, string assetsPath)
{
int count = 0;
List<UUID> remainingTextures = new List<UUID>(assets);
AutoResetEvent AllPropertiesReceived = new AutoResetEvent(false);
for (int i = 0; i < assets.Count; i++)
{
UUID texture = assets[i];
if(assetType == AssetType.Texture)
{
assetManager.RequestImage(texture, (state, assetTexture) =>
{
string extension = string.Empty;
if (assetTexture == null)
{
Console.WriteLine("Missing asset " + texture);
return;
}
if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(assetType))
extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[assetType];
File.WriteAllBytes(Path.Combine(assetsPath, texture.ToString() + extension), assetTexture.AssetData);
remainingTextures.Remove(assetTexture.AssetID);
if (remainingTextures.Count == 0)
AllPropertiesReceived.Set();
++count;
});
}
else
{
assetManager.RequestAsset(texture, assetType, false, (transfer, asset) =>
{
string extension = string.Empty;
if (asset == null)
{
Console.WriteLine("Missing asset " + texture);
return;
}
if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(assetType))
extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[assetType];
File.WriteAllBytes(Path.Combine(assetsPath, texture.ToString() + extension), asset.AssetData);
remainingTextures.Remove(asset.AssetID);
if (remainingTextures.Count == 0)
AllPropertiesReceived.Set();
++count;
});
}
Thread.Sleep(200);
if (i % 5 == 0)
Thread.Sleep(250);
}
AllPropertiesReceived.WaitOne(5000 + 350 * assets.Count);
Logger.Log("Copied " + count + " textures to the asset archive folder", Helpers.LogLevel.Info);
}