Ejemplo n.º 1
0
        async Task Cancel(bool async, CancellationToken cancellationToken = default)
        {
            _state = ImporterState.Cancelled;
            _buf.Clear();
            _buf.EndCopyMode();
            await _connector.WriteCopyFail(async, cancellationToken);

            await _connector.Flush(async, cancellationToken);

            try
            {
                var msg = await _connector.ReadMessage(async, cancellationToken);

                // The CopyFail should immediately trigger an exception from the read above.
                throw _connector.Break(
                          new NpgsqlException("Expected ErrorResponse when cancelling COPY but got: " + msg.Code));
            }
            catch (PostgresException e)
            {
                if (e.SqlState != PostgresErrorCodes.QueryCanceled)
                {
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Completes the import operation. The writer is unusable after this operation.
        /// </summary>
        public void Complete()
        {
            CheckReady();

            if (InMiddleOfRow)
            {
                Cancel();
                throw new InvalidOperationException("Binary importer closed in the middle of a row, cancelling import.");
            }

            try
            {
                WriteTrailer();
                _buf.Flush();
                _buf.EndCopyMode();

                _connector.SendMessage(CopyDoneMessage.Instance);
                Expect <CommandCompleteMessage>(_connector.ReadMessage());
                Expect <ReadyForQueryMessage>(_connector.ReadMessage());
                _state = ImporterState.Committed;
            }
            catch
            {
                // An exception here will have already broken the connection etc.
                Cleanup();
                throw;
            }
        }
Ejemplo n.º 3
0
    async ValueTask <ulong> Complete(bool async, CancellationToken cancellationToken = default)
    {
        CheckReady();

        using var registration = _connector.StartNestedCancellableOperation(cancellationToken, attemptPgCancellation: false);

        if (InMiddleOfRow)
        {
            await Cancel(async, cancellationToken);

            throw new InvalidOperationException("Binary importer closed in the middle of a row, cancelling import.");
        }

        try
        {
            await WriteTrailer(async, cancellationToken);

            await _buf.Flush(async, cancellationToken);

            _buf.EndCopyMode();
            await _connector.WriteCopyDone(async, cancellationToken);

            await _connector.Flush(async, cancellationToken);

            var cmdComplete = Expect <CommandCompleteMessage>(await _connector.ReadMessage(async), _connector);
            Expect <ReadyForQueryMessage>(await _connector.ReadMessage(async), _connector);
            _state = ImporterState.Committed;
            return(cmdComplete.Rows);
        }
        catch
        {
            Cleanup();
            throw;
        }
    }
Ejemplo n.º 4
0
        async ValueTask <ulong> Complete(bool async, CancellationToken cancellationToken = default)
        {
            CheckReady();

            if (InMiddleOfRow)
            {
                await Cancel(async, cancellationToken);

                throw new InvalidOperationException("Binary importer closed in the middle of a row, cancelling import.");
            }

            try
            {
                await WriteTrailer(async, cancellationToken);

                await _buf.Flush(async, cancellationToken);

                _buf.EndCopyMode();
                await _connector.WriteCopyDone(async, cancellationToken);

                await _connector.Flush(async, cancellationToken);

                var cmdComplete = Expect <CommandCompleteMessage>(await _connector.ReadMessage(async, cancellationToken), _connector);
                Expect <ReadyForQueryMessage>(await _connector.ReadMessage(async, cancellationToken), _connector);
                _state = ImporterState.Committed;
                return(cmdComplete.Rows);
            }
            catch
            {
                // An exception here will have already broken the connection etc.
                Cleanup();
                throw;
            }
        }
Ejemplo n.º 5
0
 void Cleanup()
 {
     Log.Debug("COPY operation ended", _connector.Id);
     _connector.CurrentCopyOperation = null;
     _connector = null;
     _buf       = null;
     _state     = ImporterState.Disposed;
 }
Ejemplo n.º 6
0
        void Cleanup()
        {
            var connector = _connector;

            Log.Debug("COPY operation ended", connector?.Id ?? -1);

            if (connector != null)
            {
                connector.CurrentCopyOperation = null;
                _connector = null;
            }

            _buf   = null;
            _state = ImporterState.Disposed;
        }
Ejemplo n.º 7
0
#pragma warning disable CS8625
    void Cleanup()
    {
        if (_state == ImporterState.Disposed)
        {
            return;
        }
        var connector = _connector;

        LogMessages.BinaryCopyOperationCompleted(Logger, _rowsImported, connector?.Id ?? -1);

        if (connector != null)
        {
            connector.CurrentCopyOperation = null;
            _connector.Connection?.EndBindingScope(ConnectorBindingScope.Copy);
            _connector = null;
        }

        _buf   = null;
        _state = ImporterState.Disposed;
    }
Ejemplo n.º 8
0
#pragma warning disable CS8625
        void Cleanup()
        {
            if (_state == ImporterState.Disposed)
            {
                return;
            }
            var connector = _connector;

            Log.Debug("COPY operation ended", connector?.Id ?? -1);

            if (connector != null)
            {
                connector.CurrentCopyOperation = null;
                _connector.Connection?.EndBindingScope(ConnectorBindingScope.Copy);
                _connector = null;
            }

            _buf   = null;
            _state = ImporterState.Disposed;
        }
Ejemplo n.º 9
0
 void Cancel()
 {
     _state = ImporterState.Cancelled;
     _buf.Clear();
     _buf.EndCopyMode();
     _connector.SendMessage(new CopyFailMessage());
     try
     {
         var msg = _connector.ReadMessage();
         // The CopyFail should immediately trigger an exception from the read above.
         _connector.Break();
         throw new NpgsqlException("Expected ErrorResponse when cancelling COPY but got: " + msg.Code);
     }
     catch (PostgresException e)
     {
         if (e.SqlState != "57014")
         {
             throw;
         }
     }
 }
Ejemplo n.º 10
0
        public void ImportFromFile(string filename)
        {
            ImportDirectory = Path.GetDirectoryName(filename);
            string xml;
            List<Primitive> prims;

            xml = File.ReadAllText(filename);

            prims = Helpers.OSDToPrimList(OSDParser.DeserializeLLSDXml(xml));

            Dictionary<uint, Linkset> linksets = new Dictionary<uint, Linkset>();
            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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>();

            foreach (Linkset linkset in linksets.Values)
            {
                if (linkset.RootPrim.LocalID != 0)
                {
                    state = ImporterState.RezzingParent;
                    currentPrim = linkset.RootPrim;
                    linkset.RootPrim.Position = RezAt;
                    currentPosition = RezAt;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, UUID.Zero,
                        linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(10000, false))
                        throw new Exception("Rez failed, timed out while creating the root prim.");

                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, linkset.RootPrim.Position);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, UUID.Zero, currentPosition,
                                               prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(10000, false))
                            throw new Exception("Rez failed, timed out while creating child prim.");
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
                    }

                    // 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.

                    if (linkset.Children.Count != 0)
                    {
                        // Add the rest of the prims to the list of local IDs)
                        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);

                        if (primDone.WaitOne(1000 * linkset.Children.Count, false))
                            Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        else
                            LogMessage("Warning: Failed to link {0} prims",linkQueue.Count);
                    }
                    else
                    {
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                    }

                    // Set permissions on newly created prims
                    Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                        PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
                        PermissionMask.All, true);

                    state = ImporterState.Idle;
                }
                else
                    LogMessage("WARNING: Skipping a linkset with a missing root prim");

                // Reset everything for the next linkset
                primsCreated.Clear();
            }
        }
