Exemple #1
0
        private static bool HandleExtensionSwitches(Dictionary <string, string> config, out Dictionary <string, string> modifiedExtensionSwitches)
        {
            modifiedExtensionSwitches = new Dictionary <string, string>();
            Dictionary <string, string> toReplace = new Dictionary <string, string>();

            foreach (var entry in config)
            {
                if (!entry.Key.StartsWith(TAGS.EXTENSION_SWITCH))
                {
                    continue;
                }
                string[] splitVal = entry.Value.Split("="); if (splitVal.Count() < 2)
                {
                    continue;
                }
                string extName = splitVal[0], extVal = splitVal[1];
                modifiedExtensionSwitches.Add(extName, extVal);
                if (!EM_Helpers.IsGuid(extName))
                {
                    toReplace.Add(entry.Key, extName.ToLower());
                }
            }
            if (toReplace.Count == 0)
            {
                return(true);
            }

            EMPath       pathHandler       = new EMPath(config[TAGS.CONFIG_PATH_EUROMODFILES]);
            Communicator dummyCommunicator = new Communicator();
            var          globExt           = ExeXmlReader.ReadExtensions(pathHandler.GetExtensionsFilePath(), dummyCommunicator);

            // first search in global file ...
            List <string> done = new List <string>();

            foreach (var tr in toReplace)
            {
                string extName = tr.Value, configKey = tr.Key;
                // the name of the extension must be replaced by the id:
                foreach (var e in globExt)
                {
                    string extId = e.Key, shortName = e.Value.Item1.ToLower(), longName = e.Value.Item2.ToLower();
                    if (extName == shortName || extName == longName)
                    {
                        Replace(configKey, extId); break;
                    }
                }
            }
            // ... if any extension-name was not found in global file, search in country file
            if (done.Count == toReplace.Count)
            {
                return(true);
            }
            ExeXml.Country country = ExeXmlReader.ReadCountry(pathHandler.GetCountryFilePath(config[TAGS.CONFIG_COUNTRY]),
                                                              config[TAGS.CONFIG_ID_SYSTEM], config[TAGS.CONFIG_ID_DATA], false, dummyCommunicator);
            foreach (var tr in toReplace)
            {
                string extName = tr.Value, configKey = tr.Key;
                if (done.Contains(configKey))
                {
                    continue;
                }
                foreach (var e in from ce in country.extensions where ce.Value.localShortName != null select ce)
                {
                    string extId = e.Key, shortName = e.Value.localShortName.ToLower(),
                             longName = e.Value.localLongName; if (longName != null)
                    {
                        longName = longName.ToLower();
                    }
                    if (extName == shortName || extName == longName)
                    {
                        Replace(configKey, extId); break;
                    }
                }
            }

            if (done.Count == toReplace.Count)
            {
                return(true);
            }

            string notDone = string.Empty; foreach (var tr in toReplace)

            {
                if (!done.Contains(tr.Key))
                {
                    notDone += tr.Value + " ";
                }
            }

            Console.WriteLine($"Unknown extension(s): {notDone}");
            return(false);

            void Replace(string configKey, string extId)
            {
                config[configKey] = $"{extId}={config[configKey].Split("=")[1]}"; done.Add(configKey);
            }
        }
        private void TakeAddOnExtensionSwitches(List <ExeXml.AddOn> addOns)
        {
            // make a list of extensions that are switched by the add-on(s), if several add-ons switch the same extension, the last one wins
            Dictionary <string, bool> aoExtensionSwitches = new Dictionary <string, bool>(); // key: extension-id, value: on/off (on=true)

            foreach (ExeXml.AddOn addOn in addOns)                                           // run over all add-ons
            {
                foreach (ExeXml.Fun aoFun in addOn.polAOControl.funs.Values)                 // search for add-on-function AddOn_ExtensionSwitch
                {
                    if (!aoFun.on || aoFun.Name.ToLower() != DefFun.AddOn_ExtensionSwitch.ToLower())
                    {
                        continue;
                    }

                    // first sort the groups together (there may be several groups of Extension_Id or Extension_Name + Extension_Switch)
                    Dictionary <string, List <ExeXml.Par> > parGroups = new Dictionary <string, List <ExeXml.Par> >();
                    foreach (ExeXml.Par aoPar in aoFun.pars.Values)
                    {
                        if (!parGroups.ContainsKey(aoPar.Group))
                        {
                            parGroups.Add(aoPar.Group, new List <ExeXml.Par>());
                        }
                        parGroups[aoPar.Group].Add(aoPar);
                    }

                    // check if the content of each group is ok
                    Description fDesc = new Description(addOn.polAOControl, aoFun); // just for error-messages
                    foreach (var parGroup in parGroups)
                    {
                        string        extID = null, extName = null, extSwitch = null;
                        List <string> dataPatterns = new List <string>(), sysPatterns = new List <string>();
                        foreach (ExeXml.Par par in parGroup.Value)
                        {
                            if (par.val == DefPar.Value.NA)
                            {
                                continue;
                            }
                            if (par.Name.ToLower() == DefPar.AddOn_ExtensionSwitch.Extension_Id.ToLower())
                            {
                                extID = par.val.Trim().ToLower();
                            }
                            else if (par.Name.ToLower() == DefPar.AddOn_ExtensionSwitch.Extension_Name.ToLower())
                            {
                                extName = par.val.Trim().ToLower();
                            }
                            else if (par.Name.ToLower() == DefPar.AddOn_ExtensionSwitch.Extension_Switch.ToLower())
                            {
                                extSwitch = par.val.Trim().ToLower();
                            }
                            else if (par.Name.ToLower() == DefPar.AddOn_ExtensionSwitch.Dataset.ToLower())
                            {
                                dataPatterns.Add(par.val.Trim());
                            }
                            else if (par.Name.ToLower() == DefPar.AddOn_ExtensionSwitch.System.ToLower())
                            {
                                sysPatterns.Add(par.val.Trim());
                            }
                            else
                            {
                                MakeWarning($"unknown parameter {par.Name} is ignored", false);
                            }
                        }

                        if (extID == null && extName == null && extSwitch == null &&
                            dataPatterns.Count() == 0 && sysPatterns.Count() == 0)
                        {
                            continue;                                                        // probably all set to n/a
                        }
                        if (extID == null && extName == null)
                        {
                            MakeWarning($"neither {DefPar.AddOn_ExtensionSwitch.Extension_Name} nor {DefPar.AddOn_ExtensionSwitch.Extension_Id} defined");
                            continue;
                        }

                        if (extID != null)
                        {
                            if (!infoStore.country.extensions.ContainsKey(extID))
                            {
                                MakeWarning($"unknown extension id {extID}"); continue;
                            }
                        }
                        if (extName != null)
                        {
                            if (extID == null)
                            {
                                extID = GetIdByName(extName);
                                if (extID == null)
                                {
                                    MakeWarning($"unknown extension name {extName}"); continue;
                                }
                            }
                            else
                            {
                                MakeWarning($"{DefPar.AddOn_ExtensionSwitch.Extension_Name} is ignored, as {DefPar.AddOn_ExtensionSwitch.Extension_Id} is defined as well", false);
                            }

                            // note that no warning is issued, if a global extension exsits, but there is no implementation for the country
                            // or even only no switch for the system-data-combination
                            // one could easily catch this, by some rearranging of code, but I think it's safer to avoid a maybe unwanted error-messsage
                        }

                        if (extSwitch != null)
                        {
                            if (extSwitch != DefPar.Value.ON && extSwitch != DefPar.Value.OFF)
                            {
                                MakeWarning($"invalid value for {DefPar.AddOn_ExtensionSwitch.Extension_Switch}, must be {DefPar.Value.ON} or {DefPar.Value.OFF}"); continue;
                            }
                        }
                        else
                        {
                            MakeWarning($"parameter {DefPar.AddOn_ExtensionSwitch.Extension_Switch} missing"); continue;
                        }

                        bool patternMatches = dataPatterns.Count == 0;
                        foreach (string pattern in dataPatterns)
                        {
                            if (infoStore.IsUsedDatabase(pattern))
                            {
                                patternMatches = true; break;
                            }
                        }
                        if (!patternMatches)
                        {
                            continue;
                        }

                        patternMatches = sysPatterns.Count == 0;
                        foreach (string pattern in sysPatterns)
                        {
                            if (EM_Helpers.DoesValueMatchPattern(pattern, infoStore.country.sys.name))
                            {
                                patternMatches = true; break;
                            }
                        }
                        if (!patternMatches)
                        {
                            continue;
                        }

                        aoExtensionSwitches.Add(extID, extSwitch == DefPar.Value.ON);

                        void MakeWarning(string message, bool addIgnore = true)
                        {
                            infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                            {
                                isWarning = true,
                                message   = $"{fDesc.Get()} (group {parGroup.Key}): {message}" + (addIgnore ? ", switch-redefinition is ignored" : string.Empty)
                            });
                        }
                    }
                }
            }

            TakeExtensionSwitches(aoExtensionSwitches);

            string GetIdByName(string name)
            {
                // first try if the name belongs to a local extension (where we have got the name, as it is stored in the country file) ...
                var lext = from e in infoStore.country.extensions
                           where (e.Value.localLongName != null && e.Value.localLongName.ToLower() == name) ||
                           (e.Value.localShortName != null && e.Value.localShortName.ToLower() == name)
                           select e;

                if (lext.Count() > 0)
                {
                    return(lext.First().Key);
                }

                // ... only if not found, need to read gloabl extensions
                var gext = from e in ExeXmlReader.ReadExtensions(infoStore.runConfig.pathHandler.GetExtensionsFilePath(), infoStore.communicator)
                           where (e.Value.Item1 != null && e.Value.Item1.ToLower() == name) ||
                           (e.Value.Item2 != null && e.Value.Item2.ToLower() == name)
                           select e;

                return(gext.Count() > 0 ? gext.First().Key : null);
            }
        }