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!");
     }
 }
        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");
            }
        }