コード例 #1
0
    public static void SaveResourceDatas(string path, ResourceDatas datas)
    {
        FileStream fs = new FileStream(path, FileMode.Create);

        datas.WriteTo(fs);
        fs.Flush();
        fs.Close();
    }
コード例 #2
0
    static void CopyGameResources(BuildTarget target, string path, bool miniBuid)
    {
        string exportPath = "";

        switch (target)
        {
        case BuildTarget.Android:
            exportPath = Application.dataPath + "/../../Builds/ExportResources/Android/";
            break;

        case BuildTarget.iOS:
            exportPath = Application.dataPath + "/../../Builds/ExportResources/IOS/";
            break;

        case BuildTarget.StandaloneWindows:
            exportPath = Application.dataPath + "/../../Builds/ExportResources/Windows/";
            break;
        }
        ResourceDatas resourceList = BuildHelper.LoadResourceDatas(exportPath + "_ResourceList.ab");

        if (resourceList == null)
        {
            EditorUtility.DisplayDialog("提示", "你应该先导出资源", "好的");
            return;
        }

        ResourceDatas miniList = new ResourceDatas();

        var e = resourceList.Resources.GetEnumerator();

        while (e.MoveNext())
        {
            if ((miniBuid && e.Current.Value.IsInstall()) || (!miniBuid && !e.Current.Value.IsOptional()))
            {
                string key = e.Current.Key;
                if (miniBuid)
                {
                    miniList.Resources.Add(key, e.Current.Value);
                }
                string targetPath = path + key.Substring(0, 2) + "/";
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                File.Copy(exportPath + key + ".ab", targetPath + key + ".ab", true);
            }
        }

        if (miniBuid)
        {
            BuildHelper.SaveResourceDatas(path + "ResourceList.ab", miniList);
        }
        else
        {
            File.Copy(exportPath + "_ResourceList.ab", path + "ResourceList.ab", true);
        }
    }
コード例 #3
0
    public static ResourceDatas LoadResourceDatas(string path)
    {
        if (!File.Exists(path))
        {
            return(null);
        }

        FileStream    fs  = new FileStream(path, FileMode.Open);
        ResourceDatas rds = LoadResourceDatas(fs);

        fs.Close();
        return(rds);
    }
コード例 #4
0
        static void Main(string[] args)
        {
            string[] plantforms = new string[] { "Android", "IOS", "Windows" };
            string   rootPath   = args[0];

            exportPath   = rootPath + "Builds";
            projectPath  = rootPath + "GameClient/";
            resourcePath = rootPath + "GameClient/Assets/Resources/";
            dllPath      = rootPath + "output/GameLogic.dll";
            dllbinPath   = rootPath + "GameClient/Assets/Resources/Install/Unpackage/GameLogic.bytes";
            dataPath     = rootPath + "GameClient/Assets/Resources/Install/Unpackage/Data/";
            slnPath      = rootPath + "GameLogic/GameLogic.sln";
            csprojPath   = rootPath + "GameLogic/GameLogic/GameLogic.csproj";
            msbuild      = rootPath + "tools/MSBuild/MSBuild.exe";

            File.Copy(csprojPath, csprojPath + ".back", true);

            ResourceDatas resourceList = new ResourceDatas();

            AddConfigDatas(ref resourceList);

            for (int i = 0; i < plantforms.Length; ++i)
            {
                CopyDll(plantforms[i], ref resourceList);
                CopyFiles(plantforms[i], resourceList);
            }

            File.Copy(csprojPath + ".back", csprojPath, true);
            File.Delete(csprojPath + ".back");

            Process          p  = new Process();
            ProcessStartInfo pi = new ProcessStartInfo(msbuild, slnPath + " /t:Rebuild /p:Configuration=Release");

            //pi.UseShellExecute = false;
            pi.CreateNoWindow = true;
            p.StartInfo       = pi;
            p.Start();
            p.WaitForExit();
        }
コード例 #5
0
        static void AddConfigDatas(ref ResourceDatas resourceList)
        {
            DirectoryInfo dir = new DirectoryInfo(dataPath);

            FileInfo[] files = dir.GetFiles("*.bytes");
            for (int i = 0; i < files.Length; ++i)
            {
                FileInfo     f        = files[i];
                string       filepath = f.FullName.Replace("\\", "/");
                string       subpath  = filepath.Substring(resourcePath.Length);
                string       md5      = FileHelper.GetStringMd5(subpath.ToLower());
                uint         crc      = FileHelper.GetFileCrc(filepath);
                int          size     = FileHelper.GetFileSize(filepath);
                string       path     = "Assets/Resources/" + subpath;
                ResourceType type     = ResourceType.Install | ResourceType.Unpackage;
                ResourceData rd       = new ResourceData();
                rd.Crc  = crc;
                rd.Size = size;
                rd.Type = type;
                rd.Path = path;
                resourceList.Resources.Add(md5, rd);
            }
        }
コード例 #6
0
        static void CopyDll(string plantform, ref ResourceDatas resourceList)
        {
            BuildDll(plantform);

            /**/////////////////////////////////////////////////////////////////////////
            byte[] bytes = File.ReadAllBytes(dllPath);
            Rc4.rc4_go(ref bytes, bytes, (long)bytes.Length, Rc4.key, Rc4.key.Length, 0);
            File.WriteAllBytes(dllbinPath, bytes);
            /////////////////////////////////////////////////////////////////////////*/

            string       subpath = dllbinPath.Substring(resourcePath.Length);
            string       md5     = FileHelper.GetStringMd5(subpath.ToLower());
            uint         crc     = FileHelper.GetFileCrc(dllbinPath);
            int          size    = FileHelper.GetFileSize(dllbinPath);
            string       path    = "Assets/Resources/" + subpath;
            ResourceType type    = ResourceType.Install | ResourceType.Unpackage;
            ResourceData rd      = new ResourceData();

            rd.Crc  = crc;
            rd.Size = size;
            rd.Type = type;
            rd.Path = path;
            resourceList.Resources[md5] = rd;
        }
コード例 #7
0
        static void CopyFiles(string plantform, ResourceDatas resourceList)
        {
            string targetPath = exportPath + "/ExportResources/" + plantform + "/";

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

            if (File.Exists(targetPath + "export_names.txt"))
            {
                File.Copy(targetPath + "export_names.txt", targetPath + "_export_names.txt", true);
            }

            string        r1 = targetPath + "_ResourceList_1.ab";
            string        r  = targetPath + "_ResourceList.ab";
            ResourceDatas l1 = BuildHelper.LoadResourceDatas(r1);

            if (l1 == null)
            {
                l1 = new ResourceDatas();
            }
            ResourceDatas l = BuildHelper.LoadResourceDatas(r);

            if (l == null)
            {
                l = new ResourceDatas();
            }
            var e = resourceList.Resources.GetEnumerator();

            while (e.MoveNext())
            {
                var key = e.Current.Key;
                var rd  = e.Current.Value;
                File.Copy(projectPath + rd.Path, targetPath + key + ".ab", true);
                l1.Resources[key] = rd;
                l.Resources[key]  = rd;
            }

            BuildHelper.SaveResourceDatas(r1, l1);
            BuildHelper.SaveResourceDatas(r, l);

            SevenZipHelper.CompressFile(r, targetPath + "ResourceList.ab");

            StreamWriter writer = new StreamWriter(targetPath + "export_names.txt", false, Encoding.Default);

            writer.WriteLine("string,uint,int,int,string,string");
            writer.WriteLine("key,crc,size,type,path,depends");
            foreach (var c in l1.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Path, depends));
            }

            ResourceDatas resources_2 = BuildHelper.LoadResourceDatas(targetPath + "_ResourceList_2.ab");

            if (resources_2 != null)
            {
                foreach (var c in resources_2.Resources)
                {
                    string[] dependsArray = new string[c.Value.Depends.Count];
                    for (int i = 0; i < dependsArray.Length; ++i)
                    {
                        dependsArray[i] = c.Value.Depends[i];
                    }
                    string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                    writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Path, depends));
                }
            }
            writer.Close();

            if (File.Exists(targetPath + "version.txt"))
            {
                string[] strs     = File.ReadAllLines(targetPath + "version.txt");
                string[] vers     = strs[0].Split(' ');
                string   versions = vers[0];
                versions += " ";
                versions += FileHelper.GetFileCrc(targetPath + "_ResourceList.ab");
                versions += " ";
                versions += vers[2];
                byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
                File.WriteAllBytes(targetPath + "version.txt", buf);
            }
        }
