コード例 #1
0
 public override void ReplaceFile(EdataContentFile oldFile, byte[] newContent)
 {
     if (LoadedFiles.ContainsKey(oldFile))
         LoadedFiles[oldFile] = newContent;
     else
         LoadedFiles.Add(oldFile, newContent);
 }
コード例 #2
0
ファイル: EdataPackage.cs プロジェクト: zawecha1/moddingSuite
 public override void ReplaceFile(EdataContentFile oldFile, byte[] newContent)
 {
     if (LoadedFiles.ContainsKey(oldFile))
     {
         LoadedFiles[oldFile] = newContent;
     }
     else
     {
         LoadedFiles.Add(oldFile, newContent);
     }
 }
コード例 #3
0
        public ScenarioEditorViewModel(EdataContentFile file, EdataFileViewModel ownerVm)
        {
            OwnerFile = file;
            EdataFileViewModel = ownerVm;

            var reader = new ScenarioReader();
            ScenarioFile = reader.Read(ownerVm.EdataManager.GetRawData(file));

            EditGameModeLogicCommand = new ActionCommand(EditGameModeLogicExecute);
            ZoneEditorCommand = new ActionCommand(ZoneEditorExecute);
            SaveCommand = new ActionCommand(SaveExecute);
        }
コード例 #4
0
        public NdfEditorMainViewModel(EdataContentFile contentFile, EdataFileViewModel ownerVm)
        {
            OwnerFile = contentFile;
            EdataFileViewModel = ownerVm;

            var ndfbinReader = new NdfbinReader();
            NdfBinary = ndfbinReader.Read(ownerVm.EdataManager.GetRawData(contentFile));

            //var ndfbinManager = new NdfbinManager(ownerVm.EdataManager.GetRawData(contentFile));
            //NdfbinManager = ndfbinManager;

            //ndfbinManager.Initialize();

            InitializeNdfEditor();
        }
コード例 #5
0
        public TradFileViewModel(EdataContentFile owner, EdataFileViewModel contentFile)
        {
            SaveCommand = new ActionCommand(SaveCommandExecute);
            CreateHashCommand = new ActionCommand(CreateHashExecute, CreateHashCanExecute);
            AddEntryCommand = new ActionCommand(AddEntryExecute);
            RemoveEntryCommand = new ActionCommand(RemoveEntryExecute);

            OwnerFile = owner;
            OwnerVm = contentFile;

            Manager = new TradManager(OwnerVm.EdataManager.GetRawData(OwnerFile));

            Entries = Manager.Entries;

            TitleText = string.Format("Dictionary editor [{0}]", OwnerFile.Path);
        }
コード例 #6
0
        public byte[] GetRawData(EdataContentFile ofFile, bool cacheResult = true)
        {
            byte[] rawData;

            if (LoadedFiles.ContainsKey(ofFile))
            {
                rawData = LoadedFiles[ofFile];
                ofFile.Checksum = MD5.Create().ComputeHash(rawData);
            }
            else
            {
                rawData = base.GetRawData(ofFile);
                if (cacheResult)
                    LoadedFiles.Add(ofFile, rawData);
            }

            return rawData;
        }
コード例 #7
0
        public int GetFileSize(EdataContentFile file)
        {
            int size = 0;
            // GroupSize
            size += 4;
            // Entry Size
            size += 4;
            // Zero terminated string
            size += file.Name.Length + 1;

            if (size % 2 == 1)
                size++;

            size += 8;
            size += 8;
            size += 16;

            return size;
        }
コード例 #8
0
ファイル: EdataPackage.cs プロジェクト: zawecha1/moddingSuite
        public byte[] GetRawData(EdataContentFile ofFile, bool cacheResult = true)
        {
            byte[] rawData;

            if (LoadedFiles.ContainsKey(ofFile))
            {
                rawData         = LoadedFiles[ofFile];
                ofFile.Checksum = MD5.Create().ComputeHash(rawData);
            }
            else
            {
                rawData = base.GetRawData(ofFile);
                if (cacheResult)
                {
                    LoadedFiles.Add(ofFile, rawData);
                }
            }

            return(rawData);
        }
コード例 #9
0
        private void SaveExecute(object obj)
        {
            if (zoneEditor != null)
            {
                zoneEditor.Save();
            }
            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
            Action<string> report = msg => StatusText = msg;

            var s = new Task(() =>
                {
                    try
                    {
                        dispatcher.Invoke(() => IsUIBusy = true);
                        dispatcher.Invoke(report, string.Format("Saving back changes..."));

                        var writer = new ScenarioWriter();
                        byte[] newFile = writer.Write(ScenarioFile);
                        dispatcher.Invoke(report, string.Format("Recompiling of {0} finished! ", EdataFileViewModel.EdataManager.FilePath));

                        EdataFileViewModel.EdataManager.ReplaceFile(OwnerFile, newFile);
                        dispatcher.Invoke(report, "Replacing new File in edata finished!");

                        EdataFileViewModel.LoadFile(EdataFileViewModel.LoadedFile);

                        EdataContentFile newOwen = EdataFileViewModel.EdataManager.Files.Single(x => x.Path == OwnerFile.Path);

                        OwnerFile = newOwen;
                        dispatcher.Invoke(report, string.Format("Saving of changes finished! {0}", EdataFileViewModel.EdataManager.FilePath));
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(string.Format("Error while saving scenario file: {0}", ex));
                        dispatcher.Invoke(report, "Saving interrupted - Did you start Wargame before I was ready?");
                    }
                    finally
                    {
                        dispatcher.Invoke(() => IsUIBusy = false);
                    }
                });
            s.Start();
        }
コード例 #10
0
        protected void ResolveFileType(FileStream fs, EdataContentFile file)
        {
            // save original offset
            long origOffset = fs.Position;

            fs.Seek(file.Offset + Header.FileOffset, SeekOrigin.Begin);

            var headerBuffer = new byte[12];
            fs.Read(headerBuffer, 0, headerBuffer.Length);

            file.FileType = GetFileTypeFromHeaderData(headerBuffer);

            // set offset back to original
            fs.Seek(origOffset, SeekOrigin.Begin);
        }
コード例 #11
0
        /// <summary>
        /// Replaces a file and rebuilds the Edata File with
        /// </summary>
        /// <param name="oldFile">The EdataFile object which is to be replaced.</param>
        /// <param name="newContent">The data of the new File including Header and content.</param>
        /// <returns>The data of the completly rebuilt EdataFile. This has to be saved back to the file.</returns>
        protected string ReplaceRebuildV2(EdataContentFile oldFile, byte[] newContent)
        {
            var reserveBuffer = new byte[200];

            var tmp = new FileInfo(FilePath);

            var tmpPath = Path.Combine(tmp.DirectoryName, string.Format("{0}_{1}", tmp.FullName, "temp"));

            if (!File.Exists(tmpPath))
                using (File.Create(tmpPath)) { }

            using (var fs = new FileStream(FilePath, FileMode.Open))
            {
                using (var newFile = new FileStream(tmpPath, FileMode.Truncate))
                {
                    var headerPart = new byte[Header.FileOffset];
                    fs.Read(headerPart, 0, headerPart.Length);
                    newFile.Write(headerPart, 0, headerPart.Length);

                    fs.Seek(Header.FileOffset, SeekOrigin.Begin);

                    uint filesContentLength = 0;
                    byte[] fileBuffer;

                    foreach (EdataContentFile file in Files)
                    {
                        long oldOffset = file.Offset;
                        file.Offset = newFile.Position - Header.FileOffset;

                        if (file == oldFile)
                        {
                            fileBuffer = newContent;
                            file.Size = newContent.Length;
                            file.Checksum = MD5.Create().ComputeHash(fileBuffer);
                        }
                        else
                        {
                            fileBuffer = new byte[file.Size];
                            fs.Seek(oldOffset + Header.FileOffset, SeekOrigin.Begin);
                            fs.Read(fileBuffer, 0, fileBuffer.Length);
                        }

                        newFile.Write(fileBuffer, 0, fileBuffer.Length);
                        newFile.Write(reserveBuffer, 0, reserveBuffer.Length);

                        filesContentLength += (uint)fileBuffer.Length + (uint)reserveBuffer.Length;
                    }

                    newFile.Seek(0x25, SeekOrigin.Begin);
                    newFile.Write(BitConverter.GetBytes(filesContentLength), 0, 4);

                    newFile.Seek(Header.DictOffset, SeekOrigin.Begin);
                    long dirEnd = Header.DictOffset + Header.DictLength;
                    uint id = 0;

                    while (newFile.Position < dirEnd)
                    {
                        var buffer = new byte[4];
                        newFile.Read(buffer, 0, 4);
                        int fileGroupId = BitConverter.ToInt32(buffer, 0);

                        if (fileGroupId == 0)
                        {
                            EdataContentFile curFile = Files[(int)id];

                            // FileEntrySize
                            newFile.Seek(4, SeekOrigin.Current);

                            buffer = BitConverter.GetBytes(curFile.Offset);
                            newFile.Write(buffer, 0, buffer.Length);

                            buffer = BitConverter.GetBytes(curFile.Size);
                            newFile.Write(buffer, 0, buffer.Length);

                            byte[] checkSum = curFile.Checksum;
                            newFile.Write(checkSum, 0, checkSum.Length);

                            string name = Utils.ReadString(newFile);

                            if ((name.Length + 1) % 2 == 1)
                                newFile.Seek(1, SeekOrigin.Current);

                            id++;
                        }
                        else if (fileGroupId > 0)
                        {
                            newFile.Seek(4, SeekOrigin.Current);
                            string name = Utils.ReadString(newFile);

                            if ((name.Length + 1) % 2 == 1)
                                newFile.Seek(1, SeekOrigin.Current);
                        }
                    }

                    newFile.Seek(Header.DictOffset, SeekOrigin.Begin);
                    var dirBuffer = new byte[Header.DictLength];
                    newFile.Read(dirBuffer, 0, dirBuffer.Length);

                    byte[] dirCheckSum = MD5.Create().ComputeHash(dirBuffer);

                    newFile.Seek(0x31, SeekOrigin.Begin);

                    newFile.Write(dirCheckSum, 0, dirCheckSum.Length);
                }
            }

            return tmpPath;
        }
コード例 #12
0
        /// <summary>
        /// The only tricky part about that algorythm is that you have to skip one byte if the length of the File/Dir name PLUS nullbyte is an odd number.
        /// </summary>
        /// <returns>A Collection of the Files found in the Dictionary</returns>
        protected ObservableCollection<EdataContentFile> ReadEdatV2Dictionary()
        {
            var files = new ObservableCollection<EdataContentFile>();
            var dirs = new List<EdataDir>();
            var endings = new List<long>();

            using (FileStream fileStream = File.Open(FilePath, FileMode.Open))
            {
                fileStream.Seek(Header.DictOffset, SeekOrigin.Begin);

                long dirEnd = Header.DictOffset + Header.DictLength;
                uint id = 0;

                while (fileStream.Position < dirEnd)
                {
                    var buffer = new byte[4];
                    fileStream.Read(buffer, 0, 4);
                    int fileGroupId = BitConverter.ToInt32(buffer, 0);

                    if (fileGroupId == 0)
                    {
                        var file = new EdataContentFile();
                        fileStream.Read(buffer, 0, 4);
                        file.FileEntrySize = BitConverter.ToInt32(buffer, 0);

                        buffer = new byte[8];
                        fileStream.Read(buffer, 0, buffer.Length);
                        file.Offset = BitConverter.ToInt64(buffer, 0);

                        fileStream.Read(buffer, 0, buffer.Length);
                        file.Size = BitConverter.ToInt64(buffer, 0);

                        var checkSum = new byte[16];
                        fileStream.Read(checkSum, 0, checkSum.Length);
                        file.Checksum = checkSum;

                        file.Name = Utils.ReadString(fileStream);
                        file.Path = MergePath(dirs, file.Name);

                        if (file.Name.Length % 2 == 0)
                            fileStream.Seek(1, SeekOrigin.Current);

                        file.Id = id;
                        id++;

                        ResolveFileType(fileStream, file);

                        files.Add(file);

                        while (endings.Count > 0 && fileStream.Position == endings.Last())
                        {
                            dirs.Remove(dirs.Last());
                            endings.Remove(endings.Last());
                        }
                    }
                    else if (fileGroupId > 0)
                    {
                        var dir = new EdataDir();

                        fileStream.Read(buffer, 0, 4);
                        dir.FileEntrySize = BitConverter.ToInt32(buffer, 0);

                        if (dir.FileEntrySize != 0)
                            endings.Add(dir.FileEntrySize + fileStream.Position - 8);
                        else if (endings.Count > 0)
                            endings.Add(endings.Last());

                        dir.Name = Utils.ReadString(fileStream);

                        if (dir.Name.Length % 2 == 0)
                            fileStream.Seek(1, SeekOrigin.Current);

                        dirs.Add(dir);
                    }
                }
            }
            return files;
        }
コード例 #13
0
        /// <summary>
        /// Replaces a file in the current Edata package and saves the changes back.
        /// </summary>
        /// <param name="oldFile">The EdataFile object which is to be replaced.</param>
        /// <param name="newContent">The data of the new File including Header and content.</param>
        public virtual void ReplaceFile(EdataContentFile oldFile, byte[] newContent)
        {
            if (!File.Exists(FilePath))
                throw new InvalidOperationException("The Edata file does not exist anymore.");

            string newFile;

            switch (Header.Version)
            {
                case 1:
                    newFile = ReplaceRebuildV1(oldFile, newContent);
                    break;
                case 2:
                    newFile = ReplaceRebuildV2(oldFile, newContent);
                    break;
                default:
                    throw new NotSupportedException(string.Format("Edata Version {0} is currently not supported", Header.Version));
            }

            var oldFileInfo = new FileInfo(FilePath);

            var backupFile = Path.Combine(oldFileInfo.DirectoryName, "to_delete.dat");

            File.Move(FilePath, backupFile);
            File.Move(newFile, FilePath);

            File.Delete(backupFile);
        }
コード例 #14
0
        /// <summary>
        /// Reads the raw data of a file inside the current package.
        /// </summary>
        /// <param name="ofFile">A EdataFile of the current manager</param>
        /// <returns>The data of the desired EdataFile.</returns>
        public virtual byte[] GetRawData(EdataContentFile ofFile)
        {
            //if (ofFile.Manager != this)
            //    throw new ArgumentException("ofFile must be created by this instance of EdataManager");

            byte[] buffer;

            using (FileStream fs = File.Open(FilePath, FileMode.Open))
            {
                long offset = Header.FileOffset + ofFile.Offset;
                fs.Seek(offset, SeekOrigin.Begin);

                buffer = new byte[ofFile.Size];
                fs.Read(buffer, 0, buffer.Length);
            }

            return buffer;
        }