Skip to content

Commit 37f7103

Browse files
committed
Add project files.
1 parent c52ded2 commit 37f7103

17 files changed

Lines changed: 483 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\AddonObfuscator.Core\AddonObfuscator.Core.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

AddonObfuscator.Cli/Program.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using AddonObfuscator.Core;
2+
using System;
3+
using System.Diagnostics;
4+
5+
namespace AddonObfuscator.Cli
6+
{
7+
internal class Program
8+
{
9+
private static void Main()
10+
{
11+
Console.Title = "Add-on Obfuscator";
12+
13+
string? Input(string message)
14+
{
15+
Console.WriteLine(message);
16+
return Console.ReadLine();
17+
}
18+
19+
var source = Input("Enter source folder path: ");
20+
var target = Input("Enter target folder path: ");
21+
var formatting = Input("Enter formatting type (0: Default, 1: Minify): ");
22+
23+
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target) || string.IsNullOrEmpty(formatting))
24+
{
25+
Input("Invalid input.");
26+
return;
27+
}
28+
29+
if (int.TryParse(formatting, out var format) && format is 0 or 1)
30+
{
31+
var obfuscator = new Obfuscator(source, target, (Formatting)format);
32+
var watch = new Stopwatch();
33+
watch.Start();
34+
obfuscator.Run();
35+
watch.Stop();
36+
37+
Console.WriteLine($"Finished. Process took {watch.Elapsed}");
38+
Input("Press any key to exit...");
39+
return;
40+
}
41+
42+
Input("Invalid input.");
43+
}
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
</Project>

AddonObfuscator.Core/Formatting.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AddonObfuscator.Core
2+
{
3+
public enum Formatting
4+
{
5+
Default,
6+
Minify
7+
}
8+
}

