4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
6
using System . Text . RegularExpressions ;
7
+ using System . Threading ;
7
8
8
9
namespace APITracker
9
10
{
@@ -23,37 +24,32 @@ class SpecificRateTracker
23
24
[ JsonProperty ( PropertyName = "max" ) ]
24
25
uint maxTriggers = uint . MinValue ;
25
26
[ JsonIgnore ]
26
- bool updated = false ;
27
+ bool changed = false ;
27
28
28
29
public SpecificRateTracker ( int mins , int secs )
29
30
{
30
31
FrameStart = DateTime . Now ;
31
32
FrameSize = new TimeSpan ( 0 , mins , secs ) ;
32
33
}
33
34
34
- public void Trigger ( DateTime now )
35
- {
36
- if ( now > FrameStart + FrameSize )
37
- {
38
- if ( Triggers < minTriggers ) { minTriggers = Triggers ; updated = true ; }
39
- if ( Triggers > maxTriggers ) { maxTriggers = Triggers ; updated = true ; }
40
-
41
- FrameStart = now ;
42
- Triggers = 0 ;
43
- }
44
- else
45
- {
46
- Triggers ++ ;
47
- }
48
- }
49
-
50
- public string GetStatus ( )
51
- {
52
- if ( ! updated ) return null ;
53
- updated = false ;
54
-
55
- return $ "Time({ FrameSize } ) Min({ minTriggers } ) Max({ maxTriggers } )";
56
- }
35
+ public bool Update ( DateTime now )
36
+ {
37
+ if ( now > FrameStart + FrameSize )
38
+ {
39
+ if ( Triggers < minTriggers ) { minTriggers = Triggers ; changed = true ; }
40
+ if ( Triggers > maxTriggers ) { maxTriggers = Triggers ; changed = true ; }
41
+
42
+ FrameStart = now ;
43
+ Triggers = 0 ;
44
+ }
45
+ return changed ;
46
+ }
47
+
48
+ public void Trigger ( DateTime now )
49
+ {
50
+ Update ( now ) ;
51
+ Triggers ++ ;
52
+ }
57
53
}
58
54
class EndPointInfo
59
55
{
@@ -83,37 +79,26 @@ public EndPointInfo(string url)
83
79
} ;
84
80
}
85
81
86
-
87
82
public void Trigger ( DateTime now )
88
83
{
89
84
foreach ( SpecificRateTracker tracker in Trackers ) {
90
85
tracker . Trigger ( now ) ;
91
86
}
92
87
}
93
88
94
- public bool Log ( MelonLogger . Instance loggerInstance )
89
+ public bool Update ( DateTime now )
95
90
{
96
- uint updates = 0 ;
97
- string msg = $ "Endpoint { Url } \n ";
91
+ bool updated = false ;
98
92
foreach ( SpecificRateTracker tracker in Trackers )
99
93
{
100
- string status = tracker . GetStatus ( ) ;
101
- if ( status != null )
102
- {
103
- updates ++ ;
104
- msg += $ "\t \t { status } \n ";
105
- }
106
- }
107
- if ( updates != 0 )
108
- {
109
- loggerInstance . Msg ( msg ) ;
110
- return true ;
94
+ updated |= tracker . Update ( now ) ;
111
95
}
112
- return false ;
96
+ return updated ;
113
97
}
114
98
}
115
99
static Dictionary < string , EndPointInfo > endpointTimings = new Dictionary < string , EndPointInfo > ( ) ;
116
-
100
+ static bool run ;
101
+ static Thread updateThread ;
117
102
public static void Init ( )
118
103
{
119
104
try
@@ -124,16 +109,36 @@ public static void Init()
124
109
{
125
110
endpointTimings = new Dictionary < string , EndPointInfo > ( ) ;
126
111
}
112
+ run = true ;
113
+ updateThread = new Thread ( Update ) ;
114
+ updateThread . Start ( ) ;
127
115
}
128
116
129
117
public static void RequestTriggered ( String url )
130
118
{
131
119
var requestTime = DateTime . Now ;
132
120
133
- url = Regex . Replace ( url . Split ( '?' ) [ 0 ] , @"(\w*_[0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})" , "[VRCID]" ) ;
134
- url = Regex . Replace ( url , @"(\[VRCID\]:[0-9]*)" , "[VRCID]:[INSTANCEID]" ) ;
135
- url = Regex . Replace ( url , @"(~region\([a-z]*\))" , "~region([REGION])" ) ;
136
- url = Regex . Replace ( url , @"([A-Z0-9]{48})" , "[NONCE]" ) ;
121
+ url = Regex . Replace (
122
+ Regex . Replace (
123
+ Regex . Replace (
124
+ Regex . Replace (
125
+ Regex . Replace (
126
+ url . Split ( '?' ) [ 0 ] ,
127
+
128
+ @"([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})" ,
129
+ "[GUID]" ) ,
130
+
131
+ @"(\w+_\[GUID\])" ,
132
+ "[VRCID]" ) ,
133
+
134
+ @"(\[VRCID\]:[^~/]+)" ,
135
+ "[VRCID]:[INSTANCEID]" ) ,
136
+
137
+ @"(~region\([^\)]+\))" ,
138
+ "~region([REGION])" ) ,
139
+
140
+ @"(~nonce\([^\)]+\))" ,
141
+ "~nonce([NONCE])" ) ;
137
142
138
143
lock ( endpointTimings )
139
144
{
@@ -147,20 +152,32 @@ public static void RequestTriggered(String url)
147
152
}
148
153
}
149
154
150
- public static void Log ( MelonLogger . Instance loggerInstance )
151
- {
152
- bool updated = false ;
153
- lock ( endpointTimings )
154
- {
155
- foreach ( var endpoint in endpointTimings )
156
- {
157
- updated |= endpoint . Value . Log ( loggerInstance ) ;
158
- }
159
- if ( updated )
160
- {
161
- File . WriteAllText ( "endpointlimits.json" , JsonConvert . SerializeObject ( endpointTimings ) ) ;
162
- }
163
- }
155
+ public static void Stop ( )
156
+ {
157
+ run = false ;
158
+ updateThread . Join ( ) ;
159
+ }
160
+
161
+ private static void Update ( )
162
+ {
163
+ while ( run )
164
+ {
165
+ var updateTime = DateTime . Now ;
166
+
167
+ bool updated = false ;
168
+ lock ( endpointTimings )
169
+ {
170
+ foreach ( var endpoint in endpointTimings )
171
+ {
172
+ updated |= endpoint . Value . Update ( updateTime ) ;
173
+ }
174
+ if ( updated )
175
+ {
176
+ File . WriteAllText ( "endpointlimits.json" , JsonConvert . SerializeObject ( endpointTimings , Formatting . Indented ) ) ;
177
+ }
178
+ }
179
+ Thread . Sleep ( 10 ) ;
180
+ }
164
181
}
165
182
}
166
183
}
0 commit comments