-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateExe.ps1
180 lines (153 loc) · 4.94 KB
/
CreateExe.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Powershell script to create executable launcher for gamepass games, e.g., for LGS
#
## Usage:
# ./CreateExe.ps1 <part of game name>
#
## Example
#
# ./CreateExe <NoMans>
#
# Creates NomansSky.exe that can be used launch No Man's Sky
# The executable can then be used in LGS to detect if NMS is running
#
## Rquirements
#
# [PS2EXE](https://github.com/MScholtes/PS2EXE)
# Install with
# Install-Module ps2exe
#
# Enable running PS scripts asneeded
# Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
param([string]$Search, [Alias("ac")][switch]$Autoclose, [Alias("m")][switch]$Monitor, [switch]$NoExe, [switch]$y, [Double]$InitWait = 30.0, [Double]$Wait = 1.0, [string]$Template = "LauncherTemplate.ps1", [string]$Name=$null)
function select_from_array() {
[CmdletBinding()]
param (
$prompt,
$items,
$controls="q",
$help="q - quit"
)
$n=$null
$items | Format-Table | Out-Host
write-host "? for commands"
do {
if ($items.Length -lt 10) {
write-Host -NoNewLine "${prompt}:"
$select = [Console]::ReadKey().KeyChar
write-Host ""
} else {
$select = Read-Host -Prompt $prompt
$select = $select -replace '\s',''
}
if ($select -eq "?") {
Write-host $help
continue
}
} while (!($controls.contains($select) -or ([System.Int32]::TryParse($select, [ref]$n) -and $n -le $items.Length -and $n -gt 0)))
return $select
}
function as_bool_string() {
param([bool]$value)
$ret= if ($value) { '$true' } else { '$false' }
return $ret
}
$oPackage = Get-AppxPackage *$Search*
# The publisher name is typically included with the install location
if (!$oPackage -and !$oPackage -is [array]){
$oPackage = Get-AppxPackage -ErrorAction SilentlyContinue | Where-object { $_.InstallLocation -like "*$Search*"}
}
# Finally try with publisher displayname
# TODO: Use Package.Publisher, needs converting name to publisher id
if (!$oPackage -and !$oPackage -is [array]){
$p = Get-AppxPackage -ErrorAction SilentlyContinue | Get-AppxPackageManifest -ErrorAction SilentlyContinue | Where-object { $_.Package.Properties.PublisherDisplayName -like "*$Search*"}
$oPackage = Get-AppxPackage -Name $p.Package.Identity.Name
}
if (!$oPackage)
{
Write-Host -ForegroundColor Red "Could not find package for '$Search'"
Write-Output "You can try with part of publisher name or install directory"
Exit
} elseif ($oPackage -is [array])
{
Write-Host -ForegroundColor Red "Multiple packages found for search '$Search'"
$a = @()
$i=0
foreach ($p in $oPackage)
{
$i++
$a+=[PSCustomObject]@{ "#" = $i; "Package Name" = $p.name; "Display Name" = (Get-AppPackageManifest $p).Package.Properties.DisplayName}
}
$select=select_from_array "Select one" $a
if ($select -eq "q"){
Exit
}
$n=([int][string]$select)-1
$oPackage = $oPackage[$n]
}
$oManifest = Get-AppPackageManifest $oPackage
$appId = $oManifest.Package.Applications.Application.id
if ($Name) {
$sDisplayName = $Name
} else {
$sDisplayName = $oManifest.Package.Properties.DisplayName
}
$FamilyName = $oPackage.PackageFamilyName
$sCommand = "explorer.exe shell:appsFolder\$FamilyName!$appId"
$sName = $sDisplayName.replace(" ", "")
$sFileName = $sName
foreach($c in [System.IO.Path]::GetInvalidFileNameChars()) {
$sFileName = $sFileName.replace([string]$c,"-")
}
Write-Host $sFileName
$sScript = $sFileName+".ps1"
$sExe = $sFileName+".exe"
$Titles = @($sName,$sDisplayName) -join ("|")
$Template = Get-Content "LauncherTemplate.ps1" -Raw
$sMonitor = as_bool_string $Monitor
$sAutoClose = as_bool_string $Autoclose
if (!$Monitor) {
Write-host -NoNewline "Creating launcher "
} else {
Write-host -NoNewline "Creating monitoring "
}
Write-Host "for '$sDisplayName'"
$pub = $oManifest.Package.Properties.PublisherDisplayName
$inst = $oPackage.InstallLocation
Write-Output "Publisher: $pub"
Write-Output "Package location: $inst"
if (!$y)
{
do {
$continue = Read-Host "Continue? (Y/n)"
} while (!"YNny".contains("$continue"))
if ($continue -eq "n") {
exit
}
}
$Launcher = $template -replace "%%InitWait%%", $InitWait -replace "%%Wait%%", $Wait -replace "%%DisplayName%%", $sDisplayName `
-replace "%%Titles%%", $Titles -replace "%%Command%%", $sCommand -replace "%%Autoclose%%", $sAutoClose `
-replace "%%Monitor%%", $sMonitor `
-replace "%%AutoClose%%", $Autoclose
try {
$Launcher | Out-File -FilePath $sScript
} catch {
Write-host -ForegroundColor Red "could not create launcher $sScript"
exit
}
if (!$NoExe)
{
try {
Invoke-ps2exe .\$sScript .\$sExe
} catch {
Write-host -ForegroundColor Red "Could not create launcher $sExe"
exit
}
Write-Host -NoNewline "Created $sExe "
} else {
Write-Host -NoNewline "Would Create $sScript "
}
if ($Monitor) {
Write-Output "to monitor $sDisplayName"
} else {
Write-Output "with launch command $sCommand"
}