Example #1
0
        /// <summary>
        /// Initializes the specified field offset. This will throw an exception if failed to find.
        /// </summary>
        /// <param name="typeId">The type identifier.</param>
        /// <param name="fieldId">The field identifier.</param>
        /// <exception cref="System.ArgumentException">Unable to initialize address with unique ID of  + id + !</exception>
        public static CachedFid Initialize(ulong typeId, uint fieldId)
        {
            var t = Main.GameInfo != null?Main.GameInfo.GetTypeInfo(typeId) : null;

            if (t == null)
            {
                throw new ArgumentException("Unable to initialize field offset due to type with unique ID of " + typeId + " was not found!");
            }

            var ls = t.Fields;

            GameInfo.GameFieldInfo fld = null;
            if (ls != null)
            {
                // Save some time, usually field id is also index.
                if (fieldId > 0 && fieldId <= int.MaxValue)
                {
                    int index = (int)fieldId - 1;
                    if (index < ls.Count)
                    {
                        var x = ls[index];
                        if (x.FieldId == fieldId)
                        {
                            fld = x;
                        }
                    }
                }

                if (fld == null)
                {
                    foreach (var x in ls)
                    {
                        if (x.FieldId == fieldId)
                        {
                            fld = x;
                            break;
                        }
                    }
                }
            }

            if (fld == null)
            {
                throw new ArgumentException("Unable to initialize field offset due to field with ID " + fieldId + " was not found in type " + (t.Name ?? "") + " (" + t.Id + ")!");
            }

            if (!fld.Begin.HasValue)
            {
                throw new ArgumentException("Unable to initialize field offset due to field with ID " + fieldId + " in type " + (t.Name ?? "") + " (" + t.Id + ") did not have a known offset!");
            }

            return(new CachedFid(fld.Begin.Value));
        }
Example #2
0
        /// <summary>
        /// Tries to initializes the specified field offset.
        /// </summary>
        /// <param name="typeId">The type identifier.</param>
        /// <param name="fieldId">The field identifier.</param>
        /// <returns></returns>
        public static CachedFid TryInitialize(ulong typeId, uint fieldId)
        {
            var t = Main.GameInfo != null?Main.GameInfo.GetTypeInfo(typeId) : null;

            if (t != null)
            {
                var ls = t.Fields;
                if (ls != null)
                {
                    GameInfo.GameFieldInfo fld = null;

                    // Save some time, usually field id is also index.
                    if (fieldId > 0 && fieldId <= int.MaxValue)
                    {
                        int index = (int)fieldId - 1;
                        if (index < ls.Count)
                        {
                            var x = ls[index];
                            if (x.FieldId == fieldId)
                            {
                                fld = x;
                            }
                        }
                    }

                    if (fld == null)
                    {
                        foreach (var x in ls)
                        {
                            if (x.FieldId == fieldId)
                            {
                                fld = x;
                                break;
                            }
                        }
                    }

                    if (fld != null && fld.Begin.HasValue)
                    {
                        return(new CachedFid(fld.Begin.Value));
                    }
                }
            }

            return(new CachedFid());
        }