public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string random_key1 = Guid.NewGuid().ToString(); string random_key2 = Guid.NewGuid().ToString(); string random_key3 = Guid.NewGuid().ToString(); string random1 = Guid.NewGuid().ToString(); string random2 = Guid.NewGuid().ToString(); string random3 = Guid.NewGuid().ToString(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.PushString(random1); lua.SetField(-2, random_key1); lua.PushString(random2); lua.SetField(-2, random_key2); lua.PushString(random3); lua.SetField(-2, random_key3); lua.Pop(lua.Top()); lua.PushGlobalTable(); lua.GetField(-1, random_key1); if (lua.GetString(-1) != random1) { throw new Exception("First random string is invalid"); } lua.Pop(1); lua.GetField(-1, random_key2); if (lua.GetString(-1) != random2) { throw new Exception("Second random string is invalid"); } lua.Pop(1); lua.GetField(-1, random_key3); if (lua.GetString(-1) != random3) { throw new Exception("Third random string is invalid"); } lua.Pop(1); lua.Pop(1); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedClosure(lua => { if (lua.Top() != 3) { throw new Exception("Closure execution stack has incorect number of items."); } string one = lua.GetString(1); string two = lua.GetString(2); double three = lua.GetNumber(3); lua.Pop(3); lua.PushString(one + three); lua.PushString(three + two); if (lua.Top() != 2) { throw new Exception("Closure execution stack has incorrect number of items after executtion."); } return(2); }, 0); lua.PushString(random1); lua.PushString(random2); lua.PushNumber(random3); lua.MCall(3, 2); string ret_1 = lua.GetString(-2); string ret_2 = lua.GetString(-1); lua.Pop(2); if (ret_1 != random1 + random3) { throw new Exception("First return string is incorrect."); } if (ret_2 != random3 + random2) { throw new Exception("Second return string is incorrect."); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { Random rand = new Random(); int random_number = rand.Next(1, 500); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tonumber"); lua.PushString(random_number.ToString()); try { lua.MCall(1, 1); } catch (GmodLuaException) { throw new MCallTestException("Exception was thrown by MCall, but it is not expected."); } int received_number = (int)lua.GetNumber(-1); if (received_number != random_number) { throw new MCallTestException("Numbers mismatch."); } lua.Pop(2); string random_error_message = Guid.NewGuid().ToString(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "assert"); lua.PushBool(false); lua.PushString(random_error_message); try { lua.MCall(2, 0); } catch (GmodLuaException e) { if (e.Message != random_error_message) { throw new MCallTestException("Error message is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception) { throw new MCallTestException("Wrong exception type was thrown"); } } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedFunction((lua) => { int stack_items = lua.Top(); if (stack_items != 3) { throw new Exception("The number of items on the execution stack is incorrect"); } string first = lua.GetString(1); string second = lua.GetString(2); double third = lua.GetNumber(3); lua.Pop(3); lua.PushString(first + third); lua.PushString(third + second); return(2); }); lua.PushString(random_string_1); lua.PushString(random_string_2); lua.PushNumber(random_number); lua.MCall(3, 2); string ret_1 = lua.GetString(-2); string ret_2 = lua.GetString(-1); lua.Pop(2); if (ret_1 != random_string_1 + random_number) { throw new Exception("First return string is incorrect"); } if (ret_2 != random_number + random_string_2) { throw new Exception("Second return string is incorrect"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void Unload(ILua lua) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Remove"); lua.PushString("Tick"); lua.PushString(this.OnTickIdentifier); lua.MCall(2, 0); lua.Pop(2); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int stack_state = lua.Top(); lua.PushString(random1); lua.PushString(random2); lua.PushManagedClosure(lua => { if (lua.Top() != 1) { throw new Exception("Managed closure execution stack has incorrect number of items"); } double num = lua.GetNumber(1); lua.Pop(1); string first = lua.GetString(GmodInterop.GetUpvalueIndex(1)); string second = lua.GetString(GmodInterop.GetUpvalueIndex(2)); lua.PushString(first + num + second); return(1); }, 2); if (lua.Top() != stack_state + 1) { throw new Exception("Wrong number of items left on the stack"); } lua.PushNumber(random3); lua.MCall(1, 1); string ret = lua.GetString(-1); lua.Pop(1); if (ret != random1 + random3 + random2) { throw new Exception("Return string is incorrect"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { this.lua = lua; this.isServerSide = is_serverside; this.current_load_context = assembly_context; this.OnTickDelegate = this.OnTick; this.OnTickIdentifier = Guid.NewGuid().ToString(); current_test = null; if (isServerSide) { try { lua.Log("Loading test runner"); //Register OnTick with Garry's Mod lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(this.OnTickIdentifier); lua.PushManagedFunction(this.OnTickDelegate); lua.MCall(3, 0); lua.Pop(2); //Get the list of tests ListOfTests = new List <ITest>(); if (typeof(Tests).Assembly.GetTypes().Any(type => typeof(ITest).IsAssignableFrom(type) && type != typeof(ITest))) { foreach (Type t in typeof(Tests).Assembly.GetTypes().Where(type => type != typeof(ITest) && typeof(ITest).IsAssignableFrom(type))) { ListOfTests.Add((ITest)Activator.CreateInstance(t)); } } lua.Log("There are " + ListOfTests.Count + " tests to run:"); foreach (ITest test in ListOfTests) { lua.Log(test.GetType().ToString()); } IsEverythingSuccessful = true; tests_start_time = DateTime.Now; lua.Log("Test runner was loaded!"); } catch (Exception e) { lua.Log("Test runner FAILED to start: " + e.GetType().ToString() + ". Exception message: " + e.Message); } } }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { try { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(hook_id); lua.PushManagedFunction(l => { try { if (counter < 33) { counter++; } else { l.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); l.GetField(-1, "hook"); l.GetField(-1, "Remove"); l.PushString("Tick"); l.PushString(hook_id); l.MCall(2, 0); l.Pop(2); if (counter == 33) { taskCompletion.TrySetResult(true); } } } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(0); }); lua.MCall(3, 0); lua.Pop(2); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void Dispose(ILua lua) { if (!disposed) { disposed = true; lua.PushGlobalTable(); lua.GetField(-1, "hook"); lua.GetField(-1, "Remove"); lua.PushString("Tick"); lua.PushString(onTickCallbackId); lua.MCall(2, 0); lua.Pop(2); } }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { try { Random rand = new Random(); int random_number = rand.Next(1, 500); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "tonumber"); lua.PushString(random_number.ToString()); if (lua.PCall(1, 1, 0) != 0) { throw new PCallTest_NoError_Exception("PCall returned nonzero error code"); } double recieved_num = lua.GetNumber(-1); if (recieved_num != (double)random_number) { throw new PCallTest_NoError_Exception("Recieved value is invalid"); } lua.Pop(2); string random_error_msg = Guid.NewGuid().ToString(); lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "assert"); lua.PushBool(false); lua.PushString(random_error_msg); if (lua.PCall(2, 0, 0) == 0) { throw new PCallTest_Exception("assert(false, ...) didn't throw an error"); } string res_err_msg = lua.GetString(-1); if (res_err_msg != random_error_msg) { throw new PCallTest_Exception("Received error message is invalid"); } lua.Pop(2); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
int SpawnFunc(ILua lua) { try { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "ents"); lua.GetField(-1, "Create"); lua.PushString("prop_physics"); lua.MCall(1, 1); if (lua.GetType(-1) != (int)TYPES.ENTITY) { throw new SpawnEntityException("ents.Create returned a value which type is not Entity"); } lua.GetField(-1, "SetModel"); lua.Push(-2); lua.PushString("models/props_c17/oildrum001.mdl"); lua.MCall(2, 0); lua.GetField(-1, "SetPos"); lua.Push(-2); lua.PushVector(new System.Numerics.Vector3(0)); lua.MCall(2, 0); lua.GetField(-1, "Spawn"); lua.Push(-2); lua.MCall(1, 0); lua.GetField(-1, "IsValid"); lua.Push(-2); lua.MCall(1, 1); if (!lua.GetBool(-1)) { throw new SpawnEntityException("Entity is not valid"); } lua.Pop(lua.Top()); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(0); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushManagedFunction((lua) => { lua.PushString(random_string); return(1); }); lua.MCall(0, 1); string ret_string = lua.GetString(-1); lua.Pop(1); if (ret_string != random_string) { throw new Exception("Return string is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { this.lua_extructor = lua_extructor; TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string closure_upvalue = Guid.NewGuid().ToString(); lua.PushString(closure_upvalue); lua.PushCClosure(closure_ptr, 1); lua.MCall(0, 1); string received_string = lua.GetString(-1); lua.Pop(1); if (received_string != (closure_upvalue + string_to_add)) { throw new PushCClosureTestException("Received string is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string key = Guid.NewGuid().ToString(); string value = Guid.NewGuid().ToString(); lua.CreateTable(); lua.PushString(value); lua.SetField(-2, key); lua.PushNil(); lua.Next(-2); string received_key = lua.GetString(-2); string received_value = lua.GetString(-1); lua.Pop(3); if (received_key != key || received_value != value) { throw new NextTestException("Received key value pair is invalid"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int lua_state = lua.Top(); lua.PushString(random); lua.PushManagedClosure(lua => { if (lua.Top() != 0) { throw new Exception("Managed closure execution stack is non-empty"); } string upvalue = lua.GetString(GmodInterop.GetUpvalueIndex(1)); lua.PushString(upvalue + upvalue); return(1); }, 1); if (lua.Top() != lua_state + 1) { throw new Exception("There is incorrect number of items on the Lua stack"); } lua.MCall(0, 1); string ret = lua.GetString(-1); lua.Pop(1); if (ret != random + random) { throw new Exception("Return string is incorrect"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public void PushToLua(ILua lua) { lua.CreateTable(); lua.PushString(Name); lua.SetField(-2, "Name"); lua.PushVector(NodeSize); lua.SetField(-2, "NodeSize"); }
int MetaToString(ILua lua) { lua.Pop(lua.Top()); lua.PushString(to_str_msg); return(1); }
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "print"); lua.PushString("Hello World!"); lua.MCall(1, 0); lua.Pop(); }
public void Unload(ILua lua) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "print"); lua.PushString("Goodbye World!"); lua.MCall(1, 0); lua.Pop(); }
internal static void PrintToConsole(this ILua lua, string message) { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); lua.GetField(-1, "print"); lua.PushString(message); lua.MCall(1, 0); lua.Pop(1); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int type1 = lua.CreateMetaTable(Guid.NewGuid().ToString()); int type2 = lua.CreateMetaTable(Guid.NewGuid().ToString()); lua.Pop(2); lua.PushUserType((IntPtr)1, type1); if (lua.GetUserType(-1, type2) != IntPtr.Zero) { throw new Exception("GetUserType returned non-zero pointer in the first test."); } if (lua.GetUserType(-1, type1) != (IntPtr)1) { throw new Exception("GetUserType returned invalid pointer in the second test."); } lua.Pop(1); lua.PushUserType((IntPtr)2, type2); if (lua.GetUserType(-1, type1) != IntPtr.Zero) { throw new Exception("GetUserType returned non-zero pointer in the third test."); } if (lua.GetUserType(-1, type2) != (IntPtr)2) { throw new Exception("GetUserType returned invalid pointer in the fourth test."); } lua.Pop(1); lua.PushString(Guid.NewGuid().ToString()); if (lua.GetUserType(-1, type1) != IntPtr.Zero) { throw new Exception("GetUserType returned invalid pointer in the fifth test."); } lua.Pop(1); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
static int TestFunc(IntPtr lua_state) { ILua lua = GmodInterop.GetLuaFromState(lua_state); lua.Pop(lua.Top()); lua.PushString(random); return(1); }
public GmodInteropService(ILua lua) { disposed = false; onTickCallbackId = Guid.NewGuid().ToString(); getPlayersTasks = new ConcurrentQueue <TaskCompletionSource <List <string> > >(); lua.PushGlobalTable(); lua.GetField(-1, "hook"); lua.GetField(-1, "Add"); lua.PushString("Tick"); lua.PushString(onTickCallbackId); lua.PushManagedFunction(lua => { SetPlayerList(lua); return(0); }); lua.MCall(3, 0); lua.Pop(2); }
static int TestFunc(IntPtr lua_state) { ILua lua = GmodInterop.GetLuaFromState(lua_state); lua.PushNumber(random_number); lua.PushString(random_string); return(2); }
static int ClosureTestFunc(IntPtr lua_state) { ILua lua = GmodInterop.GetLuaFromState(lua_state); lua.Pop(lua.Top()); string upvalue = lua.GetString(GmodInterop.GetUpvalueIndex(1, false)); lua.PushString(upvalue + string_to_add); return(1); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string first_string = Guid.NewGuid().ToString(); string second_string = Guid.NewGuid().ToString(); int number_of_items_on_stack = lua.Top(); lua.PushString(first_string); lua.PushString(second_string); lua.Push(-2); string received_string = lua.GetString(-1); string another_received_string = lua.GetString(-2); lua.Pop(3); if (received_string != first_string || another_received_string != second_string) { throw new PushTestException("Received strings are invalid"); } if (lua.Top() != number_of_items_on_stack) { throw new PushTestException("Not all strings were poped from the stack"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { int stack_state = lua.Top(); custom_type_id = lua.CreateMetaTable(Guid.NewGuid().ToString()); lua.PushManagedFunction(lua => { long value = (long)lua.GetUserType(1, custom_type_id); lua.Pop(1); lua.PushString((value * 2).ToString()); return(1); }); lua.SetField(-2, "__call"); lua.Pop(1); lua.PushUserType((IntPtr)random_integer, custom_type_id); lua.MCall(0, 1); string ret_string = lua.GetString(-1); lua.Pop(1); lua.PushMetaTable(custom_type_id); lua.PushNil(); lua.SetField(-2, "__call"); lua.Pop(1); if ((stack_state - lua.Top()) != 0) { throw new Exception("Lua stack has some values left"); } if (ret_string != (random_integer * 2).ToString()) { throw new Exception("Return string is incorrect"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { lua.PushNumber(1); lua.PushString("Dima is the best boy."); lua.PushBool(true); lua.PushNil(); if (!lua.IsType(-4, TYPES.NUMBER)) { throw new Exception("IsType returned false on NUMBER type"); } if (!lua.IsType(-3, TYPES.STRING)) { throw new Exception("IsType returned false on STRING type"); } if (!lua.IsType(-2, TYPES.BOOL)) { throw new Exception("IsType returned false on BOOL type"); } if (!lua.IsType(-1, TYPES.NIL)) { throw new Exception("IsType returned false on NIL type"); } lua.Pop(4); taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); try { string random_string = Guid.NewGuid().ToString(); lua.PushString(random_string); int reference = lua.ReferenceCreate(); if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) == random_string) { throw new ReferenceTestException("String wasn't poped from the stack"); } lua.ReferencePush(reference); if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) != random_string) { throw new ReferenceTestException("Reference wasn't pushed to the stack"); } lua.Pop(1); lua.ReferenceFree(reference); lua.ReferencePush(reference); if (lua.GetType(-1) == (int)TYPES.STRING && lua.GetString(-1) == random_string) { throw new ReferenceTestException("Reference wasn't freed"); } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }
public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext _) { taskCompletion = new TaskCompletionSource <bool>(); string[] TestStrings = new string[5]; for (int i = 0; i < 5; i++) { TestStrings[i] = Guid.NewGuid().ToString(); } try { lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB); for (int i = 0; i < 5; i++) { lua.PushString(TestStrings[i]); lua.SetField(-2, FieldName + "Str" + i.ToString()); } for (int i = 0; i < 5; i++) { lua.GetField(-1, FieldName + "Str" + i.ToString()); string tmp = lua.GetString(-1); lua.Pop(1); if (tmp != TestStrings[i]) { throw new PushStringException(i, TestStrings[i], tmp); } } taskCompletion.TrySetResult(true); } catch (Exception e) { taskCompletion.TrySetException(new Exception[] { e }); } return(taskCompletion.Task); }