Skip to content

Commit

Permalink
Release version v1.0.8-alpha (#28)
Browse files Browse the repository at this point in the history
V1.0.8 alpha
  • Loading branch information
atrexus authored Jan 12, 2024
2 parents 261b2d1 + 86e2214 commit de59e4f
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 348 deletions.
15 changes: 14 additions & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf
dotnet_style_prefer_collection_expression = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
###############################
# C# Coding Conventions #
###############################
Expand Down Expand Up @@ -116,6 +121,14 @@ csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
###############################
# VB Coding Conventions #
###############################
Expand Down
48 changes: 22 additions & 26 deletions src/Unluau.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using System.Runtime.InteropServices;
using CommandLine;
using CommandLine.Text;
using Serilog;
using Serilog.Events;

namespace Unluau.CLI
{
class Program
{
private static string Version = "0.0.7-alpha";
private static string Version = "0.0.8-alpha";

/// <summary>
/// Avalible options for the Unluau decompiler/dissasembler.
Expand All @@ -32,12 +34,12 @@ public class Options
[Option('v', "verbose", Default = false, HelpText = "Shows log messages as the decompiler is decompiling a script.")]
public bool Verbose { get; set; }

[Option("supress-warnings", Default = false, HelpText = "Does not display warnings to the log file or console.")]
public bool SupressWarnings { get; set; }

[Option("logs", Default = null, HelpText = "The file in which the logs for the decompilation will go (uses stdout if not set).")]
public string? LogFile { get; set; }

[Option("log-level", Default = LogEventLevel.Verbose, HelpText = "The minimum log level to use.")]
public LogEventLevel LogLevel { get; set; }

#region Decompiler Configuration

[Option("inline-tables", Default = false, HelpText = "Inlines table definitions. Usually leads to cleaner code.")]
Expand All @@ -46,7 +48,7 @@ public class Options
[Option("rename-upvalues", Default = true, HelpText = "Renames upvalues to \"upval{x}\" to help distinguish from regular local variables.")]
public bool RenameUpvalues { get; set; }

[Option("smart-variable-names", Default = true, HelpText = "Generates logical names for local variables based on their value.")]
[Option("smart-variable-names", Default = false, HelpText = "Generates logical names for local variables based on their value.")]
public bool SmartVariableNames { get; set; }

[Option("descriptive-comments", Default = false, HelpText = "Adds descriptive comments around each block (almost like debug info).")]
Expand Down Expand Up @@ -83,30 +85,29 @@ static void RunOptions(Options options)
{
using (Stream stream = string.IsNullOrEmpty(options.InputFile) ? Console.OpenStandardInput() : File.OpenRead(options.InputFile))
{
var logConfig = new LoggerConfiguration();

if (string.IsNullOrEmpty(options.LogFile))
logConfig.WriteTo.Console(options.LogLevel);
else
logConfig.WriteTo.File(options.OutputFile!, options.LogLevel);

logConfig.MinimumLevel.Is(options.LogLevel);

DecompilerOptions decompilerOptions = new DecompilerOptions()
{
Output = options.OutputFile == null ? new Output() : new Output(File.CreateText(options.OutputFile)),
DescriptiveComments = options.ShowDescriptiveComments,
Verbose = options.Verbose,
HeaderEnabled = false,
HeaderEnabled = true,
InlineTableDefintions = options.InlineTables,
RenameUpvalues = options.RenameUpvalues,
VariableNameGuessing = options.SmartVariableNames,
Version = Version,
Warnings = !options.SupressWarnings,
PerferStringInterpolation = options.StringInterpolation,
Encoding = options.Encoding
Encoding = options.Encoding,
};

if (string.IsNullOrEmpty(options.LogFile))
{
StreamWriter consoleWriter = new StreamWriter(Console.OpenStandardOutput());
consoleWriter.AutoFlush = true;

decompilerOptions.LogFile = consoleWriter;
}
else
decompilerOptions.LogFile = File.CreateText(options.LogFile);
Log.Logger = decompilerOptions.Logger = logConfig.CreateLogger();

try
{
Expand All @@ -119,19 +120,14 @@ static void RunOptions(Options options)
}
catch (DecompilerException e)
{
Console.Error.WriteLine("An error occured when decompiling: \t" + e.Message);
Log.Fatal(e, "Decompilation error");
}
catch (Exception e)
{
Console.Error.WriteLine($"An unknown error occured while decompiling the script: {e.Message}\n");
if (options.Verbose)
Console.Error.WriteLine(e.StackTrace);
Console.Error.WriteLine($"Please create an issue at the GitHub repository here: https://github.com/valencefun/unluau/issues");

Log.Fatal(e, "Unexpected error; please report here: https://github.com/atrexus/unluau/issues");
}

decompilerOptions.Output.Flush();
decompilerOptions.LogFile.Flush();
Log.CloseAndFlush();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Unluau.CLI/Unluau.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file modified src/Unluau.Test/Binary/Namecall.luau
Binary file not shown.
Binary file added src/Unluau.Test/Binary/RepeatUntil01.luau
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Unluau.Test/Expect/RepeatUntil01.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repeat
repeat
f()
until b
g()
until a
6 changes: 6 additions & 0 deletions src/Unluau.Test/UnluauTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,11 @@ public void Test_BooleanAssignment()
{
GetAndAssert("Binary/BooleanAssign01.luau", "Expect/BooleanAssign01.lua");
}

[TestMethod]
public void Test_RepeatUntil()
{
GetAndAssert("Binary/RepeatUntil01.luau", "Expect/RepeatUntil01.lua");
}
}
}
55 changes: 8 additions & 47 deletions src/Unluau/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Serilog;

namespace Unluau
{
Expand All @@ -18,60 +19,31 @@ public class DecompilerOptions
public bool InlineTableDefintions { get; set; } = false;
public bool RenameUpvalues { get; set; }
public string? Version { get; set; }
public bool Verbose { get; set; }
public bool Warnings { get; set; }
public OpCodeEncoding Encoding { get; set; }
public Output Output { get; set; } = new Output();
public StreamWriter? LogFile { get; set; }
public ILogger Logger { get; set; } = new LoggerConfiguration().CreateLogger();
}

public class Decompiler
{
private DecompilerOptions _options;
private Chunk chunk;
private LogManager manager;
private readonly DecompilerOptions _options;
private readonly Chunk _chunk;

public Guid Guid { get; private set; }

public Decompiler(Stream stream, DecompilerOptions options)
{
// Create our logger instance for the decompiler
manager = new LogManager(GetLogSeverity(options));
manager.LogRecieved += OnLogRecieved;
Log.Logger = options.Logger!;

_options = options;
chunk = new Deserializer(manager, stream, options.Encoding).Deserialize();
_chunk = new Deserializer(stream, options.Encoding).Deserialize();

Guid = Guid.NewGuid();
}

private void OnLogRecieved(object? sender, LogRecievedEventArgs e)
{
if (e.Message.Severity == LogSeverity.Warn && !_options.Warnings)
return;

string Text = $"{DateTime.UtcNow.ToString("hh:mm:ss")} [{e.Message.Severity}] {e.Message.Source}: {e.Message.Exception?.ToString() ?? e.Message.Message}";

switch (e.Message.Severity)
{
case LogSeverity.Warn:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogSeverity.Fatal:
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
case LogSeverity.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
}

_options.LogFile!.WriteLine(Text);
Console.ForegroundColor = ConsoleColor.Gray;
}

public void Decompile()
{
Lifter lifter = new Lifter(chunk, _options);
Lifter lifter = new Lifter(_chunk, _options);

OuterBlock program = lifter.LiftProgram();

Expand All @@ -82,17 +54,6 @@ public void Decompile()
_options.Output.Flush();
}

public string Dissasemble()
{
return chunk.ToString();
}

private LogSeverity GetLogSeverity(DecompilerOptions options)
{
if (options.Verbose)
return LogSeverity.Debug;

return LogSeverity.Info;
}
public string Dissasemble() => _chunk.ToString();
}
}
Loading

0 comments on commit de59e4f

Please sign in to comment.