Merge pull request #5410 from RoosterDragon/IDisposable
Disposable fixup
This commit is contained in:
@@ -70,6 +70,7 @@ namespace OpenRA
|
||||
{
|
||||
Game.OnQuit -= Cancel;
|
||||
wc.CancelAsync();
|
||||
wc.Dispose();
|
||||
cancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ using OpenRA.FileFormats;
|
||||
|
||||
namespace OpenRA.FileSystem
|
||||
{
|
||||
public class MixFile : IFolder
|
||||
public sealed class MixFile : IFolder, IDisposable
|
||||
{
|
||||
readonly Dictionary<uint, PackageEntry> index;
|
||||
readonly long dataStart;
|
||||
@@ -258,5 +258,11 @@ namespace OpenRA.FileSystem
|
||||
s.Write(file.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (s != null)
|
||||
s.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
@@ -16,7 +17,7 @@ using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile;
|
||||
|
||||
namespace OpenRA.FileSystem
|
||||
{
|
||||
public class ZipFile : IFolder
|
||||
public sealed class ZipFile : IFolder, IDisposable
|
||||
{
|
||||
string filename;
|
||||
SZipFile pkg;
|
||||
@@ -105,6 +106,12 @@ namespace OpenRA.FileSystem
|
||||
pkg.Close();
|
||||
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(filename)));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (pkg != null)
|
||||
pkg.Close();
|
||||
}
|
||||
}
|
||||
|
||||
class StaticMemoryDataSource : IStaticDataSource
|
||||
|
||||
@@ -504,6 +504,8 @@ namespace OpenRA
|
||||
// Ensure that the active replay is properly saved
|
||||
if (orderManager != null)
|
||||
orderManager.Dispose();
|
||||
|
||||
Renderer.Device.Dispose();
|
||||
|
||||
OnQuit();
|
||||
}
|
||||
|
||||
@@ -31,7 +31,8 @@ namespace OpenRA.GameRules
|
||||
return;
|
||||
|
||||
Exists = true;
|
||||
Length = (int)AudLoader.SoundLength(GlobalFileSystem.Open(Filename));
|
||||
using (var s = GlobalFileSystem.Open(Filename))
|
||||
Length = (int)AudLoader.SoundLength(s);
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
@@ -40,7 +41,8 @@ namespace OpenRA.GameRules
|
||||
return;
|
||||
|
||||
Exists = true;
|
||||
Length = (int)AudLoader.SoundLength(GlobalFileSystem.Open(Filename));
|
||||
using (var s = GlobalFileSystem.Open(Filename))
|
||||
Length = (int)AudLoader.SoundLength(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,31 +40,34 @@ namespace OpenRA.Graphics
|
||||
|
||||
public Sheet(string filename)
|
||||
{
|
||||
var bitmap = (Bitmap)Image.FromStream(GlobalFileSystem.Open(filename));
|
||||
Size = bitmap.Size;
|
||||
|
||||
data = new byte[4*Size.Width*Size.Height];
|
||||
var b = bitmap.LockBits(bitmap.Bounds(),
|
||||
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
||||
|
||||
unsafe
|
||||
using (var stream = GlobalFileSystem.Open(filename))
|
||||
using (var bitmap = (Bitmap)Image.FromStream(stream))
|
||||
{
|
||||
int* c = (int*)b.Scan0;
|
||||
Size = bitmap.Size;
|
||||
|
||||
for (var x = 0; x < Size.Width; x++)
|
||||
for (var y = 0; y < Size.Height; y++)
|
||||
data = new byte[4 * Size.Width * Size.Height];
|
||||
var b = bitmap.LockBits(bitmap.Bounds(),
|
||||
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
||||
|
||||
unsafe
|
||||
{
|
||||
var i = 4*Size.Width*y + 4*x;
|
||||
int* c = (int*)b.Scan0;
|
||||
|
||||
// Convert argb to bgra
|
||||
var argb = *(c + (y * b.Stride >> 2) + x);
|
||||
data[i++] = (byte)(argb >> 0);
|
||||
data[i++] = (byte)(argb >> 8);
|
||||
data[i++] = (byte)(argb >> 16);
|
||||
data[i++] = (byte)(argb >> 24);
|
||||
for (var x = 0; x < Size.Width; x++)
|
||||
for (var y = 0; y < Size.Height; y++)
|
||||
{
|
||||
var i = 4 * Size.Width * y + 4 * x;
|
||||
|
||||
// Convert argb to bgra
|
||||
var argb = *(c + (y * b.Stride >> 2) + x);
|
||||
data[i++] = (byte)(argb >> 0);
|
||||
data[i++] = (byte)(argb >> 8);
|
||||
data[i++] = (byte)(argb >> 16);
|
||||
data[i++] = (byte)(argb >> 24);
|
||||
}
|
||||
}
|
||||
bitmap.UnlockBits(b);
|
||||
}
|
||||
bitmap.UnlockBits(b);
|
||||
}
|
||||
|
||||
public ITexture Texture
|
||||
|
||||
@@ -108,27 +108,29 @@ namespace OpenRA.Graphics
|
||||
Offset = { X = face.Glyph.BitmapLeft, Y = -face.Glyph.BitmapTop }
|
||||
};
|
||||
|
||||
unsafe
|
||||
{
|
||||
var p = (byte*)face.Glyph.Bitmap.Buffer;
|
||||
var dest = s.sheet.Data;
|
||||
var destStride = s.sheet.Size.Width * 4;
|
||||
|
||||
for (var j = 0; j < s.size.Y; j++)
|
||||
// A new bitmap is generated each time this property is accessed, so we do need to dispose it.
|
||||
using (var bitmap = face.Glyph.Bitmap)
|
||||
unsafe
|
||||
{
|
||||
for (var i = 0; i < s.size.X; i++)
|
||||
if (p[i] != 0)
|
||||
{
|
||||
var q = destStride * (j + s.bounds.Top) + 4 * (i + s.bounds.Left);
|
||||
dest[q] = c.Second.B;
|
||||
dest[q + 1] = c.Second.G;
|
||||
dest[q + 2] = c.Second.R;
|
||||
dest[q + 3] = p[i];
|
||||
}
|
||||
var p = (byte*)bitmap.Buffer;
|
||||
var dest = s.sheet.Data;
|
||||
var destStride = s.sheet.Size.Width * 4;
|
||||
|
||||
p += face.Glyph.Bitmap.Pitch;
|
||||
for (var j = 0; j < s.size.Y; j++)
|
||||
{
|
||||
for (var i = 0; i < s.size.X; i++)
|
||||
if (p[i] != 0)
|
||||
{
|
||||
var q = destStride * (j + s.bounds.Top) + 4 * (i + s.bounds.Left);
|
||||
dest[q] = c.Second.B;
|
||||
dest[q + 1] = c.Second.G;
|
||||
dest[q + 2] = c.Second.R;
|
||||
dest[q + 3] = p[i];
|
||||
}
|
||||
|
||||
p += bitmap.Pitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
s.sheet.CommitData();
|
||||
|
||||
return g;
|
||||
|
||||
@@ -33,10 +33,10 @@ namespace OpenRA.Graphics
|
||||
|
||||
Sprite[] CacheSpriteFrames(string filename)
|
||||
{
|
||||
var stream = GlobalFileSystem.OpenWithExts(filename, exts);
|
||||
return SpriteSource.LoadSpriteSource(stream, filename).Frames
|
||||
.Select(a => SheetBuilder.Add(a))
|
||||
.ToArray();
|
||||
using (var stream = GlobalFileSystem.OpenWithExts(filename, exts))
|
||||
return SpriteSource.LoadSpriteSource(stream, filename).Frames
|
||||
.Select(a => SheetBuilder.Add(a))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public Sprite[] LoadAllSprites(string filename) { return sprites[filename]; }
|
||||
|
||||
@@ -210,8 +210,12 @@ namespace OpenRA.Graphics
|
||||
|
||||
Voxel LoadFile(Pair<string,string> files)
|
||||
{
|
||||
var vxl = new VxlReader(GlobalFileSystem.OpenWithExts(files.First, ".vxl"));
|
||||
var hva = new HvaReader(GlobalFileSystem.OpenWithExts(files.Second, ".hva"));
|
||||
VxlReader vxl;
|
||||
HvaReader hva;
|
||||
using (var s = GlobalFileSystem.OpenWithExts(files.First, ".vxl"))
|
||||
vxl = new VxlReader(s);
|
||||
using (var s = GlobalFileSystem.OpenWithExts(files.Second, ".hva"))
|
||||
hva = new HvaReader(s);
|
||||
return new Voxel(this, vxl, hva);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace OpenRA
|
||||
|
||||
public enum BlendMode { None, Alpha, Additive, Subtractive, Multiply }
|
||||
|
||||
public interface IGraphicsDevice
|
||||
public interface IGraphicsDevice : IDisposable
|
||||
{
|
||||
IVertexBuffer<Vertex> CreateVertexBuffer(int length);
|
||||
ITexture CreateTexture(Bitmap bitmap);
|
||||
@@ -58,8 +58,6 @@ namespace OpenRA
|
||||
void DisableDepthBuffer();
|
||||
|
||||
void SetBlendMode(BlendMode mode);
|
||||
|
||||
void Quit();
|
||||
}
|
||||
|
||||
public interface IVertexBuffer<T>
|
||||
|
||||
@@ -93,8 +93,9 @@ namespace OpenRA
|
||||
List<string> extracted = new List<string>();
|
||||
try
|
||||
{
|
||||
var z = new ZipInputStream(File.OpenRead(zipFile));
|
||||
z.ExtractZip(dest, extracted, s => onProgress("Extracting " + s));
|
||||
using (var stream = File.OpenRead(zipFile))
|
||||
using (var z = new ZipInputStream(stream))
|
||||
z.ExtractZip(dest, extracted, s => onProgress("Extracting " + s));
|
||||
}
|
||||
catch (SharpZipBaseException)
|
||||
{
|
||||
|
||||
@@ -172,13 +172,11 @@ namespace OpenRA
|
||||
|
||||
public static List<MiniYamlNode> FromFileInPackage(string path)
|
||||
{
|
||||
StreamReader reader = new StreamReader(GlobalFileSystem.Open(path));
|
||||
List<string> lines = new List<string>();
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
lines.Add(reader.ReadLine());
|
||||
reader.Close();
|
||||
|
||||
using (var stream = GlobalFileSystem.Open(path))
|
||||
using (var reader = new StreamReader(stream))
|
||||
while (!reader.EndOfStream)
|
||||
lines.Add(reader.ReadLine());
|
||||
return FromLines(lines.ToArray(), path);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ namespace OpenRA.Network
|
||||
if (packet.Length == 0)
|
||||
throw new NotImplementedException();
|
||||
lock (this)
|
||||
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet } );
|
||||
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet });
|
||||
}
|
||||
|
||||
public virtual void Receive(Action<int, byte[]> packetFn)
|
||||
@@ -102,10 +102,16 @@ namespace OpenRA.Network
|
||||
packetFn(p.FromClient, p.Data);
|
||||
}
|
||||
|
||||
public virtual void Dispose() { }
|
||||
protected virtual void Dispose(bool disposing) { }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkConnection : EchoConnection
|
||||
sealed class NetworkConnection : EchoConnection
|
||||
{
|
||||
TcpClient socket;
|
||||
int clientId;
|
||||
@@ -193,22 +199,27 @@ namespace OpenRA.Network
|
||||
|
||||
bool disposed = false;
|
||||
|
||||
public override void Dispose()
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed) return;
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
t.Abort();
|
||||
if (socket != null)
|
||||
socket.Client.Close();
|
||||
if (disposing)
|
||||
if (socket != null)
|
||||
socket.Client.Close();
|
||||
using (new PerfSample("Thread.Join"))
|
||||
{
|
||||
if (!t.Join(1000))
|
||||
return;
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
~NetworkConnection() { Dispose(); }
|
||||
~NetworkConnection()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
public class OrderManager : IDisposable
|
||||
public sealed class OrderManager : IDisposable
|
||||
{
|
||||
readonly SyncReport syncReport;
|
||||
readonly FrameData frameData = new FrameData();
|
||||
@@ -197,22 +197,10 @@ namespace OpenRA.Network
|
||||
++NetFrameNumber;
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
Connection.Dispose();
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
if (Connection != null)
|
||||
Connection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
public class ReplayConnection : IConnection
|
||||
public sealed class ReplayConnection : IConnection
|
||||
{
|
||||
class Chunk
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
class ReplayRecorderConnection : IConnection, IDisposable
|
||||
sealed class ReplayRecorderConnection : IConnection
|
||||
{
|
||||
public ReplayMetadata Metadata;
|
||||
|
||||
@@ -98,30 +98,24 @@ namespace OpenRA.Network
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
if (Metadata != null)
|
||||
{
|
||||
if (Metadata.GameInfo != null)
|
||||
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
|
||||
Metadata.Write(writer);
|
||||
}
|
||||
writer.Close();
|
||||
inner.Dispose();
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
|
||||
if (Metadata != null)
|
||||
{
|
||||
if (Metadata.GameInfo != null)
|
||||
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
|
||||
Metadata.Write(writer);
|
||||
}
|
||||
|
||||
if (preStartBuffer != null)
|
||||
preStartBuffer.Dispose();
|
||||
writer.Close();
|
||||
inner.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ using System;
|
||||
|
||||
namespace OpenRA.Primitives
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
public sealed class DisposableAction : IDisposable
|
||||
{
|
||||
public DisposableAction(Action onDispose, Action onFinalize)
|
||||
{
|
||||
@@ -26,7 +26,8 @@ namespace OpenRA.Primitives
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) return;
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
onDispose();
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
@@ -81,12 +81,11 @@ namespace OpenRA.Scripting
|
||||
public ScriptGlobalAttribute(string name) { Name = name; }
|
||||
}
|
||||
|
||||
public class ScriptContext : IDisposable
|
||||
public sealed class ScriptContext : IDisposable
|
||||
{
|
||||
public World World { get; private set; }
|
||||
public WorldRenderer WorldRenderer { get; private set; }
|
||||
|
||||
bool disposed;
|
||||
readonly MemoryConstrainedLuaRuntime runtime;
|
||||
readonly LuaFunction tick;
|
||||
|
||||
@@ -100,6 +99,8 @@ namespace OpenRA.Scripting
|
||||
public readonly Cache<ActorInfo, Type[]> ActorCommands;
|
||||
public readonly Type[] PlayerCommands;
|
||||
|
||||
bool disposed;
|
||||
|
||||
public ScriptContext(World world, WorldRenderer worldRenderer,
|
||||
IEnumerable<string> scripts)
|
||||
{
|
||||
@@ -196,27 +197,13 @@ namespace OpenRA.Scripting
|
||||
tick.Call().Dispose();
|
||||
}
|
||||
|
||||
protected void Dispose(bool disposing)
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
runtime.Dispose();
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~ScriptContext()
|
||||
{
|
||||
// Dispose unmanaged resources only
|
||||
Dispose(false);
|
||||
if (runtime != null)
|
||||
runtime.Dispose();
|
||||
}
|
||||
|
||||
static Type[] ExtractRequiredTypes(Type t)
|
||||
|
||||
@@ -39,9 +39,11 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
if (filename.ToLowerInvariant().EndsWith("wav"))
|
||||
return LoadWave(new WavLoader(GlobalFileSystem.Open(filename)));
|
||||
using (var s = GlobalFileSystem.Open(filename))
|
||||
return LoadWave(new WavLoader(s));
|
||||
|
||||
return LoadSoundRaw(AudLoader.LoadSound(GlobalFileSystem.Open(filename)));
|
||||
using (var s = GlobalFileSystem.Open(filename))
|
||||
return LoadSoundRaw(AudLoader.LoadSound(s));
|
||||
}
|
||||
|
||||
static ISoundSource LoadWave(WavLoader wave)
|
||||
|
||||
@@ -105,7 +105,7 @@ namespace OpenRA.Support
|
||||
}
|
||||
}
|
||||
|
||||
public class PerfSample : IDisposable
|
||||
public sealed class PerfSample : IDisposable
|
||||
{
|
||||
readonly Stopwatch sw = Stopwatch.StartNew();
|
||||
readonly string Item;
|
||||
|
||||
@@ -16,7 +16,7 @@ using System.Threading;
|
||||
|
||||
namespace OpenRA.Support
|
||||
{
|
||||
public class PerfTimer : IDisposable
|
||||
public sealed class PerfTimer : IDisposable
|
||||
{
|
||||
readonly string name;
|
||||
readonly float thresholdMs;
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace OpenRA
|
||||
|
||||
if (Game.Settings.Debug.ShowFatalErrorDialog && !Game.Settings.Server.Dedicated)
|
||||
{
|
||||
Game.Renderer.Device.Quit();
|
||||
Game.Renderer.Device.Dispose();
|
||||
Platform.ShowFatalErrorDialog();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user