場景拼圖工具開發

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ResPublish
{
    class Program
    {
        //迭代計數器
        public static int count=0;
        /// <summary>
        /// 函數入口
        /// 程晗0709
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //獲取所有步驟的返回值
            Result result = new Result(true,"");
            //運行程序時,檢測 texture路徑,圖片路徑是否存在
            result = checkAll();
            if (!result.isSuccess)
            {
                Console.WriteLine(result.msg);
                Console.ReadLine();
            }
            //程序調用texture生成圖片

            //遞歸開始
            result = doTexture(Settings.Default.imagePath+@"\temp0");
            Console.WriteLine("操作成功");
            Console.ReadLine();           
        }
        
        /// <summary>
        /// 運行程序時,檢測 texture路徑,圖片路徑是否存在
        /// 程晗0709
        /// </summary>
        /// <returns></returns>
        public static Result checkAll()
        {
            //驗證texture是否存在
            if (!Directory.Exists(Settings.Default.texturePath)) {
                return new Result(false,"texture路徑錯誤");
            };
            //驗證image是否存在
            if (!Directory.Exists(Settings.Default.imagePath)) {
                return new Result(false, "image路徑錯誤");
            }
            //檢測圖片是否超過大小,並存入temp0中
            //創建temp0文件夾
            if (!Directory.Exists(Settings.Default.imagePath + @"\temp0"))
            {
                // Create the directory it does not exist.
                Directory.CreateDirectory(Settings.Default.imagePath + @"\temp0");
            }
            else
            {
                Directory.Delete(Settings.Default.imagePath + @"\temp0", true);
                Directory.CreateDirectory(Settings.Default.imagePath + @"\temp0");
            }
            //將樣本圖片存入文件夾
            //讀取文件夾中所有文件 存入數組中
            string[] dir = Directory.GetFiles(Settings.Default.imagePath);
            //將數組中的文件存入新的文件夾中。
            Image img;
            //驗證是否存在圖片
            bool isImage = false;
            Regex reg = new Regex("jpg|png");
            for (int i = 0; i < dir.Length; i++)
            {
                //判斷是否是圖片
                if (reg.IsMatch(dir[i])) {
                    isImage = true;
                    img = System.Drawing.Image.FromFile(dir[i]);
                    if (img.Width > 2048 || img.Height > 2048) {
                    return new Result(false,dir[i]+"圖片大小超標!");
                     }
                    img.Dispose();
                    //取出文件名
                    string imageName = dir[i].Substring(Settings.Default.imagePath.Length + 1);
                    System.IO.File.Copy(dir[i], Settings.Default.imagePath + @"\temp0\" + imageName);
                    //剪切文件
                    //File.Move(listStr[i], path + @"\temp" + count);
                }
            }
            //驗證是否存在圖片
            if (!isImage)
            {
                return new Result(false, "選中文件夾不存在圖片,無法拼圖");
            }
            return new Result(true, "");
        }

        /// <summary>
        /// 程序調用texture生成圖片
        /// 程晗0710
        /// </summary>
        /// <returns></returns>
        public static Result doTexture(string imagePath)
        {
            Result result= new Result(true,"");
            //新建進程並生成拼圖
            try
            {
                //進程所在文件夾
                ProcessStartInfo startInfo = new ProcessStartInfo(Settings.Default.texturePath + @"TexturePacker.exe");
                //重定向輸出 
                startInfo.RedirectStandardOutput = true;
                //錯誤輸出
                startInfo.RedirectStandardError = true;
                startInfo.UseShellExecute = false;
                //進程不打開窗口
                startInfo.CreateNoWindow = true;
                //打開進程的時候附帶的命令行
                startInfo.Arguments = @"--data e:\" + count + @".plist --format cocos2d --sheet e:\" + count + ".png --size-constraints POT --dither-fs-alpha " + " " + Settings.Default.imagePath + @"\temp" + count;
                //開啓進程,並讀取信息
                result.msg=Process.Start(startInfo).StandardError.ReadToEnd();
                //正則匹配error 並且切片大於1
                Regex reg = new Regex("error");
                if (reg.IsMatch(result.msg))
                {
                    //溢出一次就會迭代一次
                    result.isSuccess = false;
                    //將錯誤信息拆分爲單詞
                    string[] errorWord = result.msg.Split(' ');
                    //提取多出來的圖片
                    List<string> images = new List<string>();
                    Regex regImage=new Regex("jpg|png");
                    for (int i = 0; i < errorWord.Length; i++) {
                        if (regImage.IsMatch(errorWord[i])) {
                            images.Add(errorWord[i]);
                        }
                    }
                        //調用移動文件函數
                        moveImage(images, Settings.Default.imagePath + @"\temp" + count);
                }
            }
            catch (Exception e ) {
                result.isSuccess = false;
                result.msg += e;
            }
            //回調本身
                if (count > 0)
                {
                    result = doTexture(Settings.Default.imagePath + @"\temp" + --count);
                }
            return result;
        }

        /// <summary>
        /// 將數組中的文件剪切到指定文件夾中。
        /// </summary>
        /// <param name="images">多出的文件</param>
        /// <param name="path">指定文件路徑</param>
        /// <returns></returns>
        public static Result moveImage(List<string> images, string path)
        {
            count++;
            //讀取文件夾中所有文件 存入數組中
            string[] dir = Directory.GetFiles(path);
            //正則匹配獲取圖片
            Regex reg=new Regex("jpg|png");
            //循環正則,如果有後綴則存入list中
            List<string> listStr=new List<string>();
            for(int i=0;i<dir.Length;i++){
                if(reg.IsMatch(dir[i])){
                   listStr.Add(dir[i]);
                }
            }
            //創建臨時文件夾,如果存在就則刪除後再創建空文件夾
            try
            {
                if (!Directory.Exists(Settings.Default.imagePath + @"\temp" + count))
                {
                    // Create the directory it does not exist.
                    Directory.CreateDirectory(Settings.Default.imagePath + @"\temp" + count);
                }
                else
                {
                    Directory.Delete(Settings.Default.imagePath + @"\temp" + count, true);
                    Directory.CreateDirectory(Settings.Default.imagePath + @"\temp" + count);
                }
                //如果list中沒有多出圖片,則存入temp並調用拼圖函數
                string regStr = "";
                foreach (string str in images) {
                    regStr = str + "|" + regStr;
                }
                Regex regOver = new Regex(regStr.Substring(0,regStr.Length-1));
                for (int i = 0; i < listStr.Count; i++)
                {
                    //如果當前圖片不存在,則複製入新的文件夾 
                    if (regOver.IsMatch(listStr[i]))
                    {
                        //取出文件名
                        string imageName = listStr[i].Substring(path.Length+1);
                        System.IO.File.Move(listStr[i], Settings.Default.imagePath + @"\temp" + count + @"\" + imageName);
                        //剪切文件
                        //File.Move(listStr[i], path + @"\temp" + count);
                    }
                }
            }
            catch (Exception e) {
                return new Result(false,e.ToString());
            }
            //調用拼圖函數對temp中的圖片進行拼圖
            doTexture(Settings.Default.imagePath + @"\temp" + count);
            return new Result(true, ""); 
        }
    }

    /// <summary>
    /// 結果類
    /// </summary>
    public class Result {
        public bool isSuccess { get; set; }
        public string msg { get; set; }

        public Result(bool isSuccess, string msg) {
            this.isSuccess = isSuccess;
            this.msg = msg;
        }
    }
}

發佈了30 篇原創文章 · 獲贊 2 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章