public ProtectionManager(
     PluginTrace pluginTrace, Configuration config, ChestManager chestManager, ServerMetadataHandler serverMetadataHandler, WorldMetadata worldMetadata
     )
 {
     this.PluginTrace = pluginTrace;
       this.config = config;
       this.ChestManager = chestManager;
       this.ServerMetadataHandler = serverMetadataHandler;
       this.WorldMetadata = worldMetadata;
 }
        public ProtectionManager(
            PluginTrace pluginTrace, Configuration config, ServerMetadataHandler serverMetadataHandler, WorldMetadata worldMetadata
            )
        {
            this.PluginTrace = pluginTrace;
              this.config = config;
              this.ServerMetadataHandler = serverMetadataHandler;
              this.WorldMetadata = worldMetadata;

              this.RefillTimers = new TimerManager(pluginTrace);
              this.RefillTimerCallbackHandler = this.RefillChestTimer_Callback;
        }
Example #3
0
        public static Configuration Read(string filePath)
        {
            XmlReaderSettings configReaderSettings = new XmlReaderSettings {
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings
              };

              string configSchemaPath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + ".xsd");
              configReaderSettings.Schemas.Add(null, configSchemaPath);

              XmlDocument document = new XmlDocument();
              using (XmlReader configReader = XmlReader.Create(filePath, configReaderSettings))
            document.Load(configReader);

              // Before validating using the schema, first check if the configuration file's version matches with the supported version.
              XmlElement rootElement = document.DocumentElement;
              string fileVersionRaw;
              if (rootElement.HasAttribute("Version"))
            fileVersionRaw = rootElement.GetAttribute("Version");
              else
            fileVersionRaw = "1.0";

              if (fileVersionRaw != Configuration.CurrentVersion) {
            throw new FormatException(string.Format(
              "The configuration file is either outdated or too new. Expected version was: {0}. File version is: {1}",
              Configuration.CurrentVersion, fileVersionRaw
            ));
              }

              Configuration resultingConfig = new Configuration();
              Configuration.UpdateTileIdArrayByString(resultingConfig.ManuallyProtectableTiles, rootElement["ManuallyProtectableTiles"].InnerXml);
              Configuration.UpdateTileIdArrayByString(resultingConfig.AutoProtectedTiles, rootElement["AutoProtectedTiles"].InnerXml);
              Configuration.UpdateTileIdArrayByString(resultingConfig.NotDeprotectableTiles, rootElement["NotDeprotectableTiles"].InnerXml);
              resultingConfig.MaxProtectionsPerPlayerPerWorld = int.Parse(rootElement["MaxProtectionsPerPlayerPerWorld"].InnerText);
              resultingConfig.MaxBankChestsPerPlayer = int.Parse(rootElement["MaxBankChestsPerPlayer"].InnerXml);

              XmlElement subElement = rootElement["AllowRefillChestContentChanges"];
              if (subElement == null)
            resultingConfig.AllowRefillChestContentChanges = true;
              else
            resultingConfig.AllowRefillChestContentChanges = BoolEx.ParseEx(subElement.InnerXml);

              resultingConfig.EnableBedSpawnProtection = BoolEx.ParseEx(rootElement["EnableBedSpawnProtection"].InnerXml);
              resultingConfig.LoginRequiredForChestUsage = BoolEx.ParseEx(rootElement["LoginRequiredForChestUsage"].InnerXml);
              resultingConfig.AutoShareRefillChests = BoolEx.ParseEx(rootElement["AutoShareRefillChests"].InnerXml);
              resultingConfig.AllowChainedSharing = BoolEx.ParseEx(rootElement["AllowChainedSharing"].InnerXml);
              resultingConfig.AllowChainedShareAltering = BoolEx.ParseEx(rootElement["AllowChainedShareAltering"].InnerXml);
              resultingConfig.AllowWiringProtectedBlocks = BoolEx.ParseEx(rootElement["AllowWiringProtectedBlocks"].InnerXml);
              resultingConfig.AutoDeprotectEverythingOnDestruction = BoolEx.ParseEx(rootElement["AutoDeprotectEverythingOnDestruction"].InnerXml);
              resultingConfig.NotifyAutoProtections = BoolEx.ParseEx(rootElement["NotifyAutoProtection"].InnerXml);
              resultingConfig.NotifyAutoDeprotections = BoolEx.ParseEx(rootElement["NotifyAutoDeprotection"].InnerXml);
              resultingConfig.DungeonChestProtection = BoolEx.ParseEx(rootElement["DungeonChestProtection"].InnerXml);
              resultingConfig.QuickStackNearbyRange = float.Parse(rootElement["QuickStackNearbyRange"].InnerXml);

              XmlElement maxBankChestsElement = rootElement["MaxBankChests"];
              resultingConfig.MaxBankChests = new Dictionary<string,int>();
              foreach (XmlElement limitElement in maxBankChestsElement)
            resultingConfig.MaxBankChests.Add(limitElement.GetAttribute("Group"), int.Parse(limitElement.InnerXml));

              return resultingConfig;
        }
        public UserInteractionHandler(
            PluginTrace trace, PluginInfo pluginInfo, Configuration config, ServerMetadataHandler serverMetadataHandler,
            WorldMetadata worldMetadata, ProtectionManager protectionManager, ChestManager chestManager,
            PluginCooperationHandler pluginCooperationHandler, Func<Configuration> reloadConfigurationCallback
            )
            : base(trace)
        {
            Contract.Requires<ArgumentNullException>(trace != null);
              Contract.Requires<ArgumentException>(!pluginInfo.Equals(PluginInfo.Empty));
              Contract.Requires<ArgumentNullException>(config != null);
              Contract.Requires<ArgumentNullException>(serverMetadataHandler != null);
              Contract.Requires<ArgumentNullException>(worldMetadata != null);
              Contract.Requires<ArgumentNullException>(protectionManager != null);
              Contract.Requires<ArgumentNullException>(pluginCooperationHandler != null);
              Contract.Requires<ArgumentNullException>(reloadConfigurationCallback != null);

              this.PluginInfo = pluginInfo;
              this.Config = config;
              this.ServerMetadataHandler = serverMetadataHandler;
              this.WorldMetadata = worldMetadata;
              this.ChestManager = chestManager;
              this.ProtectionManager = protectionManager;
              this.PluginCooperationHandler = pluginCooperationHandler;
              this.ReloadConfigurationCallback = reloadConfigurationCallback;

              this.PlayerIndexChestDictionary = new Dictionary<int,DPoint>(20);
              this.ChestPlayerIndexDictionary = new Dictionary<DPoint,int>(20);

              #region Command Setup
              base.RegisterCommand(
            new[] { "protector" }, this.RootCommand_Exec, this.RootCommand_HelpCallback
              );
              base.RegisterCommand(
            new[] { "protect", "pt" },
            this.ProtectCommand_Exec, this.ProtectCommand_HelpCallback, ProtectorPlugin.ManualProtect_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "deprotect", "dp" },
            this.DeprotectCommand_Exec, this.DeprotectCommand_HelpCallback, ProtectorPlugin.ManualDeprotect_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "protectioninfo", "ptinfo", "pi" }, this.ProtectionInfoCommand_Exec, this.ProtectionInfoCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "share" }, this.ShareCommand_Exec, this.ShareCommandHelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unshare" }, this.UnshareCommand_Exec, this.UnshareCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "sharepublic" }, this.SharePublicCommand_Exec, this.SharePublicCommandHelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unsharepublic" }, this.UnsharePublicCommand_Exec, this.UnsharePublicCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "sharegroup" }, this.ShareGroupCommand_Exec, this.ShareGroupCommand_HelpCallback,
            ProtectorPlugin.ShareWithGroups_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unsharegroup" }, this.UnshareGroupCommand_Exec, this.UnshareGroup_HelpCallback,
            ProtectorPlugin.ShareWithGroups_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "lockchest", "lchest" },
            this.LockChestCommand_Exec, this.LockChestCommand_HelpCallback, ProtectorPlugin.Utility_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "swapchest", "schest" },
            this.SwapChestCommand_Exec, this.SwapChestCommand_HelpCallback, ProtectorPlugin.Utility_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "refillchest", "rchest" },
            this.RefillChestCommand_Exec, this.RefillChestCommand_HelpCallback, ProtectorPlugin.SetRefillChests_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "refillchestmany", "rchestmany" },
            this.RefillChestManyCommand_Exec, this.RefillChestManyCommand_HelpCallback, ProtectorPlugin.Utility_Permission
              );
              base.RegisterCommand(
            new[] { "bankchest", "bchest" },
            this.BankChestCommand_Exec, this.BankChestCommand_HelpCallback, ProtectorPlugin.SetBankChests_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "dumpbankchest", "dbchest" },
            this.DumpBankChestCommand_Exec, this.DumpBankChestCommand_HelpCallback, ProtectorPlugin.DumpBankChests_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "tradechest", "tchest" },
            this.TradeChestCommand_Exec, this.TradeChestCommand_HelpCallback, ProtectorPlugin.SetTradeChests_Permission,
            allowServer: false
              );
              #endregion

              #if DEBUG
              base.RegisterCommand(new[] { "fc" }, args => {
            for (int i= 0; i < Main.chest.Length; i++) {
              if (i != ChestManager.DummyChestIndex)
            Main.chest[i] = Main.chest[i] ?? new Chest();
            }
              }, requiredPermission: Permissions.maintenance);
              base.RegisterCommand(new[] { "fcnames" }, args => {
            for (int i= 0; i < Main.chest.Length; i++) {
              if (i != ChestManager.DummyChestIndex) {
            Main.chest[i] = Main.chest[i] ?? new Chest();
            Main.chest[i].name = "Chest!";
              }
            }
              }, requiredPermission: Permissions.maintenance);
              #endif
        }
        public UserInteractionHandler(
            PluginTrace trace, PluginInfo pluginInfo, Configuration config, ServerMetadataHandler serverMetadataHandler,
            WorldMetadata worldMetadata, ProtectionManager protectionManager, PluginCooperationHandler pluginCooperationHandler,
            Func<Configuration> reloadConfigurationCallback
            )
            : base(trace)
        {
            Contract.Requires<ArgumentNullException>(trace != null);
              Contract.Requires<ArgumentException>(!pluginInfo.Equals(PluginInfo.Empty));
              Contract.Requires<ArgumentNullException>(config != null);
              Contract.Requires<ArgumentNullException>(serverMetadataHandler != null);
              Contract.Requires<ArgumentNullException>(worldMetadata != null);
              Contract.Requires<ArgumentNullException>(protectionManager != null);
              Contract.Requires<ArgumentNullException>(pluginCooperationHandler != null);
              Contract.Requires<ArgumentNullException>(reloadConfigurationCallback != null);

              this.PluginInfo = pluginInfo;
              this.Config = config;
              this.ServerMetadataHandler = serverMetadataHandler;
              this.WorldMetadata = worldMetadata;
              this.ProtectionManager = protectionManager;
              this.PluginCooperationHandler = pluginCooperationHandler;
              this.ReloadConfigurationCallback = reloadConfigurationCallback;

              #region Command Setup
              base.RegisterCommand(
            new[] { "protector" }, this.RootCommand_Exec, this.RootCommand_HelpCallback
              );
              base.RegisterCommand(
            new[] { "protect", "pt" },
            this.ProtectCommand_Exec, this.ProtectCommand_HelpCallback, ProtectorPlugin.ManualProtect_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "deprotect", "dp" },
            this.DeprotectCommand_Exec, this.DeprotectCommand_HelpCallback, ProtectorPlugin.ManualDeprotect_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "protectioninfo", "ptinfo", "pi" }, this.ProtectionInfoCommand_Exec, this.ProtectionInfoCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "share" }, this.ShareCommand_Exec, this.ShareCommandHelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unshare" }, this.UnshareCommand_Exec, this.UnshareCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "sharepublic" }, this.SharePublicCommand_Exec, this.SharePublicCommandHelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unsharepublic" }, this.UnsharePublicCommand_Exec, this.UnsharePublicCommand_HelpCallback,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "sharegroup" }, this.ShareGroupCommand_Exec, this.ShareGroupCommand_HelpCallback,
            ProtectorPlugin.ShareWithGroups_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "unsharegroup" }, this.UnshareGroupCommand_Exec, this.UnshareGroup_HelpCallback,
            ProtectorPlugin.ShareWithGroups_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "lockchest", "lchest" },
            this.LockChestCommand_Exec, this.LockChestCommand_HelpCallback, ProtectorPlugin.Utility_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "refillchest", "rchest" },
            this.RefillChestCommand_Exec, this.RefillChestCommand_HelpCallback, ProtectorPlugin.SetRefillChests_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "refillchestmany", "rchestmany" },
            this.RefillChestManyCommand_Exec, this.RefillChestManyCommand_HelpCallback, ProtectorPlugin.Utility_Permission
              );
              base.RegisterCommand(
            new[] { "bankchest", "bchest" },
            this.BankChestCommand_Exec, this.BankChestCommand_HelpCallback, ProtectorPlugin.SetBankChests_Permission,
            allowServer: false
              );
              base.RegisterCommand(
            new[] { "dumpbankchest", "dbchest" },
            this.DumpBankChestCommand_Exec, this.DumpBankChestCommand_HelpCallback, ProtectorPlugin.DumpBankChests_Permission,
            allowServer: false
              );
              #endregion
        }
        private void InitUserInteractionHandler()
        {
            Func<Configuration> reloadConfiguration = () => {
            if (this.isDisposed)
              return null;

            this.config = Configuration.Read(ProtectorPlugin.ConfigFilePath);

            this.protectionManager.Config = this.Config;

            return this.config;
              };
              this.userInteractionHandler = new UserInteractionHandler(
            this.Trace, this.PluginInfo, this.Config, this.ServerMetadataHandler,
            this.WorldMetadataHandler.Metadata, this.ProtectionManager, this.PluginCooperationHandler, reloadConfiguration
              );
        }
        private bool InitConfig()
        {
            if (File.Exists(ProtectorPlugin.ConfigFilePath)) {
            try {
              this.config = Configuration.Read(ProtectorPlugin.ConfigFilePath);
            } catch (Exception ex) {
              this.Trace.WriteLineError(
            "Reading the configuration file failed. This plugin will be disabled. Exception details:\n{0}", ex
              );
              this.Trace.WriteLineError("THIS PLUGIN IS DISABLED, EVERYTHING IS UNPROTECTED!");

              this.Dispose();
              return false;
            }
              } else {
            this.config = new Configuration();
              }

              // Invalidate Configuration
              if (this.Config.ManuallyProtectableTiles[(int)BlockType.SandBlock] || this.Config.AutoProtectedTiles[(int)BlockType.SandBlock])
            this.Trace.WriteLineWarning("Protector is configured to protect sand blocks, this is generally not recommended as protections will not move with falling sand and thus cause invalid protections.");
              if (this.Config.ManuallyProtectableTiles[(int)BlockType.SiltBlock] || this.Config.AutoProtectedTiles[(int)BlockType.SiltBlock])
            this.Trace.WriteLineWarning("Protector is configured to protect silt blocks, this is generally not recommended as protections will not move with falling silt and thus cause invalid protections.");
              if (this.Config.ManuallyProtectableTiles[(int)BlockType.IceBlock] || this.Config.AutoProtectedTiles[(int)BlockType.IceBlock])
            this.Trace.WriteLineWarning("Protector is configured to protect ice blocks, this is generally not recommended as protections will not be automatically removed when the ice block disappears.");

              return true;
        }