Merge pull request #5410 from RoosterDragon/IDisposable

Disposable fixup
This commit is contained in:
Paul Chote
2014-06-11 10:55:44 +12:00
37 changed files with 248 additions and 260 deletions

View File

@@ -15,7 +15,7 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Move
{
public class PathSearch : IDisposable
public sealed class PathSearch : IDisposable
{
World world;
public CellInfo[,] cellInfo;
@@ -291,11 +291,12 @@ namespace OpenRA.Mods.RA.Move
{
if (disposed)
return;
disposed = true;
GC.SuppressFinalize(this);
PutBackIntoPool(cellInfo);
cellInfo = null;
GC.SuppressFinalize(this);
}
~PathSearch() { Dispose(); }

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class CallLuaFunc : Activity, IDisposable
public sealed class CallLuaFunc : Activity, IDisposable
{
LuaFunction function;
@@ -39,28 +39,11 @@ namespace OpenRA.Mods.RA.Activities
base.Cancel(self);
}
protected void Dispose(bool disposing)
{
if (function == null)
return;
if (disposing)
{
function.Dispose();
function = null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CallLuaFunc()
{
// Dispose unmanaged resources only
Dispose(false);
if (function == null) return;
function.Dispose();
function = null;
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using OpenRA.Graphics;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -21,7 +22,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScript(this); }
}
public class LuaScript : ITick, IWorldLoaded
public sealed class LuaScript : ITick, IWorldLoaded, IDisposable
{
readonly LuaScriptInfo info;
ScriptContext context;
@@ -42,5 +43,11 @@ namespace OpenRA.Mods.RA.Scripting
{
context.Tick(self);
}
public void Dispose()
{
if (context != null)
context.Dispose();
}
}
}

View File

@@ -18,7 +18,7 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Scripting
{
public class LuaScriptContext : IDisposable
public sealed class LuaScriptContext : IDisposable
{
public Lua Lua { get; private set; }
readonly Cache<string, LuaFunction> functionCache;
@@ -133,19 +133,9 @@ namespace OpenRA.Mods.RA.Scripting
}
public void Dispose()
{
if (Lua == null)
return;
GC.SuppressFinalize(this);
Lua.Dispose();
Lua = null;
}
~LuaScriptContext()
{
if (Lua != null)
Game.RunAfterTick(Dispose);
Lua.Dispose();
}
}
}

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScriptInterface(this); }
}
public class LuaScriptInterface : IWorldLoaded, ITick
public sealed class LuaScriptInterface : IWorldLoaded, ITick, IDisposable
{
World world;
SpawnMapActors sma;
@@ -90,6 +90,11 @@ namespace OpenRA.Mods.RA.Scripting
context.InvokeLuaFunction("Tick");
}
public void Dispose()
{
context.Dispose();
}
[LuaGlobal]
public object New(string typeName, LuaTable args)
{

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.RA.Scripting
[Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
public class ScriptTriggersInfo : TraitInfo<ScriptTriggers> { }
public class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
{
public event Action<Actor> OnKilledInternal = _ => {};
@@ -100,37 +100,11 @@ namespace OpenRA.Mods.RA.Scripting
}
}
bool disposed;
protected void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
var toDispose = new [] { onIdle, onDamaged, onKilled, onProduction };
foreach (var f in toDispose.SelectMany(f => f))
f.First.Dispose();
foreach (var l in toDispose)
l.Clear();
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ScriptTriggers()
{
// Dispose unmanaged resources only
Dispose(false);
var pairs = new[] { onIdle, onDamaged, onKilled, onProduction };
pairs.SelectMany(l => l).Select(p => p.First).Do(f => f.Dispose());
pairs.Do(l => l.Clear());
}
}
}

View File

@@ -18,7 +18,6 @@ namespace OpenRA.Mods.RA.Widgets
{
public class HueSliderWidget : SliderWidget
{
Bitmap hueBitmap;
Sprite hueSprite;
public HueSliderWidget() {}
@@ -28,20 +27,22 @@ namespace OpenRA.Mods.RA.Widgets
{
base.Initialize(args);
hueBitmap = new Bitmap(256, 256);
hueSprite = new Sprite(new Sheet(new Size(256, 256)), new Rectangle(0, 0, 256, 1), TextureChannel.Alpha);
var bitmapData = hueBitmap.LockBits(hueBitmap.Bounds(),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
using (var hueBitmap = new Bitmap(256, 256))
{
int* c = (int*)bitmapData.Scan0;
for (var h = 0; h < 256; h++)
*(c + h) = HSLColor.FromHSV(h/255f, 1, 1).RGB.ToArgb();
}
hueBitmap.UnlockBits(bitmapData);
hueSprite = new Sprite(new Sheet(new Size(256, 256)), new Rectangle(0, 0, 256, 1), TextureChannel.Alpha);
hueSprite.sheet.Texture.SetData(hueBitmap);
var bitmapData = hueBitmap.LockBits(hueBitmap.Bounds(),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var h = 0; h < 256; h++)
*(c + h) = HSLColor.FromHSV(h / 255f, 1, 1).RGB.ToArgb();
}
hueBitmap.UnlockBits(bitmapData);
hueSprite.sheet.Texture.SetData(hueBitmap);
}
}
public override void Draw()

View File

@@ -68,16 +68,18 @@ namespace OpenRA.Mods.RA.Widgets
mapRect = new Rectangle(previewOrigin.X, previewOrigin.Y, (int)(previewScale * width), (int)(previewScale * height));
// Only needs to be done once
var terrainBitmap = Minimap.TerrainBitmap(world.Map.Rules.TileSets[world.Map.Tileset], world.Map);
var r = new Rectangle(0, 0, width, height);
var s = new Size(terrainBitmap.Width, terrainBitmap.Height);
terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
terrainSprite.sheet.Texture.SetData(terrainBitmap);
using (var terrainBitmap = Minimap.TerrainBitmap(world.Map.Rules.TileSets[world.Map.Tileset], world.Map))
{
var r = new Rectangle(0, 0, width, height);
var s = new Size(terrainBitmap.Width, terrainBitmap.Height);
terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
terrainSprite.sheet.Texture.SetData(terrainBitmap);
// Data is set in Tick()
customTerrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
actorSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
shroudSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
// Data is set in Tick()
customTerrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
actorSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
shroudSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
}
}
public override string GetCursor(int2 pos)
@@ -197,14 +199,17 @@ namespace OpenRA.Mods.RA.Widgets
if (updateTicks <= 0)
{
updateTicks = 12;
customTerrainSprite.sheet.Texture.SetData(Minimap.CustomTerrainBitmap(world));
using (var bitmap = Minimap.CustomTerrainBitmap(world))
customTerrainSprite.sheet.Texture.SetData(bitmap);
}
if (updateTicks == 8)
actorSprite.sheet.Texture.SetData(Minimap.ActorsBitmap(world));
using (var bitmap = Minimap.ActorsBitmap(world))
actorSprite.sheet.Texture.SetData(bitmap);
if (updateTicks == 4)
shroudSprite.sheet.Texture.SetData(Minimap.ShroudBitmap(world));
using (var bitmap = Minimap.ShroudBitmap(world))
shroudSprite.sheet.Texture.SetData(bitmap);
// Enable/Disable the radar
var enabled = IsEnabled();