Advertisement
Guest User

Untitled

a guest
Oct 8th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. namespace Example
  2. {
  3.     /// <summary>
  4.     /// Full Lua state
  5.     /// </summary>
  6.     public class LuaState : Lua
  7.     {
  8.         /// <summary>
  9.         /// Lock which covers Lua function lookups and execution
  10.         /// </summary>
  11.         readonly object _lock = new object();
  12.  
  13.         /// <summary>
  14.         /// Initialize an independent threadsafe Lua state with a preset namespace.
  15.         /// </summary>
  16.         public LuaState ()
  17.         {
  18.             lock (_lock) // Try to keep any Lua functions from being called via C# until the namespace is set.
  19.             {
  20.                 // Environment setup - The lock isn't really needed here, but it's a good idea as long as you're not blocking for too long (ie, if you're paranoid about other threads trying to access the state before it's fully initialized)
  21.             }
  22.         }
  23.  
  24.         /// <summary>
  25.         /// Threadsafe return of the LuaFunction object for a named function.
  26.         /// Fails silently.
  27.         /// </summary>
  28.         /// <param name="name">Name/location of function</param>
  29.         /// <returns>LuaFunction or null</returns>
  30.         new public LuaFunction GetFunction(string name)
  31.         {
  32.             lock (_lock)
  33.             {
  34.                 try
  35.                 {
  36.                     return base.GetFunction(name);
  37.                 }
  38.                 catch (Exception)
  39.                 {
  40.                     return null;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         /// <summary>
  46.         /// Threadsafe Lua function call with optional args.
  47.         /// </summary>
  48.         /// <param name="name">Name/location of function</param>
  49.         /// <param name="args">List of args to pass</param>
  50.         public void Call(string name, params object[] args)
  51.         {
  52.             lock (_lock)
  53.             {
  54.                 Log.getInstance();
  55.  
  56.                 LuaFunction f = GetFunction(name);
  57.                 if (f == null)
  58.                 {
  59.                     Log.log(1, "Unable to find Lua function '" + name + "'. This is probably harmless.");
  60.                 }
  61.                 else
  62.                 {
  63.                     try
  64.                     {
  65.                         f.Call(args);
  66.                     }
  67.                     catch (Exception e)
  68.                     {
  69.                         Log.log(4, e.Message);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Lock-and-get shortcut for threaded reads.
  77.         /// </summary>
  78.         /// <param name="k">path within Lua state</param>
  79.         /// <returns></returns>
  80.         public object Get(string k)
  81.         {
  82.             lock (_lock)
  83.             {
  84.                 return this[k];
  85.             }
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Lock-and-set shortcut for threaded writes.
  90.         /// </summary>
  91.         /// <param name="k">path within Lua state</param>
  92.         /// <param name="v">value to write</param>
  93.         public void Set(string k, object v)
  94.         {
  95.             lock (_lock)
  96.             {
  97.                 this[k] = v;
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement