1- function Compile-SourceScript {
2- <#
3- . SYNOPSIS
4- Compile-SourceScript is a wrapper for compiling SourceMod (.sp) and AMX Mod X (.sma) plugin source files for Source / GoldSource games.
5-
6- . DESCRIPTION
7- The script works by getting the specified plugin's source file compiled, and upon successful compilation, populating the respective mod's plugins directory with the newly compiled plugin.
8-
9- . PARAMETER File
10- Path to the plugin's .sp or .sma file.
11-
12- . PARAMETER Force
13- Copies the newly compiled plugin to the plugins directory without user confirmation.
14-
15- . EXAMPLE
16- ./Compile-SourceScript.ps1 -File ~/servers/csgo/addons/sourcemod/scripting/plugin1.sp
17- Compiles the SourceMod plugin source file 'plugin1.sp' with user confirmation for the game Counter-Strike: Global Offensive.
18-
19- . EXAMPLE
20- ./Compile-SourceScript.ps1 -File ~/servers/cstrike/addons/amxmodx/scripting/plugin2.sma -Force
21- Compiles the AMX Mod X plugin source file 'plugin2.sma' without user confirmation for the game Counter-Strike 1.6.
22-
23- . LINK
24- https://github.com/theohbrothers/Compile-SourceScript
25- #>
26-
27- [CmdletBinding ()]
28- param (
29- [Parameter (Mandatory = $False )]
30- [ValidateScript ({ Test-Path - Path $_ - PathType Leaf })]
31- $File
32- ,
33- [Parameter (Mandatory = $False )]
34- [switch ]$Force
35- )
36-
37- begin {
38- $ErrorActionPreference = ' Stop'
39-
40- # Define variables
41- $SCRIPT_EXTS = ' .sp' , ' .sma'
42- $PLUGIN_EXTS = ' .smx' , ' .amxx'
43- $COMPILE_WRAPPER_NAME = if ($IsWindows -Or $env: OS ) { ' compile.exe' } else { ' compile.sh' }
44- $COMPILED_DIR_NAME = ' compiled'
45- $PLUGINS_DIR_NAME = ' plugins'
46-
47- # Copy-Item Cmlet parameters
48- $copyParams = @ {
49- Confirm = ! $Force
50- }
51- }
52- process {
53- try {
54- # Process script
55- $script = Get-Item - Path $File
56- if ($script.Extension -notin $SCRIPT_EXTS ) {
57- throw " File is not a .sp or .sma source file."
58- }
59-
60- " Running compile wrapper" | Write-Host - ForegroundColor Cyan
61-
62- # Normalize paths
63- $scriptingDir = $script.DirectoryName
64- $compiledDir = Join-Path $scriptingDir $COMPILED_DIR_NAME
65- $pluginsDir = Join-Path (Split-Path $scriptingDir - Parent) $PLUGINS_DIR_NAME
66-
67- # Validate compiler binary
68- $compilerItem = Get-Item - Path (Join-Path $scriptingDir $COMPILE_WRAPPER_NAME )
69- " Compiler: $ ( $compilerItem.FullName ) " | Write-Host
70-
71- # Get all items in compiled folder before compilation by hash
72- $compiledDirItemsPre = Get-ChildItem $compiledDir - Recurse - Force | ? { $_.Extension -in $PLUGIN_EXTS } | Select-Object * , @ {name = ' md5' ; expression = {(Get-FileHash $_.Fullname - Algorithm MD5).Hash}}
73-
74- # Run the compiler
75- " Compiling..." | Write-Host - ForegroundColor Cyan
76- $epoch = [Math ]::Floor([decimal ](Get-Date (Get-Date ).ToUniversalTime()- uformat " %s" ))
77- $stdInFile = New-Item - Path (Join-Path $scriptingDir " .$epoch " ) - ItemType File - Force
78- ' 1' | Out-File - FilePath $stdInFile.FullName - Force - Encoding utf8
79- Start-Process $compilerItem.FullName - ArgumentList $script.Name - WorkingDirectory $scriptingDir - RedirectStandardInput $stdInFile.FullName - Wait - NoNewWindow
80-
81- # Get all items in compiled folder after compilation by hash
82- $compiledDirItemsPost = Get-ChildItem $compiledDir - Recurse - Force | ? { $_.Extension -in $PLUGIN_EXTS } | Select-Object * , @ {name = ' md5' ; expression = {(Get-FileHash $_.FullName - Algorithm MD5).Hash}}
83-
84- # Get items with differing hashes
85- $compiledDirItemsDiff = if ($compiledDirItemsPost ) {
86- if ($compiledDirItemsPre ) {
87- $hashesDiffObj = Compare-object - ReferenceObject $compiledDirItemsPre - DifferenceObject $compiledDirItemsPost - Property FullName, md5 | ? { $_.SideIndicator -eq ' =>' }
88- $compiledDirItemsPost | ? { $_.md5 -in $hashesDiffObj.md5 }
89- }else {
90- $compiledDirItemsPost
91- }
92- }
93-
94- if ($compiledDirItemsDiff ) {
95- # List successfully compiled plugins
96- " `n Newly compiled plugins:" | Write-Host - ForegroundColor Cyan
97- $compiledDirItemsDiff | % {
98- $compiledPluginHash = (Get-FileHash $_.FullName - Algorithm MD5).Hash
99- " $ ( $_.Name ) , $ ( $_.LastWriteTime ) , $compiledPluginHash " | Write-Host - ForegroundColor White
100- }
101-
102- New-Item - Path $pluginsDir - ItemType Directory - Force | Out-Null
103- $compiledDirItemsDiff | % {
104- " `n $ ( $_.Name ) :" | Write-Host - ForegroundColor Green
105- if ($_.Basename -ne $script.Basename ) {
106- " The plugin's name does not match the specified script's name." | Write-Host - ForegroundColor Magenta
107- return # continue in %
108- }
109- $existingPlugin = Get-Item " $pluginsDir /$ ( $_.Name ) " - ErrorAction SilentlyContinue
110- if (! $existingPlugin ) {
111- " Plugin does not currently exist in the plugins directory." | Write-Host - ForegroundColor Yellow
112- }else {
113- $existingPluginHash = (Get-FileHash $existingPlugin - Algorithm MD5).Hash
114- " Existing: $ ( $existingPlugin.LastWriteTime ) , $existingPluginHash " | Write-Host - ForegroundColor Yellow
115- }
116- # Display the compiled and existing plugin's file info
117- $compiledPluginHash = (Get-FileHash $_.FullName - Algorithm MD5).Hash
118- " Compiled: $ ( $_.LastWriteTime ) , $compiledPluginHash " | Write-Host - ForegroundColor Green
119-
120- # Attempt to copy the compiled plugin to the plugins folder
121- Copy-Item - Path $_.FullName - Destination $pluginsDir - Recurse @copyParams
122-
123- if ($LASTEXITCODE ) { " Plugin copy error." | Write-Host - ForegroundColor Magenta; return }
124- # Alert the user on the situation of the plugin
125- $updatedPlugin = Get-Item " $pluginsDir /$ ( $_.Name ) "
126- $updatedPluginHash = (Get-FileHash $updatedPlugin - Algorithm MD5).Hash
127- if ($updatedPluginHash -eq $compiledPluginHash ) { " `n Plugin successfully copied to $ ( $_.Fullname ) " | Write-Host - ForegroundColor Green }
128- else { " `n Plugin not copied." | Write-Host - ForegroundColor Magenta; return }
129- }
130- }else {
131- " `n No newly compiled plugins found. No operations were performed." | Write-Host - ForegroundColor Magenta
132- }
133- }catch {
134- throw " Runtime error. Exception: $ ( $_.Exception.Message ) "
135- }finally {
136- # Cleanup
137- if ($stdInFile ) {
138- Remove-Item $stdInFile - Force
139- }
140- " End of compile wrapper." | Write-Host - ForegroundColor Cyan
141- }
142- }
143- }
144-
145- # Export the members of the module
146- Export-ModuleMember - Function Compile- SourceScript
1+ function Compile-SourceScript {
2+ <#
3+ . SYNOPSIS
4+ Compile-SourceScript is a wrapper for compiling SourceMod (.sp) and AMX Mod X (.sma) plugin source files for Source / GoldSource games.
5+
6+ . DESCRIPTION
7+ The script works by getting the specified plugin's source file compiled, and upon successful compilation, populating the respective mod's plugins directory with the newly compiled plugin.
8+
9+ . PARAMETER File
10+ Path to the plugin's .sp or .sma file.
11+
12+ . PARAMETER Force
13+ Copies the newly compiled plugin to the plugins directory without user confirmation.
14+
15+ . EXAMPLE
16+ ./Compile-SourceScript.ps1 -File ~/servers/csgo/addons/sourcemod/scripting/plugin1.sp
17+ Compiles the SourceMod plugin source file 'plugin1.sp' with user confirmation for the game Counter-Strike: Global Offensive.
18+
19+ . EXAMPLE
20+ ./Compile-SourceScript.ps1 -File ~/servers/cstrike/addons/amxmodx/scripting/plugin2.sma -Force
21+ Compiles the AMX Mod X plugin source file 'plugin2.sma' without user confirmation for the game Counter-Strike 1.6.
22+
23+ . LINK
24+ https://github.com/theohbrothers/Compile-SourceScript
25+ #>
26+
27+ [CmdletBinding ()]
28+ param (
29+ [Parameter (Mandatory = $False )]
30+ [ValidateScript ({ Test-Path - Path $_ - PathType Leaf })]
31+ $File
32+ ,
33+ [Parameter (Mandatory = $False )]
34+ [switch ]$Force
35+ )
36+
37+ begin {
38+ $ErrorActionPreference = ' Stop'
39+
40+ # Define variables
41+ $SCRIPT_EXTS = ' .sp' , ' .sma'
42+ $PLUGIN_EXTS = ' .smx' , ' .amxx'
43+ $COMPILE_WRAPPER_NAME = if ($IsWindows -Or $env: OS ) { ' compile.exe' } else { ' compile.sh' }
44+ $COMPILED_DIR_NAME = ' compiled'
45+ $PLUGINS_DIR_NAME = ' plugins'
46+
47+ # Copy-Item Cmlet parameters
48+ $copyParams = @ {
49+ Confirm = ! $Force
50+ }
51+ }
52+ process {
53+ try {
54+ # Process script
55+ $script = Get-Item - Path $File
56+ if ($script.Extension -notin $SCRIPT_EXTS ) {
57+ throw " File is not a .sp or .sma source file."
58+ }
59+
60+ " Running compile wrapper" | Write-Host - ForegroundColor Cyan
61+
62+ # Normalize paths
63+ $scriptingDir = $script.DirectoryName
64+ $compiledDir = Join-Path $scriptingDir $COMPILED_DIR_NAME
65+ $pluginsDir = Join-Path (Split-Path $scriptingDir - Parent) $PLUGINS_DIR_NAME
66+
67+ # Validate compiler binary
68+ $compilerItem = Get-Item - Path (Join-Path $scriptingDir $COMPILE_WRAPPER_NAME )
69+ " Compiler: $ ( $compilerItem.FullName ) " | Write-Host
70+
71+ # Get all items in compiled folder before compilation by hash
72+ $compiledDirItemsPre = Get-ChildItem $compiledDir - Recurse - Force | ? { $_.Extension -in $PLUGIN_EXTS } | Select-Object * , @ {name = ' md5' ; expression = {(Get-FileHash $_.Fullname - Algorithm MD5).Hash}}
73+
74+ # Run the compiler
75+ " Compiling..." | Write-Host - ForegroundColor Cyan
76+ $epoch = [Math ]::Floor([decimal ](Get-Date (Get-Date ).ToUniversalTime()- uformat " %s" ))
77+ $stdInFile = New-Item - Path (Join-Path $scriptingDir " .$epoch " ) - ItemType File - Force
78+ ' 1' | Out-File - FilePath $stdInFile.FullName - Force - Encoding utf8
79+ Start-Process $compilerItem.FullName - ArgumentList $script.Name - WorkingDirectory $scriptingDir - RedirectStandardInput $stdInFile.FullName - Wait - NoNewWindow
80+
81+ # Get all items in compiled folder after compilation by hash
82+ $compiledDirItemsPost = Get-ChildItem $compiledDir - Recurse - Force | ? { $_.Extension -in $PLUGIN_EXTS } | Select-Object * , @ {name = ' md5' ; expression = {(Get-FileHash $_.FullName - Algorithm MD5).Hash}}
83+
84+ # Get items with differing hashes
85+ $compiledDirItemsDiff = if ($compiledDirItemsPost ) {
86+ if ($compiledDirItemsPre ) {
87+ $hashesDiffObj = Compare-object - ReferenceObject $compiledDirItemsPre - DifferenceObject $compiledDirItemsPost - Property FullName, md5 | ? { $_.SideIndicator -eq ' =>' }
88+ $compiledDirItemsPost | ? { $_.md5 -in $hashesDiffObj.md5 }
89+ }else {
90+ $compiledDirItemsPost
91+ }
92+ }
93+
94+ if ($compiledDirItemsDiff ) {
95+ # List successfully compiled plugins
96+ " `n Newly compiled plugins:" | Write-Host - ForegroundColor Cyan
97+ $compiledDirItemsDiff | % {
98+ $compiledPluginHash = (Get-FileHash $_.FullName - Algorithm MD5).Hash
99+ " $ ( $_.Name ) , $ ( $_.LastWriteTime ) , $compiledPluginHash " | Write-Host - ForegroundColor White
100+ }
101+
102+ New-Item - Path $pluginsDir - ItemType Directory - Force | Out-Null
103+ $compiledDirItemsDiff | % {
104+ " `n $ ( $_.Name ) :" | Write-Host - ForegroundColor Green
105+ if ($_.Basename -ne $script.Basename ) {
106+ " The plugin's name does not match the specified script's name." | Write-Host - ForegroundColor Magenta
107+ return # continue in %
108+ }
109+ $existingPlugin = Get-Item " $pluginsDir /$ ( $_.Name ) " - ErrorAction SilentlyContinue
110+ if (! $existingPlugin ) {
111+ " Plugin does not currently exist in the plugins directory." | Write-Host - ForegroundColor Yellow
112+ }else {
113+ $existingPluginHash = (Get-FileHash $existingPlugin - Algorithm MD5).Hash
114+ " Existing: $ ( $existingPlugin.LastWriteTime ) , $existingPluginHash " | Write-Host - ForegroundColor Yellow
115+ }
116+ # Display the compiled and existing plugin's file info
117+ $compiledPluginHash = (Get-FileHash $_.FullName - Algorithm MD5).Hash
118+ " Compiled: $ ( $_.LastWriteTime ) , $compiledPluginHash " | Write-Host - ForegroundColor Green
119+
120+ # Attempt to copy the compiled plugin to the plugins folder
121+ Copy-Item - Path $_.FullName - Destination $pluginsDir - Recurse @copyParams
122+
123+ if ($LASTEXITCODE ) { " Plugin copy error." | Write-Host - ForegroundColor Magenta; return }
124+ # Alert the user on the situation of the plugin
125+ $updatedPlugin = Get-Item " $pluginsDir /$ ( $_.Name ) "
126+ $updatedPluginHash = (Get-FileHash $updatedPlugin - Algorithm MD5).Hash
127+ if ($updatedPluginHash -eq $compiledPluginHash ) { " `n Plugin successfully copied to $ ( $_.Fullname ) " | Write-Host - ForegroundColor Green }
128+ else { " `n Plugin not copied." | Write-Host - ForegroundColor Magenta; return }
129+ }
130+ }else {
131+ " `n No newly compiled plugins found. No operations were performed." | Write-Host - ForegroundColor Magenta
132+ }
133+ }catch {
134+ throw " Runtime error. Exception: $ ( $_.Exception.Message ) "
135+ }finally {
136+ # Cleanup
137+ if ($stdInFile ) {
138+ Remove-Item $stdInFile - Force
139+ }
140+ " End of compile wrapper." | Write-Host - ForegroundColor Cyan
141+ }
142+ }
143+ }
0 commit comments