Advertisement
dalvorsn

Untitled

Apr 21st, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Collections;
  6. using PluginInterface;
  7. using otitemeditor;
  8. using System.Diagnostics;
  9.  
  10. namespace Tibia860
  11. {
  12.     public class Plugin : IPlugin
  13.     {
  14.         Dictionary<UInt16, Sprite> sprites = new Dictionary<UInt16, Sprite>();
  15.         SpriteItems items = new SpriteItems();
  16.         List<SupportedClient> supportedClients = new List<SupportedClient>();
  17.  
  18.         IPluginHost myHost = null;
  19.  
  20.         //IPlugin implementation
  21.         public IPluginHost Host { get { return myHost; } set { myHost = value; } }
  22.  
  23.         public List<SupportedClient> SupportedClients { get { return supportedClients; } }
  24.         public SpriteItems Items { get { return items; } set { items = value; } }
  25.  
  26.         public bool LoadClient(SupportedClient client, string datFullPath, string sprFullPath)
  27.         {
  28.             return loadDat(datFullPath, client.datSignature) && loadSprites(sprFullPath, client.sprSignature);
  29.         }
  30.  
  31.         public void Initialize()
  32.         {
  33.             settings.Load("plugin860.xml");
  34.             supportedClients = settings.GetSupportedClientList();
  35.         }
  36.  
  37.         public void Dispose()
  38.         {
  39.             sprites.Clear();
  40.             items.Clear();
  41.         }
  42.  
  43.         //internal implementation
  44.         public Settings settings = new Settings();
  45.  
  46.         public bool loadSprites(string filename, UInt32 signature) { return Sprite.loadSprites(filename, ref sprites, signature); }
  47.  
  48.         public bool loadDat(string filename, UInt32 signature)
  49.         {
  50.             FileStream fileStream = new FileStream(filename, FileMode.Open);
  51.             try
  52.             {
  53.                 using (BinaryReader reader = new BinaryReader(fileStream))
  54.                 {
  55.                     UInt32 datSignature = reader.ReadUInt32();
  56.                     if (signature != 0 && datSignature != signature)
  57.                     {
  58.                         return false;
  59.                     }
  60.  
  61.                     //get max id
  62.                     UInt16 itemCount = reader.ReadUInt16();
  63.                     UInt16 creatureCount = reader.ReadUInt16();
  64.                     UInt16 effectCount = reader.ReadUInt16();
  65.                     UInt16 distanceCount = reader.ReadUInt16();
  66.  
  67.                     UInt16 minclientID = 100; //items starts at 100
  68.                     UInt16 maxclientID = itemCount;
  69.  
  70.                     UInt16 id = minclientID;
  71.                     while (id <= maxclientID)
  72.                     {
  73.                         SpriteItem item = new SpriteItem();
  74.                         item.id = id;
  75.                         items[id] = item;
  76.  
  77.                         // read the options until we find 0xff
  78.                         byte optbyte;
  79.                         do
  80.                         {
  81.                             optbyte = reader.ReadByte();
  82.                             //Trace.WriteLine(String.Format("{0:X}", optbyte));
  83.  
  84.                             switch (optbyte)
  85.                             {
  86.                                 case 0x00: //groundtile
  87.                                     {
  88.                                         item.groundSpeed = reader.ReadUInt16();
  89.                                         item.type = ItemType.Ground;
  90.                                     } break;
  91.  
  92.                                 case 0x01: //all OnTop
  93.                                     {
  94.                                         item.alwaysOnTop = true;
  95.                                         item.alwaysOnTopOrder = 1;
  96.                                     } break;
  97.  
  98.                                 case 0x02: //can walk trough (open doors, arces, bug pen fence)
  99.                                     {
  100.                                         item.alwaysOnTop = true;
  101.                                         item.alwaysOnTopOrder = 2;
  102.                                     } break;
  103.  
  104.                                 case 0x03: //can walk trough (arces)
  105.                                     {
  106.                                         item.alwaysOnTop = true;
  107.                                         item.alwaysOnTopOrder = 3;
  108.                                     } break;
  109.  
  110.                                 case 0x04: //container
  111.                                     {
  112.                                         item.type = ItemType.Container;
  113.                                     } break;
  114.  
  115.                                 case 0x05: //stackable
  116.                                     {
  117.                                         item.isStackable = true;
  118.                                         break;
  119.                                     }
  120.  
  121.                                 case 0x06: //unknown
  122.                                     {
  123.                                     } break;
  124.  
  125.                                 case 0x07: //useable
  126.                                     {
  127.                                         item.hasUseWith = true;
  128.                                     } break;
  129.  
  130.                                 case 0x08: //read/write-able
  131.                                     {
  132.                                         item.isReadable = true;
  133.                                         //item.isWriteable = true;
  134.                                         item.maxReadWriteChars = reader.ReadUInt16();
  135.                                     } break;
  136.  
  137.                                 case 0x09: //readable
  138.                                     {
  139.                                         item.isReadable = true;
  140.                                         item.maxReadChars = reader.ReadUInt16();
  141.                                     } break;
  142.  
  143.                                 case 0x0A: //fluid containers
  144.                                     {
  145.                                         item.type = ItemType.Fluid;
  146.                                     } break;
  147.  
  148.                                 case 0x0B: //splashes
  149.                                     {
  150.                                         item.type = ItemType.Splash;
  151.                                     } break;
  152.  
  153.                                 case 0x0C: //blocks solid objects (creatures, walls etc)
  154.                                     {
  155.                                         item.blockObject = true;
  156.                                     } break;
  157.  
  158.                                 case 0x0D: //not moveable
  159.                                     {
  160.                                         item.isMoveable = false;
  161.                                     } break;
  162.  
  163.                                 case 0x0E: //blocks missiles (walls, magic wall etc)
  164.                                     {
  165.                                         item.blockProjectile = true;
  166.                                     } break;
  167.  
  168.                                 case 0x0F: //blocks pathfind algorithms (monsters)
  169.                                     {
  170.                                         item.blockPathFind = true;
  171.                                     } break;
  172.  
  173.                                 case 0x10: //blocks monster movement (flowers, parcels etc)
  174.                                     {
  175.                                         item.isPickupable = true;
  176.                                     } break;
  177.  
  178.                                 case 0x11: //hangable objects (wallpaper etc)
  179.                                     {
  180.                                         item.isHangable = true;
  181.                                     } break;
  182.  
  183.                                 case 0x12: //horizontal wall
  184.                                     {
  185.                                         item.isHorizontal = true;
  186.                                     } break;
  187.  
  188.                                 case 0x13: //vertical wall
  189.                                     {
  190.                                         item.isVertical = true;
  191.                                     } break;
  192.  
  193.                                 case 0x14: //rotatable
  194.                                     {
  195.                                         item.isRotatable = true;
  196.                                     } break;
  197.  
  198.                                 case 0x15: //light info
  199.                                     {
  200.                                         item.lightLevel = reader.ReadUInt16();
  201.                                         item.lightColor = reader.ReadUInt16();
  202.                                     } break;
  203.  
  204.                                 case 0x16: //unknown
  205.                                     {
  206.                                     } break;
  207.  
  208.                                 case 0x17: //changes floor
  209.                                     {
  210.                                     } break;
  211.  
  212.                                 case 0x18: //unknown
  213.                                     {
  214.                                         reader.BaseStream.Seek(4, SeekOrigin.Current);
  215.                                     } break;
  216.  
  217.                                 case 0x19:
  218.                                     {
  219.                                         item.hasHeight = true;
  220.                                         UInt16 height = reader.ReadUInt16();
  221.                                     } break;
  222.  
  223.                                 case 0x1A: //unknown
  224.                                     {
  225.                                     } break;
  226.  
  227.  
  228.                                 case 0x1B: //unknown
  229.                                     {
  230.                                     } break;                                    
  231.  
  232.                                 case 0x1C: //minimap color
  233.                                     {
  234.                                         item.minimapColor = reader.ReadUInt16();
  235.                                         break;
  236.                                     }
  237.  
  238.                                 case 0x1D: //in-game help info
  239.                                     {
  240.                                         UInt16 opt = reader.ReadUInt16();
  241.                                         if (opt == 1112)
  242.                                         {
  243.                                             item.isReadable = true;
  244.                                         }
  245.                                     } break;
  246.  
  247.                                 case 0x1E: //unknown
  248.                                     {
  249.                                     } break;
  250.  
  251.                                 case 0x1F: //look through (borders)
  252.                                     {
  253.                                         item.lookThrough = true;
  254.                                     } break;
  255.  
  256.                                 case 0x20: //unknown
  257.                                     {
  258.                                     } break;
  259.  
  260.                                 case 0xFF: //end of attributes
  261.                                     {
  262.                                     } break;
  263.  
  264.                                 default:
  265.                                     {
  266.                                         Trace.WriteLine(String.Format("Plugin: Error while parsing, unknown optbyte {0:X}", optbyte));
  267.                                         return false;
  268.                                     }
  269.                             }
  270.                         } while (optbyte != 0xFF);
  271.  
  272.                         item.width = reader.ReadByte();
  273.                         item.height = reader.ReadByte();
  274.                         if ((item.width > 1) || (item.height > 1))
  275.                         {
  276.                             reader.BaseStream.Position++;
  277.                         }
  278.  
  279.                         item.frames = reader.ReadByte();
  280.                         item.xdiv = reader.ReadByte();
  281.                         item.ydiv = reader.ReadByte();
  282.                         item.zdiv = reader.ReadByte();
  283.                         item.animationLength = reader.ReadByte();
  284.  
  285.                         item.numSprites =
  286.                             (UInt32)item.width * (UInt32)item.height *
  287.                             (UInt32)item.frames *
  288.                             (UInt32)item.xdiv * (UInt32)item.ydiv * item.zdiv *
  289.                             (UInt32)item.animationLength;
  290.  
  291.                         // Read the sprite ids
  292.                         for (UInt32 i = 0; i < item.numSprites; ++i)
  293.                         {
  294.                             UInt16 spriteId = reader.ReadUInt16();
  295.                             Sprite sprite;
  296.                             if (!sprites.TryGetValue(spriteId, out sprite))
  297.                             {
  298.                                 sprite = new Sprite();
  299.                                 sprite.id = spriteId;
  300.                                 sprites[spriteId] = sprite;
  301.                             }
  302.  
  303.                             item.spriteList.Add(sprite);
  304.                         }
  305.  
  306.                         ++id;
  307.                     }
  308.                 }
  309.             }
  310.             finally
  311.             {
  312.                 fileStream.Close();
  313.             }
  314.  
  315.             return true;
  316.         }
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement