// returns an eList array with preconfigured fields from configuration file private EList[] LoadConfiguration(string file) { //ConfigFile = configFiles[0]->Substring(configFiles[0]->LastIndexOf("\\")); var sr = new StreamReader(file); var li = new EList[Convert.ToInt32(sr.ReadLine())]; try { ConversationListIndex = Convert.ToInt32(sr.ReadLine()); } catch { ConversationListIndex = 58; } for (var i = 0; i < li.Length; i++) { Application.DoEvents(); string line; while ((line = sr.ReadLine()) == "") { } li[i] = new EList(); li[i].listName = line; li[i].listOffset = null; var offset = sr.ReadLine(); if (offset != "AUTO") li[i].listOffset = new byte[Convert.ToInt32(offset)]; var readLine = sr.ReadLine(); if (readLine != null) li[i].elementFields = readLine.Split(new[] { ';' }); var s = sr.ReadLine(); if (s != null) li[i].elementTypes = s.Split(new[] { ';' }); } sr.Close(); return li; }
// add all new elements into the elementValues public ArrayList JoinElements(EList newList, int listID, bool addNew, bool backupNew, bool replaceChanged, bool backupChanged, bool removeMissing, bool backupMissing, string dirBackupNew, string dirBackupChanged, string dirBackupMissing) { var newElementValues = newList.elementValues; //var newElementTypes = newList.elementTypes; var report = new ArrayList(); bool exists; // check which elements are missing (removed) for (var n = 0; n < elementValues.Length; n++) { Application.DoEvents(); exists = false; for (var e = 0; e < newElementValues.Length; e++) if (GetValue(n, 0) == newList.GetValue(e, 0)) exists = true; if (exists) continue; if (dirBackupMissing != null && Directory.Exists(dirBackupMissing)) ExportItem(dirBackupMissing + @"\List_" + listID.ToString(CultureInfo.InvariantCulture) + "_Item_" + GetValue(n, 0) + ".txt", n); if (removeMissing) { report.Add("- MISSING ITEM (*removed): " + ((int) elementValues[n][0]).ToString(CultureInfo.InvariantCulture)); RemoveItem(n); n--; } else report.Add("- MISSING ITEM (*not removed): " + ((int) elementValues[n][0]).ToString(CultureInfo.InvariantCulture)); } for (var e = 0; e < newElementValues.Length; e++) { Application.DoEvents(); // check if the element with this id already exists exists = false; for (var n = 0; n < elementValues.Length; n++) { if (GetValue(n, 0) != newList.GetValue(e, 0)) continue; exists = true; // check if this item is different if (elementValues[n].Length != newList.elementValues[e].Length) // invalid amount of values !!! report.Add("<> DIFFERENT ITEM (*not replaced, invalid amount of values): " + GetValue(n, 0)); else { // compare all values of current element for (var i = 0; i < elementValues[n].Length; i++) { if (GetValue(n, i) == newList.GetValue(e, i)) continue; if (backupChanged && Directory.Exists(dirBackupChanged)) ExportItem(dirBackupChanged + @"\List_" + listID.ToString(CultureInfo.InvariantCulture) + "_Item_" + GetValue(n, 0) + ".txt", n); if (replaceChanged) { report.Add("<> DIFFERENT ITEM (*replaced): " + GetValue(n, 0)); elementValues[n] = newList.elementValues[e]; } else report.Add("<> DIFFERENT ITEM (*not replaced): " + GetValue(n, 0)); break; } } break; } if (exists) continue; if (backupNew && Directory.Exists(dirBackupNew)) newList.ExportItem(dirBackupNew + @"\List_" + listID.ToString(CultureInfo.InvariantCulture) + "_Item_" + newList.GetValue(e, 0) + ".txt", e); if (addNew) { AddItem(newElementValues[e]); report.Add("+ NEW ITEM (*added): " + GetValue(elementValues.Length - 1, 0)); } else report.Add("+ NEW ITEM (*not added): " + GetValue(elementValues.Length - 1, 0)); } return report; }
public EList[] Load(string elFile) { var li = new EList[0]; // open the element file var fs = File.OpenRead(elFile); var br = new BinaryReader(fs); Version = br.ReadInt16(); Signature = br.ReadInt16(); // check if a corresponding configuration file exists // original: ConfigFile = Application::StartupPath + "\\configs\\" + ((ToolStripMenuItem^)sender)->Text + ".cfg"; var configFiles = Directory.GetFiles(Application.StartupPath + @"\Plugins\Elements.PW\configs", "PW_*_v" + Version + ".cfg"); if (configFiles.Length > 0) { // configure an eList array with the configuration file li = LoadConfiguration(configFiles[0]); // read the element file for (var l = 0; l < li.Length; l++) { Application.DoEvents(); // 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 { byte[] head; byte[] count; byte[] body; switch (l) { case 20: head = br.ReadBytes(4); count = br.ReadBytes(4); body = br.ReadBytes(BitConverter.ToInt32(count, 0)); var 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); break; case 100: head = br.ReadBytes(4); count = br.ReadBytes(4); 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); break; } } // read conversation list if (l == ConversationListIndex) { // Auto detect only works for Perfect World elements.data !!! if (li[l].elementTypes[0].Contains("AUTO")) { var pattern = (Encoding.GetEncoding("GBK")).GetBytes("facedata\\"); var sourcePosition = br.BaseStream.Position; var listLength = -72 - pattern.Length; var run = true; while (run) { run = false; foreach (var t in pattern) { listLength++; if (br.ReadByte() == t) continue; run = true; break; } } br.BaseStream.Position = sourcePosition; li[l].elementTypes[0] = "byte:" + listLength; } li[l].elementValues = new object[1][]; li[l].elementValues[0] = new object[li[l].elementTypes.Length]; li[l].elementValues[0][0] = ReadValue(br, li[l].elementTypes[0]); } // read lists else { li[l].elementValues = new object[br.ReadInt32()][]; // go through all elements of a list for (var e = 0; e < li[l].elementValues.Length; e++) { li[l].elementValues[e] = new object[li[l].elementTypes.Length]; // go through all fields of an element for (var f = 0; f < li[l].elementValues[e].Length; f++) li[l].elementValues[e][f] = ReadValue(br, li[l].elementTypes[f]); } } } } else MessageBox.Show("No corresponding configuration file found!\nVersion: " + Version + "\nPattern: " + @"Plugins\Elements.PW\configs\PW_*_v" + Version + ".cfg"); br.Close(); fs.Close(); return li; }