コード例 #8
0
    static void Export(BuildTarget target)
    {
#if !RECOURCE_CLIENT
        CopyDll(target);
#endif
        UnityEngine.Debug.Log("Start Export Resources " + target + " " + DateTime.Now);
        //UpdateProgressBar("正在导出 " + GetBuildTargetName(target) + " 平台资源");
        string manifest;
        if (target == BuildTarget.Android)
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/Android/";
            manifest  = exportDir + "Android";
        }
        else if (target == BuildTarget.iOS)
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/IOS/";
            manifest  = exportDir + "IOS";
        }
        else
        {
            exportDir = Application.dataPath + "/../../Builds/ExportResources/Windows/";
            manifest  = exportDir + "Windows";
        }

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

        if (File.Exists(exportDir + "export_names.txt"))
        {
            File.Copy(exportDir + "export_names.txt", exportDir + "_export_names.txt", true);
        }

        string saveManifest      = manifest + "_2";
        string resourceList1Name = "_ResourceList_1.ab";
        string resourceList2Name = "_ResourceList_2.ab";
        string resourceListName  = resourceList2Name;
#if !RECOURCE_CLIENT
        saveManifest     = manifest + "_1";
        resourceListName = resourceList1Name;
#endif

        if (File.Exists(saveManifest))
        {
            File.Copy(saveManifest, manifest, true);
        }
        if (File.Exists(saveManifest + ".manifest"))
        {
            File.Copy(saveManifest + ".manifest", manifest + ".manifest", true);
        }

        ResourceDatas lastCompressFiles = BuildHelper.LoadResourceDatas(exportDir + resourceListName);
        List <string> lastExports       = new List <string>();

        if (lastCompressFiles != null && lastCompressFiles.Resources.Count > 0)
        {
            var e = lastCompressFiles.Resources.GetEnumerator();
            while (e.MoveNext())
            {
                lastExports.Add(e.Current.Key);
            }
        }

//         if (target == BuildTarget.Android)
//         {
//             for (int idx = 0; idx < sSoundPoolMusic.Count; ++idx)
//             {
//                 int num = sSoundPoolMusic[idx];
//                 string oldName = "Assets/Resources/Sound/" + num + ".mp3";
//                 string newName = "Assets/Resources/UnPackage/Sound/" + num + ".mp3";
//                 string oldId = GetPathID(oldName);
//                 sExportFiles.Remove(oldId);
//                 AssetDatabase.MoveAsset(oldName, newName);
//                 AssetDatabase.Refresh();
//                 string newId = GetPathID(newName);
//                 sExportFiles.Add(newId, newName);
//             }
//         }

        foreach (KeyValuePair <string, string> pair in sExportFiles)
        {
            string fullPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + pair.Value;
            if (lastExports.Contains(pair.Key))
            {
                lastExports.Remove(pair.Key);
            }

            if (pair.Value.StartsWith("Assets/Resources/") && pair.Value.Contains("Unpackage"))
            {
                string targetDir = exportDir + pair.Key + ".ab";
                FileHelper.CopyFile(pair.Value, targetDir);
                SetAssetBundleName(fullPath, "", "");
            }
        }
        foreach (string str in lastExports)
        {
            if (File.Exists(exportDir + str + ".ab"))
            {
                File.Delete(exportDir + str + ".ab");
            }

            if (File.Exists(exportDir + str + ".ab.manifest"))
            {
                File.Delete(exportDir + str + ".ab.manifest");
            }
        }

        AssetDatabase.Refresh();

        BuildPipeline.BuildAssetBundles(exportDir, BuildAssetBundleOptions.ChunkBasedCompression, target);
        //AssetDatabase.Refresh();
        //UpdateProgressBar();

        ResourceDatas resources = new ResourceDatas();

        foreach (KeyValuePair <string, string> pair in sExportFiles)
        {
            if (!File.Exists(exportDir + pair.Key + ".ab"))
            {
                FileHelper.CopyFile(pair.Value, exportDir + pair.Key + ".ab");
                UnityEngine.Debug.LogError("Export AssetBundle " + pair.Value + " fail, use copy instead!");
            }

            ResourceData rd = GetResourceData(pair.Key, pair.Value);
            resources.Resources.Add(pair.Key, rd);

            //UpdateProgressBar();
        }
        BuildHelper.SaveResourceDatas(exportDir + resourceListName, resources);

        ResourceDatas resources_1 = BuildHelper.LoadResourceDatas(exportDir + resourceList1Name);
        ResourceDatas resources_2 = BuildHelper.LoadResourceDatas(exportDir + resourceList2Name);
