1212
1313namespace Numantics {
1414 public class Numantics : ResoniteMod {
15- internal const string VERSION_CONSTANT = "1.0.2 " ;
15+ internal const string VERSION_CONSTANT = "1.0.3 " ;
1616 public override string Name => "Numantics" ;
1717 public override string Author => "NalaTheThird" ;
1818 public override string Version => VERSION_CONSTANT ;
@@ -34,10 +34,6 @@ public class Numantics : ResoniteMod {
3434 private static readonly ModConfigurationKey < bool > EnableEasterEggs =
3535 new ModConfigurationKey < bool > ( "enable_easter_eggs" , "Enable 'Easter eggs' - (Who knows what you might get with this on...)" , ( ) => false ) ;
3636
37- [ AutoRegisterConfigKey ]
38- private static readonly ModConfigurationKey < bool > VerboseLogging =
39- new ModConfigurationKey < bool > ( "verbose_logging" , "Enables Verbose Logging (Not Likely needed for regular folks)" , ( ) => false ) ;
40-
4137 private static ModConfiguration Config ;
4238
4339 public override void OnEngineInit ( ) {
@@ -53,11 +49,7 @@ public override void OnEngineInit() {
5349 class TextEditor_OnFinished_Patch {
5450 static void Prefix ( TextEditor __instance ) {
5551 try {
56- bool verbose = Config ? . GetValue ( VerboseLogging ) ?? false ;
57-
58-
5952 if ( __instance ? . Text ? . Target == null ) {
60- if ( verbose ) Msg ( "TextEditor or Text.Target is null, skipping" ) ;
6153 return ;
6254 }
6355
@@ -67,40 +59,30 @@ static void Prefix(TextEditor __instance) {
6759 }
6860
6961 if ( ! Config . GetValue ( EnableValueFieldMath ) ) {
70- if ( verbose ) Msg ( "Math evaluation is disabled in config" ) ;
7162 return ;
7263 }
7364
7465 string text = __instance . Text . Target . Text ;
75- if ( verbose ) Msg ( $ "Input text: '{ text } '") ;
7666
7767 if ( string . IsNullOrWhiteSpace ( text ) ) {
78- if ( verbose ) Msg ( "Text is null or whitespace, skipping" ) ;
7968 return ;
8069 }
8170
82- Type fieldType = GetFieldType ( __instance , verbose ) ;
71+ Type fieldType = GetFieldType ( __instance ) ;
8372 bool isStringField = fieldType == typeof ( string ) ;
8473
85- if ( verbose ) Msg ( $ "Detected field type: { fieldType ? . Name ?? "Unknown" } ") ;
86-
8774 if ( isStringField ) {
8875 if ( ! Config . GetValue ( IncludeStrings ) ) {
89- if ( verbose ) Msg ( "Editing string field but include_strings is disabled, skipping" ) ;
9076 return ;
9177 }
92- if ( verbose ) Msg ( "String field detected and include_strings is enabled" ) ;
9378 }
9479
9580 // Storage for EE.
9681 string originalText = text . Replace ( " " , "" ) ;
9782
9883 text = text . Replace ( "pi" , Math . PI . ToString ( CultureInfo . InvariantCulture ) ) ;
99- if ( verbose && text != originalText ) {
100- Msg ( "Replaced 'pi' constant" ) ;
101- }
10284
103- string exprText = ProcessMathFunctions ( text , verbose ) ;
85+ string exprText = ProcessMathFunctions ( text ) ;
10486
10587 // Shorthand ops - with regex to avoid breaking function names
10688 exprText = Regex . Replace ( exprText , @"(?<![a-zA-Z])x(?![a-zA-Z])" , "*" ) ;
@@ -109,11 +91,6 @@ static void Prefix(TextEditor __instance) {
10991 exprText = Regex . Replace ( exprText , @"(?<![a-zA-Z])s(?![a-zA-Z])" , "-" ) ;
11092 exprText = exprText . Replace ( "^" , "^" ) ;
11193
112- if ( verbose && exprText != text ) {
113- Msg ( $ "Replaced operators: '{ text } ' => '{ exprText } '") ;
114- }
115-
116- // EE Values and Checks
11794 bool easterEggs = Config . GetValue ( EnableEasterEggs ) ;
11895 if ( easterEggs ) {
11996
@@ -156,34 +133,27 @@ static void Prefix(TextEditor __instance) {
156133 }
157134
158135 // Expression Evaluator - If you cant express it, you cant impress it! | Main Logic for Math Evaluation
159- if ( TryEvaluateExpression ( exprText , out string result , verbose ) ) {
136+ if ( TryEvaluateExpression ( exprText , out string result ) ) {
160137 if ( Config . GetValue ( RoundResults ) ) {
161138 if ( double . TryParse ( result , NumberStyles . Float , CultureInfo . InvariantCulture , out double value ) ) {
162139 double rounded = Math . Round ( value , MidpointRounding . AwayFromZero ) ;
163- if ( verbose ) Msg ( $ "Rounded { value } to { rounded } ") ;
164140 result = rounded . ToString ( CultureInfo . InvariantCulture ) ;
165141 }
166142 }
167143
168- Msg ( $ "SUCCESS - Evaluated '{ originalText } ' => '{ result } '") ;
144+ Debug ( $ "SUCCESS - Evaluated '{ originalText } ' => '{ result } '") ;
169145 __instance . Text . Target . Text = result ;
170-
171- if ( verbose ) Msg ( $ "Updated Text.Target.Text to '{ result } '") ;
172146 } else {
173147 if ( double . TryParse ( exprText , NumberStyles . Float , CultureInfo . InvariantCulture , out double numValue ) ) {
174148 string numResult = numValue . ToString ( CultureInfo . InvariantCulture ) ;
175149
176150 if ( Config . GetValue ( RoundResults ) ) {
177151 double rounded = Math . Round ( numValue , MidpointRounding . AwayFromZero ) ;
178- if ( verbose ) Msg ( $ "Rounded { numValue } to { rounded } ") ;
179152 numResult = rounded . ToString ( CultureInfo . InvariantCulture ) ;
180153 }
181154
182155 Msg ( $ "SUCCESS - Evaluated '{ originalText } ' => '{ numResult } '") ;
183156 __instance . Text . Target . Text = numResult ;
184- if ( verbose ) Msg ( $ "Updated Text.Target.Text to '{ numResult } '") ;
185- } else {
186- if ( verbose ) Msg ( $ "Not a math expression or evaluation failed: '{ exprText } '") ;
187157 }
188158 }
189159 } catch ( Exception e ) {
@@ -206,7 +176,7 @@ private static bool IsIntegerType(Type type) {
206176 /// Reflection is a standard .NET feature that allows inspection of types at runtime.
207177 /// Here, we are using an "Inspect Component State" approach to navigate the component hierarchy to find the field type associated with the TextEditor.
208178 /// This ENTIRE next section is to JUST get the field type of the TextEditor being edited so we know if its a String, Float, Int etc. which makes it easier for the mod to determine how to handle the input.
209- private static Type GetFieldType ( TextEditor editor , bool verbose ) {
179+ private static Type GetFieldType ( TextEditor editor ) {
210180 try {
211181 var components = new List < Component > ( ) ;
212182 editor . Slot . GetComponentsInParents < Component > ( components ) ;
@@ -220,7 +190,6 @@ private static Type GetFieldType(TextEditor editor, bool verbose) {
220190 if ( targetTypeProp != null ) {
221191 var targetType = targetTypeProp . GetValue ( accessor ) as Type ;
222192 if ( targetType != null ) {
223- if ( verbose ) Msg ( $ "Found field type from MemberEditor.Accessor: { targetType . Name } ") ;
224193 return targetType ;
225194 }
226195 }
@@ -229,7 +198,6 @@ private static Type GetFieldType(TextEditor editor, bool verbose) {
229198 }
230199
231200 if ( editor . Text . Target is IField directField ) {
232- if ( verbose ) Msg ( $ "Text.Target is IField: { directField . ValueType . Name } ") ;
233201 return directField . ValueType ;
234202 }
235203
@@ -241,7 +209,6 @@ private static Type GetFieldType(TextEditor editor, bool verbose) {
241209 if ( genericTypeDef == typeof ( ValueField < > ) ) {
242210 var genericArgs = component . GetType ( ) . GetGenericArguments ( ) ;
243211 if ( genericArgs . Length > 0 ) {
244- if ( verbose ) Msg ( $ "Found ValueField<{ genericArgs [ 0 ] . Name } >") ;
245212 return genericArgs [ 0 ] ;
246213 }
247214 }
@@ -252,49 +219,44 @@ private static Type GetFieldType(TextEditor editor, bool verbose) {
252219 if ( slot != null ) {
253220 var parentField = slot . GetComponentInParents < IField > ( ) ;
254221 if ( parentField != null ) {
255- if ( verbose ) Msg ( $ "Found IField in parents: { parentField . ValueType . Name } ") ;
256222 return parentField . ValueType ;
257223 }
258224 }
259225 }
260226 }
261227
262- // Fallback: check editors slot hierarchy - JUSTTT in-case all the above fails. (which, I doubt it wont, but hey, safety first!)
263228 var editorField = editor . Slot . GetComponentInParents < IField > ( ) ;
264229 if ( editorField != null ) {
265- if ( verbose ) Msg ( $ "Found IField from editor slot: { editorField . ValueType . Name } ") ;
266230 return editorField . ValueType ;
267231 }
268232
269- if ( verbose ) Msg ( "Could not determine field type" ) ;
270233 return null ;
271- } catch ( Exception ex ) {
272- if ( verbose ) Warn ( $ "Error detecting field type: { ex . Message } ") ;
234+ } catch {
273235 return null ;
274236 }
275237 }
276238
277- private static string ProcessMathFunctions ( string input , bool verbose ) {
239+ private static string ProcessMathFunctions ( string input ) {
278240 string processed = input ;
279241
280- processed = ProcessFunction ( processed , "sqrt" , Math . Sqrt , verbose ) ;
281- processed = ProcessFunction ( processed , "sin" , x => Math . Sin ( x * Math . PI / 180.0 ) , verbose ) ;
282- processed = ProcessFunction ( processed , "cos" , x => Math . Cos ( x * Math . PI / 180.0 ) , verbose ) ;
283- processed = ProcessFunction ( processed , "tan" , x => Math . Tan ( x * Math . PI / 180.0 ) , verbose ) ;
284- processed = ProcessFunction ( processed , "asin" , x => Math . Asin ( x ) * 180.0 / Math . PI , verbose ) ;
285- processed = ProcessFunction ( processed , "acos" , x => Math . Acos ( x ) * 180.0 / Math . PI , verbose ) ;
286- processed = ProcessFunction ( processed , "atan" , x => Math . Atan ( x ) * 180.0 / Math . PI , verbose ) ;
287- processed = ProcessFunction ( processed , "log10" , Math . Log10 , verbose ) ;
288- processed = ProcessFunction ( processed , "log" , Math . Log , verbose ) ;
289- processed = ProcessFunction ( processed , "ln" , Math . Log , verbose ) ;
290- processed = ProcessFunction ( processed , "abs" , Math . Abs , verbose ) ;
291- processed = ProcessFunction ( processed , "floor" , Math . Floor , verbose ) ;
292- processed = ProcessFunction ( processed , "ceil" , Math . Ceiling , verbose ) ;
242+ processed = ProcessFunction ( processed , "sqrt" , Math . Sqrt ) ;
243+ processed = ProcessFunction ( processed , "sin" , x => Math . Sin ( x * Math . PI / 180.0 ) ) ;
244+ processed = ProcessFunction ( processed , "cos" , x => Math . Cos ( x * Math . PI / 180.0 ) ) ;
245+ processed = ProcessFunction ( processed , "tan" , x => Math . Tan ( x * Math . PI / 180.0 ) ) ;
246+ processed = ProcessFunction ( processed , "asin" , x => Math . Asin ( x ) * 180.0 / Math . PI ) ;
247+ processed = ProcessFunction ( processed , "acos" , x => Math . Acos ( x ) * 180.0 / Math . PI ) ;
248+ processed = ProcessFunction ( processed , "atan" , x => Math . Atan ( x ) * 180.0 / Math . PI ) ;
249+ processed = ProcessFunction ( processed , "log10" , Math . Log10 ) ;
250+ processed = ProcessFunction ( processed , "log" , Math . Log ) ;
251+ processed = ProcessFunction ( processed , "ln" , Math . Log ) ;
252+ processed = ProcessFunction ( processed , "abs" , Math . Abs ) ;
253+ processed = ProcessFunction ( processed , "floor" , Math . Floor ) ;
254+ processed = ProcessFunction ( processed , "ceil" , Math . Ceiling ) ;
293255
294256 return processed ;
295257 }
296258
297- private static string ProcessFunction ( string input , string funcName , Func < double , double > func , bool verbose ) {
259+ private static string ProcessFunction ( string input , string funcName , Func < double , double > func ) {
298260 var regex = new Regex ( $@ "{ funcName } \(([^)]+)\)", RegexOptions . IgnoreCase ) ;
299261 while ( regex . IsMatch ( input ) ) {
300262 var match = regex . Match ( input ) ;
@@ -308,38 +270,29 @@ private static string ProcessFunction(string input, string funcName, Func<double
308270 }
309271
310272 double funcResult = func ( innerValue ) ;
311-
312- // Floating Point Rounding - We all float down here...
313273 funcResult = Math . Round ( funcResult , 10 , MidpointRounding . AwayFromZero ) ;
314-
315274 input = input . Replace ( match . Value , funcResult . ToString ( CultureInfo . InvariantCulture ) ) ;
316-
317- if ( verbose ) Msg ( $ "Evaluated { funcName } ({ innerExpr } ) = { funcResult } ") ;
318275 }
319276 return input ;
320277 }
321278
322- private static bool TryEvaluateExpression ( string input , out string result , bool verbose ) {
279+ private static bool TryEvaluateExpression ( string input , out string result ) {
323280 result = input ;
324281
325282 // Container Checks - Operator, do we have a dial tone? - (basically checks for math operators then allows calculations)
326283 if ( ! ( input . Contains ( "+" ) || input . Contains ( "-" ) || input . Contains ( "*" ) ||
327284 input . Contains ( "/" ) || input . Contains ( "^" ) || input . Contains ( "%" ) ) ) {
328- if ( verbose ) Msg ( "No math operators found in input" ) ;
329285 return false ;
330286 }
331287
332288 try {
333- if ( verbose ) Msg ( $ "Attempting to parse expression: '{ input } '") ;
334-
335289 string processedInput = input ;
336290
337291 // Percentage Handler - you just HAD to use these instead of decimals didn't you? For SHAME!
338292 var percentRegex = new Regex ( @"(\d+(?:\.\d+)?)%" ) ;
339293 processedInput = percentRegex . Replace ( processedInput , match => {
340294 double percentValue = double . Parse ( match . Groups [ 1 ] . Value , CultureInfo . InvariantCulture ) ;
341295 string converted = $ "*{ ( percentValue / 100.0 ) . ToString ( CultureInfo . InvariantCulture ) } ";
342- if ( verbose ) Msg ( $ "Converted { match . Value } to { converted } ") ;
343296 return converted ;
344297 } ) ;
345298
@@ -373,31 +326,23 @@ private static bool TryEvaluateExpression(string input, out string result, bool
373326 processedInput = processedInput . Substring ( 0 , leftStart ) +
374327 powResult . ToString ( CultureInfo . InvariantCulture ) +
375328 processedInput . Substring ( rightEnd ) ;
376-
377- if ( verbose ) Msg ( $ "Evaluated power: { left } ^{ right } = { powResult } ") ;
378329 }
379330
380331 var table = new DataTable ( ) ;
381332 var evaluated = table . Compute ( processedInput , "" ) ;
382333 double resultValue = Convert . ToDouble ( evaluated ) ;
383-
384- // Floating Point Precision Handling
385334 resultValue = Math . Round ( resultValue , 7 , MidpointRounding . AwayFromZero ) ;
386-
387- if ( verbose ) Msg ( $ "Evaluated to: { resultValue } ") ;
388-
389335 result = resultValue . ToString ( CultureInfo . InvariantCulture ) ;
390336 return true ;
391337 } catch ( Exception ex ) {
392338 Warn ( $ "Expression evaluation failed for '{ input } ': { ex . Message } ") ;
393- if ( verbose ) Warn ( $ "Exception type: { ex . GetType ( ) . Name } ") ;
394339 return false ;
395340 }
396341 }
397342
398343 private static int FindNumberStart ( string expr , int from ) {
399344 while ( from > 0 && ( char . IsDigit ( expr [ from ] ) || expr [ from ] == '.' ||
400- ( expr [ from ] == '-' && from > 0 && ! char . IsDigit ( expr [ from - 1 ] ) ) ) ) {
345+ ( expr [ from ] == '-' && from > 0 && ! char . IsDigit ( expr [ from - 1 ] ) ) ) ) {
401346 from -- ;
402347 }
403348 if ( from == 0 && ( char . IsDigit ( expr [ 0 ] ) || expr [ 0 ] == '-' || expr [ 0 ] == '.' ) ) {
@@ -408,7 +353,7 @@ private static int FindNumberStart(string expr, int from) {
408353
409354 private static int FindNumberEnd ( string expr , int from ) {
410355 while ( from < expr . Length && ( char . IsDigit ( expr [ from ] ) || expr [ from ] == '.' ||
411- ( expr [ from ] == '-' && from == 0 ) ) ) {
356+ ( expr [ from ] == '-' && from == 0 ) ) ) {
412357 from ++ ;
413358 }
414359 return from ;
0 commit comments