public void TestCommandLine() { CommandLine cmdline = new CommandLine(new string[] { "-batchmode", "+server.hostname", "My", "Server", "Name", "+server.port", "28015", "+server.identity", "facepunchdev", "+server.seed", "6738" }); Assert.AreEqual(true, cmdline.HasVariable("batchmode"), "Failed cmdline.HasFlag"); Assert.AreEqual(false, cmdline.HasVariable("random"), "Failed cmdline.HasFlag"); Assert.AreEqual(false, cmdline.HasVariable("server.hostname"), "Failed cmdline.HasFlag"); Assert.AreEqual(true, cmdline.HasVariable("server.hostname"), "Failed cmdline.HasVariable"); Assert.AreEqual(true, cmdline.HasVariable("server.port"), "Failed cmdline.HasVariable"); Assert.AreEqual(true, cmdline.HasVariable("server.identity"), "Failed cmdline.HasVariable"); Assert.AreEqual(true, cmdline.HasVariable("server.seed"), "Failed cmdline.HasVariable"); Assert.AreEqual(false, cmdline.HasVariable("server.random"), "Failed cmdline.HasVariable"); Assert.AreEqual(false, cmdline.HasVariable("batchmode"), "Failed cmdline.HasVariable"); Assert.AreEqual("My Server Name", cmdline.GetVariable("server.hostname"), "Failed cmdline.GetVariable"); Assert.AreEqual("28015", cmdline.GetVariable("server.port"), "Failed cmdline.GetVariable"); Assert.AreEqual("facepunchdev", cmdline.GetVariable("server.identity"), "Failed cmdline.GetVariable"); Assert.AreEqual("6738", cmdline.GetVariable("server.seed"), "Failed cmdline.GetVariable"); Assert.AreEqual(null, cmdline.GetVariable("server.random"), "Failed cmdline.GetVariable"); Assert.AreEqual(null, cmdline.GetVariable("batchmode"), "Failed cmdline.GetVariable"); }
/// <summary> /// Initializes a new instance of the OxideMod class /// </summary> public void Load() { RootDirectory = Environment.CurrentDirectory; if (RootDirectory.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))) { RootDirectory = AppDomain.CurrentDomain.BaseDirectory; } Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Culture = CultureInfo.InvariantCulture }; // Create the commandline commandline = new CommandLine(Environment.GetCommandLineArgs()); // Load the config var oxideConfig = Path.Combine(RootDirectory, "oxide.root.json"); if (!File.Exists(oxideConfig)) { throw new FileNotFoundException("Could not load Oxide root configuration", oxideConfig); } rootconfig = ConfigFile.Load <OxideConfig>(oxideConfig); // Work out the instance directory for (int i = 0; i < rootconfig.InstanceCommandLines.Length; i++) { string varname, format; rootconfig.GetInstanceCommandLineArg(i, out varname, out format); if (string.IsNullOrEmpty(varname) || commandline.HasVariable(varname)) { InstanceDirectory = Path.Combine(RootDirectory, Utility.CleanPath(string.Format(format, commandline.GetVariable(varname)))); break; } } if (InstanceDirectory == null) { throw new Exception("Could not identify instance directory"); } ExtensionDirectory = Path.Combine(RootDirectory, Utility.CleanPath(rootconfig.ExtensionDirectory)); PluginDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath(rootconfig.PluginDirectory)); DataDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath(rootconfig.DataDirectory)); LangDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath(rootconfig.LangDirectory)); LogDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath(rootconfig.LogDirectory)); ConfigDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath(rootconfig.ConfigDirectory)); if (!Directory.Exists(ExtensionDirectory)) { throw new Exception("Could not identify extension directory"); } if (!Directory.Exists(InstanceDirectory)) { Directory.CreateDirectory(InstanceDirectory); } if (!Directory.Exists(PluginDirectory)) { Directory.CreateDirectory(PluginDirectory); } if (!Directory.Exists(DataDirectory)) { Directory.CreateDirectory(DataDirectory); } if (!Directory.Exists(LangDirectory)) { Directory.CreateDirectory(LangDirectory); } if (!Directory.Exists(LogDirectory)) { Directory.CreateDirectory(LogDirectory); } if (!Directory.Exists(ConfigDirectory)) { Directory.CreateDirectory(ConfigDirectory); } RegisterLibrarySearchPath(Path.Combine(ExtensionDirectory, IntPtr.Size == 8 ? "x64" : "x86")); // Create the loggers RootLogger = new CompoundLogger(); RootLogger.AddLogger(new RotatingFileLogger { Directory = LogDirectory }); if (debugCallback != null) { RootLogger.AddLogger(new CallbackLogger(debugCallback)); } // Log Oxide core loading LogInfo("Loading Oxide Core v{0}...", Version); // Create the managers RootPluginManager = new PluginManager(RootLogger) { ConfigPath = ConfigDirectory }; extensionmanager = new ExtensionManager(RootLogger); // Initialize other things DataFileSystem = new DataFileSystem(DataDirectory); // Register core libraries extensionmanager.RegisterLibrary("Global", new Global()); extensionmanager.RegisterLibrary("Time", new Time()); extensionmanager.RegisterLibrary("Timer", libtimer = new Timer()); extensionmanager.RegisterLibrary("Permission", new Permission()); extensionmanager.RegisterLibrary("Plugins", new Libraries.Plugins(RootPluginManager)); extensionmanager.RegisterLibrary("WebRequests", new WebRequests()); extensionmanager.RegisterLibrary("Lang", new Lang()); extensionmanager.RegisterLibrary("Covalence", covalence = new Covalence()); // Load all extensions LogInfo("Loading extensions..."); extensionmanager.LoadAllExtensions(ExtensionDirectory); // Remove old files Cleanup.Run(); // Initialize covalence library after extensions (as it depends on things from within an ext) covalence.Initialize(); // If no clock has been defined, make our own if (getTimeSinceStartup == null) { timer = new Stopwatch(); timer.Start(); getTimeSinceStartup = () => (float)timer.Elapsed.TotalSeconds; } // Load all watchers foreach (var ext in extensionmanager.GetAllExtensions()) { ext.LoadPluginWatchers(PluginDirectory); } // Load all plugins LogInfo("Loading plugins..."); LoadAllPlugins(true); // Hook all watchers foreach (var watcher in extensionmanager.GetPluginChangeWatchers()) { watcher.OnPluginSourceChanged += watcher_OnPluginSourceChanged; watcher.OnPluginAdded += watcher_OnPluginAdded; watcher.OnPluginRemoved += watcher_OnPluginRemoved; } }
/// <summary> /// Initializes a new instance of the OxideMod class /// </summary> public void Load() { RootDirectory = Environment.CurrentDirectory; if (RootDirectory.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))) { RootDirectory = AppDomain.CurrentDomain.BaseDirectory; } if (RootDirectory == null) { throw new Exception($"RootDirectory is null"); } InstanceDirectory = Path.Combine(RootDirectory, "oxide"); Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Culture = CultureInfo.InvariantCulture }; CommandLine = new CommandLine(Environment.GetCommandLineArgs()); if (CommandLine.HasVariable("oxide.directory")) { string var, format; CommandLine.GetArgument("oxide.directory", out var, out format); if (string.IsNullOrEmpty(var) || CommandLine.HasVariable(var)) { InstanceDirectory = Path.Combine(RootDirectory, Utility.CleanPath(string.Format(format, CommandLine.GetVariable(var)))); } } ExtensionDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (ExtensionDirectory == null || !Directory.Exists(ExtensionDirectory)) { throw new Exception("Could not identify extension directory"); } PluginDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("plugins")); ConfigDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("config")); DataDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("data")); LangDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("lang")); LogDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("logs")); if (!Directory.Exists(InstanceDirectory)) { Directory.CreateDirectory(InstanceDirectory); } if (!Directory.Exists(PluginDirectory)) { Directory.CreateDirectory(PluginDirectory); } if (!Directory.Exists(ConfigDirectory)) { Directory.CreateDirectory(ConfigDirectory); } if (!Directory.Exists(DataDirectory)) { Directory.CreateDirectory(DataDirectory); } if (!Directory.Exists(LangDirectory)) { Directory.CreateDirectory(LangDirectory); } if (!Directory.Exists(LogDirectory)) { Directory.CreateDirectory(LogDirectory); } RegisterLibrarySearchPath(Path.Combine(ExtensionDirectory, IntPtr.Size == 8 ? "x64" : "x86")); var config = Path.Combine(InstanceDirectory, "oxide.config.json"); if (File.Exists(config)) { Config = ConfigFile.Load <OxideConfig>(config); } else { Config = new OxideConfig(config); Config.Save(); } if (CommandLine.HasVariable("rcon.port")) { Config.Rcon.Port = int.Parse(CommandLine.GetVariable("rcon.port")); } RootLogger = new CompoundLogger(); RootLogger.AddLogger(new RotatingFileLogger { Directory = LogDirectory }); if (debugCallback != null) { RootLogger.AddLogger(new CallbackLogger(debugCallback)); } LogInfo("Loading Oxide Core v{0}...", Version); RootPluginManager = new PluginManager(RootLogger) { ConfigPath = ConfigDirectory }; extensionManager = new ExtensionManager(RootLogger); DataFileSystem = new DataFileSystem(DataDirectory); extensionManager.RegisterLibrary("Covalence", covalence = new Covalence()); extensionManager.RegisterLibrary("Global", new Global()); extensionManager.RegisterLibrary("Lang", new Lang()); extensionManager.RegisterLibrary("Permission", new Permission()); extensionManager.RegisterLibrary("Plugins", new Libraries.Plugins(RootPluginManager)); extensionManager.RegisterLibrary("Time", new Time()); extensionManager.RegisterLibrary("Timer", libtimer = new Timer()); extensionManager.RegisterLibrary("WebRequests", new WebRequests()); LogInfo("Loading extensions..."); extensionManager.LoadAllExtensions(ExtensionDirectory); Cleanup.Run(); covalence.Initialize(); RemoteConsole = new RemoteConsole.RemoteConsole(); RemoteConsole?.Initalize(); if (getTimeSinceStartup == null) { timer = new Stopwatch(); timer.Start(); getTimeSinceStartup = () => (float)timer.Elapsed.TotalSeconds; LogWarning("A reliable clock is not available, falling back to a clock which may be unreliable on certain hardware"); } foreach (var ext in extensionManager.GetAllExtensions()) { ext.LoadPluginWatchers(PluginDirectory); } LogInfo("Loading plugins..."); LoadAllPlugins(true); foreach (var watcher in extensionManager.GetPluginChangeWatchers()) { watcher.OnPluginSourceChanged += watcher_OnPluginSourceChanged; watcher.OnPluginAdded += watcher_OnPluginAdded; watcher.OnPluginRemoved += watcher_OnPluginRemoved; } if (CommandLine.HasVariable("nolog")) { LogWarning("Usage of the 'nolog' variable will prevent logging"); } }