Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Complete, ready to use TypeID parse (c# 7+)

Joined
Jul 23, 2011
Messages
391
Reaction score
664
Hi there, it has been pretty long since I've released anything for the SRO community, guess it's time. It isn't anything big, but yet will be useful for many devs. Basically, this peace of code lets you easily "parse" type ids that are widely used @ sro.

It uses value tuple language feature, which is only available in c# 7 and newer.

Code:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace Phoenix.FilterApp
{
    //TID 1: 0 -> DUMMY_OBJECT, 1 -> Character, 2 -> Unplayable (?), 3 -> Item, 4 -> Store
    //TID 2: Variable
    //TID 3: Variable
    //TID 4: Variable
    [DebuggerDisplay("{ToString()}")]
    [StructLayout(LayoutKind.Sequential, Pack = 1, Size = sizeof(ushort))]
    public struct TypeID : IEquatable<TypeID>
    {
        private const int IsCashItemOffset = 0;
        private const int IsCashItemSize = 1;
        private const ushort IsCashItemMask = ((1 << IsCashItemSize) - 1) << IsCashItemOffset;

        private const int IsBionicOffset = IsCashItemOffset + IsCashItemSize;
        private const int IsBionicSize = 1;
        private const ushort IsBionicMask = ((1 << IsBionicSize) - 1) << IsBionicOffset;

        private const int FirstTidOffset = IsBionicOffset + IsBionicSize;
        private const int FirstTidSize = 3;
        private const ushort FirstTidMask = ((1 << FirstTidSize) - 1) << FirstTidOffset;

        private const int SecondTidOffset = FirstTidOffset + FirstTidSize;
        private const int SecondTidSize = 2;
        private const ushort SecondTidMask = ((1 << SecondTidSize) - 1) << SecondTidOffset;

        private const int ThirdTidOffset = SecondTidOffset + SecondTidSize;
        private const int ThirdTidSize = 4;
        private const ushort ThirdTidMask = ((1 << ThirdTidSize) - 1) << ThirdTidOffset;

        private const int FourthTidOffset = ThirdTidOffset + ThirdTidSize;
        private const int FourthTidSize = 5;
        private const ushort FourthTidMask = ((1 << FourthTidSize) - 1) << FourthTidOffset;

        public ushort Value 
        { get; set; }

        public bool IsCashItem
        {
            get => ((this.Value & IsCashItemMask) >> IsCashItemOffset) > 0;
            set => this.Value = (ushort)((this.Value & ~IsCashItemMask) | ((Convert.ToByte(value) << IsCashItemOffset) & IsCashItemMask));
        }

        public bool IsBionic
        {
            get => ((this.Value & IsBionicMask) >> IsBionicOffset) > 0;
            set => this.Value = (ushort)((this.Value & ~IsBionicMask) | ((Convert.ToByte(value) << IsBionicOffset) & IsBionicMask));
        }

        public byte First
        {
            get => (byte)((this.Value & FirstTidMask) >> FirstTidOffset);
            set => this.Value = (ushort)((this.Value & ~FirstTidMask) | ((Convert.ToByte(value) << FirstTidOffset) & FirstTidMask));
        }

        public byte Second
        {
            get => (byte)((this.Value & SecondTidMask) >> SecondTidOffset);
            set => this.Value = (ushort)((this.Value & ~SecondTidMask) | ((Convert.ToByte(value) << SecondTidOffset) & SecondTidMask));
        }


        public byte Third
        {
            get => (byte)((this.Value & ThirdTidMask) >> ThirdTidOffset);
            set => this.Value = (ushort)((this.Value & ~ThirdTidMask) | ((Convert.ToByte(value) << ThirdTidOffset) & ThirdTidMask));
        }

        public byte Fourth
        {
            get => (byte)((this.Value & FourthTidMask) >> FourthTidOffset);
            set => this.Value = (ushort)((this.Value & ~FourthTidMask) | ((Convert.ToByte(value) << FourthTidOffset) & FourthTidMask));
        }

        public TypeID(ushort value) => this.Value = value;

        public TypeID(bool isCashItem, bool isBionic, byte first,  byte second, byte third, byte fourth)  : 
            this(default)
        {
            this.IsCashItem = isCashItem;
            this.IsBionic = isBionic;
            this.First = first;
            this.Second = second;
            this.Third = third;
            this.Fourth = fourth;
        }

        #region IEquatable & related

        public static bool operator ==(TypeID first, TypeID second) => first.Equals(second);
        public static bool operator !=(TypeID first, TypeID second) => !first.Equals(second);
        public override bool Equals(object obj) => obj is TypeID id && this.Equals(id);
        public bool Equals(TypeID other) => this.Value == other.Value;

        #endregion

        public override int GetHashCode() => (IsCashItem, IsBionic, First, Second, Third, Fourth).GetHashCode();

        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.AppendLine($"IsCashItem = {this.IsCashItem}");
            sb.AppendLine($"IsBionic = {this.IsBionic}");
            sb.AppendLine($"First = {this.First}");
            sb.AppendLine($"Second = {this.Second}");
            sb.AppendLine($"Third = {this.Third}");
            sb.AppendLine($"Fourth = {this.Fourth}");

            return sb.ToString();
        }

        public static implicit operator ushort(TypeID id) => id.Value;
        public static explicit operator TypeID(ushort value) => new TypeID(value);
    }
}
 
Back
Top