Skip to content

Commit 6574be7

Browse files
committed
Workaround IL2CPP limitation (no Process.Start)
1 parent 1d85e1d commit 6574be7

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.InteropServices;
4+
5+
namespace StlVault.ViewModels
6+
{
7+
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")]
8+
[SuppressMessage("ReSharper", "BuiltInTypeReferenceStyle")]
9+
[SuppressMessage("ReSharper", "MemberCanBePrivate.Local")]
10+
[SuppressMessage("ReSharper", "InconsistentNaming")]
11+
[SuppressMessage("ReSharper", "IdentifierTypo")]
12+
internal static class NativeMethods
13+
{
14+
public static void BrowseTo(string filePath)
15+
{
16+
const uint normalPriorityClass = 0x0020;
17+
18+
var application = Environment.GetEnvironmentVariable("windir") + @"\explorer.exe";
19+
var commandLine = $"/e, /select, \"{filePath}\"";
20+
var sInfo = new STARTUPINFO();
21+
var pSec = new SECURITY_ATTRIBUTES();
22+
var tSec = new SECURITY_ATTRIBUTES();
23+
pSec.nLength = Marshal.SizeOf(pSec);
24+
tSec.nLength = Marshal.SizeOf(tSec);
25+
26+
//Open Explorer at location
27+
CreateProcess(application, commandLine,
28+
ref pSec, ref tSec, false, normalPriorityClass,
29+
IntPtr.Zero, null, ref sInfo, out _);
30+
}
31+
32+
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
33+
static extern bool CreateProcess(
34+
string lpApplicationName,
35+
string lpCommandLine,
36+
ref SECURITY_ATTRIBUTES lpProcessAttributes,
37+
ref SECURITY_ATTRIBUTES lpThreadAttributes,
38+
bool bInheritHandles,
39+
uint dwCreationFlags,
40+
IntPtr lpEnvironment,
41+
string lpCurrentDirectory,
42+
[In] ref STARTUPINFO lpStartupInfo,
43+
out PROCESS_INFORMATION lpProcessInformation);
44+
45+
// This also works with CharSet.Ansi as long as the calling function uses the same character set.
46+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
47+
private struct STARTUPINFO
48+
{
49+
public Int32 cb;
50+
public string lpReserved;
51+
public string lpDesktop;
52+
public string lpTitle;
53+
public Int32 dwX;
54+
public Int32 dwY;
55+
public Int32 dwXSize;
56+
public Int32 dwYSize;
57+
public Int32 dwXCountChars;
58+
public Int32 dwYCountChars;
59+
public Int32 dwFillAttribute;
60+
public Int32 dwFlags;
61+
public Int16 wShowWindow;
62+
public Int16 cbReserved2;
63+
public IntPtr lpReserved2;
64+
public IntPtr hStdInput;
65+
public IntPtr hStdOutput;
66+
public IntPtr hStdError;
67+
}
68+
69+
[StructLayout(LayoutKind.Sequential)]
70+
private struct PROCESS_INFORMATION
71+
{
72+
public IntPtr hProcess;
73+
public IntPtr hThread;
74+
public int dwProcessId;
75+
public int dwThreadId;
76+
}
77+
78+
[StructLayout(LayoutKind.Sequential)]
79+
private struct SECURITY_ATTRIBUTES
80+
{
81+
public int nLength;
82+
public unsafe byte* lpSecurityDescriptor;
83+
public int bInheritHandle;
84+
}
85+
}
86+
}

Assets/Scripts/ViewModels/NativeMethods.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/ViewModels/UserFeedbackModel.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,29 @@ private void CreateArchive()
8181

8282
private static void ShowInExplorer(string zipPath)
8383
{
84-
Process.Start(new ProcessStartInfo
85-
{
86-
FileName = "explorer",
87-
Arguments = $"/e, /select, \"{zipPath}\""
88-
});
84+
NativeMethods.BrowseTo(zipPath);
8985
}
9086

9187
private static void Add(ZipArchive archive, string folder, string fileName)
9288
{
93-
var filePath = Path.Combine(folder, fileName);
94-
if (!File.Exists(filePath)) return;
89+
var sourceFilePath = Path.Combine(folder, fileName);
90+
if (!File.Exists(sourceFilePath)) return;
9591

96-
archive.CreateEntryFromFile(filePath, fileName);
92+
using (var stream = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
93+
{
94+
var zipArchiveEntry = archive.CreateEntry(fileName);
95+
var dateTime = File.GetLastWriteTime(sourceFilePath);
96+
if (dateTime.Year < 1980 || dateTime.Year > 2107)
97+
{
98+
dateTime = new DateTime(1980, 1, 1, 0, 0, 0);
99+
}
100+
101+
zipArchiveEntry.LastWriteTime = dateTime;
102+
using (var targetStream = zipArchiveEntry.Open())
103+
{
104+
stream.CopyTo(targetStream);
105+
}
106+
}
97107
}
98108

99109
protected override void OnAccept()

0 commit comments

Comments
 (0)