View of /trunk/AssetClient/Program.cs
Parent Directory
|
Revision Log
Revision 17 -
(download)
(annotate)
Mon Nov 3 19:21:57 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 8634 byte(s)
Mon Nov 3 19:21:57 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 8634 byte(s)
* Adding sanity check when loading config file * Added an asset upload interface where the UUID is specified * Added asset upload support for ReferenceInterface and OpenSimInterface (untested) * Removing dead code * Cleanup
/*
* Copyright (c) 2008 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* -- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* -- Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
namespace AssetClient
{
public class Program
{
static int verbosity;
static NDesk.Options.OptionSet argParser;
static void Main(string[] args)
{
bool showhelp = false;
string assetIDString = null;
string inputFile = null;
string operation = null;
string outputFile = null;
argParser = new NDesk.Options.OptionSet()
.Add("a|assetid=", "asset ID to fetch data or metadata for", delegate(string v) { assetIDString = v; })
.Add("h|?|help", delegate(string v) { showhelp = (v != null); })
.Add("i|input-file=", "asset data to upload with createasset", delegate(string v) { inputFile = v; })
.Add("o|operation=", "data|metadata|createasset (default is 'data')", delegate(string v) { operation = v; })
.Add("O|output-file=", "file to write fetched data to. If 'default', the asset ID will be used as the filename", delegate(string v) { outputFile = v; })
.Add("v|verbose", delegate(string v) { if (v != null) ++verbosity; });
argParser.Parse(args);
if (!showhelp)
{
UUID assetID;
if (operation == null)
operation = "data";
switch (operation)
{
case "data":
if (UUID.TryParse(assetIDString, out assetID))
FetchData(assetID, outputFile);
else
ShowHelp();
break;
case "metadata":
if (UUID.TryParse(assetIDString, out assetID))
FetchMetadata(assetID, outputFile);
else
ShowHelp();
break;
case "createasset":
Console.WriteLine("createasset operation is currently not supported");
break;
default:
Console.WriteLine("Unrecognized operation: " + operation);
break;
}
}
else
{
ShowHelp();
}
}
static void FetchData(UUID assetID, string outputFile)
{
// Fetch the metadata first to locate this asset, then fetch the actual asset
byte[] data;
if (AssetClient.TryGetMetadata(assetID, out data))
{
OSD metadata = OSDParser.DeserializeLLSDXml(data);
OSDMap map = (OSDMap)metadata;
string assetURL = map["asset_url"].AsString();
FetchData(assetID, assetURL, outputFile);
}
}
static void FetchData(UUID assetID, string assetURL, string outputFile)
{
if (verbosity > 0)
Console.WriteLine(String.Format("Starting data fetch for asset {0} from {1}", assetID, assetURL));
Asset asset;
if (AssetClient.TryGetAsset(assetID, assetURL, out asset))
{
if (verbosity > 0)
{
Console.WriteLine(String.Format("Downloaded a {0} byte asset of type {1}",
asset.AssetData.Length, asset.AssetType));
}
try
{
Stream outStream;
if (outputFile == "default")
outputFile = GetDefaultFilename(asset);
if (!String.IsNullOrEmpty(outputFile))
outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
else
outStream = Console.OpenStandardOutput();
int pos = 0;
while (pos < asset.AssetData.Length)
{
int len = (asset.AssetData.Length - pos < 1024) ? asset.AssetData.Length - pos : 1024;
outStream.Write(asset.AssetData, pos, len);
pos += len;
}
outStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error writing the asset: " + ex.Message);
}
}
else
{
Console.WriteLine("Failed fetching asset " + assetID.ToString());
return;
}
}
static void FetchMetadata(UUID assetID, string outputFile)
{
if (verbosity > 0)
Console.WriteLine("Starting metadata fetch for asset " + assetID.ToString());
byte[] metadata;
if (AssetClient.TryGetMetadata(assetID, out metadata))
{
if (verbosity > 0)
Console.WriteLine(String.Format("Downloaded {0} bytes of metadata", metadata.Length));
try
{
Stream outStream;
if (outputFile == "default")
outputFile = assetID.ToString() + ".xml";
if (!String.IsNullOrEmpty(outputFile))
outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
else
outStream = Console.OpenStandardOutput();
int pos = 0;
while (pos < metadata.Length)
{
int len = (metadata.Length - pos < 1024) ? metadata.Length - pos : 1024;
outStream.Write(metadata, pos, len);
pos += len;
}
outStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error writing the metadata: " + ex.Message);
}
}
else
{
Console.WriteLine("Failed fetching metadata for asset " + assetID.ToString());
return;
}
}
static void ShowHelp()
{
Console.WriteLine("Usage: assetclient [OPTION]...");
Console.WriteLine("A non-interactive client for the asset server");
Console.WriteLine("Options:");
argParser.WriteOptionDescriptions(Console.Out);
}
static string GetDefaultFilename(Asset asset)
{
return String.Format("{0}.{1}", asset.AssetID, asset.AssetType.ToString().ToLower());
}
}
}
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

