Пример #1
0
        public static STBLXMLFile ImportFromPackageFile(string importFilePath, System.Collections.IEnumerable importingLanguages)
        {
            STBLXMLFile importedFile = new STBLXMLFile();


            using (BinaryReader importFileReader = new BinaryReader(new FileStream(importFilePath, FileMode.Open))) {
                string fileIdentifier = Encoding.UTF8.GetString(importFileReader.ReadBytes(4));

                if (fileIdentifier != "STBL")
                {
                    throw new NotPackageSTBLFileException("Invalid STBL file identifier, expected 'STBL'.");
                }

                short version = importFileReader.ReadInt16();

                if (version != 5)
                {
                    throw new NotPackageSTBLFileException("Invalid STBL file version, expected '5'.");
                }

                importFileReader.BaseStream.Seek(1, SeekOrigin.Current);                 // Skip the 'compressed' byte, its not used.
                ulong entryCount = importFileReader.ReadUInt64();

                importFileReader.BaseStream.Seek(6, SeekOrigin.Current);                 // Skip the two 'reserved' bytes and the 'string length' bytes.

                for (ulong entryIndex = 0; entryIndex < entryCount; entryIndex++)
                {
                    uint entryKey = importFileReader.ReadUInt32();
                    importFileReader.BaseStream.Seek(1, SeekOrigin.Current);                     // Skip the flags byte.
                    ushort entryLength = importFileReader.ReadUInt16();
                    string entryText   = Encoding.UTF8.GetString(importFileReader.ReadBytes(entryLength));

                    STBLXMLEntry entry = new STBLXMLEntry()
                    {
                        Key = entryKey
                    };

                    foreach (STBL.Languages importingLanguage in importingLanguages)
                    {
                        entry.SetText(importingLanguage, entryText);
                    }

                    importedFile.Entries.Add(entry);
                }
            }

            return(importedFile);
        }
Пример #2
0
        public static void NewFile()
        {
            RemoveEvents(Data);

            Data = new STBLXMLFile();
            AddEvents(Data);

            FilePath = null;

            Data.IsDirtyable = true;

            for (int entryIndex = 0; entryIndex < Data.Entries.Count; entryIndex++)
            {
                Data.Entries[entryIndex].IsDirtyable = true;
            }

            InvokeFileChangedEvent();
        }
Пример #3
0
        private void FormOkButton_Click(object sender, EventArgs e)
        {
            if (FilePathTextBox.Text == null || string.IsNullOrWhiteSpace(FilePathTextBox.Text))
            {
                ShowNoOrInvalidFileDialog();
                DialogResult = DialogResult.None;
                return;
            }

            if (SelectedLanguages.Count <= 0)
            {
                ShowNoLanguagesSelectedDialog();
                DialogResult = DialogResult.None;
                return;
            }

            STBLXMLFile splitSTBLFile = new STBLXMLFile();

            foreach (STBLXMLEntry splittingEntry in SplittingEntries)
            {
                STBLXMLEntry splitEntry = new STBLXMLEntry()
                {
                    Key        = splittingEntry.Key,
                    Identifier = splittingEntry.Identifier
                };

                foreach (STBL.Languages language in SelectedLanguages)
                {
                    splitEntry.SetText(language, splittingEntry.GetText(language));
                }

                splitSTBLFile.Entries.Add(splitEntry);
            }

            try {
                Tools.WriteXML(FilePathTextBox.Text, splitSTBLFile);
            } catch (Exception saveException) {
                ShowSaveFailureDialog(saveException);
                DialogResult = DialogResult.None;
                return;
            }
        }
Пример #4
0
        public static void ExportToPackageIdentifiersFiles(STBLXMLFile exportingFile, string exportDirectoryPath, bool buildSourceInfoFiles)
        {
            Directory.CreateDirectory(exportDirectoryPath);

            string identifiersFileName = exportingFile.IdentifiersName + "." + IdentifiersFileExtension;
            string identifiersFilePath = Path.Combine(exportDirectoryPath, identifiersFileName);

            string identifiersText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

            identifiersText += "<I n=\"" + SecurityElement.Escape(exportingFile.IdentifiersName) + "\" s=\"" + exportingFile.IdentifiersInstance.ToString("0") + "\" i=\"snippet\" m=\"snippets\" c=\"NeonoceanS4GlobalLanguageIdentifiers\">\n";
            identifiersText += "\t<L n=\"value\">\n";

            for (int entryIndex = 0; entryIndex < exportingFile.Entries.Count; entryIndex++)
            {
                identifiersText += "\t\t<U>\n";
                identifiersText += "\t\t\t<T n=\"key\">" + SecurityElement.Escape(exportingFile.Entries[entryIndex].Identifier) + "</T>\n";
                identifiersText += "\t\t\t<T n=\"value\">" + exportingFile.Entries[entryIndex].Key.ToString("0") + "</T>\n";
                identifiersText += "\t\t</U>\n";
            }

            identifiersText += "\t</L>\n</I>";

            using (StreamWriter writer = new StreamWriter(identifiersFilePath)) {
                writer.Write(identifiersText);
            }

            if (buildSourceInfoFiles)
            {
                string sourceInfoFilePath = identifiersFilePath + "." + SourceInfoFileExtension;

                Tools.WriteXML(sourceInfoFilePath, new SourceInfo()
                {
                    Name       = exportingFile.IdentifiersName,
                    TypeID     = 2113017500,
                    GroupID    = exportingFile.IdentifiersGroup,
                    InstanceID = exportingFile.IdentifiersInstance
                });
            }
        }
