CntoDev / central-plaza

Repository for the documentation that isn't related to a specific project and organization of new projects/ideas.
0 stars 0 forks source link

Write a self-contained dedicated server startup script for MMs #15

Closed freghar closed 3 years ago

freghar commented 3 years ago

When testing mods or missions, I always start up a local dedicated server, since it's different to a Multiplayer Preview in the editor or even a Hosted MP server. However it would be worth making this easy to use for others.

Create a (windows batch / powershell) script + related directory, both of which would be put inside the Arma 3 install dir:

The actual server.cfg / basic.cfg / server.Arma3Profile are easy to make - get them either from https://github.com/freghar/arma3dedicated/tree/master/dedicated-config or https://github.com/CntoDev/misc-scripts/tree/master/server-setup or just poke me.

For the record - I don't plan to work on this, but it would be really good if somebody with PowerShell powers could. :)

Thanks!

milivojm commented 3 years ago

Sneaky!

Didr commented 3 years ago

Can't say I write much Powershell, but here's a beta version!

Missing features:

# --- User Config ---
# Which mod repos do you want to run? Pick between: main, main+dev, main+campaign or main+dev+campaign
$modRepo = "main"
# WE DEAL WITH ABSOLUTES!
$configLocation = "C:\Users\did\Documents\GIT-CNTO\testscript"
# Main cnto repo directory path
$mainRepoPath = "X:\Games\steamapps\common\Arma 3\Main-Repo"
# Campaign cnto repo path (optional)
$campaignRepoPath = "X:\Games\steamapps\common\Arma 3\Campaign-Repo"
# Dev cnto repo path (optional)
$devRepoPath = "X:\Games\steamapps\common\Arma 3\Dev-Repo"
# Path to Arma3
$armaPath = "X:\Games\steamapps\common\Arma 3"
# Start HC: yes/no
$startHC = "yes"
# Use latest CBA settings: yes/no
$useLatestCBA = "yes"

# Static Config
Set-Location $configLocation
$currentLocation = Get-Location
$ServerScriptUrl = "https://github.com/CntoDev/misc-scripts/archive/refs/heads/master.zip"
$cbaSettingsURL = "https://raw.githubusercontent.com/CntoDev/cba-settings-lock/master/cba_settings_userconfig/cba_settings.sqf"
$basicCfgURL = "https://raw.githubusercontent.com/freghar/arma3dedicated/master/dedicated-config/basic.cfg"
$commonServerParameters = "-port 2302 -noSplash -noLand -enableHT -hugePages -profiles=$configDir\profiles"
$configDir = "$configLocation\server-setup"
#IfExists
$mainMods = Get-ChildItem $mainRepoPath
$campaignMods = Get-ChildItem $campaignRepoPath
$devMods = Get-ChildItem $devRepoPath

# Functions
function Get-MainMods() {
    $modList = $mainMods | Group-Object -Property Directory | ForEach-Object {
        @(
            $_.Group | Resolve-Path | Convert-Path
        )-join','
    }
    return $modList
}

function Get-CampaignMods() {
    $campaignModList = $campaignMods | Group-Object -Property Directory | ForEach-Object {
        @(
            $_.Group | Resolve-Path | Convert-Path
        )-join','
    }
    return $campaignModList
}

function Get-DevModDiff() {
    $devDiff = Compare-Object -ReferenceObject $mainMods -DifferenceObject $devMods -IncludeEqual -ExcludeDifferent -PassThru
    $modList = Compare-Object -ReferenceObject $devDiff -DifferenceObject $mainMods -PassThru | Group-Object -Property Directory | ForEach-Object {
        @(
            $_.Group | Resolve-Path | Convert-Path
        )-join','
    }
    $devModList = $devMods | Group-Object -Property Directory | ForEach-Object {
        @(
            $_.Group | Resolve-Path | Convert-Path
        )-join','
    }
    return $modList+','+$devModList
}

# Awful switch statement to get correct mods
function Get-ModList($type) {
    switch ($type) {
        "main" {return Get-MainMods}

        "main+dev" {return Get-DevModDiff}

        "main+campaign" {return "$(Get-MainMods)+','+$(Get-CampaignMods)"}

        "main+dev+campaign" {return "$(Get-DevModDiff)+','+$(Get-CampaignMods)"}
    }
}

function Start-Server($type,$getLatestCBA,$useHC) {
    # Force overwrite cba_settings.sqf
    if ($getLatestCBA -eq "yes") {
        Write-Host "Downloading latest CBA Settings from Github"
        Invoke-RestMethod -Uri $cbaSettingsURL -OutFile "$armaPath\userconfig\cba_settings.sqf"
    }
    if ($useHC -eq "yes") {
        Write-Host "Starting headless client..."
        Start-Process -FilePath "$armapath\arma3server.exe" -ArgumentList "$commonServerParameters -client -connect=127.0.0.1 -password=cnto -mod=`"$(Get-ModList($type))`""
    }
    Write-Host "Starting the server..."
    Start-Process -FilePath "$armapath\arma3server.exe" -ArgumentList "$commonServerParameters -filePatching -name=server -config=$configDir\server.cfg -cfg=$configDir\basic.cfg -mod=`"$(Get-ModList($type))`""

}

# First Setup - 
# https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-7.1
function Initialize-Server {
    # Add confirmation dialogue about overwriting
    Write-Host "Downloading the latest startup script ZIP from Github"
    Invoke-RestMethod -Uri $ServerScriptUrl -OutFile "$configLocation\master.zip"
    Write-Host "Extracting it..."
    Expand-Archive -Path "$configLocation\master.zip" -DestinationPath $currentLocation -Force
    Write-Host "Deleting the ZIP after extraction has been completed"
    Copy-Item -Path "$configLocation\misc-scripts-master\server-setup" -Destination "$configLocation" -Force -Recurse
    Invoke-RestMethod -Uri $basicCfgURL -OutFile "$configDir\basic.cfg"
    Remove-Item -Path "$configLocation\misc-scripts-master" -Force -Recurse
    Remove-Item -Path "$configLocation\master.zip" -Force

    # Create userconfig folder in arma3 folder, if it doesn't exist:
    New-Item -Path "$armaPath" -Name "userconfig" -ItemType "directory" -Force | Out-Null
    # Add cba_settings
    Invoke-RestMethod -Uri $cbaSettingsURL -OutFile "$armaPath\userconfig\cba_settings.sqf"
}
Initialize-Server
Start-Server -type $modRepo -getLatestCBA $useLatestCBA -useHC $startHC
freghar commented 3 years ago

Development ongoing in https://github.com/CntoDev/arma-startup-scripts