/// <summary>
        /// Extract a File from MPK package
        /// </summary>
        /// <param name="data"></param>
        /// <param name="packageName"></param>
        /// <param name="fileName"></param>
        /// <returns>File Byte Array</returns>
        public static byte[] GetFileDataFromPackage(this FileInfo[] data, string packageName, string fileName)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (string.IsNullOrEmpty(packageName))
            {
                throw new ArgumentNullException("packageName");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            var pack = data.FirstOrDefault(file => file.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase));

            if (pack == null)
            {
                throw new FileNotFoundException("Package could not be found !", packageName);
            }

            var result = new TinyMPK(pack)[fileName];

            if (result == null)
            {
                throw new FileNotFoundException(string.Format("File could not be found in package {0} !", packageName), fileName);
            }

            return(result.Data);
        }
        /// <summary>
        /// Extract a File from given MPK package
        /// </summary>
        /// <param name="data"></param>
        /// <param name="fileName"></param>
        /// <returns>File Byte Array</returns>
        public static byte[] GetFileDataFromPackage(this FileInfo data, string fileName)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (!data.Exists)
            {
                throw new FileNotFoundException("Package could not be found !", data.FullName);
            }

            var result = new TinyMPK(data)[fileName];

            if (result == null)
            {
                throw new FileNotFoundException(string.Format("File could not be found in package {0} !", data.FullName), fileName);
            }

            return(result.Data);
        }
        public void TestMPKConstructor()
        {
            var test = new TinyMPK(ValidMPKPath);

            Assert.AreEqual(1, test.Count);
            Assert.NotNull(test["existing.txt"]);
            Assert.NotNull(test["existing.txt"].Data);
            Assert.Null(test["not existing at all.not"]);
            Assert.AreEqual("existing.txt", test["existing.txt"].Name);

            var expected = System.Text.Encoding.UTF8.GetBytes("Hello World !");

            for (int i = 0; i < test["existing.txt"].Data.Length; i++)
            {
                Assert.AreEqual(expected[i], test["existing.txt"].Data[i]);
            }
        }
		/// <summary>
		/// Try to Retrieve File resources from arbitrary name 
		/// </summary>
		/// <param name="name"></param>
		/// <param name="packageExt"></param>
		/// <param name="fileExt"></param>
		/// <returns>byte array file or null if not found</returns>
		public byte[] SearchRawFileOrPackaged(string name, string[] packageExt, string[] fileExt)
		{
			var file = m_clientFiles.FirstOrDefault(f => f.Name.Substring(0, f.Name.Length - f.Extension.Length).Equals(name, StringComparison.OrdinalIgnoreCase) && fileExt.Any(ext => ext.Equals(f.Extension, StringComparison.OrdinalIgnoreCase)));
			// Found as Raw File
			if (file != null)
				return File.ReadAllBytes(file.FullName);
			
			var packages = m_clientFiles.Where(f => f.Name.Substring(0, f.Name.Length - f.Extension.Length).Equals(name, StringComparison.OrdinalIgnoreCase) && packageExt.Any(ext => ext.Equals(f.Extension, StringComparison.OrdinalIgnoreCase)));
			// Found as Packaged File
			foreach (var package in packages)
			{
				var mpk = new TinyMPK(package);
				var mpkfile = mpk.Select(kv => kv.Key).FirstOrDefault(ent => fileExt.Any(ext => string.Format("{0}{1}", name, ext).Equals(ent, StringComparison.OrdinalIgnoreCase)));
				if (mpkfile != null)
					return mpk[mpkfile].Data;
			}
			
			return null;
		}
