Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bb65107
Refactor CLI code
StefanGreve Mar 6, 2024
36c36e4
Rewrite project
StefanGreve Mar 10, 2024
e17f2e4
Refactor code: change format type from string to enum
StefanGreve Mar 11, 2024
e8bdfde
Add path option to export command
StefanGreve Mar 11, 2024
2277815
Start implementation of ExtractorService
StefanGreve Mar 11, 2024
37e80d1
Implement JsonSerializer
StefanGreve Mar 12, 2024
cd57370
Add namespace and scope properties to AbstractEnum
StefanGreve Mar 13, 2024
e42c20f
Throw exceptions in not-implemented methods
StefanGreve Mar 13, 2024
05cebba
Change format option in launch settings to csharp for debugging purposes
StefanGreve Mar 13, 2024
631de00
Implement CSharpSerializer
StefanGreve Mar 13, 2024
1337216
Use dynamic programming to generate the enum code for CSharp serializer
StefanGreve Mar 21, 2024
a98bfd5
Refactor CSharp serializer
StefanGreve Mar 23, 2024
981f5df
Implement Ini class and Ini serializer
StefanGreve Mar 23, 2024
74c3041
Extract models
StefanGreve Mar 23, 2024
13c79b5
Minor code refactoring
StefanGreve Mar 23, 2024
5a7b72d
Add encoding to appsettings.json
StefanGreve Mar 26, 2024
9192f6b
Normalize line endings
StefanGreve Mar 29, 2024
096d728
Implment Python Serializer and add Comment and Identation to AppSettings
StefanGreve Mar 30, 2024
b4bdc36
Add custom top-level comment to CSharpSerializer
StefanGreve Mar 30, 2024
6568b96
Implement Php Serializer
StefanGreve Mar 30, 2024
c1a57df
Implement Rust Serializer
StefanGreve Mar 30, 2024
a793933
Insert newline after top-level comment
StefanGreve Mar 30, 2024
a8ecedd
Define default nuget source
StefanGreve Mar 30, 2024
f9bf045
Edit release build configuration
StefanGreve Mar 30, 2024
515112b
Update documentation
StefanGreve Mar 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
Expand Down Expand Up @@ -46,9 +46,9 @@

###############################################################################
# diff behavior for common document formats
#
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
Expand Down
27 changes: 27 additions & 0 deletions JackTheEnumRipper/Core/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Linq;
using System.Text;

namespace JackTheEnumRipper.Core
{
public static class Extensions
{
public static string Repeat(this string source, int times)
{
return (times != 0) ? string.Concat(Enumerable.Repeat(source, times)) : source;
}

public static bool IsValidEncoding(this Encoding encoding, string name)
{
try
{
_ = Encoding.GetEncoding(name);
return true;
}
catch (ArgumentException)
{
return false;
}
}
}
}
111 changes: 111 additions & 0 deletions JackTheEnumRipper/Core/Ini.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using JackTheEnumRipper.Models;

namespace JackTheEnumRipper.Core
{
public class Ini
{
#region Private Fields

private readonly List<Section> _sections = [];

#endregion

public Ini(IEnumerable<Section>? sections = null, string? comment = null, char? commentSymbol = null, StringComparison? comparisonType = null)
{
this.Comment = comment ?? string.Empty;
this.CommentSymbol = commentSymbol ?? ';';
this.ComparisonType = comparisonType ?? StringComparison.InvariantCultureIgnoreCase;

if (sections != null)
{
foreach (Section section in sections)
{
this.AddSection(section);
}
}
}

#region Properties

public string Comment { get; set; }

public char CommentSymbol { get; private set; }

public StringComparison ComparisonType { get; private set; }

#endregion

#region Methods

public void AddSection(Section section)
{
if (this._sections.Exists(s => string.Equals(s.Name, section.Name, ComparisonType)))
throw new ArgumentException($"Duplicated section: {section.Name}");

this._sections.Add(section);
}

public Section? GetSection(string name)
{
return this._sections.First(section => string.Equals(name, section.Name, this.ComparisonType));
}

public bool RemoveSection(string name)
{
Section section = this._sections.FirstOrDefault(section => string.Equals(name, section.Name, this.ComparisonType));

if (section == default) return false;

return this._sections.Remove(section);
}

public IEnumerable<string> GetSections()
{
return this._sections.Select(section => section.Name);
}

private void Serialize(StringBuilder builder, Section section)
{
if (!section.IsGlobal)
{
if (!string.IsNullOrEmpty(section.Comment))
builder.AppendLine($"{this.CommentSymbol} {section.Comment}");

builder.AppendLine($"[{section.Name}]");
}

foreach (Setting setting in section.Settings)
{
if (!string.IsNullOrEmpty(setting.Comment))
builder.AppendLine($"{this.CommentSymbol} {setting.Comment}");

builder.AppendLine($"{setting.Name} = {setting.Value}");
}

builder.AppendLine(string.Empty);
}

public override string ToString()
{
StringBuilder builder = new();

var sections = this._sections.OrderBy(x => x.IsGlobal);

if (!string.IsNullOrEmpty(this.Comment))
builder.AppendLine($"{this.CommentSymbol} {this.Comment} {Environment.NewLine}");

foreach (Section section in sections)
{
this.Serialize(builder, section);
}

return builder.ToString();
}

#endregion
}
}
56 changes: 56 additions & 0 deletions JackTheEnumRipper/Core/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.Hosting;

namespace JackTheEnumRipper.Core
{
public static class Project
{
public static Assembly Assembly { get; } = Assembly.GetExecutingAssembly();

public static string BasePath { get; } = Directory.GetCurrentDirectory();

public static string CompileTimeEnvironment
{
get
{
#if DEBUG
return Environments.Development;
#else
return Environments.Production;
#endif
}
}

public static string Name
{
get
{
return Assembly.GetName().Name ?? string.Empty;
}
}

public static string Description
{
get
{
return Assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description ?? string.Empty;
}
}

public static string Version
{
get
{
return Assembly.GetName().Version?.ToString() ?? string.Empty;
}
}
}
}
80 changes: 80 additions & 0 deletions JackTheEnumRipper/Core/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text;

using JackTheEnumRipper.Factories;
using JackTheEnumRipper.Interfaces;
using JackTheEnumRipper.Models;
using JackTheEnumRipper.Serializer;
using JackTheEnumRipper.Services;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using NLog.Extensions.Logging;

using Serializer;

using JsonSerializer = Serializer.JsonSerializer;

namespace JackTheEnumRipper.Core
{
public static class Startup
{
public static IConfigurationBuilder ConfigureAppBuilder(this IConfigurationBuilder builder, IHostEnvironment environment)
{
return builder.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
}

public static IServiceCollection ConfigureAppServices(this IServiceCollection services, IHostEnvironment envionment, IConfigurationRoot configurationRoot)
{
services.AddSingleton(envionment);

services
.AddOptions<AppSettings>()
.Bind(configurationRoot.GetRequiredSection(nameof(AppSettings)))
.Validate(option =>
{
if (!Encoding.Default.IsValidEncoding(option.Encoding))
{
return false;
}

return true;
})
.ValidateOnStart();

services.AddLogging(logBuilder =>
{
logBuilder.ClearProviders();
logBuilder.SetMinimumLevel(string.Equals(envionment.EnvironmentName, Environments.Development) ? LogLevel.Debug : LogLevel.Error);
logBuilder.AddNLog();
});

services.AddSerializerFactory();
services.AddSingleton<ISerializerService, SerializerService>();
services.AddSingleton<IExtractorService, ExtractorService>();

return services;
}

public static void AddSerializerFactory(this IServiceCollection services)
{
services.AddTransient<ISerializer, CSharpSerializer>();
services.AddTransient<ISerializer, IniSerializer>();
services.AddTransient<ISerializer, JsonSerializer>();
services.AddTransient<ISerializer, PhpSerializer>();
services.AddTransient<ISerializer, RustSerializer>();
services.AddTransient<ISerializer, PythonSerializer>();

services.AddSingleton<Func<IEnumerable<ISerializer>>>(x => () => x.GetService<IEnumerable<ISerializer>>()!);

services.AddSingleton<ISerializerFactory, SerializerFactory>();
}
}
}
39 changes: 39 additions & 0 deletions JackTheEnumRipper/Core/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;

using JackTheEnumRipper.Models;

using Mono.Cecil;

namespace JackTheEnumRipper.Core
{
public class Utils
{
public static IEnumerable<AbstractEnum> ParseEnum(IEnumerable<TypeDefinition> types)
{
return types.Select(x => new AbstractEnum
{
Namespace = x.Namespace,
IsPublic = x.IsPublic,
Name = x.Name,
Type = x.Fields.Select(y => y.FieldType.Name).First(),
Fields = x.Fields.Skip(1).Select(y => new AbstractField { Name = y.Name, Value = y.Constant })
});
}

public static string GetExtension(Format format)
{
return format switch
{
Format.CSharp => ".cs",
Format.Ini => ".ini",
Format.Json => ".json",
Format.Php => ".php",
Format.Rust => ".rs",
Format.Python => ".py",
_ => throw new NotImplementedException(),
};
}
}
}
Loading