godot/modules/mono/editor/GodotTools/GodotTools.IdeConnection/GodotIdeBase.cs
Ignacio Etcheverry 0b94203a79 C#: Add Ide Connection library and server for the editor
This will be used for communicating between the Godot editor and external IDEs/editors, for things like opening files, triggering hot-reload and running the game with a debugger attached.
2019-08-04 01:57:53 +02:00

95 lines
2.3 KiB
C#

using System;
using Path = System.IO.Path;
namespace GodotTools.IdeConnection
{
public class GodotIdeBase : IDisposable
{
private ILogger logger;
public ILogger Logger
{
get => logger ?? (logger = new ConsoleLogger());
set => logger = value;
}
private readonly string projectMetadataDir;
protected const string MetaFileName = "ide_server_meta.txt";
protected string MetaFilePath => Path.Combine(projectMetadataDir, MetaFileName);
private GodotIdeConnection connection;
protected readonly object ConnectionLock = new object();
public bool IsDisposed { get; private set; } = false;
public bool IsConnected => connection != null && !connection.IsDisposed && connection.IsConnected;
public event Action Connected
{
add
{
if (connection != null && !connection.IsDisposed)
connection.Connected += value;
}
remove
{
if (connection != null && !connection.IsDisposed)
connection.Connected -= value;
}
}
protected GodotIdeConnection Connection
{
get => connection;
set
{
connection?.Dispose();
connection = value;
}
}
protected GodotIdeBase(string projectMetadataDir)
{
this.projectMetadataDir = projectMetadataDir;
}
protected void DisposeConnection()
{
lock (ConnectionLock)
{
connection?.Dispose();
}
}
~GodotIdeBase()
{
Dispose(disposing: false);
}
public void Dispose()
{
if (IsDisposed)
return;
lock (ConnectionLock)
{
if (IsDisposed) // lock may not be fair
return;
IsDisposed = true;
}
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
connection?.Dispose();
}
}
}
}