Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using LuaInterface;
- namespace GRoboCode_Lua
- {
- struct KeyValue
- {
- public KeyValue(string key, object val)
- {
- Key = key;
- Value = val;
- }
- public string Key;
- public object Value;
- }
- class RobotInstance
- {
- private Lua lState;
- private Thread executionThread;
- private LuaTable tblData;
- private IDictionary<string,object> dOut;
- private string strName;
- private bool bWorking;
- private bool bErrored;
- private string strError;
- private List<KeyValue> lstData;
- private List<string> lstWatch;
- public RobotInstance(string script, string name)
- {
- lState = new Lua();
- lState.NewTable("_data");
- tblData = (LuaTable)lState["_data"];
- dOut = new Dictionary<string,object>();
- strName = name;
- bErrored = false;
- strError = "";
- bWorking = false;
- lstData = new List<KeyValue>();
- lstWatch = new List<string>();
- }
- public void AbortCall()
- {
- if (!bWorking) return;
- executionThread.Abort();
- executionThread = null;
- bWorking = false;
- }
- public void ListenFor(string varname)
- {
- Monitor.Enter(lstWatch);
- lstWatch.Add(varname);
- Monitor.Exit(lstWatch);
- }
- public bool IsWorking() { return bWorking; }
- public bool HasErrored() { return bErrored; }
- public string GetError() { return strError; }
- public void SetValue(string key, object value)
- {
- Monitor.Enter(lstData);
- lstData.Add(new KeyValue(key,value));
- Monitor.Exit(lstData);
- }
- public object GetValue(string key)
- {
- Monitor.Enter(dOut);
- object temp = dOut[key];
- Monitor.Exit(dOut);
- return temp;
- }
- public void Load(string script)
- {
- try
- {
- // Load the script
- LuaFunction func = lState.LoadString(script, strName);
- lState["_temp"] = func;
- // Setup the environment
- lState.NewTable("_env");
- LuaTable env = (LuaTable)lState["_env"];
- env["string"] = lState["string"];
- env["math"] = lState["math"];
- env["table"] = lState["table"];
- env["data"] = tblData;
- // Sandbox it
- lState.DoString("setfenv( _G._temp, _G._env );");
- }
- catch (Exception ex)
- {
- bErrored = true;
- strError = ex.Message;
- }
- }
- public void Call()
- {
- if (bWorking) return;
- bErrored = false;
- executionThread = new Thread(new ThreadStart(ProcessCall));
- executionThread.Start();
- }
- public void ProcessCall()
- {
- Monitor.Enter(lstData);
- for (int i = 0; i < lstData.Count; i++)
- tblData[lstData[i].Key] = lstData[i].Value;
- lstData.Clear();
- Monitor.Exit(lstData);
- try
- {
- LuaFunction func = (LuaFunction)lState["_temp"];
- func.Call();
- }
- catch (Exception ex)
- {
- strError = ex.Message;
- bErrored = true;
- }
- Monitor.Enter(lstWatch);
- Monitor.Enter(dOut);
- for (int i = 0; i < lstWatch.Count; i++)
- dOut[lstWatch[i]] = tblData[lstWatch[i]];
- Monitor.Exit(dOut);
- Monitor.Exit(lstWatch);
- bWorking = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement