<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Aurora.Framework;
using OpenSim.Framework;
using OpenSim.Region.Framework.Scenes;
using OpenMetaverse;
using OpenSim;
using log4net;
using Nini.Config;
using Nini.Ini;

namespace Aurora.Modules.RegionLoader
{
    public partial class RegionManager : Form
    {
        private static readonly ILog m_log
           = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public delegate void NewRegion(RegionInfo info);
        public event NewRegion OnNewRegion;
        private bool KillAfterRegionCreation = false;
        private UUID CurrentRegionID = UUID.Zero;
        private ISimulationBase m_OpenSimBase;
        private IRegionInfoConnector m_connector = null;
        private bool m_changingRegion = false;
        private bool m_textHasChanged = false;

        public RegionManager(bool killOnCreate, bool openCreatePageFirst, ISimulationBase baseOpenSim)
        {
            m_OpenSimBase = baseOpenSim;
            m_connector = Aurora.DataManager.DataManager.RequestPlugin<IRegionInfoConnector>();
            KillAfterRegionCreation = killOnCreate;
            InitializeComponent();
            if (openCreatePageFirst)
                tabControl1.SelectedTab = tabPage2;
            RefreshCurrentRegions();
        }

        private void RefreshCurrentRegions()
        {
            RegionListBox.Items.Clear();
            RegionInfo[] regionInfos = m_connector.GetRegionInfos(false);
            foreach(RegionInfo r in regionInfos)
            {
                RegionListBox.Items.Add(r.RegionName);
            }
        }

        private void CreateNewRegion(object sender, EventArgs e)
        {
            if (RName.Text == "")
            {
                MessageBox.Show("You must enter a region name!");
                return;
            }
            RegionInfo region = new RegionInfo();
            region.RegionName = RName.Text;
            region.RegionID = UUID.Random();
            region.RegionLocX = int.Parse(LocX.Text) * Constants.RegionSize;
            region.RegionLocY = int.Parse(LocY.Text) * Constants.RegionSize;
            
            IPAddress address = IPAddress.Parse("0.0.0.0");
            int port = port = Convert.ToInt32(Port.Text);
            region.InternalEndPoint = new IPEndPoint(address, port);

            string externalName = ExternalIP.Text;
            if (externalName == "DEFAULT")
            {
                externalName = Aurora.Framework.Utilities.GetExternalIp();
                region.FindExternalAutomatically = true;
            }
            else
                region.FindExternalAutomatically = false;
            region.ExternalHostName = externalName;

            region.RegionType = Type.Text;
            region.ObjectCapacity = int.Parse(ObjectCount.Text);
            int maturityLevel = 0;
            if (!int.TryParse(Maturity.Text, out maturityLevel))
            {
                if (Maturity.Text == "Adult")
                    maturityLevel = 2;
                else if (Maturity.Text == "Mature")
                    maturityLevel = 1;
                else //Leave it as PG by default if they do not select a valid option
                    maturityLevel = 0;
            }
            region.RegionSettings.Maturity = maturityLevel;
            region.Disabled = DisabledEdit.Checked;
            region.RegionSizeX = int.Parse(CRegionSizeX.Text);
            region.RegionSizeY = int.Parse(CRegionSizeY.Text);
            if ((region.RegionSizeX % Constants.MinRegionSize) != 0 ||
                (region.RegionSizeY % Constants.MinRegionSize) != 0)
            {
                MessageBox.Show ("You must enter a valid region size (multiple of " + Constants.MinRegionSize + "!");
                return;
            }
            region.NumberStartup = int.Parse (CStartNum.Text);
            region.Startup = ConvertIntToStartupType(CStartupType.SelectedIndex);

            m_connector.UpdateRegionInfo(region);
            if (KillAfterRegionCreation)
            {
                System.Windows.Forms.Application.Exit();
                return;
            }
            else
            {
                IScene scene;
                m_log.Info("[LOADREGIONS]: Creating Region: " + region.RegionName + ")");
                SceneManager manager = m_OpenSimBase.ApplicationRegistry.RequestModuleInterface<SceneManager>();
                manager.CreateRegion(region, out scene);
            }
            RefreshCurrentRegions();
        }

        private void SearchForRegionByName_Click(object sender, EventArgs e)
        {
            RegionInfo region = m_connector.GetRegionInfo(RegionToFind.Text);
            if (region == null)
            {
                MessageBox.Show("Region was not found!");
                return;
            }
            m_changingRegion = true;
            CurrentRegionID = region.RegionID;
            textBox11.Text = region.RegionType;
            textBox6.Text = region.ObjectCapacity.ToString();
            uint maturityLevel = Util.ConvertAccessLevelToMaturity(region.AccessLevel);
            if (maturityLevel == 0)
                textBox4.Text = "PG";
            else if (maturityLevel == 1)
                textBox4.Text = "Mature";
            else
                textBox4.Text = "Adult";
            DisabledEdit.Checked = region.Disabled;
            if (region.FindExternalAutomatically)
                textBox9.Text = "DEFAULT";
            else
                textBox9.Text = region.ExternalHostName;
            textBox7.Text = region.HttpPort.ToString();
            textBox3.Text = (region.RegionLocX / Constants.RegionSize).ToString();
            textBox5.Text = (region.RegionLocY / Constants.RegionSize).ToString();
            textBox1.Text = region.RegionName;
            RegionSizeX.Text = region.RegionSizeX.ToString ();
            RegionSizeY.Text = region.RegionSizeY.ToString ();
            startupType.SelectedIndex = ConvertStartupType(region.Startup);
            m_changingRegion = false;
        }

