SerialiserModule.cs 9.3 KB

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