Skip to content

Commit

Permalink
use eo-lib and some refactoring (#5)
Browse files Browse the repository at this point in the history
* use eo-lib and some refactoring

* remove duplicate folder

* refactor
  • Loading branch information
MrDanOak authored Jun 13, 2024
1 parent f831994 commit a36e193
Show file tree
Hide file tree
Showing 50 changed files with 404 additions and 285 deletions.
File renamed without changes.
Binary file not shown.
36 changes: 0 additions & 36 deletions src/EndlessOnlinePatcher.sln

This file was deleted.

48 changes: 0 additions & 48 deletions src/EndlessOnlinePatcher/Core/ClientVersionFetcher.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/EndlessOnlinePatcher/Core/FileVersion.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/EndlessOnlinePatcher/Core/IClientVersionFetcher.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/EndlessOnlinePatcher/Core/IPatcher.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/EndlessOnlinePatcher/EndlessOnlinePatcher.csproj

This file was deleted.

14 changes: 14 additions & 0 deletions src/EoPatcher.Core/EoPatcher.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moffat.EndlessOnline.SDK" Version="1.0.0-rc2" />
<PackageReference Include="OneOf" Version="3.0.271" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EndlessOnlinePatcher.Extensions;
namespace EoPatcher.Extensions;
public static class HttpClientExtensions
{
public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
namespace EoPatcher.Extensions;

namespace EndlessOnlinePatcher.Extensions;
public static class StreamExtensions
{
public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, long contentLength, IProgress<int>? progress = null, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.InteropServices;
using EoPatcher.Models;

namespace EndlessOnlinePatcher.Core;
namespace EoPatcher.Interop;

public static class Windows
{
Expand All @@ -27,7 +28,7 @@ private static void RunAsDesktopUser(string fileName)
// 6. Make a primary token with that token(DuplicateTokenEx)
// 7. Start the new process with that primary token(CreateProcessWithTokenW)

var hProcessToken = IntPtr.Zero;
var hProcessToken = nint.Zero;
// Enable SeIncreaseQuotaPrivilege in this process. (This won't work if current process is not elevated.)
try
{
Expand All @@ -46,7 +47,7 @@ private static void RunAsDesktopUser(string fileName)

tkp.Privileges[0].Attributes = 0x00000002;

if (!AdjustTokenPrivileges(hProcessToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero))
if (!AdjustTokenPrivileges(hProcessToken, false, ref tkp, 0, nint.Zero, nint.Zero))
return;
}
finally
Expand All @@ -59,12 +60,12 @@ private static void RunAsDesktopUser(string fileName)
// replaced with a custom shell. This also won't return what you probably want if Explorer has been terminated and
// restarted elevated.
var hwnd = GetShellWindow();
if (hwnd == IntPtr.Zero)
if (hwnd == nint.Zero)
return;

var hShellProcess = IntPtr.Zero;
var hShellProcessToken = IntPtr.Zero;
var hPrimaryToken = IntPtr.Zero;
var hShellProcess = nint.Zero;
var hShellProcessToken = nint.Zero;
var hPrimaryToken = nint.Zero;
try
{
// Get the PID of the desktop shell process.
Expand All @@ -74,7 +75,7 @@ private static void RunAsDesktopUser(string fileName)

// Open the desktop shell process in order to query it (get the token)
hShellProcess = OpenProcess(ProcessAccessFlags.QueryInformation, false, dwPID);
if (hShellProcess == IntPtr.Zero)
if (hShellProcess == nint.Zero)
return;

// Get the process token of the desktop shell.
Expand All @@ -85,13 +86,13 @@ private static void RunAsDesktopUser(string fileName)

// Duplicate the shell's process token to get a primary token.
// Based on experimentation, this is the minimal set of rights required for CreateProcessWithTokenW (contrary to current documentation).
if (!DuplicateTokenEx(hShellProcessToken, dwTokenRights, IntPtr.Zero, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenPrimary, out hPrimaryToken))
if (!DuplicateTokenEx(hShellProcessToken, dwTokenRights, nint.Zero, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenPrimary, out hPrimaryToken))
return;

// Start the target process with the new token.
var si = new STARTUPINFO();
var pi = new PROCESS_INFORMATION();
if (!CreateProcessWithTokenW(hPrimaryToken, 0, fileName, "", 0, IntPtr.Zero, Path.GetDirectoryName(fileName)!, ref si, out pi))
if (!CreateProcessWithTokenW(hPrimaryToken, 0, fileName, "", 0, nint.Zero, Path.GetDirectoryName(fileName)!, ref si, out pi))
return;
}
finally
Expand All @@ -107,7 +108,7 @@ private static void RunAsDesktopUser(string fileName)

private struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
public uint PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
Expand All @@ -116,7 +117,7 @@ private struct TOKEN_PRIVILEGES
private struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
public uint Attributes;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down Expand Up @@ -161,66 +162,66 @@ private enum TOKEN_TYPE
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public nint hProcess;
public nint hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFO
{
public Int32 cb;
public int 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;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public nint lpReserved2;
public nint hStdInput;
public nint hStdOutput;
public nint hStdError;
}

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetCurrentProcess();
private static extern nint GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
private static extern bool OpenProcessToken(nint h, int acc, ref nint phtok);

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string host, string name, ref LUID pluid);

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TOKEN_PRIVILEGES newst, int len, IntPtr prev, IntPtr relen);
private static extern bool AdjustTokenPrivileges(nint htok, bool disall, ref TOKEN_PRIVILEGES newst, int len, nint prev, nint relen);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);
private static extern bool CloseHandle(nint hObject);


[DllImport("user32.dll")]
private static extern IntPtr GetShellWindow();
private static extern nint GetShellWindow();

[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private static extern uint GetWindowThreadProcessId(nint hWnd, out uint lpdwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, uint processId);
private static extern nint OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, uint processId);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL impersonationLevel, TOKEN_TYPE tokenType, out IntPtr phNewToken);
private static extern bool DuplicateTokenEx(nint hExistingToken, uint dwDesiredAccess, nint lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL impersonationLevel, TOKEN_TYPE tokenType, out nint phNewToken);

[DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CreateProcessWithTokenW(IntPtr hToken, int dwLogonFlags, string lpApplicationName, string lpCommandLine, int dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
private static extern bool CreateProcessWithTokenW(nint hToken, int dwLogonFlags, string lpApplicationName, string lpCommandLine, int dwCreationFlags, nint lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

#endregion
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace EndlessOnlinePatcher.Core;
namespace EoPatcher.Models;

public static class EndlessOnlineDirectory
{
public static DirectoryInfo Get()
Expand Down
Loading

0 comments on commit a36e193

Please sign in to comment.