Пример #1
0
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
        //textview2.Visible=false;
        //endMark = textview2.Buffer.CreateMark("end-mark", textview2.Buffer.EndIter, false);

        string settingFile = System.IO.Path.Combine(MainClass.Paths.SettingDir, "moscrif.mss");

        if (!File.Exists(settingFile))
        {
            SetLabel("Setting file not found!");
            btnQuit.Visible = true;
            return;
        }
        try{
            Console.WriteLine(settingFile);
            XmlDocument xml = new XmlDocument();
            //xml.LoadXml(settingFile);
            StreamReader reader = new StreamReader(settingFile);

            xml.LoadXml(reader.ReadToEnd());

            XmlNodeList xnList = xml.SelectNodes("/Settings");

            filePublishPath  = xnList[0].Attributes["publishDir"].Value;
            fileLibsPath     = xnList[0].Attributes["frameworkDir"].Value;
            fileEmulatorPath = xnList[0].Attributes["emulatorDir"].Value;
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            SetLabel("Setting file is corrupted");
            btnQuit.Visible = true;
            return;
        }

        //timer.Interval = 250;
        //timer.Elapsed += new ElapsedEventHandler(OnTimeElapsed);
        ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;


        /*for (int i = 0; i < args.Length; i++){
         *      WriteMesage(args[i]);
         * }*/

        /*if (!MainClass.Auto){
         *      messageLabel.LabelProp = "ERROR.";
         *      WriteMesage("Incorect parameter.");
         *      return;
         * }*/

        //tag = new TextTag("0");
        if (MainClass.Platform.IsWindows)
        {
            WriteMesage("Process -->");
            Process [] pArry = Process.GetProcesses();
            foreach (Process p in pArry)
            {
                string s = p.ProcessName;
                s = s.ToLower();

                WriteMesage("\t" + s);
                if (s.CompareTo("moscrif-ide") == 0)
                {
                    try{
                        p.Kill();
                    } catch {
                        SetLabel("Moscrif IDE is already running!");
                        btnQuit.Visible = true;
                        return;
                    }
                }
            }
        }

        string moscriftIdePath = System.IO.Path.Combine(MainClass.Paths.AppPath, "moscrif-ide.exe");

        if (!System.IO.File.Exists(moscriftIdePath))
        {
            SetLabel("Moscrif IDE not found!");
            btnQuit.Visible = true;
            return;
        }
        string version = "";

        FileVersionInfo moFI = FileVersionInfo.GetVersionInfo(moscriftIdePath);

        VersionChecker vers = new VersionChecker();

        if (moFI == null)
        {
            WriteMesage("ERROR: Unknown version.");
            btnQuit.Visible = true;
            return;
        }
        else
        {
            version = moFI.FileVersion;
            string[] versions = version.Split('.');
            if (versions.Length != 4)
            {
                WriteMesage("ERROR: Invalid Version.");
                btnQuit.Visible = true;
                return;
            }
            string webVersion = vers.VersionConverter(version);

            WriteMesage("Old Version " + webVersion);
            //return;
        }
        string newVersion;
        string message;

        try{
            if (!String.IsNullOrEmpty(MainClass.Token))
            {
                message = vers.CheckVersionSynch(version, MainClass.Token, out newVersion, false, test);
            }
            else
            {
                message = vers.CheckVersionSynch(version, "", out newVersion, false, test);
            }
        }catch (Exception ex) {
            SetLabel(ex.Message);
            WriteMesage(ex.Message);
            btnQuit.Visible = true;
            return;
        }
        bool nextVersion = true;

        while (nextVersion)
        {
            nextVersion = false;
            newVersion  = System.IO.Path.GetFileNameWithoutExtension(newVersion);

            if (String.IsNullOrEmpty(newVersion))
            {
                SetLabel("Your version is up-to-date.");
                WriteMesage(message);
                btnQuit.Visible = true;
                return;
            }

            WriteMesage(message);
            WriteMesage("New Version " + newVersion);
            //pd = new ProgressDialog("Download",ProgressDialog.CancelButtonType.None,1,null);//MainClass.MainWindow
            Reset(1, "Downloading version : " + newVersion);
            string fullpath = System.IO.Path.Combine(MainClass.Paths.TempDir, newVersion + ".zip");         //Temp
            //timer.Start();
            //		data = null;
            //Stream str ;
            isFinish = false;
            isError  = false;
            try{
                if (!String.IsNullOrEmpty(MainClass.Token))
                {
                    //vers.GetVersionSynch(newVersion,MainClass.Token,out str);
                    GetVersionAsynch(newVersion, MainClass.Token, fullpath, test);
                    //	GetVersionHandler
                }
                else
                {
                    GetVersionAsynch(newVersion, "", fullpath, test);
                }

                int indx = 0;
                while (!isFinish)                //(client.IsBusy)
                {
                    //progressBar.Text=indx.ToString();
                    AutomaticUpdate();
                    indx++;
                    while (Application.EventsPending())
                    {
                        Application.RunIteration();
                    }
                    Thread.Sleep(150);
                }
            } catch (Exception ex) {
                SetLabel(ex.Message);
                WriteMesage(ex.Message);
                //timer.Enabled = false;
                //timer.Stop();
                btnQuit.Visible = true;
                return;
            }
            if ((!File.Exists(fullpath)) || (isError))
            {
                WriteMesage("Error downloading file.");
                SetLabel("Error downloading file!");
                //timer.Enabled = false;
                //timer.Stop();
                btnQuit.Visible = true;
                //if(pd != null) pd.Destroy();
                return;
            }

            /*if (data!= null){	//str
             *      FileStream writeStream = new FileStream(fullpath, FileMode.Create, FileAccess.Write);
             *      Copy(data,writeStream);
             *      writeStream.Close();
             *      writeStream.Dispose();
             * } else {
             *      WriteMesage("Error downloading file.");
             *      SetLabel("Error downloading file!");
             *      timer.Enabled = false;
             *      timer.Stop();
             *      btnQuit.Visible = true;
             *      //if(pd != null) pd.Destroy();
             *      return;
             * }*/
            //timer.Enabled = false;
            //timer.Stop();
            //if(pd != null) pd.Destroy();
            Reset(1, "Download Finish");

            string outputPath = System.IO.Path.Combine(MainClass.Paths.Temp, newVersion);

            if (!Directory.Exists(outputPath))
            {
                try{
                    Directory.CreateDirectory(outputPath);
                }catch {
                    SetLabel("Error creating output directory!");
                    WriteMesage("ERROR: FILE CANNOT BY CREATE.");
                    btnQuit.Visible = true;
                    return;
                }
            }

            List <string> listFile = new List <string>();
            try{
                listFile = UnzipFile(fullpath, outputPath);
            } catch {
                SetLabel("Error unpacking new version!");
                WriteMesage("ERROR:FILE CANNOT BY UNPACK.");
                btnQuit.Visible = true;
                return;
            }
            Console.WriteLine("btnQuit.Visible 4 -> {0}", btnQuit.Visible);
            if ((listFile == null) || (listFile.Count < 1))
            {
                SetLabel("Error unpacking new version!");
                WriteMesage("ERROR:FILE CANNOT BY UNPACK.");
                btnQuit.Visible = true;
                return;
            }
            Console.WriteLine("btnQuit.Visible 5 -> {0}", btnQuit.Visible);
            foreach (string file in listFile)
            {
                string fileName       = System.IO.Path.GetFileName(file).ToLower();
                string fileNameWE     = System.IO.Path.GetFileNameWithoutExtension(file).ToLower();
                string fileOutputPath = "";

                switch (fileName)
                {
                case "emu.win.zip":
                    if (MainClass.Platform.IsWindows)
                    {
                        fileOutputPath = fileEmulatorPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "emu.mac.zip":
                    if (MainClass.Platform.IsMac)
                    {
                        fileOutputPath = fileEmulatorPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "emu.lin.zip":
                    if (MainClass.Platform.IsX11)
                    {
                        fileOutputPath = fileEmulatorPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "ide.zip":
                    fileOutputPath = MainClass.Paths.AppPath;
                    break;

                case "lib.zip":
                    fileOutputPath = fileLibsPath;
                    break;

                /*case "pub.zip":
                 *      fileOutputPath = filePublishPath;
                 *      break;	*/
                case "pub.win.zip":
                    if (MainClass.Platform.IsWindows)
                    {
                        fileOutputPath = filePublishPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "pub.mac.zip":
                    if (MainClass.Platform.IsMac)
                    {
                        fileOutputPath = filePublishPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "pub.lin.zip":
                    if (MainClass.Platform.IsX11)
                    {
                        fileOutputPath = filePublishPath;
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case "sam.zip":
                    fileOutputPath = MainClass.Paths.SampleDir;
                    break;

                default:
                    fileOutputPath = System.IO.Path.Combine(MainClass.Paths.AppPath, fileNameWE);
                    break;
                }
                WriteMesage("fileName :" + file);
                try{
                    UnzipFile(file, fileOutputPath);
                    if (cancelUnpack)
                    {
                        WriteMesage("SKIP ");
                        goto nav;
                    }
                }catch (Exception ex) {
                    WriteMesage("Error unpacking file: " + ex.Message);
                    SetLabel("Error unpacking file '" + file + "'!");
                }
            }


            string fixVersion = "";
            if (newVersion.Length == 7)
            {
                fixVersion = newVersion[6].ToString();
            }

            string versionPath = System.IO.Path.Combine(MainClass.Paths.AppPath, "version.txt");

            using (StreamWriter fileVersion = new StreamWriter(versionPath)) {
                fileVersion.Write(fixVersion);
                fileVersion.Close();
                fileVersion.Dispose();
            }

            nav :;
            try{
                WriteMesage("Delete temp file.");
                SetLabel("Deleting temporary files.");
                System.IO.File.Delete(fullpath);
                System.IO.Directory.Delete(outputPath, true);
            }catch {
            }

            try{
                version = newVersion;
                Console.WriteLine("newVersion -> {0}", newVersion);
                Console.WriteLine("version -> {0}", version);
                if (!String.IsNullOrEmpty(MainClass.Token))
                {
                    message = vers.CheckVersionSynch(version, MainClass.Token, out newVersion, true, test);
                }
                else
                {
                    message = vers.CheckVersionSynch(version, "", out newVersion, true, test);
                }

                if (String.IsNullOrEmpty(newVersion))
                {
                    nextVersion = false;
                }
                else
                {
                    nextVersion = true;
                }
            }catch {           //(Exception ex){
                //SetLabel(ex.Message);
                //WriteMesage(ex.Message);
                //btnQuit.Visible = true;
                //return;
                nextVersion = false;
            }
        }

        btnQuit.Visible = true;
        SetLabel("Update is completed.");
    }