public void Disconnect() { connectedIP = null; srcRcon = null; mReconnectTries = 0; mIsConnected = false; OnlineStateEvent(); }
//************************************************* // Methods //************************************************* public void Connect() { if (!String.IsNullOrEmpty(Settings.Default.RconIP)) { mIsConnected = false; connectedIP = Settings.Default.RconIP; connectedPort = Settings.Default.RconPort.ToString(); srcRcon = null; srcRcon = new SourceRcon.SourceRcon(); srcRcon.Errors += new SourceRcon.StringOutput(OnError); srcRcon.ServerOutput += new SourceRcon.StringOutput(ConsoleOutput); srcRcon.ConnectionSuccess += new SourceRcon.BoolInfo(ConnectionSuccessInfo); mThreadConnect = new Thread(delegate() { srcRcon.Connect(new IPEndPoint(IPAddress.Parse(Settings.Default.RconIP), Settings.Default.RconPort), Settings.Default.RconPW); }); mThreadConnect.Start(); if (mReconnectTries == 0) { OnlineStateEvent(); } } }
public void StartPug( Guid pugId, Map map ) { if (!Rcon.Connected) { Rcon.Connect( new IPEndPoint( Dns.GetHostAddresses( Address )[0], (int)Port ), RconPassword ); System.Threading.Thread.Sleep( 1000 ); } Rcon = new SourceRcon.SourceRcon(); Rcon.ConnectionSuccess += new SourceRcon.BoolInfo( Rcon_ConnectionSuccess ); Rcon.ServerOutput += new SourceRcon.StringOutput( Rcon_ServerOutput ); Rcon.Connect( new IPEndPoint( Dns.GetHostAddresses( Address )[0], (int)Port ), RconPassword ); System.Threading.Thread.Sleep( 1000 ); Rcon.ServerCommand( String.Format( "sm_pug_id {0}", pugId ) ); Rcon.ServerCommand( String.Format( "changelevel {0}", map.Name ) ); //Rcon.Disconnect(); Rcon = null; }
public void RefreshStatus() { PlayerCount = -1; Rcon = new SourceRcon.SourceRcon(); Rcon.ConnectionSuccess += new SourceRcon.BoolInfo( Rcon_ConnectionSuccess ); Rcon.ServerOutput += new SourceRcon.StringOutput( Rcon_ServerOutput ); Rcon.Connect( new IPEndPoint( Dns.GetHostAddresses( Address )[0], (int)Port ), RconPassword ); System.Threading.Thread.Sleep( 1000 ); //Rcon.Disconnect(); //Rcon = null; return; if (!Rcon.Connected) Rcon.Connect( new IPEndPoint( Dns.GetHostAddresses( Address )[0], (int)Port ), RconPassword ); else { Rcon.ServerCommand( @"status" ); } }
public static void Main(string[] args) { string title = "Sourcemod Compiler Helper ver " + (FileVersionInfo.GetVersionInfo((Assembly.GetExecutingAssembly()).Location)).ProductVersion; Console.Title = title; Console_ResetColor(); Console.Clear(); //Console.BackgroundColor = ConsoleColor.Blue; //Console.ForegroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = FGcolorH1; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine(title); Console.WriteLine("----------------------------------------------------------------"); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(@"Free contact me over GitHub https://github.com/k64t34/SourceModPawnCompilerPluginHelper/issues" + "\n"); Console.ForegroundColor = ConsoleColor.White; #region Parsing argumets if (args.Length < 1) { Console.WriteLine("Usage: SMcompiler.exe <path\\file.sp>"); ScriptFinish(true); System.Environment.Exit(0); } //Console.Write(" "); Console.WriteLine(args[0]); Console_ResetColor(); mySMcomp_Folder = AppDomain.CurrentDomain.BaseDirectory; ini_Compilator_Folder = mySMcomp_Folder; // or //Application.ExecutablePath; //Assembly.GetExecutingAssembly().Location; //Application.StartupPath; ConsoleWriteField("SMcompiler folder", mySMcomp_Folder); Debug.Print("Args count=" + args.Length); Console.Title = title + @" " + args[0] + @" " + DateTime.Now.ToString(); SourceFile = args[0]; if (!File.Exists(SourceFile)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERR: File \"" + SourceFile + "\" not found"); Console_ResetColor(); ScriptFinish(true); System.Environment.Exit(1); } SourceFolder = System.IO.Directory.GetParent(SourceFile).ToString() + "\\"; EndFolderBackslash(ref SourceFolder); SourceFile = Path.GetFileNameWithoutExtension(SourceFile); Console.Title = title + " " + SourceFile + ".sp " + DateTime.Now.ToString(); Console.ForegroundColor = FGcolorFieldName; Console.Write("Source file \t"); Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Black; Console.Write(SourceFolder); Console.BackgroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine(SourceFile + ".sp"); Console_ResetColor(); #endregion PluginFolder = System.IO.Directory.GetParent(SourceFolder).ToString(); if (String.Compare(Path.GetFileName(PluginFolder), "SCRIPTING", true) == 0) { PluginFolder = System.IO.Directory.GetParent(PluginFolder).ToString(); PluginFolder = System.IO.Directory.GetParent(PluginFolder).ToString(); PluginFolder = System.IO.Directory.GetParent(PluginFolder).ToString(); PluginFolder = System.IO.Directory.GetParent(PluginFolder).ToString(); } EndFolderBackslash(ref PluginFolder); ConsoleWriteField("Plugin Folder", PluginFolder); GetConfigFile(mySMcomp_Folder + INIFile); GetConfigFile(PluginFolder + INIFile); ConsoleWriteField("Compilator_Folder", ini_Compilator_Folder); SRCDS_Folder = ini_SRCDS_Folder; EndFolderBackslash(ref SRCDS_Folder); SRCDS_Folder = @"\\" + ini_Hostname + @"\" + ini_Share + @"\" + SRCDS_Folder; if (String.IsNullOrEmpty(ini_rcon_Address)) { ini_rcon_Address = ini_Hostname; } #region Create include file datetime.inc string curDate = DateTime.Now.ToString("dd.MM.yy HH:mm:ss"); System.IO.StreamWriter f_inc = new System.IO.StreamWriter(SourceFolder + "datetimecomp.inc", false); f_inc.WriteLine("#if defined DEBUG"); f_inc.WriteLine("\t#define PLUGIN_DATETIME \"" + curDate + "\""); f_inc.WriteLine("\t#if defined PLUGIN_VERSION"); f_inc.WriteLine("\t\t#undef PLUGIN_VERSION"); f_inc.WriteLine("\t#endif"); f_inc.WriteLine("\t#define PLUGIN_VERSION \"" + curDate + "\""); f_inc.WriteLine("#endif"); f_inc.WriteLine("#if !defined PLUGIN_NAME"); f_inc.WriteLine("\t#define PLUGIN_NAME \"" + SourceFile + "\""); f_inc.WriteLine("#endif"); f_inc.WriteLine("#if !defined PLUGIN_AUTHOR"); f_inc.WriteLine("\t#define PLUGIN_AUTHOR \"" + ini_Plugin_Author + "\""); f_inc.WriteLine("#endif"); f_inc.Close(); #endregion #region Delete old err smx files if (!File.Exists(SourceFile + ".err")) { File.Delete(SourceFile + ".err"); } #endregion #region Test Pawn compiler file spcomp.exe exist if (!File.Exists(ini_Compilator_Folder + Compilator)) { Console.ForegroundColor = ConsoleColor.Red; if (Directory.Exists(ini_Compilator_Folder)) { Console.WriteLine("ERR: File spcomp.exe Pawn compiler {0} not found in folder {1}", Compilator, ini_Compilator_Folder); } else { Console.WriteLine("ERR: Folder {0} with file spcomp.exe Pawn compiler {1} not found.", ini_Compilator_Folder, Compilator); } Console_ResetColor(); ScriptFinish(true); System.Environment.Exit(4); } #endregion #region Prepare folder PLUGINS (game\addons\sourcemod\plugins) for compiled files , delete old smx files if (Directory.Exists(PluginFolder + SMXFolder)) { if (File.Exists(PluginFolder + SMXFolder + SourceFile + ".smx")) { File.Delete(PluginFolder + SMXFolder + SourceFile + ".smx"); } } else { System.IO.Directory.CreateDirectory(PluginFolder + SMXFolder); } #endregion #region Compiling Console.ForegroundColor = FGcolorH1; Console.Write("Run compiling "); Console.ForegroundColor = FGcolorFieldValue; Process compiler = new Process(); compiler.StartInfo.RedirectStandardOutput = true; compiler.StartInfo.RedirectStandardError = true; compiler.StartInfo.CreateNoWindow = true; compiler.StartInfo.WorkingDirectory = PluginFolder; //compiler.StartInfo.WorkingDirectory = SourceFolder; compiler.StartInfo.FileName = ini_Compilator_Folder + Compilator; Console.WriteLine(compiler.StartInfo.FileName); compiler.StartInfo.UseShellExecute = false; //https://msdn.microsoft.com/ru-ru/library/system.diagnostics.processstartinfo.workingdirectory(v=vs.110).aspx string DiffSourceFolder = FolderDifference(SourceFolder, PluginFolder); string buffArg; buffArg = " " + DiffSourceFolder + SourceFile + ".sp"; Console.WriteLine(buffArg); compiler.StartInfo.Arguments += buffArg; buffArg = " -D\"" + TrimEndBackslash(PluginFolder) + "\""; Console.WriteLine(buffArg); compiler.StartInfo.Arguments += buffArg; buffArg = " -e" + DiffSourceFolder + SourceFile + ".err"; Console.WriteLine(buffArg); compiler.StartInfo.Arguments += buffArg; buffArg = " -o" + SMXFolder + SourceFile + ".smx"; Console.WriteLine(buffArg); compiler.StartInfo.Arguments += buffArg; if (ini_Compilator_Params.Length != 0) { Console.WriteLine(" {0}", ini_Compilator_Params); compiler.StartInfo.Arguments += " " + ini_Compilator_Params; } #region Parsing include from INI file //Compilator_Include_FoldersArray[Compilator_Include_FoldersArray.Length - 1] = FolderDifference(SourceFolder, PluginFolder); Compilator_Include_FoldersList = " -i" + SourceFolder; foreach (string s in Compilator_Include_FoldersArray) { String p = "\"" + FolderDifference(s, PluginFolder) + "\""; Console.WriteLine(" -i " + p); Compilator_Include_FoldersList += " -i" + p; } #endregion compiler.StartInfo.Arguments += Compilator_Include_FoldersList; //Console.WriteLine(compiler.StartInfo.Arguments); compiler.StartInfo.UseShellExecute = false; compiler.StartInfo.RedirectStandardOutput = true; ConsoleWriteField("WorkingDirectory", compiler.StartInfo.WorkingDirectory); #region Set console windows size IntPtr ConsoleHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; const UInt32 WINDOW_FLAGS = SWP_SHOWWINDOW; var wndRect = new RECT(); GetWindowRect(ConsoleHandle, out wndRect); // Получили размеры текущего она консоли var cWidth = wndRect.Right - wndRect.Left; var cHeight = wndRect.Bottom - wndRect.Top; //Rectangle waRectangle; //int ScreenHeight = SystemInformation.PrimaryMonitorSize.Height; int ScreenHeight = SystemInformation.WorkingArea.Height; SetWindowPos(ConsoleHandle, HWND_NOTOPMOST, 0, 0, cWidth, cHeight, WINDOW_FLAGS); SetWindowPos(ConsoleHandle, HWND_NOTOPMOST, 0, 0, cWidth, ScreenHeight, WINDOW_FLAGS); #endregion #region Run Compiler try { compiler.Start(); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console_ResetColor(); } string output = compiler.StandardOutput.ReadToEnd(); string err = compiler.StandardError.ReadToEnd(); if (output.Length != 0) { Console.WriteLine(output); } if (err.Length != 0) { Console.WriteLine(err); } compiler.WaitForExit(); //Delete datetime.inc if (File.Exists(SourceFolder + "datetimecomp.inc")) { File.Delete(SourceFolder + "datetimecomp.inc"); } //ERRORLEVEL ConsoleColor ERRORLEVEL_color; if (compiler.ExitCode > 0) { ERRORLEVEL_color = ConsoleColor.Red; } else { if (File.Exists(SourceFolder + SourceFile + ".err")) { ERRORLEVEL_color = ConsoleColor.Yellow; } else { ERRORLEVEL_color = ConsoleColor.Green; } } Console.ForegroundColor = ERRORLEVEL_color; Console.WriteLine("ERRORLEVEL=" + compiler.ExitCode); Console_ResetColor(); if (File.Exists(SourceFolder + SourceFile + ".err")) { Console.ForegroundColor = ERRORLEVEL_color; if (compiler.ExitCode > 0) { Console.WriteLine("ERR: " + SourceFolder + SourceFile + ".err\n--------------------------------------------------------"); } else { Console.WriteLine("WARN: " + SourceFolder + SourceFile + ".err\n--------------------------------------------------------"); } Console_ResetColor(); try { using (StreamReader sr = new StreamReader(SourceFolder + SourceFile + ".err")) { String line = sr.ReadToEnd(); Console.WriteLine(line); } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The file could not be read " + SourceFolder + SourceFile + ".err"); Console.WriteLine(e.Message); Console_ResetColor(); } } if (compiler.ExitCode > 0) { ScriptFinish(true); System.Environment.Exit(0); } #endregion #endregion #region Copy to server #region Check Hostname & SRCDS_Folder #endregion Console.ForegroundColor = FGcolorH1; Console.Write("Copy output files "); Console.ForegroundColor = FGcolorFieldValue; Console.Write(SourceFile + ".smx"); Console.ForegroundColor = FGcolorH1; Console.Write(" from folder "); Console.ForegroundColor = FGcolorFieldValue; Console.Write(PluginFolder + "game"); Console.ForegroundColor = FGcolorH1; Console.Write(" to "); Console.ForegroundColor = FGcolorFieldValue; Console.WriteLine(SRCDS_Folder); #region NET USE if (isSMBPath(SRCDS_Folder)) { if (!Directory.Exists(SRCDS_Folder)) { try { compiler.StartInfo.FileName = Path.Combine(Environment.GetEnvironmentVariable("windir") + @"\system32", "NET.exe"); compiler.StartInfo.Arguments = @"USE \\" + ini_Hostname + @"\" + ini_Share + " /USER:"******" " + ini_Share_Password; Console.WriteLine(compiler.StartInfo.FileName + " " + compiler.StartInfo.Arguments); compiler.Start(); output = compiler.StandardOutput.ReadToEnd(); err = compiler.StandardError.ReadToEnd(); if (output.Length != 0) { Console.WriteLine(output); } if (err.Length != 0) { Console.WriteLine(err); } compiler.WaitForExit(); //} } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console_ResetColor(); } //NetworkCredential theNetworkCredential = new NetworkCredential("dod","12345678"); //CredentialCache theNetcache = new CredentialCache(); //theNetcache.Add(@"\\192.168.56.102\tmp", 445, "Basic", theNetworkCredential); //then do whatever, such as getting a list of folders: //string[] theFolders = System.IO.Directory.GetDirectories(@"\\192.168.56.102\tmp"); } } #endregion if (!Directory.Exists(SRCDS_Folder)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERR:Folder for copy smx file " + SRCDS_Folder + "not found"); Console_ResetColor(); ScriptFinish(true); System.Environment.Exit(0); } CopyDirectory(PluginFolder + "game", SRCDS_Folder); #endregion #region Reload plugin Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\nReload plugin {0} on server {1}:{2}\n", SourceFile, ini_rcon_Address, ini_rcon_Port); Console_ResetColor(); //make you class https://developer.valvesoftware.com/wiki/Source_RCON_Protocol SourceRcon.SourceRcon RCon = new SourceRcon.SourceRcon(); RCon.Errors += new SourceRcon.StringOutput(ErrorOutput); RCon.ServerOutput += new SourceRcon.StringOutput(ConsoleOutput); try { IPAddress address; if (!IPAddress.TryParse(ini_rcon_Address, out address)) { IPHostEntry host = Dns.GetHostEntry(ini_rcon_Address); ini_rcon_Address = host.AddressList[0].ToString(); Console.WriteLine("Host {0} have IP address {1}", ini_Hostname, ini_rcon_Address); Console_ResetColor(); } Console.Write("Connect to {0}:{1}", ini_rcon_Address, ini_rcon_Port); RCon.Connect(new IPEndPoint(IPAddress.Parse(ini_rcon_Address), ini_rcon_Port), ini_rcon_password); for (int i = 0; i != 10; i++) { Thread.Sleep(1000); if (RCon.Connected) { break; } Console.Write("."); } Console.WriteLine(" OK"); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } //if (RCon.Connect(new IPEndPoint(IPAddress.Parse(rcon_Address), rcon_Port), rcon_password)) if (RCon.Connected) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Connected"); Console_ResetColor(); RCon.ServerCommand("status"); Thread.Sleep(100); if (ini_RestartServer) { Console.WriteLine("Restart server"); RCon.ServerCommand("_restart"); Thread.Sleep(5000); } else if (ini_RestartMap) { Console.WriteLine("Restart map"); RCon.ServerCommand("sm_map "); Thread.Sleep(5000); } else { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Reload plugin"); Console_ResetColor(); RCon.ServerCommand("sm plugins unload " + SourceFile); Thread.Sleep(1000); RCon.ServerCommand("sm plugins load " + SourceFile); Thread.Sleep(1000); } Thread.Sleep(1000); RCon.ServerCommand("sm plugins info " + SourceFile); Thread.Sleep(1000); /*Console.Write("Press Esc key to exit . . . "); * ConsoleKeyInfo k=Console.ReadKey(); * while (k.Key!= ConsoleKey.Escape) * { * k=Console.ReadKey(); * RCon.ServerCommand(Console.ReadLine()); * Console.ReadLine * }*/ /*while(true) * { * RCon.ServerCommand(Console.ReadLine()); * } */ } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERR: No connection."); Console_ResetColor(); } RCon = null; //Thread.Sleep(10000); #endregion ScriptFinish(true); System.Environment.Exit(0); }