Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using LuaInterface;
  7.  
  8. namespace GRoboCode_Lua
  9. {
  10.     struct KeyValue
  11.     {
  12.         public KeyValue(string key, object val)
  13.         {
  14.             Key = key;
  15.             Value = val;
  16.         }
  17.         public string Key;
  18.         public object Value;
  19.     }
  20.  
  21.     class RobotInstance
  22.     {
  23.         private Lua lState;
  24.         private Thread executionThread;
  25.  
  26.         private LuaTable tblData;
  27.         private IDictionary<string,object> dOut;
  28.  
  29.         private string strName;
  30.  
  31.         private bool bWorking;
  32.  
  33.         private bool bErrored;
  34.         private string strError;
  35.  
  36.         private List<KeyValue> lstData;
  37.         private List<string> lstWatch;
  38.  
  39.         public RobotInstance(string script, string name)
  40.         {
  41.             lState = new Lua();
  42.             lState.NewTable("_data");
  43.             tblData = (LuaTable)lState["_data"];
  44.             dOut = new Dictionary<string,object>();
  45.  
  46.             strName = name;
  47.             bErrored = false;
  48.             strError = "";
  49.             bWorking = false;
  50.             lstData = new List<KeyValue>();
  51.             lstWatch = new List<string>();
  52.         }
  53.  
  54.         public void AbortCall()
  55.         {
  56.             if (!bWorking) return;
  57.             executionThread.Abort();
  58.             executionThread = null;
  59.             bWorking = false;
  60.         }
  61.  
  62.         public void ListenFor(string varname)
  63.         {
  64.             Monitor.Enter(lstWatch);
  65.             lstWatch.Add(varname);
  66.             Monitor.Exit(lstWatch);
  67.         }
  68.  
  69.         public bool IsWorking() { return bWorking; }
  70.         public bool HasErrored() { return bErrored; }
  71.         public string GetError() { return strError; }
  72.  
  73.         public void SetValue(string key, object value)
  74.         {
  75.             Monitor.Enter(lstData);
  76.             lstData.Add(new KeyValue(key,value));
  77.             Monitor.Exit(lstData);
  78.         }
  79.  
  80.         public object GetValue(string key)
  81.         {
  82.             Monitor.Enter(dOut);
  83.             object temp = dOut[key];
  84.             Monitor.Exit(dOut);
  85.             return temp;
  86.         }
  87.  
  88.         public void Load(string script)
  89.         {
  90.             try
  91.             {
  92.                 // Load the script
  93.                 LuaFunction func = lState.LoadString(script, strName);
  94.                 lState["_temp"] = func;
  95.  
  96.                 // Setup the environment
  97.                 lState.NewTable("_env");
  98.                 LuaTable env = (LuaTable)lState["_env"];
  99.                 env["string"] = lState["string"];
  100.                 env["math"] = lState["math"];
  101.                 env["table"] = lState["table"];
  102.                 env["data"] = tblData;
  103.  
  104.                 // Sandbox it
  105.                 lState.DoString("setfenv( _G._temp, _G._env );");
  106.             }
  107.             catch (Exception ex)
  108.             {
  109.                 bErrored = true;
  110.                 strError = ex.Message;
  111.             }
  112.         }
  113.  
  114.         public void Call()
  115.         {
  116.             if (bWorking) return;
  117.             bErrored = false;
  118.             executionThread = new Thread(new ThreadStart(ProcessCall));
  119.             executionThread.Start();
  120.         }
  121.  
  122.         public void ProcessCall()
  123.         {
  124.             Monitor.Enter(lstData);
  125.             for (int i = 0; i < lstData.Count; i++)
  126.                 tblData[lstData[i].Key] = lstData[i].Value;
  127.             lstData.Clear();
  128.             Monitor.Exit(lstData);
  129.             try
  130.             {
  131.                 LuaFunction func = (LuaFunction)lState["_temp"];
  132.                 func.Call();
  133.             }
  134.             catch (Exception ex)
  135.             {
  136.                 strError = ex.Message;
  137.                 bErrored = true;
  138.             }
  139.             Monitor.Enter(lstWatch);
  140.             Monitor.Enter(dOut);
  141.             for (int i = 0; i < lstWatch.Count; i++)
  142.                 dOut[lstWatch[i]] = tblData[lstWatch[i]];
  143.             Monitor.Exit(dOut);
  144.             Monitor.Exit(lstWatch);
  145.             bWorking = false;
  146.         }
  147.  
  148.        
  149.  
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement