Skip to content

Commit

Permalink
Issue found when starting service during windows boot. First server c…
Browse files Browse the repository at this point in the history
…onstantly restarts on a loop. Count crashes to a limit and force restart the service seems to be a working hack for now. Added automatic recovery to the service installation. Fixed more path issues.
  • Loading branch information
crowbarmaster authored and crowbarmaster committed Dec 27, 2020
1 parent 2cdaaca commit 08fecc9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
22 changes: 1 addition & 21 deletions BedrockClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
using BedrockService;
using System;
using System.IO;
using System.Reflection;

namespace BedrockClient
{
class Program
{
public static Random rand = new Random();
public static string LogPath = $@"{Directory.GetCurrentDirectory()}\Logs\Logfile.log";

public static void LogSetup()
{
string LogPathRand = $@"{Directory.GetCurrentDirectory()}\Logs\Logfile_{rand.Next(111111, 999999)}.log";
if (File.Exists(LogPath))
{
File.Copy(LogPath, LogPathRand);
}
}

public static void AppendToLog(string TextToLog)
{
if (TextToLog != null)
{
string output = $"{TextToLog}\n";
File.AppendAllText(LogPath, output);
}
}

static void Main(string[] args)
{
Console.WriteLine("Minecraft Bedrock Service Console");
Expand Down
12 changes: 11 additions & 1 deletion BedrockService/BedrockServerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class BedrockServerWrapper
string loggedThroughput;
StringBuilder consoleBufferServiceOutput = new StringBuilder();
bool serverStarted = false;
int RestartLimit = 3;
int RestartCount = 0;

const string worldsFolder = "worlds";
const string startupMessage = "[INFO] Server started.";
Expand Down Expand Up @@ -217,7 +219,15 @@ public void Monitor()
Thread.Sleep(5000);
if (!Stopping)
{
Monitor();
if(RestartCount < RestartLimit)
{
Monitor();
}
else
{
StopControl();
Environment.Exit(1);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions BedrockService/BedrockServiceWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -263,7 +264,7 @@ private void ValidSettingsCheck()
}
try
{
ZipFile.ExtractToDirectory($@"{Directory.GetCurrentDirectory()}\MCSFiles\Update.zip", server.ServerConfig.BedrockServerExeLocation);
ZipFile.ExtractToDirectory($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\MCSFiles\Update.zip", server.ServerConfig.BedrockServerExeLocation);
}
catch (Exception e)
{
Expand Down Expand Up @@ -293,7 +294,7 @@ private void ValidSettingsCheck()
}
try
{
ZipFile.ExtractToDirectory($@"{Directory.GetCurrentDirectory()}\MCSFiles\Update.zip", server.ServerConfig.BedrockServerExeLocation);
ZipFile.ExtractToDirectory($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\MCSFiles\Update.zip", server.ServerConfig.BedrockServerExeLocation);
}
catch (Exception e)
{
Expand Down
18 changes: 15 additions & 3 deletions BedrockService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace BedrockService
{
class Program
{
public static bool DebugModeEnabled = false;
static void Main(string[] args)
{

Expand Down Expand Up @@ -38,12 +39,23 @@ static void Main(string[] args)
x.SetDisplayName("BedrockService");
x.SetServiceName("BedrockService");

x.EnableServiceRecovery(src =>
{
src.OnCrashOnly();
src.RestartService(delayInMinutes: 0);
src.RestartService(delayInMinutes: 1);
src.SetResetPeriod(days: 1);
});

});

var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Console.Write("Program is force-quitting. Press any key to exit.");
Console.Out.Flush();
Console.ReadLine();
if (DebugModeEnabled)
{
Console.Write("Program is force-quitting. Press any key to exit.");
Console.Out.Flush();
Console.ReadLine();
}
Environment.ExitCode = exitCode;
}
}
Expand Down
15 changes: 8 additions & 7 deletions BedrockService/Updater.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -33,34 +34,34 @@ public static async Task<bool> CheckUpdates()
string Version = m.Groups[2].Value;
client.Dispose();

if (File.Exists($@"{Directory.GetCurrentDirectory()}\bedrock_ver.ini"))
if (File.Exists($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\bedrock_ver.ini"))
{
string LocalVer = File.ReadAllText($@"{Directory.GetCurrentDirectory()}\bedrock_ver.ini");
string LocalVer = File.ReadAllText($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\bedrock_ver.ini");
if (LocalVer != Version)
{
Console.WriteLine($"New version detected! Now fetching from {DownloadPath}...");
VersionChanged = true;
FetchBuild(DownloadPath).Wait();
File.WriteAllText($@"{Directory.GetCurrentDirectory()}\bedrock_ver.ini", Version);
File.WriteAllText($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\bedrock_ver.ini", Version);
return true;
}
}
else
{
Console.WriteLine("Version ini file missing, fetching build to recreate...");
FetchBuild(DownloadPath).Wait();
File.WriteAllText($@"{Directory.GetCurrentDirectory()}\bedrock_ver.ini", Version);
File.WriteAllText($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\bedrock_ver.ini", Version);
return true;
}
return false;
}

public static async Task FetchBuild(string path)
{
string ZipDir = $@"{Directory.GetCurrentDirectory()}\MCSFiles\Update.zip";
if (!Directory.Exists($@"{Directory.GetCurrentDirectory()}\MCSFiles"))
string ZipDir = $@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\MCSFiles\Update.zip";
if (!Directory.Exists($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\MCSFiles"))
{
Directory.CreateDirectory($@"{Directory.GetCurrentDirectory()}\MCSFiles");
Directory.CreateDirectory($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\MCSFiles");
}
if (File.Exists(ZipDir))
{
Expand Down

0 comments on commit 08fecc9

Please sign in to comment.