Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using MoonSharp.Interpreter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using TwitchToolkit.IRC;
  8. using Verse;
  9.  
  10. namespace TwitchToolkit
  11. {
  12.     public class Command : Def
  13.     {
  14.  
  15.         public void RunCommand(IRCMessage message)
  16.         {
  17.             if (command == null)
  18.             {
  19.                 throw new Exception("Command is null");
  20.             }
  21.  
  22.             CommandDriver driver = (CommandDriver)Activator.CreateInstance(commandDriver);
  23.             driver.command = this;
  24.             driver.RunCommand(message);
  25.         }
  26.  
  27.         public string command = null;
  28.  
  29.         public bool enabled = true;
  30.  
  31.         public bool shouldBeInSeparateRoom = false;
  32.  
  33.         public Type commandDriver = typeof(CommandDriver);
  34.  
  35.         public bool requiresMod = false;
  36.  
  37.         public bool requiresAdmin = false;
  38.  
  39.         public string outputMessage = "";
  40.  
  41.         public bool isCustomMessage = false;
  42.     }
  43.  
  44.     public class CommandDriver
  45.     {
  46.         public Command command = null;
  47.  
  48.         public virtual void RunCommand(IRCMessage message)
  49.         {
  50.             Helper.Log("filtering command");
  51.  
  52.             string output = FilterTags(message, command.outputMessage);
  53.  
  54.             Helper.Log("command filtered");
  55.  
  56.             Toolkit.client.SendMessage(output);
  57.         }
  58.  
  59.         public string FilterTags(IRCMessage message, string input)
  60.         {
  61.             Helper.Log("starting filter");
  62.  
  63.             Viewer viewer = Viewers.GetViewer(message.User);
  64.  
  65.             StringBuilder output = new StringBuilder(input);
  66.             output.Replace("{username}", viewer.username);
  67.             output.Replace("{balance}", viewer.GetViewerCoins().ToString());
  68.             output.Replace("{karma}", viewer.GetViewerKarma().ToString());
  69.             output.Replace("{purchaselist}", ToolkitSettings.CustomPricingSheetLink);
  70.  
  71.             Helper.Log("starting regex");
  72.  
  73.             Regex regex = new Regex("\\[(.*?)\\]");
  74.  
  75.             MatchCollection matches = regex.Matches(output.ToString());
  76.  
  77.             foreach (Match match in matches)
  78.             {
  79.                 Helper.Log("found match " + match.Value);
  80.                 string code = match.Value;
  81.                 code = code.Replace("[", "");
  82.                 code = code.Replace("]", "");
  83.  
  84.                 //Regex doubleReg = new Regex("double\\<(.*?)\\>");
  85.  
  86.                 //foreach (Match innerMatch in doubleReg.Matches(match.Value.ToString()))
  87.                 //{
  88.                 //    Helper.Log("found match " + innerMatch.Value);
  89.  
  90.                 //    string innerCode = innerMatch.Value;
  91.                 //    innerCode = innerCode.Replace("double<", "");
  92.                 //    innerCode = innerCode.Replace(">", "");
  93.  
  94.                 //    Helper.Log("executing double " + innerCode);
  95.  
  96.                 //    output.Replace(innerMatch.Value, MoonSharpDouble(code).ToString());
  97.                 //}
  98.  
  99.                 Helper.Log("finished inner code");
  100.  
  101.                 output.Replace(match.Value, MoonSharpString(code));
  102.             }
  103.  
  104.             return output.ToString();
  105.         }
  106.  
  107.         public string MoonSharpString(string function)
  108.         {
  109.             string script = @function;
  110.  
  111.             DynValue res = Script.RunString(script);
  112.             return res.String;
  113.         }
  114.  
  115.         public double MoonSharpDouble(string function)
  116.         {
  117.             string script = @function;
  118.  
  119.             DynValue res = Script.RunString(script);
  120.             return res.Number;
  121.         }
  122.     }
  123.  
  124.     //public static class LUATools
  125.     //{
  126.     //    public static double Double(string code)
  127.     //    {
  128.     //        try
  129.     //        {
  130.     //            string script = code;
  131.  
  132.     //            DynValue res = Script.RunString(script);
  133.  
  134.     //            return res.Number;
  135.     //        }
  136.     //        catch (Exception e)
  137.     //        {
  138.     //            Log.Error(e.Message);
  139.  
  140.     //            return 0d;
  141.     //        }
  142.     //    }
  143.     //}
  144.  
  145.     //public class LUAActivator
  146.     //{
  147.     //    public static object CreateInstance(string type)
  148.     //    {
  149.     //        Type classType = Type.GetType(type);
  150.     //        object classObject = (object)Activator.CreateInstance(classType);
  151.  
  152.     //        return classObject;
  153.     //    }
  154.     //}
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement