Advertisement
Anaristos

CLR function to handle inv data.

Apr 9th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.32 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Microsoft.SqlServer.Server;
  8.  
  9. namespace EquFunctions
  10. {
  11.     public partial class InvHandler
  12.     {
  13.         // ParseInventory Procedure
  14.         // Returns the row data for an inventory entry (invdata).
  15.         [SqlProcedure]
  16.         public static void ParseInventory(string data)
  17.         {
  18.             SqlMetaData cObjectID    = new SqlMetaData("ObjectID",    SqlDbType.BigInt);
  19.             SqlMetaData cFlags       = new SqlMetaData("Flags",       SqlDbType.Int);
  20.             SqlMetaData cItemFlags   = new SqlMetaData("ItemFlags",   SqlDbType.VarChar, 16);
  21.             SqlMetaData cName        = new SqlMetaData("Name",        SqlDbType.NVarChar, 1024);
  22.             SqlMetaData cDisplay     = new SqlMetaData("Display",     SqlDbType.NVarChar, 1024);
  23.             SqlMetaData cLevel       = new SqlMetaData("Level",       SqlDbType.TinyInt);
  24.             SqlMetaData cType        = new SqlMetaData("Type",        SqlDbType.TinyInt);
  25.             SqlMetaData cWear        = new SqlMetaData("Wear",        SqlDbType.SmallInt);
  26.             SqlMetaData cTimer       = new SqlMetaData("Timer",       SqlDbType.Int);
  27.             SqlMetaData cContainerID = new SqlMetaData("ContainerID", SqlDbType.BigInt);
  28.  
  29.             Dictionary<char, int> fd = new Dictionary<char, int>() { {'K', (int) invflags.keep },
  30.                                                                      {'M', (int) invflags.magic},
  31.                                                                      {'H', (int) invflags.hum  },
  32.                                                                      {'I', (int) invflags.invis},
  33.                                                                      {'G', (int) invflags.glow },
  34.                                                                      {'T', (int) invflags.temp } };
  35.  
  36.             string ESC = @"";
  37.             string STX = @"";
  38.  
  39.             Hashtable ct = new Hashtable() { {"R", ESC + @"[1;31m"},
  40.                                              {"r", ESC + @"[0;31m"},
  41.                                              {"G", ESC + @"[1;32m"},
  42.                                              {"g", ESC + @"[0;32m"},
  43.                                              {"Y", ESC + @"[1;33m"},
  44.                                              {"y", ESC + @"[0;33m"},
  45.                                              {"B", ESC + @"[1;34m"},
  46.                                              {"b", ESC + @"[0;34m"},
  47.                                              {"M", ESC + @"[1;35m"},
  48.                                              {"m", ESC + @"[0;35m"},
  49.                                              {"C", ESC + @"[1;36m"},
  50.                                              {"c", ESC + @"[0;36m"},                                        
  51.                                              {"W", ESC + @"[1;37m"},
  52.                                              {"w", ESC + @"[0;37m"},
  53.                                              {"D", ESC + @"[1;30m"}};
  54.  
  55.             SqlDataRecord r;
  56.  
  57.             Regex pattern = new Regex(@"(?<id>\d+),(?<iflgs>\w+)?,(?<disp>.+?),(?<lvl>\d+),(?<type>\d+),(?<unq>\d+),(?<wear>-?\d+),(?<tmr>-?\d+),(?<cid>\d+)");
  58.  
  59.             Regex dpatt1 = new Regex(@"^([^@]+)");
  60.  
  61.             Regex dpatt2 = new Regex(@"@(\w)([^@|$]+)");
  62.  
  63.             r = new SqlDataRecord(cObjectID, cFlags, cItemFlags, cDisplay, cName, cLevel, cType, cWear, cTimer, cContainerID);
  64.  
  65.             int flags = 0;
  66.  
  67.             Byte  t08;
  68.             Int16 t16;
  69.             Int32 t32;
  70.             Int64 t64;
  71.  
  72.             string items;
  73.  
  74.             Match m = pattern.Match(data);
  75.  
  76.             if (m.Success)
  77.             {
  78.                 Int64.TryParse(m.Groups["id"].Value, out t64);
  79.  
  80.                 r.SetInt64(0, t64);
  81.  
  82.                 items = m.Groups["iflgs"].Value;
  83.  
  84.                 r.SetString(2, items);
  85.  
  86.                 string tname = "", dname = "";
  87.  
  88.                 StringBuilder sb = new StringBuilder(m.Groups["disp"].Value);
  89.  
  90.                 sb.Replace(@"""", "");
  91.  
  92.                 sb.Replace("@-", "~");
  93.  
  94.                 sb.Replace("@@", STX);
  95.  
  96.                 string name = sb.ToString();
  97.  
  98.                 Match match1 = dpatt1.Match(name);
  99.  
  100.                 int len = match1.Groups[1].Length;
  101.  
  102.                 if (len > 0)
  103.                 {
  104.                     tname = ct["w"] + match1.Groups[1].Value;
  105.  
  106.                     dname = match1.Groups[1].Value;
  107.  
  108.                     name = name.Substring(len);
  109.                 }
  110.  
  111.                 if (name.Length > 0)
  112.                 {
  113.                     foreach (Match m2 in dpatt2.Matches(name))
  114.                     {
  115.                         tname += ct[m2.Groups[1].Value] + m2.Groups[2].Value;
  116.  
  117.                         dname += m2.Groups[2].Value;
  118.                     }
  119.                 }
  120.  
  121.                 tname = tname.Replace(STX, "@");
  122.  
  123.                 if (dname == "") dname = tname;
  124.  
  125.                 else dname = dname.Replace(STX, "@");
  126.  
  127.                 if (tname == dname) tname = ct["w"] + tname;
  128.  
  129.                 if (!name.EndsWith(ESC + @"[0;37m")) name += ESC + @"[0;37m";
  130.  
  131.                 r.SetString(3, tname);
  132.  
  133.                 r.SetString(4, dname);
  134.  
  135.                 Byte.TryParse(m.Groups["lvl"].Value, out t08);
  136.  
  137.                 r.SetByte(5, t08);
  138.  
  139.                 Byte.TryParse(m.Groups["type"].Value, out t08);
  140.  
  141.                 r.SetByte(6, t08);
  142.  
  143.                 Byte.TryParse(m.Groups["unq"].Value, out t08);
  144.  
  145.                 flags = (t08 != 0) ? (int)invflags.unique : 0;
  146.  
  147.                 Int16.TryParse(m.Groups["wear"].Value, out t16);
  148.  
  149.                 r.SetInt16(7, t16);
  150.  
  151.                 Int32.TryParse(m.Groups["tmr"].Value, out t32);
  152.  
  153.                 r.SetInt32(8, t32);
  154.  
  155.                 Int64.TryParse(m.Groups["cid"].Value, out t64);
  156.  
  157.                 r.SetInt64(9, t64);
  158.  
  159.                 if (items != null) for (int i = 0; i < items.Length; i++) flags |= fd[items[i]];
  160.  
  161.                 r.SetInt32(1, flags);
  162.  
  163.                 SqlContext.Pipe.Send(r);
  164.             }
  165.         }
  166.  
  167.         // ParseItem Procedure
  168.         // Returns the row data for an inventory entry (invitem).
  169.         public static void ParseItem(string data)
  170.         {
  171.             SqlMetaData cObjectID    = new SqlMetaData("ObjectID",    SqlDbType.BigInt);
  172.             SqlMetaData cFlags       = new SqlMetaData("Flags",       SqlDbType.Int);
  173.             SqlMetaData cItemFlags   = new SqlMetaData("ItemFlags",   SqlDbType.VarChar, 16);
  174.             SqlMetaData cName        = new SqlMetaData("Name",        SqlDbType.NVarChar, 1024);
  175.             SqlMetaData cDisplay     = new SqlMetaData("Display",     SqlDbType.NVarChar, 1024);
  176.             SqlMetaData cLevel       = new SqlMetaData("Level",       SqlDbType.TinyInt);
  177.             SqlMetaData cType        = new SqlMetaData("Type",        SqlDbType.TinyInt);
  178.             SqlMetaData cWear        = new SqlMetaData("Wear",        SqlDbType.SmallInt);
  179.             SqlMetaData cTimer       = new SqlMetaData("Timer",       SqlDbType.Int);
  180.             SqlMetaData cContainerID = new SqlMetaData("ContainerID", SqlDbType.BigInt);
  181.  
  182.             Dictionary<char, int> fd = new Dictionary<char, int>() { {'K', (int) invflags.keep },
  183.                                                                      {'M', (int) invflags.magic},
  184.                                                                      {'H', (int) invflags.hum  },
  185.                                                                      {'I', (int) invflags.invis},
  186.                                                                      {'G', (int) invflags.glow },
  187.                                                                      {'T', (int) invflags.temp } };
  188.  
  189.             string ESC = @"";
  190.  
  191.             SqlDataRecord r;
  192.  
  193.             Regex pattern = new Regex(@"(?<id>\d+),(?<iflgs>\w+)?,(?<disp>[^,]+),(?<lvl>\d+),(?<type>\d+),(?<unq>\d+),(?<wear>-?\d+),(?<tmr>-?\d+)");
  194.  
  195.             Regex dpatt = new Regex(@"\[(?:[0-1];)?\d+m([^|$]+)");
  196.  
  197.             r = new SqlDataRecord(cObjectID, cFlags, cItemFlags, cDisplay, cName, cLevel, cType, cWear, cTimer, cContainerID);
  198.  
  199.             int flags = 0;
  200.  
  201.             Byte  t08;
  202.             Int16 t16;
  203.             Int32 t32;
  204.             Int64 t64;
  205.  
  206.             string items;
  207.  
  208.             string tname = "";
  209.  
  210.             Match m = pattern.Match(data);
  211.  
  212.             if (m.Success)
  213.             {
  214.                 Int64.TryParse(m.Groups["id"].Value, out t64);
  215.  
  216.                 r.SetInt64(0, t64);
  217.  
  218.                 items = m.Groups["iflgs"].Value;
  219.  
  220.                 r.SetString(2, items);
  221.  
  222.                 string name = m.Groups["disp"].Value;
  223.  
  224.                 Match mf = dpatt.Match(name);
  225.  
  226.                 if (mf.Success)
  227.                 {
  228.                     if (mf.Index > 0) name = ESC + @"[0;37m" + name;
  229.                 }
  230.  
  231.                 foreach (Match m3 in dpatt.Matches(name)) tname += m3.Groups[1].Value;
  232.  
  233.                 if (tname == "")
  234.                 {
  235.                     tname = name;
  236.                     name = ESC + @"[0;37m" + name;
  237.                 }
  238.  
  239.                 if (!name.EndsWith(ESC + @"[0;37m")) name += ESC + @"[0;37m";
  240.  
  241.                 r.SetString(3, name);
  242.  
  243.                 r.SetString(4, tname);
  244.  
  245.                 Byte.TryParse(m.Groups["lvl"].Value, out t08);
  246.  
  247.                 r.SetByte(5, t08);
  248.  
  249.                 Byte.TryParse(m.Groups["type"].Value, out t08);
  250.  
  251.                 r.SetByte(6, t08);
  252.  
  253.                 Byte.TryParse(m.Groups["unq"].Value, out t08);
  254.  
  255.                 flags = (t08 != 0) ? (int)invflags.unique : 0;
  256.  
  257.                 Int16.TryParse(m.Groups["wear"].Value, out t16);
  258.  
  259.                 r.SetInt16(7, t16);
  260.  
  261.                 Int32.TryParse(m.Groups["tmr"].Value, out t32);
  262.  
  263.                 r.SetInt32(8, t32);
  264.  
  265.                 Int64.TryParse(m.Groups["cid"].Value, out t64);
  266.  
  267.                 r.SetInt64(9, 0);
  268.  
  269.                 if (items != null) for (int i = 0; i < items.Length; i++) flags |= fd[items[i]];
  270.  
  271.                 r.SetInt32(1, flags);
  272.  
  273.                 SqlContext.Pipe.Send(r);
  274.             }
  275.         }
  276.  
  277.         // GetInvEntry Procedure
  278.         // Returns the row data for an inventory entry.
  279.         [SqlProcedure]
  280.         public static void ParseEquipment(string data)
  281.         {
  282.             SqlMetaData cObjectID    = new SqlMetaData("ObjectID",    SqlDbType.BigInt);
  283.             SqlMetaData cFlags       = new SqlMetaData("Flags",       SqlDbType.Int);
  284.             SqlMetaData cName        = new SqlMetaData("Name",        SqlDbType.VarChar, 128);
  285.             SqlMetaData cLevel       = new SqlMetaData("Level",       SqlDbType.TinyInt);
  286.             SqlMetaData cType        = new SqlMetaData("Type",        SqlDbType.TinyInt);
  287.             SqlMetaData cWear        = new SqlMetaData("Wear",        SqlDbType.SmallInt);
  288.             SqlMetaData cContainerID = new SqlMetaData("ContainerID", SqlDbType.BigInt);
  289.  
  290.             Dictionary<char, int> fd = new Dictionary<char, int>() { {'K', (int) invflags.keep },
  291.                                                                      {'M', (int) invflags.magic},
  292.                                                                      {'H', (int) invflags.hum  },
  293.                                                                      {'I', (int) invflags.invis},
  294.                                                                      {'G', (int) invflags.glow },
  295.                                                                      {'T', (int) invflags.temp } };
  296.  
  297.             SqlDataRecord r;
  298.  
  299.             Regex pattern = new Regex(@"(?<id>\d+),(?<iflgs>\w+),(?<name>\w+),(?<lvl>\d+),(?<type>\d+),(?<wear>-?\d+),(?<cid>\d+)");
  300.  
  301.             r = new SqlDataRecord(cObjectID, cFlags, cName, cLevel, cType, cWear, cContainerID);
  302.  
  303.             int flags = 0;
  304.  
  305.             Byte  t08;
  306.             Int16 t16;
  307.             Int64 t64;
  308.  
  309.             Match m = pattern.Match(data);
  310.  
  311.             if (m.Success)
  312.             {
  313.                 Int64.TryParse(m.Groups["id"].Value, out t64);
  314.  
  315.                 r.SetInt64(0, t64);
  316.  
  317.                 string items = m.Groups["iflgs"].Value;
  318.  
  319.                 for (int i = 0; i < items.Length; i++) flags |= fd[items[i]];
  320.  
  321.                 r.SetInt32(1, flags);
  322.  
  323.                 r.SetString(2, m.Groups["name"].Value);
  324.  
  325.                 Byte.TryParse(m.Groups["lvl"].Value, out t08);
  326.  
  327.                 r.SetByte(3, t08);
  328.  
  329.                 Byte.TryParse(m.Groups["type"].Value, out t08);
  330.  
  331.                 r.SetByte(4, t08);
  332.  
  333.                 Int16.TryParse(m.Groups["wear"].Value, out t16);
  334.  
  335.                 r.SetInt16(5, t16);
  336.  
  337.                 Int64.TryParse(m.Groups["cid"].Value, out t64);
  338.  
  339.                 r.SetInt64(6, t64);
  340.  
  341.                 SqlContext.Pipe.Send(r);
  342.             }
  343.         }
  344.     }
  345.  
  346.     //This enum describes the items flags.
  347.     [FlagsAttribute]
  348.     enum invflags
  349.     {
  350.         None   =   0, //no flags are set.
  351.         keep   =   1, //the item is kept.
  352.         invis  =   2, //the item is invisible.
  353.         magic  =   4, //the item is magic.
  354.         glow   =   8, //the item glows.
  355.         hum    =  16, //the item hums.
  356.         temp   =  32, //the item has a temporary timer.
  357.         unique = 128, //the item has been marked as unique.
  358.         ALL = keep | invis | magic | glow | hum | temp | unique
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement