Closed #38. Implements a base for plugins.

This commit is contained in:
Benito Palacios Sánchez 2015-03-15 11:06:44 +01:00
parent 1ae07b9bef
commit ccdf3980a9
5 changed files with 129 additions and 0 deletions

View File

@ -51,6 +51,7 @@
<Compile Include="GdbStreamTests.cs" />
<Compile Include="GdbTestingBase.cs" />
<Compile Include="GdbSessionManagerTests.cs" />
<Compile Include="PluginTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -0,0 +1,63 @@
//
// PluginTests.cs
//
// Author:
// Benito Palacios Sánchez <benito356@gmail.com>
//
// Copyright (c) 2015 Benito Palacios Sánchez
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Linq;
using Moq;
using NitroDebugger;
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class PluginTests
{
[Test]
public void GetInstance()
{
TestPlugin plugin = new TestPlugin();
Assert.AreSame(Plugin.Instances[0], plugin);
plugin.Dispose();
}
[Test]
public void GetFirstInstance()
{
TestPlugin plugin1 = new TestPlugin();
TestPlugin plugin2 = new TestPlugin();
Assert.AreSame(Plugin.Instances.First(), plugin1);
plugin1.Dispose();
plugin2.Dispose();
}
[Test]
public void RemoveAfterDispose()
{
TestPlugin plugin = new TestPlugin();
plugin.Dispose();
Assert.Throws<InvalidOperationException>(() => Plugin.Instances.First());
}
}
class TestPlugin : Plugin
{
}
}

View File

@ -36,6 +36,9 @@
<Reference Include="libgame">
<HintPath>..\libgame\bin\Debug\libgame.dll</HintPath>
</Reference>
<Reference Include="Mono.Addins">
<HintPath>..\libgame\libs\mono-addins\bin\Mono.Addins.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
@ -65,6 +68,7 @@
<Compile Include="RSP\ExecutionManager.cs" />
<Compile Include="RSP\GdbStream.cs" />
<Compile Include="RSP\GdbSessionManager.cs" />
<Compile Include="Plugin.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

59
NitroDebugger/Plugin.cs Normal file
View File

@ -0,0 +1,59 @@
//
// Plugin.cs
//
// Author:
// Benito Palacios Sánchez <benito356@gmail.com>
//
// Copyright (c) 2015 Benito Palacios Sánchez
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Mono.Addins;
namespace NitroDebugger
{
[TypeExtensionPoint]
public abstract class Plugin : IDisposable
{
static List<Plugin> instances = new List<Plugin>();
public Plugin()
{
instances.Add(this);
}
~Plugin()
{
this.Dispose(false);
}
public static ReadOnlyCollection<Plugin> Instances {
get { return new ReadOnlyCollection<Plugin>(instances); }
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool freeManagedResourcesAlso)
{
instances.Remove(this);
}
}
}

View File

@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Addins;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
@ -40,3 +41,4 @@ using System.Runtime.CompilerServices;
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]
[assembly:AddinRoot("NitroDebugger", "1.0")]