View of /trunk/AssetClient/Program.cs
Parent Directory
|
Revision Log
Revision 25 -
(download)
(annotate)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 12184 byte(s)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 12184 byte(s)
* Added MemcacheStorage, sits on top of another storage provider and uses memcached to cache metadata and asset data * Implemented Robert's suggestion to replace AssetURL with a list of method to URI mappings that contains data=>uri * Upgraded to latest ExtensionLoader with simplified extension starting * Simplified SimpleStorage by removing SimpleMetadata and using a dictionary of UUID to filenames * Moved Metadata into its own file, added serialization and deserialization methods * Added LICENSES.txt file for third party libraries (not complete yet)
/*
* 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":
if (assetIDString != null && UUID.TryParse(assetIDString, out assetID))
CreateAsset(assetID, inputFile);
else
CreateAsset(UUID.Zero, inputFile);
break;
default:
Console.WriteLine("Unrecognized operation: " + operation);
break;
}
}
else
{
ShowHelp();
}
}
static void FetchData(UUID assetID, string outputFile)
{
if (verbosity > 0)
Console.WriteLine(String.Format("Starting metadata fetch for asset {0}", assetID));
// 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;
OSDMap methods = map["methods"] as OSDMap;
Uri assetLocation = methods["data"].AsUri();
if (assetLocation != null)
{
if (verbosity > 0)
Console.WriteLine("Metadata retrieved, fetching asset data from " + assetLocation);
FetchData(assetID, assetLocation.ToString(), outputFile);
}
else
{
Console.WriteLine("Metadata did not contain a method for retrieving asset data:");
Console.WriteLine(System.Text.Encoding.UTF8.GetString(data));
}
}
}
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));
byte[] assetData;
if (AssetClient.TryGetAsset(assetID, assetURL, out assetData))
{
if (verbosity > 0)
{
Console.WriteLine(String.Format("Downloaded a {0} byte asset", assetData.Length));
}
try
{
Stream outStream;
if (outputFile == "default")
outputFile = assetID.ToString();
if (!String.IsNullOrEmpty(outputFile))
outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
else
outStream = Console.OpenStandardOutput();
int pos = 0;
while (pos < assetData.Length)
{
int len = (assetData.Length - pos < 1024) ? assetData.Length - pos : 1024;
outStream.Write(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 CreateAsset(UUID assetID, string inputFile)
{
if (verbosity > 0)
{
if (assetID != UUID.Zero)
{
Console.WriteLine(String.Format("Starting asset creation for asset {0} from file {1}",
assetID, inputFile));
}
else
{
Console.WriteLine("Starting asset creation from file " + inputFile);
}
}
byte[] assetData;
try { assetData = File.ReadAllBytes(inputFile); }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
string name = Path.GetFileNameWithoutExtension(inputFile);
string extension = Path.GetExtension(inputFile).TrimStart('.').ToLower();
#region AssetType Parsing
AssetType type;
if (!OpenMetaverse.Utils.EnumTryParse<AssetType>(extension, out type))
{
switch (extension)
{
case "jp2":
case "j2c":
case "j2k":
type = AssetType.Texture;
break;
case "ogg":
type = AssetType.Sound;
break;
case "lsl":
type = AssetType.LSLText;
break;
case "lso":
type = AssetType.LSLBytecode;
break;
case "jpg":
case "jpeg":
type = AssetType.ImageJPEG;
break;
case "tga":
type = AssetType.ImageTGA;
break;
case "wav":
type = AssetType.SoundWAV;
break;
default:
Console.WriteLine("Unrecognized file extension " + extension);
return;
}
}
#endregion AssetType Parsing
if (assetID != UUID.Zero)
assetID = AssetClient.CreateAsset(name, "Created with AssetClient", type, false, assetData, assetID);
else
assetID = AssetClient.CreateAsset(name, "Created with AssetClient", type, false, assetData);
if (assetID != UUID.Zero)
Console.WriteLine("Created asset " + assetID.ToString());
else
Console.WriteLine("Failed to create asset");
}
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 |