Ejemplo n.º 11
0
        public override string Execute(string[] args, LLUUID fromAgentID)
        {
            if (args.Length != 1)
                return "Usage: import inputfile.xml";

            string filename = args[0];
            Dictionary<uint, Primitive> prims = new Dictionary<uint, Primitive>(); ;

            currentClient = Client;

            return "This command is currently under construction";

            try
            {
                //XmlReader reader = XmlReader.Create(filename);
                //List<Primitive> listprims = Helpers.PrimListFromXml(reader);
                //reader.Close();

                // Create a dictionary indexed by the old local ID of the prims
                //foreach (Primitive prim in listprims)
                //{
                //    prims.Add(prim.LocalID, prim);
                //}
            }
            catch (Exception)
            {
                return "Failed to import the object XML file, maybe it doesn't exist or is in the wrong format?";
            }

            if (!registeredCreateEvent)
            {
                Client.Objects.OnNewPrim += new ObjectManager.NewPrimCallback(Objects_OnNewPrim);
                registeredCreateEvent = true;
            }

            // Build an organized structure from the imported prims
            Dictionary<uint, Linkset> linksets = new Dictionary<uint, Linkset>();
            foreach (Primitive prim in prims.Values)
            {
                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;
                    // HACK: Offset the root prim position so it's not lying on top of the original
                    // We need a more elaborate solution for importing with relative or absolute offsets
                    linkset.RootPrim.Position = Client.Self.SimPosition;
                    linkset.RootPrim.Position.Z += 3.0f;
                    currentPosition = linkset.RootPrim.Position;
                    // A better solution would move the bot to the desired position.
                    // or to check if we are within a certain distance of the desired position.

                    // Rez the root prim with no rotation
                    LLQuaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = LLQuaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.Data, LLUUID.Zero,
                        linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(10000, false))
                        return "Rez failed, timed out while creating the root prim.";

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.Data, LLUUID.Zero, currentPosition,
                            prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(10000, false))
                            return "Rez failed, timed out while creating child prim.";
                    }

                    if (linkset.Children.Count != 0)
                    {
                        // 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.
                        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);
                        if (primDone.WaitOne(100000 * linkset.Children.Count, false))
                        {
                            Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                                PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
                                PermissionMask.All, true);

                            Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        }
                        else
                        {
                            Console.WriteLine("Warning: Failed to link {0} prims", linkQueue.Count);
                        }
                    }
                    else
                    {
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                    }
                    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.";
        }
