Skip to content

Commit 6d2863a

Browse files
committed
perf: 优化代码
1 parent 555e5b2 commit 6d2863a

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

Plain Craft Launcher 2/Modules/Base/ModBase.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Public Module ModBase
1515
#Region "声明"
1616

1717
'下列版本信息由更新器自动修改
18-
Public Const VersionBaseName As String = "2.11.2-beta.2" '不含分支前缀的显示用版本名
18+
Public Const VersionBaseName As String = "2.11.2-beta.3" '不含分支前缀的显示用版本名
1919
Public Const VersionStandardCode As String = "2.11.1." & VersionBranchCode '标准格式的四段式版本号
2020
Public Const CommitHash As String = "native" 'Commit Hash,由 GitHub Workflow 自动替换
2121
Public CommitHashShort As String = If(CommitHash = "native", "native", CommitHash.Substring(0, 7)) 'Commit Hash,取前 7 位
2222
Public Const UpstreamVersion As String = "2.10.0" '上游版本
23-
Public Const VersionCode As Integer = 372 '内部版本号
23+
Public Const VersionCode As Integer = 373 '内部版本号
2424
'自动生成的版本信息
2525
#If RELEASE Then
2626
Public Const VersionBranchName As String = "Slow Ring"

Plain Craft Launcher 2/Modules/Minecraft/ModStyle.vb

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,86 @@
44
Public Class TimerRun
55
Inherits Run
66
Implements IDisposable
7-
7+
88
Private _timer As Threading.DispatcherTimer = Nothing
9-
9+
Private ReadOnly Property _isTimerRunning
10+
Get
11+
Return _timer IsNot Nothing AndAlso _timer.IsEnabled
12+
End Get
13+
End Property
14+
1015
'定义依赖属性
11-
Public Shared ReadOnly UpdateIntervalProperty As DependencyProperty = DependencyProperty.Register(NameOf(UpdateInterval), GetType(TimeSpan), GetType(TimerRun), New PropertyMetadata(TimeSpan.FromSeconds(1)))
12-
16+
Public Shared ReadOnly UpdateIntervalProperty As DependencyProperty =
17+
DependencyProperty.Register(
18+
NameOf(UpdateInterval),
19+
GetType(TimeSpan),
20+
GetType(TimerRun),
21+
New PropertyMetadata(TimeSpan.FromSeconds(1)))
22+
'属性变化处理
23+
Protected Overrides Sub OnPropertyChanged(e As DependencyPropertyChangedEventArgs)
24+
MyBase.OnPropertyChanged(e)
25+
If e.Property Is UpdateIntervalProperty AndAlso _timer IsNot Nothing Then
26+
_timer.Interval = UpdateInterval
27+
End If
28+
End Sub
29+
1330
'UpdateInterval 属性
1431
Public Property UpdateInterval As TimeSpan
1532
Get
1633
Return CType(GetValue(UpdateIntervalProperty), TimeSpan)
1734
End Get
1835
Set
19-
SetValue(UpdateIntervalProperty, value)
36+
If Value > TimeSpan.Zero Then
37+
SetValue(UpdateIntervalProperty, Value)
38+
End If
2039
End Set
2140
End Property
22-
41+
2342
Public Property AutoStart As Boolean
24-
25-
Public Sub New(Optional autoStart As Boolean = False)
43+
44+
Public Sub New(Optional interval As TimeSpan = Nothing, Optional autoStart As Boolean = False)
45+
_timer = New Threading.DispatcherTimer
46+
AddHandler _timer.Tick, AddressOf _timerTick
47+
Me.UpdateInterval = If(interval = Nothing, TimeSpan.FromSeconds(1), interval)
2648
Me.AutoStart = autoStart
2749
End Sub
28-
50+
2951
'定时器事件
30-
Public Event TimerTick As Action(Of TimerRun)
31-
32-
Private Sub _TimerTick(sender As Object, e As EventArgs)
52+
Public Delegate Sub TimerTickDelegate(sender As TimerRun)
53+
Public Event TimerTick As TimerTickDelegate
54+
55+
Private Sub _timerTick(sender As Object, e As EventArgs)
3356
RaiseEvent TimerTick(Me)
3457
End Sub
35-
58+
3659
Private Sub OnLoaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
37-
if AutoStart Then StartTimer()
60+
If AutoStart Then StartTimer()
3861
End Sub
39-
62+
4063
Private Sub OnUnloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
4164
StopTimer()
4265
End Sub
43-
66+
4467
Public Sub StartTimer()
45-
StopTimer() '确保没有重复的定时器
46-
_timer = New Threading.DispatcherTimer With { .Interval = UpdateInterval }
47-
AddHandler _timer.Tick, AddressOf _TimerTick
48-
_timer.Start()
49-
End Sub
50-
51-
Public Sub StopTimer()
52-
If _timer IsNot Nothing Then
53-
_timer.Stop()
54-
RemoveHandler _timer.Tick, AddressOf _TimerTick
55-
_timer = Nothing
68+
If Me.Dispatcher Is Nothing Then
69+
Log("[TimerRun] Dispatcher is null, unable to run", LogLevel.Critical)
70+
Return
5671
End If
72+
If Not _isTimerRunning Then _timer?.Start()
5773
End Sub
58-
59-
'属性变化处理
60-
Protected Overrides Sub OnPropertyChanged(e As DependencyPropertyChangedEventArgs)
61-
MyBase.OnPropertyChanged(e)
62-
If e.Property Is UpdateIntervalProperty AndAlso _timer IsNot Nothing Then
63-
_timer.Interval = UpdateInterval
64-
End If
74+
75+
Public Sub StopTimer()
76+
If _isTimerRunning Then _timer?.Stop()
6577
End Sub
66-
67-
'实现 IDisposable
68-
Private _disposed As Boolean = False
78+
79+
Private _isDisposed = False
6980
Public Sub Dispose() Implements IDisposable.Dispose
70-
If Not _disposed Then
71-
StopTimer()
72-
_disposed = True
73-
End If
74-
GC.SuppressFinalize(Me)
81+
If _isDisposed Then Return
82+
_isDisposed = True
83+
'资源释放
84+
RemoveHandler _timer.Tick, AddressOf _timerTick
85+
_timer?.Stop()
86+
_timer = Nothing
7587
End Sub
7688

7789
End Class
@@ -169,7 +181,7 @@
169181
lab.Inlines.Add(curRun)
170182

171183
' 用于存储需要随机化的文本段
172-
Dim randomTextRuns As New List(Of TimerRun)()
184+
Dim randomTextRuns As New List(Of TimerRun)
173185

174186
For Each c As Char In text
175187
If c = "§" Then '下一字符是格式化代码

0 commit comments

Comments
 (0)