mirror of
https://git.do.srb2.org/STJr/UltimateZoneBuilder.git
synced 2024-11-26 22:01:45 +00:00
prepared for more plugin features
This commit is contained in:
parent
1ac1bb3c8e
commit
7c08847a5b
7 changed files with 165 additions and 139 deletions
|
@ -315,6 +315,7 @@
|
|||
<Compile Include="Map\SidedefPart.cs" />
|
||||
<Compile Include="Map\Thing.cs" />
|
||||
<Compile Include="Map\Vertex.cs" />
|
||||
<Compile Include="Plugins\Plug.cs" />
|
||||
<Compile Include="Plugins\Plugin.cs" />
|
||||
<Compile Include="Plugins\PluginManager.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
|
|
@ -37,9 +37,10 @@
|
|||
<Compile Include="ClassicModes\DragSectorsMode.cs" />
|
||||
<Compile Include="ClassicModes\DragGeometryMode.cs" />
|
||||
<Compile Include="ClassicModes\DrawGeometryMode.cs" />
|
||||
<Compile Include="General\BuilderPlug.cs" />
|
||||
<Compile Include="Testing\TriangulatorMode.cs" />
|
||||
<Compile Include="ClassicModes\DragThingsMode.cs" />
|
||||
<Compile Include="Tools\LineLengthLabel.cs" />
|
||||
<Compile Include="General\LineLengthLabel.cs" />
|
||||
<Compile Include="VisualModes\BaseVisualMode.cs" />
|
||||
<Compile Include="VisualModes\BaseVisualSector.cs" />
|
||||
<Compile Include="ClassicModes\DragVerticesMode.cs" />
|
||||
|
|
|
@ -1,123 +0,0 @@
|
|||
|
||||
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
||||
* This program is released under GNU General Public License
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using CodeImp.DoomBuilder.Interface;
|
||||
using CodeImp.DoomBuilder.IO;
|
||||
using CodeImp.DoomBuilder.Map;
|
||||
using CodeImp.DoomBuilder.Rendering;
|
||||
using CodeImp.DoomBuilder.Geometry;
|
||||
using System.Drawing;
|
||||
using CodeImp.DoomBuilder.Editing;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CodeImp.DoomBuilder.BuilderModes
|
||||
{
|
||||
public class LineLengthLabel : IDisposable
|
||||
{
|
||||
#region ================== Constants
|
||||
|
||||
private const int TEXT_CAPACITY = 10;
|
||||
private const float TEXT_SCALE = 14f;
|
||||
private const string VALUE_FORMAT = "0";
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Variables
|
||||
|
||||
private TextLabel label;
|
||||
private Vector2D start;
|
||||
private Vector2D end;
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Properties
|
||||
|
||||
public TextLabel TextLabel { get { return label; } }
|
||||
public Vector2D Start { get { return start; } set { start = value; Update(); } }
|
||||
public Vector2D End { get { return end; } set { end = value; Update(); } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Constructor / Disposer
|
||||
|
||||
// Constructor
|
||||
public LineLengthLabel()
|
||||
{
|
||||
// Initialize
|
||||
Initialize();
|
||||
}
|
||||
|
||||
// Constructor
|
||||
public LineLengthLabel(Vector2D start, Vector2D end)
|
||||
{
|
||||
// Initialize
|
||||
Initialize();
|
||||
Move(start, end);
|
||||
}
|
||||
|
||||
// Initialization
|
||||
private void Initialize()
|
||||
{
|
||||
label = new TextLabel(TEXT_CAPACITY);
|
||||
label.AlignX = TextAlignmentX.Center;
|
||||
label.AlignY = TextAlignmentY.Middle;
|
||||
label.Color = General.Colors.Highlight;
|
||||
label.Backcolor = General.Colors.Background;
|
||||
label.Scale = TEXT_SCALE;
|
||||
label.TransformCoords = true;
|
||||
}
|
||||
|
||||
// Disposer
|
||||
public void Dispose()
|
||||
{
|
||||
label.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Methods
|
||||
|
||||
// This updates the text
|
||||
private void Update()
|
||||
{
|
||||
Vector2D delta = end - start;
|
||||
float length = delta.GetLength();
|
||||
label.Text = length.ToString(VALUE_FORMAT);
|
||||
label.Rectangle = new RectangleF(start.x + delta.x * 0.5f, start.y + delta.y * 0.5f, 0f, 0f);
|
||||
}
|
||||
|
||||
// This moves the label
|
||||
public void Move(Vector2D start, Vector2D end)
|
||||
{
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
Update();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -91,7 +91,7 @@ namespace CodeImp.DoomBuilder.Editing
|
|||
// Make button info
|
||||
if((attr.ButtonImage != null) && (attr.ButtonDesc != null))
|
||||
{
|
||||
buttonimagestream = plugin.FindResource(attr.ButtonImage);
|
||||
buttonimagestream = plugin.GetResourceStream(attr.ButtonImage);
|
||||
if(buttonimagestream != null)
|
||||
{
|
||||
buttonimage = Image.FromStream(buttonimagestream);
|
||||
|
|
106
Source/Plugins/Plug.cs
Normal file
106
Source/Plugins/Plug.cs
Normal file
|
@ -0,0 +1,106 @@
|
|||
#region ================== Namespaces
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CodeImp.DoomBuilder.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the key link between the Doom Builder core and the plugin.
|
||||
/// Every plugin must expose a single class that inherits this class.
|
||||
/// </summary>
|
||||
public class Plug : IDisposable
|
||||
{
|
||||
#region ================== Constants
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Variables
|
||||
|
||||
// Internals
|
||||
private Plugin plugin;
|
||||
|
||||
// Disposing
|
||||
private bool isdisposed = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Properties
|
||||
|
||||
// Internals
|
||||
internal Plugin Plugin { get { return plugin; } set { plugin = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the plugin has been disposed.
|
||||
/// </summary>
|
||||
public bool IsDisposed { get { return isdisposed; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Constructor / Disposer
|
||||
|
||||
/// <summary>
|
||||
/// This is the key link between the Doom Builder core and the plugin.
|
||||
/// Every plugin must expose a single class that inherits this class.
|
||||
/// <para>
|
||||
/// NOTE: Some methods cannot be used in this constructor, because the plugin
|
||||
/// is not yet fully initialized. Instead, use the Initialize method to do
|
||||
/// your initializations.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public Plug()
|
||||
{
|
||||
// Initialize
|
||||
|
||||
// We have no destructor
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is called by the Doom Builder core when the plugin is being disposed.
|
||||
/// </summary>
|
||||
public virtual void Dispose()
|
||||
{
|
||||
// Not already disposed?
|
||||
if(!isdisposed)
|
||||
{
|
||||
// Clean up
|
||||
plugin = null;
|
||||
|
||||
// Done
|
||||
isdisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ================== Methods
|
||||
|
||||
/// <summary>
|
||||
/// This is called after the constructor to allow a plugin to initialize.
|
||||
/// </summary>
|
||||
public virtual void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This finds the embedded resource with the specified name in the plugin and creates
|
||||
/// a Stream from it. Returns null when the embedded resource cannot be found.
|
||||
/// </summary>
|
||||
/// <param name="resourcename">Name of the resource in the plugin.</param>
|
||||
/// <returns>Returns a Stream of the embedded resource,
|
||||
/// or null when the resource cannot be found.</returns>
|
||||
public Stream GetResourceStream(string resourcename)
|
||||
{
|
||||
return plugin.GetResourceStream(resourcename);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -29,7 +29,7 @@ using CodeImp.DoomBuilder.Controls;
|
|||
|
||||
namespace CodeImp.DoomBuilder.Plugins
|
||||
{
|
||||
internal class Plugin
|
||||
internal class Plugin : IDisposable
|
||||
{
|
||||
#region ================== Constants
|
||||
|
||||
|
@ -40,6 +40,9 @@ namespace CodeImp.DoomBuilder.Plugins
|
|||
// The plugin assembly
|
||||
private Assembly asm;
|
||||
|
||||
// The plug
|
||||
private Plug plug;
|
||||
|
||||
// Unique name used to refer to this assembly
|
||||
private string name;
|
||||
|
||||
|
@ -51,6 +54,7 @@ namespace CodeImp.DoomBuilder.Plugins
|
|||
#region ================== Properties
|
||||
|
||||
public Assembly Assembly { get { return asm; } }
|
||||
public Plug Plug { get { return plug; } }
|
||||
public string Name { get { return name; } }
|
||||
public bool IsDisposed { get { return isdisposed; } }
|
||||
|
||||
|
@ -67,6 +71,29 @@ namespace CodeImp.DoomBuilder.Plugins
|
|||
|
||||
// Load assembly
|
||||
asm = Assembly.LoadFile(filename);
|
||||
|
||||
// Find the class that inherits from Plugin
|
||||
Type t = FindSingleClass(typeof(Plug));
|
||||
if(t != null)
|
||||
{
|
||||
// Are the multiple plug classes?
|
||||
if(FindClasses(typeof(Plug)).Length > 1)
|
||||
{
|
||||
// Show a warning
|
||||
General.WriteLogLine("WARNING: Plugin '" + name + "' has more than one plug!");
|
||||
}
|
||||
|
||||
// Make plug instance
|
||||
plug = CreateObject<Plug>(t);
|
||||
plug.Plugin = this;
|
||||
plug.Initialize();
|
||||
}
|
||||
else
|
||||
{
|
||||
// How can we plug something in without a plug?
|
||||
General.WriteLogLine("ERROR: Could not load plugin '" + name + "', plugin is missing the plug!");
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
// Load actions
|
||||
General.Actions.LoadActions(asm);
|
||||
|
@ -94,7 +121,7 @@ namespace CodeImp.DoomBuilder.Plugins
|
|||
#region ================== Methods
|
||||
|
||||
// This creates a stream to read a resource or returns null when not found
|
||||
public Stream FindResource(string resourcename)
|
||||
public Stream GetResourceStream(string resourcename)
|
||||
{
|
||||
string[] resnames;
|
||||
|
||||
|
|
|
@ -76,20 +76,34 @@ namespace CodeImp.DoomBuilder.Plugins
|
|||
{
|
||||
// Load plugin from this file
|
||||
General.MainWindow.DisplayStatus("Loading plugin '" + Path.GetFileName(fn) + "'...");
|
||||
p = new Plugin(fn);
|
||||
if(!p.IsDisposed) this.plugins.Add(p);
|
||||
|
||||
// For all classes that inherit from EditMode
|
||||
editclasses = p.FindClasses(typeof(EditMode));
|
||||
foreach(Type t in editclasses)
|
||||
try
|
||||
{
|
||||
// For all defined EditMode attributes
|
||||
emattrs = (EditModeAttribute[])t.GetCustomAttributes(typeof(EditModeAttribute), true);
|
||||
foreach(EditModeAttribute a in emattrs)
|
||||
p = new Plugin(fn);
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
General.WriteLogLine("WARNING: Plugin file '" + Path.GetFileName(fn) + "' was not loaded.");
|
||||
p = null;
|
||||
}
|
||||
|
||||
// Continue if no errors
|
||||
if((p != null) && (!p.IsDisposed))
|
||||
{
|
||||
// Add to plugins
|
||||
this.plugins.Add(p);
|
||||
|
||||
// For all classes that inherit from EditMode
|
||||
editclasses = p.FindClasses(typeof(EditMode));
|
||||
foreach(Type t in editclasses)
|
||||
{
|
||||
// Make edit mode information
|
||||
editmodeinfo = new EditModeInfo(p, t, a);
|
||||
editmodes.Add(editmodeinfo);
|
||||
// For all defined EditMode attributes
|
||||
emattrs = (EditModeAttribute[])t.GetCustomAttributes(typeof(EditModeAttribute), true);
|
||||
foreach(EditModeAttribute a in emattrs)
|
||||
{
|
||||
// Make edit mode information
|
||||
editmodeinfo = new EditModeInfo(p, t, a);
|
||||
editmodes.Add(editmodeinfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue