Exemplo n.º 1
0
        /// <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.config.json");

            if (!File.Exists(oxideConfig))
            {
                throw new FileNotFoundException("Could not load the Oxide configuration file", oxideConfig);
            }
            Config = ConfigFile.Load <OxideConfig>(oxideConfig);
            Config.Save();

            // Work out the instance directory
            for (var i = 0; i < Config.InstanceCommandLines.Length; i++)
            {
                string varname, format;
                Config.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");
            }

            // Clean and set directory paths
            ExtensionDirectory = Path.Combine(RootDirectory, Utility.CleanPath(Config.ExtensionDirectory));
            PluginDirectory    = Path.Combine(InstanceDirectory, Utility.CleanPath("plugins"));
            DataDirectory      = Path.Combine(InstanceDirectory, Utility.CleanPath("data"));
            LangDirectory      = Path.Combine(InstanceDirectory, Utility.CleanPath("lang"));
            LogDirectory       = Path.Combine(InstanceDirectory, Utility.CleanPath("logs"));
            ConfigDirectory    = Path.Combine(InstanceDirectory, Utility.CleanPath(Config.ConfigDirectory));

            // Create directories if needed
            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);
            }

            // Register the library path
            RegisterLibrarySearchPath(Path.Combine(ExtensionDirectory, IntPtr.Size == 8 ? "x64" : "x86"));

            // Set the default group
            DefaultGroup = Config.DefaultGroup;

            // 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("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());

            // Load all extensions
            LogInfo("Loading extensions...");
            extensionManager.LoadAllExtensions(ExtensionDirectory);

            // Initialize covalence library after extensions (as it depends on things from within an ext)
            covalence.Initialize();

            // Remove old files
            Cleanup.Add(Path.Combine(Interface.Oxide.RootDirectory, "oxide.root.json"));
            Cleanup.Run();

            // If no clock has been defined, make our own unreliable clock
            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");
            }

            // 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;
            }

            // Check for 'load' variable and warn
            if (CommandLine.HasVariable("load"))
            {
                LogWarning("The 'load' variable is unused and can be removed");
            }

            // Check for 'nolog' variable and warn
            if (CommandLine.HasVariable("nolog"))
            {
                LogWarning("Usage of the 'nolog' variable will prevent logging");
            }
        }
Exemplo n.º 2
0
        /// <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;
            }

            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"));
            DataDirectory   = Path.Combine(InstanceDirectory, Utility.CleanPath("data"));
            LangDirectory   = Path.Combine(InstanceDirectory, Utility.CleanPath("lang"));
            LogDirectory    = Path.Combine(InstanceDirectory, Utility.CleanPath("logs"));
            ConfigDirectory = Path.Combine(InstanceDirectory, Utility.CleanPath("config"));
            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"));

            Cleanup.Add(Path.Combine(RootDirectory, "oxide.config.json"));

            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);

            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("load"))
            {
                LogWarning("The 'load' variable is unused and can be removed");
            }
            if (CommandLine.HasVariable("nolog"))
            {
                LogWarning("Usage of the 'nolog' variable will prevent logging");
            }

            Cleanup.Run();
        }