Annotation of /trunk/AssetClient/Program.cs
Parent Directory
|
Revision Log
Revision 21 - (view) (download)
| 1 : | jhurliman | 17 | /* |
| 2 : | * Copyright (c) 2008 Intel Corporation | ||
| 3 : | * All rights reserved. | ||
| 4 : | * Redistribution and use in source and binary forms, with or without | ||
| 5 : | * modification, are permitted provided that the following conditions | ||
| 6 : | * are met: | ||
| 7 : | * | ||
| 8 : | * -- Redistributions of source code must retain the above copyright | ||
| 9 : | * notice, this list of conditions and the following disclaimer. | ||
| 10 : | * -- Redistributions in binary form must reproduce the above copyright | ||
| 11 : | * notice, this list of conditions and the following disclaimer in the | ||
| 12 : | * documentation and/or other materials provided with the distribution. | ||
| 13 : | * -- Neither the name of the Intel Corporation nor the names of its | ||
| 14 : | * contributors may be used to endorse or promote products derived from | ||
| 15 : | * this software without specific prior written permission. | ||
| 16 : | * | ||
| 17 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 18 : | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 19 : | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
| 20 : | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
| 21 : | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 22 : | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 23 : | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 24 : | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 25 : | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 26 : | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 : | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 : | */ | ||
| 29 : | |||
| 30 : | using System; | ||
| 31 : | jhurliman | 8 | using System.Collections.Generic; |
| 32 : | using System.IO; | ||
| 33 : | using OpenMetaverse; | ||
| 34 : | jhurliman | 11 | using OpenMetaverse.StructuredData; |
| 35 : | jhurliman | 8 | |
| 36 : | namespace AssetClient | ||
| 37 : | { | ||
| 38 : | public class Program | ||
| 39 : | { | ||
| 40 : | static int verbosity; | ||
| 41 : | static NDesk.Options.OptionSet argParser; | ||
| 42 : | |||
| 43 : | static void Main(string[] args) | ||
| 44 : | { | ||
| 45 : | bool showhelp = false; | ||
| 46 : | string assetIDString = null; | ||
| 47 : | string inputFile = null; | ||
| 48 : | string operation = null; | ||
| 49 : | string outputFile = null; | ||
| 50 : | |||
| 51 : | argParser = new NDesk.Options.OptionSet() | ||
| 52 : | .Add("a|assetid=", "asset ID to fetch data or metadata for", delegate(string v) { assetIDString = v; }) | ||
| 53 : | .Add("h|?|help", delegate(string v) { showhelp = (v != null); }) | ||
| 54 : | .Add("i|input-file=", "asset data to upload with createasset", delegate(string v) { inputFile = v; }) | ||
| 55 : | .Add("o|operation=", "data|metadata|createasset (default is 'data')", delegate(string v) { operation = v; }) | ||
| 56 : | .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; }) | ||
| 57 : | .Add("v|verbose", delegate(string v) { if (v != null) ++verbosity; }); | ||
| 58 : | argParser.Parse(args); | ||
| 59 : | |||
| 60 : | if (!showhelp) | ||
| 61 : | { | ||
| 62 : | UUID assetID; | ||
| 63 : | |||
| 64 : | if (operation == null) | ||
| 65 : | operation = "data"; | ||
| 66 : | |||
| 67 : | switch (operation) | ||
| 68 : | { | ||
| 69 : | case "data": | ||
| 70 : | if (UUID.TryParse(assetIDString, out assetID)) | ||
| 71 : | FetchData(assetID, outputFile); | ||
| 72 : | else | ||
| 73 : | ShowHelp(); | ||
| 74 : | break; | ||
| 75 : | case "metadata": | ||
| 76 : | if (UUID.TryParse(assetIDString, out assetID)) | ||
| 77 : | FetchMetadata(assetID, outputFile); | ||
| 78 : | else | ||
| 79 : | ShowHelp(); | ||
| 80 : | break; | ||
| 81 : | case "createasset": | ||
| 82 : | jhurliman | 20 | if (assetIDString != null && UUID.TryParse(assetIDString, out assetID)) |
| 83 : | CreateAsset(assetID, inputFile); | ||
| 84 : | else | ||
| 85 : | CreateAsset(UUID.Zero, inputFile); | ||
| 86 : | jhurliman | 8 | break; |
| 87 : | default: | ||
| 88 : | Console.WriteLine("Unrecognized operation: " + operation); | ||
| 89 : | break; | ||
| 90 : | } | ||
| 91 : | } | ||
| 92 : | else | ||
| 93 : | { | ||
| 94 : | ShowHelp(); | ||
| 95 : | } | ||
| 96 : | } | ||
| 97 : | |||
| 98 : | static void FetchData(UUID assetID, string outputFile) | ||
| 99 : | { | ||
| 100 : | jhurliman | 11 | // Fetch the metadata first to locate this asset, then fetch the actual asset |
| 101 : | byte[] data; | ||
| 102 : | if (AssetClient.TryGetMetadata(assetID, out data)) | ||
| 103 : | { | ||
| 104 : | jhurliman | 12 | OSD metadata = OSDParser.DeserializeLLSDXml(data); |
| 105 : | OSDMap map = (OSDMap)metadata; | ||
| 106 : | jhurliman | 11 | string assetURL = map["asset_url"].AsString(); |
| 107 : | |||
| 108 : | jhurliman | 20 | if (verbosity > 0) |
| 109 : | Console.WriteLine("Metadata retrieved, fetching asset data from " + assetURL); | ||
| 110 : | |||
| 111 : | jhurliman | 11 | FetchData(assetID, assetURL, outputFile); |
| 112 : | } | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | static void FetchData(UUID assetID, string assetURL, string outputFile) | ||
| 116 : | { | ||
| 117 : | jhurliman | 8 | if (verbosity > 0) |
| 118 : | jhurliman | 11 | Console.WriteLine(String.Format("Starting data fetch for asset {0} from {1}", assetID, assetURL)); |
| 119 : | jhurliman | 8 | |
| 120 : | jhurliman | 20 | byte[] assetData; |
| 121 : | if (AssetClient.TryGetAsset(assetID, assetURL, out assetData)) | ||
| 122 : | jhurliman | 8 | { |
| 123 : | if (verbosity > 0) | ||
| 124 : | { | ||
| 125 : | jhurliman | 20 | Console.WriteLine(String.Format("Downloaded a {0} byte asset", assetData.Length)); |
| 126 : | jhurliman | 8 | } |
| 127 : | |||
| 128 : | try | ||
| 129 : | { | ||
| 130 : | Stream outStream; | ||
| 131 : | |||
| 132 : | if (outputFile == "default") | ||
| 133 : | jhurliman | 20 | outputFile = assetID.ToString(); |
| 134 : | jhurliman | 8 | |
| 135 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 136 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 137 : | else | ||
| 138 : | outStream = Console.OpenStandardOutput(); | ||
| 139 : | |||
| 140 : | int pos = 0; | ||
| 141 : | jhurliman | 20 | while (pos < assetData.Length) |
| 142 : | jhurliman | 8 | { |
| 143 : | jhurliman | 20 | int len = (assetData.Length - pos < 1024) ? assetData.Length - pos : 1024; |
| 144 : | outStream.Write(assetData, pos, len); | ||
| 145 : | jhurliman | 8 | pos += len; |
| 146 : | } | ||
| 147 : | |||
| 148 : | outStream.Close(); | ||
| 149 : | } | ||
| 150 : | catch (Exception ex) | ||
| 151 : | { | ||
| 152 : | Console.WriteLine("Error writing the asset: " + ex.Message); | ||
| 153 : | } | ||
| 154 : | } | ||
| 155 : | else | ||
| 156 : | { | ||
| 157 : | Console.WriteLine("Failed fetching asset " + assetID.ToString()); | ||
| 158 : | return; | ||
| 159 : | } | ||
| 160 : | } | ||
| 161 : | |||
| 162 : | static void FetchMetadata(UUID assetID, string outputFile) | ||
| 163 : | { | ||
| 164 : | if (verbosity > 0) | ||
| 165 : | Console.WriteLine("Starting metadata fetch for asset " + assetID.ToString()); | ||
| 166 : | |||
| 167 : | byte[] metadata; | ||
| 168 : | if (AssetClient.TryGetMetadata(assetID, out metadata)) | ||
| 169 : | { | ||
| 170 : | if (verbosity > 0) | ||
| 171 : | Console.WriteLine(String.Format("Downloaded {0} bytes of metadata", metadata.Length)); | ||
| 172 : | |||
| 173 : | try | ||
| 174 : | { | ||
| 175 : | Stream outStream; | ||
| 176 : | |||
| 177 : | if (outputFile == "default") | ||
| 178 : | outputFile = assetID.ToString() + ".xml"; | ||
| 179 : | |||
| 180 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 181 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 182 : | else | ||
| 183 : | outStream = Console.OpenStandardOutput(); | ||
| 184 : | |||
| 185 : | int pos = 0; | ||
| 186 : | while (pos < metadata.Length) | ||
| 187 : | { | ||
| 188 : | int len = (metadata.Length - pos < 1024) ? metadata.Length - pos : 1024; | ||
| 189 : | outStream.Write(metadata, pos, len); | ||
| 190 : | pos += len; | ||
| 191 : | } | ||
| 192 : | |||
| 193 : | outStream.Close(); | ||
| 194 : | } | ||
| 195 : | catch (Exception ex) | ||
| 196 : | { | ||
| 197 : | Console.WriteLine("Error writing the metadata: " + ex.Message); | ||
| 198 : | } | ||
| 199 : | } | ||
| 200 : | else | ||
| 201 : | { | ||
| 202 : | Console.WriteLine("Failed fetching metadata for asset " + assetID.ToString()); | ||
| 203 : | return; | ||
| 204 : | } | ||
| 205 : | } | ||
| 206 : | |||
| 207 : | jhurliman | 20 | static void CreateAsset(UUID assetID, string inputFile) |
| 208 : | { | ||
| 209 : | if (verbosity > 0) | ||
| 210 : | Console.WriteLine(String.Format("Starting asset creation for asset {0} from file {1}", | ||
| 211 : | assetID, inputFile)); | ||
| 212 : | |||
| 213 : | byte[] assetData; | ||
| 214 : | try { assetData = File.ReadAllBytes(inputFile); } | ||
| 215 : | catch (Exception ex) | ||
| 216 : | { | ||
| 217 : | Console.WriteLine(ex.Message); | ||
| 218 : | return; | ||
| 219 : | } | ||
| 220 : | |||
| 221 : | string name = Path.GetFileNameWithoutExtension(inputFile); | ||
| 222 : | string extension = Path.GetExtension(inputFile).TrimStart('.').ToLower(); | ||
| 223 : | |||
| 224 : | #region AssetType Parsing | ||
| 225 : | AssetType type; | ||
| 226 : | if (!OpenMetaverse.Utils.EnumTryParse<AssetType>(extension, out type)) | ||
| 227 : | { | ||
| 228 : | switch (extension) | ||
| 229 : | { | ||
| 230 : | case "jp2": | ||
| 231 : | case "j2c": | ||
| 232 : | case "j2k": | ||
| 233 : | type = AssetType.Texture; | ||
| 234 : | break; | ||
| 235 : | case "ogg": | ||
| 236 : | type = AssetType.Sound; | ||
| 237 : | break; | ||
| 238 : | case "lsl": | ||
| 239 : | type = AssetType.LSLText; | ||
| 240 : | break; | ||
| 241 : | case "lso": | ||
| 242 : | type = AssetType.LSLBytecode; | ||
| 243 : | break; | ||
| 244 : | case "jpg": | ||
| 245 : | case "jpeg": | ||
| 246 : | type = AssetType.ImageJPEG; | ||
| 247 : | break; | ||
| 248 : | case "tga": | ||
| 249 : | type = AssetType.ImageTGA; | ||
| 250 : | break; | ||
| 251 : | case "wav": | ||
| 252 : | type = AssetType.SoundWAV; | ||
| 253 : | break; | ||
| 254 : | default: | ||
| 255 : | Console.WriteLine("Unrecognized file extension " + extension); | ||
| 256 : | return; | ||
| 257 : | } | ||
| 258 : | } | ||
| 259 : | #endregion AssetType Parsing | ||
| 260 : | |||
| 261 : | if (assetID != UUID.Zero) | ||
| 262 : | assetID = AssetClient.CreateAsset(name, "Created with AssetClient", type, false, assetData, assetID); | ||
| 263 : | else | ||
| 264 : | assetID = AssetClient.CreateAsset(name, "Created with AssetClient", type, false, assetData); | ||
| 265 : | |||
| 266 : | if (assetID != UUID.Zero) | ||
| 267 : | Console.WriteLine("Created asset " + assetID.ToString()); | ||
| 268 : | else | ||
| 269 : | Console.WriteLine("Failed to create asset"); | ||
| 270 : | } | ||
| 271 : | |||
| 272 : | jhurliman | 8 | static void ShowHelp() |
| 273 : | { | ||
| 274 : | Console.WriteLine("Usage: assetclient [OPTION]..."); | ||
| 275 : | Console.WriteLine("A non-interactive client for the asset server"); | ||
| 276 : | Console.WriteLine("Options:"); | ||
| 277 : | argParser.WriteOptionDescriptions(Console.Out); | ||
| 278 : | } | ||
| 279 : | |||
| 280 : | static string GetDefaultFilename(Asset asset) | ||
| 281 : | { | ||
| 282 : | return String.Format("{0}.{1}", asset.AssetID, asset.AssetType.ToString().ToLower()); | ||
| 283 : | } | ||
| 284 : | } | ||
| 285 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

