This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Malware
committed
Feb 28, 2019
1 parent
94a1489
commit f9c2312
Showing
21 changed files
with
966 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Sandbox.Game.World; | ||
using SpaceEngineers.Game; | ||
|
||
namespace MDKListenerPlugin.Base | ||
{ | ||
public abstract class SpaceEngineersPlugin | ||
{ | ||
bool _isInitialized; | ||
bool _isConnectedToSession; | ||
|
||
public SpaceEngineersGame Game { get; private set; } | ||
|
||
~SpaceEngineersPlugin() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
if (MySession.Static != null) | ||
MySession.Static.OnReady -= OnSessionReady; | ||
MySession.OnLoading -= OnSessionLoading; | ||
MySession.AfterLoading -= OnSessionAfterLoading; | ||
MySession.OnUnloading -= OnSessionUnloading; | ||
MySession.OnUnloaded -= OnSessionUnloaded; | ||
} | ||
} | ||
|
||
public void Init(object gameInstance) | ||
{ | ||
Debugger.Launch(); | ||
Game = (SpaceEngineersGame)gameInstance; | ||
OnStarting(); | ||
} | ||
|
||
protected virtual void OnStarting() | ||
{ } | ||
|
||
protected virtual void OnSessionUnloaded() | ||
{ } | ||
|
||
protected virtual void OnSessionUnloading() | ||
{ } | ||
|
||
protected virtual void OnSessionAfterLoading() | ||
{ } | ||
|
||
protected virtual void OnSessionLoading() | ||
{ | ||
if (!_isConnectedToSession) | ||
{ | ||
_isConnectedToSession = true; | ||
MySession.Static.OnReady += OnSessionReady; | ||
} | ||
} | ||
|
||
protected virtual void OnSessionReady() | ||
{ } | ||
|
||
public void Update() | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
_isInitialized = true; | ||
OnInitialize(); | ||
} | ||
|
||
if (MySession.Static == null) | ||
return; | ||
OnUpdate(); | ||
} | ||
|
||
protected virtual void OnInitialize() | ||
{ | ||
MySession.OnLoading += OnSessionLoading; | ||
MySession.AfterLoading += OnSessionAfterLoading; | ||
MySession.OnUnloading += OnSessionUnloading; | ||
MySession.OnUnloaded += OnSessionUnloaded; | ||
} | ||
|
||
protected virtual void OnUpdate() | ||
{ } | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Source/MDKListenerPlugin/Base/SpaceEngineersSynchronizationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Sandbox; | ||
|
||
namespace MDKListenerPlugin.Base | ||
{ | ||
public class SpaceEngineersSynchronizationContext : SynchronizationContext | ||
{ | ||
readonly string _invocationId; | ||
|
||
public SpaceEngineersSynchronizationContext(string invocationId) | ||
{ | ||
_invocationId = invocationId; | ||
} | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
void invoke() => d(state); | ||
MySandboxGame.Static.Invoke(invoke, _invocationId); | ||
} | ||
|
||
public override void Send(SendOrPostCallback d, object state) | ||
{ | ||
var tcs = new TaskCompletionSource<object>(); | ||
|
||
void invoke() | ||
{ | ||
try | ||
{ | ||
d(state); | ||
tcs.SetResult(null); | ||
} | ||
catch (Exception e) | ||
{ | ||
tcs.SetException(e); | ||
} | ||
} | ||
|
||
MySandboxGame.Static.Invoke(invoke, _invocationId); | ||
tcs.Task.Wait(); | ||
} | ||
} | ||
} |
Oops, something went wrong.