Skip to content
Draft
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
18 changes: 18 additions & 0 deletions SetupDevEnvironment/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SetupDevEnvironment.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<SetupDevEnvironment.Properties.Settings>
<setting name="ValheimInstallDir" serializeAs="String">
<value />
</setting>
<setting name="ValheimPlusDevInstallDir" serializeAs="String">
<value />
</setting>
</SetupDevEnvironment.Properties.Settings>
</userSettings>
</configuration>
112 changes: 112 additions & 0 deletions SetupDevEnvironment/IO/AssemblyPublicizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//elliotttate/Bepinex-Tools

using dnlib.DotNet;
using System.Reflection;
using FieldAttributes = dnlib.DotNet.FieldAttributes;
using MethodAttributes = dnlib.DotNet.MethodAttributes;
using TypeAttributes = dnlib.DotNet.TypeAttributes;

namespace SetupDevEnvironment.IO;

internal class AssemblyPublicizer
{
public AssemblyPublicizer()
{
Logger.Start();
}

public static void ProcessAssemblies(string[] files)
{
foreach (var file in files)
{
try
{
var assembly = Assembly.LoadFile(file);
ProcessAssembly(assembly);
} catch(Exception ex)
{
Logger.Log(ex.Message);
}
}
}

private static void ProcessAssembly(Assembly assembly)
{
string assemblyPath = assembly.Location;
string filename = assembly.GetName().Name!;
string outputPath = Path.Combine(Path.GetDirectoryName(assemblyPath)!, "publicized_assemblies");
string outputSuffix = "_publicized";

Directory.CreateDirectory(outputPath);

string curHash = ComputeHash(assembly);

string hashPath = Path.Combine(outputPath, $"{filename}{outputSuffix}.hash");
string lastHash = string.Empty;

if (File.Exists(hashPath))
lastHash = File.ReadAllText(hashPath);

if (curHash == lastHash)
return;

Logger.Log($"Making a public assembly from {filename}");
RewriteAssembly(assemblyPath).Write($"{Path.Combine(outputPath, filename)}{outputSuffix}.dll");
File.WriteAllText(hashPath, curHash);
}

private static string ComputeHash(Assembly assembly)
{
return assembly.ManifestModule.ModuleVersionId.ToString();
}

private static ModuleDef RewriteAssembly(string assemblyPath)
{
var assembly = ModuleDefMD.Load(assemblyPath);

var types = assembly.GetTypes();
Logger.Log($"{assembly.Name}: {types.Count()} types");

foreach (var type in types)
{
MakeTypePublic(type);
MakeMethodsPublic(type);
MakeFieldsPublic(type);
}

return assembly;
}

private static void MakeFieldsPublic(TypeDef type)
{
var eventNames = type.Events
.Select(ev => ev.Name.ToString()).ToList();

var fields = type.Fields
.Where(x => !eventNames.Contains(x.Name)).ToArray();

foreach (FieldDef field in fields)
{
field.Attributes &= ~FieldAttributes.FieldAccessMask;
field.Attributes |= FieldAttributes.Public;
}
}

private static void MakeTypePublic(TypeDef type)
{
type.Attributes &= ~TypeAttributes.VisibilityMask;
if (type.IsNested)
type.Attributes |= TypeAttributes.NestedPublic;
else
type.Attributes |= TypeAttributes.Public;
}

private static void MakeMethodsPublic(TypeDef type)
{
foreach (MethodDef method in type.Methods)
{
method.Attributes &= ~MethodAttributes.MemberAccessMask;
method.Attributes |= MethodAttributes.Public;
}
}
}
13 changes: 13 additions & 0 deletions SetupDevEnvironment/IO/DirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SetupDevEnvironment.IO
{
public static class DirectoryHelper
{
public static string CreateTempFolder()
{
var tempFolder = Path.GetTempPath();
var folder = Path.Combine(tempFolder, Path.GetRandomFileName());
Directory.CreateDirectory(folder);
return folder;
}
}
}
17 changes: 17 additions & 0 deletions SetupDevEnvironment/IO/Downloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SetupDevEnvironment.IO;

internal partial class Downloader
{

public static async Task<string> Download(string url, string? fileName = null)
{
var tempFile = fileName ?? Path.GetTempFileName();

using (var client = new HttpClient())
{
var data = await client.GetByteArrayAsync(url);
await File.WriteAllBytesAsync(tempFile, data);
return tempFile;
}
}
}
34 changes: 34 additions & 0 deletions SetupDevEnvironment/IO/FileMover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using dnlib.DotNet;