#if !RECOURCE_CLIENT
        if (resources_2 != null)
        {
            foreach (var c in resources_2.Resources)
            {
#else
        if (resources_1 != null)
        {
            foreach (var c in resources_1.Resources)
            {
#endif
                if (!resources.Resources.ContainsKey(c.Key))
                {
                    resources.Resources.Add(c.Key, c.Value);
                }
                else
                {
                    UnityEngine.Debug.LogError("ResourceClient与GameClient中有相同路径的资源 : " + c.Value.Path);
                }
            }
        }

        BuildHelper.SaveResourceDatas(exportDir + "_ResourceList.ab", resources);
        SevenZipHelper.CompressFile(exportDir + "_ResourceList.ab", exportDir + "ResourceList.ab");
        //#endif


        StreamWriter writer = new StreamWriter(exportDir + "export_names.txt", false, Encoding.Default);
        writer.WriteLine("string,uint,int,int,string,string");
        writer.WriteLine("key,crc,size,type,reference,path,depends");
        if (resources_1 != null)
        {
            foreach (var c in resources_1.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Reference, c.Value.Path, depends));
            }
        }
        if (resources_2 != null)
        {
            foreach (var c in resources_2.Resources)
            {
                string[] dependsArray = new string[c.Value.Depends.Count];
                for (int i = 0; i < dependsArray.Length; ++i)
                {
                    dependsArray[i] = c.Value.Depends[i];
                }
                string depends = c.Value.Depends.Count == 0 ? "" : string.Join("|", dependsArray);
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", c.Key, c.Value.Crc, c.Value.Size, (int)c.Value.Type, c.Value.Reference, c.Value.Path, depends));
            }
        }
        writer.Close();

#if !RECOURCE_CLIENT
        string versions = ResourceManager.CodeVersion.ToString();
        if (File.Exists(exportDir + "version.txt"))
        {
            string[] strs = File.ReadAllLines(exportDir + "version.txt");
            string[] vers = strs[0].Split(' ');
            versions = vers[0];
        }
        versions += " ";
        versions += FileHelper.GetFileCrc(exportDir + "_ResourceList.ab");
        versions += " ";
        string version = "";
        BuildHelper.GetCSharpVersionCode(ref version);
        version  += ".";
        version  += BuildHelper.GetRourceVersion();
        versions += version;
        byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
        File.WriteAllBytes(exportDir + "version.txt", buf);
#else
        if (File.Exists(exportDir + "version.txt"))
        {
            string[] strs     = File.ReadAllLines(exportDir + "version.txt");
            string[] vers     = strs[0].Split(' ');
            string   versions = vers[0]; versions += " ";
            versions += FileHelper.GetFileCrc(exportDir + "_ResourceList.ab");
            byte[] buf = System.Text.Encoding.Default.GetBytes(versions);
            File.WriteAllBytes(exportDir + "version.txt", buf);
        }
#endif

        //         if (target == BuildTarget.Android)
        //         {
        //             for (int idx = 0; idx < sSoundPoolMusic.Count; ++idx)
        //             {
        //                 int num = sSoundPoolMusic[idx];
        //                 string oldName = "Assets/Resources/UnPackage/Sound/" + num + ".mp3";
        //                 string newName = "Assets/Resources/Sound/" + num + ".mp3";
        //                 string oldId = GetPathID(oldName);
        //                 sExportFiles.Remove(oldId);
        //                 AssetDatabase.MoveAsset(oldName, newName);
        //                 AssetDatabase.Refresh();
        //                 string newId = GetPathID(newName);
        //                 sExportFiles.Add(newId, newName);
        //
        //                 string fullPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + newName;
        //                 string[] abs = newId.Split('.');
        //                 SetAssetBundleName(fullPath, abs[0], abs[1]);
        //                 AssetDatabase.Refresh();
        //             }
        //         }
        //         UpdateProgressBar();

        File.Copy(manifest, saveManifest, true);
        File.Copy(manifest + ".manifest", saveManifest + ".manifest", true);

        UnityEngine.Debug.Log("Finish Export Resources " + target + " " + DateTime.Now);
    }