Annotation of /trunk/AssetClient/Program.cs
Parent Directory
|
Revision Log
Revision 12 - (view) (download)
| 1 : | jhurliman | 8 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.IO; | ||
| 4 : | using OpenMetaverse; | ||
| 5 : | jhurliman | 11 | using OpenMetaverse.StructuredData; |
| 6 : | jhurliman | 8 | |
| 7 : | namespace AssetClient | ||
| 8 : | { | ||
| 9 : | public class Program | ||
| 10 : | { | ||
| 11 : | static int verbosity; | ||
| 12 : | static NDesk.Options.OptionSet argParser; | ||
| 13 : | |||
| 14 : | static void Main(string[] args) | ||
| 15 : | { | ||
| 16 : | bool showhelp = false; | ||
| 17 : | string assetIDString = null; | ||
| 18 : | string inputFile = null; | ||
| 19 : | string operation = null; | ||
| 20 : | string outputFile = null; | ||
| 21 : | |||
| 22 : | argParser = new NDesk.Options.OptionSet() | ||
| 23 : | .Add("a|assetid=", "asset ID to fetch data or metadata for", delegate(string v) { assetIDString = v; }) | ||
| 24 : | .Add("h|?|help", delegate(string v) { showhelp = (v != null); }) | ||
| 25 : | .Add("i|input-file=", "asset data to upload with createasset", delegate(string v) { inputFile = v; }) | ||
| 26 : | .Add("o|operation=", "data|metadata|createasset (default is 'data')", delegate(string v) { operation = v; }) | ||
| 27 : | .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; }) | ||
| 28 : | .Add("v|verbose", delegate(string v) { if (v != null) ++verbosity; }); | ||
| 29 : | argParser.Parse(args); | ||
| 30 : | |||
| 31 : | if (!showhelp) | ||
| 32 : | { | ||
| 33 : | UUID assetID; | ||
| 34 : | |||
| 35 : | if (operation == null) | ||
| 36 : | operation = "data"; | ||
| 37 : | |||
| 38 : | switch (operation) | ||
| 39 : | { | ||
| 40 : | case "data": | ||
| 41 : | if (UUID.TryParse(assetIDString, out assetID)) | ||
| 42 : | FetchData(assetID, outputFile); | ||
| 43 : | else | ||
| 44 : | ShowHelp(); | ||
| 45 : | break; | ||
| 46 : | case "metadata": | ||
| 47 : | if (UUID.TryParse(assetIDString, out assetID)) | ||
| 48 : | FetchMetadata(assetID, outputFile); | ||
| 49 : | else | ||
| 50 : | ShowHelp(); | ||
| 51 : | break; | ||
| 52 : | case "createasset": | ||
| 53 : | Console.WriteLine("createasset operation is currently not supported"); | ||
| 54 : | break; | ||
| 55 : | default: | ||
| 56 : | Console.WriteLine("Unrecognized operation: " + operation); | ||
| 57 : | break; | ||
| 58 : | } | ||
| 59 : | } | ||
| 60 : | else | ||
| 61 : | { | ||
| 62 : | ShowHelp(); | ||
| 63 : | } | ||
| 64 : | } | ||
| 65 : | |||
| 66 : | static void FetchData(UUID assetID, string outputFile) | ||
| 67 : | { | ||
| 68 : | jhurliman | 11 | // Fetch the metadata first to locate this asset, then fetch the actual asset |
| 69 : | byte[] data; | ||
| 70 : | if (AssetClient.TryGetMetadata(assetID, out data)) | ||
| 71 : | { | ||
| 72 : | jhurliman | 12 | OSD metadata = OSDParser.DeserializeLLSDXml(data); |
| 73 : | OSDMap map = (OSDMap)metadata; | ||
| 74 : | jhurliman | 11 | |
| 75 : | string assetURL = map["asset_url"].AsString(); | ||
| 76 : | |||
| 77 : | FetchData(assetID, assetURL, outputFile); | ||
| 78 : | } | ||
| 79 : | } | ||
| 80 : | |||
| 81 : | static void FetchData(UUID assetID, string assetURL, string outputFile) | ||
| 82 : | { | ||
| 83 : | jhurliman | 8 | if (verbosity > 0) |
| 84 : | jhurliman | 11 | Console.WriteLine(String.Format("Starting data fetch for asset {0} from {1}", assetID, assetURL)); |
| 85 : | jhurliman | 8 | |
| 86 : | Asset asset; | ||
| 87 : | jhurliman | 11 | if (AssetClient.TryGetAsset(assetID, assetURL, out asset)) |
| 88 : | jhurliman | 8 | { |
| 89 : | if (verbosity > 0) | ||
| 90 : | { | ||
| 91 : | Console.WriteLine(String.Format("Downloaded a {0} byte asset of type {1}", | ||
| 92 : | asset.AssetData.Length, asset.AssetType)); | ||
| 93 : | } | ||
| 94 : | |||
| 95 : | try | ||
| 96 : | { | ||
| 97 : | Stream outStream; | ||
| 98 : | |||
| 99 : | if (outputFile == "default") | ||
| 100 : | outputFile = GetDefaultFilename(asset); | ||
| 101 : | |||
| 102 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 103 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 104 : | else | ||
| 105 : | outStream = Console.OpenStandardOutput(); | ||
| 106 : | |||
| 107 : | int pos = 0; | ||
| 108 : | while (pos < asset.AssetData.Length) | ||
| 109 : | { | ||
| 110 : | int len = (asset.AssetData.Length - pos < 1024) ? asset.AssetData.Length - pos : 1024; | ||
| 111 : | outStream.Write(asset.AssetData, pos, len); | ||
| 112 : | pos += len; | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | outStream.Close(); | ||
| 116 : | } | ||
| 117 : | catch (Exception ex) | ||
| 118 : | { | ||
| 119 : | Console.WriteLine("Error writing the asset: " + ex.Message); | ||
| 120 : | } | ||
| 121 : | } | ||
| 122 : | else | ||
| 123 : | { | ||
| 124 : | Console.WriteLine("Failed fetching asset " + assetID.ToString()); | ||
| 125 : | return; | ||
| 126 : | } | ||
| 127 : | } | ||
| 128 : | |||
| 129 : | static void FetchMetadata(UUID assetID, string outputFile) | ||
| 130 : | { | ||
| 131 : | if (verbosity > 0) | ||
| 132 : | Console.WriteLine("Starting metadata fetch for asset " + assetID.ToString()); | ||
| 133 : | |||
| 134 : | byte[] metadata; | ||
| 135 : | if (AssetClient.TryGetMetadata(assetID, out metadata)) | ||
| 136 : | { | ||
| 137 : | if (verbosity > 0) | ||
| 138 : | Console.WriteLine(String.Format("Downloaded {0} bytes of metadata", metadata.Length)); | ||
| 139 : | |||
| 140 : | try | ||
| 141 : | { | ||
| 142 : | Stream outStream; | ||
| 143 : | |||
| 144 : | if (outputFile == "default") | ||
| 145 : | outputFile = assetID.ToString() + ".xml"; | ||
| 146 : | |||
| 147 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 148 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 149 : | else | ||
| 150 : | outStream = Console.OpenStandardOutput(); | ||
| 151 : | |||
| 152 : | int pos = 0; | ||
| 153 : | while (pos < metadata.Length) | ||
| 154 : | { | ||
| 155 : | int len = (metadata.Length - pos < 1024) ? metadata.Length - pos : 1024; | ||
| 156 : | outStream.Write(metadata, pos, len); | ||
| 157 : | pos += len; | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | outStream.Close(); | ||
| 161 : | } | ||
| 162 : | catch (Exception ex) | ||
| 163 : | { | ||
| 164 : | Console.WriteLine("Error writing the metadata: " + ex.Message); | ||
| 165 : | } | ||
| 166 : | } | ||
| 167 : | else | ||
| 168 : | { | ||
| 169 : | Console.WriteLine("Failed fetching metadata for asset " + assetID.ToString()); | ||
| 170 : | return; | ||
| 171 : | } | ||
| 172 : | } | ||
| 173 : | |||
| 174 : | static void ShowHelp() | ||
| 175 : | { | ||
| 176 : | Console.WriteLine("Usage: assetclient [OPTION]..."); | ||
| 177 : | Console.WriteLine("A non-interactive client for the asset server"); | ||
| 178 : | Console.WriteLine("Options:"); | ||
| 179 : | argParser.WriteOptionDescriptions(Console.Out); | ||
| 180 : | } | ||
| 181 : | |||
| 182 : | static string GetDefaultFilename(Asset asset) | ||
| 183 : | { | ||
| 184 : | return String.Format("{0}.{1}", asset.AssetID, asset.AssetType.ToString().ToLower()); | ||
| 185 : | } | ||
| 186 : | } | ||
| 187 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

