-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC_backup_setup.ps1
201 lines (176 loc) · 7.29 KB
/
LC_backup_setup.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# CONFIG
$BACKUP_FILES = @("LCChallengeFile","LCGeneralSaveData","LCSaveFile1","LCSaveFile2","LCSaveFile3","SaveFile.es3")
# VARS
$CWD = (Get-Location).path
$LC_LOCAL_PATH = $env:LOCALAPPDATA + "Low\ZeekerssRBLX\Lethal Company"
$USE_GIT = $false
# note: cant use simple gitignore bc powershell does not write utf-8 that can be properly be picked up by git
$gitExcludes = @("*.txt", "*.log","*.ps1","*.bat","!LCChallangeFile","!LCGeneralSaveData", "!LCSaveFile*", "!SaveFile.es3")
$gitignoreFileName = ".gitignore"
$gitignoreContents = @"
*.txt`r`n
*.log`r`n
*.ps1`r`n
*.bat`r`n
`r`n
# never ignore these files`r`n
!LCChallangeFile`r`n
!LCGeneralSaveData`r`n
!LCSaveFile`r`n
!SaveFile.es3`r`n
"@
$inefficientBackupDirectory = "$($env:LOCALAPPDATA)Low\ZeekerssRBLX\Lethal Company\Backups"
$inefficientBackupFilePath = "$($env:LOCALAPPDATA)Low\ZeekerssRBLX\Lethal Company\BackupLCSaveFiles_fs.ps1"
$inefficientBackupExecutableName = "RunBackup_fs.bat"
$inefficientBackupContents = @"
# V0.1 --> Only save latest version
Set-Location `$PSScriptRoot
`$filesToSave = @("$($BACKUP_FILES -join '","')")
`$saveDirectory = "$($inefficientBackupDirectory)"
if (!(Test-Path -Path `$saveDirectory)) {
mkdir `$saveDirectory
}
Get-ChildItem |
Foreach-Object {
if (`$filesToSave.Contains(`$_.Name)) {
Write-Host Saving `$_.FullName
Copy-Item `$_.FullName -Destination `$saveDirectory
}
}
"@
$inefficientRestoreFilePath = "$($env:LOCALAPPDATA)Low\ZeekerssRBLX\Lethal Company\RestoreLCSaveFiles_fs.ps1"
$inefficientRestoreExecutableName = "RunRestore_fs.bat"
$inefficientRestoreContents = @"
# V0.1 --> Only restore from single version
Set-Location `$PSScriptRoot
`$saveDirectory = "$($inefficientBackupDirectory)"
if (!(Test-Path -Path `$saveDirectory)) {
Write-Host "Backup directory not found!"
Exit 1
}
Copy-Item -Path `$saveDirectory\* -Destination `$PSScriptRoot -Recurse
Write-Host Restored!
"@
$askToInstallGitMsg = @"
You do not have git installed.
[Yes] => Will install it now.
[No] => Will use a less efficent method of creating backups.
Protip: Don't install things you are not familiar with. That includes git if you haven't heard of it. Although, if you got this far, why not do it anyways 🤡
"@
$quickBackupFilePath = "$($env:LOCALAPPDATA)Low\ZeekerssRBLX\Lethal Company\BackupLCSaveFiles_git.ps1"
$quickBackupExecutableName = "QuickBackup.bat"
$quickBackupWithGitContents = @"
Set-Location `$PSScriptRoot
try
{
# attempt to commit current file contents to repo
git add -A
git commit --allow-empty-message --no-edit
git log -n 1 --pretty=format:"Last saved hash: %H%nLast saved timestamp: %aD"
pause
}
catch
{
Write-Host "Failed to backup data to git repo, will use the inefficient method instead!"
$($CWD)/$($inefficientBackupFilePath)
}
"@
$quickRestoreFilePath = $env:LOCALAPPDATA + "Low\ZeekerssRBLX\Lethal Company\RestoreLCSaveFiles_git.ps1"
$quickRestoreExecutableName = "QuickRestore.bat"
$quickRestoreGitContents = @"
Set-Location `$PSScriptRoot
`$msgBoxInput = [System.Windows.Forms.MessageBox]::Show("This will reset latest unsaved progress, are you sure you want to continue?",'Restore?','YesNoCancel','Error')
if (`$msgBoxInput -eq 'Yes') {
git reset --hard
git log -n 1 --pretty=format:"Restored from hash: %H%nRestored from timestamp: %aD"
}
"@
Function WriteBatFileToExecPs1($outfilePath, $ps1FilePath){
Write-Host $outfilePath
Write-Host $ps1FilePath
$content = @"
powershell.exe -ExecutionPolicy Bypass -File "$($ps1FilePath)"
pause
"@
Set-Content -Path "$($CWD)\$($outfilePath)" -Value $content
}
# RUN
## Test for expected directory
if (!(Test-Path -Path $LC_LOCAL_PATH)) {
Write-Host "I CANT FIND YOUR LETHAL COMPANY DIRECTORY CONTAINING YOUR SAVE FILES!"
Write-Host "I dont feel like writing complex code to figure it out so ill just say gg, glhf for now, ask around"
Pause
Exit 1
}
## Is git is installed?
try
{
git | Out-Null
$USE_GIT = $true
}
catch [System.Management.Automation.CommandNotFoundException]
{
$msgBoxInput = [System.Windows.Forms.MessageBox]::Show($askToInstallGitMsg,'Install Git?','YesNoCancel','Error')
switch ($msgBoxInput) {
'Yes' {
[System.Windows.Forms.MessageBox]::Show('We are about to install git, after git is installed you might need to re-run this script!')
winget install --id Git.Git -e --source winget
}
'No' {
Write-Host "We will not install git, script will generate script to generate backups purely with copy/paste files."
}
'Cancel' {
Write-Host "Canceled"
Exit 3
}
}
}
## Write the simlpe way to save files, (to be used as a backup if git fails somehow)
Set-Content -Path $inefficientBackupFilePath -Value $inefficientBackupContents
# writes quick executable to wherever you are running this from.
WriteBatFileToExecPs1 $inefficientBackupExecutableName $inefficientBackupFilePath
Write-Host "The less efficent method of backing up your save files can be executed by simply executing the file: $($inefficientBackupExecutableName)"
Set-Content -Path $inefficientRestoreFilePath -Value $inefficientRestoreContents
WriteBatFileToExecPs1 $inefficientRestoreExecutableName $inefficientRestoreFilePath
if ($USE_GIT) {
Write-Host "We will also write the better method of creating backups via the git command."
## STEP 1: Go to the directory where the LC save files exist
Set-Location $LC_LOCAL_PATH | Out-Null
## STEP 2: Initalize a git repo
if (!(Test-Path "$($LC_LOCAL_PATH)\.git")) {
Write-Host "Creating git repo at this location $(pwd)"
git init | Out-Null
} else {
Write-Host "Git repo already exists at this location $(pwd)"
}
## STEP 3: Generate the .gitignore file
### note: cant use simple gitignore bc powershell does not write utf-8 that can be properly be picked up by git will write directly to .git/info/exclude
Write-Host "Writing gitignore file so we dont save useless log files"
Set-Content -Path $gitignoreFileName -Value $gitignoreContents -Encoding UTF8
## STEP 4: Commit .gitignore file to repo
Write-Host "Committing .gitignore file to repo"
git add $gitignoreFileName | Out-Null
git commit -m "Adding .gitignore" | Out-Null
## note: not git push, if you want to be able to transfer save files then maybe add an upstream origin.
## STEP 5: write QuickBackup, and QuickRestore script and executable
Set-Content -Path $quickBackupFilePath -Value $quickBackupWithGitContents
WriteBatFileToExecPs1 $quickBackupExecutableName $quickBackupFilePath
Write-Host "Backing up your save files can be executed by simply executing the file: $($quickBackupExecutableName)"
Set-Content -Path $quickRestoreFilePath -Value $quickRestoreGitContents
WriteBatFileToExecPs1 $quickRestoreExecutableName $quickRestoreFilePath
}
$saveOptExe = $inefficientBackupExecutableName
$restoreOptExe = $inefficientRestoreExecutableName
if ($USE_GIT) {
$saveOptExe = "$($quickBackupExecutableName) OR $($saveOptExe)"
$restoreOptExe = "$($quickRestoreExecutableName) OR $($restoreOptExe)"
}
$report = @"
Completed setup, read the command line logs!
Pressing OK will close this window and the script logs!
TL;DR:
Backup save files by running: $($saveOptExe)
Restore save files by running: $($restoreOptExe)
Should work even while game is running.
"@
[System.Windows.Forms.MessageBox]::Show($report)