Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git.bottswanamedia.info:James/ARKUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
Bottswana committed Sep 22, 2015
2 parents 867187c + 364c486 commit 4994716
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
11 changes: 8 additions & 3 deletions ARKUpdater/Classes/SteamKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SteamKit : IDisposable
private ARKUpdater _Parent;
private SteamClient _Client;
private CallbackManager _CManager;
private AutoResetEvent _ResetEvent;

public bool Ready;
public bool Failed;
Expand Down Expand Up @@ -71,9 +72,9 @@ protected virtual void Dispose(bool disposing)
#endregion Disposal

#region Create Thread
public static ThreadPair SpawnThread(ARKUpdater p)
public static ThreadPair SpawnThread(ARKUpdater p, AutoResetEvent r)
{
var thisThread = new SteamKit(p);
var thisThread = new SteamKit(p, r);
Thread tThread = new Thread( () => thisThread.RunThread() )
{
IsBackground = true
Expand All @@ -85,9 +86,11 @@ public static ThreadPair SpawnThread(ARKUpdater p)
#endregion Create Thread

#region Thread Setup
public SteamKit(ARKUpdater p)
public SteamKit(ARKUpdater p, AutoResetEvent r)
{
this._Parent = p;
this._ResetEvent = r;

this.Ready = false;
this.Failed = false;
this._ThreadRunning = true;
Expand Down Expand Up @@ -133,6 +136,7 @@ private void ConnectedCallback(SteamClient.ConnectedCallback connected)
private void DisconnectedCallback(SteamClient.DisconnectedCallback disconnected)
{
_Parent.Log.ConsolePrint(LogLevel.Debug, "Disconnected from Steam3");
_ResetEvent.Set();
_ThreadRunning = false;
}

Expand All @@ -147,6 +151,7 @@ private void LogOnCallback(SteamUser.LoggedOnCallback loggedOn)
}

_Parent.Log.ConsolePrint(LogLevel.Debug, "Logged in anonymously to Steam3");
_ResetEvent.Set();
Ready = true;
}
#endregion Steam3 Callbacks
Expand Down
17 changes: 10 additions & 7 deletions ARKUpdater/Interfaces/ServerInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using ARKUpdater.Classes;
using System.Diagnostics;
using System.Collections;
Expand All @@ -19,9 +20,9 @@ public ServerInterface(ARKUpdater Parent)
this._Parent = Parent;
}

public abstract bool StopServer(SettingsLoader.ServerChild ServerData);
public abstract int StartServer(SettingsLoader.ServerChild ServerData);
public abstract bool ServerRunning(SettingsLoader.ServerChild ServerData);
public abstract bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent);
}

class ServerInterfaceWindows : ServerInterface
Expand All @@ -35,7 +36,7 @@ static class NativeMethods
#endregion W32API Imports

public ServerInterfaceWindows(ARKUpdater parent) : base(parent) {}
public override bool StopServer(SettingsLoader.ServerChild ServerData)
public override bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent)
{
Process thisProcess = null;
foreach( var tProcess in _ProcessDict )
Expand All @@ -45,8 +46,11 @@ public override bool StopServer(SettingsLoader.ServerChild ServerData)
}

if( thisProcess == null ) return false;
File.Delete(string.Format("{0}\\server.pid", ServerData.GameServerPath));
thisProcess.Exited += new EventHandler( (object sender, EventArgs e) => {
ResetEvent.Set();
});

File.Delete(string.Format("{0}\\server.pid", ServerData.GameServerPath));
thisProcess.CloseMainWindow();
return true;
}
Expand Down Expand Up @@ -117,8 +121,8 @@ public override int StartServer(SettingsLoader.ServerChild ServerData)
Proc.Exited += new EventHandler(_ProcessExited);

// Set Window Title
System.Threading.Thread.Sleep(2000);
NativeMethods.SetWindowText(Proc.MainWindowHandle, string.Format("ARK: {0} (Managed by ARKUpdater)", ServerData.GameServerName));
//System.Threading.Thread.Sleep(2000);
//NativeMethods.SetWindowText(Proc.MainWindowHandle, string.Format("ARK: {0} (Managed by ARKUpdater)", ServerData.GameServerName));

