private void AbortTerrainXferHandler(IClientAPI remoteClient, ulong XferID)
        {
            if (TerrainUploader != null)
            {
                lock (TerrainUploader)
                {
                    if (XferID == TerrainUploader.XferID)
                    {
                        remoteClient.OnXferReceive -= TerrainUploader.XferReceive;
                        remoteClient.OnAbortXfer -= AbortTerrainXferHandler;
                        TerrainUploader.TerrainUploadDone -= HandleTerrainApplication;

                        TerrainUploader = null;
                        remoteClient.SendAlertMessage("Terrain Upload aborted by the client");
                    }
                }
            }
        }
        private void HandleTerrainApplication(string filename, byte[] terrainData, IClientAPI remoteClient)
        {
            lock (TerrainUploader)
            {
                remoteClient.OnXferReceive -= TerrainUploader.XferReceive;
                remoteClient.OnAbortXfer -= AbortTerrainXferHandler;
                TerrainUploader.TerrainUploadDone -= HandleTerrainApplication;

                TerrainUploader = null;
            }
            remoteClient.SendAlertMessage("Terrain Upload Complete. Loading....");
            ITerrainModule terr = m_scene.RequestModuleInterface<ITerrainModule>();

            if (terr != null)
            {
                MainConsole.Instance.Warn("[CLIENT]: Got Request to Send Terrain in region " +
                                          m_scene.RegionInfo.RegionName);

                try
                {
                    FileInfo x = new FileInfo(filename);

                    if (x.Extension == ".oar") // It's an oar file
                    {
                        bool check = false;
                        while (!check)
                        {
                            if (File.Exists(filename))
                            {
                                filename = "duplicate" + filename;
                            }
                            else
                                check = true;
                        }
                        FileStream input = new FileStream(filename, FileMode.CreateNew);
                        input.Write(terrainData, 0, terrainData.Length);
                        input.Close();
                        MainConsole.Instance.RunCommand("load oar " + filename);
                        remoteClient.SendAlertMessage("Your oar file was loaded. It may take a few moments to appear.");
                    }
                    else
                    {
                        MemoryStream terrainStream = new MemoryStream(terrainData);
                        terr.LoadFromStream(filename, terrainStream);
                        terrainStream.Close();
                        remoteClient.SendAlertMessage("Your terrain was loaded as a ." + x.Extension +
                                                      " file. It may take a few moments to appear.");
                    }
                }
                catch (IOException e)
                {
                    MainConsole.Instance.ErrorFormat(
                        "[TERRAIN]: Error Saving a terrain file uploaded via the estate tools.  It gave us the following error: {0}",
                        e);
                    remoteClient.SendAlertMessage(
                        "There was an IO Exception loading your terrain.  Please check free space.");

                    return;
                }
                catch (SecurityException e)
                {
                    MainConsole.Instance.ErrorFormat(
                        "[TERRAIN]: Error Saving a terrain file uploaded via the estate tools.  It gave us the following error: {0}",
                        e);
                    remoteClient.SendAlertMessage(
                        "There was a security Exception loading your terrain.  Please check the security on the simulator drive");

                    return;
                }
                catch (UnauthorizedAccessException e)
                {
                    MainConsole.Instance.ErrorFormat(
                        "[TERRAIN]: Error Saving a terrain file uploaded via the estate tools.  It gave us the following error: {0}",
                        e);
                    remoteClient.SendAlertMessage(
                        "There was a security Exception loading your terrain.  Please check the security on the simulator drive");

                    return;
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat(
                        "[TERRAIN]: Error loading a terrain file uploaded via the estate tools.  It gave us the following error: {0}",
                        e);
                    remoteClient.SendAlertMessage(
                        "There was a general error loading your terrain.  Please fix the terrain file and try again");
                }
            }
            else
            {
                remoteClient.SendAlertMessage("Unable to apply terrain.  Cannot get an instance of the terrain module");
            }
        }
 private void handleUploadTerrain(IClientAPI remote_client, string clientFileName)
 {
     if (TerrainUploader == null)
     {
         remote_client.SendAlertMessage("Uploading terrain file...");
         TerrainUploader = new EstateTerrainXferHandler(remote_client, clientFileName);
         lock (TerrainUploader)
         {
             remote_client.OnXferReceive += TerrainUploader.XferReceive;
             remote_client.OnAbortXfer += AbortTerrainXferHandler;
             TerrainUploader.TerrainUploadDone += HandleTerrainApplication;
         }
         TerrainUploader.RequestStartXfer(remote_client);
     }
     else
     {
         remote_client.SendAlertMessage("Another Terrain Upload is in progress.  Please wait your turn!");
     }
 }
        void HandleTerrainApplication (string filename, byte[] terrainData, IClientAPI remoteClient)
        {
            lock (_lock)
            {
                remoteClient.OnXferReceive -= TerrainUploader.XferReceive;
                remoteClient.OnAbortXfer -= AbortTerrainXferHandler;
                TerrainUploader.TerrainUploadDone -= HandleTerrainApplication;

                TerrainUploader = null;
            }

            remoteClient.SendAlertMessage ("Terrain Upload Complete. Loading....");
            ITerrainModule terr = m_scene.RequestModuleInterface<ITerrainModule> ();

            if (terr != null)
            {
                MainConsole.Instance.Warn ("[Estate Manager]: Got Request to send Terrain in region " +
                m_scene.RegionInfo.RegionName);

                try
                {
                    FileInfo x = new FileInfo (filename);

                    if (x.Extension == ".oar") // It's an oar file
                    {
                        if (File.Exists(filename))
                            filename = DateTime.UtcNow.ToString("yyyMMdd") + filename;

                        // save OAR to a local file
                        FileStream input = new FileStream (filename, FileMode.CreateNew);
                        try {
                            input.Write (terrainData, 0, terrainData.Length);
                        } finally {
                            input.Close ();
                        }

                        // load it to the region
                        MainConsole.Instance.RunCommand ("load oar " + filename);
                        remoteClient.SendAlertMessage ("Your oar file was loaded. It may take a few moments to appear.");

                        return;
                    } 

                    // just terrain data
                    MemoryStream terrainStream = new MemoryStream (terrainData);
                    terr.LoadFromStream (filename, terrainStream);
                    terrainStream.Close ();

                    remoteClient.SendAlertMessage ("Your terrain was loaded as a ." + x.Extension +
                     " file. It may take a few moments to appear.");
                    
                } catch (IOException e)
                {
                    MainConsole.Instance.ErrorFormat (
                        "[Estate Manager]: Error Saving a terrain file uploaded via the estate tools.\n: {0}",
                        e);
                    remoteClient.SendAlertMessage (
                        "There was an IO Exception loading your terrain.  Please check free space.");

                } catch (SecurityException e)
                {
                    MainConsole.Instance.ErrorFormat (
                        "[Estate Manager]: Error Saving a terrain file uploaded via the estate tools.\n: {0}",
                        e);
                    remoteClient.SendAlertMessage (
                        "There was a security Exception loading your terrain.  Please check the security on the simulator drive");

                } catch (UnauthorizedAccessException e)
                {
                    MainConsole.Instance.ErrorFormat (
                        "[Estate Manager]: Error Saving a terrain file uploaded via the estate tools.\n: {0}",
                        e);
                    remoteClient.SendAlertMessage (
                        "There was a security Exception loading your terrain.  Please check the security on the simulator drive");

                } catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat (
                        "[Estate Manager]: Error loading a terrain file uploaded via the estate tools.\n: {0}",
                        e);
                    remoteClient.SendAlertMessage (
                        "There was a general error loading your terrain.  Please fix the terrain file and try again");
                    
                }
                return;
            }
            remoteClient.SendAlertMessage ("Unable to apply terrain.  Cannot get an instance of the terrain module");

        }