@@ -391,11 +391,7 @@ private void DeserializeItem(LuaTable table, ErrorCollector _) {
391
391
item . stackSize = table . Get ( "stack_size" , 1 ) ;
392
392
393
393
if ( item . locName == null && table . Get ( "placed_as_equipment_result" , out string ? result ) ) {
394
- Localize ( "equipment-name." + result , null ) ;
395
-
396
- if ( localeBuilder . Length > 0 ) {
397
- item . locName = FinishLocalize ( ) ;
398
- }
394
+ item . locName = LocalisedStringParser . Parse ( "equipment-name." + result , [ ] ) ! ;
399
395
}
400
396
if ( table . Get ( "fuel_value" , out string ? fuelValue ) ) {
401
397
item . fuelValue = ParseEnergy ( fuelValue ) ;
@@ -505,166 +501,6 @@ private void DeserializeFluid(LuaTable table, ErrorCollector _) {
505
501
return null ;
506
502
}
507
503
508
- private readonly StringBuilder localeBuilder = new StringBuilder ( ) ;
509
-
510
- private void Localize ( object obj ) {
511
- if ( obj is LuaTable table ) {
512
- if ( ! table . Get ( 1 , out string ? key ) ) {
513
- return ;
514
- }
515
-
516
- Localize ( key , table ) ;
517
- }
518
- else {
519
- _ = localeBuilder . Append ( obj ) ;
520
- }
521
- }
522
-
523
- private string FinishLocalize ( ) {
524
- _ = localeBuilder . Replace ( "\\ n" , "\n " ) ;
525
-
526
- // Cleaning up tags using simple state machine
527
- // 0 = outside of tag, 1 = first potential tag char, 2 = inside possible tag, 3 = inside definite tag
528
- // tag is definite when it contains '=' or starts with '/' or '.'
529
- int state = 0 , tagStart = 0 ;
530
- for ( int i = 0 ; i < localeBuilder . Length ; i ++ ) {
531
- char chr = localeBuilder [ i ] ;
532
-
533
- switch ( state ) {
534
- case 0 :
535
- if ( chr == '[' ) {
536
- state = 1 ;
537
- tagStart = i ;
538
- }
539
- break ;
540
- case 1 :
541
- if ( chr == ']' ) {
542
- state = 0 ;
543
- }
544
- else {
545
- state = ( chr is '/' or '.' ) ? 3 : 2 ;
546
- }
547
-
548
- break ;
549
- case 2 :
550
- if ( chr == '=' ) {
551
- state = 3 ;
552
- }
553
- else if ( chr == ']' ) {
554
- state = 0 ;
555
- }
556
-
557
- break ;
558
- case 3 :
559
- if ( chr == ']' ) {
560
- _ = localeBuilder . Remove ( tagStart , i - tagStart + 1 ) ;
561
- i = tagStart - 1 ;
562
- state = 0 ;
563
- }
564
- break ;
565
- }
566
- }
567
-
568
- string s = localeBuilder . ToString ( ) ;
569
- _ = localeBuilder . Clear ( ) ;
570
-
571
- return s ;
572
- }
573
-
574
- private void Localize ( string ? key , LuaTable ? table ) {
575
- if ( string . IsNullOrEmpty ( key ) ) {
576
- if ( table == null ) {
577
- return ;
578
- }
579
-
580
- foreach ( object ? elem in table . ArrayElements ) {
581
- if ( elem is LuaTable sub ) {
582
- Localize ( sub ) ;
583
- }
584
- else {
585
- _ = localeBuilder . Append ( elem ) ;
586
- }
587
- }
588
- return ;
589
- }
590
-
591
- key = FactorioLocalization . Localize ( key ) ;
592
-
593
- if ( key == null ) {
594
- if ( table != null ) {
595
- _ = localeBuilder . Append ( string . Join ( " " , table . ArrayElements < string > ( ) ) ) ;
596
- }
597
-
598
- return ;
599
- }
600
-
601
- if ( ! key . Contains ( "__" ) ) {
602
- _ = localeBuilder . Append ( key ) ;
603
- return ;
604
- }
605
-
606
- using var parts = ( ( IEnumerable < string > ) key . Split ( "__" ) ) . GetEnumerator ( ) ;
607
-
608
- while ( parts . MoveNext ( ) ) {
609
- _ = localeBuilder . Append ( parts . Current ) ;
610
-
611
- if ( ! parts . MoveNext ( ) ) {
612
- break ;
613
- }
614
-
615
- string control = parts . Current ;
616
-
617
- if ( control is "ITEM" or "FLUID" or "RECIPE" or "ENTITY" ) {
618
- if ( ! parts . MoveNext ( ) ) {
619
- break ;
620
- }
621
-
622
- string subKey = control . ToLowerInvariant ( ) + "-name." + parts . Current ;
623
- Localize ( subKey , null ) ;
624
- }
625
- else if ( control == "CONTROL" ) {
626
- if ( ! parts . MoveNext ( ) ) {
627
- break ;
628
- }
629
-
630
- _ = localeBuilder . Append ( parts . Current ) ;
631
- }
632
- else if ( control == "ALT_CONTROL" ) {
633
- if ( ! parts . MoveNext ( ) || ! parts . MoveNext ( ) ) {
634
- break ;
635
- }
636
-
637
- _ = localeBuilder . Append ( parts . Current ) ;
638
- }
639
- else if ( table != null && int . TryParse ( control , out int i ) ) {
640
- if ( table . Get ( i + 1 , out string ? s ) ) {
641
- Localize ( s , null ) ;
642
- }
643
- else if ( table . Get ( i + 1 , out LuaTable ? t ) ) {
644
- Localize ( t ) ;
645
- }
646
- else if ( table . Get ( i + 1 , out float f ) ) {
647
- _ = localeBuilder . Append ( f ) ;
648
- }
649
- }
650
- else if ( control . StartsWith ( "plural" ) ) {
651
- _ = localeBuilder . Append ( "(???)" ) ;
652
-
653
- if ( ! parts . MoveNext ( ) ) {
654
- break ;
655
- }
656
- }
657
- else {
658
- // Not supported token... Append everything else as-is
659
- while ( parts . MoveNext ( ) ) {
660
- _ = localeBuilder . Append ( parts . Current ) ;
661
- }
662
-
663
- break ;
664
- }
665
- }
666
- }
667
-
668
504
private T DeserializeCommon < T > ( LuaTable table , string prototypeType ) where T : FactorioObject , new ( ) {
669
505
if ( ! table . Get ( "name" , out string ? name ) ) {
670
506
throw new NotSupportedException ( $ "Read a definition of a { prototypeType } that does not have a name.") ;
@@ -674,22 +510,19 @@ private void Localize(string? key, LuaTable? table) {
674
510
target . factorioType = table . Get ( "type" , "" ) ;
675
511
676
512
if ( table . Get ( "localised_name" , out object ? loc ) ) { // Keep UK spelling for Factorio/LUA data objects
677
- Localize ( loc ) ;
513
+ target . locName = LocalisedStringParser . Parse ( loc ) ! ;
678
514
}
679
515
else {
680
- Localize ( prototypeType + "-name." + target . name , null ) ;
516
+ target . locName = LocalisedStringParser . Parse ( prototypeType + "-name." + target . name , [ ] ) ! ;
681
517
}
682
518
683
- target . locName = localeBuilder . Length == 0 ? null ! : FinishLocalize ( ) ; // null-forgiving: We have another chance at the end of CalculateMaps.
684
-
685
519
if ( table . Get ( "localised_description" , out loc ) ) { // Keep UK spelling for Factorio/LUA data objects
686
- Localize ( loc ) ;
520
+ target . locDescr = LocalisedStringParser . Parse ( loc ) ;
687
521
}
688
522
else {
689
- Localize ( prototypeType + "-description." + target . name , null ) ;
523
+ target . locDescr = LocalisedStringParser . Parse ( prototypeType + "-description." + target . name , [ ] ) ;
690
524
}
691
525
692
- target . locDescr = localeBuilder . Length == 0 ? null : FinishLocalize ( ) ;
693
526
_ = table . Get ( "icon_size" , out float defaultIconSize ) ;
694
527
695
528
if ( table . Get ( "icon" , out string ? s ) ) {
0 commit comments