17
17
18
18
package org .photonvision .vision .processes ;
19
19
20
+ import java .lang .reflect .Field ;
20
21
import java .util .ArrayList ;
21
22
import java .util .Arrays ;
22
23
import java .util .Comparator ;
@@ -465,6 +466,17 @@ private static String createUniqueName(
465
466
}
466
467
}
467
468
469
+ private static List <Field > getAllFields (Class base ) {
470
+ List <Field > ret = new ArrayList <>();
471
+ ret .addAll (List .of (base .getDeclaredFields ()));
472
+ var superclazz = base .getSuperclass ();
473
+ if (superclazz != null ) {
474
+ ret .addAll (getAllFields (superclazz ));
475
+ }
476
+
477
+ return ret ;
478
+ }
479
+
468
480
public void changePipelineType (int newType ) {
469
481
// Find the PipelineType proposed
470
482
// To do this we look at all the PipelineType entries and look for one with
@@ -490,21 +502,38 @@ public void changePipelineType(int newType) {
490
502
return ;
491
503
}
492
504
493
- // Our new settings will be totally nuked, but that's ok
494
- // We *could* set things in common between the two, if we want
495
- // But they're different enough it shouldn't be an issue
496
- var name = getCurrentPipelineSettings ().pipelineNickname ;
497
- var newSettings = createSettingsForType (type , name );
498
-
499
505
var idx = currentPipelineIndex ;
500
506
if (idx < 0 ) {
501
507
logger .error ("Cannot replace non-user pipeline!" );
502
508
return ;
503
509
}
504
510
511
+ // The settings we used to have
512
+ var oldSettings = userPipelineSettings .get (idx );
513
+
514
+ var name = getCurrentPipelineSettings ().pipelineNickname ;
515
+ // Dummy settings to copy common fileds over
516
+ var newSettings = createSettingsForType (type , name );
517
+
518
+ // Copy all fields from AdvancedPipelineSettings/its superclasses from old to new
519
+ try {
520
+ for (Field field : getAllFields (AdvancedPipelineSettings .class )) {
521
+ Object value = field .get (oldSettings );
522
+ logger .debug ("setting " + field .getName () + " to " + value );
523
+ field .set (newSettings , value );
524
+ }
525
+ } catch (Exception e ) {
526
+ logger .error ("Couldn't copy old settings" , e );
527
+ }
528
+
505
529
logger .info ("Adding new pipe of type " + type + " at idx " + idx );
530
+
531
+ // type gets overritten by reflction hackery above
506
532
newSettings .pipelineIndex = idx ;
533
+ newSettings .pipelineType = type ;
534
+
507
535
userPipelineSettings .set (idx , newSettings );
536
+
508
537
setPipelineInternal (idx );
509
538
reassignIndexes ();
510
539
recreateUserPipeline ();
0 commit comments