// Return with Process ID
_Parent.Log.ConsolePrint(LogLevel.Debug, "Spawned new Server Process with ID {0}", Proc.Id);
Expand Down Expand Up @@ -167,7 +171,6 @@ private void _ProcessExited(object sender, EventArgs e)
}

// We are expecting the server to send us an exit signal, which means we have an update pending for the server (holding the main loop)
// Set processid to 0 to signal that the server has exited and we are free to start the update process
_Parent.Log.ConsolePrint(LogLevel.Debug, "Server '{0}' completed shutdown, marking as ready for update", Server.ServerData.GameServerName);
Server.ProcessID = 0;
break;
Expand Down Expand Up @@ -210,7 +213,7 @@ public override int StartServer(SettingsLoader.ServerChild ServerData)
throw new NotImplementedException();
}

public override bool StopServer(SettingsLoader.ServerChild ServerData)
public override bool StopServer(SettingsLoader.ServerChild ServerData, AutoResetEvent ResetEvent)
{
throw new NotImplementedException();
}
Expand Down
11 changes: 7 additions & 4 deletions ARKUpdater/Interfaces/SteamInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ public override bool VerifySteamPath(string ExecutablePath)

public override int GetGameInformation(uint appid)
{
using( var Steam3 = SteamKit.SpawnThread(_Parent) )
var WaitHandle = new AutoResetEvent(false);
using( var Steam3 = SteamKit.SpawnThread(_Parent, WaitHandle) )
{
while( !Steam3.tClass.Ready && !Steam3.tClass.Failed ) Thread.Sleep(100);
var WaitHandle = new AutoResetEvent(false);
// Wait for Steam3 to be ready
WaitHandle.WaitOne();
WaitHandle.Reset();

if( Steam3.tClass.Ready )
// Prepare request to Steam3
if( Steam3.tClass.Ready && !Steam3.tClass.Failed )
{
var returndata = -1;
Steam3.tClass.RequestAppInfo(appid, (x) => {
Expand Down
16 changes: 10 additions & 6 deletions ARKUpdater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,14 @@ public void Run()
Log.ConsolePrint(LogLevel.Debug, "Checking with Steam for updates to ARK (Current Build: {0})", BuildNumber);
BuildNumber = SteamInt.GetGameInformation(376030);

LastUpdatePollTime = Helpers.CurrentUnixStamp;
if( BuildNumber > PreviousBuild ) Log.ConsolePrint(LogLevel.Info, "A new build of `ARK: Survival Evolved` is available. Build number: {0}", BuildNumber);
if( BuildNumber != -1 )
{
LastUpdatePollTime = Helpers.CurrentUnixStamp;
if( BuildNumber > PreviousBuild ) Log.ConsolePrint(LogLevel.Info, "A new build of `ARK: Survival Evolved` is available. Build number: {0}", BuildNumber);
}
}

bool MinutePassed = (LastMinutePollTime+60 <= Helpers.CurrentUnixStamp) ? true : false;
bool MinutePassed = ( LastMinutePollTime + 60 <= Helpers.CurrentUnixStamp ) ? true : false;
foreach( var Server in Servers )
{
if( Server.MinutesRemaining == -1 )
Expand Down Expand Up @@ -338,11 +341,12 @@ public void Run()
_Sleeper.WaitOne( TimeSpan.FromSeconds(2) );

// Shutdown Server
var ResetEvent = new AutoResetEvent(false);
ServerInt.StopServer(Server.ServerData, ResetEvent);
Log.ConsolePrint(LogLevel.Info, "Server '{0}' will now be shutdown for an update", Server.ServerData.GameServerName);
ServerInt.StopServer(Server.ServerData);


ResetEvent.WaitOne();
Log.ConsolePrint(LogLevel.Debug, "Server '{0}' now waiting for process exit", Server.ServerData.GameServerName);
while( Server.ProcessID != 0 ) Thread.Sleep(100); // This is set to 0 by our Exit event on the process in ServerInterface.cs

// Update Server
SteamInt.UpdateGame(Server.ServerData.SteamUpdateScript, ARKConfiguration.ShowSteamUpdateInConsole);
Expand Down

0 comments on commit 4994716

Please sign in to comment.