-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Settings.vb
131 lines (112 loc) · 4.35 KB
/
Settings.vb
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
Imports System.Environment
Imports System.IO
Imports System.IO.IsolatedStorage
Public Enum Scoring
None
Standard
Vegas
VegasCumulative
End Enum
Public Class Settings
Private Const DEFAUILT_FILENAME As String = "settings.xml"
Private Const DEFAULT_USE_ISOLATED_STORAGE As Boolean = False
#Region "Loading and Saving"
Overloads Shared Function GetFileStreamForWriting() As Stream
Return GetFileStreamForWriting(DEFAUILT_FILENAME, DEFAULT_USE_ISOLATED_STORAGE)
End Function
Overloads Shared Function GetFileStreamForWriting(filename As String) As Stream
Return GetFileStreamForWriting(filename, DEFAULT_USE_ISOLATED_STORAGE)
End Function
Overloads Shared Function GetFileStreamForWriting(filename As String, useIsolatedStorage As Boolean) As Stream
If useIsolatedStorage Then
Using isf = IsolatedStorageFile.GetUserStoreForAssembly()
Using file As New IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, isf)
isf.Close()
Return file
End Using
End Using
Else
Dim workingPath = IO.Path.Combine(GetFolderPath(SpecialFolder.LocalApplicationData), Application.CompanyName)
workingPath = IO.Path.Combine(workingPath, Application.ProductName)
If Not IO.Directory.Exists(workingPath) Then
Directory.CreateDirectory(workingPath)
End If
workingPath = IO.Path.Combine(workingPath, filename)
Return New FileStream(workingPath, FileMode.Create, FileAccess.Write, FileShare.None)
End If
End Function
Overloads Shared Function GetFileStreamForReading() As Stream
Return GetFileStreamForReading(DEFAUILT_FILENAME, DEFAULT_USE_ISOLATED_STORAGE)
End Function
Overloads Shared Function GetFileStreamForReading(filename As String) As Stream
Return GetFileStreamForReading(filename, DEFAULT_USE_ISOLATED_STORAGE)
End Function
Overloads Shared Function GetFileStreamForReading(filename As String, useIsolatedStorage As Boolean) As Stream
If useIsolatedStorage Then
Using isf = IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForAssembly()
Using file As New IsolatedStorageFileStream(filename, IO.FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read, isf)
isf.Close()
Return file
End Using
End Using
Else
Dim workingPath = IO.Path.Combine(GetFolderPath(SpecialFolder.LocalApplicationData), Application.CompanyName)
workingPath = IO.Path.Combine(workingPath, Application.ProductName)
If Not IO.Directory.Exists(workingPath) Then
Directory.CreateDirectory(workingPath)
End If
workingPath = IO.Path.Combine(workingPath, filename)
Return New FileStream(workingPath, IO.FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
End If
End Function
Shared Sub Persist(settings As Settings)
Try
Using outputStream = GetFileStreamForWriting()
Persist(settings, outputStream)
End Using
Catch ex As Exception
Dim e As New IOException("Could not save Settings", ex)
Throw e
End Try
End Sub
Shared Sub Persist(settings As Settings, outputStream As Stream)
Dim xs As New Xml.Serialization.XmlSerializer(GetType(Settings))
xs.Serialize(outputStream, settings)
outputStream.Close()
End Sub
Public Sub PersistMe()
Settings.Persist(Me)
End Sub
Public Sub PersistMe(stream As Stream)
Settings.Persist(Me, stream)
End Sub
Shared Function Load() As Settings
Try
Using inputStream = GetFileStreamForReading()
Return Load(inputStream)
End Using
Catch ex As Exception
Dim e As New IOException("Could not load Settings", ex)
Throw e
End Try
End Function
Shared Function Load(stream As Stream) As Settings
Dim result As Settings
If stream.Length = 0 Then
result = New Settings
Else
Dim xs As New Xml.Serialization.XmlSerializer(GetType(Settings))
result = CType(xs.Deserialize(stream), Settings)
End If
stream.Close()
Return result
End Function
#End Region
Public Property Location() As Point ' Location of the main window.
Public Property Size() As Size ' Size of the main window.
Public Property Hints() As Boolean
Public Property StatusBar() As Boolean
Public Property BoardRows() As Integer = 8
Public Property BoardColumns() As Integer = 8
Public Property BoardDepth() As Integer = 2
End Class