Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Config System #2

Merged
merged 12 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .github/release-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Added
- Configuration system
12 changes: 6 additions & 6 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-20.04, windows-latest ]
framework: [ '7.0' ]
framework: [ '8.0' ]
include:
- os: ubuntu-20.04
target: linux-x64
Expand Down Expand Up @@ -63,6 +63,7 @@ jobs:
name: Release
needs: build
runs-on: ubuntu-20.04
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -71,15 +72,14 @@ jobs:
- name: Zip artifacts
run: |
mkdir ./zipped
zip -r ./zipped/${{ env.PROJECT_NAME }}-all.zip ./${{ env.PROJECT_NAME }}-all-7.0
zip -r ./zipped/${{ env.PROJECT_NAME }}-linux-x64.zip ./${{ env.PROJECT_NAME }}-linux-x64-7.0
zip -r ./zipped/${{ env.PROJECT_NAME }}-win-x64.zip ./${{ env.PROJECT_NAME }}-win-x64-7.0
zip -r ./zipped/${{ env.PROJECT_NAME }}-all.zip ./${{ env.PROJECT_NAME }}-all-8.0
zip -r ./zipped/${{ env.PROJECT_NAME }}-linux.zip ./${{ env.PROJECT_NAME }}-linux-x64-8.0
zip -r ./zipped/${{ env.PROJECT_NAME }}-win.zip ./${{ env.PROJECT_NAME }}-win-x64-8.0
- name: Release All
uses: ncipollo/release-action@v1
if: startsWith(github.ref, 'refs/tags/')
with:
artifacts: 'zipped/${{ env.PROJECT_NAME }}-*.zip'
bodyFile: "CHANGELOG.md"
bodyFile: ".github/release-body.md"
commit: ${{ github.sha }}
draft: false
prerelease: false
10 changes: 0 additions & 10 deletions CHANGELOG.md

This file was deleted.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# SecretLabDependenciesBuilder

An all in one publicizer and assembly downloader for the game scpsl, made for modding purposes.

## Configuration Options

| Option | Type | Description | Default Value |
|------------------------|----------|---------------------------------------------------------|----------------------------------------------------------|
| AssembliesToPublicize | String[] | Array specifying assemblies to publicize | `["Assembly-CSharp.dll", "Mirror.dll", "PluginAPI.dll"]` |
| ZipReferences | Boolean | Flag to indicate whether to zip references | `true` |
| ReferencesZipName | String | Name/path of the zip file for references | `"References.zip"` |
| SaveReferencesToFolder | Boolean | Flag to indicate whether to save references to a folder | `false` |
| ReferencesFolderName | String | Name/path of the directory to save references to | `"References"` |

https://github.com/Jesus-QC/SecretLabDependenciesBuilder/assets/69375249/9fff440e-924a-4cf2-b4f9-7e2ef63ab3e6
64 changes: 42 additions & 22 deletions SecretLabDependenciesBuilder/AssembliesPublicizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,65 @@ namespace SecretLabDependenciesBuilder;