Пример #5
0
        public MetaData(STBLXMLFile stblFile = null)
        {
            if (stblFile != null)
            {
                STBLFile = stblFile;
            }

            InitializeComponent();

            Forms.SetupLanguageComboBox(FallbackLanguageSelector, STBL.GetLanguage(STBLFile.FallbackLanguage));

            LastSTBLGroupValue               = STBLFile.STBLGroup;
            STBLGroupDecimalTextBox.Text     = STBLFile.STBLGroup.ToString("0");
            STBLGroupHexadecimalTextBox.Text = STBLFile.STBLGroup.ToString("x").ToUpper();

            LastSTBLInstanceValue               = STBLFile.STBLInstance;
            STBLInstanceDecimalTextBox.Text     = STBLFile.STBLInstance.ToString("0");
            STBLInstanceHexadecimalTextBox.Text = STBLFile.STBLInstance.ToString("x").ToUpper();

            STBLNameTextBox.Text = STBLFile.STBLName;

            if (STBLFile.STBLName == "")
            {
                UpdateSTBLNameTextBoxColors(STBLNameTextBox);
            }

            BuildIdentifiersCheckBox.Checked = STBLFile.BuildIdentifiers;

            LastIdentifiersGroupValue               = STBLFile.IdentifiersGroup;
            IdentifiersGroupDecimalTextBox.Text     = STBLFile.IdentifiersGroup.ToString("0");
            IdentifiersGroupHexadecimalTextBox.Text = STBLFile.IdentifiersGroup.ToString("x").ToUpper();

            LastIdentifiersInstanceValue               = STBLFile.IdentifiersInstance;
            IdentifiersInstanceDecimalTextBox.Text     = STBLFile.IdentifiersInstance.ToString("0");
            IdentifiersInstanceHexadecimalTextBox.Text = STBLFile.IdentifiersInstance.ToString("x").ToUpper();

            IdentifiersNameTextBox.Text = STBLFile.IdentifiersName;
        }
Пример #6
0
        private void FormOkButton_Click(object sender, EventArgs e)
        {
            if (FilePathTextBox.Text == null || string.IsNullOrWhiteSpace(FilePathTextBox.Text) || !File.Exists(FilePathTextBox.Text))
            {
                ShowNoOrInvalidFileDialog();
                DialogResult = DialogResult.None;
                return;
            }

            if (SelectedLanguages.Count <= 0)
            {
                ShowNoLanguagesSelectedDialog();
                DialogResult = DialogResult.None;
                return;
            }

            STBLXMLFile mergingFile = null;

            try {
                mergingFile = ImportExport.ImportFromPackageFile(FilePathTextBox.Text, SelectedLanguages);
            } catch (Exception readException) {
                ShowOpenFailureDialog(readException);
                DialogResult = DialogResult.None;
                return;
            }

            HashSet <uint> existingEntryKeys = new HashSet <uint>();

            if (CreateMissingEntriesCheckBox.Checked)
            {
                foreach (STBLXMLEntry existingEntry in Loading.Data.Entries)
                {
                    existingEntryKeys.Add(existingEntry.Key);
                }
            }

            Dictionary <uint, Dictionary <STBL.Languages, string> > mergingLanguagesKey = new Dictionary <uint, Dictionary <STBL.Languages, string> >();

            foreach (STBLXMLEntry mergingEntry in mergingFile.Entries)
            {
                string mergingEntryIdentifier = mergingEntry.Identifier;

                if (CreateMissingEntriesCheckBox.Checked)
                {
                    if (!existingEntryKeys.Contains(mergingEntry.Key))
                    {
                        Loading.AddNewEntry(key: mergingEntry.Key, identifier: mergingEntry.Identifier);
                        existingEntryKeys.Add(mergingEntry.Key);
                    }
                }

                if (!mergingLanguagesKey.ContainsKey(mergingEntry.Key))
                {
                    mergingLanguagesKey[mergingEntry.Key] = new Dictionary <STBL.Languages, string>();
                }

                foreach (STBL.Languages language in SelectedLanguages)
                {
                    string mergingEntryLanguageText = mergingEntry.GetText(language);

                    if (mergingEntryLanguageText == null)
                    {
                        continue;
                    }

                    mergingLanguagesKey[mergingEntry.Key][language] = mergingEntryLanguageText;
                }
            }

            List <uint> unmergedKeys = mergingLanguagesKey.Keys.ToList();

            foreach (STBLXMLEntry existingEntry in Loading.Data.Entries)
            {
                if (!mergingLanguagesKey.ContainsKey(existingEntry.Key))
                {
                    continue;
                }

                foreach (KeyValuePair <STBL.Languages, string> languageTextPair in mergingLanguagesKey[existingEntry.Key])
                {
                    if (TextPriorityExistingRadioButton.Checked)
                    {
                        if (existingEntry.HasText(languageTextPair.Key))
                        {
                            continue;
                        }

                        existingEntry.SetText(languageTextPair.Key, languageTextPair.Value);
                    }
                    else
                    {
                        existingEntry.SetText(languageTextPair.Key, languageTextPair.Value);
                    }
                }
            }
        }
Пример #7
0
        public static void ExportToPackageFiles(STBLXMLFile exportingFile, string exportDirectoryPath, bool buildSourceInfoFiles)
        {
            Directory.CreateDirectory(exportDirectoryPath);

            STBL.Languages fallbackLanguage = STBL.GetLanguage(exportingFile.FallbackLanguage);

            foreach (STBL.Languages language in STBL.GetAllLanguages())
            {
                string languageFileName = string.Format(exportingFile.STBLName, new STBL.LanguageFileName(language));

                string stblFileName = languageFileName + "." + STBLFileExtension;
                string stblFilePath = Path.Combine(exportDirectoryPath, stblFileName);

                using (BinaryWriter exportFileWriter = new BinaryWriter(new FileStream(stblFilePath, FileMode.Create))) {
                    string[] texts           = new string[exportingFile.Entries.Count];
                    ushort[] textsByteCounts = new ushort[exportingFile.Entries.Count];
                    uint     entryByteCount  = 0;

                    for (int entryIndex = 0; entryIndex < exportingFile.Entries.Count; entryIndex++)
                    {
                        string entryLanguageText = exportingFile.Entries[entryIndex].GetText(language);

                        if (entryLanguageText == null)
                        {
                            entryLanguageText = exportingFile.Entries[entryIndex].GetText(fallbackLanguage);
                        }

                        if (entryLanguageText == null)
                        {
                            entryLanguageText = exportingFile.Entries[entryIndex].Identifier;
                        }

                        texts[entryIndex] = entryLanguageText;

                        textsByteCounts[entryIndex] = (ushort)Encoding.UTF8.GetByteCount(texts[entryIndex]);
                        entryByteCount += textsByteCounts[entryIndex] + 1u;
                    }

                    exportFileWriter.Write(Encoding.UTF8.GetBytes("STBL"));
                    exportFileWriter.Write((byte)5);
                    exportFileWriter.Write((ushort)0);
                    exportFileWriter.Write((uint)exportingFile.Entries.Count);
                    exportFileWriter.Write(0u);
                    exportFileWriter.Write((ushort)0);
                    exportFileWriter.Write(entryByteCount);

                    for (int textsIndex = 0; textsIndex < exportingFile.Entries.Count; textsIndex++)
                    {
                        exportFileWriter.Write(exportingFile.Entries[textsIndex].Key);
                        exportFileWriter.Write((byte)0);
                        exportFileWriter.Write(textsByteCounts[textsIndex]);
                        exportFileWriter.Write(texts[textsIndex].ToCharArray());
                    }
                }

                if (buildSourceInfoFiles)
                {
                    string sourceInfoFilePath = stblFilePath + "." + SourceInfoFileExtension;

                    string languageInstanceHexadecimal = ((int)language).ToString("x2") + exportingFile.STBLInstance.ToString("x").Substring(2);
                    ulong  languageInstance            = ulong.Parse(languageInstanceHexadecimal, NumberStyles.HexNumber);

                    Tools.WriteXML(sourceInfoFilePath, new SourceInfo()
                    {
                        Name       = languageFileName,
                        TypeID     = 570775514,
                        GroupID    = exportingFile.STBLGroup,
                        InstanceID = languageInstance
                    });
                }
            }
        }
Пример #8
0
 private static void RemoveEvents(STBLXMLFile file)
 {
     file.BecameClean -= Data_BecameClean;
     file.BecameDirty -= Data_BecameDirty;
 }
Пример #9
0
 private static void AddEvents(STBLXMLFile file)
 {
     file.BecameClean += Data_BecameClean;
     file.BecameDirty += Data_BecameDirty;
 }