Skip to content

Commit

Permalink
Workaround IL2CPP limitation (no Process.Start)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwe committed Apr 10, 2020
1 parent 1d85e1d commit 6574be7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
86 changes: 86 additions & 0 deletions Assets/Scripts/ViewModels/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace StlVault.ViewModels
{
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")]
[SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Local")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "IdentifierTypo")]
internal static class NativeMethods
{
public static void BrowseTo(string filePath)
{
const uint normalPriorityClass = 0x0020;

var application = Environment.GetEnvironmentVariable("windir") + @"\explorer.exe";
var commandLine = $"/e, /select, \"{filePath}\"";
var sInfo = new STARTUPINFO();
var pSec = new SECURITY_ATTRIBUTES();
var tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);

//Open Explorer at location
CreateProcess(application, commandLine,
ref pSec, ref tSec, false, normalPriorityClass,
IntPtr.Zero, null, ref sInfo, out _);
}

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

// This also works with CharSet.Ansi as long as the calling function uses the same character set.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
public int nLength;
public unsafe byte* lpSecurityDescriptor;
public int bInheritHandle;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/ViewModels/NativeMethods.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions Assets/Scripts/ViewModels/UserFeedbackModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,29 @@ private void CreateArchive()

private static void ShowInExplorer(string zipPath)
{
Process.Start(new ProcessStartInfo
{
FileName = "explorer",
Arguments = $"/e, /select, \"{zipPath}\""
});
NativeMethods.BrowseTo(zipPath);
}

private static void Add(ZipArchive archive, string folder, string fileName)
{
var filePath = Path.Combine(folder, fileName);
if (!File.Exists(filePath)) return;
var sourceFilePath = Path.Combine(folder, fileName);
if (!File.Exists(sourceFilePath)) return;

archive.CreateEntryFromFile(filePath, fileName);
using (var stream = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var zipArchiveEntry = archive.CreateEntry(fileName);
var dateTime = File.GetLastWriteTime(sourceFilePath);
if (dateTime.Year < 1980 || dateTime.Year > 2107)
{
dateTime = new DateTime(1980, 1, 1, 0, 0, 0);
}

zipArchiveEntry.LastWriteTime = dateTime;
using (var targetStream = zipArchiveEntry.Open())
{
stream.CopyTo(targetStream);
}
}
}

protected override void OnAccept()
Expand Down

0 comments on commit 6574be7

Please sign in to comment.