Advertisement
_DarkLex_

Untitled

Aug 11th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using MiNET;
  3. using MiNET.Net;
  4. using MiNET.Items;
  5. using MiNET.Utils;
  6. using fNbt;
  7. using log4net;
  8. namespace xCore
  9. {
  10.     public class HashedItemSigner : ItemSigner
  11.     {
  12.         public static string SessionTagKey { get; set; } = string.Empty;
  13.         //public static int SessionKey = 0;
  14.         private static readonly ILog Log = LogManager.GetLogger(typeof(HashedItemSigner));
  15.         private static bool _signItems = true;
  16.         public override Item SignItem(Item item)
  17.         {
  18.            
  19.             //if (SessionKey == 0) xCoreGames.rnd.Next(0, 100000);
  20.             if(string.IsNullOrEmpty(SessionTagKey))
  21.             {
  22.                 var m = xCoreGames.rnd.Next(10, 20);
  23.                 for (var i = 0; i < m; i++)
  24.                     SessionTagKey += (char)xCoreGames.rnd.Next(0, 40);
  25.             }
  26.             //if (!_signItems) return item;
  27.             if (item.ExtraData == null) item.ExtraData = new NbtCompound();
  28.             //item.ExtraData["Hash"] = new NbtInt("Hash", 911);
  29.             {
  30.                 if (!item.ExtraData.Contains(SessionTagKey)) {
  31.                     //lock(item.ExtraData)
  32.                     //{
  33.  
  34.                     var vTag = new NbtCompound(SessionTagKey);
  35.                     foreach (var t in item.ExtraData)
  36.                         vTag.Add((NbtTag)t.Clone());
  37.  
  38.                     var itemDat = new NbtCompound("ItemData");
  39.                     itemDat.Add(new NbtInt("Id", item.Id));
  40.                     itemDat.Add(new NbtShort("Metadata", item.Metadata));
  41.                     //itemDat.Add(new NbtByte("Count", item.Count));
  42.                     vTag.Add(itemDat);
  43.  
  44.                     item.ExtraData.Add(vTag);
  45.                     //}
  46.                     //item.ExtraData["Hash"] = new NbtInt(911);
  47.                 }
  48.             }
  49.             return item;
  50.         }
  51.  
  52.         public override bool VerifyItemStack(Player player, Item itemStack)
  53.         {
  54.             //if (!_signItems) return true;
  55.             if (itemStack.Id == 0 && itemStack.Count == 0 && itemStack.Metadata == 0) return true;
  56.             if (itemStack.ExtraData == null)
  57.             {
  58.                 Log.Error($"{player.Username} Missing ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
  59.                 return false;
  60.             }
  61.             //if (!itemStack.ExtraData.Contains(SessionTagKey))
  62.             //{
  63.             //  Log.Error($"{player.Username} Missing hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
  64.             //  return false;
  65.             //}
  66.             //if (tag.IntValue != SessionKey)
  67.             //{
  68.             //  Log.Error($"{player.Username} Invalid hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count} NBT: {itemStack.ExtraData}");
  69.             //  return false;
  70.             //}
  71.             NbtCompound signTag = null;
  72.             if (!itemStack.ExtraData.TryGet(SessionTagKey, out signTag))
  73.             {
  74.                 Log.Error($"{player.Username} Missing hash for ExtraData on item with ID: {itemStack.Id}, Meta: {itemStack.Metadata}, Count: {itemStack.Count}");
  75.                 return false;
  76.             }
  77.  
  78.             NbtCompound itemDatTag = null;
  79.             if (signTag.TryGet("ItemData", out itemDatTag))
  80.             {
  81.                 NbtInt idTag = null;
  82.                 NbtShort metadataTag = null;
  83.                 NbtByte countTag = null;
  84.                 if(itemDatTag.TryGet("Id", out idTag) && itemDatTag.TryGet("Metadata", out metadataTag) /*&& itemDatTag.TryGet("Count", out countTag)*/)
  85.                 {
  86.                     if(idTag.IntValue != itemStack.Id || metadataTag.ShortValue != itemStack.Metadata /*|| countTag.ByteValue != itemStack.Count*/)
  87.                     {
  88.                         return false;//id, metadata или count не совпадают с подписью
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     return false;//отсутствует информация об id, metadata и count
  94.                 }
  95.             }
  96.             else
  97.             {
  98.                 return false;//отсутствует информация об id, metadata и count
  99.             }
  100.  
  101.             foreach(var extradataTag in itemStack.ExtraData)
  102.             {
  103.                 if (extradataTag.Name == SessionTagKey) continue;
  104.                 NbtTag outTag = null;
  105.                 if (signTag.TryGet(extradataTag.Name, out outTag))
  106.                 {
  107.                     if (extradataTag != outTag)
  108.                     {
  109.                         return false;//неверный тег extradataTag.Name
  110.                     }
  111.                 }
  112.                 else
  113.                 {
  114.                     return false;//в SignData отсутствует тег extradataTag.Name
  115.                 }
  116.             }
  117.  
  118.             foreach (var sigTag in itemStack.ExtraData)
  119.             {
  120.                 if (sigTag.Name == "ItemData") continue;
  121.                 NbtTag outTag = null;
  122.                 if (!itemStack.ExtraData.TryGet(sigTag.Name, out outTag))
  123.                 {
  124.                     return false;//в ExtraData отсутствует тег extradataTag.Name
  125.                 }
  126.         }
  127.  
  128.             return true;
  129.         }
  130.     }
  131.  
  132.     public static class ItemSignerConfig
  133.     {
  134.         public static void AddTag(this Item item, NbtTag tag)
  135.         {
  136.             if (item.ExtraData == null) return;//Hacks...
  137.             NbtCompound signTag = null;
  138.             if (!item.ExtraData.TryGet(HashedItemSigner.SessionTagKey, out signTag)) return;//hacks...
  139.             signTag.Add((NbtTag)tag.Clone());
  140.             item.ExtraData.Add((NbtTag)tag.Clone());
  141.         }
  142.  
  143.         public static void RemoveTag(this Item item, string tagName)
  144.         {
  145.             if (item.ExtraData == null) return;//Hacks...
  146.             NbtCompound signTag = null;
  147.             if (!item.ExtraData.TryGet(HashedItemSigner.SessionTagKey, out signTag)) return;//hacks...
  148.             NbtTag tag = null;
  149.             if (signTag.TryGet(tagName, out tag))
  150.                 signTag.Remove(tag);
  151.             if (item.ExtraData.TryGet(tagName, out tag))
  152.                 item.ExtraData.Remove(tag);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement