예제 #1
0
        /// <summary>Replace whatever faction are in the receiver by those of this editor</summary>
        public void ReplaceFactionToWrite(SSModWritable receiver)
        {
            if (Factions == null)
            {
                throw new InvalidOperationException("no factions merged");
            }

            IEnumerable <SSFactionGroup> OldFactions = from ISSWritable w in receiver.FileList
                                                       where w is SSFactionGroup
                                                       select w as SSFactionGroup;

            foreach (SSFactionGroup f in OldFactions.ToList())
            {
                receiver.FileList.Remove(f);
            }
            foreach (SSFactionGroup f in Factions)
            {
                receiver.FileList.Add(f);
            }

            SSRelativeUrl settingUrl  = new SSRelativeUrl("data\\config\\settings.json");
            SSJson        SettingFile = (from ISSWritable w in receiver.FileList
                                         where w.RelativeUrl.Equals(settingUrl)
                                         select w as SSJson).SingleOrDefault();

            Portraits = new List <JsonValue>(Ressources.PortraitsRessources.GetOriginalPortraits(Factions));
            var        UsedPortraits  = new List <JsonValue>(Ressources.PortraitsRessources.GetCurrentPortraits(Factions));
            var        UnusedPortrait = Portraits.Except(UsedPortraits);
            JsonObject finalPortraits = new JsonObject(UnusedPortrait, "portraits");

            if (SettingFile == null)
            {
                SettingFile             = new SSJson(receiver, settingUrl);
                SettingFile.JsonContent = new JsonObject();
                receiver.FileList.Add(SettingFile);
                SettingFile.JsonContent.AddSubField(".graphics.portraits", finalPortraits);
                SettingFile.RefreshFields();
            }
            else
            {
                JsonToken setted;
                SettingFile.Fields.TryGetValue(".graphics.portraits", out setted);
                if (setted == null)
                {
                    SettingFile.JsonContent.AddSubField(".graphics.portraits", finalPortraits);
                }
                else
                {
                    JsonObject set = (JsonObject)setted;
                    set.Values.Clear();
                    foreach (KeyValuePair <JsonValue, JsonToken> kv in finalPortraits.Values)
                    {
                        set.Values.Add(kv.Key, kv.Value);
                    }
                    SettingFile.RefreshFields();
                }
            }



            IEnumerable <SSFactionGroup> OverWritten = from SSFactionGroup f in Factions
                                                       where f.MustOverwrite == true
                                                       select f;
            IEnumerable <string> ModOverWritten = OverWritten.Select(f => f.MonitoredContent).SelectMany(m => m.Files).Select(f => f.SourceMod).Distinct().Select(mod => mod.ModName);

            IEnumerable <JsonValue> AddedPortrait = Factions.FindAll(f => f.MonitoredContent.IsModified()).SelectMany(f =>
            {
                List <MonitoredArrayModification> result = new List <MonitoredArrayModification>();
                result.AddRange(f.MalePortraits.GetAddedMod());
                result.AddRange(f.FemalePortraits.GetAddedMod());
                return(result);
            }).Select(m => ((JsonValue)m.Content)).Distinct();

            JsonRelativeToPortraits converter = new JsonRelativeToPortraits();
            IEnumerable <string>    addMod    = AddedPortrait.Select(j => converter.Convert(new object[] { j, PortraitsRessource }, null, null, null)).Select(p => ((Portraits)p).SourceModName).Distinct();

            var       together = (ModOverWritten ?? Enumerable.Empty <string>()).Concat(addMod ?? Enumerable.Empty <string>()).Distinct();
            JsonValue OldDesc  = receiver.ModInfo.Fields[".description"] as JsonValue;
            string    old      = OldDesc.ToString();

            OldDesc.SetContent(old + " Faction were modified using mods: " + string.Join(", ", together));
        }