Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        void SetPlayerList(ILua lua)
        {
            if (!getPlayersTasks.IsEmpty)
            {
                List <string> players            = new List <string>();
                Exception     executionException = null;

                try
                {
                    lua.PushGlobalTable();
                    lua.GetField(-1, "player");
                    lua.GetField(-1, "GetAll");
                    lua.MCall(0, 1);
                    lua.PushNil();
                    while (lua.Next(-2) != 0)
                    {
                        lua.GetField(-1, "Nick");
                        lua.Push(-2);
                        lua.MCall(1, 1);
                        players.Add(lua.GetString(-1));
                        lua.Pop(2);
                    }
                    lua.Pop(lua.Top());
                }
                catch (Exception e)
                {
                    executionException = e;
                }

                TaskCompletionSource <List <string> > task;
                while (getPlayersTasks.TryDequeue(out task))
                {
                    if (executionException == null)
                    {
                        task.SetResult(players);
                    }
                    else
                    {
                        task.SetException(executionException);
                    }
                }
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
        {
            lua.PushGlobalTable();
            lua.GetField(-1, "hook");
            lua.GetField(-1, "Add");
            lua.PushString("Tick");
            lua.PushString(tickEventId);
            lua.PushManagedFunction(lua =>
            {
                try
                {
                    lua.Print("Test started");

                    Logger log1 = new LoggerConfiguration()
                                  .MinimumLevel.Verbose()
                                  .WriteTo.GmodSink()
                                  .CreateLogger();

                    string VerboseMessage1            = Guid.NewGuid().ToString();
                    string DebugMessage1              = Guid.NewGuid().ToString();
                    string InformationMessage1        = Guid.NewGuid().ToString();
                    string WarningMessage1            = Guid.NewGuid().ToString();
                    string ErrorMessage1              = Guid.NewGuid().ToString();
                    string FatalMessage1              = Guid.NewGuid().ToString();
                    string FatalWithExceptionMessage1 = Guid.NewGuid().ToString();
                    Exception FatalException1         = new Exception(Guid.NewGuid().ToString());

                    log1.Verbose(VerboseMessage1);
                    log1.Debug(DebugMessage1);
                    log1.Information(InformationMessage1);
                    log1.Warning(WarningMessage1);
                    log1.Error(ErrorMessage1);
                    log1.Fatal(FatalMessage1);
                    log1.Fatal(FatalException1, FatalWithExceptionMessage1);

                    Logger log2 = new LoggerConfiguration()
                                  .MinimumLevel.Verbose()
                                  .WriteTo.GmodSink(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning)
                                  .CreateLogger();

                    string InformationMessage2 = Guid.NewGuid().ToString();
                    string WarningMessage2     = Guid.NewGuid().ToString();
                    string ErrorMessage2       = Guid.NewGuid().ToString();

                    log2.Information(InformationMessage2);
                    log2.Warning(WarningMessage2);
                    log2.Error(ErrorMessage2);

                    Thread.Sleep(2000);

                    FileStream fileStream     = File.Open("garrysmod/console.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamReader streamReader = new StreamReader(fileStream);

                    string console_log = streamReader.ReadToEnd();

                    if (!Regex.IsMatch(console_log, @$ "\[Verbose\].+{VerboseMessage1}\r{{0,1}}$", RegexOptions.ECMAScript | RegexOptions.Multiline | RegexOptions.Compiled))
                    {
                        throw new Exception("Verbose message 1 test failed");
                    }
                    if (!Regex.IsMatch(console_log, @$ "\[Debug\].+{DebugMessage1}\r{{0,1}}$", RegexOptions.ECMAScript | RegexOptions.Multiline | RegexOptions.Compiled))
                    {
                        throw new Exception("Debug message 1 test failed");
                    }
                    if (!Regex.IsMatch(console_log, @$ "\[Information\].+{InformationMessage1}\r{{0,1}}$", RegexOptions.ECMAScript | RegexOptions.Multiline | RegexOptions.Compiled))
                    {
                        throw new Exception("Information message 1 test failed");