private static void DumpCharacterData(SaveReader saveData)
 {
     lock (outputLock)
     {
         sw.WriteLine(GetCharacterCSVData(saveData));
     }
 }
        private static string GetCharacterCSVData(SaveReader saveData)
        {
            StringBuilder sb = new StringBuilder();

            PropertyInfo[] characterProperties = typeof(Character).GetProperties();
            PropertyInfo[] statProperties = typeof(Stat).GetProperties();

            foreach (var item in characterProperties)
            {
                if (item.PropertyType.IsPrimitive || item.PropertyType.IsEnum || item.PropertyType == typeof(string))
                {
                    if (saveData != null)
                    {
                        sb.Append(item.GetValue(saveData.Character, null) + ",");
                    }
                    else
                    {
                        sb.Append(item.Name + ",");
                    }
                }
            }
            foreach (var item in statProperties)
            {
                if (item.PropertyType.IsPrimitive || item.PropertyType.IsEnum || item.PropertyType == typeof(string))
                {
                    if (saveData != null)
                    {
                        sb.Append(item.GetValue(saveData.Stat, null) + ",");
                    }
                    else
                    {
                        sb.Append(item.Name + ",");
                    }
                }
            }

            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
        private static StreamWriter sw; // TEMP!

        #endregion Fields

        #region Methods

        private static void BatchConvertToD2s(string path, string resourceSet)
        {
            string convertedSavePath = path + "\\d2s\\";
            string[] fileNames = Directory.GetFiles(path);

            if (!Directory.Exists(convertedSavePath))
            {
                Directory.CreateDirectory(convertedSavePath);
            }
            for (int i = 0; i < fileNames.Length; i++)
            {
                SaveReader currentSave = new SaveReader(resourceSet);

                try
                {
                    currentSave.Read(File.ReadAllBytes(fileNames[i]));
                    if (currentSave.Character.UnknownFlags != 64)
                    {
                        Console.WriteLine("Flags = {0}", currentSave.Character.UnknownFlags);
                    }
                    currentSave.Character.UnknownFlags = 0;

                    string savePath = convertedSavePath + Path.GetFileNameWithoutExtension(fileNames[i]) + ".d2s";

                    using (FileStream saveStream = File.OpenWrite(savePath))
                    {
                        currentSave.Write(saveStream, false);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to convert {0}: {1}", fileNames[i], ex.Message);
                }
            }
        }
Exemple #4
0
        private void ProcessCharacter(byte[] rawCharacterBytes)
        {
            playerData = new SaveReader(comboBoxResourceSet.Text);

            playerData.Read(rawCharacterBytes);

            if (playerData.FailedCharacterDecoding)
            {
                MessageBox.Show("Failed character decoding");
            }
            if (playerData.FailedInventoryDecoding)
            {
                MessageBox.Show("Failed inventory decoding");
            }
            if (playerData.FailedSkillDecoding)
            {
                MessageBox.Show("Failed skill decoding");
            }

            if (playerData.Inventory.FailedItemCount > 0)
            {
                MessageBox.Show(string.Format("Failed to read {0} items. These items will not be included when saving character", playerData.Inventory.FailedItemCount));
            }

            listBoxInventory.Items.Clear();
            listBoxCorpseInventory.Items.Clear();
            listBoxGolemInventory.Items.Clear();
            listBoxMercInventory.Items.Clear();

            UpdateInventoryList();

            textBoxUnknownFlags.DataBindings.Clear();
            textBoxName.DataBindings.Clear();
            characterBindingSource.Clear();
            statBindingSource.Clear();
            itemBindingSource.Clear();
            dataGridViewItemProperties.DataSource = null;
            dataGridViewItemRunewordProperties.DataSource = null;
            dataGridViewItemSetProperties.DataSource = null;

            textBoxUnknownFlags.DataBindings.Add("Text", playerData.Character, "UnknownFlags");
            textBoxName.DataBindings.Add("Text", playerData.Character, "Name");

            characterBindingSource.Add(playerData.Character);
            statBindingSource.Add(playerData.Stat);
        }
        /// <summary>
        /// Process raw character bytes from save file
        /// </summary>
        /// <param name="rawCharacterbytes">Raw data from save file</param>
        private void ProcessCharacter(byte[] rawCharacterbytes)
        {
            ClearPlayerData();

            CharacterEditor.Resources.Instance.ResourceSet = (string)comboBoxResourceSets.SelectedItem;
            playerData = new SaveReader(comboBoxResourceSets.SelectedItem.ToString());

            try
            {
                playerData.Read(rawCharacterbytes);
            }
            catch (Exception ex)
            {
                new ErrorWindow("Unable to read character, most likely wrong version: " + ex.Message, true);
                return;
            }

            string errorString = "";

            if (playerData.FailedCharacterDecoding)
            {
                errorString += "Failed character decoding\n";
            }
            if (playerData.FailedInventoryDecoding)
            {
                errorString += "Failed inventory decoding";
            }
            if (playerData.FailedSkillDecoding)
            {
                errorString += "Failed skill decoding";
            }

            if (errorString.Length > 0)
            {
                ErrorWindow errorWindow = new ErrorWindow(errorString, true);
            }

            if (playerData.Inventory.FailedItemCount > 0)
            {
                new ErrorWindow(string.Format("Failed to read {0} items. These items will not be included when saving character", playerData.Inventory.FailedItemCount));
            }

            RefreshCharacter();
            RefreshInventory();
        }
        private void ClearPlayerData()
        {
            playerData = null;
            itemToEdit = null;

            RefreshCharacter();
            RefreshInventory();
            RefreshItemEditor();
        }
        private static void TestAllFiles(string path, string resourceSet, bool detectDuplicateIds, bool detectFlaggedItems, bool detectFlaggedProperties, string flaggedItemsPath, string flaggedPropertiesPath)
        {
            string invalidSavePath = path + "\\invalid\\";
            string invalidItemPath = path + "\\baditems\\";
            string invalidPropsPath = path + "\\badprops\\";
            string invalidDecodePath = path + "\\undecoded\\";
            string[] fileNames = Directory.GetFiles(path);

            List<string> invlaidSaves = new List<string>();
            List<string> invalidItemSaves = new List<string>();
            List<string> invalidPropsSaves = new List<string>();
            List<string> invalidDecodeSaves = new List<string>();

            Dictionary<uint, List<ItemData>> itemIds = new Dictionary<uint, List<ItemData>>();
            Dictionary<string, List<ItemData>> detectedFlaggedItems = new Dictionary<string, List<ItemData>>();
            Dictionary<string, List<ItemData>> detectedFlaggedProps = new Dictionary<string, List<ItemData>>();

            HashSet<string> flaggedItems = null;
            Dictionary<string, ItemPropData> flaggedProperties = null;

            if (detectFlaggedItems)
            {
                try
                {
                    flaggedItems = ReadFlaggedItemList(flaggedItemsPath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to read " + flaggedItemsPath + ": " + ex.Message);
                    sw.Close();
                    return;
                }
            }
            if (detectFlaggedProperties)
            {
                try
                {
                    flaggedProperties = ReadFlaggedPropertyList(flaggedPropertiesPath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to read " + flaggedPropertiesPath + ": " + ex.Message);
                    return;
                }
            }

            for (int i = 0; i < fileNames.Length; i++)
            {
                SaveReader currentSave = new SaveReader(resourceSet);

                try
                {
                    currentSave.Read(File.ReadAllBytes(fileNames[i]));

                    foreach (var item in currentSave.Inventory.PlayerItems)
                    {
                        if (detectDuplicateIds)
                        {
                            if (!itemIds.ContainsKey(item.Id))
                            {
                                itemIds[item.Id] = new List<ItemData>();
                            }

                            itemIds[item.Id].Add(new ItemData() { FileName = fileNames[i], Item = item });
                        }
                        if (detectFlaggedItems)
                        {
                            if (flaggedItems.Contains(item.ItemCode))
                            {
                                if (!detectedFlaggedItems.ContainsKey(item.ItemCode))
                                {
                                    detectedFlaggedItems[item.ItemCode] = new List<ItemData>();
                                }

                                detectedFlaggedItems[item.ItemCode].Add(new ItemData() { FileName = fileNames[i], Item = item });
                            }
                        }
                        if (detectFlaggedProperties)
                        {
                            foreach (var prop in item.Properties)
                            {
                                if (flaggedProperties.ContainsKey(prop.PropertyName))
                                {
                                    if (flaggedProperties[prop.PropertyName].TestValue(prop.Value))
                                    {
                                        if (!detectedFlaggedProps.ContainsKey(prop.PropertyName))
                                        {
                                            detectedFlaggedProps[prop.PropertyName] = new List<ItemData>();
                                        }

                                        detectedFlaggedProps[prop.PropertyName].Add(new ItemData() { FileName = fileNames[i], Item = item });
                                    }
                                }
                            }
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    invlaidSaves.Add(fileNames[i]);
                    continue;
                }
                catch (KeyNotFoundException)
                {
                    invalidPropsSaves.Add(fileNames[i]);
                    continue;
                }
                catch (Exception)
                {
                    invlaidSaves.Add(fileNames[i]);
                    continue;
                }

                if (currentSave != null)
                {
                    DumpCharacterData(currentSave);
                    if (currentSave.Inventory.FailedItemCount > 0)
                    {
                        invalidItemSaves.Add(fileNames[i]);
                    }
                    else if (currentSave.FailedCharacterDecoding || currentSave.FailedInventoryDecoding || currentSave.FailedSkillDecoding)
                    {
                        invalidDecodeSaves.Add(fileNames[i]);
                    }
                }

                if (i % 100 == 0)
                {
                    Console.Write("\r");
                    Console.Write(string.Format("Progress: {0,7:0.00%}", i / (double)fileNames.Length));
                }
            }
            Console.Write("\r");
            Console.Write(string.Format("Progress: {0,7:0.00%}", 1.0));
            Console.WriteLine();

            MoveInvalidSaves(invlaidSaves, invalidSavePath);
            MoveInvalidSaves(invalidItemSaves, invalidItemPath);
            MoveInvalidSaves(invalidPropsSaves, invalidPropsPath);
            MoveInvalidSaves(invalidDecodeSaves, invalidDecodePath);

            if (detectDuplicateIds)
            {
                Console.WriteLine();
                Console.WriteLine("Searching for duplicate ids:");
                Console.WriteLine();
                var dupes = (from n in itemIds where n.Key > 0 && n.Value.Count > 1 select n).ToList();

                foreach (var item in dupes)
                {
                    Console.WriteLine("Item {0} - {1}:", item.Key, CharacterEditor.ItemDefs.GetItemDescription(item.Value[0].Item.ItemCode));
                    foreach (var dupe in item.Value)
                    {
                        Console.WriteLine("\t{0}", dupe.FileName);
                    }
                    Console.WriteLine();
                }
            }

            if (detectFlaggedItems)
            {
                Console.WriteLine();
                Console.WriteLine("Flagged items detected:");
                Console.WriteLine();
                foreach (var item in detectedFlaggedItems)
                {
                    Console.WriteLine("Item {0} - {1}:", item.Key, CharacterEditor.ItemDefs.GetItemDescription(item.Key));
                    foreach (var dupe in item.Value)
                    {
                        Console.WriteLine("\t{0}", dupe.FileName);
                    }
                    Console.WriteLine();
                }
            }

            if (detectFlaggedProperties)
            {
                Console.WriteLine();
                Console.WriteLine("Flagged item properties detected:");
                Console.WriteLine();
                foreach (var item in detectedFlaggedProps)
                {
                    Console.WriteLine("Property {0}:", flaggedProperties[item.Key]);
                    foreach (var flagged in item.Value)
                    {
                        Console.Write("\t{0} -> ", flagged.FileName);
                        foreach (var prop in flagged.Item.Properties)
                        {
                            if (prop.PropertyName == item.Key)
                            {
                                Console.WriteLine("[n = {0}] {1}", prop.Value, flagged.Item);
                                break;
                            }
                        }
                    }
                    Console.WriteLine();
                }
            }
        }