Beispiel #1
0
        public static void ItemGroupCopyEntries(List <ReadableTuple <int> > items, BaseDb gdb, TabControl control, ServerType serverType)
        {
            var parent = WpfUtilities.FindDirectParentControl <SdeEditor>(control);

            parent.AsyncOperation.SetAndRunOperation(new GrfThread(delegate {
                items = items.OrderBy(p => p.GetKey <int>()).ToList();

                StringBuilder builder = new StringBuilder();

                try {
                    AProgress.Init(parent);
                    DbLoaderErrorHandler.Start();

                    var dbItems = gdb.GetMeta <int>(ServerDbs.Items);

                    List <string> aegisNames = dbItems.FastItems.Select(p => p.GetStringValue(ServerItemAttributes.AegisName.Index)).ToList();
                    List <string> names      = dbItems.FastItems.Select(p => p.GetStringValue(ServerItemAttributes.Name.Index)).ToList();

                    for (int i = 0; i < items.Count; i++)
                    {
                        AProgress.IsCancelling(parent);
                        DbWriterMethods.DbItemGroupWriter2(items[i], serverType, builder, gdb, aegisNames, names);
                        parent.Progress = (i + 1f) / items.Count * 100f;
                    }
                }
                catch (OperationCanceledException) { }
                finally {
                    AProgress.Finalize(parent);
                    DbLoaderErrorHandler.Stop();
                }

                Clipboard.SetText(builder.ToString());
            }, parent, 200, null, true, true));
        }
