Skip to content
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
34 changes: 11 additions & 23 deletions Runtimes/NET/BepisLoader/BepisLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
Expand All @@ -11,10 +10,11 @@ public class BepisLoader
internal static AssemblyLoadContext alc = null!;
static void Main(string[] args)
{
resoDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#if DEBUG
File.WriteAllText("BepisLoader.log", "BepisLoader started\n");
logPath = Path.Combine(resoDir, "BepisLoader.log");
File.WriteAllText(logPath, "BepisLoader started\n");
#endif
resoDir = Directory.GetCurrentDirectory();

alc = new BepisLoadContext();

Expand All @@ -31,20 +31,6 @@ static void Main(string[] args)

var asm = alc.LoadFromAssemblyPath(Path.Combine(bepinPath, "core", "BepInEx.NET.CoreCLR.dll"));

// Check if we're launching from outside the game directory
var exePath = Process.GetCurrentProcess().MainModule?.FileName;
if (exePath != null)
{
var exeDir = Path.GetDirectoryName(exePath);
if (exeDir != resoDir)
{
// Change working directory to the game directory for direct launches
Log($"Changing working directory from {resoDir} to {exeDir}");
Directory.SetCurrentDirectory(exeDir);
resoDir = exeDir;
}
}

var resoDllPath = Path.Combine(resoDir, "Renderite.Host.dll");
if (!File.Exists(resoDllPath)) resoDllPath = Path.Combine(resoDir, "Resonite.dll");

Expand All @@ -54,18 +40,19 @@ static void Main(string[] args)

// Find and load Resonite
var resoAsm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == "Renderite.Host");
if (resoAsm == null && File.Exists(resoDllPath))
{
resoAsm = alc.LoadFromAssemblyPath(resoDllPath);
}

try
{
if (resoAsm == null)
{
resoAsm = alc.LoadFromAssemblyPath(resoDllPath);
}
var result = resoAsm.EntryPoint!.Invoke(null, [args]);
if (result is Task task) task.Wait();
}
catch (Exception e)
{
File.WriteAllLines("BepisCrash.log", [DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - Resonite crashed", e.ToString()]);
File.WriteAllLines(Path.Combine(resoDir, "BepisCrash.log"), [DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - Resonite crashed", e.ToString()]);
}
}

Expand Down Expand Up @@ -167,14 +154,15 @@ private static string GetUnmanagedLibraryName(string name)
}

#if DEBUG
private static string logPath;
private static object _lock = new object();
#endif
public static void Log(string message)
{
#if DEBUG
lock (_lock)
{
File.AppendAllLines("BepisLoader.log", [message]);
File.AppendAllLines(logPath, [message]);
}
#endif
}
Expand Down
73 changes: 69 additions & 4 deletions build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using Cake.Common;
using Cake.Common.IO;
Expand Down Expand Up @@ -573,10 +574,74 @@ public override void Run(BuildContext ctx)
}
}

[TaskName("FixThunderstoreLinuxPermissions")]
[IsDependentOn(typeof(BuildThunderstorePackageTask))]
[SupportedOSPlatform("linux")]
public sealed class FixThunderstoreLinuxPermissionsTask : FrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext ctx) =>
ctx.Distributions.Any(d => d.Runtime == "BepisLoader") &&
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);

[SupportedOSPlatform("linux")]
public override void Run(BuildContext ctx)
{
ctx.Log.Information("Fixing Linux permissions in Thunderstore package...");

var thunderstorePackageDir = ctx.DistributionDirectory.Combine("thunderstore-package");
var packageFiles = ctx.GetFiles(thunderstorePackageDir.Combine("*.zip").FullPath);

foreach (var packageFile in packageFiles)
{
ctx.Log.Information($"Fixing Linux permissions in {packageFile.GetFilename()}...");

var tempDir = ctx.DistributionDirectory.Combine("temp-thunderstore-fix");
if (ctx.DirectoryExists(tempDir))
{
ctx.CleanDirectory(tempDir);
}
else
{
ctx.CreateDirectory(tempDir);
}

System.IO.Compression.ZipFile.ExtractToDirectory(packageFile.FullPath, tempDir.FullPath);

var executablePermissions = System.IO.UnixFileMode.UserExecute |
System.IO.UnixFileMode.GroupExecute |
System.IO.UnixFileMode.OtherExecute;

var executableFiles = new[] { "LinuxBootstrap.sh" };

foreach (var fileName in executableFiles)
{
var filePath = tempDir.CombineWithFilePath($"BepInExPack/{fileName}");
if (ctx.FileExists(filePath))
{
var fileInfo = new System.IO.FileInfo(filePath.FullPath);
var originalMode = fileInfo.UnixFileMode;
ctx.Log.Information($"Original permissions for {fileName}: {Convert.ToString((int)originalMode, 8).PadLeft(3, '0')} ({originalMode})");

fileInfo.UnixFileMode |= executablePermissions;

var newMode = fileInfo.UnixFileMode;
ctx.Log.Information($"New permissions for {fileName}: {Convert.ToString((int)newMode, 8).PadLeft(3, '0')} ({newMode})");
}
}

ctx.DeleteFile(packageFile);

System.IO.Compression.ZipFile.CreateFromDirectory(tempDir.FullPath, packageFile.FullPath);

ctx.Log.Information($"Fixed Linux permissions in {packageFile.GetFilename()}");
}
}
}

[TaskName("Publish")]
[IsDependentOn(typeof(MakeDistTask))]
[IsDependentOn(typeof(PushNuGetTask))]
[IsDependentOn(typeof(BuildThunderstorePackageTask))]
[IsDependentOn(typeof(FixThunderstoreLinuxPermissionsTask))]
public sealed class PublishTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext ctx)
Expand All @@ -587,9 +652,9 @@ public override void Run(BuildContext ctx)
{
var targetZipName = $"BepInEx-{dist.Target}-{ctx.BuildPackageVersion}.zip";
ctx.Log.Information($"Packing {targetZipName}");
ctx.Zip(ctx.DistributionDirectory.Combine(dist.Target),
ctx.DistributionDirectory
.CombineWithFilePath(targetZipName));
// https://github.com/cake-build/cake/issues/2592
System.IO.Compression.ZipFile.CreateFromDirectory(ctx.DistributionDirectory.Combine(dist.Target).FullPath,
ctx.DistributionDirectory.CombineWithFilePath(targetZipName).FullPath);
}


Expand Down
Loading