SerialiserModule.cs 8.3 KB

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