Advertisement
700hours

rndport

Oct 7th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Diagnostics;
  6. using System.IO;
  7. namespace randomport
  8. {
  9.     class Core
  10.     {
  11.         public static int[] usedPorts = new int[] { };
  12.         public static int
  13.             minval = 8001,
  14.             maxval = 8129,
  15.             chosenPort = 0,
  16.             timeout = 10,
  17.             temp = 1024;
  18.         public static bool read = false;
  19.         public static void Main(string[] args)
  20.         {
  21.             string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
  22.             if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
  23.             {
  24.                 Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
  25.                 //  using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"))) { }
  26.             }
  27.             if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
  28.             {
  29.                 Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
  30.                 using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd"))) { }
  31.             }
  32.             if (args.Length > 0)
  33.             {
  34.                 if (args[0] == "-noread")
  35.                 {
  36.                 }
  37.                 else if (args[0] == "-read" || args[0] == "-default")
  38.                 {
  39.                     if (!read)
  40.                     {
  41.                         Read();
  42.                         read = true;
  43.                     }
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 if (!read)
  49.                 {
  50.                     Read();
  51.                     read = true;
  52.                 }
  53.             }
  54.             if (args.Length >= 3)
  55.             {
  56.                 if (args[1] != "-default" || args[1] != "0")
  57.                 {
  58.                     int.TryParse(args[1], out temp);
  59.                     if (temp > 1024 && temp < 65535)
  60.                     {
  61.                         minval = temp;
  62.                     }
  63.                 }
  64.                 if (args[2] != "-default" || args[2] != "0")
  65.                 {
  66.                     int.TryParse(args[2], out temp);
  67.                     if (temp > 1024 && temp < 65535)
  68.                     {
  69.                         maxval = temp;
  70.                     }
  71.                 }
  72.             }
  73.             RandGen();
  74.         }
  75.         public static void RandGen()
  76.         {
  77.             string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
  78.             Random rand = new Random();
  79.             chosenPort = rand.Next(minval, maxval);
  80.             for (int i = 0; i < usedPorts.Length; i++)
  81.             {
  82.                 if (chosenPort != usedPorts[i])
  83.                 {
  84.                     Write();
  85.                 }
  86.                 else return;
  87.             }
  88.         }
  89.         public static void Read()
  90.         {
  91.             string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
  92.             if (!File.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
  93.             {
  94.                 File.Create(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"));
  95.             }
  96.             using (StreamReader sr = new StreamReader(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
  97.             {
  98.                 string[] values = sr.ReadToEnd().Split(';');
  99.                 usedPorts = new int[values.Length];//Initialize the array with the same length as values
  100.                 for (int i = 0; i < values.Length; i++)
  101.                 {
  102.                     int.TryParse(values[i], out usedPorts[i]);
  103.                 }
  104.             }
  105.         }
  106.         public static void Write()
  107.         {
  108.             string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
  109.             File.AppendAllLines(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"), new string[] { chosenPort + ";" });
  110.             using (StreamWriter sw2 = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
  111.             {
  112.                 sw2.WriteLine("set port=" + chosenPort);
  113.             }
  114.             Environment.Exit(0);
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement