-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectrumPlaylist.vb
More file actions
162 lines (124 loc) · 5.83 KB
/
Copy pathSpectrumPlaylist.vb
File metadata and controls
162 lines (124 loc) · 5.83 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
Imports System.Text
Public Class SpectrumPlaylist
Public Property Name As String
Public Property ImagePath As String
Public Property Created As DateTime
Public Property Files As New List(Of String)
Public Property PLPath As String
Public ReadOnly MAX_VER As Double = 1.1F
Public ReadOnly MIN_VER As Double = 1.0F
'==================== LOAD ====================
Public Shared Function Load(path As String) As SpectrumPlaylist
Dim pl As New SpectrumPlaylist With {.PLPath = path}
Dim hasHeader As Boolean = False
Dim hasVersion As Boolean = False
Dim inMeta As Boolean = False
For Each rawLine In IO.File.ReadAllLines(path)
Dim line = rawLine.Trim()
If line = "" Then Continue For
' DIRECTIVES
If line.StartsWith("!!!") Then
Dim content = line.Substring(3).Trim()
' Strip inline comments ONLY for directives
Dim hash = content.IndexOf("#"c)
If hash >= 0 Then content = content.Substring(0, hash).Trim()
Select Case True
Case content = "SPECTRUM_PLAYLIST"
hasHeader = True
Case content.StartsWith("SYNTAX_VERSION=")
Dim PlaylistVersions = New SpectrumPlaylist
hasVersion = True
Dim sv As Double = Val(content.Replace("SYNTAX_VERSION=", ""))
If sv > PlaylistVersions.MAX_VER And sv < PlaylistVersions.MIN_VER Then
MsgBox("The syntax version of the found playlist is unsupported. Currently supported versions are 1.0 and 1.1, please fix it and restart Spectrum to load the playlist!", MsgBoxStyle.Critical, "Error")
Exit Select
End If
Case content = "[META]"
If Not hasHeader OrElse Not hasVersion Then
Throw New Exception("META block before header/version")
End If
inMeta = True
Case inMeta AndAlso content.StartsWith("PLAYLIST_NAME=")
pl.Name = content.Substring(14)
Case inMeta AndAlso content.StartsWith("PLAYLIST_IMAGE=")
pl.ImagePath = content.Substring(15)
Case inMeta AndAlso content.StartsWith("CREATED=")
DateTime.TryParse(content.Substring(8), pl.Created)
End Select
Else
If Not line.StartsWith("#") Then
' FILE ENTRIES (allow #)
pl.Files.Add(line)
Else Continue For
End If
End If
Next
' VALIDATION
If Not hasHeader Then Throw New Exception("Missing SPECTRUM_PLAYLIST header")
If Not hasVersion Then Throw New Exception("Missing SYNTAX_VERSION")
If String.IsNullOrWhiteSpace(pl.Name) Then
pl.Name = IO.Path.GetFileNameWithoutExtension(path)
End If
Return pl
End Function
Public Sub Save()
If String.IsNullOrEmpty(PLPath) Then
Throw New InvalidOperationException("Playlist path not set")
End If
Dim sb As New StringBuilder()
' ===== HEADER =====
sb.AppendLine("!!! SPECTRUM_PLAYLIST")
sb.AppendLine("!!! SYNTAX_VERSION=" & MAX_VER.ToString())
sb.AppendLine()
' ===== META =====
sb.AppendLine("!!! [META]")
sb.AppendLine("!!! PLAYLIST_NAME=" & Name)
If Not String.IsNullOrEmpty(ImagePath) Then
sb.AppendLine("!!! PLAYLIST_IMAGE=" & ImagePath)
End If
sb.AppendLine("!!! CREATED=" & Created.ToString("o"))
sb.AppendLine()
' ===== INFO BLOCK =====
sb.AppendLine("#=======================================================================#")
sb.AppendLine("# THIS IS AN AUTO GENERATED PLAYLIST | PLEASE DO NOT REMOVE THE HEADER! #")
sb.AppendLine("# ALL PLAYLIST FILES ARE BELOW | AS WELL AS THE VERSION! #")
sb.AppendLine("#=======================================================================#")
sb.AppendLine()
' ===== FILE ENTRIES =====
For Each file In Files
sb.AppendLine(file)
Next
IO.File.WriteAllText(PLPath, sb.ToString(), Encoding.UTF8)
End Sub
Public Sub SaveCreated()
If String.IsNullOrEmpty(PLPath) Then
Throw New InvalidOperationException("Playlist path not set")
End If
Dim sb As New StringBuilder()
' ===== HEADER =====
sb.AppendLine("!!! SPECTRUM_PLAYLIST")
sb.AppendLine("!!! SYNTAX_VERSION=" & MAX_VER.ToString())
sb.AppendLine()
' ===== META =====
sb.AppendLine("!!! [META]")
sb.AppendLine("!!! PLAYLIST_NAME=" & Name)
If Not String.IsNullOrEmpty(ImagePath) Then
sb.AppendLine("!!! PLAYLIST_IMAGE=" & ImagePath)
End If
sb.AppendLine("!!! CREATED=" & Created.ToString("o"))
sb.AppendLine()
' ===== INFO BLOCK =====
sb.AppendLine("#=======================================================================#")
sb.AppendLine("# THIS IS AN AUTO GENERATED PLAYLIST | PLEASE DO NOT REMOVE THE HEADER! #")
sb.AppendLine("# ALL PLAYLIST FILES ARE BELOW | AS WELL AS THE VERSION! #")
sb.AppendLine("#=======================================================================#")
sb.AppendLine()
IO.File.WriteAllText(PLPath, sb.ToString(), Encoding.UTF8)
End Sub
Public Function ToListViewItem() As ListViewItem
Dim item As New ListViewItem(Name)
item.ImageIndex = 4
item.Tag = PLPath
Return item
End Function
End Class