public static string getOriginal(this MechDef def)
 {
     if (originals.TryGetValue(def.GetType(), out Dictionary <string, string> origs) == false)
     {
         return(def.ToJSON());
     }
     if (origs.TryGetValue(def.Description.Id, out string result) == false)
     {
         return(def.ToJSON());
     }
     return(result);
 }
Esempio n. 2
0
        public static void ExportCurrentMechDefToJson(this MechLabPanel mechLabPanel, string mechDefId, string mechDefName)
        {
            try
            {
                MechDef            mechDef          = new MechDef(mechLabPanel.activeMechDef, null, true);
                MechDef            baseMechDef      = new MechDef(mechLabPanel.Sim.DataManager.MechDefs.Get(mechDef.Description.Id), null, false);
                MechComponentRef[] mechDefInventory = (MechComponentRef[])AccessTools.Field(typeof(MechDef), "inventory").GetValue(mechDef);

                // Remove fixed equipment as it will be ignored from dismounting et all
                MechComponentRef[] mechDefInventoryFiltered = mechDefInventory.Where(component => component.IsFixed != true).ToArray();
                AccessTools.Field(typeof(MechDef), "inventory").SetValue(mechDef, mechDefInventoryFiltered);

                // Try to fix HardpointSlots the KISS way
                foreach (ChassisLocations chassisLocation in Enum.GetValues(typeof(ChassisLocations)))
                {
                    MechComponentRef[] mechDefWeaponsAtLocation = mechDefInventoryFiltered
                                                                  .Where(component => component.MountedLocation == chassisLocation)
                                                                  .Where(component => component.ComponentDefType == ComponentType.Weapon)
                                                                  .ToArray();

                    for (int i = 0; i < mechDefWeaponsAtLocation.Length; i++)
                    {
                        new Traverse(mechDefWeaponsAtLocation[i]).Property("HardpointSlot").SetValue(i);
                        Logger.Debug("[Extensions.ExportCurrentMechDefToJson] (" + chassisLocation + ") (" + mechDefWeaponsAtLocation[i].ComponentDefID + ") HardpointSlot: " + mechDefWeaponsAtLocation[i].HardpointSlot);
                    }
                }

                // Tag MechDef according to threatLevel/equipment
                //string additionalTag = Utilities.TagMechDefAccordingToInventory(mechDefInventory);
                int    threatLevel   = Utilities.GetExtraThreatLevelFromMechDef(mechDef, true);
                string additionalTag = Utilities.GetMechTagForThreatLevel(threatLevel);


                Logger.Debug("[Extensions.ExportCurrentMechDefToJson] additionalTag: " + additionalTag);

                // Set some halfway correct value for part value
                int simGameMechPartCost = mechDef.SimGameMechPartCost > 0 ? mechDef.SimGameMechPartCost : (mechDef.BattleValue / 10);

                string baseDirectory = $"{ MadLabs.ModDirectory}";
                string filePath      = Path.Combine(Path.Combine(baseDirectory, "MechDefs"), $"{mechDefId}.json");
                Directory.CreateDirectory(Directory.GetParent(filePath).FullName);



                using (StreamWriter streamWriter = new StreamWriter(filePath, false))
                //using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' })
                {
                    string mechDefJson = mechDef.ToJSON();
                    TagSet mechTags    = new TagSet(baseMechDef.MechTags);
                    mechTags.Add("unit_madlabs");
                    mechTags.Add(additionalTag);

                    if (MadLabs.Settings.BlacklistExportedMechDefs)
                    {
                        mechTags.Add("BLACKLISTED");
                    }

                    string mechTagsJson = mechTags.ToJSON();

                    // Fix MechDefJson
                    JObject jMechDef = JObject.Parse(mechDefJson);
                    JObject jBaseMechDefMechTagsJson = JObject.Parse(mechTagsJson);

                    jMechDef.Property("Chassis").Remove();
                    jMechDef.Property("PaintTextureID").Remove();
                    jMechDef.Property("HeraldryID").Remove();
                    jMechDef.Property("simGameMechPartCost").Remove();
                    jMechDef.Property("prefabOverride").Remove();

                    jMechDef["MechTags"] = jBaseMechDefMechTagsJson;

                    JObject jDescription = (JObject)jMechDef["Description"];
                    // Raise rarity?
                    jDescription["Rarity"] = (int)(mechDef.Description.Rarity + 4);

                    jDescription["Manufacturer"] = null;
                    jDescription["Model"]        = null;
                    jDescription["Name"]         = mechDefName;
                    jDescription["Id"]           = mechDefId;


                    jMechDef.Property("ChassisID").AddAfterSelf(new JProperty("HeraldryID", null));
                    jMechDef.Property("Description").AddAfterSelf(new JProperty("simGameMechPartCost", simGameMechPartCost));
                    jMechDef["Version"] = 1;

                    JArray jInventory = (JArray)jMechDef["inventory"];
                    foreach (JObject jComponent in jInventory)
                    {
                        jComponent["SimGameUID"] = null;
                        jComponent.Property("IsFixed").Remove();
                        jComponent["prefabName"]    = null;
                        jComponent["hasPrefabName"] = false;
                    }



                    streamWriter.Write(jMechDef.ToString());
                    //jMechDef.WriteTo(jsonTextWriter);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }