Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1.         /// <summary>
  2.         /// Represent info about particular file within a .torrent file.
  3.         /// </summary>
  4.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  5.         public struct TorrentInfoType_FilesList
  6.         {
  7.             // Full file path within a .torrent
  8.             [MarshalAs(UnmanagedType.LPWStr)]
  9.             public string filepath;
  10.  
  11.             // Filename of a file within a .torrent
  12.             [MarshalAs(UnmanagedType.LPWStr)]
  13.             public string filename;
  14.            
  15.             // Filesize of a file within a .torrent
  16.             public Int64 filesize;
  17.         }
  18.        
  19.         /// <summary>
  20.         /// Represent general information about a .torrent file.
  21.         /// </summary>
  22.         /// <see cref="DLL->TorrentInfo.cpp/h"/>
  23.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  24.         public struct TorrentInfoType
  25.         {
  26.             // Is torrent valid?
  27.             [MarshalAs(UnmanagedType.U1)]
  28.             public bool IsValid;
  29.  
  30.             // Torrent name/title.
  31.             [MarshalAs(UnmanagedType.LPWStr)]
  32.             public string name;
  33.  
  34.             // Torrent sha1 hash.
  35.             [MarshalAs(UnmanagedType.LPWStr)]
  36.             public string hash;
  37.  
  38.             // Torrent creation date
  39.             public Int64 creationDate;
  40.  
  41.             // Torrent creator (usually, software ะดัˆะปัƒ uTorrent/3310)
  42.             [MarshalAs(UnmanagedType.LPWStr)]
  43.             public string creator;
  44.  
  45.             // Torrent author's comment.
  46.             [MarshalAs(UnmanagedType.LPWStr)]
  47.             public string comment;
  48.  
  49.             // Is torrent private?
  50.             [MarshalAs(UnmanagedType.U1)]
  51.             public bool IsPrivate;
  52.  
  53.             // Total number of bytes the torrent-file represents (all the files in it).
  54.             public Int64 totalSize;
  55.  
  56.             // Files in torrent.
  57.             public int filesCount;
  58.  
  59.             public int pieceLength;
  60.             public int piecesCount;
  61.  
  62.             // Files list in .torrent
  63.             [MarshalAs(UnmanagedType.LPArray)]
  64.             public TorrentInfoType_FilesList[] files;
  65.         }
  66.  
  67.         [DllImport(Globals.WRAPPER_DLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
  68.         private static extern TORRENT_ERROR dll_TorrentGetInfo(string filepath, IntPtr infoStruct);
  69.  
  70. ...
  71.  
  72.         /// <summary>
  73.         /// Retrieves .torrent information (creator, comment, files count and so on)
  74.         /// </summary>
  75.         /// <param name="filepath">full path to .torrent file (ex. C:\torr.torrent)</param>
  76.         /// <returns></returns>
  77.         public static bool GetTorrentInfo(string filepath, out TorrentInfoType info)
  78.         {
  79.             // Init structure with default values.
  80.             info = new TorrentInfoType()
  81.             {
  82.                 IsValid = false,
  83.                 name = "Unknown",
  84.                 hash = "",
  85.                 creationDate = 0,
  86.                 creator = "Unknown creator",
  87.                 comment = "",
  88.                 IsPrivate = false,
  89.                 totalSize = 0,
  90.                 filesCount = 0,
  91.                 pieceLength = 0,
  92.                 piecesCount = 0,
  93.                 files = new TorrentInfoType_FilesList[]
  94.                 {
  95.                    
  96.                 }
  97.             };
  98.  
  99.             // Is .torrent exists?
  100.             if (!File.Exists(filepath))
  101.             {
  102.                 AppCore.ShowErr(
  103.                     String.Format(AppCore.LS("Error.FileNotExists"), filepath)
  104.                 );
  105.  
  106.                 return false;
  107.             }
  108.  
  109.             // Allocate structure to be ready marshalled in/out DLL.
  110.             int tempSize = Marshal.SizeOf(typeof(TorrentInfoType));
  111.             IntPtr pInfo = Marshal.AllocHGlobal(tempSize);
  112.             Marshal.StructureToPtr(info, pInfo, false);
  113.  
  114.             // Query info.
  115.             var err = dll_TorrentGetInfo(filepath, pInfo);
  116.             if (err == TORRENT_ERROR.TE_INFO_INVALIDTORRENT)
  117.             {
  118.                 Marshal.FreeHGlobal(pInfo);
  119.                 return false;
  120.             }
  121.  
  122.             // Update info structure with received data.
  123.             info = (TorrentInfoType)(Marshal.PtrToStructure(pInfo, typeof(TorrentInfoType)));
  124.             // Free memory.
  125.             Marshal.FreeHGlobal(pInfo);
  126.             DumpTorrentInfoData(info);
  127.  
  128.             return true;
  129.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement