コード例 #1
0
ファイル: Form1.cs プロジェクト: cageo/Zhang-2012
        private void button3_Click(object sender, EventArgs e)
        {
            //form2.Location = new Point(this.Location.X+100,this.Location.Y+150);
            // zhj.修改输入路径判空条件
            if (inPath.Length == 0)
            {
                MessageBox.Show("Please select a DEM file");
                return;
            }
            /*if (inPath == null)
            {
                MessageBox.Show("Please select a DEM file");
                return;
            }*/

            if (outPath == null)
            {
                MessageBox.Show("Output path can not be empty!");
                return;
            }

            if (radioButton_nodata_repair_no.Checked)
            {
                xiufu = false;//修复无值栅格
            }

            //if (radioButton1.Checked)
            //{
            //    guodu = true;
            //}//2010.01.05修改,以前没有else语句,存在问题
            //else
            //{
            //    guodu = false;
            //}
            if (radioButton_fill_min.Checked)
            {
                fillWay = false;
            }
            //2010.6.8改,无须选择单位了
            //if (radioButton_mul_dir.Checked)
            //{
            //    unit = false;
            //}
            //if (radioButton7.Checked)
            //{
            //    unit = true;
            //}
            if (radioButton_channel_no.Checked)
            {
                //CumulatWay = false;
                Channel_Consider = false;
                threshold = 0;
            }
            else
            {
                //CumulatWay = true;
                Channel_Consider = true;
                threshold = Convert.ToSingle(textBox_threshold.Text);//2011.04.20目前只有D8和MS多流向的方法中涉及到了河网的问题
            }
            if (radioButton_cutoff_no.Checked)
            {
                flowcut = false;
            }
            else
            {
                flowcut = true;
            }
            if (radioButton_sinkfill_no.Checked)
            {
                fillsink = false;
            }
            else
            {
                fillsink = true;
            }
            if (radioButton_Cumulated_Max.Checked)
            {
                CumulatWay = true;
            }
            else
            {
                CumulatWay = false;
            }
            if (radioButton_model_CSLE.Checked)
            {
                RUSLE_CSLE = 1;//CSLE
            }
            else
            {
                RUSLE_CSLE = 0;//RUSLE
            }
            button_start.Enabled = false;
            qianzhui = textBox_profix.Text;
            xiaoyu5 = float.Parse(textBox_cutoff_slopeless.Text);
            dayu5 = float.Parse(textBox_cutoff_slopegreat.Text);

            form2 = new Form2();
            form2.progressTextBox.Text = "OK";
            // zhj.开始计算
            for (int fileCount = 0; fileCount < inPath.Length; ++fileCount)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();

                string form2Title = "正在计算 Calculating:  "
                    + (fileCount) + "/" + inPath.Length + " missions are completed";
                form2.Text = form2Title;
                //form2.progressTextBox
                form2.progressBar.Value = 0;
                form2.progressBar.Maximum = 28;
                form2.progressTextBox.Update();
                form2.Owner = this;
                //form2.StartPosition = this.StartPosition;
                form2.StartPosition = FormStartPosition.CenterScreen;
                form2.Show();
                //2010.06.06添加单流向及多流向的选择
                //string tempOutPath = outPath;
                //string tempQianZhui = "";
                string tempOutPath = outPath;
                string tempQianZhui = outDir[fileCount];
                try
                {
                    //tempOutPath += ("\\" + outDir[fileCount]);
                    //tempOutPath += DateTime.Now.ToString(" yyyy_MM_dd HH_mm_ss");
                    //Directory.CreateDirectory(tempOutPath);
                    if (radioButton_flowdir_mul.Checked)
                    {
                        if (radioButton_Mul_MS.Checked)
                        {
                            LS_MultDir_MSClass lsClass = new LS_MultDir_MSClass();
                            //lsClass.LSBegin(inPath, outPath, qianzhui, xiufu, xiaoyu5, dayu5, unit, fillWay, flowcut, fillsink, threshold, ExportAll, MyCheckBox, this);
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, flowcut, fillsink, threshold, CumulatWay, RUSLE_CSLE, Channel_Consider, ExportAll, MyCheckBox, this);
                            lsClass = null;
                        }
                        else if (radioButton_Mul_FMFD.Checked)
                        {
                            LS_MultDir_FMFDClass lsClass = new LS_MultDir_FMFDClass();
                            // 显示一个窗口
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, flowcut, fillsink, ExportAll, MyCheckBox, float.Parse(textBox_slope_exp.Text), this);
                            lsClass = null;
                        }
                        else if (radioButton_Mul_DEMON.Checked)
                        {
                            LS_MultDir_DEMONClass lsClass = new LS_MultDir_DEMONClass();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, CumulatWay, ExportAll, MyCheckBox, this);
                            lsClass = null;

                        }
                        else
                        {
                            LS_MultDir_PilesjClass lsClass = new LS_MultDir_PilesjClass();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, CumulatWay, ExportAll, MyCheckBox, this);
                            lsClass = null;
                        }

                    }
                    else
                    {
                        if (radioButton_Sgn_D8.Checked)
                        {
                            LS_SingleDir_D8Class lsClass = new LS_SingleDir_D8Class();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, flowcut, fillsink, threshold, CumulatWay, RUSLE_CSLE, Channel_Consider, ExportAll, MyCheckBox, this);
                            //导入路径、输出路径、前缀、是否修复、小于5%、大于5%、单位、填充方式、是否截断、是否进行洼地填充、阈值、是否考虑河网、哪一个模型、是否所有都输出、输出的复选框选择
                            lsClass = null;
                        }
                        else if (radioButton_Sgn_Rho4.Checked)
                        {
                            LS_SingleDir_Rho4Class lsClass = new LS_SingleDir_Rho4Class();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, CumulatWay, ExportAll, MyCheckBox, this);
                            lsClass = null;
                        }
                        else if (radioButton_Sgn_Rho8.Checked)
                        {
                            LS_SingleDir_Rho8Class lsClass = new LS_SingleDir_Rho8Class();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, CumulatWay, ExportAll, MyCheckBox, this);
                            lsClass = null;

                        }
                        else if (radioButton_Sgn_Rho.Checked)
                        {
                            LS_SingleDir_RhoClass lsClass = new LS_SingleDir_RhoClass();
                            lsClass.LSBegin(inPath[fileCount], tempOutPath, tempQianZhui, xiufu, xiaoyu5, dayu5, unit, fillWay, CumulatWay, ExportAll, MyCheckBox, this);
                            lsClass = null;

                        }//以上是单流向,以下是多流向

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("物理内存空间不足,无法完成计算!" + ex.ToString(), "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    form2.Close();
                    form2.Dispose();
                }
            }
            form2.Text = "全部文件计算完成(All missions are completed)";
            form2.button_form2_ok.Visible = true;
            //form2.Dispose();
            button_start.Enabled = false;
            textBox_outputpath.Text = "";
            textBox_inputpath.Text = "";
            // 清空路径数组
            inPath = null;
            outPath = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
コード例 #2
0
ファイル: PCCObject.cs プロジェクト: Dybuk/ME3Explorer
        /// <summary>
        /// This method is an alternate way of saving PCCs
        /// Instead of reconstructing the PCC from the data taken, it instead copies across the existing
        /// data, appends new exports, updates the export list, changes the namelist location and updates the
        /// value in the header
        /// </summary>
        /// <param name="newFileName">The filename to write to</param>
        /// <param name="attemptOverwrite">Do you wish to attempt to overwrite the existing export</param>
        public string altSaveToFile(string newFileName, bool attemptOverwrite, int HeadeNameOffset = 34)
        {
            string rtValues = "";
            string loc = Path.GetDirectoryName(Application.ExecutablePath);

            //Check whether compressed
            if (this.bCompressed)
            {
                Form2 decompress = new Form2();
                decompress.Decompress(this.pccFileName);
                decompress.Close();
                //MessageBox.Show("Decompression complete");
            }

            //Get info
            expInfoEndOffset = ExportOffset + Exports.Sum(export => export.info.Length);
            if (expDataBegOffset < expInfoEndOffset)
                expDataBegOffset = expInfoEndOffset;
            //List<ExportEntry> unchangedExports = Exports.Where(export => !export.hasChanged || (export.hasChanged && export.Data.Length <= export.DataSize)).ToList();
            List<ExportEntry> unchangedExports = Exports.Where(export => !export.hasChanged).ToList();
            List<ExportEntry> changedExports;
            List<ExportEntry> replaceExports = null;
            if (!attemptOverwrite)
            {
                //If not trying to overwrite, then select all exports that have been changed
                changedExports = Exports.Where(export => export.hasChanged).ToList();
                //MessageBox.Show("No changed exports = " + changedExports.Count);
                //throw new NullReferenceException();
            }
            else
            {
                //If we are trying to overwrite, then split up the exports that have been changed that can and can't overwrite the originals
                changedExports = Exports.Where(export => export.hasChanged && export.Data.Length > export.DataSize).ToList();
                replaceExports = Exports.Where(export => export.hasChanged && export.Data.Length <= export.DataSize).ToList();
            }
            //ExportEntry lastExport = unchangedExports.Find(export => export.DataOffset == unchangedExports.Max(maxExport => maxExport.DataOffset));
            ExportEntry lastExport = Exports.Find(export => export.DataOffset == Exports.Max(maxExport => maxExport.DataOffset));
            int lastDataOffset = lastExport.DataOffset + lastExport.DataSize;
            byte[] oldPCC = new byte[lastDataOffset];
            //byte[] oldName;

            if (!attemptOverwrite)
            {
                int offset = ExportOffset;
                foreach (ExportEntry export in Exports)
                {
                    if (!export.hasChanged)
                    {
                        offset += export.info.Length;
                    }
                    else
                        break;
                }
                rtValues += offset.ToString() + " ";
                using (FileStream stream = new FileStream(loc + "\\exec\\infoCache.bin", FileMode.Append))
                {
                    stream.Seek(0, SeekOrigin.End);
                    rtValues += stream.Position + " ";
                    //throw new FileNotFoundException();
                    stream.Write(changedExports[0].info, 32, 8);
                }
            }

            using (FileStream oldPccStream = new FileStream(this.pccFileName, FileMode.Open))
            {
                //Read the original data up to the last export
                oldPccStream.Read(oldPCC, 0, lastDataOffset);
                #region Unused code
                /* Maybe implement this if I want to directly copy the names across.
                 * Not useful at this time
                if (NameOffset == 0x8E)
                {
                    oldName = new byte[ImportOffset - 0x8E];
                    oldPccStream.Seek(0x8E, SeekOrigin.Begin);
                    oldPccStream.Read(oldName, 0, (int)oldPccStream.Length - lastDataOffset);
                }
                else
                {
                    oldName = new byte[oldPccStream.Length - lastDataOffset];
                    oldPccStream.Seek(lastDataOffset, SeekOrigin.Begin);
                    oldPccStream.Read(oldName, 0, (int)oldPccStream.Length - lastDataOffset);
                }
                 * */
                #endregion
            }
            //Start writing the new file
            using (FileStream newPCCStream = new FileStream(newFileName, FileMode.Create))
            {
                newPCCStream.Seek(0, SeekOrigin.Begin);
                //Write the original file up til the last original export (note that this leaves in all the original exports)
                newPCCStream.Write(oldPCC, 0, lastDataOffset);
                if (!attemptOverwrite)
                {
                    //If we're not trying to overwrite then just append all the changed exports
                    foreach (ExportEntry export in changedExports)
                    {
                        export.DataOffset = (int)newPCCStream.Position;
                        export.DataSize = export.Data.Length;
                        newPCCStream.Write(export.Data, 0, export.Data.Length);
                    }
                }
                else
                {
                    //If we are then move to each offset and overwrite the data with the new exports
                    foreach (ExportEntry export in replaceExports)
                    {
                        //newPCCStream.Position = export.DataOffset;
                        newPCCStream.Seek(export.DataOffset, SeekOrigin.Begin);
                        export.DataSize = export.Data.Length;
                        newPCCStream.Write(export.Data, 0, export.Data.Length);
                    }
                    //Then move to the end and append the new data
                    //newPCCStream.Position = lastDataOffset;
                    newPCCStream.Seek(lastDataOffset, SeekOrigin.Begin);

                    foreach (ExportEntry export in changedExports)
                    {
                        export.DataOffset = (int)newPCCStream.Position;
                        export.DataSize = export.Data.Length;
                        newPCCStream.Write(export.Data, 0, export.Data.Length);
                    }
                }
                //Set the new nameoffset and namecounts
                NameOffset = (int)newPCCStream.Position;
                NameCount = Names.Count;
                //Then write out the namelist
                foreach (string name in Names)
                {
                    newPCCStream.WriteValueS32(-(name.Length + 1));
                    newPCCStream.WriteString(name + "\0", (uint)(name.Length + 1) * 2, Encoding.Unicode);
                }
                //Move to the name info position in the header - not a strong piece of code, but it's working so far
                //newPCCStream.Position = 34;
                newPCCStream.Seek(HeadeNameOffset, SeekOrigin.Begin);
                //And write the new info
                byte[] nameHeader = new byte[8];
                byte[] nameCount = BitConverter.GetBytes(NameCount);
                byte[] nameOff = BitConverter.GetBytes(NameOffset);
                for (int i = 0; i < 4; i++)
                    nameHeader[i] = nameCount[i];
                for (int i = 0; i < 4; i++)
                    nameHeader[i + 4] = nameOff[i];
                newPCCStream.Write(nameHeader, 0, 8);

                //Finally, update the export list
                newPCCStream.Seek(ExportOffset, SeekOrigin.Begin);
                foreach (ExportEntry export in Exports)
                {
                    newPCCStream.Write(export.info, 0, export.info.Length);
                }

                if (!attemptOverwrite)
                {
                    using (FileStream stream = new FileStream(loc + "\\exec\\infoCache.bin", FileMode.Append))
                    {
                        stream.Seek(0, SeekOrigin.End);
                        rtValues += stream.Position + " ";
                        stream.Write(changedExports[0].info, 32, 8);
                    }
                }
            }
            return rtValues;
        }