ArchiverModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. /// <value>
  52. /// The file used to load and save an opensimulator archive if no filename has been specified
  53. /// </value>
  54. protected const string DEFAULT_OAR_BACKUP_FILENAME = "region.oar";
  55. public string Name
  56. {
  57. get { return "RegionArchiverModule"; }
  58. }
  59. public Type ReplaceableInterface
  60. {
  61. get { return null; }
  62. }
  63. public void Initialise(IConfigSource source)
  64. {
  65. //m_log.Debug("[ARCHIVER] Initialising");
  66. }
  67. public void AddRegion(Scene scene)
  68. {
  69. Scene = scene;
  70. Scene.RegisterModuleInterface<IRegionArchiverModule>(this);
  71. //m_log.DebugFormat("[ARCHIVER]: Enabled for region {0}", scene.RegionInfo.RegionName);
  72. }
  73. public void RegionLoaded(Scene scene)
  74. {
  75. }
  76. public void RemoveRegion(Scene scene)
  77. {
  78. }
  79. public void Close()
  80. {
  81. }
  82. /// <summary>
  83. /// Load a whole region from an opensimulator archive.
  84. /// </summary>
  85. /// <param name="cmdparams"></param>
  86. public void HandleLoadOarConsoleCommand(string module, string[] cmdparams)
  87. {
  88. bool mergeOar = false;
  89. bool mergeReplaceObjects = false;
  90. bool skipAssets = false;
  91. bool mergeTerrain = false;
  92. bool mergeParcels = false;
  93. bool noObjects = false;
  94. Vector3 displacement = new Vector3(0f, 0f, 0f);
  95. String defaultUser = "";
  96. float rotation = 0f;
  97. Vector3 rotationCenter = new Vector3(Scene.RegionInfo.RegionSizeX / 2f, Scene.RegionInfo.RegionSizeY / 2f, 0);
  98. Vector3 boundingOrigin = new Vector3(0f, 0f, 0f);
  99. Vector3 boundingSize = new Vector3(Scene.RegionInfo.RegionSizeX, Scene.RegionInfo.RegionSizeY, float.MaxValue);
  100. bool debug = false;
  101. OptionSet options = new OptionSet();
  102. options.Add("m|merge", delegate(string v) { mergeOar = (v != null); });
  103. options.Add("mergeReplaceObjects", delegate (string v) { mergeReplaceObjects = (v != null); });
  104. options.Add("s|skip-assets", delegate(string v) { skipAssets = (v != null); });
  105. options.Add("merge-terrain", delegate(string v) { mergeTerrain = (v != null); });
  106. options.Add("force-terrain", delegate (string v) { mergeTerrain = (v != null); }); // downward compatibility
  107. options.Add("forceterrain", delegate (string v) { mergeTerrain = (v != null); }); // downward compatibility
  108. options.Add("merge-parcels", delegate(string v) { mergeParcels = (v != null); });
  109. options.Add("force-parcels", delegate (string v) { mergeParcels = (v != null); }); // downward compatibility
  110. options.Add("forceparcels", delegate (string v) { mergeParcels = (v != null); }); // downward compatibility
  111. options.Add("no-objects", delegate(string v) { noObjects = (v != null); });
  112. options.Add("default-user=", delegate(string v) { defaultUser = (v == null) ? "" : v; });
  113. options.Add("displacement=", delegate(string v)
  114. {
  115. try
  116. {
  117. displacement = v == null ? Vector3.Zero : Vector3.Parse(v);
  118. }
  119. catch
  120. {
  121. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing displacement");
  122. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as vector3: --displacement \"<128,128,0>\"");
  123. return;
  124. }
  125. });
  126. options.Add("rotation=", delegate(string v)
  127. {
  128. try
  129. {
  130. rotation = v == null ? 0f : float.Parse(v);
  131. }
  132. catch
  133. {
  134. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing rotation");
  135. m_log.ErrorFormat("[ARCHIVER MODULE] Must be an angle in degrees between -360 and +360: --rotation 45");
  136. return;
  137. }
  138. //pass this in as degrees now, convert to radians later during actual work phase
  139. rotation = Util.Clamp<float>(rotation, -359f, 359f);
  140. });
  141. options.Add("rotation-center=", delegate(string v)
  142. {
  143. try
  144. {
  145. m_log.Info("[ARCHIVER MODULE] Warning: --rotation-center no longer does anything and will be removed soon!");
  146. rotationCenter = v == null ? Vector3.Zero : Vector3.Parse(v);
  147. }
  148. catch
  149. {
  150. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing rotation displacement");
  151. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as vector3: --rotation-center \"<128,128,0>\"");
  152. return;
  153. }
  154. });
  155. options.Add("bounding-origin=", delegate(string v)
  156. {
  157. try
  158. {
  159. boundingOrigin = v == null ? Vector3.Zero : Vector3.Parse(v);
  160. }
  161. catch
  162. {
  163. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing bounding cube origin");
  164. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as vector3: --bounding-origin \"<128,128,0>\"");
  165. return;
  166. }
  167. });
  168. options.Add("bounding-size=", delegate(string v)
  169. {
  170. try
  171. {
  172. boundingSize = v == null ? new Vector3(Scene.RegionInfo.RegionSizeX, Scene.RegionInfo.RegionSizeY, float.MaxValue) : Vector3.Parse(v);
  173. }
  174. catch
  175. {
  176. m_log.ErrorFormat("[ARCHIVER MODULE] failure parsing bounding cube size");
  177. m_log.ErrorFormat("[ARCHIVER MODULE] Must be represented as a positive vector3: --bounding-size \"<256,256,4096>\"");
  178. return;
  179. }
  180. });
  181. options.Add("d|debug", delegate(string v) { debug = (v != null); });
  182. // Send a message to the region ready module
  183. /* bluewall* Disable this for the time being
  184. IRegionReadyModule rready = m_scene.RequestModuleInterface<IRegionReadyModule>();
  185. if (rready != null)
  186. {
  187. rready.OarLoadingAlert("load");
  188. }
  189. */
  190. List<string> mainParams = options.Parse(cmdparams);
  191. // m_log.DebugFormat("MERGE OAR IS [{0}]", mergeOar);
  192. //
  193. // foreach (string param in mainParams)
  194. // m_log.DebugFormat("GOT PARAM [{0}]", param);
  195. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  196. if (mergeOar) archiveOptions.Add("merge", null);
  197. if (skipAssets) archiveOptions.Add("skipAssets", null);
  198. if (mergeReplaceObjects) archiveOptions.Add("mReplaceObjects", null);
  199. if (mergeTerrain) archiveOptions.Add("merge-terrain", null);
  200. if (mergeParcels) archiveOptions.Add("merge-parcels", null);
  201. if (noObjects) archiveOptions.Add("no-objects", null);
  202. if (defaultUser != "")
  203. {
  204. UUID defaultUserUUID = UUID.Zero;
  205. try
  206. {
  207. defaultUserUUID = Scene.UserManagementModule.GetUserIdByName(defaultUser);
  208. }
  209. catch
  210. {
  211. m_log.ErrorFormat("[ARCHIVER MODULE] default user must be in format \"First Last\"", defaultUser);
  212. }
  213. if (defaultUserUUID == UUID.Zero)
  214. {
  215. m_log.ErrorFormat("[ARCHIVER MODULE] cannot find specified default user {0}", defaultUser);
  216. return;
  217. }
  218. else
  219. {
  220. archiveOptions.Add("default-user", defaultUserUUID);
  221. }
  222. }
  223. archiveOptions.Add("displacement", displacement);
  224. archiveOptions.Add("rotation", rotation);
  225. archiveOptions.Add("rotation-center", rotationCenter);
  226. archiveOptions.Add("bounding-origin", boundingOrigin);
  227. archiveOptions.Add("bounding-size", boundingSize);
  228. if (debug) archiveOptions.Add("debug", null);
  229. if (mainParams.Count > 2)
  230. {
  231. DearchiveRegion(mainParams[2], Guid.Empty, archiveOptions);
  232. }
  233. else
  234. {
  235. DearchiveRegion(DEFAULT_OAR_BACKUP_FILENAME, Guid.Empty, archiveOptions);
  236. }
  237. }
  238. /// <summary>
  239. /// Save a region to a file, including all the assets needed to restore it.
  240. /// </summary>
  241. /// <param name="cmdparams"></param>
  242. public void HandleSaveOarConsoleCommand(string module, string[] cmdparams)
  243. {
  244. Dictionary<string, object> options = new Dictionary<string, object>();
  245. OptionSet ops = new OptionSet();
  246. // legacy argument [obsolete]
  247. ops.Add("p|profile=", delegate(string v) { Console.WriteLine("\n WARNING: -profile option is obsolete and it will not work. Use -home instead.\n"); });
  248. // preferred
  249. ops.Add("h|home=", delegate(string v) { options["home"] = v; });
  250. ops.Add("noassets", delegate(string v) { options["noassets"] = v != null; });
  251. ops.Add("publish", v => options["wipe-owners"] = v != null);
  252. ops.Add("perm=", delegate(string v) { options["checkPermissions"] = v; });
  253. ops.Add("all", delegate(string v) { options["all"] = v != null; });
  254. List<string> mainParams = ops.Parse(cmdparams);
  255. string path;
  256. if (mainParams.Count > 2)
  257. path = mainParams[2];
  258. else
  259. path = DEFAULT_OAR_BACKUP_FILENAME;
  260. // Not doing this right now as this causes some problems with auto-backup systems. Maybe a force flag is
  261. // needed
  262. // if (!ConsoleUtil.CheckFileDoesNotExist(MainConsole.Instance, path))
  263. // return;
  264. ArchiveRegion(path, options);
  265. }
  266. public void ArchiveRegion(string savePath, Dictionary<string, object> options)
  267. {
  268. ArchiveRegion(savePath, Guid.Empty, options);
  269. }
  270. public void ArchiveRegion(string savePath, Guid requestId, Dictionary<string, object> options)
  271. {
  272. m_log.InfoFormat(
  273. "[ARCHIVER]: Writing archive for region {0} to {1}", Scene.RegionInfo.RegionName, savePath);
  274. new ArchiveWriteRequest(Scene, savePath, requestId).ArchiveRegion(options);
  275. }
  276. public void ArchiveRegion(Stream saveStream)
  277. {
  278. ArchiveRegion(saveStream, Guid.Empty);
  279. }
  280. public void ArchiveRegion(Stream saveStream, Guid requestId)
  281. {
  282. ArchiveRegion(saveStream, requestId, new Dictionary<string, object>());
  283. }
  284. public void ArchiveRegion(Stream saveStream, Guid requestId, Dictionary<string, object> options)
  285. {
  286. new ArchiveWriteRequest(Scene, saveStream, requestId).ArchiveRegion(options);
  287. }
  288. public void DearchiveRegion(string loadPath)
  289. {
  290. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  291. DearchiveRegion(loadPath, Guid.Empty, archiveOptions);
  292. }
  293. public void DearchiveRegion(string loadPath, Guid requestId, Dictionary<string, object> options)
  294. {
  295. m_log.InfoFormat(
  296. "[ARCHIVER]: Loading archive to region {0} from {1}", Scene.RegionInfo.RegionName, loadPath);
  297. new ArchiveReadRequest(Scene, loadPath, requestId, options).DearchiveRegion();
  298. }
  299. public void DearchiveRegion(Stream loadStream)
  300. {
  301. Dictionary<string, object> archiveOptions = new Dictionary<string, object>();
  302. DearchiveRegion(loadStream, Guid.Empty, archiveOptions);
  303. }
  304. public void DearchiveRegion(Stream loadStream, Guid requestId, Dictionary<string, object> options)
  305. {
  306. new ArchiveReadRequest(Scene, loadStream, requestId, options).DearchiveRegion();
  307. }
  308. }
  309. }