OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SerialiserModule.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 
33 using log4net;
34 using Nini.Config;
35 using Mono.Addins;
36 
37 using OpenMetaverse;
38 using OpenSim.Region.CoreModules.Framework.InterfaceCommander;
39 using OpenSim.Region.Framework.Interfaces;
40 using OpenSim.Region.Framework.Scenes;
41 using OpenSim.Region.Framework.Scenes.Serialization;
42 
43 namespace OpenSim.Region.CoreModules.World.Serialiser
44 {
45  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SerialiserModule")]
47  {
48  private static readonly ILog m_log =
49  LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50 
51 // private Commander m_commander = new Commander("export");
52  private List<Scene> m_regions = new List<Scene>();
53  private string m_savedir = "exports";
54  private List<IFileSerialiser> m_serialisers = new List<IFileSerialiser>();
55 
56  #region ISharedRegionModule Members
57 
58  public Type ReplaceableInterface
59  {
60  get { return null; }
61  }
62 
63  public void Initialise(IConfigSource source)
64  {
65  IConfig config = source.Configs["Serialiser"];
66  if (config != null)
67  {
68  m_savedir = config.GetString("save_dir", m_savedir);
69  }
70 
71  m_log.InfoFormat("[Serialiser] Enabled, using save dir \"{0}\"", m_savedir);
72  }
73 
74  public void PostInitialise()
75  {
76  lock (m_serialisers)
77  {
78  m_serialisers.Add(new SerialiseTerrain());
79  m_serialisers.Add(new SerialiseObjects());
80  }
81 
82 // LoadCommanderCommands();
83  }
84 
85  public void AddRegion(Scene scene)
86  {
87 // scene.RegisterModuleCommander(m_commander);
88 // scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
89  scene.RegisterModuleInterface<IRegionSerialiserModule>(this);
90 
91  lock (m_regions)
92  {
93  m_regions.Add(scene);
94  }
95  }
96 
97  public void RegionLoaded(Scene scene)
98  {
99  }
100 
101  public void RemoveRegion(Scene scene)
102  {
103  lock (m_regions)
104  {
105  m_regions.Remove(scene);
106  }
107  }
108 
109  public void Close()
110  {
111  m_regions.Clear();
112  }
113 
114  public string Name
115  {
116  get { return "ExportSerialisationModule"; }
117  }
118 
119  #endregion
120 
121 
122  #region IRegionSerialiser Members
123 
124  public void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
125  {
126  SceneXmlLoader.LoadPrimsFromXml(scene, fileName, newIDS, loadOffset);
127  }
128 
129  public void SavePrimsToXml(Scene scene, string fileName)
130  {
131  SceneXmlLoader.SavePrimsToXml(scene, fileName);
132  }
133 
134  public void LoadPrimsFromXml2(Scene scene, string fileName)
135  {
136  SceneXmlLoader.LoadPrimsFromXml2(scene, fileName);
137  }
138 
139  public void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
140  {
141  SceneXmlLoader.LoadPrimsFromXml2(scene, reader, startScripts);
142  }
143 
144  public void SavePrimsToXml2(Scene scene, string fileName)
145  {
146  SceneXmlLoader.SavePrimsToXml2(scene, fileName);
147  }
148 
149  public void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
150  {
151  SceneXmlLoader.SavePrimsToXml2(scene, stream, min, max);
152  }
153 
154  public void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
155  {
156  SceneXmlLoader.SaveNamedPrimsToXml2(scene, primName, fileName);
157  }
158 
160  {
161  return SceneXmlLoader.DeserializeGroupFromXml2(xmlString);
162  }
163 
164  public string SerializeGroupToXml2(SceneObjectGroup grp, Dictionary<string, object> options)
165  {
166  return SceneXmlLoader.SaveGroupToXml2(grp, options);
167  }
168 
169  public void SavePrimListToXml2(EntityBase[] entityList, string fileName)
170  {
171  SceneXmlLoader.SavePrimListToXml2(entityList, fileName);
172  }
173 
174  public void SavePrimListToXml2(EntityBase[] entityList, TextWriter stream, Vector3 min, Vector3 max)
175  {
176  SceneXmlLoader.SavePrimListToXml2(entityList, stream, min, max);
177  }
178 
179  public List<string> SerialiseRegion(Scene scene, string saveDir)
180  {
181  List<string> results = new List<string>();
182 
183  if (!Directory.Exists(saveDir))
184  {
185  Directory.CreateDirectory(saveDir);
186  }
187 
188  lock (m_serialisers)
189  {
190  foreach (IFileSerialiser serialiser in m_serialisers)
191  {
192  results.Add(serialiser.WriteToFile(scene, saveDir));
193  }
194  }
195 
196  TextWriter regionInfoWriter = new StreamWriter(Path.Combine(saveDir, "README.TXT"));
197  regionInfoWriter.WriteLine("Region Name: " + scene.RegionInfo.RegionName);
198  regionInfoWriter.WriteLine("Region ID: " + scene.RegionInfo.RegionID.ToString());
199  regionInfoWriter.WriteLine("Backup Time: UTC " + DateTime.UtcNow.ToString());
200  regionInfoWriter.WriteLine("Serialise Version: 0.1");
201  regionInfoWriter.Close();
202 
203  TextWriter manifestWriter = new StreamWriter(Path.Combine(saveDir, "region.manifest"));
204  foreach (string line in results)
205  {
206  manifestWriter.WriteLine(line);
207  }
208  manifestWriter.Close();
209 
210  return results;
211  }
212 
213  #endregion
214 
215 // private void EventManager_OnPluginConsole(string[] args)
216 // {
217 // if (args[0] == "export")
218 // {
219 // string[] tmpArgs = new string[args.Length - 2];
220 // int i = 0;
221 // for (i = 2; i < args.Length; i++)
222 // tmpArgs[i - 2] = args[i];
223 //
224 // m_commander.ProcessConsoleCommand(args[1], tmpArgs);
225 // }
226 // }
227 
228  private void InterfaceSaveRegion(Object[] args)
229  {
230  foreach (Scene region in m_regions)
231  {
232  if (region.RegionInfo.RegionName == (string) args[0])
233  {
234  // List<string> results = SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
235  SerialiseRegion(region, Path.Combine(m_savedir, region.RegionInfo.RegionID.ToString()));
236  }
237  }
238  }
239 
240  private void InterfaceSaveAllRegions(Object[] args)
241  {
242  foreach (Scene region in m_regions)
243  {
244  // List<string> results = SerialiseRegion(region, m_savedir + region.RegionInfo.RegionID.ToString() + "/");
245  SerialiseRegion(region, Path.Combine(m_savedir, region.RegionInfo.RegionID.ToString()));
246  }
247  }
248 
249 // private void LoadCommanderCommands()
250 // {
251 // Command serialiseSceneCommand = new Command("save", CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveRegion, "Saves the named region into the exports directory.");
252 // serialiseSceneCommand.AddArgument("region-name", "The name of the region you wish to export", "String");
253 //
254 // Command serialiseAllScenesCommand = new Command("save-all",CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveAllRegions, "Saves all regions into the exports directory.");
255 //
256 // m_commander.RegisterCommand("save", serialiseSceneCommand);
257 // m_commander.RegisterCommand("save-all", serialiseAllScenesCommand);
258 // }
259  }
260 }
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
Save prims in the xml2 format, optionally specifying a bounding box for which prims should be saved...
SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
Deserializes a scene object from its xml2 representation. This does not load the object into the scen...
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
Load prims from the xml format
string SerializeGroupToXml2(SceneObjectGroup grp, Dictionary< string, object > options)
Serialize an individual scene object into the xml2 format
void SavePrimListToXml2(EntityBase[] entityList, TextWriter stream, Vector3 min, Vector3 max)
Save a set of prims in the xml2 format, optionally specifying a bounding box for which prims should b...
void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
void LoadPrimsFromXml2(Scene scene, string fileName)
Load prims from the xml2 format
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
List< string > SerialiseRegion(Scene scene, string saveDir)
void SavePrimsToXml2(Scene scene, string fileName)
Save prims in the xml2 format
void SavePrimsToXml(Scene scene, string fileName)
Save prims in the xml format
void SavePrimListToXml2(EntityBase[] entityList, string fileName)
Save a set of prims in the xml2 format
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
Load prims from the xml2 format