Example #1
0
        static byte[] PlistPasswordByte = BitConverter.GetBytes(PlistPasswordInt);  //密码缓存
        public static void PackPlist(string path, DirectoryInfo theFolder)
        {
            string outFolder; //输出目录
            string head = "";

            if (theFolder == null)
            {
                theFolder        = new DirectoryInfo(@path);
                outFolder        = Util.config.PlistOutDir + "\\"; //目标文件夹
                FirstLoadDirHead = theFolder.Name;                 //主目录
            }
            else
            {
                head      = theFolder.FullName.Remove(0, theFolder.FullName.IndexOf(FirstLoadDirHead) + FirstLoadDirHead.Length + 1);
                outFolder = Util.config.PlistOutDir + "\\" + head + "\\"; //目标文件夹
            }
            //目标文件夹不存在就创建
            if (!Directory.Exists(outFolder))
            {
                Directory.CreateDirectory(outFolder);
            }
            bool IsPlistChange    = false;  //图集图片发生改变
            bool IsHaveImage      = false;  //目录下是否有图片
            bool IsHavePlistImage = false;  //是否有需要打包的图片
            int  ImageWidth       = 0;      //图片宽度
            int  ImageHeight      = 0;      //图片高度

            //遍历文件夹
            foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
            {
                PackPlist(NextFolder.FullName, NextFolder);
            }

            List <string> ImageRecord = null;

            FolderImageRecord.TryGetValue(head, out ImageRecord);
            //遍历文件
            foreach (FileInfo NextFile in theFolder.GetFiles())
            {
                string strType = NextFile.Extension.ToLower();
                if (strType == ".png" || strType == ".jpg") //只判断图片
                {
                    IsHaveImage = true;
                    bool IsNewImage = false;
                    var  fileData   = NextFile.Open(FileMode.Open);
                    //读取密码
                    fileData.Seek(-8, SeekOrigin.End);
                    fileData.Read(ulongRead, 0, 8);
                    if (PlistPasswordInt != System.BitConverter.ToUInt64(ulongRead, 0))
                    {
                        //没找到密码说明是新的
                        IsNewImage = true;
                        fileData.Seek(0, SeekOrigin.End);
                        fileData.Write(PlistPasswordByte, 0, 8);
                    }
                    //从缓存中取出数据
                    string key  = head + "\\" + NextFile.Name + "_" + fileData.Length;
                    string data = null;
                    if (ImageRecord != null)
                    {
                        ImageRecord.Remove(key);
                    }
                    if (OldImageInfo != null)
                    {
                        OldImageInfo.TryGetValue(key, out data);
                    }
                    if (data != null)
                    {
                        //找到缓存了
                        NewImageInfo.Add(key, data);
                        var value = data.Split('|');
                        ImageWidth  = int.Parse(value[0]);
                        ImageHeight = int.Parse(value[1]);
                    }
                    else
                    {
                        //没缓存,是新图片
                        IsNewImage = true;
                        System.Drawing.Image image = System.Drawing.Image.FromStream(fileData);
                        ImageWidth  = image.Width;
                        ImageHeight = image.Height;
                        NewImageInfo.Add(key, ImageWidth + "|" + ImageHeight);
                    }
                    //关闭文件
                    fileData.Close();
                    //大图需要剪切
                    if (Util.config.OnlyCopyFiles.IndexOf(NextFile.Name) >= 0 ||
                        (ImageHeight >= Util.config.CopyFileHeight && ImageWidth >= Util.config.CopyFileWidth))
                    {
                        string strSrc  = NextFile.FullName;
                        string strDesc = outFolder + Util.GetTimeStamp() + "_" + NextFile.Name;
                        string strCopy = outFolder + NextFile.Name;
                        File.Move(strSrc, strDesc);
                        ImageMoveMap.Add(strSrc, strDesc);
                        //目标位置没有图片
                        if (!File.Exists(strCopy))
                        {
                            File.Copy(strDesc, strCopy);
                            LogOut.Add("ImageCopy: " + strCopy);
                        }
                    }
                    else
                    {
                        IsHavePlistImage = true;
                        if (IsNewImage)
                        {
                            //小图需要合并图集
                            IsPlistChange = true;
                        }
                    }
                }
            }

            //有图片被删掉了,强制生成
            if (ImageRecord != null && ImageRecord.Count > 0)
            {
                IsPlistChange = true;
            }

            //导出的名字
            string PlistName = theFolder.Name;

            if (FirstLoadDirHead == PlistName)
            {
                PlistName = Util.config.PlistOutDir;
                PlistName = PlistName.Remove(0, PlistName.LastIndexOf("\\") + 1);
            }
            //plist是否存在
            if (!IsPlistChange && IsHavePlistImage && (!File.Exists(outFolder + PlistName + ".png") || !File.Exists(outFolder + PlistName + ".plist")))
            {
                IsPlistChange = true;
            }
            //必须有图片,有改变或者强制才导出图集
            if (IsHaveImage && (IsPlistChange || Util.config.IsForceOutPlist))
            {
                int MaxPlistSize = Util.config.MaxPlistSize;
                if (Util.config.OnlyOnePlist.IndexOf(head) >= 0) //判断是否单图模式
                {
                    MaxPlistSize = 16384;
                }
                //为了优化时间,只有在添加满足图集条件的新图片时才导出, 删除图片不检测。
                //--multipack  \"{n1}\" 多图模式有问题,不要用,分子文件夹
                LogOut.Add("PackPlist: " + path + " PlistSize: " + MaxPlistSize);
                string cmd = "./TexturePacker/bin/TexturePacker --allow-free-size --opt RGBA8888 --format cocos2d --enable-rotation --trim-sprite-names";
                if (!Util.config.IsForceOutPlist)
                {
                    cmd += " --smart-update";                          //强制导出模式
                }
                cmd += " --ignore-files */" + theFolder.Name + "/*/*"; //忽略子文件夹
                cmd += " --max-size " + MaxPlistSize;                  //图集最大尺寸
                cmd += " --sheet " + outFolder + PlistName + ".png";   //图片
                cmd += " --data " + outFolder + PlistName + ".plist";  //plist
                cmd += " " + path;                                     //来源
                PlistCmdList.Add(cmd);
            }
        }
Example #2
0
        public static void XlsxToCSV()
        {
            string xlsxpath   = Application.dataPath + "/XLSX";
            string streampath = Application.dataPath + "/StreamingAssets";
            string csvpath    = Application.dataPath + "/StreamingAssets/csv";

            _csvListToBeRestored.Clear();
            //文件列表
            //string listpath = Application.dataPath + "/StreamingAssets/csvList.txt";
            //FileStream fs = new FileStream( listpath, FileMode.Create );
            //StreamWriter listwriter = new StreamWriter( fs, new UTF8Encoding(false) );
            DirectoryInfo TheFolder = new DirectoryInfo(xlsxpath);

            if (!Directory.Exists(csvpath))
            {
                Directory.CreateDirectory(csvpath);
            }

            try
            {
                //对文件进行遍历
                foreach (var NextFile in TheFolder.GetFiles())
                {
                    if (Path.GetExtension(NextFile.Name) == ".xlsx" && !NextFile.Name.StartsWith("~$"))
                    {
                        string csvfile = XLSXTOCSV(NextFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                        CreateCSVFile(csvpath + "/" + NextFile.Name.Split('.')[0] + ".csv", csvfile);
                        Debug.Log(NextFile.Name.Split('.')[0] + "  文件生成成功!");
                        //listwriter.WriteLine( "csv/" + NextFile.Name.Split( '.' )[ 0 ] + ".csv" );
                        string str = "csv/" + NextFile.Name.Split('.')[0] + ".csv";
                        _csvListToBeRestored.Add(new ABVersion {
                            AbName = str, MD5 = LitFramework.Crypto.Crypto.md5.GetFileHash(csvpath + "/" + NextFile.Name.Split('.')[0] + ".csv"), Version = 1
                        });
                    }
                    else if (Path.GetExtension(NextFile.Name) == ".txt")
                    {
                        FileInfo fi = new FileInfo(csvpath + "/" + NextFile.Name);
                        if (fi.Exists)
                        {
                            fi.Delete();
                        }
                        NextFile.CopyTo(csvpath + "/" + NextFile.Name);
                        //listwriter.WriteLine( NextFile.Name );
                        _csvListToBeRestored.Add(new ABVersion {
                            AbName = NextFile.Name, MD5 = string.Empty, Version = 0
                        });
                    }
                }

                //遍历框架配置的额外后缀文件
                string[] extralFile = FrameworkConfig.Instance.configs_suffix.Split('|');
                foreach (var item in extralFile)
                {
                    if (item.Equals("csv"))
                    {
                        continue;
                    }

                    GetFiles(new DirectoryInfo(streampath), item, _csvListToBeRestored);
                }
            }
            catch (Exception e) { Debug.LogError(e.Message); }
            finally
            {
                //加载本地文件,没有就创建完成。有则比对同名文件的MD5,不一样则version+1
                MatchCSVTotalFile(_csvListToBeRestored);

                //listwriter.Close();
                //listwriter.Dispose();
                //fs.Dispose();
#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
            }
        }
Example #3
0
        public static void TraversalFolder(string path, DirectoryInfo theFolder, string LogicType)
        {
            if (theFolder == null)
            {
                theFolder = new DirectoryInfo(@path);
            }

            if (LogicType == "ENCRY")    //加密
            {
                var ary = Tag.ToCharArray();
                for (var i = 0; i < Tag.Length; i++)
                {
                    WriteBuffer[i] = (byte)ary[i];
                }
            }

            //遍历文件
            foreach (FileInfo NextFile in theFolder.GetFiles())
            {
                string strType = NextFile.Extension.ToLower();
                if (strType == ".png" || strType == ".jpg") //只判断图片
                {
                    var readFile = NextFile.Open(FileMode.Open);
                    int FileSize = (int)readFile.Length;
                    readFile.Read(ReadBuffer, 0, FileSize);
                    readFile.Close();

                    var    writeFile = NextFile.Open(FileMode.Truncate);
                    string strHead   = System.Text.Encoding.Default.GetString(ReadBuffer, 0, Tag.Length);
                    if (LogicType == "ENCRY")    //加密
                    {
                        if (strHead == Tag)
                        {
                            Util.Error("该图片已加密,请勿重复。" + NextFile.FullName);
                            continue;
                        }
                        //加密
                        for (var i = 0; i < FileSize; i++)
                        {
                            int value = (ReadBuffer[i] + i - i % 3) ^ Key;
                            WriteBuffer[i + Tag.Length] = (byte)value;
                        }
                        writeFile.Write(WriteBuffer, 0, FileSize + Tag.Length);
                    }
                    else if (LogicType == "DECRY")  //解密
                    {
                        if (strHead != Tag)
                        {
                            Util.Error("该图片未加密,无法解密。" + NextFile.FullName);
                            continue;
                        }
                        //解密
                        for (var i = 0; i < FileSize; i++)
                        {
                            int value = (ReadBuffer[i + Tag.Length] ^ Key) - i + i % 3;
                            WriteBuffer[i] = (byte)value;
                        }
                        writeFile.Write(WriteBuffer, 0, FileSize - Tag.Length);
                    }
                    writeFile.Close();
                }
            }
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
            {
                TraversalFolder(NextFolder.FullName, NextFolder, LogicType);
            }
        }
Example #4
0
        public static void CsvToCs()
        {
            Debug.Log("配置文件转化为代码  开始!");
            _csvListToBeRestored.Clear();
            string xlsxpath   = Application.dataPath + "/XLSX";
            string streampath = Application.dataPath + "/StreamingAssets";
            string csvOutPath = Application.dataPath + "/StreamingAssets/csv";
            string csOutPath  = Application.dataPath + "/Scripts/CSV";

            if (!FrameworkConfig.Instance.UseHotFixMode)
            {
                csOutPath = Application.dataPath + "/Scripts/CSV";
            }
            else
            {
                csOutPath = Application.dataPath + "/Scripts/ILRuntime/HotFixLogic/CSV";
            }
            DirectoryInfo theXMLFolder = new DirectoryInfo(xlsxpath);

            //文件列表
            //string listpath = Application.dataPath + "/StreamingAssets/csvList.txt";
            //FileStream fs = new FileStream( listpath, FileMode.Create );
            //StreamWriter listwriter = new StreamWriter( fs, new UTF8Encoding(false) );

            if (!Directory.Exists(csvOutPath))
            {
                Directory.CreateDirectory(csvOutPath);
            }
            if (!Directory.Exists(csOutPath))
            {
                Directory.CreateDirectory(csOutPath);
            }

            try
            {
                ConfigsNamesTemplate cnt = new ConfigsNamesTemplate();
                //对文件进行遍历
                foreach (var NextFile in theXMLFolder.GetFiles())
                {
                    if (Path.GetExtension(NextFile.Name) == ".xlsx" && !NextFile.Name.StartsWith("~$"))
                    {
                        LDebug.Log(" >表处理 : " + NextFile.Name);
                        FileStream stream  = NextFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        string     csvfile = XLSXTOCSV(stream);
                        CSVParser  cp      = new CSVParser();
                        CreateCSFile(csOutPath, NextFile.Name.Split('.')[0] + ".cs", cp.CreateCS(NextFile.Name.Split('.')[0], csvfile));
                        CreateCSVFile(csvOutPath + "/" + NextFile.Name.Split('.')[0] + ".csv", csvfile);
                        LDebug.Log(NextFile.Name.Split('.')[0] + "  文件生成成功!");

                        //这里固定取配置表第三行配置作为类型读取,如果需要修改配置表适配服务器(增加第四行),需要同步修改
                        CSVReader reader = new CSVReader(csvfile);
                        cnt.configsNameList.Add(NextFile.Name.Split('.')[0], reader.GetData(0, 2));
                        //listwriter.WriteLine( "csv/" + NextFile.Name.Split( '.' )[ 0 ] + ".csv" );
                        string str = "csv/" + NextFile.Name.Split('.')[0] + ".csv";
                        _csvListToBeRestored.Add(new ABVersion {
                            AbName = str, MD5 = LitFramework.Crypto.Crypto.md5.GetFileHash(csvOutPath + "/" + NextFile.Name.Split('.')[0] + ".csv"), Version = 1
                        });
                    }
                    else if (Path.GetExtension(NextFile.Name) == ".txt")
                    {
                        FileInfo fi = new FileInfo(csvOutPath + "/" + NextFile.Name);
                        if (fi.Exists)
                        {
                            fi.Delete();
                        }
                        NextFile.CopyTo(csvOutPath + "/" + NextFile.Name);
                        //listwriter.WriteLine( NextFile.Name );
                        _csvListToBeRestored.Add(new ABVersion {
                            AbName = NextFile.Name, MD5 = string.Empty, Version = 0
                        });
                    }
                }

                //遍历框架配置的额外后缀文件
                string[] extralFile = FrameworkConfig.Instance.configs_suffix.Split('|');
                foreach (var item in extralFile)
                {
                    if (item.Equals("csv"))
                    {
                        continue;
                    }

                    GetFiles(new DirectoryInfo(streampath), item, _csvListToBeRestored);
                }

                //============更新并保存CS============//
                ConfigsParse rpp = new ConfigsParse();

                if (!FrameworkConfig.Instance.UseHotFixMode)
                {
                    EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts/Model/Const/", "Configs.cs", rpp.CreateCS(cnt));
                }
                else
                {
                    EditorMenuExtention.CreateCSFile(Application.dataPath + "/Scripts/ILRuntime/HotFixLogic/Model/Const/", "Configs.cs", rpp.CreateCS(cnt));
                }
            }
            catch (Exception e) { LDebug.LogError(e.Message); }
            finally
            {
                //加载本地文件,没有就创建完成。有则比对同名文件的MD5,不一样则version+1
                MatchCSVTotalFile(_csvListToBeRestored);

                //listwriter.Close();
                //listwriter.Dispose();
                //fs.Dispose();
#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
            }
        }