Debian Codegraph @

terps/frankendrift/FrankenDrift.Adrift/StronglyTypedCollections.vb

raw
1 Imports System.Collections.Generic
2
3 Public Module StronglyTypedCollections
4 ' TODO - phase out the individual hashtables in favour of a single dictionary
5
6 Public Class ItemDictionary
7 Private AllItems As Generic.Dictionary(Of String, clsItem)
8
9 Public Sub New()
10 AllItems = New Generic.Dictionary(Of String, clsItem)
11 End Sub
12
13 Public ReadOnly Property Values() As Generic.Dictionary(Of String, clsItem).ValueCollection
14 Get
15 Return AllItems.Values
16 End Get
17 End Property
18
19 Public ReadOnly Property Keys() As Generic.Dictionary(Of String, clsItem).KeyCollection
20 Get
21 Return AllItems.Keys
22 End Get
23 End Property
24
25 Public Function ContainsKey(ByVal key As String) As Boolean
26 Return AllItems.ContainsKey(key)
27 End Function
28
29 Public Function TryGetValue(ByVal key As String, ByRef item As clsItem) As Boolean
30 Return AllItems.TryGetValue(key, item)
31 End Function
32
33 Default Public Property Item(ByVal key As String) As clsItem
34 Get
35 Dim itm As clsItem = Nothing
36 If TryGetValue(key, itm) Then Return itm
37 Return Nothing
38 End Get
39 Set(ByVal value As clsItem)
40 AllItems(key) = value
41 End Set
42 End Property
43
44 Public Sub AddBase(ByVal item As clsItem)
45 AllItems.Add(item.Key, item)
46 End Sub
47
48 Public Sub RemoveBase(ByVal key As String)
49 AllItems.Remove(key)
50 End Sub
51
52 ' This is currently just a view of the existing hashtables
53
54 Shadows Sub Add(ByVal item As clsItem)
55 Select Case True
56 Case TypeOf item Is clsLocation
57 Adventure.htblLocations.Add(CType(item, clsLocation), item.Key)
58 Case TypeOf item Is clsObject
59 Adventure.htblObjects.Add(CType(item, clsObject), item.Key)
60 Case TypeOf item Is clsTask
61 Adventure.htblTasks.Add(CType(item, clsTask), item.Key)
62 Case TypeOf item Is clsEvent
63 Adventure.htblEvents.Add(CType(item, clsEvent), item.Key)
64 Case TypeOf item Is clsCharacter
65 Adventure.htblCharacters.Add(CType(item, clsCharacter), item.Key)
66 Case TypeOf item Is clsVariable
67 Adventure.htblVariables.Add(CType(item, clsVariable), item.Key)
68 Case TypeOf item Is clsALR
69 Adventure.htblALRs.Add(CType(item, clsALR), item.Key)
70 Case TypeOf item Is clsGroup
71 Adventure.htblGroups.Add(CType(item, clsGroup), item.Key)
72 Case TypeOf item Is clsHint
73 Adventure.htblHints.Add(CType(item, clsHint), item.Key)
74 Case TypeOf item Is clsProperty
75 Adventure.htblAllProperties.Add(CType(item, clsProperty))
76 Case TypeOf item Is clsUserFunction
77 Adventure.htblUDFs.Add(CType(item, clsUserFunction), item.Key)
78 Case Else
79 TODO("Add item of type " & item.ToString)
80 End Select
81 End Sub
82
83
84 Shadows Sub Remove(ByVal key As String)
85 If Adventure.htblLocations.ContainsKey(key) Then
86 Adventure.htblLocations.Remove(key)
87 ElseIf Adventure.htblObjects.ContainsKey(key) Then
88 Adventure.htblObjects.Remove(key)
89 ElseIf Adventure.htblTasks.ContainsKey(key) Then
90 Adventure.htblTasks.Remove(key)
91 ElseIf Adventure.htblEvents.ContainsKey(key) Then
92 Adventure.htblEvents.Remove(key)
93 ElseIf Adventure.htblCharacters.ContainsKey(key) Then
94 Adventure.htblCharacters.Remove(key)
95 ElseIf Adventure.htblVariables.ContainsKey(key) Then
96 Adventure.htblVariables.Remove(key)
97 ElseIf Adventure.htblALRs.ContainsKey(key) Then
98 Adventure.htblALRs.Remove(key)
99 ElseIf Adventure.htblAllProperties.ContainsKey(key) Then
100 Adventure.htblAllProperties.Remove(key)
101 ElseIf Adventure.htblHints.ContainsKey(key) Then
102 Adventure.htblHints.Remove(key)
103 ElseIf Adventure.htblGroups.ContainsKey(key) Then
104 Adventure.htblGroups.Remove(key)
105 ElseIf Adventure.htblUDFs.ContainsKey(key) Then
106 Adventure.htblUDFs.Remove(key)
107 Else
108 TODO("Remove item of type " & key)
109 End If
110 End Sub
111
112 End Class
113
114 Public Class LocationHashTable
115 Inherits Dictionary(Of String, clsLocation)
116
117 Shadows Sub Add(ByVal loc As clsLocation, ByVal key As String)
118 MyBase.Add(key, loc)
119 If Me Is Adventure.htblLocations Then Adventure.dictAllItems.AddBase(loc)
120 End Sub
121
122 Shadows Sub Remove(ByVal key As String)
123 MyBase.Remove(key)
124 If Me Is Adventure.htblLocations Then Adventure.dictAllItems.RemoveBase(key)
125 End Sub
126
127 Default Shadows Property Item(ByVal key As String) As clsLocation
128 Get
129 'Return CType(MyBase.Item(key), clsLocation)
130 Try
131 Return MyBase.Item(key)
132 Catch ex As Exception
133 Return Nothing
134 End Try
135 End Get
136 Set(ByVal Value As clsLocation)
137 MyBase.Item(key) = Value
138 End Set
139 End Property
140
141 Public Function List(Optional ByVal sSeparator As String = "and") As String
142 Dim iCount As Integer = MyBase.Count
143
144 List = Nothing
145
146 For Each loc As clsLocation In MyBase.Values
147 List &= loc.ShortDescription.ToString(True)
148 iCount -= 1
149 If iCount > 1 Then List &= ", "
150 If iCount = 1 Then List &= " " & sSeparator & " "
151 Next
152 If List = "" Then List = "nowhere"
153
154 End Function
155 End Class
156
157 Public Class ObjectHashTable
158 Inherits Dictionary(Of String, clsObject)
159
160 Shadows Sub Add(ByVal ob As clsObject, ByVal key As String)
161 MyBase.Add(key, ob)
162 If Me Is Adventure.htblObjects Then Adventure.dictAllItems.AddBase(ob)
163 End Sub
164
165 Shadows Sub Remove(ByVal key As String)
166 MyBase.Remove(key)
167 If Me Is Adventure.htblObjects Then Adventure.dictAllItems.RemoveBase(key)
168 End Sub
169
170 Default Shadows Property Item(ByVal key As String) As clsObject
171 Get
172 Try
173 Return MyBase.Item(key)
174 Catch ex As Exception
175 Return Nothing
176 End Try
177 End Get
178 Set(ByVal Value As clsObject)
179 MyBase.Item(key) = Value
180 End Set
181 End Property
182
183 Public Function List(Optional ByVal sSeparator As String = "and", Optional ByVal bIncludeSubObjects As Boolean = False, Optional ByVal Article As ArticleTypeEnum = ArticleTypeEnum.Definite) As String
184 Dim iCount As Integer = MyBase.Count
185
186 List = Nothing
187
188 For Each ob As clsObject In MyBase.Values
189 List &= ob.FullName(Article)
190 iCount -= 1
191 If iCount > 1 Then List &= ", "
192 If iCount = 1 Then List &= " " & sSeparator & " "
193 Next
194 If List = "" Then List = "nothing"
195
196 If bIncludeSubObjects Then
197 For Each ob As clsObject In MyBase.Values
198
199 If ob.Children(clsObject.WhereChildrenEnum.OnObject).Count > 0 Then
200 If List <> "" Then List &= ". "
201 List &= ToProper(ob.Children(clsObject.WhereChildrenEnum.OnObject).List("and", False, Article))
202 If ob.Children(clsObject.WhereChildrenEnum.OnObject).Count = 1 Then
203 List &= " is on "
204 Else
205 List &= " are on "
206 End If
207 List &= ob.FullName(ArticleTypeEnum.Definite)
208 End If
209 If (ob.Openable AndAlso ob.IsOpen) OrElse Not ob.Openable Then
210 If ob.Children(clsObject.WhereChildrenEnum.InsideObject).Count > 0 Then
211 If ob.Children(clsObject.WhereChildrenEnum.OnObject).Count > 0 Then
212 List &= ", and inside"
213 Else
214 If List <> "" Then List &= ". "
215 List &= "Inside " & ob.FullName(ArticleTypeEnum.Definite)
216 End If
217 If ob.Children(clsObject.WhereChildrenEnum.InsideObject).Count = 1 Then
218 List &= " is "
219 Else
220 List &= " are "
221 End If
222 List &= ob.Children(clsObject.WhereChildrenEnum.InsideObject).List("and", False, Article)
223 End If
224
225 End If
226 Next
227 End If
228 End Function
229
230 Public Function SeenBy(Optional ByVal sCharKey As String = "") As ObjectHashTable
231
232 If sCharKey = "" OrElse sCharKey = "%Player%" Then sCharKey = Adventure.Player.Key
233
234 Dim htbl As New ObjectHashTable
235
236 For Each ob As clsObject In Me.Values
237 If ob.SeenBy(sCharKey) Then htbl.Add(ob, ob.Key)
238 Next
239
240 Return htbl
241
242 End Function
243
244 Public Function VisibleTo(Optional ByVal sCharKey As String = "") As ObjectHashTable
245
246 If sCharKey = "" OrElse sCharKey = "%Player%" Then sCharKey = Adventure.Player.Key
247
248 Dim htbl As New ObjectHashTable
249
250 For Each ob As clsObject In Me.Values
251 If ob.IsVisibleTo(sCharKey) Then htbl.Add(ob, ob.Key)
252 Next
253
254 Return htbl
255
256 End Function
257
258 End Class
259
260
261 Public Class TaskHashTable
262 Inherits Dictionary(Of String, clsTask)
263
264 Shadows Sub Add(ByVal task As clsTask, ByVal key As String)
265 If MyBase.ContainsKey(key) Then
266 key = key
267 End If
268 MyBase.Add(key, task)
269 If Me Is Adventure.htblTasks Then Adventure.dictAllItems.AddBase(task)
270 End Sub
271
272 Shadows Sub Remove(ByVal key As String)
273 MyBase.Remove(key)
274 If Me Is Adventure.htblTasks Then Adventure.dictAllItems.RemoveBase(key)
275 End Sub
276
277 Default Shadows Property Item(ByVal key As String) As clsTask
278 Get
279 Try
280 Return MyBase.Item(key)
281 Catch ex As Exception
282 Return Nothing
283 End Try
284 End Get
285 Set(ByVal Value As clsTask)
286 MyBase.Item(key) = Value
287 End Set
288 End Property
289 End Class
290
291 ' Exposes an array of keys that we can iterate that are ordered in our original way
292 Public Class OrderedHashTable
293 Inherits Hashtable
294
295 Public OrderedKeys As New StringArrayList
296
297
298 Shadows Sub Add(ByVal key As String, ByVal value As Object)
299 MyBase.Add(key, value)
300 OrderedKeys.Add(key)
301 End Sub
302
303 Shadows Sub Remove(ByVal key As String)
304 MyBase.Remove(key)
305 OrderedKeys.Remove(key)
306 End Sub
307
308 Shadows Sub Clear()
309 MyBase.Clear()
310 OrderedKeys.Clear()
311 End Sub
312
313 Public Sub Insert(ByVal key As String, ByVal value As Object, ByVal iPosition As Integer)
314 MyBase.Add(key, value)
315 OrderedKeys.Insert(iPosition, key)
316 End Sub
317
318 Public Shadows Function Clone() As OrderedHashTable
319 Dim htbl As New OrderedHashTable
320
321 For Each sKey As String In OrderedKeys
322 htbl.Add(sKey, Item(sKey))
323 Next
324
325 Return htbl
326 End Function
327
328 End Class
329
330 Public Class StringHashTable
331 Inherits Dictionary(Of String, String)
332
333 Default Shadows Property Item(ByVal key As String) As String
334 Get
335 Try
336 Return MyBase.Item(key)
337 Catch ex As Exception
338 Return Nothing
339 End Try
340 End Get
341 Set(ByVal Value As String)
342 MyBase.Item(key) = Value
343 End Set
344 End Property
345
346 Shadows Function Clone() As StringHashTable
347 Dim htblReturn As New StringHashTable
348
349 For Each entry As KeyValuePair(Of String, String) In Me
350 htblReturn.Add(entry.Key, entry.Value)
351 Next
352
353 Return htblReturn
354 End Function
355
356 End Class
357
358
359 Public Class EventHashTable
360 Inherits Dictionary(Of String, clsEvent)
361
362 Shadows Sub Add(ByVal evnt As clsEvent, ByVal key As String)
363 MyBase.Add(key, evnt)
364 Adventure.dictAllItems.AddBase(evnt)
365 End Sub
366
367 Shadows Sub Remove(ByVal key As String)
368 MyBase.Remove(key)
369 Adventure.dictAllItems.RemoveBase(key)
370 End Sub
371
372 Default Shadows Property Item(ByVal key As String) As clsEvent
373 Get
374 Try
375 Return MyBase.Item(key)
376 Catch ex As Exception
377 Return Nothing
378 End Try
379 End Get
380 Set(ByVal Value As clsEvent)
381 MyBase.Item(key) = Value
382 End Set
383 End Property
384
385 End Class
386
387
388 Public Class CharacterHashTable
389 Inherits Dictionary(Of String, clsCharacter)
390
391 Shadows Sub Add(ByVal chrr As clsCharacter, ByVal key As String)
392 MyBase.Add(key, chrr)
393 If Me Is Adventure.htblCharacters Then Adventure.dictAllItems.AddBase(chrr)
394 End Sub
395
396 Shadows Sub Remove(ByVal key As String)
397 MyBase.Remove(key)
398 If Me Is Adventure.htblCharacters Then Adventure.dictAllItems.RemoveBase(key)
399 End Sub
400
401 Default Shadows Property Item(ByVal key As String) As clsCharacter
402 Get
403 If key = "%Player%" Then
404 If Adventure.Player Is Nothing Then Return Nothing
405 key = Adventure.Player.Key
406 End If
407 Return MyBase.Item(key)
408 End Get
409 Set(ByVal Value As clsCharacter)
410 MyBase.Item(key) = Value
411 End Set
412 End Property
413
414 Public Function List(Optional ByVal sSeparator As String = "and") As String
415 Dim iCount As Integer = MyBase.Count
416
417 List = Nothing
418
419 For Each ch As clsCharacter In MyBase.Values
420 List &= "%CharacterName[" & ch.Key & "]%"
421 iCount -= 1
422 If iCount > 1 Then List &= ", "
423 If iCount = 1 Then List &= " " & sSeparator & " "
424 Next
425 If List = "" Then List = "noone"
426 End Function
427
428 Public Function SeenBy(Optional ByVal sCharKey As String = "") As CharacterHashTable
429 If sCharKey = "" OrElse sCharKey = "%Player%" Then sCharKey = Adventure.Player.Key
430
431 Dim htbl As New CharacterHashTable
432
433 For Each ch As clsCharacter In Me.Values
434 If ch.SeenBy(sCharKey) Then htbl.Add(ch, ch.Key)
435 Next
436 Return htbl
437 End Function
438
439 Public Function VisibleTo(Optional ByVal sCharKey As String = "") As CharacterHashTable
440 If sCharKey = "" OrElse sCharKey = "%Player%" Then sCharKey = Adventure.Player.Key
441
442 Dim htbl As New CharacterHashTable
443
444 For Each ch As clsCharacter In Me.Values
445 If ch.CanSeeCharacter(sCharKey) Then htbl.Add(ch, ch.Key)
446 Next
447
448 Return htbl
449 End Function
450
451 End Class
452
453
454 Public Class GroupHashTable
455 Inherits Dictionary(Of String, clsGroup)
456
457 Shadows Sub Add(ByVal grp As clsGroup, ByVal key As String)
458 MyBase.Add(key, grp)
459 Adventure.dictAllItems.AddBase(grp)
460 End Sub
461
462 Shadows Sub Remove(ByVal key As String)
463 MyBase.Remove(key)
464 Adventure.dictAllItems.RemoveBase(key)
465 End Sub
466
467 Default Shadows Property Item(ByVal key As String) As clsGroup
468 Get
469 Try
470 Return MyBase.Item(key)
471 Catch ex As Exception
472 Return Nothing
473 End Try
474 End Get
475 Set(ByVal Value As clsGroup)
476 MyBase.Item(key) = Value
477 End Set
478 End Property
479 End Class
480
481
482 Public Class ALRHashTable
483 Inherits Dictionary(Of String, clsALR)
484
485 Shadows Sub Add(ByVal alr As clsALR, ByVal key As String)
486 MyBase.Add(key, alr)
487 Adventure.dictAllItems.AddBase(alr)
488 End Sub
489
490 Shadows Sub Remove(ByVal key As String)
491 MyBase.Remove(key)
492 Adventure.dictAllItems.RemoveBase(key)
493 End Sub
494
495 Default Shadows Property Item(ByVal key As String) As clsALR
496 Get
497 Try
498 Return MyBase.Item(key)
499 Catch ex As Exception
500 Return Nothing
501 End Try
502 End Get
503 Set(ByVal Value As clsALR)
504 MyBase.Item(key) = Value
505 End Set
506 End Property
507 End Class
508
509
510 Public Class UDFHashTable
511 Inherits Dictionary(Of String, clsUserFunction)
512
513 Shadows Sub Add(ByVal UDF As clsUserFunction, ByVal key As String)
514 MyBase.Add(key, UDF)
515 Adventure.dictAllItems.AddBase(UDF)
516 End Sub
517
518 Shadows Sub Remove(ByVal key As String)
519 MyBase.Remove(key)
520 Adventure.dictAllItems.RemoveBase(key)
521 End Sub
522
523 Default Shadows Property Item(ByVal key As String) As clsUserFunction
524 Get
525 Try
526 Return MyBase.Item(key)
527 Catch ex As Exception
528 Return Nothing
529 End Try
530 End Get
531 Set(ByVal Value As clsUserFunction)
532 MyBase.Item(key) = Value
533 End Set
534 End Property
535 End Class
536
537
538 Public Class VariableHashTable
539 Inherits Dictionary(Of String, clsVariable)
540
541 Shadows Sub Add(ByVal var As clsVariable, ByVal key As String)
542 MyBase.Add(key, var)
543 Adventure.dictAllItems.AddBase(var)
544 End Sub
545
546 Shadows Sub Remove(ByVal key As String)
547 MyBase.Remove(key)
548 Adventure.dictAllItems.RemoveBase(key)
549 End Sub
550
551 Default Shadows Property Item(ByVal key As String) As clsVariable
552 Get
553 Try
554 Return MyBase.Item(key)
555 Catch ex As Exception
556 Return Nothing
557 End Try
558 End Get
559 Set(ByVal Value As clsVariable)
560 MyBase.Item(key) = Value
561 End Set
562 End Property
563 End Class
564
565
566 Public Class SynonymHashTable
567 Inherits Dictionary(Of String, clsSynonym)
568
569 Shadows Sub Add(ByVal syn As clsSynonym)
570 MyBase.Add(syn.Key, syn)
571 Adventure.dictAllItems.AddBase(syn)
572 End Sub
573
574 Shadows Sub Remove(ByVal key As String)
575 MyBase.Remove(key)
576 Adventure.dictAllItems.RemoveBase(key)
577 End Sub
578
579 Default Shadows Property Item(ByVal key As String) As clsSynonym
580 Get
581 Try
582 Return MyBase.Item(key)
583 Catch ex As Exception
584 Return Nothing
585 End Try
586 End Get
587 Set(ByVal Value As clsSynonym)
588 MyBase.Item(key) = Value
589 End Set
590 End Property
591 End Class
592
593
594
595 Public Class RestrictionArrayList
596 Inherits List(Of clsRestriction)
597 Implements ICloneable
598
599 Private sBracketSequence As String
600
601 Public Property BracketSequence() As String
602 Get
603 Return sBracketSequence
604 End Get
605 Set(ByVal Value As String)
606 If sBracketSequence <> Value Then
607 sBracketSequence = Value
608 End If
609 End Set
610 End Property
611
612
613 Public Function ReferencesKey(ByVal sKey As String) As Integer
614 Dim iCount As Integer = 0
615 For Each r As clsRestriction In Me
616 If r.ReferencesKey(sKey) Then iCount += 1
617 Next
618 Return iCount
619 End Function
620
621
622 Public Function DeleteKey(ByVal sKey As String) As Boolean
623 For i As Integer = MyBase.Count - 1 To 0 Step -1
624 If CType(MyBase.Item(i), clsRestriction).ReferencesKey(sKey) Then
625 RemoveAt(i)
626 If MyBase.Count = 0 Then
627 BracketSequence = ""
628 ElseIf MyBase.Count = 1 Then
629 BracketSequence = "#"
630 Else
631 StripRestriction(i)
632 End If
633 End If
634 Next
635
636 Return True
637 End Function
638
639
640 Private Function StripRestriction(ByVal iRest As Integer) As Boolean
641 Dim iFound As Integer = 0
642
643 For i As Integer = 0 To BracketSequence.Length - 1
644 If sMid(BracketSequence, i, 1) = "#" Then iFound += 1
645 If iFound = iRest + 1 Then
646 ' Delete the marker
647 BracketSequence = sLeft(BracketSequence, i) & sRight(BracketSequence, BracketSequence.Length - i - 1)
648 ' Remove any trailing And/Or markers
649 If BracketSequence.Length >= i AndAlso (sMid(BracketSequence, i, 1) = "A" OrElse sMid(BracketSequence, i, 1) = "O") Then
650 BracketSequence = sLeft(BracketSequence, i) & sRight(BracketSequence, BracketSequence.Length - i - 1)
651 End If
652 ' Correct any duff brackets
653 Exit For
654 End If
655 Next
656 End Function
657
658
659 Public Function IsBracketsValid() As Boolean
660 If sBracketSequence Is Nothing Then Return True
661 Dim sBS As String = sBracketSequence.Replace("[", "((").Replace("]", "))")
662 Dim sTemp As String = Nothing
663
664 While sTemp <> sBS
665 sTemp = sBS
666 sBS = Replace(sBS, "exprAexpr", "expr")
667 sBS = Replace(sBS, "exprOexpr", "expr")
668 sBS = Replace(sBS, "#", "expr")
669 sBS = Replace(sBS, "(expr)", "expr")
670 sBS = Replace(sBS, "[expr]", "expr")
671 'Debug.Print(brackstring)
672 End While
673
674 If sBS = "expr" OrElse (Me.Count = 0 AndAlso sBS = "") Then
675 Return True
676 Else
677 Return False
678 End If
679 End Function
680
681 Default Shadows Property Item(ByVal idx As Integer) As clsRestriction
682 Get
683 Try
684 Return MyBase.Item(idx)
685 Catch ex As Exception
686 Return Nothing
687 End Try
688 'Return CType(MyBase.Item(idx), clsRestriction)
689 End Get
690 Set(ByVal Value As clsRestriction)
691 MyBase.Item(idx) = Value
692 End Set
693 End Property
694
695 Public Function Copy() As RestrictionArrayList
696 Dim ral As New RestrictionArrayList
697
698 ral.BracketSequence = sBracketSequence
699 For i As Integer = 0 To MyBase.Count - 1
700 Dim rest As clsRestriction = CType(MyBase.Item(i), clsRestriction).Copy
701 ral.Add(rest)
702 Next
703
704 Return ral
705 End Function
706
707 Private Function Clone() As Object Implements ICloneable.Clone
708 Return Me.MemberwiseClone
709 End Function
710 Public Function CloneMe() As RestrictionArrayList
711 Return CType(Clone(), RestrictionArrayList)
712 End Function
713 End Class
714
715
716
717 Public Class ActionArrayList
718 Inherits List(Of clsAction)
719
720 Public Function ReferencesKey(ByVal sKey As String) As Integer
721 Dim iCount As Integer = 0
722 For Each a As clsAction In Me
723 If a.ReferencesKey(sKey) Then iCount += 1
724 Next
725 Return iCount
726 End Function
727
728 Public Function DeleteKey(ByVal sKey As String) As Boolean
729 For i As Integer = MyBase.Count - 1 To 0 Step -1
730 If CType(MyBase.Item(i), clsAction).ReferencesKey(sKey) Then
731 RemoveAt(i)
732 End If
733 Next
734 Return True
735 End Function
736
737 Default Shadows Property Item(ByVal idx As Integer) As clsAction
738 Get
739 Try
740 Return MyBase.Item(idx)
741 Catch ex As Exception
742 Return Nothing
743 End Try
744 'Return CType(MyBase.Item(idx), clsAction)
745 End Get
746 Set(ByVal Value As clsAction)
747 MyBase.Item(idx) = Value
748 End Set
749 End Property
750
751 Public Function Copy() As ActionArrayList
752 Dim aal As New ActionArrayList
753
754 For i As Integer = 0 To MyBase.Count - 1
755 Dim act As clsAction = CType(MyBase.Item(i), clsAction).Copy
756 aal.Add(act)
757 Next
758
759 Return aal
760 End Function
761
762 End Class
763
764
765 Public Class EventDescriptionArrayList
766 Inherits List(Of clsEventDescription)
767
768 Default Shadows Property Item(ByVal idx As Integer) As clsEventDescription
769 Get
770 Try
771 Return MyBase.Item(idx)
772 Catch ex As Exception
773 Return Nothing
774 End Try
775 'Return CType(MyBase.Item(idx), clsEventDescription)
776 End Get
777 Set(ByVal Value As clsEventDescription)
778 MyBase.Item(idx) = Value
779 End Set
780 End Property
781 End Class
782
783
784 Public Class PropertyHashTable
785 Inherits Dictionary(Of String, clsProperty)
786
787 Shadows Sub Add(ByVal prop As clsProperty)
788
789 MyBase.Add(prop.Key, prop)
790 If Me Is Adventure.htblAllProperties Then
791 Select Case prop.PropertyOf
792 Case clsProperty.PropertyOfEnum.Objects
793 Adventure.htblObjectProperties.Add(prop)
794 Case clsProperty.PropertyOfEnum.Locations
795 Adventure.htblLocationProperties.Add(prop)
796 Case clsProperty.PropertyOfEnum.Characters
797 Adventure.htblCharacterProperties.Add(prop)
798 Case clsProperty.PropertyOfEnum.AnyItem
799 Adventure.htblObjectProperties.Add(prop)
800 Adventure.htblLocationProperties.Add(prop)
801 Adventure.htblCharacterProperties.Add(prop)
802 End Select
803 Adventure.dictAllItems.AddBase(prop)
804 End If
805 End Sub
806
807 Shadows Sub Remove(ByVal key As String)
808 If Me Is Adventure.htblAllProperties Then
809 Select Case Adventure.htblAllProperties(key).PropertyOf
810 Case clsProperty.PropertyOfEnum.Objects
811 Adventure.htblObjectProperties.Remove(key)
812 Case clsProperty.PropertyOfEnum.Characters
813 Adventure.htblCharacterProperties.Remove(key)
814 Case clsProperty.PropertyOfEnum.Locations
815 Adventure.htblLocationProperties.Remove(key)
816 Case clsProperty.PropertyOfEnum.AnyItem
817 Adventure.htblObjectProperties.Remove(key)
818 Adventure.htblCharacterProperties.Remove(key)
819 Adventure.htblLocationProperties.Remove(key)
820 End Select
821 Adventure.dictAllItems.RemoveBase(key)
822 End If
823 MyBase.Remove(key)
824 End Sub
825
826 Default Shadows Property Item(ByVal key As String) As clsProperty
827 Get
828 Try
829 Return MyBase.Item(key)
830 Catch ex As Exception
831 Return Nothing
832 End Try
833 End Get
834 Set(ByVal Value As clsProperty)
835 MyBase.Item(key) = Value
836 End Set
837 End Property
838
839 Public Function CopySelected(Optional ByVal bGroup As Boolean = False) As PropertyHashTable
840
841 Dim pht As New PropertyHashTable
842
843 For Each prop As clsProperty In MyBase.Values
844 If prop.Selected AndAlso (bGroup OrElse Not (prop.GroupOnly OrElse prop.FromGroup)) Then pht.Add(prop.Copy)
845 Next
846
847 Return pht
848
849 End Function
850
851 Shadows Function Clone() As PropertyHashTable
852 Dim htblReturn As New PropertyHashTable
853
854 For Each entry As KeyValuePair(Of String, clsProperty) In Me
855 htblReturn.Add(CType(entry.Value.Clone, clsProperty))
856 Next
857
858 Return htblReturn
859 End Function
860
861
862 Public Function ReferencesKey(ByVal sKey As String) As Integer
863 Dim iCount As Integer = 0
864 If MyBase.ContainsKey(sKey) Then iCount += 1
865 For Each p As clsProperty In MyBase.Values
866 If p.AppendToProperty = sKey Then iCount += 1
867 If p.DependentKey = sKey Then iCount += 1
868 If p.RestrictProperty = sKey Then iCount += 1
869 If p.Type = clsProperty.PropertyTypeEnum.CharacterKey OrElse p.Type = clsProperty.PropertyTypeEnum.LocationGroupKey OrElse p.Type = clsProperty.PropertyTypeEnum.LocationKey OrElse p.Type = clsProperty.PropertyTypeEnum.ObjectKey Then
870 If p.Value = sKey Then iCount += 1
871 End If
872 Next
873 Return iCount
874 End Function
875
876
877 ''' <summary>
878 '''
879 ''' </summary>
880 ''' <param name="p">Property to reset or remove</param>
881 ''' <returns>True, if we remove a property</returns>
882 ''' <remarks></remarks>
883 Private Function ResetOrRemoveProperty(ByVal p As clsProperty) As Boolean
884 ' If we're not mandatory we can just remove the property
885 If Not p.Mandatory Then
886 ' Ok, just remove it
887 Remove(p.Key)
888 Return True
889 Else
890 ' If we are mandatory, we may be able to reset the value, depending on the property type
891 Dim bDelete As Boolean = False
892 Select Case p.Type
893 Case clsProperty.PropertyTypeEnum.CharacterKey, clsProperty.PropertyTypeEnum.LocationGroupKey, clsProperty.PropertyTypeEnum.LocationKey, clsProperty.PropertyTypeEnum.ObjectKey
894 ' Key types can't be reset, must be deleted, as they can't be Null on a mandatory property
895 bDelete = True
896 Case clsProperty.PropertyTypeEnum.Integer
897 p = p
898 Case clsProperty.PropertyTypeEnum.Text
899 p = p
900 Case clsProperty.PropertyTypeEnum.StateList
901 ' Ok, we just need to make sure we change the value to something that doesn't produce child property
902 Dim bAssignedValue As Boolean = False
903 For Each sValue As String In p.arlStates
904 Dim bSafeValue As Boolean = True
905 ' If we're unable to do this, we need to remove, and reset/remove parent
906 For Each pOther As clsProperty In Me.Values
907 If pOther.DependentKey = p.Key AndAlso pOther.DependentValue = sValue Then
908 bSafeValue = False
909 Exit For
910 End If
911 Next
912 If bSafeValue Then
913 ' Ok to set the value to this
914 p.Value = sValue
915 bAssignedValue = True
916 Exit For
917 End If
918 Next
919 If Not bAssignedValue Then bDelete = True
920 End Select
921
922 ' If we can't reset the property, we need to reset or remove our parent
923 If bDelete Then
924 If ContainsKey(p.DependentKey) Then
925 Dim pParent As clsProperty = Me.Item(p.DependentKey)
926 ResetOrRemoveProperty(pParent)
927 End If
928 Remove(p.Key)
929 Return True
930 End If
931 End If
932
933 Return False
934 End Function
935
936
937 Public Function DeleteKey(ByVal sKey As String) As Boolean
938 restart:
939 For Each p As clsProperty In MyBase.Values
940 If p.AppendToProperty = sKey Then p.AppendToProperty = ""
941 If p.DependentKey = sKey Then p.DependentKey = ""
942 If p.RestrictProperty = sKey Then p.RestrictProperty = ""
943 If p.Type = clsProperty.PropertyTypeEnum.CharacterKey OrElse p.Type = clsProperty.PropertyTypeEnum.LocationGroupKey OrElse p.Type = clsProperty.PropertyTypeEnum.LocationKey OrElse p.Type = clsProperty.PropertyTypeEnum.ObjectKey Then
944 If p.Value = sKey Then
945 If ResetOrRemoveProperty(p) Then GoTo restart
946 End If
947 End If
948 Next
949 If ContainsKey(sKey) Then Remove(sKey)
950 Return True
951 End Function
952
953 ' Ensures any child properties have their Selected properties set
954 Public Sub SetSelected()
955 For Each prop As clsProperty In MyBase.Values
956 prop.Selected = IsPropertySelected(prop)
957 Next
958 End Sub
959
960 Private Function IsPropertySelected(ByVal prop As clsProperty) As Boolean
961 If SafeString(prop.DependentKey) <> "" Then
962 If MyBase.ContainsKey(prop.DependentKey) Then
963 If prop.DependentValue Is Nothing OrElse Item(prop.DependentKey).Value = prop.DependentValue Then
964 Return IsPropertySelected(Item(prop.DependentKey))
965 End If
966 Else
967 WarnOnce($"Property ""{prop.Description}"" is dependent on ""{prop.DependentKey}"" but this property doesn't exist")
968 End If
969 Else
970 Return prop.Selected
971 End If
972 End Function
973
974 End Class
975
976
977 Public Class BooleanHashTable
978 Inherits Dictionary(Of String, Boolean)
979
980 Shadows Sub Add(ByVal bool As Boolean, ByVal key As String)
981 MyBase.Add(key, bool)
982 End Sub
983
984 Default Shadows Property Item(ByVal key As String) As Boolean
985 Get
986 Try
987 Return MyBase.Item(key)
988 Catch ex As Exception
989 Return Nothing
990 End Try
991 End Get
992 Set(ByVal Value As Boolean)
993 MyBase.Item(key) = Value
994 End Set
995 End Property
996
997 End Class
998
999
1000 Public Class HintHashTable
1001 Inherits Dictionary(Of String, clsHint)
1002
1003 Shadows Sub Add(ByVal hint As clsHint, ByVal key As String)
1004 MyBase.Add(key, hint)
1005 Adventure.dictAllItems.AddBase(hint)
1006 End Sub
1007
1008 Shadows Sub Remove(ByVal key As String)
1009 MyBase.Remove(key)
1010 Adventure.dictAllItems.RemoveBase(key)
1011 End Sub
1012
1013 Default Shadows Property Item(ByVal key As String) As clsHint
1014 Get
1015 Try
1016 Return MyBase.Item(key)
1017 Catch ex As Exception
1018 Return Nothing
1019 End Try
1020 End Get
1021 Set(ByVal Value As clsHint)
1022 MyBase.Item(key) = Value
1023 End Set
1024 End Property
1025 End Class
1026
1027
1028 Public Class TopicHashTable
1029 Inherits Dictionary(Of String, clsTopic)
1030
1031 Shadows Sub Add(ByVal topic As clsTopic)
1032 MyBase.Add(topic.Key, topic)
1033 End Sub
1034
1035 Default Shadows Property Item(ByVal key As String) As clsTopic
1036 Get
1037 Try
1038 Return MyBase.Item(key)
1039 Catch ex As Exception
1040 Return Nothing
1041 End Try
1042 End Get
1043 Set(ByVal Value As clsTopic)
1044 MyBase.Item(key) = Value
1045 End Set
1046 End Property
1047
1048 Shadows Function Clone() As TopicHashTable
1049 Dim htblReturn As New TopicHashTable
1050
1051 For Each entry As KeyValuePair(Of String, clsTopic) In Me
1052 htblReturn.Add(entry.Value.Clone)
1053 Next
1054
1055 Return htblReturn
1056 End Function
1057
1058 Friend Function DoesTopicHaveChildren(ByVal key As String) As Boolean
1059 For Each sKey As String In Me.Keys
1060 If key <> sKey Then
1061 If Me.Item(sKey).ParentKey = key Then Return True
1062 End If
1063 Next
1064 Return False
1065 End Function
1066
1067 End Class
1068
1069
1070 Public Class StringArrayList
1071 Inherits List(Of String)
1072
1073 Default Shadows Property Item(ByVal idx As Integer) As String
1074 Get
1075 Try
1076 Return MyBase.Item(idx)
1077 Catch ex As Exception
1078 Return Nothing
1079 End Try
1080 End Get
1081 Set(ByVal Value As String)
1082 Try
1083 MyBase.Item(idx) = Value
1084 Catch ex As Exception
1085 ErrMsg("Error assigning value """ & Value & """ to index " & idx)
1086 End Try
1087 End Set
1088 End Property
1089
1090 Sub Merge(ByVal sal As StringArrayList)
1091 For Each s As String In sal
1092 MyBase.Add(s)
1093 Next
1094 End Sub
1095
1096 Shadows Function Clone() As StringArrayList
1097 Dim htblReturn As New StringArrayList
1098
1099 For Each entry As String In Me
1100 htblReturn.Add(entry)
1101 Next
1102
1103 Return htblReturn
1104 End Function
1105
1106 Shadows Function Contains(ByVal strg As String) As Boolean
1107 Return MyBase.Contains(strg)
1108 End Function
1109
1110 Function List() As String
1111 Dim iCount As Integer = MyBase.Count
1112
1113 List = Nothing
1114
1115 For Each s As String In Me
1116 List &= s
1117 iCount -= 1
1118 If iCount > 1 Then List &= ", "
1119 If iCount = 1 Then List &= " and "
1120 Next
1121 If List = "" Then List = "nothing"
1122 End Function
1123
1124 End Class
1125
1126
1127 Public Class DirectionArrayList
1128 Inherits List(Of clsDirection)
1129
1130 Default Shadows Property Item(ByVal idx As DirectionsEnum) As clsDirection
1131 Get
1132 Try
1133 Return MyBase.Item(idx)
1134 Catch ex As Exception
1135 Return Nothing
1136 End Try
1137 End Get
1138 Set(ByVal Value As clsDirection)
1139 MyBase.Item(idx) = Value
1140 End Set
1141 End Property
1142
1143 End Class
1144
1145
1146
1147 Public Class WalkArrayList
1148 Inherits List(Of clsWalk)
1149
1150 Default Shadows Property Item(ByVal idx As Integer) As clsWalk
1151 Get
1152 Try
1153 Return MyBase.Item(idx)
1154 Catch ex As Exception
1155 Return Nothing
1156 End Try
1157 End Get
1158 Set(ByVal Value As clsWalk)
1159 MyBase.Item(idx) = Value
1160 End Set
1161 End Property
1162
1163 End Class
1164
1165 End Module