Beispiel #1
0
 /// <summary>
 /// Removes the specified resource from the package.
 /// </summary>
 /// <param name="resource">The resource to remove.</param>
 /// <returns></returns>
 public bool RemoveResource(RantResource resource)
 {
     if (resource == null)
     {
         throw new ArgumentNullException(nameof(resource));
     }
     return(_resources.Remove(resource));
 }
Beispiel #2
0
 /// <summary>
 /// Determines whether the package contains the specified resource.
 /// </summary>
 /// <param name="resource">The resource to search for.</param>
 /// <returns></returns>
 public bool ContainsResource(RantResource resource)
 {
     if (resource == null)
     {
         throw new ArgumentNullException(nameof(resource));
     }
     return(_resources.Contains(resource));
 }
Beispiel #3
0
        internal static void SerializeResource(RantResource resource, EasyWriter writer)
        {
            if (!_resourceIdRegistry.TryGetValue(resource.GetType(), out string rstr))
            {
                throw new ArgumentException($"Resource type '{resource.GetType()}' is not registered.");
            }

            writer.WriteBytes(Encoding.ASCII.GetBytes(rstr));
            resource.SerializeData(writer);
        }
Beispiel #4
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        /// <param name="compress">Specifies whether to compress the package contents.</param>
        public void Save(
            string path,
            bool compress = true)
        {
            if (string.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += EXTENSION;
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                writer.Write(Encoding.ASCII.GetBytes(MAGIC));
                writer.Write(PACKAGE_FORMAT_VERSION);
                writer.Write(compress);
                writer.Write(_title);
                writer.Write(_id);
                writer.Write(Description);
                writer.Write(Tags);
                writer.Write(Authors);
                writer.Write(Version.Major);
                writer.Write(Version.Minor);
                writer.Write(Version.Revision);
                writer.Write(_dependencies.Count);
                foreach (var dep in _dependencies)
                {
                    writer.Write(dep.ID);
                    writer.Write(dep.Version.Major);
                    writer.Write(dep.Version.Minor);
                    writer.Write(dep.Version.Revision);
                    writer.Write(dep.AllowNewer);
                }
                writer.Write(_resources.Count);

                if (compress)
                {
                    using (var compressStream = new DeflateStream(writer.BaseStream, CompressionMode.Compress, true))
                    {
                        foreach (var res in _resources)
                        {
                            RantResource.SerializeResource(res, new EasyWriter(compressStream, leaveOpen: true));
                        }
                        compressStream.Flush();
                    }
                }
                else
                {
                    foreach (var res in _resources)
                    {
                        RantResource.SerializeResource(res, writer);
                    }
                }
                writer.BaseStream.Flush();
            }
        }
Beispiel #5
0
        /// <summary>
        /// Loads a package from the specified stream and returns it as a RantPackage object.
        /// </summary>
        /// <param name="source">The stream to load the package data from.</param>
        /// <returns></returns>
        public static RantPackage Load(Stream source)
        {
            using (var reader = new EasyReader(source))
            {
                string magic = Encoding.ASCII.GetString(reader.ReadBytes(4));
                if (magic != MAGIC)
                {
                    throw new InvalidDataException(GetString("err-file-corrupt"));
                }
                ushort version = reader.ReadUInt16();
                if (version != PACKAGE_FORMAT_VERSION)
                {
                    throw new InvalidDataException(GetString("err-invalid-package-version", version));
                }
                bool compress = reader.ReadBoolean();

                var package = new RantPackage();

                package.Title       = reader.ReadString();
                package.ID          = reader.ReadString();
                package.Description = reader.ReadString();
                package.Tags        = reader.ReadStringArray();
                package.Authors     = reader.ReadStringArray();
                int vmaj = reader.ReadInt32();
                int vmin = reader.ReadInt32();
                int vrev = reader.ReadInt32();
                package.Version = new RantPackageVersion(vmaj, vmin, vrev);
                int depCount = reader.ReadInt32();
                for (int i = 0; i < depCount; i++)
                {
                    var  depId         = reader.ReadString();
                    int  depVerMaj     = reader.ReadInt32();
                    int  depVerMin     = reader.ReadInt32();
                    int  depVerRev     = reader.ReadInt32();
                    bool depAllowNewer = reader.ReadBoolean();
                    package.AddDependency(new RantPackageDependency(depId, new RantPackageVersion(depVerMaj, depVerMin, depVerRev))
                    {
                        AllowNewer = depAllowNewer
                    });
                }

                int resCount = reader.ReadInt32();

                if (compress)
                {
                    using (var decompressStream = new DeflateStream(reader.BaseStream, CompressionMode.Decompress, true))
                    {
                        for (int i = 0; i < resCount; i++)
                        {
                            package._resources.Add(RantResource.DeserializeResource(new EasyReader(decompressStream, true)));
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < resCount; i++)
                    {
                        package._resources.Add(RantResource.DeserializeResource(reader));
                    }
                }

                return(package);
            }
        }