Exemplo n.º 1
0
 public static void Postfix(HardpointDataDef __instance)
 {
     try
     {
         HardpointDataDef_FromJSON(__instance);
     }
     catch (Exception e)
     {
         Control.mod.Logger.LogError(e);
     }
 }
Exemplo n.º 2
0
 public static string getOriginal(this HardpointDataDef def)
 {
     if (originals.TryGetValue(def.GetType(), out Dictionary <string, string> origs) == false)
     {
         return(def.ToJSON());
     }
     if (origs.TryGetValue(def.ID, out string result) == false)
     {
         return(def.ToJSON());
     }
     return(result);
 }
Exemplo n.º 3
0
 public static void setOriginal(this HardpointDataDef def, string json)
 {
     if (originals.TryGetValue(def.GetType(), out Dictionary <string, string> origs) == false)
     {
         origs = new Dictionary <string, string>();
         originals.Add(def.GetType(), origs);
     }
     if (origs.ContainsKey(def.ID))
     {
         origs[def.ID] = json;
     }
     else
     {
         origs.Add(def.ID, json);
     }
 }
        // ReSharper disable once RedundantAssignment
        public static bool Prefix(HardpointDataDef hardpointDataDef, BaseComponentRef componentRef, string prefabBase, string location, ref List <string> usedPrefabNames, ref string __result)
        {
            try
            {
                if (componentRef is MechComponentRef mechComponentRef && mechComponentRef.ComponentDefType == ComponentType.Weapon)
                {
                    __result = calculator?.GetPrefabName(mechComponentRef);
                }

                return(__result == null);
            }
            catch (Exception e)
            {
                Control.mod.Logger.LogError(e);
                return(true);
            }
        }
Exemplo n.º 5
0
 public static bool Prefix(HardpointDataDef hardpointDataDef, BaseComponentRef componentRef, ref List <string> usedPrefabNames, ref string __result)
 {
     try
     {
         if (calculator != null && componentRef is MechComponentRef mechComponentRef && mechComponentRef.ComponentDefType == ComponentType.Weapon)
         {
             __result = calculator.GetPrefabName(mechComponentRef) ?? hardpointDataDef.HardpointData[0].weapons[0][0];
             usedPrefabNames.Add(__result);
             return(false);
         }
     }
     catch (Exception e)
     {
         Control.mod.Logger.LogError(e);
     }
     return(true);
 }
Exemplo n.º 6
0
        internal static void HardpointDataDef_FromJSON(HardpointDataDef def)
        {
            if (!HardpointFixFeature.Shared.Settings.AutoFixHardpointDataDefs)
            {
                return;
            }

            // chrPrfWeap_battlemaster_leftarm_ac20_bh1 -> chrPrfWeap_battlemaster_leftarm_ac20_1
            string Unique(string prefab)
            {
                return(prefab.Substring(0, prefab.Length - 3) + WeaponComponentPrefabCalculator.GroupNumber(prefab));
            }

            string SelectSingle(IEnumerable <string> enumerable)
            {
                var list   = enumerable.ToList();
                var chosen = list[0];

                if (list.Count > 1)
                {
                    var duplicates = string.Join(",", list.OrderBy(x => x).Distinct());
                    Control.mod.Logger.LogWarning($"Removing duplicate hardpoint entries [{duplicates}], kept a single {chosen}");
                }
                return(chosen);
            }

            // normalize data, remove all duplicates in each location
            for (var i = 0; i < def.HardpointData.Length; i++)
            {
                def.HardpointData[i].weapons = def.HardpointData[i].weapons
                                               .SelectMany(x => x)
                                               .OrderBy(x => x)
                                               .GroupBy(x => Unique(x))
                                               .Select(x => SelectSingle(x))
                                               .GroupBy(x => WeaponComponentPrefabCalculator.GroupNumber(x))
                                               .OrderBy(x => x.Key)
                                               .Select(x => x.ToArray())
                                               .ToArray();
            }
        }