opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.Drawing; |
||
31 | using System.IO; |
||
32 | using System.Reflection; |
||
33 | using System.Xml; |
||
34 | using log4net; |
||
35 | using OpenMetaverse; |
||
36 | using OpenSim.Framework; |
||
37 | using OpenSim.Region.Framework.Interfaces; |
||
38 | using OpenSim.Region.Framework.Scenes; |
||
39 | |||
40 | namespace OpenSim.Region.Framework.Scenes.Serialization |
||
41 | { |
||
42 | /// <summary> |
||
43 | /// Serialize and deserialize coalesced scene objects. |
||
44 | /// </summary> |
||
45 | public class CoalescedSceneObjectsSerializer |
||
46 | { |
||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
48 | |||
49 | /// <summary> |
||
50 | /// Serialize coalesced objects to Xml |
||
51 | /// </summary> |
||
52 | /// <param name="coa"></param> |
||
53 | /// <param name="doScriptStates"> |
||
54 | /// If true then serialize script states. This will halt any running scripts |
||
55 | /// </param> |
||
56 | /// <returns></returns> |
||
57 | public static string ToXml(CoalescedSceneObjects coa) |
||
58 | { |
||
59 | return ToXml(coa, true); |
||
60 | } |
||
61 | |||
62 | /// <summary> |
||
63 | /// Serialize coalesced objects to Xml |
||
64 | /// </summary> |
||
65 | /// <param name="coa"></param> |
||
66 | /// <param name="doScriptStates"> |
||
67 | /// If true then serialize script states. This will halt any running scripts |
||
68 | /// </param> |
||
69 | /// <returns></returns> |
||
70 | public static string ToXml(CoalescedSceneObjects coa, bool doScriptStates) |
||
71 | { |
||
72 | using (StringWriter sw = new StringWriter()) |
||
73 | { |
||
74 | using (XmlTextWriter writer = new XmlTextWriter(sw)) |
||
75 | { |
||
76 | Vector3 size; |
||
77 | |||
78 | List<SceneObjectGroup> coaObjects = coa.Objects; |
||
79 | |||
80 | // m_log.DebugFormat( |
||
81 | // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing {0} objects for coalesced object", |
||
82 | // coaObjects.Count); |
||
83 | |||
84 | // This is weak - we're relying on the set of coalesced objects still being identical |
||
85 | Vector3[] offsets = coa.GetSizeAndOffsets(out size); |
||
86 | |||
87 | writer.WriteStartElement("CoalescedObject"); |
||
88 | |||
89 | writer.WriteAttributeString("x", size.X.ToString()); |
||
90 | writer.WriteAttributeString("y", size.Y.ToString()); |
||
91 | writer.WriteAttributeString("z", size.Z.ToString()); |
||
92 | |||
93 | // Embed the offsets into the group XML |
||
94 | for (int i = 0; i < coaObjects.Count; i++) |
||
95 | { |
||
96 | SceneObjectGroup obj = coaObjects[i]; |
||
97 | |||
98 | // m_log.DebugFormat( |
||
99 | // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing offset for object {0}, {1}", |
||
100 | // i, obj.Name); |
||
101 | |||
102 | writer.WriteStartElement("SceneObjectGroup"); |
||
103 | writer.WriteAttributeString("offsetx", offsets[i].X.ToString()); |
||
104 | writer.WriteAttributeString("offsety", offsets[i].Y.ToString()); |
||
105 | writer.WriteAttributeString("offsetz", offsets[i].Z.ToString()); |
||
106 | |||
107 | SceneObjectSerializer.ToOriginalXmlFormat(obj, writer, doScriptStates); |
||
108 | |||
109 | writer.WriteEndElement(); // SceneObjectGroup |
||
110 | } |
||
111 | |||
112 | writer.WriteEndElement(); // CoalescedObject |
||
113 | } |
||
114 | |||
115 | string output = sw.ToString(); |
||
116 | |||
117 | // Console.WriteLine(output); |
||
118 | |||
119 | return output; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public static bool TryFromXml(string xml, out CoalescedSceneObjects coa) |
||
124 | { |
||
125 | // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml); |
||
126 | |||
127 | coa = null; |
||
128 | int i = 0; |
||
129 | |||
130 | using (StringReader sr = new StringReader(xml)) |
||
131 | { |
||
132 | using (XmlTextReader reader = new XmlTextReader(sr)) |
||
133 | { |
||
134 | try |
||
135 | { |
||
136 | reader.Read(); |
||
137 | if (reader.Name != "CoalescedObject") |
||
138 | { |
||
139 | // m_log.DebugFormat( |
||
140 | // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false", |
||
141 | // reader.Name); |
||
142 | |||
143 | return false; |
||
144 | } |
||
145 | |||
146 | coa = new CoalescedSceneObjects(UUID.Zero); |
||
147 | reader.Read(); |
||
148 | |||
149 | while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "CoalescedObject") |
||
150 | { |
||
151 | if (reader.Name == "SceneObjectGroup") |
||
152 | { |
||
153 | string soXml = reader.ReadOuterXml(); |
||
154 | |||
155 | SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(soXml); |
||
156 | |||
157 | if (so != null) |
||
158 | { |
||
159 | coa.Add(so); |
||
160 | } |
||
161 | else |
||
162 | { |
||
163 | // XXX: Possibly we should fail outright here rather than continuing if a particular component of the |
||
164 | // coalesced object fails to load. |
||
165 | m_log.WarnFormat( |
||
166 | "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.", |
||
167 | i); |
||
168 | } |
||
169 | |||
170 | i++; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | reader.ReadEndElement(); // CoalescedObject |
||
175 | } |
||
176 | catch (Exception e) |
||
177 | { |
||
178 | m_log.ErrorFormat( |
||
179 | "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} {1}", |
||
180 | e.Message, e.StackTrace); |
||
181 | |||
182 | return false; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | return true; |
||
188 | } |
||
189 | } |
||
190 | } |