@@ -19,11 +19,17 @@ namespace Torch.Patches
1919 [ PatchShim ]
2020 public static class FactionPatch
2121 {
22+ public const int MAX_FACTION_INFO_LENGTH = 512 ;
23+ public const int MAX_FACTION_NAME_LENGTH = 64 ;
24+ public const int PLAYER_FACTION_TAG_LENGTH = 3 ;
25+ public const int NPC_FACTION_TAG_LENGTH = 4 ;
26+
2227 private static Logger _log = LogManager . GetCurrentClassLogger ( ) ;
2328 private static MethodInfo _registerFactionTagMethod = typeof ( MyFactionCollection ) . GetMethod ( "RegisterFactionTag" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
2429 private static MethodInfo _addMethod = typeof ( MyFactionCollection ) . GetMethod ( "Add" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
2530 private static MethodInfo _compatDefaultFactions = typeof ( MyFactionCollection ) . GetMethod ( "CompatDefaultFactions" , BindingFlags . Public | BindingFlags . Instance ) ;
26-
31+ private static MethodInfo _cleanupFactionMsgMethod = typeof ( MyFactionCollection ) . GetMethod ( "CleanUpFactionMessage" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
32+
2733 public static void Patch ( PatchContext ctx )
2834 {
2935 ctx . GetPattern ( typeof ( MyFactionCollection ) . GetMethod ( "Add" , BindingFlags . NonPublic | BindingFlags . Instance ) ) . Prefixes . Add ( typeof ( FactionPatch ) . GetMethod ( nameof ( AddPrefix ) ) ) ;
@@ -78,12 +84,31 @@ public static bool InitPrefix(MyObjectBuilder_FactionCollection builder, MyFacti
7884 var m_factionRequests = typeof ( MyFactionCollection ) . GetField ( "m_factionRequests" , BindingFlags . NonPublic | BindingFlags . Instance ) . GetValue ( __instance ) ;
7985 var m_playerToFactionsVis = typeof ( MyFactionCollection ) . GetField ( "m_playerToFactionsVis" , BindingFlags . NonPublic | BindingFlags . Instance ) . GetValue ( __instance ) ;
8086
87+ int _factionCounter = 0 ;
8188 foreach ( var factionBuilder in builder . Factions )
8289 {
90+ _factionCounter ++ ;
91+
8392 // For compatibility from before there was currency in the game.
8493 // If existing faction have account already, do not create another one.
8594 if ( ! MyBankingSystem . Static . TryGetAccountInfo ( factionBuilder . FactionId , out var accountInfo ) )
8695 MyBankingSystem . Static . CreateAccount ( factionBuilder . FactionId , 0 ) ;
96+
97+ factionBuilder . Name = CleanUpStringInput ( factionBuilder . Name , _factionCounter ) ;
98+ factionBuilder . Description = CleanUpStringInput ( factionBuilder . Description , _factionCounter ) ;
99+ factionBuilder . Tag = CleanUpStringInput ( factionBuilder . Tag , _factionCounter ) ;
100+ factionBuilder . PrivateInfo = CleanUpStringInput ( factionBuilder . PrivateInfo , _factionCounter ) ;
101+
102+ factionBuilder . Name = factionBuilder . Name . Substring ( 0 , Math . Min ( factionBuilder . Name . Length , MAX_FACTION_NAME_LENGTH ) ) ;
103+ factionBuilder . Tag = factionBuilder . Tag . Substring ( 0 , Math . Min ( factionBuilder . Tag . Length , NPC_FACTION_TAG_LENGTH ) ) ;
104+
105+ //do the same substring but with null checks for the description and private info
106+ if ( factionBuilder . Description != null )
107+ factionBuilder . Description = factionBuilder . Description . Substring ( 0 , Math . Min ( factionBuilder . Description . Length , MAX_FACTION_INFO_LENGTH ) ) ;
108+
109+ if ( factionBuilder . PrivateInfo != null )
110+ factionBuilder . PrivateInfo = factionBuilder . PrivateInfo . Substring ( 0 , Math . Min ( factionBuilder . PrivateInfo . Length , MAX_FACTION_INFO_LENGTH ) ) ;
111+
87112
88113 _addMethod . Invoke ( __instance , new object [ ] { new MyFaction ( factionBuilder ) } ) ;
89114 }
@@ -210,5 +235,17 @@ public static bool InitPrefix(MyObjectBuilder_FactionCollection builder, MyFacti
210235 _log . Info ( "Factions INIT END" ) ;
211236 return false ;
212237 }
238+
239+ private static string CleanUpStringInput ( string input , int factionCountCurrent )
240+ {
241+ if ( input == null )
242+ {
243+ return input ;
244+ }
245+
246+ input = input . Replace ( '&' , ' ' ) . Replace ( '#' , ' ' ) ;
247+ //do the same but after, if the input is an empty string or just whitespace, return "Potentially Exploited Faction (INVESTIGATE)"
248+ return string . IsNullOrWhiteSpace ( input ) ? $ "Potentially Exploited Faction data (INVESTIGATE)-{ factionCountCurrent } " : input ;
249+ }
213250 }
214251}
0 commit comments