* First pass at persistence support * Fixed a chat crashing bug * Remove avatars from the scene on logout * Sanity check before adding objects to the scene in ObjectAdd * Sanity check in CompleteAgentMovementHandler if the avatar is already in the scene * Added ContainsKey() to DoubleDictionary git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@2245 52acb1d6-8a22-11de-b505-999d5b087335
88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using OpenMetaverse;
|
|
using OpenMetaverse.StructuredData;
|
|
|
|
namespace Simian.Extensions
|
|
{
|
|
public class XMLPersistence : ISimianExtension, IPersistenceProvider
|
|
{
|
|
Simian server;
|
|
|
|
public XMLPersistence(Simian server)
|
|
{
|
|
this.server = server;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
LLSD llsd;
|
|
|
|
try
|
|
{
|
|
XmlTextReader reader = new XmlTextReader(File.OpenRead(server.DataDir + "simiandata.xml"));
|
|
llsd = LLSDParser.DeserializeXml(reader);
|
|
reader.Close();
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log("Failed to load saved data: " + ex.Message, Helpers.LogLevel.Error);
|
|
return;
|
|
}
|
|
|
|
if (llsd is LLSDMap)
|
|
{
|
|
LLSDMap dictionary = (LLSDMap)llsd;
|
|
|
|
for (int i = 0; i < server.PersistentExtensions.Count; i++)
|
|
{
|
|
IPersistable persistable = server.PersistentExtensions[i];
|
|
|
|
LLSD savedData;
|
|
if (dictionary.TryGetValue(persistable.ToString(), out savedData))
|
|
{
|
|
Logger.DebugLog("Loading saved data for " + persistable.ToString());
|
|
persistable.Deserialize(savedData);
|
|
}
|
|
else
|
|
{
|
|
Logger.DebugLog("No saved data found for " + persistable.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
LLSDMap dictionary = new LLSDMap(server.PersistentExtensions.Count);
|
|
|
|
for (int i = 0; i < server.PersistentExtensions.Count; i++)
|
|
{
|
|
IPersistable persistable = server.PersistentExtensions[i];
|
|
|
|
Logger.DebugLog("Storing persistant data for " + persistable.ToString());
|
|
dictionary.Add(persistable.ToString(), persistable.Serialize());
|
|
}
|
|
|
|
try
|
|
{
|
|
XmlTextWriter writer = new XmlTextWriter(server.DataDir + "simiandata.xml", System.Text.Encoding.UTF8);
|
|
writer.WriteStartElement("llsd");
|
|
LLSDParser.SerializeXmlElement(writer, dictionary);
|
|
writer.WriteEndElement();
|
|
writer.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log("Failed to save persistance data: " + ex.Message, Helpers.LogLevel.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|