-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonSerialization.vb
More file actions
344 lines (321 loc) · 13.5 KB
/
Copy pathjsonSerialization.vb
File metadata and controls
344 lines (321 loc) · 13.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
Imports Newtonsoft.Json
Imports digitalSlate.World.Functions
Imports digitalSlate.World.mainClass
Imports digitalSlate.World.Vars.vDefaults
Imports System.IO
Imports System.Windows.Forms
Imports System.Diagnostics
Public Class SettingsProfile
Public Property SchemaVersion As Integer = 2
' NOTE: This profile is intentionally limited to slate content/state (scene, roll, etc.).
' Includes slate content plus portability-critical runtime prefs for LTC/logging/UI behavior.
Public Property Scene As String
Public Property ScenePre As String
Public Property SceneNum As Integer
Public Property Shot As String
' numeric types
Public Property Take As Integer
Public Property Roll As String
Public Property CameraNum As String
Public Property CamCardNum As Integer
Public Property Production As String
Public Property Director As String
Public Property DOP As String
Public Property FPS As Double
Public Property CustDate As String
Public Property CurrentDate As String
Public Property [Int] As Integer
Public Property [Day] As Integer
Public Property [Sync] As Integer
Public Property LtcEnabled As Integer
Public Property LtcFpsMode As Integer
Public Property LtcOutputDeviceId As Integer
Public Property LtcUnmute As Integer
Public Property SkipSound As Integer
Public Property ShowCountdownNumbers As Integer
Public Property AlwaysFullPreroll As Integer
Public Property MetadataFlashFpsEnabled As Integer
Public Property MetadataFlashDateEnabled As Integer
Public Property LogOutToFile As Integer
Public Property LogOutputFolder As String
Public Property MarkerAppendDaily As Integer
Public Property SessionId As String
Public Property UnitName As String
Public Property OperatorName As String
End Class
Public Class JsonSettingsManager
' Centralized file path constants
Private Const ProfilesFileName As String = "allProfiles.json"
Private Const SettingsProfileFormat As String = "settingsProfile{0}.json"
' Simple wrappers kept Shared for convenience
Public Shared Function SerializeSettings(settings As SettingsProfile) As String
Return Newtonsoft.Json.JsonConvert.SerializeObject(settings)
End Function
Public Shared Function DeserializeSettings(json As String) As SettingsProfile
Return Newtonsoft.Json.JsonConvert.DeserializeObject(Of SettingsProfile)(json)
End Function
' Consolidated single pair for saving/loading all profiles
Public Shared Sub SaveAllProfilesToFile(profiles As SettingsProfileWrapper, Optional filePath As String = ProfilesFileName)
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(profiles, Newtonsoft.Json.Formatting.Indented)
System.IO.File.WriteAllText(filePath, json)
End Sub
Public Shared Function LoadAllProfilesFromFile(Optional filePath As String = ProfilesFileName) As SettingsProfileWrapper
Try
If System.IO.File.Exists(filePath) Then
Dim json As String = System.IO.File.ReadAllText(filePath)
Dim loaded = Newtonsoft.Json.JsonConvert.DeserializeObject(Of SettingsProfileWrapper)(json)
If loaded Is Nothing Then
Return New SettingsProfileWrapper() With {.Profiles = New Dictionary(Of Integer, SettingsProfile)}
End If
If loaded.Profiles Is Nothing Then
loaded.Profiles = New Dictionary(Of Integer, SettingsProfile)
End If
Return loaded
Else
Return New SettingsProfileWrapper() With {.Profiles = New Dictionary(Of Integer, SettingsProfile)}
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"LoadAllProfilesFromFile: exception reading {filePath}: {ex.Message}")
Return New SettingsProfileWrapper() With {.Profiles = New Dictionary(Of Integer, SettingsProfile)}
End Try
End Function
' Save/load a single profile inside the consolidated allProfiles file.
' Return Boolean; UI layer should handle user messaging.
Public Shared Function SaveProfile(profileNumber As Integer) As Boolean
Try
Dim includeMetadata As Boolean = World.Functions.IsSessionMetadataEnabled()
Dim profilesWrapper = LoadAllProfilesFromFile(ProfilesFileName)
Dim settings As New SettingsProfile With {
.Scene = World.vMain.scene,
.ScenePre = World.vMain.scenePre,
.SceneNum = World.vMain.sceneNum,
.Shot = World.vMain.shot,
.Take = World.vMain.take,
.Roll = World.vMain.roll,
.CameraNum = World.vMain.cameraNum,
.CamCardNum = World.vMain.camCardNum,
.Production = World.vMain.production,
.Director = World.vMain.director,
.DOP = World.vMain.dop,
.FPS = World.vMain.fps,
.CustDate = World.vMain.custDate,
.CurrentDate = World.vMain.currentDate,
.Int = World.vMain.int,
.Day = World.vMain.day,
.Sync = World.vMain.sync,
.LtcEnabled = World.vMain.ltcEnabled,
.LtcFpsMode = World.vMain.ltcFpsMode,
.LtcOutputDeviceId = World.vMain.ltcOutputDeviceId,
.LtcUnmute = World.vMain.ltcUnmute,
.SkipSound = World.vMain.skipSound,
.ShowCountdownNumbers = World.vMain.showCountdownNumbers,
.AlwaysFullPreroll = World.vMain.alwaysFullPreroll,
.MetadataFlashFpsEnabled = World.vMain.metadataFlashFpsEnabled,
.MetadataFlashDateEnabled = World.vMain.metadataFlashDateEnabled,
.LogOutToFile = World.vMain.logOutToFile,
.LogOutputFolder = World.vMain.logOutputFolder,
.MarkerAppendDaily = World.vMain.markerAppendDaily,
.SessionId = If(includeMetadata, World.vMain.sessionId, String.Empty),
.UnitName = If(includeMetadata, World.vMain.unitName, String.Empty),
.OperatorName = If(includeMetadata, World.vMain.operatorName, String.Empty)
}
profilesWrapper.Profiles(profileNumber) = settings
SaveAllProfilesToFile(profilesWrapper, ProfilesFileName)
Return True
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"SaveProfile: failed to save profile {profileNumber}: {ex.Message}")
Return False
End Try
End Function
Public Shared Function LoadProfile(profileNumber As Integer) As Boolean
Try
Dim profilesWrapper = LoadAllProfilesFromFile(ProfilesFileName)
If profilesWrapper.Profiles.ContainsKey(profileNumber) Then
Dim settings As SettingsProfile = profilesWrapper.Profiles(profileNumber)
World.vMain.scene = settings.Scene
World.vMain.scenePre = settings.ScenePre
World.vMain.sceneNum = settings.SceneNum
World.vMain.shot = settings.Shot
World.vMain.take = settings.Take
World.vMain.roll = settings.Roll
World.vMain.cameraNum = settings.CameraNum
World.vMain.camCardNum = settings.CamCardNum
World.vMain.production = settings.Production
World.vMain.director = settings.Director
World.vMain.dop = settings.DOP
World.vMain.fps = settings.FPS
World.vMain.custDate = settings.CustDate
World.vMain.currentDate = settings.CurrentDate
World.vMain.int = settings.Int
World.vMain.day = settings.Day
World.vMain.sync = settings.Sync
World.vMain.ltcEnabled = settings.LtcEnabled
World.vMain.ltcFpsMode = settings.LtcFpsMode
World.vMain.ltcOutputDeviceId = settings.LtcOutputDeviceId
World.vMain.ltcUnmute = settings.LtcUnmute
World.vMain.skipSound = settings.SkipSound
World.vMain.showCountdownNumbers = settings.ShowCountdownNumbers
World.vMain.alwaysFullPreroll = settings.AlwaysFullPreroll
World.vMain.metadataFlashFpsEnabled = settings.MetadataFlashFpsEnabled
World.vMain.metadataFlashDateEnabled = settings.MetadataFlashDateEnabled
World.vMain.logOutToFile = settings.LogOutToFile
World.vMain.logOutputFolder = settings.LogOutputFolder
World.vMain.markerAppendDaily = settings.MarkerAppendDaily
If World.Functions.IsSessionMetadataEnabled() Then
World.vMain.sessionId = settings.SessionId
World.vMain.unitName = settings.UnitName
World.vMain.operatorName = settings.OperatorName
If String.IsNullOrWhiteSpace(World.vMain.sessionId) Then
World.vMain.sessionId = World.Functions.GenerateNewSessionId()
End If
Else
World.vMain.sessionId = String.Empty
World.vMain.unitName = String.Empty
World.vMain.operatorName = String.Empty
End If
Return True
Else
System.Diagnostics.Debug.WriteLine($"LoadProfile: requested profile {profileNumber} not found in {ProfilesFileName}.")
Return False
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"LoadProfile: exception loading profile {profileNumber}: {ex.Message}")
Return False
End Try
End Function
' Single-file operations for one-profile-per-file
Public Shared Sub SaveSlateToFile(settings As SettingsProfile, filePath As String)
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(settings, Newtonsoft.Json.Formatting.Indented)
System.IO.File.WriteAllText(filePath, json)
End Sub
Public Shared Function TryLoadSlateFromFile(filePath As String, ByRef settingsOut As SettingsProfile) As Boolean
settingsOut = Nothing
Try
If Not System.IO.File.Exists(filePath) Then
Return False
End If
Dim json As String = System.IO.File.ReadAllText(filePath)
Dim loaded = Newtonsoft.Json.JsonConvert.DeserializeObject(Of SettingsProfile)(json)
If loaded Is Nothing Then
Return False
End If
If loaded.SceneNum < 0 Then
Return False
End If
settingsOut = loaded
Return True
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"TryLoadSlateFromFile: exception reading {filePath}: {ex.Message}")
Return False
End Try
End Function
Public Shared Sub SaveSettingsProfileToFile(settings As SettingsProfile, profileNumber As Integer)
Dim filePath As String = String.Format(SettingsProfileFormat, profileNumber)
SaveSlateToFile(settings, filePath)
End Sub
Public Shared Function TryLoadSettingsProfileFromFile(profileNumber As Integer, ByRef settingsOut As SettingsProfile) As Boolean
Dim filePath As String = String.Format(SettingsProfileFormat, profileNumber)
Return TryLoadSlateFromFile(filePath, settingsOut)
End Function
' Reflection helpers retained for runtime defaults
Private Shared Function GetWorldDefaultsInstance() As Object
Try
Dim worldType = GetType(World.vDefaults)
Dim fi = worldType.GetField("vDefaults", Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
If fi IsNot Nothing Then
Return fi.GetValue(Nothing)
End If
Dim pi = worldType.GetProperty("vDefaults", Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
If pi IsNot Nothing Then
Return pi.GetValue(Nothing, Nothing)
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"GetWorldDefaultsInstance: reflection failed: {ex.Message}")
End Try
Return Nothing
End Function
Private Shared Function GetMemberValue(obj As Object, memberName As String) As Object
If obj Is Nothing Then Return Nothing
Try
Dim t = obj.GetType()
Dim fi = t.GetField(memberName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
If fi IsNot Nothing Then
Return fi.GetValue(obj)
End If
Dim pi = t.GetProperty(memberName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
If pi IsNot Nothing Then
Return pi.GetValue(obj, Nothing)
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"GetMemberValue: failed to read '{memberName}': {ex.Message}")
End Try
Return Nothing
End Function
Private Shared Function GetDefaultAs(Of T)(memberName As String, fallback As T) As T
Try
Dim defaultsObj = GetWorldDefaultsInstance()
If defaultsObj Is Nothing Then
Return fallback
End If
Dim val = GetMemberValue(defaultsObj, memberName)
If val Is Nothing Then
Return fallback
End If
Return CType(Convert.ChangeType(val, GetType(T)), T)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"GetDefaultAs: failed for '{memberName}': {ex.Message}")
Return fallback
End Try
End Function
Public Shared Function CreateDefaultSettingsProfile() As SettingsProfile
Try
Dim defaultSceneNum As Integer = GetDefaultAs(Of Integer)("sceneNum", 1)
Dim defaultShot As String = GetDefaultAs(Of String)("shot", String.Empty)
Dim defaultCameraNum As String = GetDefaultAs(Of String)("cameraNum", String.Empty)
Dim defaultCamCardNum As Integer = GetDefaultAs(Of Integer)("camCardNum", 0)
Dim defaultProduction As String = GetDefaultAs(Of String)("production", String.Empty)
Dim defaultDirector As String = GetDefaultAs(Of String)("director", String.Empty)
Dim defaultFPS As Double = GetDefaultAs(Of Double)("fps", 24)
Return New SettingsProfile() With {
.Scene = String.Empty,
.ScenePre = String.Empty,
.SceneNum = defaultSceneNum,
.Shot = defaultShot,
.Take = 1,
.Roll = String.Empty,
.CameraNum = defaultCameraNum,
.CamCardNum = defaultCamCardNum,
.Production = defaultProduction,
.Director = defaultDirector,
.DOP = String.Empty,
.FPS = defaultFPS,
.CustDate = String.Empty,
.CurrentDate = String.Empty,
.Int = 0,
.Day = 0,
.Sync = 0,
.LtcEnabled = 0,
.LtcFpsMode = 1,
.LtcOutputDeviceId = -1,
.LtcUnmute = 0,
.SkipSound = 0,
.ShowCountdownNumbers = 0,
.AlwaysFullPreroll = 1,
.MetadataFlashFpsEnabled = 1,
.MetadataFlashDateEnabled = 1,
.LogOutToFile = 0,
.LogOutputFolder = "",
.MarkerAppendDaily = 0,
.SessionId = "",
.UnitName = "",
.OperatorName = ""
}
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"CreateDefaultSettingsProfile: failed: {ex.Message}")
Return New SettingsProfile()
End Try
End Function
End Class
Public Class SettingsProfileWrapper
Public Property Profiles As Dictionary(Of Integer, SettingsProfile)
End Class