Jump to content

[C#]: Server Manager


Recommended Posts

I was making a C# server manager but am not able to redirect output from the console.

  
if(this.mtaServer != null && this.mtaServer.HasExited == false) { 
    this.mtaServer.Kill(); 
    this.mtaServer = null; 
} 
/// Navigate to MTA Server.exe 
this.mtaServer = new Process(); 
this.mtaServer.StartInfo.FileName = "C:\\Program Files (x86)\\Rockstar Games\\MTA San Andreas 1.5\\server\\MTA Server.exe"; 
/// Set up to run in the background. 
this.mtaServer.StartInfo.UseShellExecute = false; 
/// this.mtaServer.StartInfo.RedirectStandardOutput = true; 
/// this.mtaServer.StartInfo.RedirectStandardError = true; 
this.mtaServer.StartInfo.CreateNoWindow = true; 
this.mtaServer.ErrorDataReceived += HandleProcessOutput; 
this.mtaServer.OutputDataReceived += HandleProcessOutput; 
this.mtaServer.EnableRaisingEvents = true; 
/// Execute. 
this.mtaServer.Start(); 
/// this.mtaServer.BeginOutputReadLine(); 
/// this.mtaServer.BeginErrorReadLine(); 
  

^ This will work, because the output redirecting is disabled.

How can I retrieve server console messages using C#?

What I have:

  
if(this.mtaServer != null && this.mtaServer.HasExited == false) { 
    this.mtaServer.Kill(); 
    this.mtaServer = null; 
} 
/// Navigate to MTA Server.exe 
this.mtaServer = new Process(); 
this.mtaServer.StartInfo.FileName = "C:\\Program Files (x86)\\Rockstar Games\\MTA San Andreas 1.5\\server\\MTA Server.exe"; 
/// Set up to run in the background. 
this.mtaServer.StartInfo.UseShellExecute = false; 
this.mtaServer.StartInfo.RedirectStandardOutput = true; 
this.mtaServer.StartInfo.RedirectStandardError = true; 
this.mtaServer.StartInfo.CreateNoWindow = true; 
this.mtaServer.ErrorDataReceived += HandleProcessOutput; 
this.mtaServer.OutputDataReceived += HandleProcessOutput; 
this.mtaServer.EnableRaisingEvents = true; 
/// Execute. 
this.mtaServer.Start(); 
this.mtaServer.BeginOutputReadLine(); 
this.mtaServer.BeginErrorReadLine(); 
  
// Function. 
    private void HandleProcessOutput(object sender, DataReceivedEventArgs data) { 
        string output = data.Data; 
         
        Console.WriteLine(output); 
    } 
  

^ will not work, can't even connect to the server locally.

After this is fixed I would like to know how to send messages to the server console as well.

Link to comment

Thank you for replying, but that did not work.

Is there a list of command line arguments that MTA uses I can view?

Alright it worked as you said on the MTA Server.exe when I set the short-cut target to "PATH" -t. I must be doing something wrong C# wise because my program can't get it to redirect the output.

  
/// Navigate to MTA Server.exe 
this.mtaServer = new Process(); 
this.mtaServer.StartInfo.FileName = "C:\\Program Files (x86)\\Rockstar Games\\MTA San Andreas 1.5\\server\\MTA Server.exe"; 
this.mtaServer.StartInfo.Arguments = "-t"; 
/// Set up to run in the background. 
this.mtaServer.StartInfo.UseShellExecute = false; 
this.mtaServer.StartInfo.RedirectStandardOutput = true; 
this.mtaServer.StartInfo.RedirectStandardError = true; 
this.mtaServer.StartInfo.RedirectStandardInput = true; 
/// this.mtaServer.StartInfo.CreateNoWindow = true; 
this.mtaServer.ErrorDataReceived += HandleProcessOutput; 
this.mtaServer.OutputDataReceived += HandleProcessOutput; 
this.mtaServer.EnableRaisingEvents = true; 
/// Execute. 
this.mtaServer.Start(); 
this.mtaServer.BeginOutputReadLine(); 
this.mtaServer.BeginErrorReadLine(); 
  

Link to comment

I have been attempting to do this ALL DAY, and I think I FINALLY know why it is not working, well it is but I know why it's not working as intended.

When I tried executing samp-server.exe (San Andreas Multiplayer, pls forgive me I needed something else to test) it told me "Unable to exec file 'server.cfg'" and that's when it hit me, what if I moved server.cfg to the bin of the executable trying to control samp-server.exe, so I did, and this is the result of doing that

[18:50:52] I couldn't load any gamemode scripts. Please verify your server.cfg

[18:50:52] It needs a gamemode0 line at the very least.

Now I know what the problem is, it CAN redirect the output just I need a way of my Server Manager to execute the MTA Server.exe and use it as if it was in the C:/Program Files (x86)/Rockstar Games/MTA San Andreas 1.5/server/ directory?

Link to comment
Set the working directory of the process (the MTA server) to the correct path (the path where the server actually is): https://msdn.microsoft.com/en-us/librar ... y(v=vs.110).aspx

https://msdn.microsoft.com/en-us/librar ... ctory.aspx

Thank you for the reply.

This worked flawlessly with the samp-server.exe, but sadly not with MTA Server.exe.

If anyone else has any suggestions? I am open-minded.

Might help me find what I missed from the StartInfo variable: https://msdn.microsoft.com/en-us/librar ... o(v=vs.110).aspx

UnityEngine.Debug.Log("Starting MTA Server.exe..."); 
/// Navigate to MTA Server.exe 
this.mtaServer = new Process(); 
this.mtaServer.StartInfo.FileName = @"C:\Program Files (x86)\Rockstar Games\MTA San Andreas 1.5\server\MTA Server.exe"; 
this.mtaServer.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\Rockstar Games\MTA San Andreas 1.5\server\"; 
this.mtaServer.StartInfo.Arguments = "-t"; 
/// Set up to run in the background. 
this.mtaServer.StartInfo.UseShellExecute = false; 
this.mtaServer.StartInfo.RedirectStandardOutput = true; 
this.mtaServer.StartInfo.RedirectStandardError = true; 
this.mtaServer.StartInfo.RedirectStandardInput = true; 
/// this.mtaServer.StartInfo.CreateNoWindow = true; 
this.mtaServer.ErrorDataReceived += HandleProcessOutput; 
this.mtaServer.OutputDataReceived += HandleProcessOutput; 
this.mtaServer.EnableRaisingEvents = true; 
/// Execute. 
bool started = this.mtaServer.Start(); 
  
if(started == true) { 
    UnityEngine.Debug.Log("MTA Server.exe started."); 
     
    this.mtaServer.BeginOutputReadLine(); 
    this.mtaServer.BeginErrorReadLine(); 
} 
else { 
    UnityEngine.Debug.Log("MTA Server.exe did not start."); 
} 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...