2007-04-28 20:54:02 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.IO;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse;
|
|
|
|
|
using OpenMetaverse.StructuredData;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-07-08 04:35:04 +00:00
|
|
|
public class ImportCommand : Command
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-07-08 04:35:04 +00:00
|
|
|
private enum ImporterState
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-07-08 04:35:04 +00:00
|
|
|
RezzingParent,
|
|
|
|
|
RezzingChildren,
|
|
|
|
|
Linking,
|
|
|
|
|
Idle
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-08 04:35:04 +00:00
|
|
|
private class Linkset
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-07-08 04:35:04 +00:00
|
|
|
public Primitive RootPrim;
|
|
|
|
|
public List<Primitive> Children = new List<Primitive>();
|
|
|
|
|
|
|
|
|
|
public Linkset()
|
|
|
|
|
{
|
|
|
|
|
RootPrim = new Primitive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Linkset(Primitive rootPrim)
|
|
|
|
|
{
|
|
|
|
|
RootPrim = rootPrim;
|
|
|
|
|
}
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Primitive currentPrim;
|
2008-07-25 05:15:05 +00:00
|
|
|
Vector3 currentPosition;
|
2007-11-30 13:15:31 +00:00
|
|
|
AutoResetEvent primDone = new AutoResetEvent(false);
|
2007-04-28 20:54:02 +00:00
|
|
|
List<Primitive> primsCreated;
|
|
|
|
|
List<uint> linkQueue;
|
2007-11-30 13:15:31 +00:00
|
|
|
uint rootLocalID;
|
2007-04-28 20:54:02 +00:00
|
|
|
ImporterState state = ImporterState.Idle;
|
|
|
|
|
|
|
|
|
|
public ImportCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "import";
|
2007-12-20 19:20:33 +00:00
|
|
|
Description = "Import prims from an exported xml file. Usage: import inputfile.xml [usegroup]";
|
2008-07-25 08:55:36 +00:00
|
|
|
Category = CommandCategory.Objects;
|
2007-11-30 13:15:31 +00:00
|
|
|
|
|
|
|
|
testClient.Objects.OnNewPrim += new ObjectManager.NewPrimCallback(Objects_OnNewPrim);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
public override string Execute(string[] args, UUID fromAgentID)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-12-20 19:20:33 +00:00
|
|
|
if (args.Length < 1)
|
|
|
|
|
return "Usage: import inputfile.xml [usegroup]";
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
string filename = args[0];
|
2008-07-25 05:15:05 +00:00
|
|
|
UUID GroupID = (args.Length > 1) ? Client.GroupID : UUID.Zero;
|
2007-11-30 13:15:31 +00:00
|
|
|
string xml;
|
|
|
|
|
List<Primitive> prims;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2007-11-30 13:15:31 +00:00
|
|
|
try { xml = File.ReadAllText(filename); }
|
|
|
|
|
catch (Exception e) { return e.Message; }
|
2007-11-06 09:26:10 +00:00
|
|
|
|
2009-10-07 03:47:02 +00:00
|
|
|
try { prims = Helpers.OSDToPrimList(OSDParser.DeserializeLLSDXml(xml)); }
|
2007-11-30 13:15:31 +00:00
|
|
|
catch (Exception e) { return "Failed to deserialize " + filename + ": " + e.Message; }
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
// Build an organized structure from the imported prims
|
|
|
|
|
Dictionary<uint, Linkset> linksets = new Dictionary<uint, Linkset>();
|
2007-11-30 13:15:31 +00:00
|
|
|
for (int i = 0; i < prims.Count; i++)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-11-30 13:15:31 +00:00
|
|
|
Primitive prim = prims[i];
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
if (prim.ParentID == 0)
|
|
|
|
|
{
|
|
|
|
|
if (linksets.ContainsKey(prim.LocalID))
|
|
|
|
|
linksets[prim.LocalID].RootPrim = prim;
|
|
|
|
|
else
|
|
|
|
|
linksets[prim.LocalID] = new Linkset(prim);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!linksets.ContainsKey(prim.ParentID))
|
|
|
|
|
linksets[prim.ParentID] = new Linkset();
|
|
|
|
|
|
|
|
|
|
linksets[prim.ParentID].Children.Add(prim);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primsCreated = new List<Primitive>();
|
|
|
|
|
Console.WriteLine("Importing " + linksets.Count + " structures.");
|
|
|
|
|
|
|
|
|
|
foreach (Linkset linkset in linksets.Values)
|
|
|
|
|
{
|
|
|
|
|
if (linkset.RootPrim.LocalID != 0)
|
|
|
|
|
{
|
|
|
|
|
state = ImporterState.RezzingParent;
|
|
|
|
|
currentPrim = linkset.RootPrim;
|
2007-11-30 13:15:31 +00:00
|
|
|
// HACK: Import the structure just above our head
|
2007-04-28 20:54:02 +00:00
|
|
|
// We need a more elaborate solution for importing with relative or absolute offsets
|
2007-11-06 09:26:10 +00:00
|
|
|
linkset.RootPrim.Position = Client.Self.SimPosition;
|
2007-04-28 20:54:02 +00:00
|
|
|
linkset.RootPrim.Position.Z += 3.0f;
|
|
|
|
|
currentPosition = linkset.RootPrim.Position;
|
|
|
|
|
|
|
|
|
|
// Rez the root prim with no rotation
|
2008-07-25 05:15:05 +00:00
|
|
|
Quaternion rootRotation = linkset.RootPrim.Rotation;
|
|
|
|
|
linkset.RootPrim.Rotation = Quaternion.Identity;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2008-08-24 05:06:51 +00:00
|
|
|
Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, GroupID,
|
2007-04-28 20:54:02 +00:00
|
|
|
linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);
|
|
|
|
|
|
|
|
|
|
if (!primDone.WaitOne(10000, false))
|
|
|
|
|
return "Rez failed, timed out while creating the root prim.";
|
|
|
|
|
|
2008-04-07 23:06:38 +00:00
|
|
|
Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, linkset.RootPrim.Position);
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
state = ImporterState.RezzingChildren;
|
|
|
|
|
|
|
|
|
|
// Rez the child prims
|
|
|
|
|
foreach (Primitive prim in linkset.Children)
|
|
|
|
|
{
|
|
|
|
|
currentPrim = prim;
|
|
|
|
|
currentPosition = prim.Position + linkset.RootPrim.Position;
|
|
|
|
|
|
2008-08-24 05:06:51 +00:00
|
|
|
Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, GroupID, currentPosition,
|
2007-04-28 20:54:02 +00:00
|
|
|
prim.Scale, prim.Rotation);
|
|
|
|
|
|
|
|
|
|
if (!primDone.WaitOne(10000, false))
|
|
|
|
|
return "Rez failed, timed out while creating child prim.";
|
2008-04-07 23:06:38 +00:00
|
|
|
Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
2008-04-09 15:12:11 +00:00
|
|
|
// Create a list of the local IDs of the newly created prims
|
|
|
|
|
List<uint> primIDs = new List<uint>(primsCreated.Count);
|
|
|
|
|
primIDs.Add(rootLocalID); // Root prim is first in list.
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
if (linkset.Children.Count != 0)
|
|
|
|
|
{
|
2008-04-09 15:12:11 +00:00
|
|
|
// Add the rest of the prims to the list of local IDs
|
2007-04-28 20:54:02 +00:00
|
|
|
foreach (Primitive prim in primsCreated)
|
|
|
|
|
{
|
|
|
|
|
if (prim.LocalID != rootLocalID)
|
|
|
|
|
primIDs.Add(prim.LocalID);
|
|
|
|
|
}
|
|
|
|
|
linkQueue = new List<uint>(primIDs.Count);
|
|
|
|
|
linkQueue.AddRange(primIDs);
|
|
|
|
|
|
|
|
|
|
// Link and set the permissions + rotation
|
|
|
|
|
state = ImporterState.Linking;
|
|
|
|
|
Client.Objects.LinkPrims(Client.Network.CurrentSim, linkQueue);
|
|
|
|
|
|
2007-11-30 13:15:31 +00:00
|
|
|
if (primDone.WaitOne(1000 * linkset.Children.Count, false))
|
2007-04-28 20:54:02 +00:00
|
|
|
Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine("Warning: Failed to link {0} prims", linkQueue.Count);
|
2007-11-30 13:15:31 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
|
|
|
|
|
}
|
2008-04-09 15:12:11 +00:00
|
|
|
|
|
|
|
|
// Set permissions on newly created prims
|
|
|
|
|
Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
|
|
|
|
|
PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
|
|
|
|
|
PermissionMask.All, true);
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
state = ImporterState.Idle;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Skip linksets with a missing root prim
|
|
|
|
|
Console.WriteLine("WARNING: Skipping a linkset with a missing root prim");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset everything for the next linkset
|
|
|
|
|
primsCreated.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "Import complete.";
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 09:26:21 +00:00
|
|
|
void Objects_OnNewPrim(Simulator simulator, Primitive prim, ulong regionHandle, ushort timeDilation)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-08-24 05:06:51 +00:00
|
|
|
if ((prim.Flags & PrimFlags.CreateSelected) == 0)
|
2007-04-28 20:54:02 +00:00
|
|
|
return; // We received an update for an object we didn't create
|
|
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ImporterState.RezzingParent:
|
|
|
|
|
rootLocalID = prim.LocalID;
|
|
|
|
|
goto case ImporterState.RezzingChildren;
|
|
|
|
|
case ImporterState.RezzingChildren:
|
|
|
|
|
if (!primsCreated.Contains(prim))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Setting properties for " + prim.LocalID);
|
|
|
|
|
// TODO: Is there a way to set all of this at once, and update more ObjectProperties stuff?
|
2007-11-30 03:55:08 +00:00
|
|
|
Client.Objects.SetPosition(simulator, prim.LocalID, currentPosition);
|
|
|
|
|
Client.Objects.SetTextures(simulator, prim.LocalID, currentPrim.Textures);
|
2008-03-22 02:08:22 +00:00
|
|
|
|
|
|
|
|
if (currentPrim.Light.Intensity > 0) {
|
|
|
|
|
Client.Objects.SetLight(simulator, prim.LocalID, currentPrim.Light);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Client.Objects.SetFlexible(simulator, prim.LocalID, currentPrim.Flexible);
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
if (currentPrim.Sculpt.SculptTexture != UUID.Zero) {
|
2008-03-22 02:08:22 +00:00
|
|
|
Client.Objects.SetSculpt(simulator, prim.LocalID, currentPrim.Sculpt);
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
if (!String.IsNullOrEmpty(currentPrim.Properties.Name))
|
2007-11-30 03:55:08 +00:00
|
|
|
Client.Objects.SetName(simulator, prim.LocalID, currentPrim.Properties.Name);
|
2007-04-28 20:54:02 +00:00
|
|
|
if (!String.IsNullOrEmpty(currentPrim.Properties.Description))
|
2007-11-30 03:55:08 +00:00
|
|
|
Client.Objects.SetDescription(simulator, prim.LocalID, currentPrim.Properties.Description);
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
primsCreated.Add(prim);
|
|
|
|
|
primDone.Set();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ImporterState.Linking:
|
|
|
|
|
lock (linkQueue)
|
|
|
|
|
{
|
|
|
|
|
int index = linkQueue.IndexOf(prim.LocalID);
|
|
|
|
|
if (index != -1)
|
|
|
|
|
{
|
|
|
|
|
linkQueue.RemoveAt(index);
|
|
|
|
|
if (linkQueue.Count == 0)
|
|
|
|
|
primDone.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|