Пример #1
0
 public void Start()
 {
     running             = true;
     _serverListenThread = new Thread(this.serverLoop);
     _serverListenThread.Start();
     UtilClass.WriteLine("Web Server Started");
 }
Пример #2
0
 public void Stop()
 {
     UtilClass.WriteLine("Web Server Stopping");
     _listener.Stop();
     running = false;
     Thread.Sleep(50);
     _serverListenThread.Abort();
 }
Пример #3
0
        private void Process(HttpListenerContext context)
        {
            string requestedFile = context.Request.Url.AbsolutePath.Substring(1);

            if (string.IsNullOrEmpty(requestedFile))
            {
                //Index files time
                foreach (string fname in defaultFiles)
                {
                    if (File.Exists(Path.Combine(_directory, fname)))
                    {
                        requestedFile = fname;
                        break;
                    }
                }
            }

            requestedFile = Path.Combine(_directory, requestedFile);

            if (File.Exists(requestedFile))
            {
                try
                {
                    using (Stream filedata = new FileStream(requestedFile, FileMode.Open))
                    {
                        string mimetype;
                        MIME_TYPES.TryGetValue(Path.GetExtension(requestedFile), out mimetype);
                        if (string.IsNullOrEmpty(mimetype))
                        {
                            mimetype = HTML_RESPONSE_CONTENT_TYPE;                                                        //Default
                        }
                        context.Response.StatusCode      = (int)HttpStatusCode.OK;
                        context.Response.ContentLength64 = filedata.Length;
                        context.Response.ContentType     = mimetype;

                        byte[] buffer = new byte[1024 * 8];
                        int    byteCount;
                        while ((byteCount = filedata.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            context.Response.OutputStream.Write(buffer, 0, byteCount);
                        }

                        context.Response.OutputStream.Flush();
                    }
                }

                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    UtilClass.WriteLine(ex);
                }
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }
            context.Response.OutputStream.Close();
        }
Пример #4
0
 public void Start()
 {
     UtilClass.WriteLine("Loading...");
     foreach (ModConfiguration current in ModManager.mModConfigurations.Mods)
     {
         if (current.Id.Contains(UtilClass.modId) || current.Name == UtilClass.modName)
         {
             workingDir = current.Path;
             Settings.Initialise();
             break;
         }
     }
     UtilClass.WriteLine("Loaded!");
 }
Пример #5
0
 public void serverLoop()
 {
     _listener.Start();
     while (running)
     {
         try
         {
             HttpListenerContext context = _listener.GetContext();
             Process(context);
         }
         catch (Exception ex)
         {
             UtilClass.WriteLine(ex);
         }
     }
 }
Пример #6
0
 public void Load()
 {
     if (File.Exists(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + settingsFileName))
     {
         try
         {
             using (TextReader reader = File.OpenText(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + settingsFileName))
             {
                 bool hasLine = true;
                 try
                 {
                     while (hasLine)
                     {
                         string line = reader.ReadLine();
                         if (line == null)
                         {
                             hasLine = false;
                             break;
                         }
                         parseSettingsLine(line);
                     }
                     loaded = true;
                 }
                 catch (Exception ex)
                 {
                     UtilClass.WriteLine("Settings: " + ex.Message);
                     hasLine = false;
                     reader.Close();
                 }
                 reader.Close();
             }
         }
         catch (Exception ex)
         {
             UtilClass.WriteLine(String.Format("Load(): {0}", ex.ToString()));
         }
     }
     UtilClass.WriteLine("Settings Loaded!");
 }
