Debian Codegraph @

terps/frankendrift/FrankenDrift.Adrift/clsItem.vb

raw
1 #If Adravalon Then
2 Imports FrankenDrift.Glue.Util
3 #End If
4
5 Public MustInherit Class clsItem
6
7 Public Property Key() As String
8 Public Property IsLibrary() As Boolean
9 Public Property LoadedFromLibrary As Boolean ' To distinguish library items loaded from the game - we want to prompt to overwrite library items in games, but not library items loaded from a library
10 Public Property Tag As Object
11
12 Private ReadOnly Property GenerateKey As String
13 Get
14 Dim sKey As String = Me.CommonName
15 Try
16 If sKey = "New Folder" Then sKey = "Folder"
17 If TypeOf Me Is clsObject AndAlso CType(Me, clsObject).arlNames.Count > 0 Then sKey = CType(Me, clsObject).arlNames(0)
18
19 Dim sNewKey As New System.Text.StringBuilder
20 Dim bCaps As Boolean = True
21 Dim AllKeys As List(Of String) = Adventure.AllKeys
22
23 If sKey Is Nothing Then sKey = ""
24 For iPos As Integer = 0 To sKey.Length - 1
25 If Char.IsLetterOrDigit(sKey(iPos)) Then
26 sNewKey.Append(If(bCaps, Char.ToUpper(sKey(iPos)), Char.ToLower(sKey(iPos))).ToString)
27 bCaps = False
28 Else
29 bCaps = True
30 End If
31 Next
32
33 sKey = sLeft(sNewKey.ToString, 10)
34 If sKey.Length < 2 Then
35 sKey = GetNewKey(True)
36 End If
37 If AllKeys.FindAll(Function(s) s.IndexOf(sKey, StringComparison.OrdinalIgnoreCase) >= 0).Count = 0 Then Return sKey ' Case insensitive Keys.Contains
38
39 Dim i As Integer = 1
40 Do
41 If AllKeys.FindAll(Function(s) s.IndexOf(sKey & i, StringComparison.OrdinalIgnoreCase) >= 0).Count = 0 Then Return sKey & i
42 i += 1
43 Loop
44 Finally
45 If Adventure.listExcludedItems.Contains(sKey) Then Adventure.listExcludedItems.Remove(sKey)
46 End Try
47 End Get
48 End Property
49
50
51 Public Function GetNewKey(Optional ByVal bForceOldStyle As Boolean = False) As String
52
53 If Not bForceOldStyle AndAlso SafeBool(CInt(GetSetting("ADRIFT", "Generator", "KeyNames", "-1"))) Then
54 Return GenerateKey
55 Else
56 Dim sType As String = ""
57 Select Case True
58 Case TypeOf Me Is clsLocation
59 sType = "Location"
60 Case TypeOf Me Is clsObject
61 sType = "Object"
62 Case TypeOf Me Is clsTask
63 sType = "Task"
64 Case TypeOf Me Is clsEvent
65 sType = "Event"
66 Case TypeOf Me Is clsCharacter
67 sType = "Character"
68 Case TypeOf Me Is clsVariable
69 sType = "Variable"
70 Case TypeOf Me Is clsALR
71 sType = "ALR"
72 Case TypeOf Me Is clsGroup
73 sType = "Group"
74 Case TypeOf Me Is clsHint
75 sType = "Hint"
76 Case TypeOf Me Is clsProperty
77 sType = "Property"
78 Case TypeOf Me Is clsSynonym
79 sType = "Synonym"
80 Case TypeOf Me Is clsUserFunction
81 sType = "User Function"
82 Case Else
83 TODO("Other types")
84 End Select
85
86 If sType <> "" Then
87 Key = GetNewKey(sType)
88 Return Key
89 Else
90 Return Nothing
91 End If
92 End If
93
94 End Function
95
96
97 Public Function GetNewKey(ByVal sPrefix As String) As String
98 Dim iNum As Integer = 1
99
100 While Not Adventure.GetTypeFromKey(sPrefix & iNum) Is Nothing
101 iNum += 1
102 End While
103
104 Dim sKey As String = sPrefix & iNum
105
106 If Adventure.listExcludedItems.Contains(sKey) Then Adventure.listExcludedItems.Remove(sKey)
107
108 Return sKey
109 End Function
110
111
112 Private dtCreated As Date = Date.MinValue
113 Friend Property Created() As Date
114 Get
115 If dtCreated = Date.MinValue Then
116 If dtLastUpdated < Now AndAlso dtLastUpdated > Date.MinValue Then
117 dtCreated = dtLastUpdated
118 Else
119 dtCreated = Now
120 End If
121 End If
122 Return dtCreated
123 End Get
124 Set(ByVal value As Date)
125 dtCreated = value
126 End Set
127 End Property
128
129
130 Private dtLastUpdated As Date
131 Friend Property LastUpdated() As Date
132 Get
133 If dtLastUpdated > Date.MinValue Then
134 Return dtLastUpdated
135 Else
136 Return Now
137 End If
138 End Get
139 Set(ByVal value As Date)
140 dtLastUpdated = value
141 End Set
142 End Property
143
144
145 Public MustOverride ReadOnly Property CommonName() As String
146 Public MustOverride ReadOnly Property Clone() As clsItem
147 Friend MustOverride ReadOnly Property AllDescriptions() As Generic.List(Of Description)
148
149 Public MustOverride Function ReferencesKey(ByVal sKey As String) As Integer
150 Public MustOverride Function DeleteKey(ByVal sKey As String) As Boolean
151 Friend MustOverride Function FindStringLocal(ByVal sSearchString As String, Optional ByVal sReplace As String = Nothing, Optional ByVal bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object
152
153
154 Public Function SearchFor(ByVal sSearchString As String) As Boolean
155 Dim oSearch As Object = FindString(sSearchString, , False)
156 If TypeOf oSearch Is SingleDescription Then
157 Return True
158 Else
159 Return CInt(oSearch) > 0
160 End If
161 End Function
162
163
164 Public Function FindString(ByVal sSearchString As String, Optional ByVal sReplace As String = Nothing, Optional ByVal bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object
165
166 Dim sCommonName As String = Me.CommonName
167 Try
168 For Each desc As Description In AllDescriptions
169 Dim o As Object = FindStringInDescription(desc, sSearchString, sReplace, bFindAll, iReplacements)
170 If o IsNot Nothing AndAlso Not bFindAll Then Return o
171 Next
172 Return FindStringLocal(sSearchString, sReplace, bFindAll, iReplacements)
173 Catch ex As Exception
174 ErrMsg("Error finding string " & sSearchString & " in item " & CommonName, ex)
175 End Try
176
177 Return Nothing
178 End Function
179
180
181 Private Function FindText(ByVal sTextToSearch As String, ByVal sTextToFind As String) As Boolean
182 If sTextToSearch Is Nothing Then Return False
183
184 Dim re As System.Text.RegularExpressions.Regex
185 Dim sWordBound As String = CStr(IIf(SearchOptions.bFindExactWord, "\b", ""))
186 sTextToFind = sTextToFind.Replace("\", "\\").Replace("*", ".*").Replace("?", "\?").Replace("[", "\[").Replace("]", "\]").Replace("(", "\(").Replace(")", "\)").Replace(".", "\.")
187 If SearchOptions.bSearchMatchCase Then
188 re = New System.Text.RegularExpressions.Regex(sWordBound & sTextToFind & sWordBound)
189 Else
190 re = New System.Text.RegularExpressions.Regex(sWordBound & sTextToFind & sWordBound, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
191 End If
192
193 Return re.IsMatch(sTextToSearch)
194 End Function
195
196
197 Friend Function FindStringInStringProperty(ByRef text As String, ByVal sSearchString As String, Optional ByVal sReplace As String = Nothing, Optional ByVal bFindAll As Boolean = True) As Integer
198 If FindText(text, sSearchString) Then
199 Dim iReplacements As Integer = 0
200 If sReplace IsNot Nothing Then
201 text = ReplaceString(text, sSearchString, sReplace, iReplacements)
202 Else
203 iReplacements = 1
204 End If
205 Return iReplacements
206 End If
207
208 Return Nothing
209 End Function
210
211
212 Friend Function FindStringInDescription(ByVal d As Description, ByVal sSearchString As String, Optional ByVal sReplace As String = Nothing, Optional ByVal bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As SingleDescription
213
214 For Each sd As SingleDescription In d
215 If FindText(sd.Description, sSearchString) Then
216 If sReplace IsNot Nothing Then sd.Description = ReplaceString(sd.Description, sSearchString, sReplace, iReplacements)
217 If Not bFindAll Then Return sd
218 End If
219 For Each r As clsRestriction In sd.Restrictions
220 If r.oMessage IsNot Nothing Then
221 Dim sdr As SingleDescription = FindStringInDescription(r.oMessage, sSearchString, sReplace, bFindAll, iReplacements)
222 If sdr IsNot Nothing AndAlso Not bFindAll Then Return sdr
223 End If
224 Next
225 Next
226 Return Nothing
227
228 End Function
229
230
231 Private Function ReplaceString(ByRef sText As String, ByVal sFind As String, ByVal sReplace As String, ByRef iReplacements As Integer) As String
232 Dim re As System.Text.RegularExpressions.Regex
233 Dim sWordBound As String = CStr(IIf(SearchOptions.bFindExactWord, "\b", ""))
234 sFind = sFind.Replace("\", "\\").Replace("*", ".*").Replace("?", "\?")
235 If SearchOptions.bSearchMatchCase Then
236 re = New System.Text.RegularExpressions.Regex(sWordBound & sFind & sWordBound)
237 Else
238 re = New System.Text.RegularExpressions.Regex(sWordBound & sFind & sWordBound, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
239 End If
240 iReplacements += re.Matches(sText).Count
241 sText = re.Replace(sText, sReplace)
242
243 Return sText
244 End Function
245
246 Public Function SearchAndReplace(ByVal sFind As String, ByVal sReplace As String) As Integer
247 Dim iReplacements As Integer = 0
248 FindString(sFind, sReplace, True, iReplacements)
249 Return iReplacements
250 End Function
251
252 End Class
253
254
255 Public MustInherit Class clsItemWithProperties
256 Inherits clsItem
257
258 ' These are the properties that belong to the item
259 Private _htblActualProperties As New PropertyHashTable
260 Friend Property htblActualProperties As PropertyHashTable
261 Get
262 Return _htblActualProperties
263 End Get
264 Set(value As PropertyHashTable)
265 _htblActualProperties = value
266 _htblProperties = Nothing ' To Ensure any subsequent calls to htblProperties doesn't take stale values
267 End Set
268 End Property
269
270 ' Any properties inherited from the group/class
271 Private _htblInheritedProperties As PropertyHashTable
272 Private Property htblInheritedProperties As PropertyHashTable
273 Get
274 If _htblInheritedProperties Is Nothing Then
275 _htblInheritedProperties = New PropertyHashTable
276 For Each grp As clsGroup In Adventure.htblGroups.Values
277 If grp.GroupType = PropertyGroupType Then
278 If grp.arlMembers.Contains(Key) Then
279 For Each prop As clsProperty In grp.htblProperties.Values
280 If Not _htblInheritedProperties.ContainsKey(prop.Key) Then _htblInheritedProperties.Add(prop.Copy)
281 With _htblInheritedProperties(prop.Key)
282 .Value = prop.Value
283 .StringData = prop.StringData.Copy
284 .FromGroup = True
285 End With
286 Next
287 End If
288 End If
289 Next
290 End If
291 Return _htblInheritedProperties
292 End Get
293 Set(value As PropertyHashTable)
294 _htblInheritedProperties = value
295 _htblProperties = Nothing
296 End Set
297 End Property
298
299 Protected MustOverride ReadOnly Property PropertyGroupType() As clsGroup.GroupTypeEnum
300
301 Friend MustOverride ReadOnly Property Parent As String
302
303 ' Set this to False whenever the groups change
304 'Friend bCalculatedGroups As Boolean = False
305
306
307 ' This is a view of Actual + Inherited properties for the item
308 ' If both lists have the same property, inherited one should take precendence if it has a value
309 ' If it doesn't have a value, item property should take precendence
310 '
311 ' We can cache a view of the properties. But if we add/delete any we need to recalc
312 '
313 Private _htblProperties As PropertyHashTable
314 Friend ReadOnly Property htblProperties As PropertyHashTable
315 Get
316 If _htblProperties Is Nothing Then
317 ' Need a shallow clone of actual properties (so we have unique list of original properties)
318 _htblProperties = New PropertyHashTable
319 For Each prop As clsProperty In _htblActualProperties.Values
320 _htblProperties.Add(prop)
321 Next
322
323 ' Take all actual properties, then layer on the inherited ones
324 For Each prop As clsProperty In htblInheritedProperties.Values
325 If Not HasProperty(prop.Key) Then
326 _htblProperties.Add(prop.Copy)
327 Else
328 ' We have property in both actual and inherited. Check what we want to do here...
329 With _htblProperties(prop.Key)
330 .Value = prop.Value
331 .StringData = prop.StringData.Copy
332 .FromGroup = True
333 End With
334 End If
335 Next
336 End If
337 Return _htblProperties
338 End Get
339 End Property
340
341
342 ' Required if we do an action that changes the properties of the object, such as add/remove from a group
343 Friend Sub ResetInherited()
344 _htblInheritedProperties = Nothing
345 _htblProperties = Nothing
346 End Sub
347
348 Friend Function GetProperty(ByVal sPropertyKey As String) As clsProperty
349 Return htblProperties(sPropertyKey)
350 End Function
351
352
353 Friend Function GetPropertyValue(ByVal sPropertyKey As String) As String
354 If HasProperty(sPropertyKey) Then
355 Return htblProperties(sPropertyKey).Value
356 Else
357 Return Nothing
358 End If
359 End Function
360
361
362 Friend Sub SetPropertyValue(ByVal sPropertyKey As String, ByVal StringData As Description)
363 AddProperty(sPropertyKey)
364 htblProperties(sPropertyKey).StringData = StringData
365 End Sub
366
367
368 Friend Sub SetPropertyValue(ByVal sPropertyKey As String, ByVal sValue As String)
369 AddProperty(sPropertyKey)
370 htblProperties(sPropertyKey).Value = sValue
371 End Sub
372
373
374 Friend Sub SetPropertyValue(ByVal sPropertyKey As String, ByVal bValue As Boolean)
375 ' Add/Remove the property from our collection
376 If bValue Then
377 AddProperty(sPropertyKey)
378 htblProperties(sPropertyKey).Selected = True
379 Else
380 RemoveProperty(sPropertyKey)
381 End If
382
383 End Sub
384
385
386 Friend Function HasProperty(ByVal sPropertyKey As String) As Boolean
387 Return htblProperties.ContainsKey(sPropertyKey)
388 End Function
389
390 Friend Sub RemoveProperty(ByVal sPropertyKey As String)
391 If _htblActualProperties.ContainsKey(sPropertyKey) Then
392 _htblActualProperties.Remove(sPropertyKey)
393 _htblProperties = Nothing
394 End If
395 End Sub
396
397 Friend Sub AddProperty(ByVal prop As clsProperty, Optional ByVal bActualProperties As Boolean = True)
398 If HasProperty(prop.Key) Then RemoveProperty(prop.Key)
399 If bActualProperties Then
400 _htblActualProperties.Add(prop)
401 Else
402 _htblInheritedProperties.Add(prop)
403 End If
404 _htblProperties = Nothing
405 End Sub
406
407
408 Friend Sub AddProperty(ByVal sPropertyKey As String, Optional ByVal bActualProperties As Boolean = True)
409 If Not HasProperty(sPropertyKey) Then
410 If Adventure.htblAllProperties.ContainsKey(sPropertyKey) Then
411 AddProperty(Adventure.htblAllProperties(sPropertyKey).Copy, bActualProperties)
412 Else
413 Throw New Exception("Property " & sPropertyKey & " does not exist!")
414 End If
415 End If
416 End Sub
417
418 Friend Overrides ReadOnly Property AllDescriptions As System.Collections.Generic.List(Of SharedModule.Description)
419 Get
420
421 End Get
422 End Property
423
424 Public Overrides ReadOnly Property Clone As clsItem
425 Get
426
427 End Get
428 End Property
429
430 Public Overrides ReadOnly Property CommonName As String
431 Get
432
433 End Get
434 End Property
435
436 Public Overrides Function DeleteKey(sKey As String) As Boolean
437
438 End Function
439
440 Friend Overrides Function FindStringLocal(sSearchString As String, Optional sReplace As String = Nothing, Optional bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object
441
442 End Function
443
444 Public Overrides Function ReferencesKey(sKey As String) As Integer
445
446 End Function
447 End Class