Esempio n. 1
0
        private static List <SizePair> CalculateScenesSizes(Func <string, SizePair?> getSize)
        {
            List <SizePair> result = new List <SizePair>();

            for (int i = 0, levelIndex = 0; ; ++i, ++levelIndex)
            {
                SizePair totalSize = 0;
                string   levelPath = "level" + levelIndex;

                if (i == 0)
                {
                    // on new unity it's gone... don't know which version exactly made that change
                    var mainDataPath = "mainData";
                    if (getSize(mainDataPath).HasValue)
                    {
                        levelPath = mainDataPath;
                        --levelIndex;
                    }
                }

                bool hasEntryForLevel = false;

                var size = getSize(levelPath);
                if (size.HasValue)
                {
                    totalSize.compressed   += size.Value.compressed;
                    totalSize.uncompressed += size.Value.uncompressed;
                    hasEntryForLevel        = true;
                }

                for (int splitIndex = 0; ; ++splitIndex)
                {
                    var splitPath = levelPath + ".split" + splitIndex;
                    size = getSize(splitPath);
                    if (size.HasValue)
                    {
                        totalSize.compressed   += size.Value.compressed;
                        totalSize.uncompressed += size.Value.uncompressed;
                        hasEntryForLevel        = true;
                    }
                    else
                    {
                        break;
                    }
                }

                if (hasEntryForLevel)
                {
                    result.Add(totalSize);
                }
                else
                {
                    break;
                }
            }

            return(result);
        }
Esempio n. 2
0
        private static void AddSizePair(List <SizePair> sizePairs, Type sizeType, WearSize size)
        {
            foreach (var std in Enum.GetValues(sizeType))
            {
                var newPiar = new SizePair(GetSizeStdCode(std), size.Names[(int)std]);

                if (newPiar.Size == null)
                {
                    continue;
                }

                if (!sizePairs.Any(pair => pair.StandardCode == newPiar.StandardCode && pair.Size == newPiar.Size))
                {
                    sizePairs.Add(newPiar);
                }
            }
        }
Esempio n. 3
0
        private static BuildArtifactsInfo CreateFromFileSystem(string totalSizeDir, string dataDirectory, string streamingAssetsName)
        {
            var modulesDirectory         = ReliablePath.Combine(dataDirectory, "Managed");
            var streamingAssetsDirectory = ReliablePath.Combine(dataDirectory, streamingAssetsName);

            Dictionary <string, SizePair> modules = new Dictionary <string, SizePair>();
            long runtimeSize = 0;

            if (Directory.Exists(modulesDirectory))
            {
                // dlls are included as assets, so don't count them as runtime size
                modules = Directory.GetFiles(modulesDirectory, "*.dll", SearchOption.TopDirectoryOnly)
                          .ToDictionary(x => ReliablePath.GetFileName(x), x => (SizePair)GetFileSizeNoThrow(x));

                runtimeSize = GetDirectorySizeNoThrow(modulesDirectory) - Enumerable.Sum(modules, x => x.Value.uncompressed);
            }

            var unityResources = s_unityResourcesNames
                                 .Select(x => new { Relative = x, Actual = ReliablePath.Combine(dataDirectory, x) })
                                 .Where(x => !Directory.Exists(x.Actual))
                                 .Select(x => new { x.Relative, File = new FileInfo(x.Actual) })
                                 .Where(x => x.File.Exists)
                                 .ToDictionary(x => x.Relative, x => (SizePair)x.File.Length);

            return(new BuildArtifactsInfo()
            {
                totalSize = GetDirectorySizeNoThrow(totalSizeDir),
                streamingAssetsSize = GetDirectorySizeNoThrow(streamingAssetsDirectory),
                runtimeSize = runtimeSize,
                managedModules = modules,
                unityResources = unityResources,
                sceneSizes = CalculateScenesSizes(x =>
                {
                    var fileInfo = new FileInfo(ReliablePath.Combine(dataDirectory, x));
                    if (!fileInfo.Exists)
                    {
                        return null;
                    }
                    return fileInfo.Length;
                })
            });
        }
