Guest User

MapExport Wrapper

a guest
Sep 27th, 2015
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using EUDGen.Communication.Messages;
  4. using EUDGen.Communication.System;
  5.  
  6. namespace EUDGen.Models.Data
  7. {
  8.     public static class MapExport
  9.     {
  10.         public const string DllName = "MapExport.dll";
  11.  
  12.         #region Extern method signatures
  13.  
  14.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  15.         private static extern int openMap(string path);
  16.  
  17.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  18.         private static extern int saveMap();
  19.  
  20.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  21.         private static extern int openTrg(string path);
  22.  
  23.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  24.         private static extern int saveTrg();
  25.  
  26.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  27.         private static extern int newTrigger();
  28.  
  29.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  30.         private static extern int setPlayer(int id);
  31.  
  32.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  33.         private static extern int addDeaths(int player, int unit, int comparison, int amount);
  34.  
  35.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  36.         private static extern int addSetDeaths(int player, int unit, int modifier, int amount);
  37.  
  38.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  39.         private static extern int addComment(string comment);
  40.  
  41.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  42.         private static extern int addSwitch(int sw, int state);
  43.  
  44.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  45.         private static extern int addSetSwitch(int sw, int state);
  46.  
  47.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  48.         private static extern int addPreserve();
  49.  
  50.         [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  51.         private static extern void clear();
  52.  
  53.         #endregion
  54.  
  55.         private static readonly ExportLogMessage LogMessage = new ExportLogMessage(null, false);
  56.  
  57.         private static void SendLogMessage(string text, bool terminatingMessage = false)
  58.         {
  59.             LogMessage.Log = text;
  60.             LogMessage.TerminatingMessage = terminatingMessage;
  61.             Messenger.Send(LogMessage);
  62.         }
  63.  
  64.         public static bool TryOpenMap(string filename)
  65.         {
  66.             return TryFunc(() => openMap(filename));
  67.         }
  68.  
  69.         public static bool TrySaveMap()
  70.         {
  71.             return TryFunc(saveMap);
  72.         }
  73.  
  74.         public static bool TryOpenTrg(string filename)
  75.         {
  76.             return TryFunc(() => openTrg(filename));
  77.         }
  78.  
  79.         public static bool TrySaveTrg()
  80.         {
  81.             return TryFunc(saveTrg);
  82.         }
  83.  
  84.         public static bool TryNewTrigger()
  85.         {
  86.             return TryFunc(newTrigger);
  87.         }
  88.  
  89.         public static bool TrySetPlayer(int id)
  90.         {
  91.             return TryFunc(() => setPlayer(id));
  92.         }
  93.  
  94.         public static bool TryAddCondition(int player, int unit, int comparison, int amount)
  95.         {
  96.             return TryFunc(() => addDeaths(player, unit, comparison, amount));
  97.         }
  98.  
  99.         public static bool TryAddAction(int player, int unit, int modifier, int amount)
  100.         {
  101.             return TryFunc(() => addSetDeaths(player, unit, modifier, amount));
  102.         }
  103.  
  104.         public static bool TryAddComment(string comment)
  105.         {
  106.             return TryFunc(() => addComment(comment));
  107.         }
  108.  
  109.         public static bool TrySwitch(int switchNumber, int switchState)
  110.         {
  111.             return TryFunc(() => addSwitch(switchNumber, switchState));
  112.         }
  113.  
  114.         public static bool TryAddPreserve(int switchNumber, int switchState)
  115.         {
  116.             return TryFunc(addPreserve);
  117.         }
  118.  
  119.         public static bool TryAddSwitch(int switchNumber, int switchState)
  120.         {
  121.             return TryFunc(() => addSetSwitch(switchNumber, switchState));
  122.         }
  123.  
  124.         public static bool TryClear()
  125.         {
  126.             return TryAction(clear);
  127.         }
  128.  
  129.         private static bool TryAction(Action action)
  130.         {
  131.             return TryFunc(() =>
  132.             {
  133.                 action.Invoke();
  134.                 return 0;
  135.             });
  136.         }
  137.  
  138.         private static bool TryFunc(Func<int> func)
  139.         {
  140.             try
  141.             {
  142.                 return func.Invoke() == 0;
  143.             }
  144.             catch
  145.             {
  146.                 SendLogMessage("ERROR: MapExport exception");
  147.                 return false;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment