private void AnalyzePackage()
        {
            mInstance = UInt64.MaxValue;
            bool haveiid         = false;
            bool havemultipleiid = false;

            mHighestPriority = UInt32.MaxValue;
            Dictionary <string, bool> sourcelist = new Dictionary <string, bool>();

            for (int loop = 0; loop < (int)Languages.Language_Count; loop++)
            {
                foreach (KeyValuePair <UInt64, StubbleEntry> entry in mData.GetEntriesWithSourceForLanguage((Languages)loop, STBLVault.StringSource.Modified | STBLVault.StringSource.Loaded))
                {
                    if (entry.Value.Source.SourceTable == DBPF.DBPFReference.MinValue)
                    {
                        continue;
                    }
                    mHighestPriority = Math.Min(mHighestPriority, (UInt32)entry.Value.Source.SourceTable.Instance);
                    if (!havemultipleiid)
                    {
                        UInt64 thisinstance = entry.Value.Source.SourceTable.Instance & ~(0xffUL << 56);
                        if (haveiid)
                        {
                            if (mInstance != thisinstance)
                            {
                                havemultipleiid = true;
                                mInstance       = UInt64.MaxValue;
                            }
                        }
                        else
                        {
                            mInstance = thisinstance;
                        }
                    }
                    if (!string.IsNullOrEmpty(entry.Value.Source.SourceFile))
                    {
                        sourcelist[entry.Value.Source.SourceFile] = true;
                    }
                }
            }
            if (mInstance != UInt64.MaxValue)
            {
                foreach (string origpackage in STBLVault.GetAllPackageFilenames())
                {
                    if (sourcelist.ContainsKey(origpackage))
                    {
                        mInstance = UInt64.MaxValue;
                        break;
                    }
                }
            }
        }
        public IEnumerable <KeyValuePair <UInt64, string> > ImportStrings(IWin32Window owner, Stream file)
        {
            StreamReader sr = new StreamReader(file);

            using (sr)
            {
                string line;
                char   sepchar = '\t';
                if (whiteSpaceDelim.Checked)
                {
                    sepchar = '\0';
                }
                else if (otherDelim.Checked && textBox2.Text.Length > 0)
                {
                    sepchar = textBox2.Text[0];
                }
                bool           hexnum       = hexNums.Checked;
                bool           quotestrings = quoteStrings.Checked;
                bool           quoteall     = quoteAll.CanFocus;
                bool           keyids       = (idNumber.Checked || idDetect.Checked);
                bool           keynames     = (idKey.Checked || idDetect.Checked);
                StringBuilder  keybuilder   = new StringBuilder();
                int            curline      = 0;
                Queue <string> context      = new Queue <string>();
                while ((line = sr.ReadLine()) != null)
                {
                    context.Enqueue(line);
                    if (context.Count > 3)
                    {
                        context.Dequeue();
                    }
                    curline++;
                    line = line.Trim();
                    if (line == string.Empty)
                    {
                        continue;
                    }
                    int idx1 = -1;
                    int idx2 = -1;
                    if (sepchar != '\0')
                    {
                        idx1 = line.IndexOf(sepchar);
                    }
                    else
                    {
                        for (idx1 = 0; idx1 < line.Length && !char.IsWhiteSpace(line[idx1]); idx1++)
                        {
                            ;
                        }
                    }
                    if (quotestrings && keynames || quoteall)
                    {
                        idx2 = line.IndexOf('"');
                    }

                    if (idx1 < 0 || idx1 == line.Length)
                    {
                        MessageBox.Show(owner, string.Format("Invalid line in text file:\n{0}", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        yield break;
                    }

                    bool   wasquoted = false;
                    string key;
                    if ((quoteall || quotestrings && keynames) && idx2 >= 0 && idx2 < idx1)
                    {
                        wasquoted = true;
                        int curidx = idx2 + 1;
                        int idx3   = line.IndexOf('\\', curidx);
                        int idx4   = line.IndexOf('"', curidx);
                        if (idx3 >= 0 && idx3 < idx4)
                        {
                            keybuilder.Length = 0;
                            while (idx3 >= 0 && idx3 < idx4)
                            {
                                keybuilder.Append(line.Substring(curidx, idx3 - curidx));
                                curidx = idx3 + 1;
                                idx3   = line.IndexOf('\\', curidx + 1);
                                if (idx4 == curidx)
                                {
                                    idx4 = line.IndexOf('"', curidx + 1);
                                }
                            }
                            if (idx4 < 0)
                            {
                                MessageBox.Show(owner, string.Format("Invalid line in text file:\n{0}", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                yield break;
                            }
                            keybuilder.Append(line.Substring(curidx, idx4 - curidx));
                            key = keybuilder.ToString();
                        }
                        else
                        {
                            if (idx4 < 0)
                            {
                                MessageBox.Show(owner, string.Format("Invalid line in text file:\n{0}", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                yield break;
                            }
                            key = line.Substring(curidx, idx4 - curidx);
                        }
                        if (sepchar != '\0')
                        {
                            idx1 = line.IndexOf(sepchar, idx4 + 1);
                        }
                        else
                        {
                            for (idx1 = idx4 + 1; idx1 < line.Length && !char.IsWhiteSpace(line[idx1]); idx1++)
                            {
                                ;
                            }
                        }
                        if (idx1 < 0 || idx1 >= line.Length)
                        {
                            MessageBox.Show(owner, string.Format("Invalid line in text file:\n{0}", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            yield break;
                        }
                    }
                    else
                    {
                        key = line.Substring(0, idx1);
                    }
                    string value;
                    if (quotestrings)
                    {
                        int idx3 = line.IndexOf('"', idx1 + 1);
                        if (idx3 < 0)
                        {
                            MessageBox.Show(owner, string.Format("Strings must be quoted:\n{0}", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            yield break;
                        }
                        int curidx = idx3 + 1;
                        idx3 = line.IndexOf('"', curidx);
                        int           idx4     = line.IndexOf('\\', curidx);
                        StringBuilder valuestr = new StringBuilder();
                        while (idx3 < 0 || idx4 >= 0 && idx4 < idx3)
                        {
                            if (idx4 > 0)
                            {
                                valuestr.Append(line.Substring(curidx, idx4 - curidx));
                                curidx = idx4 + 1;
                                idx4   = line.IndexOf('\\', curidx + 1);
                                if (curidx == idx3)
                                {
                                    idx3 = line.IndexOf('"', curidx + 1);
                                }
                            }
                            else
                            {
                                valuestr.AppendLine(line.Substring(curidx));
                                curidx = 0;
                                line   = sr.ReadLine();
                                context.Enqueue(line);
                                if (context.Count > 3)
                                {
                                    context.Dequeue();
                                }
                                curline++;
                                if (line == null)
                                {
                                    MessageBox.Show(owner, string.Format("End of file looking for end of string quote", FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    yield break;
                                }
                                curidx = 0;
                                idx4   = line.IndexOf('\\');
                                idx3   = line.IndexOf('"');
                            }
                        }
                        valuestr.Append(line.Substring(curidx, idx3 - curidx));
                        value = valuestr.ToString();
                    }
                    else
                    {
                        value = line.Substring(idx1 + 1);
                    }

                    UInt64 id = 0;
                    if (!keyids)
                    {
                        id = STBLVault.FNV64(key);
                        if (STBLVault.LookupKey(id) == null)
                        {
                            STBLVault.AddNewKey(key);
                        }
                    }
                    else if (!keynames)
                    {
                        bool   ishex    = hexnum;
                        string parsekey = key.ToLower();
                        if (parsekey.StartsWith("0x"))
                        {
                            ishex    = true;
                            parsekey = parsekey.Substring(2);
                        }
                        if (ishex && !UInt64.TryParse(key, System.Globalization.NumberStyles.HexNumber, null, out id) ||
                            !ishex && !UInt64.TryParse(key, out id))
                        {
                            MessageBox.Show(owner, string.Format("Error parsing key ID {0}\n{1}", key, FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            yield break;
                        }
                    }
                    else
                    {
                        if (!quoteall && wasquoted)
                        {
                            id = STBLVault.FNV64(key);
                            if (STBLVault.LookupKey(id) == null)
                            {
                                STBLVault.AddNewKey(key);
                            }
                        }
                        else
                        {
                            bool   ishex    = hexnum || (!quoteall && !wasquoted && quotestrings);
                            string parsekey = key.ToLower();
                            if (parsekey.StartsWith("0x"))
                            {
                                ishex    = true;
                                parsekey = parsekey.Substring(2);
                            }
                            if (ishex && !UInt64.TryParse(parsekey, System.Globalization.NumberStyles.HexNumber, null, out id) ||
                                !ishex && !UInt64.TryParse(parsekey, out id))
                            {
                                if (quotestrings && wasquoted)
                                {
                                    MessageBox.Show(owner, string.Format("Error parsing key ID {0}\n{1}", key, FormatContext(context, curline, sr)), "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    yield break;
                                }
                                id = STBLVault.FNV64(key);
                                if (STBLVault.LookupKey(id) == null)
                                {
                                    STBLVault.AddNewKey(key);
                                }
                            }
                        }
                    }

                    yield return(new KeyValuePair <UInt64, string>(id, value));
                }
            }
        }
Exemple #3
0
        public void Export(string filename, Languages lang)
        {
            StringBuilder line = new StringBuilder();

            System.IO.StreamWriter writer = new System.IO.StreamWriter(filename, false, Encoding.UTF8);
            bool quotestrings             = quoteStrings.Checked;
            bool quoteall = quoteAll.Checked;
            bool nonames  = idNumber.Checked;
            bool barehex  = hexNums.Checked;
            char seperator;

            if (tabDelim.Checked || textBox2.Text.Length < 1)
            {
                seperator = '\t';
            }
            else
            {
                seperator = textBox2.Text[0];
            }
            using (writer)
            {
                foreach (KeyValuePair <UInt64, string> entry in mData.GetEntriesForLanguage(lang, STBLVault.StringSource.Loaded | STBLVault.StringSource.Modified))
                {
                    bool   keydone = false;
                    string keyname;
                    if (!nonames && STBLVault.LookupKey(entry.Key, out keyname))
                    {
                        if (keyname.IndexOf(seperator) < 0 && keyname.IndexOf('\n') < 0)
                        {
                            if (quotestrings)
                            {
                                writer.Write('"');
                                QuoteString(writer, keyname);
                                writer.Write('"');
                            }
                            else
                            {
                                writer.Write(keyname);
                            }
                            keydone = true;
                        }
                    }
                    if (!keydone)
                    {
                        if (quoteall)
                        {
                            writer.Write('"');
                        }
                        if (!barehex)
                        {
                            writer.Write("0x");
                        }
                        writer.Write(entry.Key.ToString("X16"));
                        if (quoteall)
                        {
                            writer.Write('"');
                        }
                    }
                    writer.Write(seperator);
                    if (quotestrings)
                    {
                        writer.Write('"');
                        QuoteString(writer, entry.Value);
                        writer.WriteLine('"');
                    }
                    else
                    {
                        NonQuoteString(writer, entry.Value);
                        writer.WriteLine();
                    }
                }
            }
        }
        private void GatherSaveData(DBPFFile dbfile, Dictionary <DBPFReference, KeyValuePair <DBPFIndexEntry, byte[]> > toreplace, List <KeyValuePair <DBPFIndexEntry, byte[]> > toadd)
        {
            STBLWriter copysource = null;

            // Copy the reference language entries
            if (checkBox2.Checked)
            {
                copysource = new STBLWriter();
                foreach (KeyValuePair <UInt64, string> entry in mData.GetEntriesForLanguage((Languages)comboBox1.Items[comboBox1.SelectedIndex], STBLVault.StringSource.Modified | STBLVault.StringSource.Loaded))
                {
                    copysource.Add(entry.Key, entry.Value);
                }
            }

            for (int loop = 0; loop < (int)Languages.Language_Count; loop++)
            {
                // Skip languages there is nothing to write for
                if (!mData.HasEntriesForLanguage((Languages)loop, STBLVault.StringSource.Loaded | STBLVault.StringSource.Modified) &&
                    copysource == null)
                {
                    continue;
                }

                STBLWriter newentry;
                if (copysource != null)
                {
                    newentry = copysource.Copy();
                }
                else
                {
                    newentry = new STBLWriter();
                }
                newentry.Instance = mInstance;
                newentry.Language = (Languages)loop;

                // Overwrite or underwrite the user defined entries
                if (copysource != null && checkBox1.Checked)
                {
                    foreach (KeyValuePair <UInt64, string> entry in mData.GetEntriesForLanguage((Languages)loop, STBLVault.StringSource.Modified | STBLVault.StringSource.Loaded))
                    {
                        newentry.AddIfNotExists(entry.Key, entry.Value);
                    }
                }
                else
                {
                    foreach (KeyValuePair <UInt64, string> entry in mData.GetEntriesForLanguage((Languages)loop, STBLVault.StringSource.Modified | STBLVault.StringSource.Loaded))
                    {
                        newentry.Add(entry.Key, entry.Value);
                    }
                }

                // Load in the existing entry if requested
                if (radioButton2.Checked)
                {
                    DBPFReference oldref = new DBPFReference(0x220557DA, 0, mInstance | ((UInt64)loop << 56));
                    if (dbfile.Index.ContainsKey(oldref))
                    {
                        DBPFDataStream oldfile = dbfile.Open(oldref);
                        using (oldfile)
                        {
                            oldfile.GetData();
                            if (checkBox3.Checked)
                            {
                                // Merge overwriting old with new
                                foreach (KeyValuePair <UInt64, string> oldentry in STBLVault.GetDictionaryLoader(oldfile))
                                {
                                    newentry.AddIfNotExists(oldentry.Key, oldentry.Value);
                                }
                            }
                            else
                            {
                                // Merge overwriting new with old
                                foreach (KeyValuePair <UInt64, string> oldentry in STBLVault.GetDictionaryLoader(oldfile))
                                {
                                    newentry.Add(oldentry.Key, oldentry.Value);
                                }
                            }
                        }
                    }
                }

                // Create the STBL format data blob
                KeyValuePair <DBPFIndexEntry, byte[]> dbentry = newentry.Export();
                if (dbfile.Index.ContainsKey(dbentry.Key.Reference))
                {
                    toreplace[dbentry.Key.Reference] = dbentry;
                }
                else
                {
                    toadd.Add(dbentry);
                }
            }
        }