From fc6bc9caa803d62bbb6d1b8290a4969b45bea45f Mon Sep 17 00:00:00 2001 From: Cas van Cooten Date: Sun, 12 Mar 2023 18:27:20 +0100 Subject: [PATCH] Implement workaround for broken copyFile (Fixes #11), remove winim res file --- NimPlant.py | 4 +--- client/commands/cp.nim | 8 +++++--- client/commands/mv.nim | 3 ++- client/util/winUtils.nim | 22 ++++++++++++++++++++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/NimPlant.py b/NimPlant.py index 78f0147..7d3a14a 100644 --- a/NimPlant.py +++ b/NimPlant.py @@ -137,9 +137,7 @@ def compile_nim(binary_type, xor_key, debug=False): # Construct compilation command if binary_type == "exe" or binary_type == "exe-selfdelete" or binary_type == "dll": - compile_command = ( - f"nim c --hints:off --warnings:off -d:xor_key={xor_key} -d:release -d:strip" - ) + compile_command = f"nim c --hints:off --warnings:off -d:xor_key={xor_key} -d:release -d:strip -d:noRes" if debug: compile_command = compile_command + " -d:verbose" diff --git a/client/commands/cp.nim b/client/commands/cp.nim index 1fe93eb..2ed880c 100644 --- a/client/commands/cp.nim +++ b/client/commands/cp.nim @@ -1,5 +1,7 @@ -from os import copyDir, copyFile, copyFileToDir, dirExists, splitPath, `/` +from ../util/winUtils import copyDir +from os import dirExists, splitPath, `/` from strutils import join +from winim/lean import CopyFileA, LPCSTR, FALSE, winstrConverterStringToPtrChar # Copy files or directories proc cp*(args : varargs[string]) : string = @@ -23,8 +25,8 @@ proc cp*(args : varargs[string]) : string = # Copying a file elif dirExists(destination): - copyFileToDir(source, destination) + CopyFileA(source, destination/splitPath(source).tail, FALSE) else: - copyFile(source, destination) + CopyFileA(source, destination, FALSE) result = obf("Copied '") & source & obf("' to '") & destination & obf("'.") \ No newline at end of file diff --git a/client/commands/mv.nim b/client/commands/mv.nim index 1cdd514..7c51583 100644 --- a/client/commands/mv.nim +++ b/client/commands/mv.nim @@ -1,4 +1,5 @@ -from os import dirExists, moveFile, moveDir, splitPath, `/` +from os import dirExists, moveFile, splitPath, `/` +from ../util/winUtils import moveDir from strutils import join # Move a file or directory diff --git a/client/util/winUtils.nim b/client/util/winUtils.nim index b3305bb..7d49de2 100644 --- a/client/util/winUtils.nim +++ b/client/util/winUtils.nim @@ -1,6 +1,6 @@ from nativesockets import getHostName, gethostbyname -from os import getCurrentProcessId, splitPath, getAppFilename -import winlean +from os import getCurrentProcessId, splitPath, getAppFilename, createDir, walkDir, splitPath, pcDir, `/`, removeDir +from winim/lean import CopyFileA, FALSE, ULONG, winstrConverterStringToPtrChar import ../commands/whoami import strenc @@ -37,6 +37,24 @@ proc getWindowsVersion*() : string = var vInfo = obf("Windows ") & $versionInfo.dwMajorVersion & obf(" build ") & $versionInfo.dwBuildNumber result = vInfo +# Define copyDir and moveDir functions to override os stdlib +# This fixes a bug where any function with using copyFile does not work with Win11 DLLs +# See: https://github.com/nim-lang/Nim/issues/21504 +proc copyDir*(source, dest: string) = + createDir(dest) + for kind, path in walkDir(source): + var + noSource = splitPath(path).tail + dPath = dest / noSource + if kind == pcDir: + copyDir(path, dPath) + else: + CopyFileA(path, dPath, FALSE) + +proc moveDir*(source, dest: string) = + copydir(source, dest) + removeDir(source) + # Get the username proc getUsername*() : string = result = whoami()