forked from Fei-Away/Codex-Dream-Skin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-windows.ps1
More file actions
398 lines (380 loc) · 17.5 KB
/
Copy paththeme-windows.ps1
File metadata and controls
398 lines (380 loc) · 17.5 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
if (-not (Get-Command Read-DreamSkinUtf8File -ErrorAction SilentlyContinue)) {
. (Join-Path $PSScriptRoot 'config-utf8.ps1')
}
$script:DreamSkinMaxImageBytes = 16 * 1024 * 1024
function Assert-DreamSkinNoReparseComponents {
param([Parameter(Mandatory = $true)][string]$Path)
$fullPath = [System.IO.Path]::GetFullPath($Path)
$root = [System.IO.Path]::GetPathRoot($fullPath)
$current = $fullPath
while ($true) {
if (Test-Path -LiteralPath $current) {
$item = Get-Item -LiteralPath $current -Force -ErrorAction Stop
if (($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
throw "Managed Dream Skin path contains a junction or symbolic link: $current"
}
}
$currentNormalized = $current.TrimEnd('\')
$rootNormalized = $root.TrimEnd('\')
if ($currentNormalized.Equals($rootNormalized, [System.StringComparison]::OrdinalIgnoreCase)) { break }
$parent = [System.IO.Path]::GetDirectoryName($current)
if (-not $parent -or $parent.Equals($current, [System.StringComparison]::OrdinalIgnoreCase)) { break }
$current = $parent
}
}
function Ensure-DreamSkinManagedDirectory {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][string]$Root
)
$fullPath = [System.IO.Path]::GetFullPath($Path)
$fullRoot = [System.IO.Path]::GetFullPath($Root).TrimEnd('\')
if (-not ($fullPath.Equals($fullRoot, [System.StringComparison]::OrdinalIgnoreCase) -or
$fullPath.StartsWith($fullRoot + '\', [System.StringComparison]::OrdinalIgnoreCase))) {
throw "Managed Dream Skin path escaped its state root: $fullPath"
}
Assert-DreamSkinNoReparseComponents -Path $fullPath
if (Test-Path -LiteralPath $fullPath -PathType Leaf) {
throw "Managed Dream Skin path is a file, not a directory: $fullPath"
}
New-Item -ItemType Directory -Force -Path $fullPath | Out-Null
Assert-DreamSkinNoReparseComponents -Path $fullPath
if (-not (Test-Path -LiteralPath $fullPath -PathType Container)) {
throw "Managed Dream Skin directory could not be created: $fullPath"
}
}
function Get-DreamSkinValidatedImageMetadata {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Get-Command Get-DreamSkinNodeRuntime -ErrorAction SilentlyContinue)) {
throw 'Node.js runtime validation is unavailable for image metadata checks.'
}
$node = Get-DreamSkinNodeRuntime
$metadataScript = Join-Path $PSScriptRoot 'image-metadata.mjs'
$output = @(& $node.Path $metadataScript '--check' ([System.IO.Path]::GetFullPath($Path)) 2>&1)
if ($LASTEXITCODE -ne 0) {
throw "Image metadata is invalid or exceeds the 16384px / 50MP safety limit: $Path"
}
try { $metadata = ($output -join "`n") | ConvertFrom-Json -ErrorAction Stop } catch {
throw "Image metadata helper returned invalid output: $Path"
}
if ($null -eq $metadata -or $null -eq $metadata.width -or $null -eq $metadata.height) {
throw "Image metadata is invalid or exceeds the 16384px / 50MP safety limit: $Path"
}
}
function Assert-DreamSkinImageFile {
param(
[Parameter(Mandatory = $true)][string]$Path,
[switch]$SkipImageMetadata
)
$fullPath = [System.IO.Path]::GetFullPath($Path)
if (-not (Test-Path -LiteralPath $fullPath -PathType Leaf)) {
throw "Image does not exist: $fullPath"
}
$extension = [System.IO.Path]::GetExtension($fullPath).ToLowerInvariant()
if ($extension -notin @('.png', '.jpg', '.jpeg', '.webp')) {
throw "Unsupported image format: $extension"
}
$length = (Get-Item -LiteralPath $fullPath -Force).Length
if ($length -lt 1) { throw 'Theme image cannot be empty.' }
if ($length -gt $script:DreamSkinMaxImageBytes) {
throw 'Theme image exceeds the 16 MB limit.'
}
if (-not $SkipImageMetadata) {
Get-DreamSkinValidatedImageMetadata -Path $fullPath
}
}
function Get-DreamSkinThemePaths {
param([string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin'))
$fullRoot = [System.IO.Path]::GetFullPath($StateRoot)
return [pscustomobject]@{
Root = $fullRoot
Active = Join-Path $fullRoot 'active-theme'
Saved = Join-Path $fullRoot 'themes'
Images = Join-Path $fullRoot 'images'
PauseFile = Join-Path $fullRoot 'paused'
State = Join-Path $fullRoot 'state.json'
}
}
function Test-DreamSkinThemePathWithin {
param([string]$Path, [string]$Root)
if (-not $Path -or -not $Root) { return $false }
try {
$fullPath = [System.IO.Path]::GetFullPath($Path)
$fullRoot = [System.IO.Path]::GetFullPath($Root).TrimEnd('\')
$inside = $fullPath.Equals($fullRoot, [System.StringComparison]::OrdinalIgnoreCase) -or
$fullPath.StartsWith($fullRoot + '\', [System.StringComparison]::OrdinalIgnoreCase)
if (-not $inside) { return $false }
$current = $fullPath.TrimEnd('\')
while ($true) {
if (-not (Test-Path -LiteralPath $current)) { return $false }
$item = Get-Item -LiteralPath $current -Force -ErrorAction Stop
if (($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
return $false
}
if ($current.Equals($fullRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
return $true
}
$parent = [System.IO.Path]::GetDirectoryName($current)
if (-not $parent -or $parent.Equals($current, [System.StringComparison]::OrdinalIgnoreCase)) {
return $false
}
$current = $parent.TrimEnd('\')
}
} catch {
return $false
}
}
function Read-DreamSkinTheme {
param(
[Parameter(Mandatory = $true)][string]$ThemeDirectory,
[switch]$SkipImageMetadata
)
$directory = [System.IO.Path]::GetFullPath($ThemeDirectory)
Assert-DreamSkinNoReparseComponents -Path $directory
$themePath = Join-Path $directory 'theme.json'
Assert-DreamSkinNoReparseComponents -Path $themePath
if (-not (Test-Path -LiteralPath $themePath -PathType Leaf)) {
throw "Theme metadata is missing: $themePath"
}
try {
$theme = (Read-DreamSkinUtf8File -Path $themePath) | ConvertFrom-Json -ErrorAction Stop
} catch {
throw "Theme metadata is invalid JSON: $themePath"
}
if ($null -eq $theme -or $theme -is [string] -or $theme -is [array] -or -not $theme.image) {
throw "Theme metadata must be an object with a relative image path: $themePath"
}
$image = "$($theme.image)"
if ([System.IO.Path]::IsPathRooted($image)) { throw 'Theme image path must be relative.' }
$imagePath = [System.IO.Path]::GetFullPath((Join-Path $directory $image))
if (-not (Test-DreamSkinThemePathWithin -Path $imagePath -Root $directory) -or
-not (Test-Path -LiteralPath $imagePath -PathType Leaf)) {
throw 'Theme image must remain inside its theme directory and exist.'
}
Assert-DreamSkinImageFile -Path $imagePath -SkipImageMetadata:$SkipImageMetadata
return [pscustomobject]@{
Directory = $directory
ThemePath = $themePath
ImagePath = $imagePath
Theme = $theme
}
}
function Write-DreamSkinTheme {
param(
[Parameter(Mandatory = $true)][string]$ThemeDirectory,
[Parameter(Mandatory = $true)][object]$Theme
)
Assert-DreamSkinNoReparseComponents -Path $ThemeDirectory
New-Item -ItemType Directory -Force -Path $ThemeDirectory | Out-Null
Assert-DreamSkinNoReparseComponents -Path $ThemeDirectory
$json = $Theme | ConvertTo-Json -Depth 8
$themePath = Join-Path $ThemeDirectory 'theme.json'
Assert-DreamSkinNoReparseComponents -Path $themePath
Write-DreamSkinUtf8FileAtomically -Path $themePath -Content ($json + "`r`n")
}
function Initialize-DreamSkinThemeStore {
param(
[Parameter(Mandatory = $true)][string]$SkillRoot,
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin')
)
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
foreach ($directory in @($paths.Root, $paths.Active, $paths.Saved, $paths.Images)) {
Ensure-DreamSkinManagedDirectory -Path $directory -Root $paths.Root
}
$assetRoot = Join-Path $SkillRoot 'assets'
$assetImage = Join-Path $assetRoot 'dream-reference.jpg'
Assert-DreamSkinImageFile -Path $assetImage
$activeTheme = Join-Path $paths.Active 'theme.json'
Assert-DreamSkinNoReparseComponents -Path $activeTheme
if (-not (Test-Path -LiteralPath $activeTheme -PathType Leaf)) {
Ensure-DreamSkinManagedDirectory -Path $paths.Active -Root $paths.Root
Assert-DreamSkinNoReparseComponents -Path (Join-Path $paths.Active 'dream-reference.jpg')
$activeImage = Join-Path $paths.Active 'dream-reference.jpg'
Copy-Item -LiteralPath (Join-Path $assetRoot 'dream-reference.jpg') `
-Destination $activeImage -Force
Assert-DreamSkinNoReparseComponents -Path $activeImage
Assert-DreamSkinImageFile -Path $activeImage
$imageArchive = Join-Path $paths.Images 'dream-reference.jpg'
Assert-DreamSkinNoReparseComponents -Path $imageArchive
Copy-Item -LiteralPath (Join-Path $assetRoot 'dream-reference.jpg') `
-Destination $imageArchive -Force
Assert-DreamSkinNoReparseComponents -Path $imageArchive
Assert-DreamSkinImageFile -Path $imageArchive
Assert-DreamSkinNoReparseComponents -Path $activeTheme
Copy-Item -LiteralPath (Join-Path $assetRoot 'theme.json') -Destination $activeTheme -Force
}
$presetDirectory = Join-Path $paths.Saved 'preset-romantic-rose'
$presetTheme = Join-Path $presetDirectory 'theme.json'
Assert-DreamSkinNoReparseComponents -Path $presetDirectory
Assert-DreamSkinNoReparseComponents -Path $presetTheme
if (-not (Test-Path -LiteralPath $presetTheme -PathType Leaf)) {
Ensure-DreamSkinManagedDirectory -Path $presetDirectory -Root $paths.Root
$presetImage = Join-Path $presetDirectory 'dream-reference.jpg'
Assert-DreamSkinNoReparseComponents -Path $presetImage
Copy-Item -LiteralPath (Join-Path $assetRoot 'dream-reference.jpg') `
-Destination $presetImage -Force
Assert-DreamSkinNoReparseComponents -Path $presetImage
Assert-DreamSkinImageFile -Path $presetImage
Assert-DreamSkinNoReparseComponents -Path $presetTheme
Copy-Item -LiteralPath (Join-Path $assetRoot 'theme.json') -Destination $presetTheme -Force
}
$null = Read-DreamSkinTheme -ThemeDirectory $paths.Active
return $paths
}
function New-DreamSkinThemeImageName {
param([Parameter(Mandatory = $true)][string]$Extension)
return 'art-' + (Get-Date).ToString('yyyyMMdd-HHmmss-fff') + '-' +
[guid]::NewGuid().ToString('N').Substring(0, 8) + $Extension.ToLowerInvariant()
}
function Set-DreamSkinActiveTheme {
param(
[Parameter(Mandatory = $true)][string]$ImagePath,
[AllowNull()][object]$Theme,
[string]$Name,
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin')
)
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
Ensure-DreamSkinManagedDirectory -Path $paths.Root -Root $paths.Root
Ensure-DreamSkinManagedDirectory -Path $paths.Active -Root $paths.Root
Ensure-DreamSkinManagedDirectory -Path $paths.Images -Root $paths.Root
$source = [System.IO.Path]::GetFullPath($ImagePath)
Assert-DreamSkinImageFile -Path $source
$extension = [System.IO.Path]::GetExtension($source).ToLowerInvariant()
$oldImage = $null
try { $oldImage = (Read-DreamSkinTheme -ThemeDirectory $paths.Active).ImagePath } catch {}
if ($null -eq $Theme) {
$Theme = [pscustomobject]@{
id = 'custom'
name = '自定义主题'
appearance = 'auto'
art = [pscustomobject]@{ focusX = $null; focusY = $null; safeArea = 'auto'; taskMode = 'auto' }
palette = [pscustomobject]@{}
}
}
$imageName = New-DreamSkinThemeImageName -Extension $extension
$target = Join-Path $paths.Active $imageName
$temporary = Join-Path $paths.Active ('.dream-tmp-' + [guid]::NewGuid().ToString('N') + $extension)
try {
Assert-DreamSkinNoReparseComponents -Path $target
Assert-DreamSkinNoReparseComponents -Path $temporary
Copy-Item -LiteralPath $source -Destination $temporary -Force
Assert-DreamSkinNoReparseComponents -Path $temporary
Assert-DreamSkinImageFile -Path $temporary
Move-Item -LiteralPath $temporary -Destination $target -Force
Assert-DreamSkinNoReparseComponents -Path $target
Assert-DreamSkinImageFile -Path $target
$Theme | Add-Member -NotePropertyName image -NotePropertyValue $imageName -Force
if ($Name) { $Theme | Add-Member -NotePropertyName name -NotePropertyValue $Name -Force }
if (-not $Theme.id) { $Theme | Add-Member -NotePropertyName id -NotePropertyValue 'custom' -Force }
if (-not $Theme.appearance) { $Theme | Add-Member -NotePropertyName appearance -NotePropertyValue 'auto' -Force }
if (-not $Theme.art) {
$Theme | Add-Member -NotePropertyName art -NotePropertyValue `
([pscustomobject]@{ focusX = $null; focusY = $null; safeArea = 'auto'; taskMode = 'auto' }) -Force
}
if (-not $Theme.palette) {
$Theme | Add-Member -NotePropertyName palette -NotePropertyValue ([pscustomobject]@{}) -Force
}
Write-DreamSkinTheme -ThemeDirectory $paths.Active -Theme $Theme
} finally {
Remove-Item -LiteralPath $temporary -Force -ErrorAction SilentlyContinue
}
$sameImage = $oldImage -and ([System.IO.Path]::GetFullPath($oldImage) -ieq [System.IO.Path]::GetFullPath($target))
if ($oldImage -and -not $sameImage -and
(Test-DreamSkinThemePathWithin -Path $oldImage -Root $paths.Active)) {
Remove-Item -LiteralPath $oldImage -Force -ErrorAction SilentlyContinue
}
$imageArchive = Join-Path $paths.Images $imageName
Assert-DreamSkinNoReparseComponents -Path $imageArchive
Copy-Item -LiteralPath $target -Destination $imageArchive -Force
Assert-DreamSkinNoReparseComponents -Path $imageArchive
Assert-DreamSkinImageFile -Path $imageArchive
return Read-DreamSkinTheme -ThemeDirectory $paths.Active
}
function Save-DreamSkinCurrentTheme {
param(
[Parameter(Mandatory = $true)][string]$Name,
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin')
)
$trimmed = $Name.Trim()
if (-not $trimmed -or $trimmed.Length -gt 80 -or $trimmed -match '[\u0000-\u001f]') {
throw 'Theme name must be between 1 and 80 visible characters.'
}
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
Ensure-DreamSkinManagedDirectory -Path $paths.Root -Root $paths.Root
Ensure-DreamSkinManagedDirectory -Path $paths.Saved -Root $paths.Root
$active = Read-DreamSkinTheme -ThemeDirectory $paths.Active
$id = (Get-Date).ToString('yyyyMMdd-HHmmss') + '-' + [guid]::NewGuid().ToString('N').Substring(0, 8)
$destination = Join-Path $paths.Saved $id
Ensure-DreamSkinManagedDirectory -Path $destination -Root $paths.Root
$extension = [System.IO.Path]::GetExtension($active.ImagePath).ToLowerInvariant()
$imageName = 'art' + $extension
$destinationImage = Join-Path $destination $imageName
Assert-DreamSkinNoReparseComponents -Path $destinationImage
Copy-Item -LiteralPath $active.ImagePath -Destination $destinationImage -Force
Assert-DreamSkinNoReparseComponents -Path $destinationImage
Assert-DreamSkinImageFile -Path $destinationImage
$theme = $active.Theme | ConvertTo-Json -Depth 8 | ConvertFrom-Json
$theme.id = $id
$theme.name = $trimmed
$theme.image = $imageName
Write-DreamSkinTheme -ThemeDirectory $destination -Theme $theme
return Read-DreamSkinTheme -ThemeDirectory $destination
}
function Get-DreamSkinSavedThemes {
param(
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin'),
[switch]$SkipImageMetadata
)
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
Ensure-DreamSkinManagedDirectory -Path $paths.Root -Root $paths.Root
Ensure-DreamSkinManagedDirectory -Path $paths.Saved -Root $paths.Root
if (-not (Test-Path -LiteralPath $paths.Saved -PathType Container)) { return @() }
$themes = @()
foreach ($directory in Get-ChildItem -LiteralPath $paths.Saved -Directory -ErrorAction SilentlyContinue) {
try {
$loaded = Read-DreamSkinTheme -ThemeDirectory $directory.FullName -SkipImageMetadata:$SkipImageMetadata
$themes += [pscustomobject]@{
Id = "$($loaded.Theme.id)"
Name = if ($loaded.Theme.name) { "$($loaded.Theme.name)" } else { $directory.Name }
Path = $directory.FullName
}
} catch {}
}
return @($themes | Sort-Object Name)
}
function Use-DreamSkinSavedTheme {
param(
[Parameter(Mandatory = $true)][string]$ThemeDirectory,
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin')
)
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
Ensure-DreamSkinManagedDirectory -Path $paths.Root -Root $paths.Root
Ensure-DreamSkinManagedDirectory -Path $paths.Saved -Root $paths.Root
$directory = [System.IO.Path]::GetFullPath($ThemeDirectory)
if (-not (Test-DreamSkinThemePathWithin -Path $directory -Root $paths.Saved)) {
throw 'Saved theme must remain inside the Dream Skin themes folder.'
}
$saved = Read-DreamSkinTheme -ThemeDirectory $directory
$theme = $saved.Theme | ConvertTo-Json -Depth 8 | ConvertFrom-Json
return Set-DreamSkinActiveTheme -ImagePath $saved.ImagePath -Theme $theme -StateRoot $StateRoot
}
function Set-DreamSkinPaused {
param(
[Parameter(Mandatory = $true)][bool]$Paused,
[string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin')
)
$paths = Get-DreamSkinThemePaths -StateRoot $StateRoot
Ensure-DreamSkinManagedDirectory -Path $paths.Root -Root $paths.Root
if ($Paused) {
Assert-DreamSkinNoReparseComponents -Path $paths.PauseFile
Write-DreamSkinUtf8FileAtomically -Path $paths.PauseFile -Content "paused`r`n"
} else {
if (Test-Path -LiteralPath $paths.PauseFile) { Assert-DreamSkinNoReparseComponents -Path $paths.PauseFile }
Remove-Item -LiteralPath $paths.PauseFile -Force -ErrorAction SilentlyContinue
}
return $Paused
}
function Test-DreamSkinPaused {
param([string]$StateRoot = (Join-Path $env:LOCALAPPDATA 'CodexDreamSkin'))
return (Test-Path -LiteralPath (Get-DreamSkinThemePaths -StateRoot $StateRoot).PauseFile -PathType Leaf)
}