Skip to content

Commit

Permalink
- Added README.md
Browse files Browse the repository at this point in the history
- Fixed server logging not being passed to service.
- Fixed working directory setup for service mode.
  • Loading branch information
kwilliams1987 committed Feb 14, 2021
1 parent 1966399 commit 8c2a0b9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 36 deletions.
1 change: 1 addition & 0 deletions MinecraftBedrockService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
publish.cmd = publish.cmd
README.md = README.md
EndProjectSection
EndProject
Global
Expand Down
11 changes: 6 additions & 5 deletions MinecraftBedrockService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
using Serilog;
using System;
using System.Diagnostics;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.ServiceProcess;
using System.Threading;

namespace MinecraftBedrockService
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "App is only designed for Windows platform.")]
class Program
{
static void Main(string[] args)
{
var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.AddJsonFile(fileProvider, "settings.json", true, false)
.Build();

var serviceConfig = configuration.Get<ServiceConfig>() ?? new ServiceConfig();

var fileProvider = new PhysicalFileProvider(serviceConfig.WorkingDirectory);
var loggerConfiguration = new LoggerConfiguration()
.WriteTo.File(fileProvider.GetFileInfo("server_wrapper.log").PhysicalPath);
.WriteTo.File(fileProvider.GetFileInfo(serviceConfig.LogFileName).PhysicalPath);

if (Environment.UserInteractive)
{
Expand Down
2 changes: 1 addition & 1 deletion MinecraftBedrockService/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"MinecraftBedrockService": {
"commandName": "Project",
"workingDirectory": "C:\\Minecraft Bedrock"
"commandLineArgs": "--workingDirectory \"C:\\Minecraft Bedrock\""
}
}
}
9 changes: 0 additions & 9 deletions MinecraftBedrockService/ServerConfig.cs

This file was deleted.

48 changes: 27 additions & 21 deletions MinecraftBedrockService/ServerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using Microsoft.Extensions.Primitives;
using System;
using System.Diagnostics;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.ServiceProcess;
using System.Threading;

namespace MinecraftBedrockService
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "App is only designed for Windows platform.")]
internal class ServerWrapper : ServiceBase
{
private readonly IConfiguration configuration;
Expand Down Expand Up @@ -61,36 +62,41 @@ private void PermissionsChangedCallback(object state)
permissionsWatcher.RegisterChangeCallback(PermissionsChangedCallback, null);
}

private void ConfigWatcher_Changed(object sender, FileSystemEventArgs e)
{
throw new NotImplementedException();
}

public void Start()
{
Running = true;

logger.LogInformation("Starting service wrapper.");
var serverConfig = configuration.Get<ServerConfig>() ?? new ServerConfig();
var serverConfig = configuration.Get<ServiceConfig>() ?? new ServiceConfig();
var serverExecutable = fileProvider.GetFileInfo(serverConfig.Executable);

if (!serverExecutable.Exists)
if (serverExecutable.Exists)
{
logger.LogError("Could not find the {0} executable.", serverConfig.Executable);
Stop();
return;
ServerProcess = new Process()
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = serverExecutable.PhysicalPath,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
}
};

ServerProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
ServerProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;

ServerProcess.Start();
ServerProcess.BeginOutputReadLine();
}

ServerProcess = Process.Start(new ProcessStartInfo
else
{
FileName = serverExecutable.PhysicalPath,
RedirectStandardError = false,
RedirectStandardInput = true,
RedirectStandardOutput = false
});

ServerProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
ServerProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
logger.LogError("Could not find the {0} executable in working directory {1}.", serverConfig.Executable, serverConfig.WorkingDirectory);
Stop();
}
}

private void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
Expand Down
11 changes: 11 additions & 0 deletions MinecraftBedrockService/ServiceConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.IO;

namespace MinecraftBedrockService
{
internal class ServiceConfig
{
public string WorkingDirectory { get; set; } = Directory.GetCurrentDirectory();
public string Executable { get; set; } = "bedrock_server.exe";
public string LogFileName { get; set; } = "bedrock_service.log";
}
}
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Minecraft Bedrock Windows Service Wrapper

This project allows the `bedrock_server.exe` file to run safely as a background service on Windows systems.

## Features

- Safe handling of Shutdown and Stop conditions by telling server to safely exit with the `stop` command.
- Automatically call `whitelist reload` when `whitelist.json` is modified.
- Automatically call `permission reload` when `permissions.json` is modified.
- Server output is saved to disk as `bedrock_service.log`.
- `bedrock_service.exe` can also be run as a console application for troubleshooting purposes.

## Usage

1. Place `bedrock_service.exe` in a directory (doesn't need to be the same place as `bedrock_server.exe`).
2. Start `bedrock_service.exe` for testing.
If `bedrock_service.exe` is not in the same directory as `bedrock_server.exe` pass the `--workingDirectory "directory\containing\bedrock_server"` parameter.
3. Exit test mode with `CTRL + X`.
3. Create a new Windows Service entry:

`.\sc.exe create MinecraftBedrockServer binPath= "path\to\bedrock_service.exe --workingDirectory \"directory\containing\bedrock_server\"" start= delayed-auto DispayName= "Minecraft Bedrock Dedicated Server" `

## Parameters

| Parameter | Default Value | Description |
|----------------------|-----------------------|------------------------------------|
| `--workingDirectory` | Current Directory | Directory of server code and logs. |
| `--executable` | `bedrock_server.exe` | Filename of server program. |
| `--logFileName` | `bedrock_service.log` | Filename of log output. |

0 comments on commit 8c2a0b9

Please sign in to comment.