SerialiserModule.cs 8.3 KB

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