public static class AssembliesPublicizer
{
private static readonly string[] AssembliesToPublicize =
{
"Assembly-CSharp.dll",
"Mirror.dll",
"PluginAPI.dll",
};

public static void RunPublicizer(DirectoryInfo directoryInfo)
{
foreach (FileInfo file in directoryInfo.GetFiles())
foreach (FileInfo file in directoryInfo.GetFiles("*.dll"))
{
if (!AssembliesToPublicize.Contains(file.Name))
if (!ConfigManager.CurrentConfig.AssembliesToPublicize.Contains(file.Name))
continue;

PublicizeAssembly(file, directoryInfo);
}

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Done!");
ConsoleWriter.Write("Done", ConsoleColor.Green);

if (ConfigManager.CurrentConfig.SaveReferencesToFolder)
SaveReferencesToFolder(directoryInfo);

if (ConfigManager.CurrentConfig.ZipReferences)
SaveReferencesToZip(directoryInfo);
}

private static void SaveReferencesToFolder(DirectoryInfo directoryInfo)
{
DirectoryInfo referencesDirectory = new DirectoryInfo(ConfigManager.CurrentConfig.ReferencesFolderName);
if (!referencesDirectory.Exists)
referencesDirectory.Create();

foreach (FileInfo file in directoryInfo.GetFiles())
{
file.CopyTo(Path.Combine(referencesDirectory.FullName, file.Name), true);
}
}

private static void SaveReferencesToZip(DirectoryInfo directoryInfo)
{
string zipFile = ConfigManager.CurrentConfig.ReferencesZipName;
if (File.Exists(zipFile))
File.Delete(zipFile);

// create directory if it doesn't exist
new FileInfo(zipFile).Directory?.Create();


string zipFile = Path.Combine(Environment.CurrentDirectory, "References.zip");
File.Delete(zipFile);
ZipFile.CreateFromDirectory(directoryInfo.FullName, zipFile);
}

private static void PublicizeAssembly(FileSystemInfo file, FileSystemInfo referencesDirectory)
{
using DefaultAssemblyResolver resolver = new ();
using DefaultAssemblyResolver resolver = new();
resolver.AddSearchDirectory(referencesDirectory.FullName);

using ModuleDefinition assembly = ModuleDefinition.ReadModule(file.FullName, new ReaderParameters
{
AssemblyResolver = resolver,
ReadWrite = true
});

if (assembly is null)
{
Console.WriteLine($"[Publicizer] Assembly in {file.FullName} not found. Could not patch.");
ConsoleWriter.Write($"[Publicizer] Assembly in {file.FullName} not found. Could not patch.",
ConsoleColor.Red);
return;
}

Expand All @@ -54,15 +74,15 @@ private static void PublicizeAssembly(FileSystemInfo file, FileSystemInfo refere

foreach (FieldDefinition field in type.Fields)
field.IsPublic = true;

foreach (MethodDefinition method in type.Methods)
method.IsPublic = true;

foreach (PropertyDefinition property in type.Properties)
{
if (property.GetMethod != null)
property.GetMethod.IsPublic = true;

if (property.SetMethod != null)
property.SetMethod.IsPublic = true;
}
Expand All @@ -71,12 +91,12 @@ private static void PublicizeAssembly(FileSystemInfo file, FileSystemInfo refere
{
eventDefinition.AddMethod.IsPublic = true;
eventDefinition.RemoveMethod.IsPublic = true;

if (eventDefinition.InvokeMethod != null)
eventDefinition.InvokeMethod.IsPublic = true;
}
}

assembly.Write(file.FullName[..^4] + "-Publicized.dll");
}
}
57 changes: 57 additions & 0 deletions SecretLabDependenciesBuilder/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.ComponentModel;
using YamlDotNet.Serialization;

namespace SecretLabDependenciesBuilder;

public static class ConfigManager
{
public static Config CurrentConfig { get; private set; } = new Config();

public static async Task LoadConfigAsync()
{
CurrentConfig = await GetConfigAsync();
}

private static async Task<Config> GetConfigAsync()
{
string configPath = Path.Combine(Environment.CurrentDirectory, "config.yml");
if (!File.Exists(configPath))
{
ConsoleWriter.Write("Config file not found. Creating one...", ConsoleColor.Yellow);

await File.WriteAllTextAsync(configPath, new SerializerBuilder().Build().Serialize(new Config()));

ConsoleWriter.Write("Config file created. Please fill it with the desired data.", ConsoleColor.Yellow);
ConsoleWriter.Write("Press any key to continue...", ConsoleColor.Yellow);
Console.ReadKey();
Console.Clear();

// ReSharper disable once TailRecursiveCall
return await GetConfigAsync();
}

return new DeserializerBuilder().Build().Deserialize<Config>(await File.ReadAllTextAsync(configPath))!;
}
}

public class Config
{
public string[] AssembliesToPublicize { get; set; } = new string[3]
{
"Assembly-CSharp.dll",
"Mirror.dll",
"PluginAPI.dll",
};

[Description("Whether to zip the references or not.")]
public bool ZipReferences { get; set; } = true;

[Description("The name and/or path of the zip file.")]
public string ReferencesZipName { get; set; } = "References.zip";

[Description("Whether to save the references to a folder or not.")]
public bool SaveReferencesToFolder { get; set; } = false;

[Description("The directory to save the references to.")]
public string ReferencesFolderName { get; set; } = "References";
}
26 changes: 26 additions & 0 deletions SecretLabDependenciesBuilder/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace SecretLabDependenciesBuilder;

public static class ConsoleWriter
{
public static void Write(string? message, ConsoleColor color = ConsoleColor.White)
{
if (message is null)
return;
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.White;
}

public static void WriteTitle()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("""
███████████████████████████████████████
█─▄▄▄▄█▄─▄█████▄─▄▄▀█▄─▄▄─█▄─▄▄─█─▄▄▄▄█
█▄▄▄▄─██─██▀████─██─██─▄█▀██─▄▄▄█▄▄▄▄─█
▀▄▄▄▄▄▀▄▄▄▄▄▀▀▀▄▄▄▄▀▀▄▄▄▄▄▀▄▄▄▀▀▀▄▄▄▄▄▀
""");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Made by Jesus-QC and x3rt");
}
}
14 changes: 5 additions & 9 deletions SecretLabDependenciesBuilder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// All in one - Server Downloader and Publicizer.
// All in one - Server Downloader and Publicizer.

Console.WriteLine("""
███████████████████████████████████████
█─▄▄▄▄█▄─▄█████▄─▄▄▀█▄─▄▄─█▄─▄▄─█─▄▄▄▄█
█▄▄▄▄─██─██▀████─██─██─▄█▀██─▄▄▄█▄▄▄▄─█
▀▄▄▄▄▄▀▄▄▄▄▄▀▀▀▄▄▄▄▀▀▄▄▄▄▄▀▄▄▄▀▀▀▄▄▄▄▄▀
Made by Jesus-QC and x3rt
""");
using SecretLabDependenciesBuilder;

await SecretLabDependenciesBuilder.ServerDownloader.RunAsync();
ConsoleWriter.WriteTitle();
await ConfigManager.LoadConfigAsync();
await ServerDownloader.RunAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Optimize>true</Optimize>
<AssemblyVersion>1.1.0</AssemblyVersion>
<AssemblyVersion>1.2.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -26,6 +26,7 @@

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.5"/>
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

</Project>
Loading