Skip to content

Commit

Permalink
Use IDisposableGenerator in unluac for LuaDecompileStream. (#115)
Browse files Browse the repository at this point in the history
This is so that way the code becomes simpler and easier to maintain.
  • Loading branch information
AraHaan authored Dec 8, 2022
1 parent e6c0fe3 commit 22dbc8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/UnluacNET/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</AdditionalFiles>
<Using Include="System.Diagnostics" />
<Using Include="System.Text" />
<Using Include="IDisposableGenerator" />
<Using Include="Elskom.Generic.Libs.UnluacNET.IO" />
</ItemGroup>

Expand Down
4 changes: 4 additions & 0 deletions src/UnluacNET/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<PackageReference Include="IDisposableGenerator">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; compile; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<Import Project="../Directory.Packages.props" />
Expand Down
32 changes: 10 additions & 22 deletions src/UnluacNET/LuaDecompileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace Elskom.Generic.Libs.UnluacNET;

public class LuaDecompileStream : Stream
/// <summary>
/// A stream that can decompile Lua files.
/// </summary>
[GenerateDispose(true)]
public partial class LuaDecompileStream : Stream
{
/// <summary>
/// Initializes a new instance of the <see cref="LuaDecompileStream"/> class.
Expand Down Expand Up @@ -39,7 +43,7 @@ public LuaDecompileStream(Stream input, bool keepOpen)
/// Initializes a new instance of the <see cref="LuaDecompileStream"/> class.
/// </summary>
public LuaDecompileStream()
: this(new MemoryStream(), false)
: this(new MemoryStream())
=> this.InputNoData = true;

/// <inheritdoc/>
Expand All @@ -65,14 +69,11 @@ public override long Position
set => throw new NotSupportedException();
}

[DisposeField(true)]
private Stream BaseStream { get; set; }

private bool InputNoData { get; }

private bool KeepOpen { get; }

private bool IsDisposed { get; set; }

/// <summary>
/// Decompiles Lua bytecode based on the data Read from the input stream.
/// </summary>
Expand Down Expand Up @@ -105,31 +106,18 @@ public override void Write(byte[] buffer, int offset, int count)
_ = this.DecompileLuaWithStreamBuffer();
}

/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();

/// <inheritdoc/>
public override void SetLength(long value)
=> throw new NotSupportedException();

/// <inheritdoc/>
public override void Flush()
=> throw new NotSupportedException();

protected override void Dispose(bool disposing)
{
if (!this.IsDisposed && disposing)
{
if (!this.KeepOpen)
{
this.BaseStream.Dispose();
this.BaseStream = null;
}

this.IsDisposed = true;
}

base.Dispose(disposing);
}

private int DecompileLuaWithStreamBuffer()
{
if (this.BaseStream is not MemoryStream baseMemStream)
Expand Down

0 comments on commit 22dbc8b

Please sign in to comment.