SerialiserModule.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Region.CoreModules.Framework.InterfaceCommander;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Region.Framework.Scenes.Serialization;
  38. namespace OpenSim.Region.CoreModules.World.Serialiser
  39. {
  40. public class SerialiserModule : ISharedRegionModule, IRegionSerialiserModule
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private Commander m_commander = new Commander("export");
  45. private List<Scene> m_regions = new List<Scene>();
  46. private string m_savedir = "exports" + "/";
  47. private List<IFileSerialiser> m_serialisers = new List<IFileSerialiser>();
  48. #region ISharedRegionModule Members
  49. public Type ReplaceableInterface
  50. {
  51. get { return null; }
  52. }
  53. public void Initialise(IConfigSource source)
  54. {
  55. IConfig config = source.Configs["Serialiser"];
  56. if (config != null)
  57. {
  58. m_savedir = config.GetString("save_dir", m_savedir);
  59. }
  60. m_log.InfoFormat("[Serialiser] Enabled, using save dir \"{0}\"", m_savedir);
  61. }
  62. public void PostInitialise()
  63. {
  64. lock (m_serialisers)
  65. {
  66. m_serialisers.Add(new SerialiseTerrain());
  67. m_serialisers.Add(new SerialiseObjects());
  68. }
  69. LoadCommanderCommands();
  70. }
  71. public void AddRegion(Scene scene)
  72. {
  73. scene.RegisterModuleCommander(m_commander);
  74. scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  75. scene.RegisterModuleInterface<IRegionSerialiserModule>(this);
  76. lock (m_regions)
  77. {
  78. m_regions.Add(scene);
  79. }
  80. }
  81. public void RegionLoaded(Scene scene)
  82. {
  83. }
  84. public void RemoveRegion(Scene scene)
  85. {
  86. lock (m_regions)
  87. {
  88. m_regions.Remove(scene);
  89. }
  90. }
  91. public void Close()
  92. {
  93. m_regions.Clear();
  94. }
  95. public string Name
  96. {
  97. get { return "ExportSerialisationModule"; }
  98. }
  99. #endregion
  100. #region IRegionSerialiser Members
  101. public void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
  102. {
  103. SceneXmlLoader.LoadPrimsFromXml(scene, fileName, newIDS, loadOffset);
  104. }
  105. public void SavePrimsToXml(Scene scene, string fileName)
  106. {
  107. SceneXmlLoader.SavePrimsToXml(scene, fileName);
  108. }
  109. public void LoadPrimsFromXml2(Scene scene, string fileName)
  110. {
  111. SceneXmlLoader.LoadPrimsFromXml2(scene, fileName);
  112. }
  113. public void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
  114. {
  115. SceneXmlLoader.LoadPrimsFromXml2(scene, reader, startScripts);
  116. }
  117. public void SavePrimsToXml2(Scene scene, string fileName)
  118. {
  119. SceneXmlLoader.SavePrimsToXml2(scene, fileName);
  120. }
  121. public void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
  122. {
  123. SceneXmlLoader.SavePrimsToXml2(scene, stream, min, max);
  124. }
  125. public void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
  126. {
  127. SceneXmlLoader.SaveNamedPrimsToXml2(scene, primName, fileName);
  128. }
  129. public SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
  130. {
  131. return SceneXmlLoader.DeserializeGroupFromXml2(xmlString);
  132. }
  133. public string SerializeGroupToXml2(SceneObjectGroup grp)
  134. {
  135. return SceneXmlLoader.SaveGroupToXml2(grp);
  136. }
  137. public void SavePrimListToXml2(List<EntityBase> entityList, string fileName)
  138. {
  139. SceneXmlLoader.SavePrimListToXml2(entityList, fileName);
  140. }
  141. public void SavePrimListToXml2(List<EntityBase> entityList, TextWriter stream, Vector3 min, Vector3 max)
  142. {
  143. SceneXmlLoader.SavePrimListToXml2(entityList, stream, min, max);
  144. }
  145. public List<string> SerialiseRegion(Scene scene, string saveDir)
  146. {
  147. List<string> results = new List<string>();
  148. if (!Directory.Exists(saveDir))
  149. {
  150. Directory.CreateDirectory(saveDir);
  151. }
  152. lock (m_serialisers)
  153. {
  154. foreach (IFileSerialiser serialiser in m_serialisers)
  155. {
  156. results.Add(serialiser.WriteToFile(scene, saveDir));
  157. }
  158. }
  159. TextWriter regionInfoWriter = new StreamWriter(saveDir + "README.TXT");
  160. regionInfoWriter.WriteLine("Region Name: " + scene.RegionInfo.RegionName);
  161. regionInfoWriter.WriteLine("Region ID: " + scene.RegionInfo.RegionID.ToString());
  162. regionInfoWriter.WriteLine("Backup Time: UTC " + DateTime.UtcNow.ToString());
  163. regionInfoWriter.WriteLine("Serialise Version: 0.1");
  164. regionInfoWriter.Close();
  165. TextWriter manifestWriter = new StreamWriter(saveDir + "region.manifest");
  166. foreach (string line in results)
  167. {
  168. manifestWriter.WriteLine(line);
  169. }
  170. manifestWriter.Close();
  171. return results;
  172. }
  173. #endregion
  174. private void EventManager_OnPluginConsole(string[] args)
  175. {
  176. if (args[0] == "export")
  177. {
  178. string[] tmpArgs = new string[args.Length - 2];
  179. int i = 0;
  180. for (i = 2; i < args.Length; i++)
  181. tmpArgs[i - 2] = args[i];
  182. m_commander.ProcessConsoleCommand(args[1], tmpArgs);
  183. }
  184. }
  185. private void InterfaceSaveRegion(Object[] args)
  186. {
  187. foreach (Scene region in m_regions)
  188. {
  189. if (region.RegionInfo.RegionName == (string) args[0])
  190. {
  191. // List<string> results = SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
  192. SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
  193. }
  194. }
  195. }
  196. private void InterfaceSaveAllRegions(Object[] args)
  197. {
  198. foreach (Scene region in m_regions)
  199. {
  200. // List<string> results = SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
  201. SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
  202. }
  203. }
  204. private void LoadCommanderCommands()
  205. {
  206. Command serialiseSceneCommand = new Command("save", CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveRegion, "Saves the named region into the exports directory.");
  207. serialiseSceneCommand.AddArgument("region-name", "The name of the region you wish to export", "String");
  208. Command serialiseAllScenesCommand = new Command("save-all",CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveAllRegions, "Saves all regions into the exports directory.");
  209. m_commander.RegisterCommand("save", serialiseSceneCommand);
  210. m_commander.RegisterCommand("save-all", serialiseAllScenesCommand);
  211. }
  212. }
  213. }