private void ParseINI(KFN KFN) { KFN.ResourceFile resource = KFN.Resources.Where(r => r.FileName == "Song.ini").First(); byte[] data = KFN.GetDataFromResource(resource); string iniText = new string(Encoding.UTF8.GetChars(data)); SongINI sINI = new SongINI(iniText); iniBlocksView.ItemsSource = sINI.Blocks; this.AutoSizeColumns(iniBlocksView.View as GridView); }
private string GetResourceText(KFN.ResourceFile resource) { byte[] data = KFN.GetDataFromResource(resource); ////UTF-8 int detEncoding = 65001; UniversalDetector Det = new UniversalDetector(null); Det.HandleData(data, 0, data.Length); Det.DataEnd(); string enc = Det.GetDetectedCharset(); if (enc != null && enc != "Not supported") { // fix encoding for 1251 upper case and MAC //if (enc == "KOI8-R" || enc == "X-MAC-CYRILLIC") { enc = "WINDOWS-1251"; } Encoding denc = Encoding.GetEncoding(enc); detEncoding = denc.CodePage; } return(new string(Encoding.GetEncoding(detEncoding).GetChars(data))); }
public void ViewResourceButtonClick(object sender, RoutedEventArgs e) { KFN.ResourceFile resource = resourcesView.SelectedItem as KFN.ResourceFile; if (resource.FileType == "Text") { byte[] data = KFN.GetDataFromResource(resource); ////UTF-8 int detEncoding = 65001; UniversalDetector Det = new UniversalDetector(null); Det.HandleData(data, 0, data.Length); Det.DataEnd(); string enc = Det.GetDetectedCharset(); if (enc != null && enc != "Not supported") { // fix encoding for 1251 upper case and MAC //if (enc == "KOI8-R" || enc == "X-MAC-CYRILLIC") { enc = "WINDOWS-1251"; } Encoding denc = Encoding.GetEncoding(enc); detEncoding = denc.CodePage; } string text = new string(Encoding.GetEncoding(detEncoding).GetChars(data)); Window viewWindow = new ViewWindow( resource.FileName, text, Encoding.GetEncodings().Where(en => en.CodePage == detEncoding).First().DisplayName ); viewWindow.Show(); } else if (resource.FileType == "Image") { byte[] data = KFN.GetDataFromResource(resource); Window viewWindow = new ImageWindow(resource.FileName, data); viewWindow.Show(); } }
public ExportWindow(string exportType, KFN KFN) { InitializeComponent(); WindowElement.Title += exportType; this.KFN = KFN; this.exportType = exportType; videoLabel.Visibility = (exportType == "EMZ") ? Visibility.Visible : Visibility.Hidden; videoSelect.Visibility = (exportType == "EMZ") ? Visibility.Visible : Visibility.Hidden; playVideoButton.Visibility = (exportType == "EMZ") ? Visibility.Visible : Visibility.Hidden; deleteID3Tags.IsChecked = true; deleteID3Tags.Visibility = (exportType == "MP3+LRC") ? Visibility.Visible : Visibility.Hidden; artistLabel.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; titleLabel.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; artistSelect.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; titleSelect.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; encLabel.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; encSelect.Visibility = (exportType != "EMZ") ? Visibility.Visible : Visibility.Hidden; // TODO playVideoButton.IsEnabled = false; playAudioButton.IsEnabled = false; // AUDIO List <KFN.ResourceFile> audios = KFN.Resources.Where(r => r.FileType == "Audio").ToList(); string audioSource = KFN.GetAudioSourceName(); audioSelect.ItemsSource = audios; audioSelect.DisplayMemberPath = "FileName"; audioSelect.SelectedItem = audios.Where(a => a.FileName == audioSource).FirstOrDefault(); if (audioSelect.SelectedItem == null) { System.Windows.MessageBox.Show("Can`t find audio source!"); return; } if (audios.Count == 1) { audioSelect.IsEnabled = false; } // LYRICS Dictionary <string, string> lyrics = new Dictionary <string, string>(); List <KFN.ResourceFile> texts = KFN.Resources.Where(r => r.FileType == "Text").ToList(); foreach (KFN.ResourceFile resource in texts) { lyrics.Add(resource.FileName, this.GetResourceText(resource)); } KFN.ResourceFile songIni = KFN.Resources.Where(r => r.FileName == "Song.ini").First(); byte[] data = KFN.GetDataFromResource(songIni); string iniText = new string(Encoding.UTF8.GetChars(data)); SongINI sINI = new SongINI(iniText); foreach (SongINI.BlockInfo block in sINI.Blocks.Where(b => b.Id == "1" || b.Id == "2")) { string lyricFromBlock = (exportType == "EMZ") ? KFN.INIToELYR(block.Content) : KFN.INIToExtLRC(block.Content); if (lyricFromBlock != null) { lyrics.Add("Song.ini: " + block.Name, lyricFromBlock); } else { lyrics.Add("Song.ini: " + block.Name, "Can`t convert lyric from Song.ini"); } } lyricSelect.DisplayMemberPath = "Key"; lyricSelect.SelectedIndex = 0; if (lyrics.Count == 1) { lyricSelect.IsEnabled = false; } lyricSelect.ItemsSource = lyrics; lyricPreview.Text = ((KeyValuePair <string, string>)lyricSelect.SelectedItem).Value; // ARTIST-TITLE if (exportType == "MP3+LRC") { List <string> artists = new List <string> { null }; List <string> titles = new List <string> { null }; KeyValuePair <string, string> kfnArtist = KFN.Properties.Where(p => p.Key == "Artist").FirstOrDefault(); if (kfnArtist.Value != null && kfnArtist.Value.Length > 0) { artists.Add(kfnArtist.Value); } KeyValuePair <string, string> kfnTitle = KFN.Properties.Where(p => p.Key == "Title").FirstOrDefault(); if (kfnTitle.Value != null && kfnTitle.Value.Length > 0) { titles.Add(kfnTitle.Value); } foreach (KFN.ResourceFile resource in KFN.Resources.Where(r => r.FileType == "Audio")) { string[] atFromID3 = ID3Class.GetArtistAndTitle(KFN.GetDataFromResource(resource)); if (atFromID3[0] != null) { artists.Add(atFromID3[0]); } if (atFromID3[1] != null) { titles.Add(atFromID3[1]); } } artists = artists.Distinct().ToList(); titles = titles.Distinct().ToList(); artistSelect.ItemsSource = artists; artistSelect.SelectedIndex = 0; titleSelect.ItemsSource = titles; titleSelect.SelectedIndex = 0; encSelect.ItemsSource = this.encodings; encSelect.DisplayMemberPath = "Value"; encSelect.SelectedIndex = 0; } // VIDEO if (exportType == "EMZ") { List <KFN.ResourceFile> videos = KFN.Resources.Where(r => r.FileType == "Video").ToList(); if (videos.Count == 0) { videos.Add(new KFN.ResourceFile("Video", "video not found", 0, 0, 0, false)); videoSelect.IsEnabled = false; } else { videos.Add(new KFN.ResourceFile("Video", "don`t use video", 0, 0, 0, false)); } videoSelect.ItemsSource = videos; videoSelect.DisplayMemberPath = "FileName"; videoSelect.SelectedIndex = 0; } }
public void ChangeKFN(List <ResourceFile> resources, bool needDecrypt = false) { this.error = null; string sourceKFNFile = this.fullFileName + ".bak"; File.Copy(this.fullFileName, sourceKFNFile); byte[] sourcePropHeader = new byte[this.endOfPropsOffset]; using (FileStream fs = new FileStream(sourceKFNFile, FileMode.Open, FileAccess.ReadWrite)) { fs.Read(sourcePropHeader, 0, sourcePropHeader.Length); } File.Delete(this.fullFileName); using (FileStream newFile = new FileStream(this.fullFileName, FileMode.Create, FileAccess.ReadWrite)) { newFile.Write(sourcePropHeader, 0, sourcePropHeader.Length); if (needDecrypt) { newFile.Position = 4; byte[] prop = new byte[5]; byte[] propValue = new byte[4]; int maxProps = 40; while (maxProps > 0) { newFile.Read(prop, 0, prop.Length); string propName = new string(Encoding.UTF8.GetChars(new ArraySegment <byte>(prop, 0, 4).ToArray())); if (propName == "ENDH") { newFile.Position += 4; break; } else if (propName == "FLID") { newFile.Read(propValue, 0, propValue.Length); uint valueLength = BitConverter.ToUInt32(propValue, 0); byte[] zeroValue = new byte[valueLength]; newFile.Write(zeroValue, 0, zeroValue.Length); maxProps--; continue; } else if (propName == "RGHT") { byte[] zeroValue = new byte[4]; newFile.Write(zeroValue, 0, zeroValue.Length); maxProps--; continue; } if (prop[4] == 1) { newFile.Position += 4; } else if (prop[4] == 2) { newFile.Read(propValue, 0, propValue.Length); newFile.Position += BitConverter.ToInt32(propValue, 0); } maxProps--; } } byte[] numOfResources = BitConverter.GetBytes(resources.Count); newFile.Write(numOfResources, 0, numOfResources.Length); int nOffset = 0; foreach (ResourceFile resource in resources.OrderBy(r => r.FileOffset)) { byte[] resourceNameLenght = BitConverter.GetBytes(resource.FileName.Length); byte[] resourceLenght = BitConverter.GetBytes(resource.FileLength); byte[] resourceEncryptedLenght = (needDecrypt) ? resourceLenght : BitConverter.GetBytes(resource.EncLength); int encrypted = (resource.IsEncrypted) ? 1 : 0; byte[] resourceEncrypted = (needDecrypt) ? new byte[4] : BitConverter.GetBytes(encrypted); newFile.Write(resourceNameLenght, 0, resourceNameLenght.Length); byte[] resourceName = Encoding.GetEncoding(this.resourceNamesEncodingAuto).GetBytes(resource.FileName); newFile.Write(resourceName, 0, resourceName.Length); byte[] type = BitConverter.GetBytes(this.GetFileTypeId(resource.FileType)); newFile.Write(type, 0, type.Length); newFile.Write(resourceLenght, 0, resourceLenght.Length); byte[] rOffset = BitConverter.GetBytes(nOffset); newFile.Write(rOffset, 0, rOffset.Length); nOffset += (needDecrypt) ? resource.FileLength : resource.EncLength; newFile.Write(resourceEncryptedLenght, 0, resourceEncryptedLenght.Length); newFile.Write(resourceEncrypted, 0, resourceEncrypted.Length); } KFN sourceKFN = new KFN(sourceKFNFile); foreach (ResourceFile resource in resources.OrderBy(r => r.FileOffset)) { byte[] rData = sourceKFN.GetDataFromResource(resource, needDecrypt); newFile.Write(rData, 0, rData.Length); } } }