ArchiverModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 log4net;
  32. using NDesk.Options;
  33. using Nini.Config;
  34. using Mono.Addins;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenMetaverse;
  40. namespace OpenSim.Region.CoreModules.World.Archiver
  41. {
  42. /// <summary>
  43. /// This module loads and saves OpenSimulator region archives
  44. /// </summary>
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ArchiverModule")]
  46. public class ArchiverModule : INonSharedRegionModule, IRegionArchiverModule
  47. {
  48. private static readonly ILog m_log =
  49. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. public Scene Scene { get; private set; }
  51. public IRegionCombinerModule RegionCombinerModule { get; private set; }
  52. /// <value>
  53. /// The file used to load and save an opensimulator archive if no filename has been specified
  54. /// </value>
  55. protected const string DEFAULT_OAR_BACKUP_FILENAME = "region.oar";
  56. public string Name
  57. {
  58. get { return "RegionArchiverModule"; }
  59. }
  60. public Type ReplaceableInterface
  61. {
  62. get { return null; }
  63. }
  64. public void Initialise(IConfigSource source)
  65. {
  66. //m_log.Debug("[ARCHIVER] Initialising");
  67. }
  68. public void AddRegion(Scene scene)
  69. {
  70. Scene = scene;
  71. Scene.RegisterModuleInterface<IRegionArchiverModule>(this);
  72. //m_log.DebugFormat("[ARCHIVER]: Enabled for region {0}", scene.RegionInfo.RegionName);
  73. }
  74. public void RegionLoaded(Scene scene)
  75. {
  76. RegionCombinerModule = scene.RequestModuleInterface<IRegionCombinerModule>();
  77. }
  78. public void RemoveRegion(Scene scene)
  79. {
  80. }
  81. public void Close()
  82. {
  83. }
  84. /// <summary>
  85. /// Load a whole region from an opensimulator archive.
  86. /// </summary>
  87. /// <param name="cmdparams"></param>
  88. public void HandleLoadOarConsoleCommand(string module, string[] cmdparams)
  89. {
  90. bool mergeOar = false;
  91. bool skipAssets = false;
  92. bool forceTerrain = false;
  93. bool forceParcels = false;
  94. bool noObjects = false;
  95. Vector3 displacement = new Vector3(0f, 0f, 0f);
  96. String defaultUser = "";
  97. float rotation = 0f;
  98. Vector3 rotationCenter = new Vector3(Constants.RegionSize / 2f, Constants.RegionSize / 2f, 0);
  99. OptionSet options = new OptionSet();
  100. options.Add("m|merge", delegate(string v) { mergeOar = (v != null); });
  101. options.Add("s|skip-assets", delegate(string v) { skipAssets = (v != null); });
  102. options.Add("force-terrain", delegate(string v) { forceTerrain = (v != null); });
  103. options.Add("forceterrain", delegate(string v) { forceTerrain = (v != null); }); // downward compatibility
  104. options.Add("force-parcels", delegate(string v) { forceParcels = (v != null); });
  105. options.Add("forceparcels", delegate(string v) { forceParcels = (v != null); }); // downward compatibility
  106. options.Add("no-objects", delegate(string v) { noObjects = (v != null); });
  107. options.Add("default-user=", delegate(string v) { defaultUser = (v == null) ? "" : v; });
  108. options.Add("displacement=", delegate(string v)
  109. {
  110. try
  111. {
  112. displacement = v == null ? Vector3.Zero : Vector3.Parse(v);
  113. }
  114. catch
  115. {
  116. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing displacement");
  117. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as vector3: --displacement \"<128,128,0>\"");
  118. return;
  119. }
  120. });
  121. options.Add("rotation=", delegate(string v)
  122. {
  123. try
  124. {
  125. rotation = v == null ? 0f : float.Parse(v);
  126. }
  127. catch
  128. {
  129. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing rotation");
  130. m_log.ErrorFormat("[ARCHIVER MODULE] Must be an angle in degrees between -360 and +360: --rotation 45");
  131. return;
  132. }
  133. // Convert to radians for internals
  134. rotation = Util.Clamp<float>(rotation, -359f, 359f) / 180f * (float)Math.PI;
  135. });
  136. options.Add("rotation-center=", delegate(string v)
  137. {
  138. try
  139. {
  140. rotationCenter = v == null ? Vector3.Zero : Vector3.Parse(v);
  141. }
  142. catch
  143. {
  144. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing rotation displacement");
  145. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as vector3: --rotation-center \"<128,128,0>\"");
  146. return;
  147. }
  148. });
  149. // Send a message to the region ready module
  150. /* bluewall* Disable this for the time being
  151. IRegionReadyModule rready = m_scene.RequestModuleInterface<IRegionReadyModule>();
  152. if (rready != null)
  153. {
  154. rready.OarLoadingAlert("load");
  155. }
  156. */
  157. List<string> mainParams = options.Parse(cmdparams);
  158. // m_log.DebugFormat("MERGE OAR IS [{0}]", mergeOar);
  159. //
  160. // foreach (string param in mainParams)
  161. // m_log.DebugFormat("GOT PARAM [{0}]", param);
  162. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  163. if (mergeOar) archiveOptions.Add("merge", null);
  164. if (skipAssets) archiveOptions.Add("skipAssets", null);
  165. if (forceTerrain) archiveOptions.Add("force-terrain", null);
  166. if (forceParcels) archiveOptions.Add("force-parcels", null);
  167. if (noObjects) archiveOptions.Add("no-objects", null);
  168. if (defaultUser != "")
  169. {
  170. UUID defaultUserUUID = UUID.Zero;
  171. try
  172. {
  173. defaultUserUUID = Scene.UserManagementModule.GetUserIdByName(defaultUser);
  174. }
  175. catch
  176. {
  177. m_log.ErrorFormat("[ARCHIVER MODULE] default user must be in format \"First Last\"", defaultUser);
  178. }
  179. if (defaultUserUUID == UUID.Zero)
  180. {
  181. m_log.ErrorFormat("[ARCHIVER MODULE] cannot find specified default user {0}", defaultUser);
  182. return;
  183. }
  184. else
  185. {
  186. archiveOptions.Add("default-user", defaultUserUUID);
  187. }
  188. }
  189. archiveOptions.Add("displacement", displacement);
  190. archiveOptions.Add("rotation", rotation);
  191. archiveOptions.Add("rotation-center", rotationCenter);
  192. if (mainParams.Count > 2)
  193. {
  194. DearchiveRegion(mainParams[2], Guid.Empty, archiveOptions);
  195. }
  196. else
  197. {
  198. DearchiveRegion(DEFAULT_OAR_BACKUP_FILENAME, Guid.Empty, archiveOptions);
  199. }
  200. }
  201. /// <summary>
  202. /// Save a region to a file, including all the assets needed to restore it.
  203. /// </summary>
  204. /// <param name="cmdparams"></param>
  205. public void HandleSaveOarConsoleCommand(string module, string[] cmdparams)
  206. {
  207. Dictionary<string, object> options = new Dictionary<string, object>();
  208. OptionSet ops = new OptionSet();
  209. // legacy argument [obsolete]
  210. ops.Add("p|profile=", delegate(string v) { Console.WriteLine("\n WARNING: -profile option is obsolete and it will not work. Use -home instead.\n"); });
  211. // preferred
  212. ops.Add("h|home=", delegate(string v) { options["home"] = v; });
  213. ops.Add("noassets", delegate(string v) { options["noassets"] = v != null; });
  214. ops.Add("publish", v => options["wipe-owners"] = v != null);
  215. ops.Add("perm=", delegate(string v) { options["checkPermissions"] = v; });
  216. ops.Add("all", delegate(string v) { options["all"] = v != null; });
  217. List<string> mainParams = ops.Parse(cmdparams);
  218. string path;
  219. if (mainParams.Count > 2)
  220. path = mainParams[2];
  221. else
  222. path = DEFAULT_OAR_BACKUP_FILENAME;
  223. // Not doing this right now as this causes some problems with auto-backup systems. Maybe a force flag is
  224. // needed
  225. // if (!ConsoleUtil.CheckFileDoesNotExist(MainConsole.Instance, path))
  226. // return;
  227. ArchiveRegion(path, options);
  228. }
  229. public void ArchiveRegion(string savePath, Dictionary<string, object> options)
  230. {
  231. ArchiveRegion(savePath, Guid.Empty, options);
  232. }
  233. public void ArchiveRegion(string savePath, Guid requestId, Dictionary<string, object> options)
  234. {
  235. m_log.InfoFormat(
  236. "[ARCHIVER]: Writing archive for region {0} to {1}", Scene.RegionInfo.RegionName, savePath);
  237. new ArchiveWriteRequest(Scene, savePath, requestId).ArchiveRegion(options);
  238. }
  239. public void ArchiveRegion(Stream saveStream)
  240. {
  241. ArchiveRegion(saveStream, Guid.Empty);
  242. }
  243. public void ArchiveRegion(Stream saveStream, Guid requestId)
  244. {
  245. ArchiveRegion(saveStream, requestId, new Dictionary<string, object>());
  246. }
  247. public void ArchiveRegion(Stream saveStream, Guid requestId, Dictionary<string, object> options)
  248. {
  249. new ArchiveWriteRequest(Scene, saveStream, requestId).ArchiveRegion(options);
  250. }
  251. public void DearchiveRegion(string loadPath)
  252. {
  253. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  254. DearchiveRegion(loadPath, Guid.Empty, archiveOptions);
  255. }
  256. public void DearchiveRegion(string loadPath, Guid requestId, Dictionary<string, object> options)
  257. {
  258. m_log.InfoFormat(
  259. "[ARCHIVER]: Loading archive to region {0} from {1}", Scene.RegionInfo.RegionName, loadPath);
  260. new ArchiveReadRequest(Scene, loadPath, requestId, options).DearchiveRegion();
  261. }
  262. public void DearchiveRegion(Stream loadStream)
  263. {
  264. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  265. DearchiveRegion(loadStream, Guid.Empty, archiveOptions);
  266. }
  267. public void DearchiveRegion(Stream loadStream, Guid requestId, Dictionary<string, object> options)
  268. {
  269. new ArchiveReadRequest(Scene, loadStream, requestId, options).DearchiveRegion();
  270. }
  271. }
  272. }