Skip to content

Commit d5a185d

Browse files
Port project to Golang
1 parent cb76259 commit d5a185d

File tree

6 files changed

+257
-289
lines changed

6 files changed

+257
-289
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@
2020
- This fixes the edge case seen when running the RSI Launcher's installer-support.exe binary.
2121
- v2.2.1 - Fix debugging and revert quote replacement changes.
2222
- This fixes the edge case seen when running the vcredist binary in the RSI Launcher.
23-
- The EAC service will fail to install due to how it is invokved, I'll have to rewrite this in something that can use regex a bit better.
23+
- The EAC service will fail to install due to how it is invokved, I'll have to rewrite this in something that can use regex a bit better.
24+
- v3.0.0 - Port wrapper to Golang
25+
- Dealing with string manipulation in C was painful, this results in a larger binary but it is far easier to deal with edge cases.
26+
- Fully fix [ProjectSynchro/powershell-wrapper-for-wine#2](https://github.com/ProjectSynchro/powershell-wrapper-for-wine/issues/2)

Makefile

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
# Compiler Definitions
2-
CC64 = x86_64-w64-mingw32-gcc
3-
CC32 = i686-w64-mingw32-gcc
2+
GO = go
43

54
# Source Files
6-
SRCS = src/wrapper.c
7-
8-
# Compiler Flags
9-
CFLAGS = -O1 -fno-ident -fno-stack-protector -fomit-frame-pointer -fno-unwind-tables -fno-asynchronous-unwind-tables -falign-functions=1 -falign-jumps=1 -falign-loops=1 -fwhole-program -mconsole -municode -mno-stack-arg-probe -Xlinker --stack=0x200000,0x200000 -Wall -Wextra -ffreestanding
10-
11-
# Linker Flags
12-
LDFLAGS = -lurlmon -lkernel32 -lucrtbase -nostdlib -lshell32 -lshlwapi -s
13-
14-
# Build Directories
15-
BUILD_DIR64 = build/x64
16-
BUILD_DIR32 = build/x86
5+
SRCS = src/wrapper.go
176

187
# Distribution Directories
198
DIST_ZIP_DIR = dist/zip
@@ -34,19 +23,17 @@ TAR_ARCHIVE = powershell-wrapper.tar.gz
3423
all: $(TARGET32) $(TARGET64)
3524

3625
# Debug Build
37-
debug: CFLAGS += -DENABLE_DEBUG_LOG -g
38-
debug: LDFLAGS += -luser32
3926
debug: all
4027

4128
# 64-bit Executable
4229
$(TARGET64): $(SRCS)
4330
@echo "Building 64-bit executable..."
44-
$(CC64) $(SRCS) $(CFLAGS) $(LDFLAGS) -o $@
31+
GOOS=windows GOARCH=amd64 $(GO) build -o $(TARGET64) $(SRCS)
4532

4633
# 32-bit Executable
4734
$(TARGET32): $(SRCS)
4835
@echo "Building 32-bit executable..."
49-
$(CC32) $(SRCS) $(CFLAGS) $(LDFLAGS) -o $@
36+
GOOS=windows GOARCH=386 $(GO) build -o $(TARGET32) $(SRCS)
5037

5138
# Distribution Target
5239
dist: zip targz

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ For example 'powershell.exe -Nologo 1+2' is internally reworked to 'pwsh.exe -No
1010
If the command is still incompatible with pwsh.exe there's an option to replace (parts of) the command to fix things (in profile.ps1).
1111
See profile.ps1 for an example: the ambigous command 'measure -s' (for which pwsh will throw an error) is replaced with 'measure -sum'
1212

13-
For fun I changed code from standard main(argc,*argv[]) to something like [this](https://nullprogram.com/blog/2016/01/31/])
1413

1514
# Install
1615

@@ -25,7 +24,7 @@ For fun I changed code from standard main(argc,*argv[]) to something like [this]
2524

2625
# Compiling and Manual Install
2726

28-
You'll need a basic mingw toolchain, for Fedora see: https://fedoraproject.org/wiki/MinGW/Tutorial
27+
You'll need golang installd, for Fedora see: https://developer.fedoraproject.org/tech/languages/go/go-installation.html
2928

3029
Clone the git repository.
3130

src/profile.ps1

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,33 @@ function Start-Process {
101101
)
102102

103103
# Function to handle logging only if enabled
104-
function Log-Message {
104+
function Write-Message {
105105
param (
106106
[string]$Message
107107
)
108108
if ($env:LOG_DEBUG) {
109-
$logFilePath = "$env:USERPROFILE\ProcessLog.txt"
109+
$logFilePath = "$env:USERPROFILE\pwsh_powershell_debug.log"
110110
$logEntry = "$(Get-Date) - $Message"
111111
Add-Content -Path $logFilePath -Value $logEntry
112112
}
113113
}
114114

115-
Log-Message "Starting process: $FilePath with arguments: $($ArgumentList -join ' ')"
115+
Write-Message "Starting process: $FilePath with arguments: $($ArgumentList -join ' ')"
116116

117117
try {
118118
# Check if the file exists at the specified path
119119
if (-not (Test-Path $FilePath -PathType Leaf)) {
120-
Log-Message "File not found: $FilePath"
120+
Write-Message "File not found: $FilePath"
121121
throw "File not found: $FilePath"
122122
}
123123

124124
# Check if the file extension is .bat and rewrite the command if needed
125125
if ([System.IO.Path]::GetExtension($FilePath).ToLower() -eq '.bat') {
126-
Log-Message "Batch file detected: $FilePath"
126+
Write-Message "Batch file detected: $FilePath"
127127

128128
$fileContent = Get-Content $FilePath -Raw
129129
if ($fileContent -match '\(echo %ERRORLEVEL%\) >') {
130-
Log-Message "Rewriting batch file: $FilePath"
130+
Write-Message "Rewriting batch file: $FilePath"
131131
$fileContent = $fileContent -replace '\(echo %ERRORLEVEL%\) >', 'echo %ERRORLEVEL% >'
132132
Set-Content $FilePath -Value $fileContent
133133
}
@@ -149,53 +149,53 @@ function Start-Process {
149149

150150
# Set working directory if provided
151151
if ($WorkingDirectory) {
152-
Log-Message "Setting working directory: $WorkingDirectory"
152+
Write-Message "Setting working directory: $WorkingDirectory"
153153
$processStartInfo.WorkingDirectory = $WorkingDirectory
154154
}
155155

156156
# Set verb if provided
157157
if ($Verb) {
158-
Log-Message "Setting verb: $Verb"
158+
Write-Message "Setting verb: $Verb"
159159
$processStartInfo.Verb = $Verb
160160
}
161161

162162
# Handle input/output redirection
163163
if ($RedirectStandardOutput) {
164-
Log-Message "Redirecting standard output to: $RedirectStandardOutput"
164+
Write-Message "Redirecting standard output to: $RedirectStandardOutput"
165165
$processStartInfo.RedirectStandardOutput = $true
166166
$processStartInfo.StandardOutputFileName = $RedirectStandardOutput
167167
}
168168
if ($RedirectStandardError) {
169-
Log-Message "Redirecting standard error to: $RedirectStandardError"
169+
Write-Message "Redirecting standard error to: $RedirectStandardError"
170170
$processStartInfo.RedirectStandardError = $true
171171
$processStartInfo.StandardErrorFileName = $RedirectStandardError
172172
}
173173
if ($RedirectStandardInput) {
174-
Log-Message "Redirecting standard input from: $RedirectStandardInput"
174+
Write-Message "Redirecting standard input from: $RedirectStandardInput"
175175
$processStartInfo.RedirectStandardInput = $true
176176
}
177177

178178
# If credentials provided, load them
179179
if ($Credential) {
180-
Log-Message "Using provided credentials for: $($Credential.UserName)"
180+
Write-Message "Using provided credentials for: $($Credential.UserName)"
181181
$processStartInfo.UserName = $Credential.UserName
182182
$password = $Credential.GetNetworkCredential().Password
183183
$processStartInfo.Password = (ConvertTo-SecureString $password -AsPlainText -Force)
184184
}
185185

186186
# UseNewEnvironment flag (whether to inherit or create new environment variables)
187187
if ($UseNewEnvironment) {
188-
Log-Message "Clearing environment variables for new process"
188+
Write-Message "Clearing environment variables for new process"
189189
$processStartInfo.EnvironmentVariables.Clear()
190190
}
191191

192192
# Start process
193-
Log-Message "Starting process: $FilePath"
193+
Write-Message "Starting process: $FilePath"
194194
$process = [System.Diagnostics.Process]::Start($processStartInfo)
195195

196196
# Log process start success
197197
if ($process) {
198-
Log-Message "Process started successfully: $FilePath (PID: $($process.Id))"
198+
Write-Message "Process started successfully: $FilePath (PID: $($process.Id))"
199199
}
200200

201201
# If PassThru is specified, return the process object
@@ -205,15 +205,15 @@ function Start-Process {
205205

206206
# Wait for the process to exit if the Wait switch is specified
207207
if ($Wait) {
208-
Log-Message "Waiting for process to exit: $FilePath"
208+
Write-Message "Waiting for process to exit: $FilePath"
209209
$process.WaitForExit()
210210

211211
$exitCode = $process.ExitCode
212-
Log-Message "Process exited with code: $exitCode"
212+
Write-Message "Process exited with code: $exitCode"
213213
}
214214

215215
} catch {
216-
Log-Message "Error starting process: $_"
216+
Write-Message "Error starting process: $_"
217217
throw $_
218218
}
219219
}

0 commit comments

Comments
 (0)