Пример #1
0
 /// <summary>
 /// Convert and copy fields from a ModnixAction to a ModDefinition.
 /// Although converting to json and back is simpler, it is also more risky, less type safe.
 /// </summary>
 internal static void ConvertActionToMod(ModnixAction action, ModDefinition def)
 {
     def.guid    = action.GetText("guid");
     def.cls     = action.GetText("cls");
     def.field   = action.GetText("field");
     def.comment = action.GetText("comment");
     action.TryGetValue("value", out def.value);
     if (action.TryGetValue("modletlist", out object mlist) && mlist is JArray list)
     {
         def.modletlist = new List <ModletStep>();
         foreach (var item in list.OfType <JObject>())
         {
             var step = new ModletStep
             {
                 field = item.GetText("field"),
                 value = item.GetValue("value").ToObject <object>(),
             };
             def.modletlist.Add(step);
         }
     }
     if (action.TryGetValue("flags", out object flist) && flist is JArray flags)
     {
         def.flags = flags.Select((f) => f?.ToString()).ToArray();
     }
 }
Пример #2
0
        /// <summary>
        /// Entry point for Modnix 3 actions.
        /// Action syntax is the same as traditional PPDefModifier mods.
        /// </summary>
        public static bool ActionMod(string modid, ModnixAction action)
        {
            if (action == null || (!action.ContainsKey("cls") && !action.ContainsKey("guid")))
            {
                return(false);
            }
            if (!action.ContainsKey("value") && !action.ContainsKey("modletlist"))
            {
                return(false);
            }

            ModFile modfile = GetModFile(modid);

            if (modid != lastactionmod)
            {
                logger.Log("Applying mod actions in {0}", modfile);
                lastactionmod = modid;
            }

            ModDefinition def = new ModDefinition();

            try
            {
                ConvertActionToMod(action, def);
                modfile.ApplyMod(def);
                return(true);
            }
            catch (Exception e)
            {
                logger.Error("Error applying mod action {0}: {1}", def.GetModName(), e.ToString());
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        ///  Apply one mod
        /// </summary>
        /// <param name="def">The mod def to apply</param>
        public void ApplyMod(ModDefinition def)
        {
            Mod mod = new Mod(fileName, def, repo);

            mod.Validate();

            // If the mod contains a modletlist expand it out into individual mods and apply each in turn.
            if (def.modletlist != null)
            {
                // The index
                int modletIndex = 1;
                // Make a new ModifierDefinition for each modlet in the modlet list and apply it.
                foreach (ModletStep modlet in def.modletlist)
                {
                    if (modlet.field == null || modlet.value == null)
                    {
                        // Skip any modlets that are malformed. Do this gracefully so we don't break a sequence.
                        logger.Error("Modlet entry {0} is missing field or value in {1}", modletIndex, def.GetModName());
                        continue;
                    }
                    ModDefinition tmpDef = new ModDefinition
                    {
                        guid  = def.guid,
                        cls   = def.cls,
                        field = modlet.field,
                        value = modlet.value
                    };

                    Mod tmpMod = new Mod(fileName, tmpDef, repo);

                    try
                    {
                        tmpMod.Apply();
                    }
                    catch (Exception e)
                    {
                        logger.Error("Error applying modlet field {0} in {1}: {2}", modlet.field, def.GetModName(), e.ToString());
                    }

                    logger.Log("Successfully applied modlet field {0} in {1}", modlet.field, def.GetModName());
                    ++modletIndex;
                }

                // Once we're done exit from the function.
                return;
            }
            else
            {
                mod.Apply();
                logger.Log("Successfully applied {0}", def.GetModName());
            }
        }
Пример #4
0
 /// <summary>
 /// Construct a new mod object
 /// </summary>
 /// <param name="filename">The name of the containing configuration file, for error and log messages.</param>
 /// <param name="def">The ModDefinition deserialized from the configuration file.</param>
 /// <param name="repo">The def repo into which to apply the change</param>
 public Mod(String filename, ModDefinition def, IDefRepository repo)
 {
     this.filename = filename;
     this.def      = def;
     this.repo     = repo;
 }