From 00ded81d19a5385297bfb29fab2ead04a1e576cd Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Wed, 11 Mar 2009 00:09:47 +0000 Subject: [PATCH] * Sanity check that we are connected to a simulator before teleporting * Gracefully handle zero length layers in JPEG2000 decoding [Simian] * Adding OAR file archive writing (untested) * Add a /save command to Periscope to save an OAR archive of a scene git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@2480 52acb1d6-8a22-11de-b505-999d5b087335 --- OpenMetaverse/AgentManager.cs | 3 + OpenMetaverse/Imaging/OpenJPEG.cs | 2 +- Programs/Simian/Archiving/ArchiveConstants.cs | 123 ++++++++ Programs/Simian/Archiving/AssetsArchiver.cs | 151 ++++++++++ Programs/Simian/Archiving/OarFile.cs | 266 ++++++++++++++++++ Programs/Simian/Archiving/TarArchiveReader.cs | 215 ++++++++++++++ Programs/Simian/Archiving/TarArchiveWriter.cs | 215 ++++++++++++++ Programs/Simian/Extensions/Periscope.cs | 18 ++ 8 files changed, 992 insertions(+), 1 deletion(-) create mode 100644 Programs/Simian/Archiving/ArchiveConstants.cs create mode 100644 Programs/Simian/Archiving/AssetsArchiver.cs create mode 100644 Programs/Simian/Archiving/OarFile.cs create mode 100644 Programs/Simian/Archiving/TarArchiveReader.cs create mode 100644 Programs/Simian/Archiving/TarArchiveWriter.cs diff --git a/OpenMetaverse/AgentManager.cs b/OpenMetaverse/AgentManager.cs index 6f74fed7..0fde9788 100644 --- a/OpenMetaverse/AgentManager.cs +++ b/OpenMetaverse/AgentManager.cs @@ -2163,6 +2163,9 @@ namespace OpenMetaverse /// false public bool Teleport(string simName, Vector3 position, Vector3 lookAt) { + if (Client.Network.CurrentSim == null) + return false; + teleportStat = TeleportStatus.None; simName = simName.ToLower(); diff --git a/OpenMetaverse/Imaging/OpenJPEG.cs b/OpenMetaverse/Imaging/OpenJPEG.cs index 07abe67a..0d06ad92 100644 --- a/OpenMetaverse/Imaging/OpenJPEG.cs +++ b/OpenMetaverse/Imaging/OpenJPEG.cs @@ -382,7 +382,7 @@ namespace OpenMetaverse.Imaging } // More sanity checking - if (layerInfo[layerInfo.Length - 1].End <= encoded.Length - 1) + if (layerInfo.Length == 0 || layerInfo[layerInfo.Length - 1].End <= encoded.Length - 1) { success = true; diff --git a/Programs/Simian/Archiving/ArchiveConstants.cs b/Programs/Simian/Archiving/ArchiveConstants.cs new file mode 100644 index 00000000..c6e54de6 --- /dev/null +++ b/Programs/Simian/Archiving/ArchiveConstants.cs @@ -0,0 +1,123 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.Collections.Generic; +using OpenMetaverse; + +namespace Simian +{ + /// + /// Constants for the archiving module + /// + public class ArchiveConstants + { + /// + /// The location of the archive control file + /// + public static readonly string CONTROL_FILE_PATH = "archive.xml"; + + /// + /// Path for the assets held in an archive + /// + public static readonly string ASSETS_PATH = "assets/"; + + /// + /// Path for the prims file + /// + public static readonly string OBJECTS_PATH = "objects/"; + + /// + /// Path for terrains. Technically these may be assets, but I think it's quite nice to split them out. + /// + public static readonly string TERRAINS_PATH = "terrains/"; + + /// + /// Path for region settings. + /// + public static readonly string SETTINGS_PATH = "settings/"; + + /// + /// The character the separates the uuid from extension information in an archived asset filename + /// + public static readonly string ASSET_EXTENSION_SEPARATOR = "_"; + + /// + /// Extensions used for asset types in the archive + /// + public static readonly IDictionary ASSET_TYPE_TO_EXTENSION = new Dictionary(); + public static readonly IDictionary EXTENSION_TO_ASSET_TYPE = new Dictionary(); + + static ArchiveConstants() + { + ASSET_TYPE_TO_EXTENSION[AssetType.Animation] = ASSET_EXTENSION_SEPARATOR + "animation.bvh"; + ASSET_TYPE_TO_EXTENSION[AssetType.Bodypart] = ASSET_EXTENSION_SEPARATOR + "bodypart.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.CallingCard] = ASSET_EXTENSION_SEPARATOR + "callingcard.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.Clothing] = ASSET_EXTENSION_SEPARATOR + "clothing.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.Folder] = ASSET_EXTENSION_SEPARATOR + "folder.txt"; // Not sure if we'll ever see this + ASSET_TYPE_TO_EXTENSION[AssetType.Gesture] = ASSET_EXTENSION_SEPARATOR + "gesture.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.ImageJPEG] = ASSET_EXTENSION_SEPARATOR + "image.jpg"; + ASSET_TYPE_TO_EXTENSION[AssetType.ImageTGA] = ASSET_EXTENSION_SEPARATOR + "image.tga"; + ASSET_TYPE_TO_EXTENSION[AssetType.Landmark] = ASSET_EXTENSION_SEPARATOR + "landmark.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.LostAndFoundFolder] = ASSET_EXTENSION_SEPARATOR + "lostandfoundfolder.txt"; // Not sure if we'll ever see this + ASSET_TYPE_TO_EXTENSION[AssetType.LSLBytecode] = ASSET_EXTENSION_SEPARATOR + "bytecode.lso"; + ASSET_TYPE_TO_EXTENSION[AssetType.LSLText] = ASSET_EXTENSION_SEPARATOR + "script.lsl"; + ASSET_TYPE_TO_EXTENSION[AssetType.Notecard] = ASSET_EXTENSION_SEPARATOR + "notecard.txt"; + ASSET_TYPE_TO_EXTENSION[AssetType.Object] = ASSET_EXTENSION_SEPARATOR + "object.xml"; + ASSET_TYPE_TO_EXTENSION[AssetType.RootFolder] = ASSET_EXTENSION_SEPARATOR + "rootfolder.txt"; // Not sure if we'll ever see this + ASSET_TYPE_TO_EXTENSION[AssetType.Simstate] = ASSET_EXTENSION_SEPARATOR + "simstate.bin"; // Not sure if we'll ever see this + ASSET_TYPE_TO_EXTENSION[AssetType.SnapshotFolder] = ASSET_EXTENSION_SEPARATOR + "snapshotfolder.txt"; // Not sure if we'll ever see this + ASSET_TYPE_TO_EXTENSION[AssetType.Sound] = ASSET_EXTENSION_SEPARATOR + "sound.ogg"; + ASSET_TYPE_TO_EXTENSION[AssetType.SoundWAV] = ASSET_EXTENSION_SEPARATOR + "sound.wav"; + ASSET_TYPE_TO_EXTENSION[AssetType.Texture] = ASSET_EXTENSION_SEPARATOR + "texture.jp2"; + ASSET_TYPE_TO_EXTENSION[AssetType.TextureTGA] = ASSET_EXTENSION_SEPARATOR + "texture.tga"; + ASSET_TYPE_TO_EXTENSION[AssetType.TrashFolder] = ASSET_EXTENSION_SEPARATOR + "trashfolder.txt"; // Not sure if we'll ever see this + + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "animation.bvh"] = AssetType.Animation; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "bodypart.txt"] = AssetType.Bodypart; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "callingcard.txt"] = AssetType.CallingCard; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "clothing.txt"] = AssetType.Clothing; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "folder.txt"] = AssetType.Folder; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "gesture.txt"] = AssetType.Gesture; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "image.jpg"] = AssetType.ImageJPEG; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "image.tga"] = AssetType.ImageTGA; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "landmark.txt"] = AssetType.Landmark; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "lostandfoundfolder.txt"] = AssetType.LostAndFoundFolder; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "bytecode.lso"] = AssetType.LSLBytecode; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "script.lsl"] = AssetType.LSLText; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "notecard.txt"] = AssetType.Notecard; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "object.xml"] = AssetType.Object; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "rootfolder.txt"] = AssetType.RootFolder; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "simstate.bin"] = AssetType.Simstate; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "snapshotfolder.txt"] = AssetType.SnapshotFolder; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "sound.ogg"] = AssetType.Sound; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "sound.wav"] = AssetType.SoundWAV; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "texture.jp2"] = AssetType.Texture; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "texture.tga"] = AssetType.TextureTGA; + EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "trashfolder.txt"] = AssetType.TrashFolder; + } + } +} diff --git a/Programs/Simian/Archiving/AssetsArchiver.cs b/Programs/Simian/Archiving/AssetsArchiver.cs new file mode 100644 index 00000000..58aadd5b --- /dev/null +++ b/Programs/Simian/Archiving/AssetsArchiver.cs @@ -0,0 +1,151 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Xml; +using OpenMetaverse; + +namespace Simian +{ + /// + /// Archives assets + /// + public class AssetsArchiver + { + /// + /// Post a message to the log every x assets as a progress bar + /// + static int LOG_ASSET_LOAD_NOTIFICATION_INTERVAL = 50; + + /// + /// Archive assets + /// + protected IDictionary m_assets; + + public AssetsArchiver(IDictionary assets) + { + m_assets = assets; + } + + /// + /// Archive the assets given to this archiver to the given archive. + /// + /// + public void Archive(TarArchiveWriter archive) + { + //WriteMetadata(archive); + WriteData(archive); + } + + /// + /// Write an assets metadata file to the given archive + /// + /// + protected void WriteMetadata(TarArchiveWriter archive) + { + StringWriter sw = new StringWriter(); + XmlTextWriter xtw = new XmlTextWriter(sw); + + xtw.Formatting = Formatting.Indented; + xtw.WriteStartDocument(); + + xtw.WriteStartElement("assets"); + + foreach (UUID uuid in m_assets.Keys) + { + Asset asset = m_assets[uuid]; + + if (asset != null) + { + xtw.WriteStartElement("asset"); + + string extension = string.Empty; + + if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.AssetType)) + { + extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.AssetType]; + } + + xtw.WriteElementString("filename", uuid.ToString() + extension); + + xtw.WriteElementString("name", uuid.ToString()); + xtw.WriteElementString("description", String.Empty); + xtw.WriteElementString("asset-type", asset.AssetType.ToString()); + + xtw.WriteEndElement(); + } + } + + xtw.WriteEndElement(); + + xtw.WriteEndDocument(); + + archive.WriteFile("assets.xml", sw.ToString()); + } + + /// + /// Write asset data files to the given archive + /// + /// + protected void WriteData(TarArchiveWriter archive) + { + // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar + //archive.AddDir("assets"); + + int assetsAdded = 0; + + foreach (UUID uuid in m_assets.Keys) + { + Asset asset = m_assets[uuid]; + + string extension = string.Empty; + + if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.AssetType)) + { + extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.AssetType]; + } + else + { + Logger.Log(String.Format( + "[ARCHIVER]: Unrecognized asset type {0} with uuid {1}. This asset will be saved but not reloaded", + asset.AssetType, asset.AssetID), Helpers.LogLevel.Warning); + } + + asset.Encode(); + + archive.WriteFile( + ArchiveConstants.ASSETS_PATH + uuid.ToString() + extension, + asset.AssetData); + + assetsAdded++; + } + } + } +} diff --git a/Programs/Simian/Archiving/OarFile.cs b/Programs/Simian/Archiving/OarFile.cs new file mode 100644 index 00000000..b5c0732a --- /dev/null +++ b/Programs/Simian/Archiving/OarFile.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Text; +using System.Xml; +using OpenMetaverse; + +namespace Simian +{ + public class Linkset + { + public SimulationObject Parent; + public List Children = new List(); + } + + public class OarFile + { + enum ProfileShape : byte + { + Circle = 0, + Square = 1, + IsometricTriangle = 2, + EquilateralTriangle = 3, + RightTriangle = 4, + HalfCircle = 5 + } + + public static void PackageArchive(string directoryName, string filename) + { + const string ARCHIVE_XML = "\n"; + + TarArchiveWriter archive = new TarArchiveWriter(new GZipStream(new FileStream(filename, FileMode.Create), CompressionMode.Compress)); + + // Create the archive.xml file + archive.WriteFile("archive.xml", ARCHIVE_XML); + + // Add the assets + string[] files = Directory.GetFiles(directoryName + "/assets"); + foreach (string file in files) + archive.WriteFile("assets/" + Path.GetFileName(file), File.ReadAllBytes(file)); + + // Add the objects + files = Directory.GetFiles(directoryName + "/objects"); + foreach (string file in files) + archive.WriteFile("objects/" + Path.GetFileName(file), File.ReadAllBytes(file)); + + // Add the terrain(s) + files = Directory.GetFiles(directoryName + "/terrains"); + foreach (string file in files) + archive.WriteFile("terrains/" + Path.GetFileName(file), File.ReadAllBytes(file)); + + archive.Close(); + } + + public static void SavePrims(Simian server, string path) + { + // Delete all of the old linkset files + try + { + Directory.Delete(path, true); + Directory.CreateDirectory(path); + } + catch (Exception ex) + { + Logger.Log("Failed saving prims: " + ex.Message, Helpers.LogLevel.Error); + return; + } + + // Copy the double dictionary to a temporary list for iterating + List primList = new List(); + server.Scene.ForEachObject(delegate(SimulationObject prim) + { + if (!(prim.Prim is Avatar)) + primList.Add(prim); + }); + + foreach (SimulationObject p in primList) + { + if (p.Prim.ParentID == 0) + { + Linkset linkset = new Linkset(); + linkset.Parent = p; + + server.Scene.ForEachObject(delegate(SimulationObject q) + { + if (q.Prim.ParentID == p.Prim.LocalID) + linkset.Children.Add(q); + }); + + SaveLinkset(linkset, path + "/Primitive_" + linkset.Parent.Prim.ID.ToString() + ".xml"); + } + } + } + + static void SaveLinkset(Linkset linkset, string filename) + { + try + { + using (StreamWriter stream = new StreamWriter(filename)) + { + XmlTextWriter writer = new XmlTextWriter(stream); + SOGToXml2(writer, linkset); + writer.Flush(); + } + } + catch (Exception ex) + { + Logger.Log("Failed saving linkset: " + ex.Message, Helpers.LogLevel.Error); + } + } + + static void SOGToXml2(XmlTextWriter writer, Linkset linkset) + { + writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty); + SOPToXml(writer, linkset.Parent, null); + writer.WriteStartElement(String.Empty, "OtherParts", String.Empty); + + foreach (SimulationObject child in linkset.Children) + SOPToXml(writer, child, linkset.Parent); + + writer.WriteEndElement(); + writer.WriteEndElement(); + } + + static void SOPToXml(XmlTextWriter writer, SimulationObject prim, SimulationObject parent) + { + writer.WriteStartElement("SceneObjectPart"); + writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); + writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); + + WriteUUID(writer, "CreatorID", prim.Prim.Properties.CreatorID); + WriteUUID(writer, "FolderID", prim.Prim.Properties.FolderID); + writer.WriteElementString("InventorySerial", prim.Prim.Properties.InventorySerial.ToString()); + writer.WriteStartElement("TaskInventory"); writer.WriteEndElement(); + writer.WriteElementString("ObjectFlags", ((int)prim.Prim.Flags).ToString()); + WriteUUID(writer, "UUID", prim.Prim.ID); + writer.WriteElementString("LocalId", prim.Prim.LocalID.ToString()); + writer.WriteElementString("Name", prim.Prim.Properties.Name); + writer.WriteElementString("Material", ((int)prim.Prim.PrimData.Material).ToString()); + writer.WriteElementString("RegionHandle", prim.Prim.RegionHandle.ToString()); + writer.WriteElementString("ScriptAccessPin", "0"); + + Vector3 groupPosition; + if (parent == null) + groupPosition = prim.Prim.Position; + else + groupPosition = parent.Prim.Position; + + WriteVector(writer, "GroupPosition", groupPosition); + WriteVector(writer, "OffsetPosition", groupPosition - prim.Prim.Position); + WriteQuaternion(writer, "RotationOffset", prim.Prim.Rotation); + WriteVector(writer, "Velocity", Vector3.Zero); + WriteVector(writer, "RotationalVelocity", Vector3.Zero); + WriteVector(writer, "AngularVelocity", prim.Prim.AngularVelocity); + WriteVector(writer, "Acceleration", Vector3.Zero); + writer.WriteElementString("Description", prim.Prim.Properties.Description); + writer.WriteStartElement("Color"); + writer.WriteElementString("R", prim.Prim.TextColor.R.ToString()); + writer.WriteElementString("G", prim.Prim.TextColor.G.ToString()); + writer.WriteElementString("B", prim.Prim.TextColor.B.ToString()); + writer.WriteElementString("A", prim.Prim.TextColor.G.ToString()); + writer.WriteEndElement(); + writer.WriteElementString("Text", prim.Prim.Text); + writer.WriteElementString("SitName", prim.Prim.Properties.SitName); + writer.WriteElementString("TouchName", prim.Prim.Properties.TouchName); + + writer.WriteElementString("LinkNum", prim.LinkNumber.ToString()); + writer.WriteElementString("ClickAction", ((int)prim.Prim.ClickAction).ToString()); + writer.WriteStartElement("Shape"); + + writer.WriteElementString("PathBegin", Primitive.PackBeginCut(prim.Prim.PrimData.PathBegin).ToString()); + writer.WriteElementString("PathCurve", ((byte)prim.Prim.PrimData.PathCurve).ToString()); + writer.WriteElementString("PathEnd", Primitive.PackEndCut(prim.Prim.PrimData.PathEnd).ToString()); + writer.WriteElementString("PathRadiusOffset", Primitive.PackPathTwist(prim.Prim.PrimData.PathRadiusOffset).ToString()); + writer.WriteElementString("PathRevolutions", Primitive.PackPathRevolutions(prim.Prim.PrimData.PathRevolutions).ToString()); + writer.WriteElementString("PathScaleX", Primitive.PackPathScale(prim.Prim.PrimData.PathScaleX).ToString()); + writer.WriteElementString("PathScaleY", Primitive.PackPathScale(prim.Prim.PrimData.PathScaleY).ToString()); + writer.WriteElementString("PathShearX", ((byte)Primitive.PackPathShear(prim.Prim.PrimData.PathShearX)).ToString()); + writer.WriteElementString("PathShearY", ((byte)Primitive.PackPathShear(prim.Prim.PrimData.PathShearY)).ToString()); + writer.WriteElementString("PathSkew", Primitive.PackPathTwist(prim.Prim.PrimData.PathSkew).ToString()); + writer.WriteElementString("PathTaperX", Primitive.PackPathTaper(prim.Prim.PrimData.PathTaperX).ToString()); + writer.WriteElementString("PathTaperY", Primitive.PackPathTaper(prim.Prim.PrimData.PathTaperY).ToString()); + writer.WriteElementString("PathTwist", Primitive.PackPathTwist(prim.Prim.PrimData.PathTwist).ToString()); + writer.WriteElementString("PathTwistBegin", Primitive.PackPathTwist(prim.Prim.PrimData.PathTwistBegin).ToString()); + writer.WriteElementString("PCode", ((byte)prim.Prim.PrimData.PCode).ToString()); + writer.WriteElementString("ProfileBegin", Primitive.PackBeginCut(prim.Prim.PrimData.ProfileBegin).ToString()); + writer.WriteElementString("ProfileEnd", Primitive.PackEndCut(prim.Prim.PrimData.ProfileEnd).ToString()); + writer.WriteElementString("ProfileHollow", Primitive.PackProfileHollow(prim.Prim.PrimData.ProfileHollow).ToString()); + WriteVector(writer, "Scale", prim.Prim.Scale); + writer.WriteElementString("State", prim.Prim.PrimData.State.ToString()); + + ProfileShape shape = (ProfileShape)prim.Prim.PrimData.ProfileCurve; + writer.WriteElementString("ProfileShape", shape.ToString()); + writer.WriteElementString("HollowShape", prim.Prim.PrimData.ProfileHole.ToString()); + writer.WriteElementString("ProfileCurve", prim.Prim.PrimData.profileCurve.ToString()); + + writer.WriteStartElement("TextureEntry"); + + byte[] te; + if (prim.Prim.Textures != null) + te = prim.Prim.Textures.GetBytes(); + else + te = Utils.EmptyBytes; + + writer.WriteBase64(te, 0, te.Length); + writer.WriteEndElement(); + + // FIXME: ExtraParams + writer.WriteStartElement("ExtraParams"); writer.WriteEndElement(); + + writer.WriteEndElement(); + + WriteVector(writer, "Scale", prim.Prim.Scale); + writer.WriteElementString("UpdateFlag", "0"); + WriteVector(writer, "SitTargetOrientation", Vector3.UnitZ); // TODO: Is this really a vector and not a quaternion? + WriteVector(writer, "SitTargetPosition", prim.SitPosition); + WriteVector(writer, "SitTargetPositionLL", prim.SitPosition); + WriteQuaternion(writer, "SitTargetOrientationLL", prim.SitRotation); + writer.WriteElementString("ParentID", prim.Prim.ParentID.ToString()); + writer.WriteElementString("CreationDate", ((int)Utils.DateTimeToUnixTime(prim.Prim.Properties.CreationDate)).ToString()); + writer.WriteElementString("Category", ((int)prim.Prim.Properties.Category).ToString()); + writer.WriteElementString("SalePrice", prim.Prim.Properties.SalePrice.ToString()); + writer.WriteElementString("ObjectSaleType", ((int)prim.Prim.Properties.SaleType).ToString()); + writer.WriteElementString("OwnershipCost", prim.Prim.Properties.OwnershipCost.ToString()); + WriteUUID(writer, "GroupID", prim.Prim.GroupID); + WriteUUID(writer, "OwnerID", prim.Prim.OwnerID); + WriteUUID(writer, "LastOwnerID", prim.Prim.Properties.LastOwnerID); + writer.WriteElementString("BaseMask", ((uint)PermissionMask.All).ToString()); + writer.WriteElementString("OwnerMask", ((uint)PermissionMask.All).ToString()); + writer.WriteElementString("GroupMask", ((uint)PermissionMask.All).ToString()); + writer.WriteElementString("EveryoneMask", ((uint)PermissionMask.All).ToString()); + writer.WriteElementString("NextOwnerMask", ((uint)PermissionMask.All).ToString()); + writer.WriteElementString("Flags", "None"); + WriteUUID(writer, "SitTargetAvatar", UUID.Zero); + + writer.WriteEndElement(); + } + + static void WriteUUID(XmlTextWriter writer, string name, UUID id) + { + writer.WriteStartElement(name); + writer.WriteElementString("UUID", id.ToString()); + writer.WriteEndElement(); + } + + static void WriteVector(XmlTextWriter writer, string name, Vector3 vec) + { + writer.WriteStartElement(name); + writer.WriteElementString("X", vec.X.ToString()); + writer.WriteElementString("Y", vec.Y.ToString()); + writer.WriteElementString("Z", vec.Z.ToString()); + writer.WriteEndElement(); + } + + static void WriteQuaternion(XmlTextWriter writer, string name, Quaternion quat) + { + writer.WriteStartElement(name); + writer.WriteElementString("X", quat.X.ToString()); + writer.WriteElementString("Y", quat.Y.ToString()); + writer.WriteElementString("Z", quat.Z.ToString()); + writer.WriteElementString("W", quat.W.ToString()); + writer.WriteEndElement(); + } + } +} diff --git a/Programs/Simian/Archiving/TarArchiveReader.cs b/Programs/Simian/Archiving/TarArchiveReader.cs new file mode 100644 index 00000000..7831cae7 --- /dev/null +++ b/Programs/Simian/Archiving/TarArchiveReader.cs @@ -0,0 +1,215 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +using System.Reflection; +using System.Text; + +namespace Simian +{ + /// + /// Temporary code to do the bare minimum required to read a tar archive for our purposes + /// + public class TarArchiveReader + { + public enum TarEntryType + { + TYPE_UNKNOWN = 0, + TYPE_NORMAL_FILE = 1, + TYPE_HARD_LINK = 2, + TYPE_SYMBOLIC_LINK = 3, + TYPE_CHAR_SPECIAL = 4, + TYPE_BLOCK_SPECIAL = 5, + TYPE_DIRECTORY = 6, + TYPE_FIFO = 7, + TYPE_CONTIGUOUS_FILE = 8, + } + + protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding(); + + /// + /// Binary reader for the underlying stream + /// + protected BinaryReader m_br; + + /// + /// Used to trim off null chars + /// + protected char[] m_nullCharArray = new char[] { '\0' }; + + /// + /// Generate a tar reader which reads from the given stream. + /// + /// + public TarArchiveReader(Stream s) + { + m_br = new BinaryReader(s); + } + + /// + /// Read the next entry in the tar file. + /// + /// + /// the data for the entry. Returns null if there are no more entries + public byte[] ReadEntry(out string filePath, out TarEntryType entryType) + { + filePath = String.Empty; + entryType = TarEntryType.TYPE_UNKNOWN; + TarHeader header = ReadHeader(); + + if (null == header) + return null; + + entryType = header.EntryType; + filePath = header.FilePath; + return ReadData(header.FileSize); + } + + /// + /// Read the next 512 byte chunk of data as a tar header. + /// + /// A tar header struct. null if we have reached the end of the archive. + protected TarHeader ReadHeader() + { + byte[] header = m_br.ReadBytes(512); + + // If we've reached the end of the archive we'll be in null block territory, which means + // the next byte will be 0 + if (header[0] == 0) + return null; + + TarHeader tarHeader = new TarHeader(); + + // If we're looking at a GNU tar long link then extract the long name and pull up the next header + if (header[156] == (byte)'L') + { + int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11); + tarHeader.FilePath = m_asciiEncoding.GetString(ReadData(longNameLength)); + //m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath); + header = m_br.ReadBytes(512); + } + else + { + tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100); + tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray); + //m_log.DebugFormat("[TAR ARCHIVE READER]: Got short file name {0}", tarHeader.FilePath); + } + + tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11); + + switch (header[156]) + { + case 0: + tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE; + break; + case (byte)'0': + tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE; + break; + case (byte)'1': + tarHeader.EntryType = TarEntryType.TYPE_HARD_LINK; + break; + case (byte)'2': + tarHeader.EntryType = TarEntryType.TYPE_SYMBOLIC_LINK; + break; + case (byte)'3': + tarHeader.EntryType = TarEntryType.TYPE_CHAR_SPECIAL; + break; + case (byte)'4': + tarHeader.EntryType = TarEntryType.TYPE_BLOCK_SPECIAL; + break; + case (byte)'5': + tarHeader.EntryType = TarEntryType.TYPE_DIRECTORY; + break; + case (byte)'6': + tarHeader.EntryType = TarEntryType.TYPE_FIFO; + break; + case (byte)'7': + tarHeader.EntryType = TarEntryType.TYPE_CONTIGUOUS_FILE; + break; + } + + return tarHeader; + } + + /// + /// Read data following a header + /// + /// + /// + protected byte[] ReadData(int fileSize) + { + byte[] data = m_br.ReadBytes(fileSize); + + //m_log.DebugFormat("[TAR ARCHIVE READER]: fileSize {0}", fileSize); + + // Read the rest of the empty padding in the 512 byte block + if (fileSize % 512 != 0) + { + int paddingLeft = 512 - (fileSize % 512); + + //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft); + + m_br.ReadBytes(paddingLeft); + } + + return data; + } + + public void Close() + { + m_br.Close(); + } + + /// + /// Convert octal bytes to a decimal representation + /// + /// + /// + public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count) + { + string oString = m_asciiEncoding.GetString(bytes, startIndex, count); + + int d = 0; + + foreach (char c in oString) + { + d <<= 3; + d |= c - '0'; + } + + return d; + } + } + + public class TarHeader + { + public string FilePath; + public int FileSize; + public TarArchiveReader.TarEntryType EntryType; + } +} diff --git a/Programs/Simian/Archiving/TarArchiveWriter.cs b/Programs/Simian/Archiving/TarArchiveWriter.cs new file mode 100644 index 00000000..4fd28d17 --- /dev/null +++ b/Programs/Simian/Archiving/TarArchiveWriter.cs @@ -0,0 +1,215 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Simian +{ + /// + /// Temporary code to produce a tar archive in tar v7 format + /// + public class TarArchiveWriter + { + protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding(); + + /// + /// Binary writer for the underlying stream + /// + protected BinaryWriter m_bw; + + public TarArchiveWriter(Stream s) + { + m_bw = new BinaryWriter(s); + } + + /// + /// Write a directory entry to the tar archive. We can only handle one path level right now! + /// + /// + public void WriteDir(string dirName) + { + // Directories are signalled by a final / + if (!dirName.EndsWith("/")) + dirName += "/"; + + WriteFile(dirName, new byte[0]); + } + + /// + /// Write a file to the tar archive + /// + /// + /// + public void WriteFile(string filePath, string data) + { + WriteFile(filePath, m_asciiEncoding.GetBytes(data)); + } + + /// + /// Write a file to the tar archive + /// + /// + /// + public void WriteFile(string filePath, byte[] data) + { + if (filePath.Length > 100) + WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L'); + + char fileType; + + if (filePath.EndsWith("/")) + { + fileType = '5'; + } + else + { + fileType = '0'; + } + + WriteEntry(filePath, data, fileType); + } + + /// + /// Finish writing the raw tar archive data to a stream. The stream will be closed on completion. + /// + /// Stream to which to write the data + /// + public void Close() + { + //m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks"); + + // Write two consecutive 0 blocks to end the archive + byte[] finalZeroPadding = new byte[1024]; + m_bw.Write(finalZeroPadding); + + m_bw.Flush(); + m_bw.Close(); + } + + public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding) + { + string oString = ""; + + while (d > 0) + { + oString = Convert.ToString((byte)'0' + d & 7) + oString; + d >>= 3; + } + + while (oString.Length < padding) + { + oString = "0" + oString; + } + + byte[] oBytes = m_asciiEncoding.GetBytes(oString); + + return oBytes; + } + + /// + /// Write a particular entry + /// + /// + /// + /// + protected void WriteEntry(string filePath, byte[] data, char fileType) + { + byte[] header = new byte[512]; + + // file path field (100) + byte[] nameBytes = m_asciiEncoding.GetBytes(filePath); + int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length; + Array.Copy(nameBytes, header, nameSize); + + // file mode (8) + byte[] modeBytes = m_asciiEncoding.GetBytes("0000777"); + Array.Copy(modeBytes, 0, header, 100, 7); + + // owner user id (8) + byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764"); + Array.Copy(ownerIdBytes, 0, header, 108, 7); + + // group user id (8) + byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764"); + Array.Copy(groupIdBytes, 0, header, 116, 7); + + // file size in bytes (12) + int fileSize = data.Length; + //m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize); + + byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11); + + Array.Copy(fileSizeBytes, 0, header, 124, 11); + + // last modification time (12) + byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332"); + Array.Copy(lastModTimeBytes, 0, header, 136, 11); + + // entry type indicator (1) + header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0]; + + Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7); + Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7); + + // check sum for header block (8) [calculated last] + Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8); + + int checksum = 0; + foreach (byte b in header) + { + checksum += b; + } + + //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum); + + byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6); + + Array.Copy(checkSumBytes, 0, header, 148, 6); + + header[154] = 0; + + // Write out header + m_bw.Write(header); + + // Write out data + m_bw.Write(data); + + if (data.Length % 512 != 0) + { + int paddingRequired = 512 - (data.Length % 512); + + //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired); + + byte[] padding = new byte[paddingRequired]; + m_bw.Write(padding); + } + } + } +} diff --git a/Programs/Simian/Extensions/Periscope.cs b/Programs/Simian/Extensions/Periscope.cs index 78a55ff8..63dcb378 100644 --- a/Programs/Simian/Extensions/Periscope.cs +++ b/Programs/Simian/Extensions/Periscope.cs @@ -9,6 +9,9 @@ namespace Simian.Extensions { public class Periscope : IExtension { + // Change this to login to a different grid + const string PERISCOPE_LOGIN_URI = Settings.AGNI_LOGIN_SERVER; + public Agent MasterAgent = null; Simian server; @@ -295,6 +298,20 @@ namespace Simian.Extensions server.Avatars.SendAlert(agent, String.Format("Downloading textures: {0}, Queued textures: {1}", imageDelivery.Pipeline.CurrentCount, imageDelivery.Pipeline.QueuedCount)); return; + case "/save": + if (messageParts.Length == 2) + { + string filename = messageParts[1]; + string directoryname = System.IO.Path.GetFileNameWithoutExtension(filename); + + Logger.Log(String.Format("Preparing to serialize {0} objects", server.Scene.ObjectCount()), Helpers.LogLevel.Info); + OarFile.SavePrims(server, directoryname + "/objects"); + Logger.Log("Saving " + directoryname, Helpers.LogLevel.Info); + OarFile.PackageArchive(directoryname, filename); + System.IO.Directory.Delete(directoryname, true); + Logger.Log("Done", Helpers.LogLevel.Info); + } + return; case "/nudemod": //int count = 0; // FIXME: AvatarAppearance locks the agents dictionary. Need to be able to copy the agents dictionary? @@ -356,6 +373,7 @@ namespace Simian.Extensions LoginParams login = client.Network.DefaultLoginParams(agent.FirstName, agent.LastName, agent.PasswordHash, "Simian Periscope", "1.0.0"); + login.URI = PERISCOPE_LOGIN_URI; login.Start = "last"; client.Network.Login(login);