Esempio n. 4
0
        private static BuildArtifactsInfo CreateForWebGL(string buildPath)
        {
            var compressedSize      = GetDirectorySizeNoThrow(buildPath);
            var totalSize           = compressedSize;
            var streamingAssetsSize = GetDirectorySizeNoThrow(ReliablePath.Combine(buildPath, "StreamingAssets"));

            var latestReport = UnityVersionAgnostic.GetLatestBuildReport();

            if (latestReport == null)
            {
                throw new System.InvalidOperationException("Unable to retreive native Unity report");
            }

            var prop = new SerializedObject(latestReport).FindPropertyOrThrow("m_Files");

            var scenes  = new List <SizePair>();
            var modules = new Dictionary <string, SizePair>();

            for (int propIdx = 0; propIdx < prop.arraySize; ++propIdx)
            {
                var elem = prop.GetArrayElementAtIndex(propIdx);
                var role = elem.FindPropertyRelativeOrThrow("role").stringValue;

                if (role == "Scene")
                {
                    var path      = elem.FindPropertyRelativeOrThrow("path").stringValue;
                    var prefix    = "level";
                    var lastIndex = path.LastIndexOf(prefix);
                    if (lastIndex < 0)
                    {
                        Log.Warning("Unexpected level path: " + path);
                        continue;
                    }

                    var levelNumberStr = path.Substring(lastIndex + prefix.Length);
                    var levelNumber    = int.Parse(levelNumberStr);

                    // pad with zeros
                    for (int i = scenes.Count; i <= levelNumber; ++i)
                    {
                        scenes.Add(0);
                    }

                    var s = elem.FindPropertyRelative("totalSize").longValue;
                    scenes[levelNumber] = new SizePair(s, s);
                }
                else if (role == "DependentManagedLibrary" || role == "ManagedLibrary")
                {
                    var path      = elem.FindPropertyRelativeOrThrow("path").stringValue;
                    var prefix    = "/Managed/";
                    var lastIndex = path.LastIndexOf(prefix);
                    if (lastIndex < 0)
                    {
                        Log.Warning("Unexpected module path: " + path);
                        continue;
                    }

                    var moduleName = path.Substring(lastIndex + prefix.Length);
                    var s          = elem.FindPropertyRelative("totalSize").longValue;
                    modules.Add(moduleName, new SizePair(0, s));
                }
            }

            // try to run 7z to get actual data size
            var releaseDir = ReliablePath.Combine(buildPath, "Release");

            if (Directory.Exists(releaseDir))
            {
                var buildName        = buildPath.Split(new[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries).Last();
                var zipPath          = ReliablePath.Combine(releaseDir, buildName + ".datagz");
                var uncompressedSize = Get7ZipArchiveUncompressedSize(zipPath);
                if (uncompressedSize >= 0)
                {
                    totalSize += uncompressedSize;
                    totalSize -= GetFileSizeNoThrow(zipPath);
                }
            }
            else
            {
                var buildDir = ReliablePath.Combine(buildPath, "Build");
                if (Directory.Exists(buildDir))
                {
                    foreach (var compressedFile in Directory.GetFiles(buildDir, "*.unityweb"))
                    {
                        var uncompressedSize = Get7ZipArchiveUncompressedSize(compressedFile);
                        if (uncompressedSize >= 0)
                        {
                            totalSize += uncompressedSize;
                            totalSize -= GetFileSizeNoThrow(compressedFile);
                        }
                    }
                }
            }

            return(new BuildArtifactsInfo()
            {
                totalSize = new SizePair(compressedSize, totalSize),
                streamingAssetsSize = streamingAssetsSize,
                sceneSizes = scenes,
                managedModules = modules,
            });
        }
Esempio n. 5
0
        private static BuildArtifactsInfo CreateForAndroid(string buildPath, bool hasObb)
        {
            Dictionary <string, SizePair> partialResults = new Dictionary <string, SizePair>();
            Dictionary <string, SizePair> managedModules = new Dictionary <string, SizePair>();
            Dictionary <string, SizePair> otherAssets    = new Dictionary <string, SizePair>();

            const string DataDirectory = "assets/bin/Data/";

            long compressedSize      = 0;
            long uncompressedSize    = 0;
            long streamingAssetsSize = 0;

            SizePair runtimeSize    = new SizePair();
            var      unityResources = new Dictionary <string, SizePair>();

            //var sources = Enumerable.Repeat(new { Source = "Apk", Files = GetFilesFromZipArchive(buildPath) }, 1);

            var sources = new List <KeyValuePair <string, string> >();

            sources.Add(new KeyValuePair <string, string>("Apk", buildPath));

            if (hasObb)
            {
                var obbPath = DropExtension(buildPath) + ".main.obb";
                sources.Add(new KeyValuePair <string, string>("Obb", obbPath));
            }

            foreach (var source in sources)
            {
                var path = source.Value;

                compressedSize += GetFileSizeNoThrow(path);

                //files = files.Concat(GetFilesFromZipArchive(obbPath));
                var files = GetFilesFromZipArchive(path);
                foreach (var entry in files)
                {
                    uncompressedSize += entry.size.uncompressed;

                    if (entry.path.StartsWith(DataDirectory))
                    {
                        var fileName = entry.path.Substring(DataDirectory.Length);
                        if (fileName.StartsWith("level") || fileName.StartsWith("mainData"))
                        {
                            partialResults.Add(fileName, entry.size);
                        }
                        else if (fileName.StartsWith("Managed/"))
                        {
                            // dlls are included as assets, so don't count them as a part of runtime
                            if (fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                            {
                                var actualFileName = ReliablePath.GetFileName(fileName);
                                managedModules.Add(actualFileName, entry.size);
                            }
                            else
                            {
                                runtimeSize.compressed   += entry.size.compressed;
                                runtimeSize.uncompressed += entry.size.uncompressed;
                            }
                        }
                        else if (s_unityResourcesNames.Contains(fileName))
                        {
                            unityResources.Add(fileName, entry.size);
                        }
                        else
                        {
                            // is it a guid?
                            var justFileName = Path.GetFileNameWithoutExtension(fileName);
                            if (justFileName.Length == 32 && justFileName.All(x => char.IsDigit(x) || x >= 'a' && x <= 'f' || x >= 'A' && x <= 'F'))
                            {
                                SizePair existingEntry;
                                if (otherAssets.TryGetValue(justFileName, out existingEntry))
                                {
                                    otherAssets[justFileName] = new SizePair(existingEntry.compressed + entry.size.compressed, existingEntry.uncompressed + entry.size.uncompressed);
                                }
                                else
                                {
                                    otherAssets.Add(justFileName, entry.size);
                                }
                            }
                        }
                    }
                    else if (entry.path.StartsWith("assets/"))
                    {
                        streamingAssetsSize += entry.size.uncompressed;
                    }
                    else if (entry.path.StartsWith("lib/"))
                    {
                        runtimeSize.compressed   += entry.size.compressed;
                        runtimeSize.uncompressed += entry.size.uncompressed;
                    }
                }
            }

            var scenes = CalculateScenesSizes(x =>
            {
                SizePair result;
                if (partialResults.TryGetValue(x, out result))
                {
                    return(result);
                }
                return(null);
            });

            return(new BuildArtifactsInfo()
            {
                managedModules = managedModules,
                sceneSizes = scenes,
                streamingAssetsSize = streamingAssetsSize,
                totalSize = new SizePair(compressedSize, uncompressedSize),
                runtimeSize = runtimeSize,
                unityResources = unityResources,
                otherAssets = otherAssets,
            });
        }
Esempio n. 6
0
 public static List <SizePair> MatchSize(SizePair sizePair, SizeUsePlace place)
 {
     return(MatchSize(sizePair.StandardCode, sizePair.Size, place));
 }