ArchiverModule.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.World.Archiver
  37. {
  38. /// <summary>
  39. /// This module loads and saves OpenSimulator region archives
  40. /// </summary>
  41. public class ArchiverModule : INonSharedRegionModule, IRegionArchiverModule
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private Scene m_scene;
  46. /// <value>
  47. /// The file used to load and save an opensimulator archive if no filename has been specified
  48. /// </value>
  49. protected const string DEFAULT_OAR_BACKUP_FILENAME = "region.oar";
  50. public string Name
  51. {
  52. get { return "RegionArchiverModule"; }
  53. }
  54. public Type ReplaceableInterface
  55. {
  56. get { return null; }
  57. }
  58. public void Initialise(IConfigSource source)
  59. {
  60. //m_log.Debug("[ARCHIVER] Initialising");
  61. }
  62. public void AddRegion(Scene scene)
  63. {
  64. m_scene = scene;
  65. m_scene.RegisterModuleInterface<IRegionArchiverModule>(this);
  66. //m_log.DebugFormat("[ARCHIVER]: Enabled for region {0}", scene.RegionInfo.RegionName);
  67. }
  68. public void RegionLoaded(Scene scene)
  69. {
  70. }
  71. public void RemoveRegion(Scene scene)
  72. {
  73. }
  74. public void Close()
  75. {
  76. }
  77. /// <summary>
  78. /// Load a whole region from an opensimulator archive.
  79. /// </summary>
  80. /// <param name="cmdparams"></param>
  81. public void HandleLoadOarConsoleCommand(string module, string[] cmdparams)
  82. {
  83. bool mergeOar = false;
  84. bool skipAssets = false;
  85. OptionSet options = new OptionSet().Add("m|merge", delegate (string v) { mergeOar = v != null; });
  86. options.Add("s|skip-assets", delegate (string v) { skipAssets = v != null; });
  87. List<string> mainParams = options.Parse(cmdparams);
  88. // m_log.DebugFormat("MERGE OAR IS [{0}]", mergeOar);
  89. //
  90. // foreach (string param in mainParams)
  91. // m_log.DebugFormat("GOT PARAM [{0}]", param);
  92. if (mainParams.Count > 2)
  93. {
  94. DearchiveRegion(mainParams[2], mergeOar, skipAssets, Guid.Empty);
  95. }
  96. else
  97. {
  98. DearchiveRegion(DEFAULT_OAR_BACKUP_FILENAME, mergeOar, skipAssets, Guid.Empty);
  99. }
  100. }
  101. /// <summary>
  102. /// Save a region to a file, including all the assets needed to restore it.
  103. /// </summary>
  104. /// <param name="cmdparams"></param>
  105. public void HandleSaveOarConsoleCommand(string module, string[] cmdparams)
  106. {
  107. Dictionary<string, object> options = new Dictionary<string, object>();
  108. OptionSet ops = new OptionSet();
  109. // ops.Add("v|version=", delegate(string v) { options["version"] = v; });
  110. ops.Add("p|profile=", delegate(string v) { options["profile"] = v; });
  111. ops.Add("noassets", delegate(string v) { options["noassets"] = v != null; });
  112. ops.Add("perm=", delegate(string v) { options["checkPermissions"] = v; });
  113. List<string> mainParams = ops.Parse(cmdparams);
  114. if (mainParams.Count > 2)
  115. {
  116. ArchiveRegion(mainParams[2], options);
  117. }
  118. else
  119. {
  120. ArchiveRegion(DEFAULT_OAR_BACKUP_FILENAME, options);
  121. }
  122. }
  123. public void ArchiveRegion(string savePath, Dictionary<string, object> options)
  124. {
  125. ArchiveRegion(savePath, Guid.Empty, options);
  126. }
  127. public void ArchiveRegion(string savePath, Guid requestId, Dictionary<string, object> options)
  128. {
  129. m_log.InfoFormat(
  130. "[ARCHIVER]: Writing archive for region {0} to {1}", m_scene.RegionInfo.RegionName, savePath);
  131. new ArchiveWriteRequestPreparation(m_scene, savePath, requestId).ArchiveRegion(options);
  132. }
  133. public void ArchiveRegion(Stream saveStream)
  134. {
  135. ArchiveRegion(saveStream, Guid.Empty);
  136. }
  137. public void ArchiveRegion(Stream saveStream, Guid requestId)
  138. {
  139. ArchiveRegion(saveStream, requestId, new Dictionary<string, object>());
  140. }
  141. public void ArchiveRegion(Stream saveStream, Guid requestId, Dictionary<string, object> options)
  142. {
  143. new ArchiveWriteRequestPreparation(m_scene, saveStream, requestId).ArchiveRegion(options);
  144. }
  145. public void DearchiveRegion(string loadPath)
  146. {
  147. DearchiveRegion(loadPath, false, false, Guid.Empty);
  148. }
  149. public void DearchiveRegion(string loadPath, bool merge, bool skipAssets, Guid requestId)
  150. {
  151. m_log.InfoFormat(
  152. "[ARCHIVER]: Loading archive to region {0} from {1}", m_scene.RegionInfo.RegionName, loadPath);
  153. new ArchiveReadRequest(m_scene, loadPath, merge, skipAssets, requestId).DearchiveRegion();
  154. }
  155. public void DearchiveRegion(Stream loadStream)
  156. {
  157. DearchiveRegion(loadStream, false, false, Guid.Empty);
  158. }
  159. public void DearchiveRegion(Stream loadStream, bool merge, bool skipAssets, Guid requestId)
  160. {
  161. new ArchiveReadRequest(m_scene, loadStream, merge, skipAssets, requestId).DearchiveRegion();
  162. }
  163. }
  164. }