        private void Update()
        {
            RegionInfo region = m_connector.GetRegionInfo(textBox1.Text);
            if (region == null)
            {
                MessageBox.Show("You must enter a valid region name!");
                return;
            }
            region.RegionName = textBox1.Text;
            region.RegionID = CurrentRegionID;
            region.RegionLocX = int.Parse(textBox3.Text) * Constants.RegionSize;
            region.RegionLocY = int.Parse(textBox5.Text) * Constants.RegionSize;

            IPAddress address = IPAddress.Parse("0.0.0.0");
            int port = port = Convert.ToInt32(textBox7.Text);
            region.InternalEndPoint = new IPEndPoint(address, port);
            region.HttpPort = (uint)port;

            string externalName = textBox9.Text;
            if (externalName == "DEFAULT")
            {
                externalName = Aurora.Framework.Utilities.GetExternalIp();
                region.FindExternalAutomatically = true;
            }
            else
                region.FindExternalAutomatically = false;
            region.ExternalHostName = externalName;<commit_msg>Remove a warning.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Aurora.Framework;
using OpenSim.Framework;
using OpenSim.Region.Framework.Scenes;
using OpenMetaverse;
using OpenSim;
using log4net;
using Nini.Config;
using Nini.Ini;

namespace Aurora.Modules.RegionLoader
{
    public partial class RegionManager : Form
    {
        private static readonly ILog m_log
           = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public delegate void NewRegion(RegionInfo info);
        public event NewRegion OnNewRegion;
        private bool KillAfterRegionCreation = false;
        private UUID CurrentRegionID = UUID.Zero;
        private ISimulationBase m_OpenSimBase;
        private IRegionInfoConnector m_connector = null;
        private bool m_changingRegion = false;
        private bool m_textHasChanged = false;

        public RegionManager(bool killOnCreate, bool openCreatePageFirst, ISimulationBase baseOpenSim)
        {
            m_OpenSimBase = baseOpenSim;
            m_connector = Aurora.DataManager.DataManager.RequestPlugin<IRegionInfoConnector>();
            KillAfterRegionCreation = killOnCreate;
            InitializeComponent();
            if (openCreatePageFirst)
                tabControl1.SelectedTab = tabPage2;
            RefreshCurrentRegions();
        }

        private void RefreshCurrentRegions()
        {
            RegionListBox.Items.Clear();
            RegionInfo[] regionInfos = m_connector.GetRegionInfos(false);
            foreach(RegionInfo r in regionInfos)
            {
                RegionListBox.Items.Add(r.RegionName);
            }
        }

        private void CreateNewRegion(object sender, EventArgs e)
        {
            if (RName.Text == "")
            {
                MessageBox.Show("You must enter a region name!");
                return;
            }
            RegionInfo region = new RegionInfo();
            region.RegionName = RName.Text;
            region.RegionID = UUID.Random();
            region.RegionLocX = int.Parse(LocX.Text) * Constants.RegionSize;
            region.RegionLocY = int.Parse(LocY.Text) * Constants.RegionSize;
            
            IPAddress address = IPAddress.Parse("0.0.0.0");
            int port = port = Convert.ToInt32(Port.Text);
            region.InternalEndPoint = new IPEndPoint(address, port);

            string externalName = ExternalIP.Text;
            if (externalName == "DEFAULT")
            {
                externalName = Aurora.Framework.Utilities.GetExternalIp();
                region.FindExternalAutomatically = true;
            }
            else
                region.FindExternalAutomatically = false;
            region.ExternalHostName = externalName;

            region.RegionType = Type.Text;
            region.ObjectCapacity = int.Parse(ObjectCount.Text);
            int maturityLevel = 0;
            if (!int.TryParse(Maturity.Text, out maturityLevel))
            {
                if (Maturity.Text == "Adult")
                    maturityLevel = 2;
                else if (Maturity.Text == "Mature")
                    maturityLevel = 1;
                else //Leave it as PG by default if they do not select a valid option
                    maturityLevel = 0;
            }
            region.RegionSettings.Maturity = maturityLevel;
            region.Disabled = DisabledEdit.Checked;
            region.RegionSizeX = int.Parse(CRegionSizeX.Text);
            region.RegionSizeY = int.Parse(CRegionSizeY.Text);
            if ((region.RegionSizeX % Constants.MinRegionSize) != 0 ||
                (region.RegionSizeY % Constants.MinRegionSize) != 0)
            {
                MessageBox.Show ("You must enter a valid region size (multiple of " + Constants.MinRegionSize + "!");
                return;
            }
            region.NumberStartup = int.Parse (CStartNum.Text);
            region.Startup = ConvertIntToStartupType(CStartupType.SelectedIndex);

            m_connector.UpdateRegionInfo(region);
            if (KillAfterRegionCreation)
            {
                System.Windows.Forms.Application.Exit();
                return;
            }
            else
            {
                IScene scene;
                m_log.Info("[LOADREGIONS]: Creating Region: " + region.RegionName + ")");
                SceneManager manager = m_OpenSimBase.ApplicationRegistry.RequestModuleInterface<SceneManager>();
                manager.CreateRegion(region, out scene);
            }
            RefreshCurrentRegions();
        }

        private void SearchForRegionByName_Click(object sender, EventArgs e)
        {
            RegionInfo region = m_connector.GetRegionInfo(RegionToFind.Text);
            if (region == null)
            {
                MessageBox.Show("Region was not found!");
                return;
            }
            m_changingRegion = true;
            CurrentRegionID = region.RegionID;
            textBox11.Text = region.RegionType;
            textBox6.Text = region.ObjectCapacity.ToString();
            uint maturityLevel = Util.ConvertAccessLevelToMaturity(region.AccessLevel);
            if (maturityLevel == 0)
                textBox4.Text = "PG";
            else if (maturityLevel == 1)
                textBox4.Text = "Mature";
            else
                textBox4.Text = "Adult";
            DisabledEdit.Checked = region.Disabled;
            if (region.FindExternalAutomatically)
                textBox9.Text = "DEFAULT";
            else
                textBox9.Text = region.ExternalHostName;
            textBox7.Text = region.HttpPort.ToString();
            textBox3.Text = (region.RegionLocX / Constants.RegionSize).ToString();
            textBox5.Text = (region.RegionLocY / Constants.RegionSize).ToString();
            textBox1.Text = region.RegionName;
            RegionSizeX.Text = region.RegionSizeX.ToString ();
            RegionSizeY.Text = region.RegionSizeY.ToString ();
            startupType.SelectedIndex = ConvertStartupType(region.Startup);
            m_changingRegion = false;
        }

        private new void Update()
        {
            RegionInfo region = m_connector.GetRegionInfo(textBox1.Text);
            if (region == null)
            {
                MessageBox.Show("You must enter a valid region name!");
                return;
            }
            region.RegionName = textBox1.Text;
            region.RegionID = CurrentRegionID;
            region.RegionLocX = int.Parse(textBox3.Text) * Constants.RegionSize;
            region.RegionLocY = int.Parse(textBox5.Text) * Constants.RegionSize;

            IPAddress address = IPAddress.Parse("0.0.0.0");
            int port = port = Convert.ToInt32(textBox7.Text);
            region.InternalEndPoint = new IPEndPoint(address, port);
            region.HttpPort = (uint)port;

            string externalName = textBox9.Text;
            if (externalName == "DEFAULT")
            {
                externalName = Aurora.Framework.Utilities.GetExternalIp();
                region.FindExternalAutomatically = true;
            }
            else
                region.FindExternalAutomatically = false;
            region.ExternalHostName = externalName;
{'fraction_non_alphanumeric': 0.08593960636290106, 'fraction_numerical': 0.005527096252359126, 'mean_word_length': 1.877060306379678, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3316088', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3696, 'n_words': 760}