コード例 #1
0
ファイル: SharpStarDb.cs プロジェクト: r00t-s/SharpStar
        public ISessionFactory GetSessionFactory()
        {
            var config = Fluently.Configure();

            if (!File.Exists(DatabaseName))
            {
                config = config.ExposeConfiguration(p => new SchemaExport(p).Execute(false, true, false));
            }

            if (MonoHelper.IsRunningOnMono())
            {
                config = config.Database(MonoSQLiteConfiguration.Standard.UsingFile(DatabaseName));
            }
            else
            {
                config = config.Database(SQLiteConfiguration.Standard.UsingFile(DatabaseName));
            }

            _config = config
                      .Mappings(p => p.FluentMappings.AddFromAssemblyOf <SharpStarUser>())
                      .BuildConfiguration();

            MigrateToLatest();

            return(config.BuildSessionFactory());
        }
コード例 #2
0
        public WebSocketServer(string location)
        {
            var uri = new Uri(location);

            Port        = uri.Port;
            Location    = location;
            _locationIP = ParseIPAddress(uri);
            _scheme     = uri.Scheme;
            var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);

            if (!MonoHelper.IsRunningOnMono())
            {
#if __MonoCS__
#else
#if !NET45
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
                {
                    socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                }
#if !NET45
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                }
#endif
#endif
            }
            ListenerSocket        = new SocketWrapper(socket);
            SupportedSubProtocols = new string[0];
        }
コード例 #3
0
ファイル: SharpStarDb.cs プロジェクト: r00t-s/SharpStar
        public void MigrateToLatest()
        {
            var announcer = new TextWriterAnnouncer(s => SharpStarLogger.DefaultLogger.Debug(s));
            var assembly  = Assembly.GetExecutingAssembly();

            var migrationContext = new RunnerContext(announcer)
            {
                Namespace = "SharpStar.Database.Migrations"
            };

            var options = new MigrationOptions {
                PreviewOnly = false, Timeout = 60
            };

            ReflectionBasedDbFactory factory;

            if (MonoHelper.IsRunningOnMono())
            {
                factory = new MonoSQLiteDbFactory();
            }
            else
            {
                factory = new SqliteDbFactory();
            }

            var connection = factory.CreateConnection(_config.GetProperty(NHibernate.Cfg.Environment.ConnectionString));

            var processor = new SqliteProcessor(connection, new SqliteGenerator(), announcer, options, factory);
            var runner    = new MigrationRunner(assembly, migrationContext, processor);

            runner.MigrateUp(true);
        }
コード例 #4
0
ファイル: SharpStarLogger.cs プロジェクト: r00t-s/SharpStar
        static SharpStarLogger()
        {
            _pluginLoggers = new Dictionary <string, SharpStarLogger>();

            PatternLayout layout = new PatternLayout();

            layout.ConversionPattern = "%level - %message%newline";
            layout.ActivateOptions();

            IAppender appender;

            if (MonoHelper.IsRunningOnMono())
            {
                AnsiColorTerminalAppender ansiColor = new AnsiColorTerminalAppender();
                ansiColor.AddMapping(new AnsiColorTerminalAppender.LevelColors {
                    Level = Level.Info, ForeColor = AnsiColorTerminalAppender.AnsiColor.Blue, BackColor = AnsiColorTerminalAppender.AnsiColor.White
                });
                ansiColor.AddMapping(new AnsiColorTerminalAppender.LevelColors {
                    Level = Level.Debug, ForeColor = AnsiColorTerminalAppender.AnsiColor.White, BackColor = AnsiColorTerminalAppender.AnsiColor.Blue
                });
                ansiColor.AddMapping(new AnsiColorTerminalAppender.LevelColors {
                    Level = Level.Warn, ForeColor = AnsiColorTerminalAppender.AnsiColor.Yellow, BackColor = AnsiColorTerminalAppender.AnsiColor.Magenta
                });
                ansiColor.AddMapping(new AnsiColorTerminalAppender.LevelColors {
                    Level = Level.Error, ForeColor = AnsiColorTerminalAppender.AnsiColor.Yellow, BackColor = AnsiColorTerminalAppender.AnsiColor.Red
                });

                ansiColor.Layout = layout;
                ansiColor.ActivateOptions();

                appender = ansiColor;
            }
            else
            {
                ColoredConsoleAppender colorAppender = new ColoredConsoleAppender();
                colorAppender.AddMapping(new ColoredConsoleAppender.LevelColors {
                    Level = Level.Info, ForeColor = ColoredConsoleAppender.Colors.Blue | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.White
                });
                colorAppender.AddMapping(new ColoredConsoleAppender.LevelColors {
                    Level = Level.Debug, ForeColor = ColoredConsoleAppender.Colors.White | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.Blue
                });
                colorAppender.AddMapping(new ColoredConsoleAppender.LevelColors {
                    Level = Level.Warn, ForeColor = ColoredConsoleAppender.Colors.Yellow | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.Purple
                });
                colorAppender.AddMapping(new ColoredConsoleAppender.LevelColors {
                    Level = Level.Error, ForeColor = ColoredConsoleAppender.Colors.Yellow | ColoredConsoleAppender.Colors.HighIntensity, BackColor = ColoredConsoleAppender.Colors.Red
                });

                colorAppender.Layout = layout;
                colorAppender.ActivateOptions();

                appender = colorAppender;
            }

            ((Logger)Log.Logger).AddAppender(appender);
        }
コード例 #5
0
        public SocketWrapper(Socket socket)
        {
            _tokenSource = new CancellationTokenSource();
            _taskFactory = new TaskFactory(_tokenSource.Token);
            _socket      = socket;
            if (_socket.Connected)
            {
                _stream = new NetworkStream(_socket);
            }

            // The tcp keepalive default values on most systems
            // are huge (~7200s). Set them to something more reasonable.
            if (!MonoHelper.IsRunningOnMono())
            {
                SetKeepAlive(socket, KeepAliveInterval, RetryInterval);
            }
        }
コード例 #6
0
ファイル: WebSocketServer.cs プロジェクト: zamTech11/Fleck
        public WebSocketServer(int port, string location)
        {
            var uri = new Uri(location);

            Port        = uri.Port > 0 ? uri.Port : port;
            Location    = location;
            _locationIP = ParseIPAddress(uri);
            _scheme     = uri.Scheme;
            var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);

            if (!MonoHelper.IsRunningOnMono())
            {
                  #if __MonoCS__
                  #else
                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                  #endif
            }
            ListenerSocket        = new SocketWrapper(socket);
            SupportedSubProtocols = new string[0];
        }
コード例 #7
0
ファイル: WebSocketServer.cs プロジェクト: Grart/Fleck-fork
        public WebSocketServer(string location)
        {
            var uri = new Uri(location);

            Port        = uri.Port;
            Location    = location;
            _locationIP = ParseIPAddress(uri);
            _scheme     = uri.Scheme;
            var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);

            if (!MonoHelper.IsRunningOnMono())
            {
#if __MonoCS__
#else
#if !NET45
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
                {
                    //https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socketoptionname?view=netframework-4.7.2
                    //socket.SetSocketOption(
                    //		SocketOptionLevel.IPv6,
                    //		SocketOptionName.KeepAlive,//SocketOptionName.IPv6Only,
                    //		false
                    //	);
                }
#if !NET45
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                }
#endif
#endif
            }
            ListenerSocket        = new SocketWrapper(socket);
            SupportedSubProtocols = new string[0];
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: r00t-s/SharpStar
        private static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            Logger = SharpStarLogger.DefaultLogger;

            SharpStarMain m = SharpStarMain.Instance;

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException      += TaskScheduler_UnobservedTaskException;

            Version ver = Assembly.GetExecutingAssembly().GetName().Version;

            Logger.Info("SharpStar Version {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);

            if (MonoHelper.IsRunningOnMono())
            {
                string monoVer = MonoHelper.GetMonoVersion();

                if (!string.IsNullOrEmpty(monoVer))
                {
                    Logger.Info("Running on Mono version {0}", monoVer);

                    if (!monoVer.StartsWith("3.2"))
                    {
                        Logger.Warn("You are running a version of Mono that has not been tested with SharpStar!");
                        Logger.Warn("SharpStar has been tested with Mono version 3.2.8. Versions other than that are not supported and may cause problems!");
                    }
                }
            }


            m.Start();

            while (true)
            {
                string line = Console.ReadLine();

                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                string[] cmd = line.Split(' ');

                if (cmd.Length == 0)
                {
                    continue;
                }

                SharpStarUser user;

                switch (cmd[0])
                {
                case "loadplugin":

                    if (cmd.Length != 2)
                    {
                        Logger.Info("Syntax: loadplugin <file> (where file is in the \"{0}\" folder)",
                                    PluginManager.PluginDirectory);
                    }
                    else
                    {
                        try
                        {
                            m.PluginManager.LoadPlugin(Path.Combine(PluginManager.PluginDirectoryPath, cmd[1]));
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.Message);
                        }
                    }

                    break;

                case "unloadplugin":

                    if (cmd.Length != 2)
                    {
                        Logger.Info("Syntax: unloadplugin <file>");
                    }
                    else
                    {
                        try
                        {
                            m.PluginManager.UnloadPlugin(Path.Combine(PluginManager.PluginDirectoryPath, cmd[1]));
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.Message);
                        }
                    }

                    break;

                case "reloadplugins":

                    m.PluginManager.Reload();

                    break;

                case "updateplugins":

                    Logger.Info("Updating Plugins...");

                    m.PluginManager.CSPluginManager.UpdatePlugins();

                    Logger.Info("Finished updating plugins!");

                    break;

                case "updateplugin":

                    string updPlugin = string.Join(" ", cmd.Skip(1));

                    if (m.PluginManager.CSPluginManager.UpdatePlugin(updPlugin))
                    {
                        Logger.Info("Plugin {0} updated!", updPlugin);
                    }
                    else
                    {
                        Logger.Error("Error updating plugin {0}", updPlugin);
                    }

                    break;

                case "installplugin":

                    string instPlugin = string.Join(" ", cmd.Skip(1));

                    m.PluginManager.CSPluginManager.InstallPlugin(instPlugin);

                    break;

                case "uninstallplugin":

                    string uninstPlugin = string.Join(" ", cmd.Skip(1));

                    m.PluginManager.CSPluginManager.UninstallPlugin(uninstPlugin);

                    break;

                case "unloadplugins":

                    m.PluginManager.UnloadPlugins();

                    break;

                case "exit":

                    m.Shutdown();

                    Environment.Exit(0);

                    break;

                case "addperm":

                    if (cmd.Length == 4)
                    {
                        user = m.Database.GetUser(cmd[1]);

                        if (user == null)
                        {
                            Logger.Info("User does not exist!");
                        }
                        else
                        {
                            bool allowed;

                            bool.TryParse(cmd[3], out allowed);

                            m.Database.AddPlayerPermission(user.Id, cmd[2], allowed);

                            Logger.Info("Added permission to user {0}!", user.Username);
                        }
                    }
                    else
                    {
                        Logger.Info("Syntax: addperm <username> <permission> <allowed>");
                    }

                    break;

                case "removeperm":

                    if (cmd.Length == 3)
                    {
                        user = m.Database.GetUser(cmd[1]);

                        if (user == null)
                        {
                            Logger.Info("User does not exist!");
                        }
                        else
                        {
                            m.Database.DeletePlayerPermission(user.Id, cmd[2]);

                            Logger.Info("Permission removed from {0}!", user.Username);
                        }
                    }
                    else
                    {
                        Logger.Info("Syntax: removeperm <username> <permission>");
                    }

                    break;

                case "op":

                    if (cmd.Length == 2)
                    {
                        user = m.Database.GetUser(cmd[1]);

                        if (user == null)
                        {
                            Logger.Info("User does not exist!");
                        }
                        else
                        {
                            m.Database.ChangeAdminStatus(user.Id, true);

                            Logger.Info("{0} is now an admin!", user.Username);
                        }
                    }

                    break;

                case "deop":

                    user = m.Database.GetUser(cmd[1]);

                    if (user == null)
                    {
                        Logger.Info("User does not exist!");
                    }
                    else
                    {
                        m.Database.ChangeAdminStatus(user.Id, false);

                        Logger.Info("{0} is no longer an admin!", user.Username);
                    }

                    break;

                default:

                    if (cmd.Length > 1)
                    {
                        m.PluginManager.PassConsoleCommand(cmd[0], new string(line.Skip(cmd[0].Length + 1).ToArray()).Split(' '));
                    }
                    else
                    {
                        m.PluginManager.PassConsoleCommand(cmd[0], new string[0]);
                    }

                    break;
                }
            }
        }