Ejemplo n.º 12
0
        public override string Execute(string[] args, UUID fromAgentID)
        {
            if (args.Length < 1)
                return "Usage: import inputfile.xml [usegroup]";

            string filename = args[0];
            UUID GroupID = (args.Length > 1) ? Client.GroupID : UUID.Zero;
            string xml;
            List<Primitive> prims;

            try { xml = File.ReadAllText(filename); }
            catch (Exception e) { return e.Message; }

            try { prims = Helpers.LLSDToPrimList(LLSDParser.DeserializeXml(xml)); }
            catch (Exception e) { return "Failed to deserialize " + filename + ": " + e.Message; }

            // Build an organized structure from the imported prims
            Dictionary<uint, Linkset> linksets = new Dictionary<uint, Linkset>();
            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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;
                    // HACK: Import the structure just above our head
                    // We need a more elaborate solution for importing with relative or absolute offsets
                    linkset.RootPrim.Position = Client.Self.SimPosition;
                    linkset.RootPrim.Position.Z += 3.0f;
                    currentPosition = linkset.RootPrim.Position;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, GroupID,
                        linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(10000, false))
                        return "Rez failed, timed out while creating the root prim.";

                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, linkset.RootPrim.Position);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, GroupID, currentPosition,
                            prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(10000, false))
                            return "Rez failed, timed out while creating child prim.";
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);

                    }

                    // 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.

                    if (linkset.Children.Count != 0)
                    {
                        // Add the rest of the prims to the list of local IDs
                        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);

                        if (primDone.WaitOne(1000 * linkset.Children.Count, false))
                            Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        else
                            Console.WriteLine("Warning: Failed to link {0} prims", linkQueue.Count);

                    }
                    else
                    {
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                    }

                    // Set permissions on newly created prims
                    Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                        PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
                        PermissionMask.All, true);

                    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.";
        }
Ejemplo n.º 13
0
        public bool CreateObject(List <Primitive> prims)
        {
            // Build an organized structure from the imported prims
            Dictionary <uint, Linkset> linksets = new Dictionary <uint, Linkset>();

            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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;
                    // HACK: Import the structure just above our head
                    // We need a more elaborate solution for importing with relative or absolute offsets
                    linkset.RootPrim.Position    = Client.Self.SimPosition;
                    linkset.RootPrim.Position.Z += 3.0f;
                    currentPosition              = linkset.RootPrim.Position;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, Client.Self.ActiveGroup,
                                           linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(25000, false))
                    {
                        throw new Exception("Rez failed, timed out while creating the root prim.");
                    }
                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim     = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, UUID.Zero, currentPosition,
                                               prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(25000, false))
                        {
                            throw new Exception("Rez failed, timed out while creating child prim.");
                        }
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
                        // Client.Objects.SetRotation(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, prim.Rotation);
                    }

                    if (linkset.Children.Count != 0)
                    {
                        // 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.
                        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);
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);

                        if (!primDone.WaitOne(5000, false))
                        {
                            Logger.Log(String.Format("Warning: Failed to link {0} prims", linkQueue.Count), Helpers.LogLevel.Warning);
                        }

                        Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                                                      PermissionWho.NextOwner,
                                                      PermissionMask.All, true);
                    }
                    else
                    {
                        List <uint> primsForPerms = new List <uint>();
                        primsForPerms.Add(rootLocalID);
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        Client.Objects.SetPermissions(Client.Network.CurrentSim, primsForPerms,
                                                      PermissionWho.NextOwner,
                                                      PermissionMask.All, true);
                    }
                    state = ImporterState.Idle;
                }
                else
                {
                    // Skip linksets with a missing root prim
                    Logger.Log("WARNING: Skipping a linkset with a missing root prim", Helpers.LogLevel.Warning);
                }

                // Reset everything for the next linkset
                primsCreated.Clear();
            }

            return(true);
        }
