Solved C# Plugin - Random Crash with threading

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
Rust Legacy Unity version really sucks. I also cannot use a Disconnect function in a timer :/ It also crashes the server randomly.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
Hello,

I converted one of my plugins to C#. It works fine without threading. But with threading it crashes randomly like 1 time per day.

Here is the debug log:

Code:
========== OUTPUTING STACK TRACE ==================

(0x002533B0) (rust_server): (filename not available): Behaviour::Transfer<StreamedBinaryWrite<0> > + 0x27f90
(0x003A248E) (rust_server): (filename not available): Behaviour::Transfer<StreamedBinaryWrite<0> > + 0x17706e
(0x0FE3F4A8) (Mono JIT code): (filename not available):  (wrapper managed-to-native) UnityEngine.Object:FindObjectsOfType (System.Type) + 0x28 (0FE3F480 0FE3F4CC) [03AB4E70 - Unity Root Domain] + 0x0
(0x0FE3EF77) (Mono JIT code): (filename not available):  Fougerite.Hooks:HandleuLinkDisconnect (string,object) + 0x57 (0FE3EF20 0FE3F429) [03AB4E70 - Unity Root Domain] + 0x0
(0x0FE3EF09) (Mono JIT code): (filename not available):  Class52:method_435 (string,object) + 0x19 (0FE3EEF0 0FE3EF0E) [03AB4E70 - Unity Root Domain] + 0x0
(0x0FE3EBAB) (Mono JIT code): (filename not available):  Class52:vmethod_3 (string,object) + 0x3b (0FE3EB70 0FE3EBD5) [03AB4E70 - Unity Root Domain] + 0x0
(0x0FE3EA92) (Mono JIT code): (filename not available):  Class45:method_4 (string,object) + 0x42 (0FE3EA50 0FE3EAB8) [03AB4E70 - Unity Root Domain] + 0x0
(0x2880CE4E) (Mono JIT code): (filename not available):  Class48:method_269 (string,Class56) + 0x3d6 (2880CA78 2880CE79) [03AB4E70 - Unity Root Domain] + 0x0
(0x12463435) (Mono JIT code): (filename not available):  Class48:method_252 (Class56,bool,int) + 0x85 (124633B0 1246345F) [03AB4E70 - Unity Root Domain] + 0x0
(0x12463295) (Mono JIT code): (filename not available):  Class48:method_250 (uLink.NetworkPlayer,bool,int) + 0x1ad (124630E8 12463393) [03AB4E70 - Unity Root Domain] + 0x0
(0x124630AF) (Mono JIT code): (filename not available):  uLink.Network:CloseConnection (uLink.NetworkPlayer,bool,int) + 0x1f (12463090 124630D5) [03AB4E70 - Unity Root Domain] + 0x0
(0x12463058) (Mono JIT code): (filename not available):  NetCull:CloseConnection (uLink.NetworkPlayer,bool) + 0x18 (12463040 1246307E) [03AB4E70 - Unity Root Domain] + 0x0
(0x12463026) (Mono JIT code): (filename not available):  NetUser:Kick (NetError,bool) + 0xd6 (12462F50 12463034) [03AB4E70 - Unity Root Domain] + 0x0
(0x12462F4B) (Mono JIT code): (filename not available):  Fougerite.Player:Disconnect () + 0x3b (12462F10 12462F50) [03AB4E70 - Unity Root Domain] + 0x0
(0x2877AD16) (Mono JIT code): (filename not available):  WhiteListCountry.WhiteListCountry:WLCountry (Fougerite.Player) + 0x31e (2877A9F8 2877AE9C) [03AB4E70 - Unity Root Domain] + 0x0
(0x2877A7D1) (Mono JIT code): (filename not available):  WhiteListCountry.WhiteListCountry/<>c__DisplayClass1:<PlayerConnect>b__0 () + 0x19 (2877A7B8 2877A7D6) [03AB4E70 - Unity Root Domain] + 0x0
(0x0407F659) (Mono JIT code): (filename not available):  (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr) + 0x41 (0407F618 0407F6AD) [03AB4E70 - Unity Root Domain] + 0x0
(0x100F0376) (mono): (filename not available): mono_set_defaults + 0x22cb
(0x1005D78C) (mono): (filename not available): mono_runtime_invoke + 0x51
(0x1005DBA8) (mono): (filename not available): mono_runtime_delegate_invoke + 0x44
(0x1007DA95) (mono): (filename not available): mono_thread_interruption_request_flag + 0x440
(0x1010B4D6) (mono): (filename not available): mono_unity_thread_clear_domain_fields + 0x3a4b
(0x749F7C04) (KERNEL32): (filename not available): BaseThreadInitThunk + 0x24
(0x76F7AB8F) (ntdll): (filename not available): RtlInitializeExceptionChain + 0x8f
(0x76F7AB5A) (ntdll): (filename not available): RtlInitializeExceptionChain + 0x5a

========== END OF STACKTRACE ===========
And here is the source code, any idea how to fix that crash?:

C#:
namespace WhiteListCountry
{
    using Fougerite;
    using Fougerite.Events;
    using System;
    using System.Globalization;
    using System.IO;
    using System.Threading;
    using System.Collections.Generic;
    using UnityEngine;
    using GeoIP;


    public class WhiteListCountry : Module
    {
     
     
        public static IniParser ConfigINI;
        //public static DataStore ds = DataStore.GetInstance();
        public static Dictionary<string, string> PluginSettings = new Dictionary<string, string> { };


        public void WLCountry(Player pl)
        {

            try
            {

                GeoIP geo = GeoIP.Instance;
                if (geo.GetDataOfIP(pl.IP) == null)
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                        Server.GetServer().Broadcast(msg);
                    }

                    return;
                }

                string countrycode = geo.GetDataOfIP(pl.IP).CountryShort;
                string country = geo.GetDataOfIP(pl.IP).Country;
                string whitelist = PluginSettings["WL"];
                string wlp = PluginSettings["WLP"];
                string steamid = pl.SteamID;

                if (find(whitelist, countrycode, wlp, steamid) == false)
                {

                    string playerdisconnectMSG = PluginSettings["PDM"];
                    if (PluginSettings["SDM"] == "true")
                    {
                        string msg = PluginSettings["DM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }

                    pl.Message(playerdisconnectMSG + " [" + countrycode + "]");
                    pl.Disconnect();
                }

                else
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }

                }
                //Logger.Log(pl.Name + " connected to this server");
                //Server.GetServer().Broadcast(playername + " has connected from " + country);

            }

            catch
            {

                string msg = PluginSettings["JM"];
                msg = msg.Replace("%PLAYER%", pl.Name);
                msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                Server.GetServer().Broadcast(msg);

            }
        

        }


           public void PlayerConnect(Player pl)
        {

            Thread thread = new Thread(() =>WLCountry(pl));
            thread.Start();
        
        }

           public bool find(string whitelist, string CountryCode, string wlp, string steamid)
           {

               try
               {
                
                   whitelist = whitelist.Replace(" ", "");
                   string[] wl = whitelist.Split(',');
                
                   wlp = wlp.Replace(" ", "");
                   string[] wlpl = wlp.Split(',');

                   // Whitelist search

                   foreach (string s in wl)
                   {

                       if (CountryCode == s)
                       {
                           return true;
                       }

                       else
                       {
                           continue;
                       }

                   }

                   // WhiteListedPlayers search

                   foreach (string s in wlpl)
                
                   {
                 
                       if (steamid == s)
                       {

                           return true;
                       }

                       else
                       {
                           continue;
                       }
                
                   }

                   return false;

               }

               catch

               {
                   return false;
            
               }
           }

    
        public void Command(Fougerite.Player Player, string cmd, string[] args)
        {
                   

            if ((cmd == "wlreload") && Player.Admin)
            {


                if (args.Length == 0)
             
                {

                    PluginSettings.Clear();
                    ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                    PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
                    PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
                    PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
                    PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
                    PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
                    PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
                    PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
                    PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));
                    Player.Message("WhiteListCountry settings reloaded");                

                }

            }         
                     
        }
   

        public override void DeInitialize()
        {
            Hooks.OnCommand -= new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected -= PlayerConnect;
         
        }      

        public override void Initialize()
        {
            Hooks.OnCommand += new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected += PlayerConnect;

            if (!Directory.Exists(@"Save\WhiteListCountry"))
            {
                Directory.CreateDirectory(@"Save\WhiteListCountry");
            }

            if (!File.Exists(@"Save\WhiteListCountry\Config.ini"))
            {
                File.Create(@"Save\WhiteListCountry\Config.ini").Close();
                ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                ConfigINI.AddSetting("Settings", "ShowAcceptedMessage", "true");
                ConfigINI.AddSetting("Settings", "ShowDeniedMessage", "true");
                ConfigINI.AddSetting("WhiteList", "WhiteListedCountries", "DE, NL");
                ConfigINI.AddSetting("WhiteList", "WhiteListedPlayers", "76561197970657636");
                ConfigINI.AddSetting("Messages", "JoinMessage", "%PLAYER% has connected from: %COUNTRY%");
                ConfigINI.AddSetting("Messages", "PlayerDisconnectMessage", "Your country is not on the servers whitelist!");
                ConfigINI.AddSetting("Messages", "ServerDisconnectMessage", "%PLAYER% is trying to connect from: %COUNTRY%, but it's not whitelisted");
                ConfigINI.AddSetting("Messages", "UnknownLocation", "A hidden location");
                ConfigINI.Save();             
            }

            ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");

         
            PluginSettings.Clear();
            PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
            PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
            PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
            PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
            PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
            PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
            PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
            PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));

        }

              

        public override string Author
        {
            get
            {
                return "Nordi";
            }
        }

        public override string Description
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override string Name
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override System.Version Version
        {
            get
            {
                return new System.Version("1.0");
            }
        }
    }
}
Can you try It with Loom please?
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
Getting this error:

[Console] System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
at Fougerite.Loom.QueueOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
at Fougerite.Player.Disconnect () [0x00000] in <filename unknown>:0
at WhiteListCountry.WhiteListCountry.GeoInit (Fougerite.Player pl) [0x00000] in <filename unknown>:0

Source Code:

C#:
namespace WhiteListCountry
{
    using Fougerite;
    using Fougerite.Events;
    using System;
    using System.Globalization;
    using System.IO;
    using System.Threading;
    using System.Collections.Generic;
    using UnityEngine;
    using GeoIP;



    public class WhiteListCountry : Module
    {


        public static IniParser ConfigINI;
        //public static DataStore ds = DataStore.GetInstance();
        public static Dictionary<string, string> PluginSettings = new Dictionary<string, string> { };

      
        public void GeoInit(Player pl)
        {
            try
            {

                GeoIP geo = GeoIP.Instance;
                if (geo.GetDataOfIP(pl.IP) == null)
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                        Server.GetServer().Broadcast(msg);
                      
                    }

                    return;
                }

                string countrycode = geo.GetDataOfIP(pl.IP).CountryShort;
                string country = geo.GetDataOfIP(pl.IP).Country;
                string whitelist = PluginSettings["WL"];
                string wlp = PluginSettings["WLP"];
                string steamid = pl.SteamID;

                if (find(whitelist, countrycode, wlp, steamid) == false)
                {

                    string playerdisconnectMSG = PluginSettings["PDM"];
                    if (PluginSettings["SDM"] == "true")
                    {
                        string msg = PluginSettings["DM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }
                    pl.Message("[color #FF0000] ------------------------");
                    pl.Message("[color #FF0000] WARNING");
                    pl.Message("[color #FF0000] " + playerdisconnectMSG + " [" + countrycode + "]");
                    pl.Message("[color #FF0000] Disconnecting now");
                    pl.Message("[color #FF0000] ------------------------");

                    try
                    {

                        if (pl.IsOnline) { pl.Disconnect(); }

                    }

                    catch (Exception ex)
                    {

                        Server.GetServer().Broadcast(ex.ToString());
                        Logger.Log(ex.ToString());

                    }



                    /* System.Timers.Timer _timer2;
                    _timer2 = new System.Timers.Timer(20000);                  
                    _timer2.Elapsed += (_, __) => pldisc(pl);
                    _timer2.Start(); */


                }

                else
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }

                }


            }

