1
0

AssetInfoModule.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.OptionalModules.Asset
  41. {
  42. /// <summary>
  43. /// A module that just holds commands for inspecting assets.
  44. /// </summary>
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AssetInfoModule")]
  46. public class AssetInfoModule : ISharedRegionModule
  47. {
  48. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private Scene m_scene;
  50. public string Name { get { return "Asset Information Module"; } }
  51. public Type ReplaceableInterface { get { return null; } }
  52. public void Initialise(IConfigSource source)
  53. {
  54. // m_log.DebugFormat("[ASSET INFO MODULE]: INITIALIZED MODULE");
  55. }
  56. public void PostInitialise()
  57. {
  58. // m_log.DebugFormat("[ASSET INFO MODULE]: POST INITIALIZED MODULE");
  59. }
  60. public void Close()
  61. {
  62. // m_log.DebugFormat("[ASSET INFO MODULE]: CLOSED MODULE");
  63. }
  64. public void AddRegion(Scene scene)
  65. {
  66. // m_log.DebugFormat("[ASSET INFO MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  67. }
  68. public void RemoveRegion(Scene scene)
  69. {
  70. // m_log.DebugFormat("[ASSET INFO MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  71. }
  72. public void RegionLoaded(Scene scene)
  73. {
  74. // m_log.DebugFormat("[ASSET INFO MODULE]: REGION {0} LOADED", scene.RegionInfo.RegionName);
  75. if (m_scene == null)
  76. m_scene = scene;
  77. MainConsole.Instance.Commands.AddCommand(
  78. "Assets",
  79. false,
  80. "show asset",
  81. "show asset <ID>",
  82. "Show asset information",
  83. HandleShowAsset);
  84. MainConsole.Instance.Commands.AddCommand(
  85. "Assets", false, "dump asset",
  86. "dump asset <id>",
  87. "Dump an asset",
  88. HandleDumpAsset);
  89. }
  90. void HandleDumpAsset(string module, string[] args)
  91. {
  92. if (args.Length < 3)
  93. {
  94. MainConsole.Instance.Output("Usage is dump asset <ID>");
  95. return;
  96. }
  97. UUID assetId;
  98. string rawAssetId = args[2];
  99. if (!UUID.TryParse(rawAssetId, out assetId))
  100. {
  101. MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
  102. return;
  103. }
  104. AssetBase asset = m_scene.AssetService.Get(assetId.ToString());
  105. if (asset == null)
  106. {
  107. MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
  108. return;
  109. }
  110. string fileName = rawAssetId;
  111. if (!ConsoleUtil.CheckFileDoesNotExist(MainConsole.Instance, fileName))
  112. return;
  113. using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
  114. {
  115. using (BinaryWriter bw = new BinaryWriter(fs))
  116. {
  117. bw.Write(asset.Data);
  118. }
  119. }
  120. MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
  121. }
  122. void HandleShowAsset(string module, string[] args)
  123. {
  124. if (args.Length < 3)
  125. {
  126. MainConsole.Instance.Output("Syntax: show asset <ID>");
  127. return;
  128. }
  129. AssetBase asset = m_scene.AssetService.Get(args[2]);
  130. if (asset == null || asset.Data.Length == 0)
  131. {
  132. MainConsole.Instance.Output("Asset not found");
  133. return;
  134. }
  135. int i;
  136. MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
  137. MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
  138. MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
  139. MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
  140. MainConsole.Instance.OutputFormat("Size: {0} bytes", asset.Data.Length);
  141. MainConsole.Instance.OutputFormat("Temporary: {0}", asset.Temporary ? "yes" : "no");
  142. MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
  143. for (i = 0 ; i < 5 ; i++)
  144. {
  145. int off = i * 16;
  146. if (asset.Data.Length <= off)
  147. break;
  148. int len = 16;
  149. if (asset.Data.Length < off + len)
  150. len = asset.Data.Length - off;
  151. byte[] line = new byte[len];
  152. Array.Copy(asset.Data, off, line, 0, len);
  153. string text = BitConverter.ToString(line);
  154. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  155. }
  156. }
  157. }
  158. }