Ejemplo n.º 14
0
        void Update()
        {
            switch (importerState)
            {
            case ImporterState.Ready:
                if (taskData.Count > 0)
                {
                    // Assimp loading
                    ImportTaskData d = taskData[0];
                    currentTask   = Task.Run(async() => await ImportAssimpFile(d.fileName));
                    importerState = ImporterState.Initialized;
                    progress      = 0f;
                }
                break;

            case ImporterState.Initialized:
                if (currentTask.IsCompleted)
                {
                    // Convert assimp structures into unity
                    if (!currentTask.IsFaulted)
                    {
                        var scene = currentTask.Result;
                        if (scene == null)
                        {
                            importerState = ImporterState.Error;
                            break;
                        }
                        ImportTaskData d = taskData[0];
                        StartCoroutine(CreateUnityDataFromAssimp(d.fileName, scene, d.root.transform));
                        importerState = ImporterState.Processing;
                        progress      = 0.5f;
                    }
                    else
                    {
                        importerState = ImporterState.Error;
                    }
                }
                break;

            case ImporterState.Error:
            {
                var tdata = taskData[0];
                taskData.RemoveAt(0);
                currentTask = null;
                Clear();
                importerState = ImporterState.Ready;
                ImportTaskEventArgs args = new ImportTaskEventArgs(null, tdata.fileName, true);
                progress = 1f;
                importEventTask.Invoke(this, args);
            }
            break;

            case ImporterState.Processing:
                if (unityDataInCoroutineCreated)
                {
                    // task done
                    var tdata = taskData[0];
                    taskData.RemoveAt(0);
                    currentTask = null;
                    unityDataInCoroutineCreated = false;
                    Clear();
                    importerState = ImporterState.Ready;

                    Transform           root = tdata.root.transform.GetChild(tdata.root.transform.childCount - 1);
                    ImportTaskEventArgs args = new ImportTaskEventArgs(root, tdata.fileName, false);
                    progress = 1f;
                    importEventTask.Invoke(this, args);
                }
                break;
            }
        }
Ejemplo n.º 15
0
        public override string Execute(string[] args, UUID fromAgentID)
        {
            if (args.Length < 1)
            {
                return("Usage: import inputfile.xml [usegroup]");
            }

            string           filename = args[0];
            UUID             GroupID  = (args.Length > 1) ? Client.GroupID : UUID.Zero;
            string           xml;
            List <Primitive> prims;

            try { xml = File.ReadAllText(filename); }
            catch (Exception e) { return(e.Message); }

            try { prims = Helpers.LLSDToPrimList(LLSDParser.DeserializeXml(xml)); }
            catch (Exception e) { return("Failed to deserialize " + filename + ": " + e.Message); }

            // Build an organized structure from the imported prims
            Dictionary <uint, Linkset> linksets = new Dictionary <uint, Linkset>();

            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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;
                    // HACK: Import the structure just above our head
                    // We need a more elaborate solution for importing with relative or absolute offsets
                    linkset.RootPrim.Position    = Client.Self.SimPosition;
                    linkset.RootPrim.Position.Z += 3.0f;
                    currentPosition              = linkset.RootPrim.Position;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, GroupID,
                                           linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(10000, false))
                    {
                        return("Rez failed, timed out while creating the root prim.");
                    }

                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, linkset.RootPrim.Position);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim     = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, GroupID, currentPosition,
                                               prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(10000, false))
                        {
                            return("Rez failed, timed out while creating child prim.");
                        }
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
                    }

                    // 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.

                    if (linkset.Children.Count != 0)
                    {
                        // Add the rest of the prims to the list of local IDs
                        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);

                        if (primDone.WaitOne(1000 * linkset.Children.Count, false))
                        {
                            Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        }
                        else
                        {
                            Console.WriteLine("Warning: Failed to link {0} prims", linkQueue.Count);
                        }
                    }
                    else
                    {
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                    }

                    // Set permissions on newly created prims
                    Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                                                  PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
                                                  PermissionMask.All, true);

                    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.");
        }
