Ejemplo n.º 1
0
        // only works for PW !!!
        public eList[] Load(String elFile)
        {
            this.loadedFile = elFile;
            eList[] Li = new eList[0];
            addonIndex = new Dictionary <int, int>();
            try
            {
                Helper.subtypesInfolist = new Dictionary <int, SubTypeElement>();
                // open the element file
                FileStream   fs = File.OpenRead(elFile);
                BinaryReader br = new BinaryReader(fs);
                Version   = br.ReadInt16();
                Signature = br.ReadInt16();
                // check if a corresponding configuration file exists
                String[] configFiles = Directory.GetFiles(Application.StartupPath + "\\configs\\Rules\\", "PW_*_v" + Version + ".cfg");
                if (configFiles.Length > 0)
                {
                    // configure an eList array with the configuration file
                    ConfigFile = configFiles[0];
                    Li         = loadConfiguration(ConfigFile);
                    // read the element file

                    for (int l = 0; l < Li.Length; l++)
                    {
                        Application.DoEvents();
                        progress_bar("Reading elements ...", l, Li.Length);

                        // read offset
                        if (Li[l].listOffset != null)
                        {
                            // offset > 0
                            if (Li[l].listOffset.Length > 0)
                            {
                                Li[l].listOffset = br.ReadBytes(Li[l].listOffset.Length);
                            }
                        }
                        // autodetect offset (for list 20 & 100)
                        else
                        {
                            if (l == 20)
                            {
                                byte[] head  = br.ReadBytes(4);
                                byte[] count = br.ReadBytes(4);
                                byte[] body  = br.ReadBytes(BitConverter.ToInt32(count, 0));
                                byte[] tail  = br.ReadBytes(4);
                                Li[l].listOffset = new byte[head.Length + count.Length + body.Length + tail.Length];
                                Array.Copy(head, 0, Li[l].listOffset, 0, head.Length);
                                Array.Copy(count, 0, Li[l].listOffset, 4, count.Length);
                                Array.Copy(body, 0, Li[l].listOffset, 8, body.Length);
                                Array.Copy(tail, 0, Li[l].listOffset, 8 + body.Length, tail.Length);
                            }
                            if (l == 100)
                            {
                                byte[] head  = br.ReadBytes(4);
                                byte[] count = br.ReadBytes(4);
                                byte[] body  = br.ReadBytes(BitConverter.ToInt32(count, 0));
                                Li[l].listOffset = new byte[head.Length + count.Length + body.Length];
                                Array.Copy(head, 0, Li[l].listOffset, 0, head.Length);
                                Array.Copy(count, 0, Li[l].listOffset, 4, count.Length);
                                Array.Copy(body, 0, Li[l].listOffset, 8, body.Length);
                            }
                        }

                        // read conversation list
                        if (l == ConversationListIndex)
                        {
                            // Auto detect only works for Perfect World elements.data !!!
                            if (Li[l].elementTypes[0].Contains("AUTO"))
                            {
                                byte[] pattern        = (Encoding.GetEncoding("GBK")).GetBytes("facedata\\");
                                long   sourcePosition = br.BaseStream.Position;
                                int    listLength     = -72 - pattern.Length;
                                bool   run            = true;
                                while (run)
                                {
                                    run = false;
                                    for (int i = 0; i < pattern.Length; i++)
                                    {
                                        listLength++;
                                        if (br.ReadByte() != pattern[i])
                                        {
                                            run = true;
                                            break;
                                        }
                                    }
                                }
                                br.BaseStream.Position = sourcePosition;
                                Li[l].elementTypes[0]  = "byte:" + listLength;
                            }

                            Li[l].elementValues       = new Dictionary <int, Dictionary <int, Object> >();
                            Li[l].elementValues[0]    = new Dictionary <int, Object>();
                            Li[l].elementValues[0][0] = readValue(br, Li[l].elementTypes[0]);
                        }
                        // read lists
                        else
                        {
                            Li[l].elementValues = new Dictionary <int, Dictionary <int, Object> >();

                            int count = br.ReadInt32();
                            // go through all elements of a list
                            for (int e = 0; e < count; e++)
                            {
                                Li[l].elementValues[e] = new Dictionary <int, Object>();

                                int    id   = -1;
                                String name = "";
                                // go through all fields of an element
                                int    idnpc   = -1;
                                String namenpc = "";
                                int    idtest  = -1;
                                for (int f = 0; f < Li[l].elementTypes.Count; f++)
                                {
                                    Li[l].elementValues[e][f] = readValue(br, Li[l].elementTypes[f]);
                                    if (Li[l].elementFields[f] == "ID")
                                    {
                                        idtest = Int32.Parse(Li[l].GetValue(e, f));
                                        if (l == 0)
                                        {
                                            if (!addonIndex.ContainsKey(idtest))
                                            {
                                                addonIndex.Add(idtest, e);
                                            }
                                            else
                                            {
                                                MessageBox.Show("Error: Found duplicate Addon id:" + idtest);
                                                addonIndex[idtest] = e;
                                            }
                                        }
                                        if (!ocupiedIds.ContainsKey(idtest))
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count  = 1;
                                            itemDupe.listID = l;
                                            itemDupe.index  = e;
                                            ocupiedIds.Add(idtest, itemDupe);
                                        }
                                        else
                                        {
                                            if (!ocupiedIds_duplicate.ContainsKey(idtest))
                                            {
                                                ItemDupe itemDupe = new ItemDupe();
                                                itemDupe.count  = 1;
                                                itemDupe.listID = l;
                                                itemDupe.index  = e;
                                                ocupiedIds_duplicate.Add(idtest, itemDupe);
                                            }
                                            else
                                            {
                                                ocupiedIds_duplicate[idtest].count++;
                                            }
                                        }
                                    }
                                    if (Li[l].elementFields[f] == "Name" && idtest > 0)
                                    {
                                        if (ocupiedIds_duplicate.ContainsKey(idtest))
                                        {
                                            ocupiedIds_duplicate[idtest].itemId = idtest;
                                            ocupiedIds_duplicate[idtest].name   = Li[l].GetValue(e, f);
                                        }
                                        if (ocupiedIds.ContainsKey(idtest))
                                        {
                                            ocupiedIds[idtest].itemId = idtest;
                                            ocupiedIds[idtest].name   = Li[l].GetValue(e, f);
                                        }

                                        int[] whereToSearch = new int[] { 3, 6, 9, 12, 15, 17, 19, 21, 22, 23, 24, 28, 30, 31, 35, 74, 75, 79, 89, 95, 106, 114, 115, 116, 33 };
                                        if (whereToSearch.Contains <int>(l))
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count        = 1;
                                            itemDupe.listID       = l;
                                            itemDupe.index        = e;
                                            itemDupe.name         = Li[l].GetValue(e, f);
                                            itemDupe.itemId       = idtest;
                                            valuableItems[idtest] = itemDupe;
                                        }
                                        if (l == 40)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count          = 1;
                                            itemDupe.listID         = l;
                                            itemDupe.index          = e;
                                            itemDupe.name           = Li[l].GetValue(e, f);
                                            itemDupe.itemId         = idtest;
                                            NPCSellServices[idtest] = itemDupe;
                                        }
                                        if (l == 45)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count          = 1;
                                            itemDupe.listID         = l;
                                            itemDupe.index          = e;
                                            itemDupe.name           = Li[l].GetValue(e, f);
                                            itemDupe.itemId         = idtest;
                                            NPCReciveQuests[idtest] = itemDupe;
                                        }
                                        if (l == 46)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count            = 1;
                                            itemDupe.listID           = l;
                                            itemDupe.index            = e;
                                            itemDupe.name             = Li[l].GetValue(e, f);
                                            itemDupe.itemId           = idtest;
                                            NPCActivateQuests[idtest] = itemDupe;
                                        }
                                        if (l == 47)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count   = 1;
                                            itemDupe.listID  = l;
                                            itemDupe.index   = e;
                                            itemDupe.name    = Li[l].GetValue(e, f);
                                            itemDupe.itemId  = idtest;
                                            NPCTasks[idtest] = itemDupe;
                                        }
                                        if (l == 48)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count   = 1;
                                            itemDupe.listID  = l;
                                            itemDupe.index   = e;
                                            itemDupe.name    = Li[l].GetValue(e, f);
                                            itemDupe.itemId  = idtest;
                                            NPCSkill[idtest] = itemDupe;
                                        }
                                        if (l == 54)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count             = 1;
                                            itemDupe.listID            = l;
                                            itemDupe.index             = e;
                                            itemDupe.name              = Li[l].GetValue(e, f);
                                            itemDupe.itemId            = idtest;
                                            NPCCraftingService[idtest] = itemDupe;
                                        }
                                        if (l == 145)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count            = 1;
                                            itemDupe.listID           = l;
                                            itemDupe.index            = e;
                                            itemDupe.name             = Li[l].GetValue(e, f);
                                            itemDupe.itemId           = idtest;
                                            NPCEngraveService[idtest] = itemDupe;
                                        }
                                        if (l == 146)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count         = 1;
                                            itemDupe.listID        = l;
                                            itemDupe.index         = e;
                                            itemDupe.name          = Li[l].GetValue(e, f);
                                            itemDupe.itemId        = idtest;
                                            NPCHoneService[idtest] = itemDupe;
                                        }
                                        if (l == 38)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count   = 1;
                                            itemDupe.listID  = l;
                                            itemDupe.index   = e;
                                            itemDupe.name    = Li[l].GetValue(e, f);
                                            itemDupe.itemId  = idtest;
                                            MONSTERS[idtest] = itemDupe;
                                        }
                                        if (l == 56)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count  = 1;
                                            itemDupe.listID = l;
                                            itemDupe.index  = e;
                                            itemDupe.name   = Li[l].GetValue(e, f);
                                            itemDupe.itemId = idtest;
                                            NPCTYPE[idtest] = itemDupe;
                                        }
                                        if (l == 150)
                                        {
                                            ItemDupe itemDupe = new ItemDupe();
                                            itemDupe.count  = 1;
                                            itemDupe.listID = l;
                                            itemDupe.index  = e;
                                            itemDupe.name   = Li[l].GetValue(e, f);
                                            itemDupe.itemId = idtest;
                                            FORCES[idtest]  = itemDupe;
                                        }

                                        // 32227
                                    }
                                    if (l == 38 || l == 57 || l == 79)
                                    {
                                        if (Li[l].elementFields[f] == "ID")
                                        {
                                            idnpc = Int32.Parse(Li[l].GetValue(e, f));
                                            // NpcEditor.npcdb
                                        }
                                        if (Li[l].elementFields[f] == "Name")
                                        {
                                            namenpc = Li[l].GetValue(e, f);

                                            NpcEditor.npcdb[idnpc] = namenpc;
                                        }
                                    }
                                    if (Helper.list_supdype.ContainsKey(l))
                                    {
                                        if (Li[l].elementFields[f] == "ID")
                                        {
                                            id = Int32.Parse(Li[l].GetValue(e, f));
                                        }
                                        if (Li[l].elementFields[f] == "Name")
                                        {
                                            name = Li[l].GetValue(e, f);
                                            SubTypeElement ste = new SubTypeElement();
                                            if (Helper.list_supdype[l].type == Constatns.typeSubType)
                                            {
                                                if (Helper.subtypesInfolist.ContainsKey(Helper.list_supdype[l].useonList))
                                                {
                                                    if (!Helper.subtypesInfolist[Helper.list_supdype[l].useonList].subtype.ContainsKey(id))
                                                    {
                                                        Helper.subtypesInfolist[Helper.list_supdype[l].useonList].subtype.Add(id, name);
                                                    }
                                                }
                                                else
                                                {
                                                    Helper.subtypesInfolist.Add(Helper.list_supdype[l].useonList, ste);
                                                    Helper.subtypesInfolist[Helper.list_supdype[l].useonList].subtype.Add(id, name);
                                                }
                                            }

                                            if (Helper.list_supdype[l].type == Constatns.typeMajorType)
                                            {
                                                if (Helper.subtypesInfolist.ContainsKey(Helper.list_supdype[l].useonList))
                                                {
                                                    if (!Helper.subtypesInfolist[Helper.list_supdype[l].useonList].majorType.ContainsKey(id))
                                                    {
                                                        Helper.subtypesInfolist[Helper.list_supdype[l].useonList].majorType.Add(id, name);
                                                    }
                                                }
                                                else
                                                {
                                                    Helper.subtypesInfolist.Add(Helper.list_supdype[l].useonList, ste);
                                                    Helper.subtypesInfolist[Helper.list_supdype[l].useonList].majorType.Add(id, name);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("No corressponding configuration file found!\nVersion: " + Version + "\nPattern: " + "configs\\PW_*_v" + Version + ".cfg");
                }
                br.Close();
                fs.Close();
                Application.DoEvents();
                progress_bar("Ready", 0, 0);
                Program.elementSeditorFirstLoad = false;
            }catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return(Li);
        }