private void CheckCryptoLib(SortedList<int, RequiredLibrary> Libraryes, IList<LogQueueItem> LogMessages)
        {
            PKParser.Arg.PKArgParser ArgParser = new PKParser.Arg.PKArgParser();

            foreach (RequiredLibrary lib in Libraryes.Values)
            {
                if (lib.Name.ToLower() == "crypto.lib")
                {
                    if (lib.ParamCollection != null) return;

                    foreach (LogQueueItem item in LogMessages)
                    {
                        if (item.Message.ToLower().Contains(ARMARPath))
                        {
                            ParamCollection par = ArgParser.Add(item.Message);
                            if (par == null) continue;

                            bool Found = false;

                            foreach (ArgCollection arg in par.Values)
                            {
                                if (arg.Tag == "archive")
                                {
                                    if (Path.GetFileName(arg.Name).ToLower() == "crypto_stub.lib") Found = true;
                                }
                            }

                            if (!Found) continue;

                            lib.ConsoleString = item.Message;
                            lib.ParamCollection = par;
                            lib.BuildList.AddRange(item.ProjectBuildContext);
                            lib.OutputFileuVision = uVisionProjectPath + "\\" + uVisionTargetsPath + "\\" + Path.GetFileNameWithoutExtension(lib.Name) + "\\" + Path.GetFileName(lib.Name);

                            ProjectCollection LocalProjColl = new ProjectCollection();
                            Project MSBProj = LocalProjColl.LoadProject(item.ProjectBuildContext[item.ProjectBuildContext.Count - 1]);
                            string LIB_EXT = MSBProj.GetPropertyValue("LIB_EXT");
                            string OBJ_EXT = MSBProj.GetPropertyValue("OBJ_EXT");


                            foreach (string file in par.Files.Values)
                            {
                                RequiredLibrary libr = null;
                                RequiredObject obj = null;
                                if (Path.GetExtension(file).ToLower() == "." + LIB_EXT.ToLower())
                                {
                                    libr = new RequiredLibrary(file, lib);
                                    lib.InputLibraries.Add(lib.InputLibraries.Count + 1, libr);
                                }

                                if (Path.GetExtension(file).ToLower() == "." + OBJ_EXT.ToLower())
                                {
                                    obj = new RequiredObject(file, lib);
                                    lib.InputObjects.Add(lib.InputObjects.Count + 1, obj);
                                }
                                if (libr != null) lib.AllInputs.Add(lib.AllInputs.Count + 1, libr);
                                if (obj != null) lib.AllInputs.Add(lib.AllInputs.Count + 1, obj);
                            }


                            foreach (ArgCollection arg in par.Values)
                            {
                                switch (arg.Tag)
                                {
                                    case "options":
                                        if (arg.Count > 0)
                                            foreach (string s in arg)
                                            {
                                                lib.OptionsString.Add(arg.Name + " " + s);
                                            }
                                        else
                                            lib.OptionsString.Add(arg.Name);
                                        break;
                                    case "archive":
                                        lib.OutputFileMSBuild = arg.Name;
                                        break;
                                }
                            }
                        }
                    }
                    if (lib.InputLibraries.Count > 0) this.AnalizeInputLibraries(lib.InputLibraries, LogMessages);
                }
            }
        }
        void m_worker_DoWork(object sender, DoWorkEventArgs e)
        {

            //if (Directory.Exists(uVisionProjectPath)) Directory.Delete(uVisionProjectPath, true);

            OnLogText("start analize");

            //ObjectList.Clear();

            Directory.CreateDirectory(uVisionProjectPath);

            WorkerParams pars = (WorkerParams)e.Argument;

            PKParser.Arg.PKArgParser ArgParser = new PKParser.Arg.PKArgParser();

            //string DefaultWorkingDir = Directory.GetCurrentDirectory();

            //List<UsedItem> LibList = new List<UsedItem>();
            //List<UsedItem> SourceList = new List<UsedItem>();

            Link Link = new Link("Link", null);

            //Собираем дерево проекта

            foreach (LogQueueItem item in pars.messages.Values)
            {
                if (item.Message.ToLower().Contains(ARMLINKPath))
                {
                    Link.ConsoleString = item.Message;
                    ParamCollection par = ArgParser.Add(item.Message);
                    if (par == null) throw new Exception(KeilWizardStrings);
                    Link.ParamCollection = par;

                    ProjectCollection LocalProjColl = new ProjectCollection();
                    Project MSBProj = LocalProjColl.LoadProject(item.ProjectBuildContext[item.ProjectBuildContext.Count - 1]);
                    string LIB_EXT = MSBProj.GetPropertyValue("LIB_EXT");
                    string OBJ_EXT = MSBProj.GetPropertyValue("OBJ_EXT");

                    foreach (ArgCollection arg in par.Values)
                    {
                        if (arg.Name == "--output")
                        {
                            Link.Name = arg[0];
                        }
                    }

                    foreach (string file in par.Files.Values)
                    {
                        RequiredLibrary lib = null;
                        RequiredObject obj = null;
                        if (Path.GetExtension(file).ToLower() == "." + LIB_EXT.ToLower())
                        {
                            lib =new RequiredLibrary(file, Link);
                            Link.InputLibraries.Add(Link.InputLibraries.Count + 1, lib);
                        }

                        if (Path.GetExtension(file).ToLower() == "." + OBJ_EXT.ToLower())
                        {
                            obj = new RequiredObject(file, Link);
                            Link.InputObjects.Add(Link.InputObjects.Count + 1, obj);
                        }
                        if (lib != null) Link.AllInputs.Add(Link.AllInputs.Count + 1, lib);
                        if (obj != null) Link.AllInputs.Add(Link.AllInputs.Count + 1, obj);
                    }
                }
            }
            ArgParser.Clear();
            //Идем по требуемым библиотекам
            this.AnalizeInputLibraries(Link.InputLibraries, pars.messages.Values);

            //Проверка Crypto.Lib
            this.CheckCryptoLib(Link.InputLibraries, pars.messages.Values);

            //Идем по всем требуемым файлам
            SortedList<int, RequiredObject> ReqObjs = Link.GetAllRequiredObjects();
            this.AnalizeInputObjects(ReqObjs, pars.messages.Values);

            if (!Link.CheckItems())
            {
                throw new Exception("Not all Link Items founded");
            }


            //Собираем необходимые для проекта файлы
            List<RequiredLibrary> ReqLib = Link.GetAllSubRequiredLibraries();

            foreach (RequiredObject obj in ReqObjs.Values)
            {
                this.AddObjFile(obj);
            }
            foreach (RequiredLibrary lib in ReqLib)
            {
                uVisionProject.AddFile(lib.OutputFileuVision, Path.GetFileNameWithoutExtension(lib.Parent.Name));
            }

            //Сортируем группы
            uVisionProject.SortGroups();

            foreach (RequiredLibrary lib in Link.InputLibraries.Values)
            {
                uVisionProject.AddFile(lib.OutputFileuVision,Path.GetFileNameWithoutExtension(Link.Name));
            }

            //Создаем таргеты
            foreach (RequiredLibrary lib in Link.InputLibraries.Values)
            {
                if (lib.Name.ToLower().Contains("crypto"))
                {
                }
                this.CreateLibraryTartet(lib);
            }

            //foreach (RequiredObject obj in Link.InputObjects.Values)
            //{
            //    this.CreateObjectTartet(obj);
            //}

            this.CreateLinkTarget(Link, pars.messages.Values);

            //this.CreateBrowseTarget(Link);

            OnLogText("Writing files");
            ///ProjectFile
            List<string> resultstrings = uVisionProject.GetAllProjectStrings();

            using (StreamWriter outfile =
                    new StreamWriter(uVisionProjectPath + @"\generated.Uv2"))
            {
                foreach (string s in resultstrings)
                {
                    outfile.WriteLine(s);
                }

            }

            ///MultiProjectFile
            using (StreamWriter outfile =
                    new StreamWriter(uVisionProjectPath + @"\project.mpw"))
            {
                outfile.WriteLine("[WORKSPACE]\r");
                outfile.WriteLine(" MPW_NAME=\"WorkSpace\"\r");
                outfile.WriteLine(" Project=\".\\generated.Uv2\",A,X,B\r\r");
            }

            ///BatFiles
            //try
            //{
                if (Directory.Exists(uVisionProjectPath + "\\BatFiles\\Prebuild\\"))
                    Directory.Delete(uVisionProjectPath + "\\BatFiles\\Prebuild\\", true);

                if (Directory.Exists(uVisionProjectPath + "\\BatFiles\\Postbuild\\"))
                    Directory.Delete(uVisionProjectPath + "\\BatFiles\\Postbuild\\", true);

                Directory.CreateDirectory(uVisionProjectPath + "\\BatFiles\\Prebuild\\");

                Directory.CreateDirectory(uVisionProjectPath + "\\BatFiles\\Postbuild\\");
            //}
           // catch
            //{
            //}



            foreach (Target t in uVisionProject.TargetList.Values)
            {
                //PreBuild
                if (t.BeforeBuild.Count > 0)
                {
                    using (StreamWriter outfile =
                        new StreamWriter(uVisionProjectPath + "\\BatFiles\\Prebuild" + @"\" + t.Name + ".bat"))
                    {
                        foreach (string s in t.BeforeBuild)
                        {
                            outfile.WriteLine(s);
                        }

                    }
                }

                if (t.AfterBuild.Count > 0)
                {
                    using (StreamWriter outfile =
                        new StreamWriter(uVisionProjectPath + "\\BatFiles\\Postbuild" + @"\" + t.Name + ".bat"))
                    {
                        foreach (string s in t.AfterBuild)
                        {
                            outfile.WriteLine(s);
                        }

                    }
                }
            }

            OnLogText("Done");

            e.Result = resultstrings;
        }
        private void AnalizeInputObjects(SortedList<int, RequiredObject> Objects, IList<LogQueueItem> LogMessages)
        {
            PKParser.Arg.PKArgParser ArgParser = new PKParser.Arg.PKArgParser();

            foreach (RequiredObject obj in Objects.Values)
            {
                foreach (LogQueueItem item in LogMessages)
                {
                    if (item.Message.ToLower().Contains(ARMCCPath) || item.Message.ToLower().Contains(ARMASMPath))
                    {
                        ParamCollection par = ArgParser.Add(item.Message);
                        if (par == null) continue;

                        bool Found = false;

                        foreach (ArgCollection arg in par.Values)
                        {
                            if (arg.Name == "-o")
                            {
                                if (String.Compare(arg[0], obj.OutputFileMSBuild, StringComparison.InvariantCultureIgnoreCase) == 0) Found = true;
                            }
                        }

                        if (!Found) continue;

                        obj.ConsoleString = item.Message;
                        obj.CurrentDir = item.CurrentDir;
                        obj.ParamCollection = par;
                        obj.BuildList.AddRange(item.ProjectBuildContext);
                        obj.OutputFileuVision = uVisionProjectPath + "\\" + uVisionTargetsPath + "\\" + Path.GetFileNameWithoutExtension(obj.Name) + "\\" + Path.GetFileNameWithoutExtension(obj.Name) + ".o";

                        obj.InputFiles.AddRange(par.Files.Values);
                    }
                }
            }
        }
        public void Convert(string ProjectPath)
        {
            using (ProjectCollection LocalProjColl = new ProjectCollection())
            {
                Project MSBProj = LocalProjColl.LoadProject(PK.Wrapper.ExpandEnvVars(ProjectPath, ""));

                ARMCCPath = MSBProj.GetPropertyValue("CC").ToLower();
                ARMASMPath = MSBProj.GetPropertyValue("AS").ToLower();
                ARMLINKPath = MSBProj.GetPropertyValue("LINK").ToLower();
                ARMARPath = MSBProj.GetPropertyValue("AR").ToLower();
                ARMFROMELFPath = MSBProj.GetPropertyValue("FromELF").ToLower();
            }

            PKParser.Arg.PKArgParser ArgParser = new PKParser.Arg.PKArgParser();

            int index = 0;

            Link Link = new Link("Link", null);

            Link.LogEvent += new MessagesParsesHelper.Link.LogEventDelegate(Link_LogEvent);

            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_PROJECT_TREE, GeneratingLogMessage.WORK_STATE.START));

            foreach (LogQueueItem item in LogQueue.Values)
            {
                index++;
                OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_ANALYSE_LINKS, GeneratingLogMessage.WORK_TYPE.CREATE_PROJECT_TREE, GeneratingLogMessage.WORK_STATE.WORKING, (int)((index * 100) / LogQueue.Values.Count)));

                string s = ArgParser.GetUtil(item.Message);
                if (s == null) continue;
                if (String.Compare(ARMLINKPath, s, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //ArmLink
                    ARMLink.Add(ARMLink.Count + 1, new ParCollContainer(ArgParser.Add(item.Message), item.Message, item.ProjectBuildContext, item.CurrentDir));
                }
                else if (String.Compare(ARMCCPath, s, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //ArmCC 
                    ARMCCandARMASM.Add(ARMCCandARMASM.Count + 1, new ParCollContainer(ArgParser.Add(item.Message), item.Message, item.ProjectBuildContext, item.CurrentDir));
                }
                else if (String.Compare(ARMASMPath, s, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //ArmAsm
                    ARMCCandARMASM.Add(ARMCCandARMASM.Count + 1, new ParCollContainer(ArgParser.Add(item.Message), item.Message, item.ProjectBuildContext, item.CurrentDir));
                }
                else if (String.Compare(ARMARPath, s, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //ArmAr
                    ARMAR.Add(ARMAR.Count + 1, new ParCollContainer(ArgParser.Add(item.Message), item.Message, item.ProjectBuildContext, item.CurrentDir));
                }
                else if (String.Compare(ARMFROMELFPath, s, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //FROMElf
                    FROMELF.Add(FROMELF.Count + 1, new ParCollContainer(ArgParser.Add(item.Message), item.Message, item.ProjectBuildContext, item.CurrentDir));
                }
            }

            ArgParser.Clear();

            //Construct project tree

            ParCollContainer container = ARMLink.Values[ARMLink.Values.Count - 1];
            if (container.ParamCollection == null) throw new MessagesParserException(KeilWizardStrings.IDS_ERROR_CANT_PASRSE_LINK);
            Link.ParamCollection = container.ParamCollection;

            string LIB_EXT = string.Empty;
            string OBJ_EXT = string.Empty;

            using (ProjectCollection LocalProjColl = new ProjectCollection())
            {
                Project MSBProj = LocalProjColl.LoadProject(container.ProjectBuildContext[container.ProjectBuildContext.Count - 1]);

                LIB_EXT = MSBProj.GetPropertyValue("LIB_EXT");
                OBJ_EXT = MSBProj.GetPropertyValue("OBJ_EXT");
            }

            foreach (ArgCollection arg in container.ParamCollection.Values)
            {
                if (arg.Name == "--output")
                {
                    Link.Name = arg[0];
                }
            }

            foreach (string file in container.ParamCollection.Files.Values)
            {
                RequiredLibrary lib = null;
                RequiredObject obj = null;

                if (Path.GetExtension(file).ToLower() == "." + LIB_EXT.ToLower())
                {
                    lib = new RequiredLibrary(file, Link);
                    Link.InputLibraries.Add(Link.InputLibraries.Count + 1, lib);
                }

                if (Path.GetExtension(file).ToLower() == "." + OBJ_EXT.ToLower())
                {
                    obj = new RequiredObject(file, Link);
                    Link.InputObjects.Add(Link.InputObjects.Count + 1, obj);
                }
                if (lib != null) Link.AllInputs.Add(Link.AllInputs.Count + 1, lib);
                if (obj != null) Link.AllInputs.Add(Link.AllInputs.Count + 1, obj);
            }
            OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_ANALYSE_LINKS, GeneratingLogMessage.WORK_TYPE.CREATE_PROJECT_TREE, GeneratingLogMessage.WORK_STATE.WORKING, 100));
            System.Threading.Thread.Sleep(sleeptime);



            //using (StreamWriter outfile =
            //            new StreamWriter(Path.GetFileNameWithoutExtension(Link.Name) + ".input"))
            //{
            //    outfile.WriteLine(PK.Wrapper.Solution.ProjectPath);
            //    outfile.WriteLine(uVisionProjectPath);
            //    outfile.WriteLine(ProjectPath);                

            //    foreach (ArgCollection arg in Link.ParamCollection.Values)
            //    {
            //        if (arg.Name == "--scatter")
            //            outfile.WriteLine(arg[0]);
            //    }
            //    outfile.WriteLine(LogQueue.Values.Count);
            //    foreach (LogQueueItem item in LogQueue.Values)
            //    {
            //        outfile.WriteLine(item.Message);
            //        outfile.WriteLine(item.CurrentDir);
            //        outfile.WriteLine(item.ProjectBuildContext.Count);
            //        foreach (string s in item.ProjectBuildContext)
            //        {
            //            outfile.WriteLine(s);
            //        }
            //    }
            //}



            ArgParser.Clear();
            //Analysing libraries
            this.AnalizeInputLibraries(Link.InputLibraries, LogQueue.Values);

            //Checking Crypto.Lib
            this.CheckCryptoLib(Link.InputLibraries, LogQueue.Values);

            //Checking Sockets Libs
            this.CheckSocketsLibs(Link.InputLibraries, LogQueue.Values);            

            //Analysing Files
            SortedList<int, RequiredObject> ReqObjs = Link.GetAllRequiredObjects();
            this.AnalizeInputObjects(ReqObjs);//, LogQueue.Values);

            if (!Link.CheckItems())
            {
                throw new MessagesParserException(KeilWizardStrings.IDS_ERROR_FAIL_LINK_ANALYSE);
            }

            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_PROJECT_TREE, GeneratingLogMessage.WORK_STATE.DONE));
            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_FILE_LIST, GeneratingLogMessage.WORK_STATE.START));
            index = 0;
            //Collects required files
            List<RequiredLibrary> ReqLib = Link.GetAllSubRequiredLibraries();

            foreach (RequiredObject obj in ReqObjs.Values)
            {
                index++;
                OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_CREATING_FILE_LIST, GeneratingLogMessage.WORK_TYPE.CREATE_FILE_LIST, GeneratingLogMessage.WORK_STATE.WORKING, (int)((index * 100) / (ReqObjs.Values.Count + ReqLib.Count))));
                this.AddObjFile(obj);
            }
            foreach (RequiredLibrary lib in ReqLib)
            {
                index++;
                OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_CREATING_FILE_LIST, GeneratingLogMessage.WORK_TYPE.CREATE_FILE_LIST, GeneratingLogMessage.WORK_STATE.WORKING, (int)((index * 100) / (ReqObjs.Values.Count + ReqLib.Count))));
                uVisionProject.AddFile(lib.OutputFileuVision, Path.GetFileNameWithoutExtension(lib.Parent.Name));
            }

            //Sorting groups
            uVisionProject.SortGroups();

            foreach (RequiredLibrary lib in Link.InputLibraries.Values)
            {
                uVisionProject.AddFile(lib.OutputFileuVision, Path.GetFileNameWithoutExtension(Link.Name));
            }
            OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_CREATING_FILE_LIST, GeneratingLogMessage.WORK_TYPE.CREATE_FILE_LIST, GeneratingLogMessage.WORK_STATE.WORKING, 100));
            System.Threading.Thread.Sleep(sleeptime);
            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_FILE_LIST, GeneratingLogMessage.WORK_STATE.DONE));
            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_TARGETS, GeneratingLogMessage.WORK_STATE.START));
            //Create targets
            index = 0;
            foreach (RequiredLibrary lib in Link.InputLibraries.Values)
            {
                index++;
                OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_CREATING_TARGETS, GeneratingLogMessage.WORK_TYPE.CREATE_TARGETS, GeneratingLogMessage.WORK_STATE.WORKING, (int)((index * 100) / (Link.InputLibraries.Count))));

                this.CreateLibraryTartet(lib);
            }

            this.CreateLinkTarget(Link);//, LogQueue.Values);

            OnLog(new GeneratingLogMessage(KeilWizardStrings.IDS_CREATING_TARGETS, GeneratingLogMessage.WORK_TYPE.CREATE_TARGETS, GeneratingLogMessage.WORK_STATE.WORKING, 100));
            System.Threading.Thread.Sleep(sleeptime);
            OnLog(new GeneratingLogMessage("", GeneratingLogMessage.WORK_TYPE.CREATE_TARGETS, GeneratingLogMessage.WORK_STATE.DONE));

            //using (StreamWriter outfile = new StreamWriter(Path.GetFileNameWithoutExtension(Link.Name) + ".result"))
            //{
            //    List<string> strs = uVisionProject.GetAllProjectStrings();
            //    foreach (string s in strs)
            //    {
            //        outfile.WriteLine(s);
            //    }
            //}
        }