AddonObfuscator.Core/Obfuscator.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.IO;
2+
using System.Text;
3+
using System.Text.Json;
4+
using System.Text.RegularExpressions;
5+
6+
namespace AddonObfuscator.Core
7+
{
8+
public class Obfuscator
9+
{
10+
private readonly string source;
11+
private readonly string target;
12+
private readonly Formatting formatting;
13+
14+
public Obfuscator(string source, string target, Formatting formatting = Formatting.Default)
15+
{
16+
this.source = source;
17+
this.target = target;
18+
this.formatting = formatting;
19+
}
20+
21+
public void Run()
22+
{
23+
foreach (var filePath in Directory.GetFiles(source, "*.*", SearchOption.AllDirectories))
24+
{
25+
var directory = Path.GetDirectoryName(filePath);
26+
if (string.IsNullOrEmpty(directory))
27+
return;
28+
29+
var newDirectory = Path.Combine(target, Path.GetRelativePath(source, directory));
30+
Directory.CreateDirectory(newDirectory);
31+
32+
var newFilePath = Path.Combine(newDirectory, Path.GetFileName(filePath));
33+
34+
if (Path.GetExtension(newFilePath) == ".json" && Path.GetFileNameWithoutExtension(newFilePath) != "manifest")
35+
File.WriteAllText(newFilePath, Obfuscate(ApplyFormatting(File.ReadAllText(filePath))));
36+
else
37+
File.WriteAllText(newFilePath, File.ReadAllText(filePath));
38+
}
39+
}
40+
41+
private string Obfuscate(string content)
42+
{
43+
return Regex.Replace(content, "(\"(?:.*?)\")", (match) =>
44+
{
45+
var stringBuilder = new StringBuilder();
46+
var escape = false;
47+
48+
foreach (var character in match.Value)
49+
{
50+
stringBuilder.Append(character != '"' || escape ? $"\\u{(ushort)character:X4}" : '"');
51+
escape = character == '\\';
52+
}
53+
54+
return stringBuilder.ToString();
55+
}, RegexOptions.Compiled);
56+
}
57+
58+
private string ApplyFormatting(string content)
59+
{
60+
switch (formatting)
61+
{
62+
case Formatting.Minify:
63+
{
64+
var options = new JsonSerializerOptions()
65+
{
66+
ReadCommentHandling = JsonCommentHandling.Skip,
67+
WriteIndented = false
68+
};
69+
70+
var json = JsonSerializer.Deserialize<object>(content, options);
71+
return JsonSerializer.Serialize(json, options);
72+
}
73+
74+
default:
75+
return content;
76+
}
77+
}
78+
}
79+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net5.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWPF>true</UseWPF>
8+
9+
<RootNamespace>AddonObfuscator.Wpf</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="HandyControls" Version="3.3.8" />
14+
<PackageReference Include="Ookii.Dialogs.Wpf" Version="4.0.0" />
15+
<PackageReference Include="Stylet" Version="1.3.6.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\AddonObfuscator.Core\AddonObfuscator.Core.csproj" />
20+
</ItemGroup>
21+
</Project>

AddonObfuscator.Wpf/App.xaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Application x:Class="AddonObfuscator.Wpf.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:hc="https://handyorg.github.io/handycontrol"
5+
xmlns:local="clr-namespace:AddonObfuscator.Wpf"
6+
xmlns:s="https://github.com/canton7/Stylet">
7+
8+
<Application.Resources>
9+
<ResourceDictionary>
10+
<Style TargetType="TextBlock" x:Key="SmallTitle">
11+
<Setter Property="FontSize" Value="13"/>
12+
<Setter Property="Margin" Value="7 7 0 5"/>
13+
</Style>
14+
15+
<ResourceDictionary.MergedDictionaries>
16+
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
17+
<hc:ThemeResources UsingSystemTheme="True"/>
18+
<hc:Theme/>
19+
20+
<s:ApplicationLoader>
21+
<s:ApplicationLoader.Bootstrapper>
22+
<local:Bootstrapper/>
23+
</s:ApplicationLoader.Bootstrapper>
24+
</s:ApplicationLoader>
25+
</ResourceDictionary.MergedDictionaries>
26+
</ResourceDictionary>
27+
</Application.Resources>
28+
</Application>

AddonObfuscator.Wpf/App.xaml.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Windows;
2+
3+
namespace AddonObfuscator.Wpf
4+
{
5+
/// <summary>
6+
/// Interaction logic for App.xaml
7+
/// </summary>
8+
public partial class App : Application
9+
{
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using AddonObfuscator.Wpf.Pages;
2+
using Stylet;
3+
using StyletIoC;
4+
5+
namespace AddonObfuscator.Wpf
6+
{
7+
public class Bootstrapper : Bootstrapper<ShellViewModel>
8+
{
9+
protected override void ConfigureIoC(IStyletIoCBuilder builder)
10+
{
11+
// Configure the IoC container in here
12+
}
13+
14+
protected override void Configure()
15+
{
16+
// Perform any other configuration before the application starts
17+
}
18+
}
19+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<hc:Window x:Class="AddonObfuscator.Wpf.Pages.ShellView"
2+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:cs="clr-namespace:AddonObfuscator.Wpf.Pages.UserControls"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:local="clr-namespace:AddonObfuscator.Wpf.Pages"
8+
xmlns:hc="https://handyorg.github.io/handycontrol"
9+
xmlns:s="https://github.com/canton7/Stylet"
10+
11+
ResizeMode="NoResize" mc:Ignorable="d"
12+
d:DataContext="{d:DesignInstance local:ShellViewModel}"
13+
Title="Add-on Obfuscator" Height="575" Width="425">
14+
15+
<Grid Margin="10 0 10 0">
16+
<Grid.RowDefinitions>
17+
<RowDefinition Height="0.175*"/>
18+
<RowDefinition Height="0.175*"/>
19+
<RowDefinition Height="*"/>
20+
<RowDefinition Height="0.175*"/>
21+
</Grid.RowDefinitions>
22+
23+
<cs:LocatePath Title="Source Folder Path" Path="{Binding SourcePath}" Click="{s:Action LocateSourcePath}" Grid.Row="0"/>
24+
25+
<cs:LocatePath Title="Target Folder Path" Path="{Binding TargetPath}" Click="{s:Action LocateTargetPath}" Grid.Row="1"/>
26+
27+
<Grid Grid.Row="2">
28+
<Grid.RowDefinitions>
29+
<RowDefinition Height="Auto"/>
30+
<RowDefinition Height="*"/>
31+
</Grid.RowDefinitions>
32+
33+
<TextBlock Text="Formatting Options" Style="{StaticResource SmallTitle}" Grid.Row="0"/>
34+
35+
<StackPanel Grid.Row="1">
36+
<StackPanel.Resources>
37+
<Style TargetType="RadioButton" BasedOn="{StaticResource RadioButtonBaseStyle}">
38+
<Setter Property="HorizontalAlignment" Value="Left"/>
39+
<Setter Property="Margin" Value="0 5"/>
40+
</Style>
41+
</StackPanel.Resources>
42+
43+
<RadioButton Content="Default" IsChecked="{Binding IsDefaultChecked}"/>
44+
<RadioButton Content="Minify" IsChecked="{Binding IsDefaultChecked, Converter={StaticResource Boolean2BooleanReConverter}}"/>
45+
</StackPanel>
46+
</Grid>
47+
48+
<StackPanel Orientation="Horizontal" Grid.Row="3">
49+
<Button Content="Run" Click="{s:Action RunObfuscator}" HorizontalAlignment="Left"/>
50+
51+
<TextBlock Text="Invalid input." VerticalAlignment="Center" Margin="5 0" Foreground="#ff2647"
52+
Visibility="{Binding IsInvalidInput, Converter={StaticResource Boolean2VisibilityConverter}}"/>
53+
</StackPanel>
54+
</Grid>
55+
</hc:Window>

0 commit comments

Comments
 (0)