예제 #1
0
파일: Form1.cs 프로젝트: kolya5544/HSMODS
        public void InstallMod(byte[] Mod)
        {
            ModBinary     modBinary = DispatchModBinary(Mod);
            List <string> toBackup  = new List <string>();

            foreach (FileBinary fb in modBinary.files)
            {
                toBackup.Add(fb.Filename);
            }
            Directory.CreateDirectory(config.GameDir + "/BACKUP");
            List <string> backUp = new List <string>();

            foreach (string s in toBackup)
            {
                if (File.Exists(config.GameDir + s))
                {
                    backUp.Add(s);
                    Directory.CreateDirectory(Path.GetDirectoryName(config.GameDir + "/BACKUP" + s));
                    File.Copy(config.GameDir + s, config.GameDir + "/BACKUP" + s, true);
                }
            }
            if (File.Exists(config.GameDir + "/backup.zip"))
            {
                File.Delete(config.GameDir + "/backup.zip");
            }
            ZipFile.CreateFromDirectory(config.GameDir + "/BACKUP", config.GameDir + "/backup.zip");
            foreach (string s in backUp)
            {
                File.Delete(config.GameDir + "/BACKUP" + s);
            }
            //todo ACTUALLY install mod
            foreach (FileBinary fb in modBinary.files)
            {
                byte[] contents = new byte[] { };
                if (File.Exists(config.GameDir + fb.Filename))
                {
                    contents = File.ReadAllBytes(config.GameDir + fb.Filename);
                }
                else
                {
                    int l = 0;
                    foreach (Block b in fb.blocks)
                    {
                        l += b.Length;
                    }
                    contents = new byte[l];
                }
                foreach (Block b in fb.blocks)
                {
                    Overwrite(contents, b.Offset, b.Data);
                }
                File.WriteAllBytes(config.GameDir + fb.Filename, contents);
            }
            File.WriteAllText(config.GameDir + "/mod", "MOD");
            config.ModInstalled = textBox3.Text;
        }
예제 #2
0
파일: Form1.cs 프로젝트: kolya5544/HSMODS
        private ModBinary DispatchModBinary(byte[] mod)
        {
            List <byte> listByte = mod.ToList();
            int         offset   = 0;
            ModBinary   m        = new ModBinary();

            m.files = new List <FileBinary>();

            while (true)
            {
                FileBinary fb       = new FileBinary();
                string     filename = ReadString(mod, offset);
                offset     += filename.Length + 1;
                fb.Filename = filename;
                if (filename.StartsWith("/") && filename.Contains(".."))
                {
                    throw new Exception("Unsecure mod filenames found.");
                }
                fb.Length = BitConverter.ToInt32(mod, offset);
                offset   += 4;
                fb.blocks = new List <Block>();
                byte[]      Remainder   = listByte.GetRange(offset, fb.Length).ToArray();
                List <byte> listRem     = Remainder.ToList();
                int         BlockOffset = 0;
                while (true)
                {
                    var block = new Block();
                    block.Offset = BitConverter.ToInt32(Remainder, BlockOffset);
                    BlockOffset += 4;
                    block.Length = BitConverter.ToInt16(Remainder, BlockOffset);
                    BlockOffset += 2;
                    block.Data   = listRem.GetRange(BlockOffset, 1024).ToArray();
                    BlockOffset += 1024;
                    fb.blocks.Add(block);
                    if (BlockOffset >= Remainder.Length)
                    {
                        break;
                    }
                }
                offset += BlockOffset;
                m.files.Add(fb);
                if (offset >= mod.Length)
                {
                    break;
                }
            }
            return(m);
        }