Пример #7
0
        public static void GenerateHTML(string templateName, string fileName)
        {
            string template = "";

            try
            {
                template = File.ReadAllText(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + templateName);
            }
            catch (IOException ioex)
            {
                UtilClass.WriteLine(templateName + " not found, downloading default...");

                using (var client = new WebClient())
                {
                    client.DownloadFile(TemplateURL, plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + templateName);
                }
                template = File.ReadAllText(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + templateName);
            }
            catch (Exception ex)
            {
                //Log others
                UtilClass.WriteLine(ex);
            }

            string AttackState = "";

            if (MobSpawnManager.mbAttackUnderway)
            {
                AttackState = "Under Attack!";
            }

            int seconds      = (int)GameManager.mrTotalServerTime;
            int totalSeconds = (int)WorldScript.instance.mWorldData.mrWorldTimePlayed;

            string serverUptime  = string.Format("{0}d, {1}h, {2}m, {3}s", seconds / (3600 * 24), (seconds / 3600) % 24, (seconds / 60) % 60, seconds % 60);
            string worldPlayTime = string.Format("{0}d, {1}h, {2}m, {3}s", totalSeconds / (3600 * 24), (totalSeconds / 3600) % 24, (totalSeconds / 60) % 60, totalSeconds % 60);

            string newPage = template.Replace("$ServerName", NetworkManager.instance.mServerThread.mServerName)

                             .Replace("$WorldName", WorldScript.instance.mWorldData.mName)
                             .Replace("$PlayerCount", GameManager.mnCurrentTotalPlayers.ToString())

                             .Replace("$Uptime", serverUptime)
                             .Replace("$PlayTime", worldPlayTime)

                             .Replace("$PowerPerSec", (GameManager.mrTotalPowerGenerated / GameManager.mrTotalTimeSimulated).ToString("F2"))
                             .Replace("$TotalPowerPyro", prettyfloat(GameManager.mrTotalPyroPower, "F2"))
                             .Replace("$TotalPowerSolar", prettyfloat(GameManager.mrTotalSolarPower, "F2"))
                             .Replace("$TotalPowerJet", prettyfloat(GameManager.mrTotalJetPower, "F2"))
                             .Replace("$TotalPower", prettyfloat(GameManager.mrTotalPowerGenerated))


                             .Replace("$CoalBurned", prettyfloat(GameManager.mnCoalBurned))
                             .Replace("$OresMin", GameManager.mnOresLastMin + " ores/min")
                             .Replace("$BarsMin", GameManager.mnBarsLastMin + " bars/min")
                             .Replace("$TotalOre", GameManager.mnTotalOre.ToString())
                             .Replace("$TotalBars", GameManager.mnTotalBars.ToString())


                             .Replace("$AttackState", AttackState)
                             .Replace("$Threat", ((int)(MobSpawnManager.mrSmoothedBaseThreat * 100)).ToString())

                             .Replace("$Waves", ((int)MobSpawnManager.TotalWavesSeen).ToString())
                             .Replace("$Losses", ((int)MobSpawnManager.TotalWavesLosses).ToString())
                             .Replace("$Kills", ((int)MobSpawnManager.TotalKills).ToString());



            string path = Settings.Instance.settings.StatsSavePath.Replace("$ModFolder$", plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar);

            if (path.LastIndexOf("\\") != (path.Length - 1))
            {
                path = path + Path.DirectorySeparatorChar;
            }

            using (TextWriter writer = File.CreateText(path + fileName))
                try
                {
                    {
                        writer.Write(newPage);
                    }
                    writer.Flush();
                    writer.Close();
                }
                catch (Exception ex)
                {
                    UtilClass.WriteLine(ex.Message);
                    writer.Flush();
                    writer.Close();
                }
        }
Пример #8
0
        public void Save()
        {
            if (loaded == false)
            {
                instance.settings.ServerName     = NetworkManager.instance.mServerThread.mServerName;
                instance.settings.MaxPlayerCount = NetworkManager.instance.mServerThread.mnMaxPlayerCount;

                loaded = true;
            }

            File.Delete(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + settingsFileName);

            using (TextWriter writer = File.CreateText(plugin_pacas00_server.workingDir + Path.DirectorySeparatorChar + settingsFileName))
                try
                {
                    {
                        writer.WriteLine("");
                        writer.WriteLine("# -------------------");
                        writer.WriteLine("# - Server Settings -");
                        writer.WriteLine("# -------------------");
                        writer.WriteLine("");
                        writer.WriteLine("#ServerName - Server Name to show in Server Browser");
                        writer.WriteLine("ServerName" + "=" + settings.ServerName);
                        writer.WriteLine("");
                        writer.WriteLine("#MaxPlayerCount - Defaults to 64 ");
                        writer.WriteLine("MaxPlayerCount" + "=" + settings.MaxPlayerCount);
                        writer.WriteLine("");
                        writer.WriteLine("#Stats - 0 is off, 1 is on. Generate a stats html page. ");
                        writer.WriteLine("# For use with an external webserver or the Built-in HTTP miniserver.");
                        writer.WriteLine("statsEnabled" + "=" + settings.statsEnabled);
                        writer.WriteLine("");
                        writer.WriteLine("#statsMode  0 stats, 1 banner, 2 both");
                        writer.WriteLine("# Generate Stats page, Banner Page or both");
                        writer.WriteLine("statsMode" + "=" + settings.statsMode);
                        writer.WriteLine("");
                        writer.WriteLine("#StatsSavePath - Path to save the stats page in. Also used by the Built-in HTTP miniserver.");
                        writer.WriteLine("StatsSavePath" + "=" + settings.StatsSavePath);
                        writer.WriteLine("");
                        writer.WriteLine("#StatsSaveFileName - file name to save the stats as ");
                        writer.WriteLine("StatsSaveFileName" + "=" + settings.StatsSaveFileName);
                        writer.WriteLine("");
                        writer.WriteLine("#BannerSaveFileName - file name to save the banner as. Uses StatSavePath. ");
                        writer.WriteLine("BannerSaveFileName" + "=" + settings.BannerSaveFileName);
                        writer.WriteLine("");
                        writer.WriteLine("");
                        writer.WriteLine("");
                        writer.WriteLine("#HTTPServerEnabled - 0 is off, 1 is on.");
                        writer.WriteLine("# Enable or Disable the Built-in HTTP miniserver to host the stats page.");
                        writer.WriteLine("# Hosts files from the StatsSavePath.");
                        writer.WriteLine("HTTPServerEnabled" + "=" + settings.HTTPServerEnabled);
                        writer.WriteLine("");
                        writer.WriteLine("#HTTPServerPort - Port to host miniserver on. Defaults to 8081 ");
                        writer.WriteLine("HTTPServerPort" + "=" + settings.HTTPServerPort);
                        writer.WriteLine("");
                    }
                    writer.Flush();
                    writer.Close();
                }
                catch (Exception ex)
                {
                    UtilClass.WriteLine(String.Format("Save(): {0}", ex.ToString()));
                    writer.Flush();
                    writer.Close();
                }
            UtilClass.WriteLine("Settings Saved!");
        }