Exemple #5
0
        /// <summary>
        /// Opens the specified file readonly
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Stream Open(string file)
        {
            if (GamePath == null)
            {
                return(null);
            }

            string curPath = GamePath;

            string[] components   = file.ToLower().Split('\\');
            string   nifComponent = null;

            foreach (var comp in components)
            {
                if (nifComponent != null)
                {
                    nifComponent = Path.Combine(nifComponent, comp);
                }
                else
                {
                    curPath = Path.Combine(curPath, comp);
                    if (comp.EndsWith(".npk") || comp.EndsWith(".mpk"))
                    {
                        nifComponent = "";
                    }
                }
            }

            // Normal file/NIF?
            if (!File.Exists(curPath))
            {
                return(null);
            }
            if (nifComponent != null)
            {
                var mpk     = TinyMPK.FromFile(curPath);
                var subfile = mpk.GetFile(nifComponent);
                return(subfile == null ? null : new MemoryStream(subfile.Data));
            }
            return(new FileStream(curPath, FileMode.Open, FileAccess.Read));
        }
        /// <summary>
        /// Try to Retrieve File resources from arbitrary name 
        /// </summary>
        /// <param name="nameExt"></param>
        /// <param name="packageExt"></param>
        /// <returns>byte array file or null if not found</returns>
        public byte[] SearchRawFileOrPackaged(string nameExt, string[] packageExt)
        {
            var file = m_clientFiles.FirstOrDefault(f => f.Name.Equals(nameExt, StringComparison.OrdinalIgnoreCase));
            // Found as Raw File
            if (file != null)
                return File.ReadAllBytes(file.FullName);

            // Search package with file name only
            var nameInfo = new FileInfo(nameExt);
            var name = nameInfo.Name.Substring(0, nameInfo.Name.Length - nameInfo.Extension.Length);

            var packages = m_clientFiles.Where(f => f.Name.Substring(0, f.Name.Length - f.Extension.Length).Equals(name, StringComparison.OrdinalIgnoreCase) && packageExt.Any(ext => ext.Equals(f.Extension, StringComparison.OrdinalIgnoreCase)));
            // Found as Packaged File
            foreach (var package in packages)
            {
                var mpk = new TinyMPK(package);
                var mpkfile = mpk.Select(kv => kv.Key).FirstOrDefault(ent => nameExt.Equals(ent, StringComparison.OrdinalIgnoreCase));
                if (mpkfile != null)
                    return mpk[mpkfile].Data;
            }

            return null;
        }
        /// <summary>
        /// Extract a File from MPK package 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="packageName"></param>
        /// <param name="fileName"></param>
        /// <returns>File Byte Array</returns>
        public static byte[] GetFileDataFromPackage(this FileInfo[] data, string packageName, string fileName)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (string.IsNullOrEmpty(packageName))
                throw new ArgumentNullException("packageName");
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");

            var pack = data.FirstOrDefault(file => file.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase));

            if (pack == null)
                throw new FileNotFoundException("Package could not be found !", packageName);

            var result = new TinyMPK(pack)[fileName];

            if (result == null)
                throw new FileNotFoundException(string.Format("File could not be found in package {0} !", packageName), fileName);

            return result.Data;
        }
        /// <summary>
        /// Extract a File from given MPK package 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="fileName"></param>
        /// <returns>File Byte Array</returns>
        public static byte[] GetFileDataFromPackage(this FileInfo data, string fileName)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");

            if (!data.Exists)
                throw new FileNotFoundException("Package could not be found !", data.FullName);

            var result = new TinyMPK(data)[fileName];

            if (result == null)
                throw new FileNotFoundException(string.Format("File could not be found in package {0} !", data.FullName), fileName);

            return result.Data;
        }
        public void TestMPKConstructor()
        {
            var test = new TinyMPK(ValidMPKPath);

            Assert.AreEqual(1, test.Count);
            Assert.NotNull(test["existing.txt"]);
            Assert.NotNull(test["existing.txt"].Data);
            Assert.Null(test["not existing at all.not"]);
            Assert.AreEqual("existing.txt", test["existing.txt"].Name);

            var expected = System.Text.Encoding.UTF8.GetBytes("Hello World !");

            for (int i = 0 ; i < test["existing.txt"].Data.Length ; i++)
                Assert.AreEqual(expected[i], test["existing.txt"].Data[i]);
        }