Ejemplo n.º 16
0
        public void ImportFromFile(string filename)
        {
            ImportDirectory = Path.GetDirectoryName(filename);
            string           xml;
            List <Primitive> prims;

            xml = File.ReadAllText(filename);

            prims = Helpers.OSDToPrimList(OSDParser.DeserializeLLSDXml(xml));

            Dictionary <uint, Linkset> linksets = new Dictionary <uint, Linkset>();

            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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>();

            foreach (Linkset linkset in linksets.Values)
            {
                if (linkset.RootPrim.LocalID != 0)
                {
                    state       = ImporterState.RezzingParent;
                    currentPrim = linkset.RootPrim;
                    linkset.RootPrim.Position = RezAt;
                    currentPosition           = RezAt;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, UUID.Zero,
                                           linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(10000, false))
                    {
                        throw new Exception("Rez failed, timed out while creating the root prim.");
                    }

                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, linkset.RootPrim.Position);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim     = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, UUID.Zero, currentPosition,
                                               prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(10000, false))
                        {
                            throw new Exception("Rez failed, timed out while creating child prim.");
                        }
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
                    }

                    // 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.

                    if (linkset.Children.Count != 0)
                    {
                        // Add the rest of the prims to the list of local IDs)
                        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);

                        if (primDone.WaitOne(1000 * linkset.Children.Count, false))
                        {
                            Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        }
                        else
                        {
                            LogMessage("Warning: Failed to link {0} prims", linkQueue.Count);
                        }
                    }
                    else
                    {
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                    }

                    // Set permissions on newly created prims
                    Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                                                  PermissionWho.Everyone | PermissionWho.Group | PermissionWho.NextOwner,
                                                  PermissionMask.All, true);

                    state = ImporterState.Idle;
                }
                else
                {
                    LogMessage("WARNING: Skipping a linkset with a missing root prim");
                }

                // Reset everything for the next linkset
                primsCreated.Clear();
            }
        }
Ejemplo n.º 17
0
        public bool CreateObject(List<Primitive> prims)
        {
            // Build an organized structure from the imported prims
            Dictionary<uint, Linkset> linksets = new Dictionary<uint, Linkset>();
            for (int i = 0; i < prims.Count; i++)
            {
                Primitive prim = prims[i];

                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;
                    // HACK: Import the structure just above our head
                    // We need a more elaborate solution for importing with relative or absolute offsets
                    linkset.RootPrim.Position = Client.Self.SimPosition;
                    linkset.RootPrim.Position.Z += 3.0f;
                    currentPosition = linkset.RootPrim.Position;

                    // Rez the root prim with no rotation
                    Quaternion rootRotation = linkset.RootPrim.Rotation;
                    linkset.RootPrim.Rotation = Quaternion.Identity;

                    Client.Objects.AddPrim(Client.Network.CurrentSim, linkset.RootPrim.PrimData, Client.Self.ActiveGroup,
                        linkset.RootPrim.Position, linkset.RootPrim.Scale, linkset.RootPrim.Rotation);

                    if (!primDone.WaitOne(25000, false))
                    {
                        throw new Exception("Rez failed, timed out while creating the root prim.");
                    }
                    Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);

                    state = ImporterState.RezzingChildren;

                    // Rez the child prims
                    foreach (Primitive prim in linkset.Children)
                    {
                        currentPrim = prim;
                        currentPosition = prim.Position + linkset.RootPrim.Position;

                        Client.Objects.AddPrim(Client.Network.CurrentSim, prim.PrimData, UUID.Zero, currentPosition,
                            prim.Scale, prim.Rotation);

                        if (!primDone.WaitOne(25000, false))
                        {
                            throw new Exception("Rez failed, timed out while creating child prim.");
                        }
                        Client.Objects.SetPosition(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, currentPosition);
                        // Client.Objects.SetRotation(Client.Network.CurrentSim, primsCreated[primsCreated.Count - 1].LocalID, prim.Rotation);
                    }

                    if (linkset.Children.Count != 0)
                    {
                        // 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.
                        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);
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);

                        if (!primDone.WaitOne(5000, false))
                        {
                            Logger.Log(String.Format("Warning: Failed to link {0} prims", linkQueue.Count), Helpers.LogLevel.Warning);
                        }

                        Client.Objects.SetPermissions(Client.Network.CurrentSim, primIDs,
                            PermissionWho.NextOwner,
                            PermissionMask.All, true);
                    }
                    else
                    {
                        List<uint> primsForPerms = new List<uint>();
                        primsForPerms.Add(rootLocalID);
                        Client.Objects.SetRotation(Client.Network.CurrentSim, rootLocalID, rootRotation);
                        Client.Objects.SetPermissions(Client.Network.CurrentSim, primsForPerms,
                            PermissionWho.NextOwner,
                            PermissionMask.All, true);
                    }
                    state = ImporterState.Idle;
                }
                else
                {
                    // Skip linksets with a missing root prim
                    Logger.Log("WARNING: Skipping a linkset with a missing root prim", Helpers.LogLevel.Warning);
                }

                // Reset everything for the next linkset
                primsCreated.Clear();
            }

            return true;
        }