namespace SetupDevEnvironment.IO
{
internal class FileCopier
{
public static void CopyFiles(string sourceFolder, string destinationFolder)
{
var sourceFiles = Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories);
CopyFiles(sourceFolder, sourceFiles, destinationFolder);
}

public static void CopyFiles(string sourceFolder, string[] sourceFiles, string destinationFolder)
{
Directory.CreateDirectory(destinationFolder);
foreach (string file in sourceFiles)
{
var relativeFile = Path.GetRelativePath(sourceFolder, file);
var destFile = Path.Combine(destinationFolder, relativeFile);

// we may need new subfolders
var folder = Path.GetDirectoryName(destFile);
Directory.CreateDirectory(folder!);

if (File.GetAttributes(file).HasFlag(System.IO.FileAttributes.Directory))
{
continue;
}

File.Copy(file, destFile, true);
}
}
}
}
29 changes: 29 additions & 0 deletions SetupDevEnvironment/IO/Links.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace SetupDevEnvironment.IO;

internal static class Links
{
internal static string DefaultValheimInstallFolder =
@"C:\Program Files (x86)\Steam\steamapps\common\Valheim";
internal static string DefaultValheimPlusDevInstallFolder =
@"C:\Program Files (x86)\Steam\steamapps\common\Valheim Plus Development";
internal static readonly string DnSpy64TargetFolder =
@"C:\Program Files\dnSpy64";

internal static readonly string BepInEx =
@"https://valheim.thunderstore.io/package/download/denikson/BepInExPack_Valheim/5.4.1900/";
internal static readonly string BepInEx21 =
@"https://github.com/BepInEx/BepInEx/releases/download/v5.4.21/BepInEx_x64_5.4.21.0.zip";

[Obsolete("This should be the correct source, but you can't run valheim with it", true)]
internal static readonly string Unstripped_Corlib =
@"https://cdn.discordapp.com/attachments/624272422295568435/841990037935882250/unstripped_corlib.7z";
internal static readonly string Unstripped_Corlib_ValheimPlusSource =
@"https://github.com/valheimPlus/ValheimPlus/releases/download/0.9.9.9/WindowsClient.zip";

internal static readonly string DnSpy64 =
@"https://github.com/dnSpy/dnSpy/releases/download/v6.1.8/dnSpy-net-win64.zip";

[Obsolete("we're doing this ourselves now", true)]
internal static readonly string Assemblyinternalizer =
@"https://github.com/elliotttate/Bepinex-Tools/releases/download/1.0.0-internalizer/Bepinex-internalizer.zip";
}
11 changes: 11 additions & 0 deletions SetupDevEnvironment/IO/LogEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SetupDevEnvironment.IO;

internal class LogEvent : EventArgs
{
public string Message { get; private set; }

public LogEvent(string message)
{
Message = message;
}
}
19 changes: 19 additions & 0 deletions SetupDevEnvironment/IO/ProcessRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Diagnostics;

namespace SetupDevEnvironment.IO
{
internal class ProcessRunner
{
internal static void Run(string path)
{
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = path,
};

process.Start();
}
}
}
18 changes: 18 additions & 0 deletions SetupDevEnvironment/IO/ResourceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SetupDevEnvironment.IO
{
#nullable disable
internal class ResourceHelper
{
public static string GetResource(string name)
{
var assembly = typeof(ResourceHelper).Assembly;
var resourceName = assembly.GetManifestResourceNames().Single(x => x.Contains(name));

using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
61 changes: 61 additions & 0 deletions SetupDevEnvironment/IO/Unzipper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using SharpCompress.Common;
using SharpCompress.Readers;
using System.IO.Compression;

namespace SetupDevEnvironment.IO
{
internal static class Unzipper
{
/// <summary>
/// returns all entries relative to the target folder
/// </summary>
/// <param name="filename"></param>
/// <param name="targetFolder"></param>
/// <returns></returns>
public static string[] Unzip(string filename, string? targetFolder = null)
{
targetFolder = targetFolder ?? DirectoryHelper.CreateTempFolder();
Directory.CreateDirectory(targetFolder);

var archive = SharpCompress.Archives.ArchiveFactory.Open(filename);
using (var reader = archive.ExtractAllEntries())
{
reader.WriteAllToDirectory(targetFolder, new ExtractionOptions { ExtractFullPath = true, Overwrite = true, PreserveFileTime = true });
}
return archive.Entries.Select(e => Path.Combine(targetFolder, e.Key)).ToArray();
}
// switch (Path.GetExtension(filename).ToLower())
// {
// case ".zip":
// return UnzipZip(filename, targetFolder);
// case ".7z":
// return Unzip7z(filename, targetFolder);
// }
//}

//private static string[] Unzip7z(string filename, string? targetFolder)
//{
// using (var zipFileStream = new SharpCompress.Archives.SevenZip.(filename))
// using (var zipfile = new ZipArchive(zipFileStream))
// {
// zipfile.ExtractToDirectory(targetFolder, true);

// return zipfile.Entries
// .Select(entry => Path.Combine(targetFolder, entry.FullName))
// .ToArray();
// }
//}

//public static string[] UnzipZip(string filename, string? targetFolder = null)
//{
// using (var zipFileStream = new FileStream(filename, FileMode.Open))
// using (var zipfile = new ZipArchive(zipFileStream))
// {
// zipfile.ExtractToDirectory(targetFolder, true);

// return zipfile.Entries
// .Select(entry => Path.Combine(targetFolder, entry.FullName))
// .ToArray();
// }
}
}
Loading