ArchiverModule.cs 14 KB

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