Beispiel #2
0
        public static string ToHerculesEntry(BaseDb db, int itemId)
        {
            var dbItems = db.GetMeta <int>(ServerDbs.Items);

            StringBuilder builder = new StringBuilder();

            var tuple = dbItems.TryGetTuple(itemId);

            if (tuple != null)
            {
                builder.AppendLineUnix("{");
                builder.AppendLineUnix("\tId: " + tuple.GetKey <int>().ToString(CultureInfo.InvariantCulture));
                builder.AppendLineUnix("\tAegisName: \"" + tuple.GetValue <string>(ServerItemAttributes.AegisName) + "\"");
                builder.AppendLineUnix("\tName: \"" + tuple.GetValue <string>(ServerItemAttributes.Name) + "\"");

                builder.AppendLineUnix("\tType: " + tuple.GetValue <string>(ServerItemAttributes.Type));
                _trySet(tuple, builder, ServerItemAttributes.Buy);
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Sell, (tuple.GetValue <int>(ServerItemAttributes.Buy) / 2).ToString(CultureInfo.InvariantCulture));
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Weight, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Attack, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Matk, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Defense, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Range, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.NumberOfSlots, "0");
                _trySetIfDefaultEmptyAddHex(tuple, builder, ServerItemAttributes.ApplicableJob, "");
                _trySetIfDefaultEmptyUpper(tuple, builder, ServerItemAttributes.Upper);
                _trySetGender(tuple, builder, ServerItemAttributes.Gender, "2");
                _trySetIfDefaultLocation(tuple, builder, ServerItemAttributes.Location);
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.WeaponLevel, "0");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.EquipLevel, "0");
                _trySetIfRefineable(tuple, builder, ServerItemAttributes.Refineable, true);
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.ClassNumber, "0");
                _trySetIfDefaultBoolean(tuple, builder, ServerItemAttributes.BindOnEquip, false);
                _trySetIfDefaultBoolean(tuple, builder, ServerItemAttributes.BuyingStore, false);
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Delay, "0");

                var trade = tuple.GetRawValue(ServerItemAttributes.Trade.Index) as Trade;
                if (trade != null && trade.NeedPrinting())
                {
                    builder.AppendLineUnix(trade.ToWriteString());
                }

                var nouse = tuple.GetRawValue(ServerItemAttributes.NoUse.Index) as NoUse;
                if (nouse != null && nouse.NeedPrinting())
                {
                    builder.AppendLineUnix(nouse.ToWriteString());
                }

                _trySetIfDefaultEmptyBracket(tuple, builder, ServerItemAttributes.Stack, "");
                _trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Sprite, "0");
                _trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.Script, "");
                _trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.OnEquipScript, "");
                _trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.OnUnequipScript, "");
                builder.Append("},");
            }

            return(builder.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// Converts a group from an ItemGroup to a Hercules entry
        /// </summary>
        /// <param name="gdb">The base db.</param>
        /// <param name="groupId">The group id.</param>
        /// <param name="aegisNames">The aegis names.</param>
        /// <param name="names">The names.</param>
        /// <returns>A string for an ItemGroup entry converted to Hercules's format</returns>
        public static string ToHerculesDbEntry(BaseDb gdb, int groupId, List <string> aegisNames, List <string> names)
        {
            StringBuilder builder  = new StringBuilder();
            var           dbItems  = gdb.GetMeta <int>(ServerDbs.Items);
            var           dbGroups = gdb.Get <int>(ServerDbs.ItemGroups);

            if (groupId < 500)
            {
                var    dbConstants = gdb.Get <string>(ServerDbs.Constants);
                string sId         = groupId.ToString(CultureInfo.InvariantCulture);
                // The current db is from rAthena

                var    tuple    = dbConstants.FastItems.FirstOrDefault(p => p.GetValue <string>(1) == sId && p.GetKey <string>().StartsWith("IG_"));
                string constant = null;

                if (tuple != null)
                {
                    constant = tuple.GetKey <string>().Substring(3);
                }
                else
                {
                    var res = DbIOItemGroups.Constants.Where(p => p.Value == groupId).ToList();

                    if (res.Count > 0)
                    {
                        constant = res[0].Key;
                    }
                }

                if (constant != null)
                {
                    string originalConstantValue = constant;
                    ReadableTuple <int> tupleItem;

                    // Attempts to retrieve the item based on the script
                    tupleItem = dbItems.FastItems.FirstOrDefault(p => p.GetValue <string>(ServerItemAttributes.Script).IndexOf("getrandgroupitem(IG_" + originalConstantValue + ")", StringComparison.OrdinalIgnoreCase) > -1);

                    if (tupleItem == null)
                    {
                        // Attempts to retrieve the item based on a formatted constant name (with underscore)

                        StringBuilder temp = new StringBuilder();
                        temp.Append(constant[0]);

                        for (int i = 1; i < constant.Length; i++)
                        {
                            if (constant[i] == '_')
                            {
                                i++;

                                if (i < constant.Length)
                                {
                                    temp.Append(constant[i]);
                                }
                            }
                            else if (char.IsUpper(constant[i]))
                            {
                                temp.Append('_');
                                temp.Append(constant[i]);
                            }
                            else
                            {
                                temp.Append(constant[i]);
                            }
                        }

                        constant = temp.ToString();

                        // Attempts to retrieve the item with the Old prefix
                        string oldConstant = "Old_" + constant;

                        // Attempts to retrieve the item without the Old prefix
                        tupleItem = dbItems.FastItems.FirstOrDefault(p => p.GetStringValue(ServerItemAttributes.AegisName.Index) == oldConstant);

                        if (tupleItem == null)
                        {
                            tupleItem = dbItems.FastItems.FirstOrDefault(p => p.GetStringValue(ServerItemAttributes.AegisName.Index) == constant);
                        }
                    }

                    // Retrieve the closest item based on the names in the ItemDb.
                    // It uses the Levenshtein distance algorithm to find the clostest match.
                    // This method 'always' returns a value, but a warning is prompted to the user in the error console.
                    if (tupleItem == null)
                    {
                        List <string> values1 = aegisNames;
                        List <string> values2 = names;

                        string closestMatch1 = Methods.ClosestString(originalConstantValue, values1);
                        string closestMatch2 = Methods.ClosestString(originalConstantValue, values2);

                        int lev1 = Methods.LevenshteinDistance(originalConstantValue, closestMatch1);
                        int lev2 = Methods.LevenshteinDistance(originalConstantValue, closestMatch2);

                        tupleItem = dbItems.FastItems[lev1 < lev2 ? values1.IndexOf(closestMatch1) : values2.IndexOf(closestMatch2)];

                        string closestMatch = tupleItem.GetValue <string>(ServerItemAttributes.AegisName);

                        if (Math.Min(lev1, lev2) != 0 && closestMatch.Replace("_", "") != constant)
                        {
                            DbIOErrorHandler.Handle(StackTraceException.GetStrackTraceException(), "A suspicious conversion occurred for the item [" + originalConstantValue + "]. The group item name is [" + tupleItem.GetValue <string>(ServerItemAttributes.AegisName) + "].");
                        }
                    }

                    builder.AppendLine(tupleItem.GetValue <string>(ServerItemAttributes.AegisName) + ": (");

                    Dictionary <int, ReadableTuple <int> > table = (Dictionary <int, ReadableTuple <int> >)dbGroups.GetTuple(groupId).GetRawValue(ServerItemGroupAttributes.Table.Index);

                    foreach (var pair in table)
                    {
                        tupleItem = dbItems.TryGetTuple(pair.Key);
                        string name = tupleItem == null ? "ID" + pair.Key : tupleItem.GetValue <string>(ServerItemAttributes.AegisName);

                        if (pair.Value.GetValue <string>(ServerItemGroupSubAttributes.Rate) == "1")
                        {
                            builder.Append("\t\"");
                            builder.Append(name);
                            builder.AppendLine("\",");
                        }
                        else
                        {
                            builder.Append("\t(\"");
                            builder.Append(name);
                            builder.Append("\",");
                            builder.Append(pair.Value.GetValue <string>(ServerItemGroupSubAttributes.Rate));
                            builder.AppendLine("),");
                        }
                    }

                    builder.Append(")");
                }
                else
                {
                    DbIOErrorHandler.Handle(StackTraceException.GetStrackTraceException(), "Failed to find the constant name with the id [" + sId + "].");
                }
            }
            else
            {
                // The current db is from Hercules
                var tuple = dbItems.TryGetTuple(groupId);

                if (tuple != null)
                {
                    // Check if the item is a package or a group!
                    Dictionary <int, ReadableTuple <int> > table = (Dictionary <int, ReadableTuple <int> >)dbGroups.GetTuple(groupId).GetRawValue(ServerItemGroupAttributes.Table.Index);

                    if (table.Values.Any(p => p.GetRawElements().Skip(3).Take(6).Count(q => (q ?? "").ToString() == "") > 0))
                    {
                        // This is a package
                    }
                    else
                    {
                        // This is an item_group
                        builder.AppendLine(tuple.GetValue <string>(ServerItemAttributes.AegisName) + ": (");

                        foreach (var pair in table)
                        {
                            tuple = dbItems.TryGetTuple(pair.Key);
                            string name = tuple == null ? "ID" + pair.Key : tuple.GetValue <string>(ServerItemAttributes.AegisName);

                            if (pair.Value.GetValue <string>(ServerItemGroupSubAttributes.Rate) == "1")
                            {
                                builder.Append("\t\"");
                                builder.Append(name);
                                builder.AppendLine("\",");
                            }
                            else
                            {
                                builder.Append("\t(\"");
                                builder.Append(name);
                                builder.Append("\",");
                                builder.Append(pair.Value.GetValue <string>(ServerItemGroupSubAttributes.Rate));
                                builder.AppendLine("),");
                            }
                        }

                        builder.Append(")");
                    }
                }
                else
                {
                    DbIOErrorHandler.Handle(StackTraceException.GetStrackTraceException(), "Failed to retrieve the item ID associated with the group ID.", groupId.ToString(CultureInfo.InvariantCulture), ErrorLevel.Critical);
                }
            }

            return(builder.ToString());
        }
Beispiel #4
0
        public static void Writer2 <TKey>(ReadableTuple <TKey> item, ServerType destServer, StringBuilder builder, BaseDb db, List <string> aegisNames, List <string> names)
        {
            var itemCopy = new ReadableTuple <TKey>(item.GetKey <TKey>(), item.Attributes);

            itemCopy.Copy(item);
            item = itemCopy;

            int key = item.GetKey <int>();

            if (destServer == ServerType.RAthena)
            {
                var        itemDb = db.GetMeta <int>(ServerDbs.Items);
                ServerType source = DbPathLocator.GetServerType();

                if (source == ServerType.Hercules)
                {
                    List <string> constantsList             = Constants.Keys.ToList();
                    Table <int, ReadableTuple <int> > table = db.GetMeta <int>(ServerDbs.Items);

                    var tuple = item;
                    var res2  = table.TryGetTuple(key);

                    if (res2 != null)
                    {
                        string name  = res2.GetValue(ServerItemAttributes.AegisName).ToString();
                        int    low   = Int32.MaxValue;
                        int    index = -1;

                        for (int j = 0; j < Constants.Count; j++)
                        {
                            int dist = Methods.LevenshteinDistance(name, constantsList[j]);

                            if (dist < low)
                            {
                                low   = dist;
                                index = j;
                            }
                        }

                        string closestString = constantsList[index];

                        int groupId = Constants[closestString];
                        tuple.SetRawValue(0, groupId);
                    }
                }

                Dictionary <int, ReadableTuple <int> > dico = (Dictionary <int, ReadableTuple <int> >)item.GetRawValue(1);
                key = item.GetKey <int>();

                foreach (var pair in dico.OrderBy(p => p.Key))
                {
                    var           dbTuple = itemDb.TryGetTuple(pair.Key);
                    List <string> items   = ServerItemGroupSubAttributes.AttributeList.Attributes.Select(p => pair.Value.GetValue <string>(p)).ToList();
                    RemoveDefaultValues(items);
                    builder.AppendLine(key + "," + string.Join(",", items.ToArray()) + (dbTuple == null ? "" : "\t// " + dbTuple.GetValue(ServerItemAttributes.Name)));
                }

                builder.AppendLine();
            }
            else if (destServer == ServerType.Hercules)
            {
                builder.AppendLine(ItemGroupParser.ToHerculesDbEntry(db, item.GetKey <int>(), aegisNames, names));
                builder.AppendLine();
            }
        }
Beispiel #5
0
		public static string ToHerculesEntry(BaseDb db, int itemId) {
			var dbItems = db.GetMeta<int>(ServerDbs.Items);

			StringBuilder builder = new StringBuilder();

			var tuple = dbItems.TryGetTuple(itemId);

			if (tuple != null) {
				builder.AppendLineUnix("{");
				builder.AppendLineUnix("\tId: " + tuple.GetKey<int>().ToString(CultureInfo.InvariantCulture));
				builder.AppendLineUnix("\tAegisName: \"" + tuple.GetValue<string>(ServerItemAttributes.AegisName) + "\"");
				builder.AppendLineUnix("\tName: \"" + tuple.GetValue<string>(ServerItemAttributes.Name) + "\"");

				builder.AppendLineUnix("\tType: " + tuple.GetValue<string>(ServerItemAttributes.Type));
				_trySet(tuple, builder, ServerItemAttributes.Buy);
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Sell, (tuple.GetValue<int>(ServerItemAttributes.Buy) / 2).ToString(CultureInfo.InvariantCulture));
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Weight, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Attack, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Matk, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Defense, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Range, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.NumberOfSlots, "0");
				_trySetIfDefaultEmptyAddHex(tuple, builder, ServerItemAttributes.ApplicableJob, "");
				_trySetIfDefaultEmptyUpper(tuple, builder, ServerItemAttributes.Upper);
				_trySetGender(tuple, builder, ServerItemAttributes.Gender, "2");
				_trySetIfDefaultLocation(tuple, builder, ServerItemAttributes.Location);
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.WeaponLevel, "0");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.EquipLevel, "0");
				_trySetIfRefineable(tuple, builder, ServerItemAttributes.Refineable, true);
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.ClassNumber, "0");
				_trySetIfDefaultBoolean(tuple, builder, ServerItemAttributes.BindOnEquip, false);
				_trySetIfDefaultBoolean(tuple, builder, ServerItemAttributes.BuyingStore, false);
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Delay, "0");

				var trade = tuple.GetRawValue(ServerItemAttributes.Trade.Index) as Trade;
				if (trade != null && trade.NeedPrinting()) builder.AppendLineUnix(trade.ToWriteString());

				var nouse = tuple.GetRawValue(ServerItemAttributes.NoUse.Index) as NoUse;
				if (nouse != null && nouse.NeedPrinting()) builder.AppendLineUnix(nouse.ToWriteString());

				_trySetIfDefaultEmptyBracket(tuple, builder, ServerItemAttributes.Stack, "");
				_trySetIfDefaultEmpty(tuple, builder, ServerItemAttributes.Sprite, "0");
				_trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.Script, "");
				_trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.OnEquipScript, "");
				_trySetIfDefaultEmptyScript(tuple, builder, ServerItemAttributes.OnUnequipScript, "");
				builder.Append("},");
			}

			return builder.ToString();
		}