            catch
            {

                string msg = PluginSettings["JM"];
                msg = msg.Replace("%PLAYER%", pl.Name);
                msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                Server.GetServer().Broadcast(msg);              
            }

        }


        public void PlayerConnect(Player pl)
        {

            Thread t1 = new Thread(() => GeoInit(pl));
            t1.Start();        


        }           

     
        public bool find(string whitelist, string CountryCode, string wlp, string steamid)
        {

            try
            {

                whitelist = whitelist.Replace(" ", "");
                string[] wl = whitelist.Split(',');

                wlp = wlp.Replace(" ", "");
                string[] wlpl = wlp.Split(',');

                // Whitelist search

                foreach (string s in wl)
                {

                    if (CountryCode == s)
                    {
                        return true;
                    }

                    else
                    {
                        continue;
                    }

                }

                // WhiteListedPlayers search

                foreach (string s in wlpl)
                {

                    if (steamid == s)
                    {

                        return true;
                    }

                    else
                    {
                        continue;
                    }

                }

                return false;

            }

            catch
            {
                return false;

            }
        }


        public void Command(Fougerite.Player Player, string cmd, string[] args)
        {


            if ((cmd == "wlreload") && Player.Admin)
            {


                if (args.Length == 0)
                {

                    PluginSettings.Clear();
                    ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                    PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
                    PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
                    PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
                    PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
                    PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
                    PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
                    PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
                    PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));
                    Player.Message("WhiteListCountry settings reloaded");

                }

            }

        }


        public override void DeInitialize()
        {
            Hooks.OnCommand -= new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected -= PlayerConnect;

        }

        public override void Initialize()
        {
            Hooks.OnCommand += new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected += PlayerConnect;

            if (!Directory.Exists(@"Save\WhiteListCountry"))
            {
                Directory.CreateDirectory(@"Save\WhiteListCountry");
            }

            if (!File.Exists(@"Save\WhiteListCountry\Config.ini"))
            {
                File.Create(@"Save\WhiteListCountry\Config.ini").Close();
                ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                ConfigINI.AddSetting("Settings", "ShowAcceptedMessage", "true");
                ConfigINI.AddSetting("Settings", "ShowDeniedMessage", "true");
                ConfigINI.AddSetting("WhiteList", "WhiteListedCountries", "DE, NL");
                ConfigINI.AddSetting("WhiteList", "WhiteListedPlayers", "76561197970657636");
                ConfigINI.AddSetting("Messages", "JoinMessage", "%PLAYER% has connected from: %COUNTRY%");
                ConfigINI.AddSetting("Messages", "PlayerDisconnectMessage", "Your country is not on the servers whitelist!");
                ConfigINI.AddSetting("Messages", "ServerDisconnectMessage", "%PLAYER% is trying to connect from: %COUNTRY%, but it's not whitelisted");
                ConfigINI.AddSetting("Messages", "UnknownLocation", "A hidden location");
                ConfigINI.Save();
            }

            ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");


            PluginSettings.Clear();
            PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
            PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
            PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
            PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
            PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
            PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
            PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
            PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));

        }



        public override string Author
        {
            get
            {
                return "Nordi";
            }
        }

        public override string Description
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override string Name
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override System.Version Version
        {
            get
            {
                return new System.Version("1.0");
            }
        }

    }

}
Loom only works when the player is completely connected? Or why this error message?
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
Getting this error:

[Console] System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
at Fougerite.Loom.QueueOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
at Fougerite.Player.Disconnect () [0x00000] in <filename unknown>:0
at WhiteListCountry.WhiteListCountry.GeoInit (Fougerite.Player pl) [0x00000] in <filename unknown>:0

Source Code:

C#:
namespace WhiteListCountry
{
    using Fougerite;
    using Fougerite.Events;
    using System;
    using System.Globalization;
    using System.IO;
    using System.Threading;
    using System.Collections.Generic;
    using UnityEngine;
    using GeoIP;



    public class WhiteListCountry : Module
    {


        public static IniParser ConfigINI;
        //public static DataStore ds = DataStore.GetInstance();
        public static Dictionary<string, string> PluginSettings = new Dictionary<string, string> { };

     
        public void GeoInit(Player pl)
        {
            try
            {

                GeoIP geo = GeoIP.Instance;
                if (geo.GetDataOfIP(pl.IP) == null)
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                        Server.GetServer().Broadcast(msg);
                     
                    }

                    return;
                }

                string countrycode = geo.GetDataOfIP(pl.IP).CountryShort;
                string country = geo.GetDataOfIP(pl.IP).Country;
                string whitelist = PluginSettings["WL"];
                string wlp = PluginSettings["WLP"];
                string steamid = pl.SteamID;

                if (find(whitelist, countrycode, wlp, steamid) == false)
                {

                    string playerdisconnectMSG = PluginSettings["PDM"];
                    if (PluginSettings["SDM"] == "true")
                    {
                        string msg = PluginSettings["DM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }
                    pl.Message("[color #FF0000] ------------------------");
                    pl.Message("[color #FF0000] WARNING");
                    pl.Message("[color #FF0000] " + playerdisconnectMSG + " [" + countrycode + "]");
                    pl.Message("[color #FF0000] Disconnecting now");
                    pl.Message("[color #FF0000] ------------------------");

                    try
                    {

                        if (pl.IsOnline) { pl.Disconnect(); }

                    }

                    catch (Exception ex)
                    {

                        Server.GetServer().Broadcast(ex.ToString());
                        Logger.Log(ex.ToString());

                    }



                    /* System.Timers.Timer _timer2;
                    _timer2 = new System.Timers.Timer(20000);                 
                    _timer2.Elapsed += (_, __) => pldisc(pl);
                    _timer2.Start(); */


                }

                else
                {

                    if (PluginSettings["SAM"] == "true")
                    {

                        string msg = PluginSettings["JM"];
                        msg = msg.Replace("%PLAYER%", pl.Name);
                        msg = msg.Replace("%COUNTRY%", country);
                        Server.GetServer().Broadcast(msg);

                    }

                }


            }

            catch
            {

                string msg = PluginSettings["JM"];
                msg = msg.Replace("%PLAYER%", pl.Name);
                msg = msg.Replace("%COUNTRY%", PluginSettings["UL"]);
                Server.GetServer().Broadcast(msg);             
            }

        }


        public void PlayerConnect(Player pl)
        {

            Thread t1 = new Thread(() => GeoInit(pl));
            t1.Start();       


        }          

    
        public bool find(string whitelist, string CountryCode, string wlp, string steamid)
        {

            try
            {

                whitelist = whitelist.Replace(" ", "");
                string[] wl = whitelist.Split(',');

                wlp = wlp.Replace(" ", "");
                string[] wlpl = wlp.Split(',');

                // Whitelist search

                foreach (string s in wl)
                {

                    if (CountryCode == s)
                    {
                        return true;
                    }

                    else
                    {
                        continue;
                    }

                }

                // WhiteListedPlayers search

                foreach (string s in wlpl)
                {

                    if (steamid == s)
                    {

                        return true;
                    }

                    else
                    {
                        continue;
                    }

                }

                return false;

            }

            catch
            {
                return false;

            }
        }


        public void Command(Fougerite.Player Player, string cmd, string[] args)
        {


            if ((cmd == "wlreload") && Player.Admin)
            {


                if (args.Length == 0)
                {

                    PluginSettings.Clear();
                    ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                    PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
                    PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
                    PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
                    PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
                    PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
                    PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
                    PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
                    PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));
                    Player.Message("WhiteListCountry settings reloaded");

                }

            }

        }


        public override void DeInitialize()
        {
            Hooks.OnCommand -= new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected -= PlayerConnect;

        }

        public override void Initialize()
        {
            Hooks.OnCommand += new Hooks.CommandHandlerDelegate(this.Command);
            Hooks.OnPlayerConnected += PlayerConnect;

            if (!Directory.Exists(@"Save\WhiteListCountry"))
            {
                Directory.CreateDirectory(@"Save\WhiteListCountry");
            }

            if (!File.Exists(@"Save\WhiteListCountry\Config.ini"))
            {
                File.Create(@"Save\WhiteListCountry\Config.ini").Close();
                ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");
                ConfigINI.AddSetting("Settings", "ShowAcceptedMessage", "true");
                ConfigINI.AddSetting("Settings", "ShowDeniedMessage", "true");
                ConfigINI.AddSetting("WhiteList", "WhiteListedCountries", "DE, NL");
                ConfigINI.AddSetting("WhiteList", "WhiteListedPlayers", "76561197970657636");
                ConfigINI.AddSetting("Messages", "JoinMessage", "%PLAYER% has connected from: %COUNTRY%");
                ConfigINI.AddSetting("Messages", "PlayerDisconnectMessage", "Your country is not on the servers whitelist!");
                ConfigINI.AddSetting("Messages", "ServerDisconnectMessage", "%PLAYER% is trying to connect from: %COUNTRY%, but it's not whitelisted");
                ConfigINI.AddSetting("Messages", "UnknownLocation", "A hidden location");
                ConfigINI.Save();
            }

            ConfigINI = new IniParser(@"Save\WhiteListCountry\Config.ini");


            PluginSettings.Clear();
            PluginSettings.Add("SAM", ConfigINI.GetSetting("Settings", "ShowAcceptedMessage"));
            PluginSettings.Add("SDM", ConfigINI.GetSetting("Settings", "ShowDeniedMessage"));
            PluginSettings.Add("WL", ConfigINI.GetSetting("WhiteList", "WhiteListedCountries"));
            PluginSettings.Add("WLP", ConfigINI.GetSetting("WhiteList", "WhiteListedPlayers"));
            PluginSettings.Add("JM", ConfigINI.GetSetting("Messages", "JoinMessage"));
            PluginSettings.Add("PDM", ConfigINI.GetSetting("Messages", "PlayerDisconnectMessage"));
            PluginSettings.Add("DM", ConfigINI.GetSetting("Messages", "ServerDisconnectMessage"));
            PluginSettings.Add("UL", ConfigINI.GetSetting("Messages", "UnknownLocation"));

        }



        public override string Author
        {
            get
            {
                return "Nordi";
            }
        }

        public override string Description
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override string Name
        {
            get
            {
                return "WhiteListCountry";
            }
        }

        public override System.Version Version
        {
            get
            {
                return new System.Version("1.0");
            }
        }

    }

}
Loom only works when the player is completely connected? Or why this error message?
Where are you using Loom? Cant see It from phone
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
I'm using inside a thread the pl.Disconnect() function since you mentioned in the wiki that Loom is automatically called at this method.

Basically like:

public void PlayerConnect(Player pl)
{

Thread t1 = new Thread(() => GeoInit(pl));
t1.Start();

}

public void GeoInit(Player pl)
{

some code ...

If player is not on whitelist [

if (pl.IsOnline) { pl.Disconnect(); }

]

}
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
Ahh yes yes. So It just throws a nullexception? Weird. I will add try catch to the function tomorrow to see what is throwing the null exception.
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
public void Disconnect()

{

if (this.IsOnline)

{

//Logger.LogError("Same? " + Thread.CurrentThread.ManagedThreadId + " - " + Bootstrap.CurrentThread.ManagedThreadId);

if (Thread.CurrentThread.ManagedThreadId != Bootstrap.CurrentThread.ManagedThreadId)

{

//Logger.LogError("Nope, invoking");

Loom.QueueOnMainThread(() => {

Disconnect();

});

return;

}

Server.GetServer().RemovePlayer(uid);

this.ourPlayer.netUser.Kick(NetError.Facepunch_Kick_RCON, false);

}

}

Is see that the Disconnect function is passed to Loom without any reference / object? Is it because of that maybe?

Direct error from Fougerite (via catch)

[5/15/2016 3:08:55 AM] [Console] System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
at Fougerite.Loom.QueueOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
at Fougerite.Player.Disconnect () [0x00000] in <filename unknown>:0
 
Last edited:

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
public void Disconnect()

{

if (this.IsOnline)

{

//Logger.LogError("Same? " + Thread.CurrentThread.ManagedThreadId + " - " + Bootstrap.CurrentThread.ManagedThreadId);

if (Thread.CurrentThread.ManagedThreadId != Bootstrap.CurrentThread.ManagedThreadId)

{

//Logger.LogError("Nope, invoking");

Loom.QueueOnMainThread(() => {

Disconnect();

});

return;

}

Server.GetServer().RemovePlayer(uid);

this.ourPlayer.netUser.Kick(NetError.Facepunch_Kick_RCON, false);

}

}

Is see that the Disconnect function is passed to Loom without any reference / object? Is it because of that maybe?

Direct error from Fougerite (via catch)

[5/15/2016 3:08:55 AM] [Console] System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
at Fougerite.Loom.QueueOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
at Fougerite.Player.Disconnect () [0x00000] in <filename unknown>:0
No.

The disconnect call is the instance. It's something different. I called this in EAC, and with any other stuff.
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
Hmm maybe its because I'm calling Player.Disconnect from a thread and not by System.Timers? Or is EAC also using threading? I will test the nightly build.
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
[5/15/2016 3:18:01 PM] [Error] [Fougerite Loom Error] System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
 

tarynkelley

Retired Staff
Retired Staff
Trusted Member
Nov 14, 2015
575
178
28
Parts Unknown
Yes a little more:

[5/15/2016 3:31:08 PM] [Error] Error for Taryn System.NullReferenceException: Object reference not set to an instance of an object
at Fougerite.Loom.QueueOnMainThread (System.Action action, Single time) [0x00000] in <filename unknown>:0
at Fougerite.Loom.QueueOnMainThread (System.Action action) [0x00000] in <filename unknown>:0
at Fougerite.Player.Disconnect () [0x00000] in <filename unknown>:0
[5/15/2016 3:31:08 PM] [Error] Error for Taryn (my Steam ID - removed it)
[5/15/2016 3:31:08 PM] [Error] Error for Taryn 2
[5/15/2016 3:31:08 PM] [Error] Error for Taryn 1