示例#1
0
        /// <summary>
        /// Loads this pack file
        /// </summary>
        public void Load()
        {
            // Get the stream
            using (Stream strm = OpenRead())
            {
                // Validate
                if (strm == null)
                {
                    throw new FileNotFoundException("File not found", filename);
                }

                // Read and verify header
                BinaryReader rdr    = new BinaryReader(strm);
                string       header = Encoding.ASCII.GetString(rdr.ReadBytes(HeaderString.Length));
                if (header != HeaderString)
                {
                    throw new Exception("Invalid header");
                }
                ushort version = rdr.ReadUInt16();
                if (version != HeaderVersion)
                {
                    throw new Exception("Invalid version");
                }
                int numentries = rdr.ReadInt32();
                flags = (PackFlags)rdr.ReadUInt32();
                if (numentries < 0)
                {
                    throw new Exception("Invalid entry count");
                }

                // Read all entries
                entries  = new List <FileEntry>(numentries);
                entrymap = new Dictionary <string, FileEntry>();
                for (uint i = 0; i < numentries; i++)
                {
                    FileEntry entry = new FileEntry();
                    entry.Name       = rdr.ReadString();
                    entry.Length     = rdr.ReadInt32();
                    entry.Offset     = rdr.ReadInt32();
                    entry.Dirty      = false;
                    entry.CachedData = null;
                    if (entrymap.ContainsKey(entry.Name))
                    {
                        throw new Exception(string.Format("Duplicate entry ({0})", entry.Name));
                    }
                    entries.Add(entry);
                    entrymap.Add(entry.Name, entry);
                }

                // Record the content location
                contentlocation = (int)strm.Position;
            }
        }
示例#2
0
        private PackFlags GetPackFlags()
        {
            PackFlags flags = PackFlags.None;

            if (Position != Vector3.zero)
            {
                flags |= PackFlags.HasPosition;
            }
            if (Direction != Vector3.zero)
            {
                flags |= PackFlags.HasDirection;
            }
            if (TargetIds != null)
            {
                flags |= PackFlags.HasTargetIds;
            }
            if (Amount != 0)
            {
                flags |= PackFlags.HasAmount;
            }
            if (ShouldQueue)
            {
                flags |= PackFlags.ShouldQueue;
            }
            if (ShouldClose)
            {
                flags |= PackFlags.ShouldClose;
            }
            if (CancelMovement)
            {
                flags |= PackFlags.CancelMovement;
            }


            return(flags);
        }
示例#3
0
        public void NetworkSerialize(NetworkSerializer serializer)
        {
            PackFlags flags = PackFlags.None;

            if (!serializer.IsReading)
            {
                flags = GetPackFlags();
            }

            serializer.Serialize(ref ActionTypeEnum);
            serializer.Serialize(ref flags);

            if (serializer.IsReading)
            {
                ShouldQueue    = (flags & PackFlags.ShouldQueue) != 0;
                CancelMovement = (flags & PackFlags.CancelMovement) != 0;
                ShouldClose    = (flags & PackFlags.ShouldClose) != 0;
            }

            if ((flags & PackFlags.HasPosition) != 0)
            {
                serializer.Serialize(ref Position);
            }
            if ((flags & PackFlags.HasDirection) != 0)
            {
                serializer.Serialize(ref Direction);
            }
            if ((flags & PackFlags.HasTargetIds) != 0)
            {
                serializer.Serialize(ref TargetIds);
            }
            if ((flags & PackFlags.HasAmount) != 0)
            {
                serializer.Serialize(ref Amount);
            }
        }
示例#4
0
        public static void PackResourcesTo(string destDirectory, PackFlags flags, Predicate <string> validateFileFunc = null)
        {
            if (string.IsNullOrEmpty(destDirectory))
            {
                Console.Error.WriteLine("Invalid empty destDirectory={0}", destDirectory);
                return;
            }

            os.mkdir(destDirectory);

            var searchOption = (flags & PackFlags.SearchAllDirectories) != 0 ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            Console.WriteLine(PathTools.ExportResourceRoot);

            var paths = Directory.GetFiles(PathTools.ExportResourceRoot, "*.*", searchOption);
            var isAssetBundleOutline = false;

            var knownDirectories = new HashSet <string>();
            var mappingInfos     = new List <MappingInfo>(2048);

            ScanTools.ScanAll("Packing builtin resources...", paths, srcPath =>
            {
                var isValid = null == validateFileFunc || validateFileFunc(srcPath);

                if (isValid)
                {
                    var bytes = File.ReadAllBytes(srcPath);
                    var localPathWithDigest = string.Empty;
                    var localPath           = os.path.normpath(PathTools.GetLocalPath(srcPath));

                    if (localPath.Equals(PathTools.PlatformResFolder))
                    {
                        isAssetBundleOutline = true;
                        localPathWithDigest  = localPath;
                    }
                    else
                    {
                        isAssetBundleOutline = false;
                        var digest           = Md5sum.Instance.GetAssetDigest(bytes);
                        localPathWithDigest  = PathTools.GetLocalPathWithDigest(localPath, digest);
                    }

                    var destPath = os.path.join(destDirectory, localPathWithDigest);

                    _CheckCreateDirectory(knownDirectories, destPath);

                    if (!File.Exists(destPath))
                    {
                        File.WriteAllBytes(destPath, bytes);
                    }

                    var info = new MappingInfo
                    {
                        localPath             = localPath
                        , localPathWithDigest = localPathWithDigest
                        , selfSize            = os.path.getsize(srcPath)
                        , totalSize           = isAssetBundleOutline ? os.path.getsize(srcPath) : BundleTools.GetTotalSize(srcPath)
                    };

                    mappingInfos.Add(info);
                }
            });

            var mappingPath = os.path.join(destDirectory, Constants.BuiltinMappingPath);

            WriteMappingFile(mappingPath, mappingInfos);
        }