Change Warps to be an internal module, and lazy load warps/waypoints only when actually required

This commit is contained in:
UnknownShadow200 2023-12-13 19:32:01 +11:00
parent bd55a9e608
commit 9ead72b356
16 changed files with 175 additions and 92 deletions

View File

@ -32,7 +32,7 @@ namespace MCGalaxy
public virtual string shortcut { get { return ""; } }
/// <summary> The type/group of this command (see `CommandTypes` class) </summary>
public abstract string type { get; }
/// <summary> Whether this comand can be used in museums </summary>
/// <summary> Whether this command can be used in museums </summary>
/// <remarks> Level altering (e.g. places a block) commands should return false </remarks>
public virtual bool museumUsable { get { return true; } }
/// <summary> The default minimum rank that is required to use this command </summary>

View File

@ -25,7 +25,6 @@ namespace MCGalaxy.Core {
CheckReviewList(p);
if (p.CanUse("ReachDistance")) LoadReach(p);
LoadWaypoints(p);
p.Ignores.Load(p);
}
@ -51,14 +50,5 @@ namespace MCGalaxy.Core {
p.ReachDistance = reachDist / 32f;
p.Session.SendSetReach(p.ReachDistance);
}
static void LoadWaypoints(Player p) {
try {
p.Waypoints.Filename = Paths.WaypointsDir + p.name + ".save";
p.Waypoints.Load();
} catch (Exception ex) {
Logger.LogError("Error loading waypoints", ex);
}
}
}
}

View File

@ -317,8 +317,6 @@
<Compile Include="Commands\World\CmdNewLvl.cs" />
<Compile Include="Commands\World\CmdOverseer.cs" />
<Compile Include="Commands\World\CmdPause.cs" />
<Compile Include="Commands\World\CmdWarp.cs" />
<Compile Include="Commands\World\CmdWaypoint.cs" />
<Compile Include="Commands\World\CmdPhysics.cs" />
<Compile Include="Commands\World\CmdRenameLvl.cs" />
<Compile Include="Commands\World\CmdResizeLvl.cs" />
@ -604,6 +602,11 @@
<Compile Include="Modules\Relay\RelayBot.cs" />
<Compile Include="Modules\Relay\RelayBotCmd.cs" />
<Compile Include="Modules\Security\IPThrottler.cs" />
<Compile Include="Modules\Warps\CmdWarp.cs" />
<Compile Include="Modules\Warps\CmdWaypoint.cs" />
<Compile Include="Modules\Warps\Warp.cs" />
<Compile Include="Modules\Warps\WarpCommand.cs" />
<Compile Include="Modules\Warps\WarpPlugin.cs" />
<Compile Include="Network\BaseWebSocket.cs" />
<Compile Include="Network\ClassicProtocol.cs" />
<Compile Include="Network\CPESupport.cs" />
@ -634,7 +637,6 @@
<Compile Include="Player\PlayerOperations.cs" />
<Compile Include="Player\PlayerPhysics.cs" />
<Compile Include="Player\SpamChecker.cs" />
<Compile Include="Player\Warp.cs" />
<Compile Include="Database\Undo\UndoFormat.cs" />
<Compile Include="Network\Player.Networking.cs" />
<Compile Include="Scripting\Plugin.cs" />
@ -710,7 +712,9 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Modules\Warps" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -58,6 +58,15 @@ namespace MCGalaxy.Modules.Awards
}
/// <summary> Finds partial matches of 'name' against the list of all awards </summary>
public static string FindMatch(Player p, string name) {
int matches;
Award award = Matcher.Find(p, name, out matches, Awards,
null, a => a.Name, "awards");
return award == null ? null : award.Name;
}
static readonly object saveLock = new object();
public static void Save() {
lock (saveLock)

View File

@ -34,10 +34,12 @@ namespace MCGalaxy.Modules.Awards
}
string[] args = message.SplitSpaces(2);
if (args.Length < 2) { Help(p); return; }
if (args.Length < 2) { Help(p); return; }
string plName = PlayerInfo.FindMatchesPreferOnline(p, args[0]);
if (plName == null) return;
string award = Matcher.FindAwards(p, args[1]);
string award = AwardsList.FindMatch(p, args[1]);
if (award == null) { p.Message("Use &T/Awards &Sfor a list of awards"); return; }
string displayName = p.FormatNick(plName);

View File

@ -0,0 +1,46 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Commands;
using MCGalaxy.Maths;
namespace MCGalaxy.Modules.Warps
{
class CmdWarp : WarpCommand
{
public override string name { get { return "Warp"; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "can manage warps") }; }
}
public override void Use(Player p, string message, CommandData data) {
if (WarpList.Global == null)
WarpList.Global = LoadList("extra/warps.save");
UseCore(p, message, data, WarpList.Global, "Warp");
}
public override void Help(Player p) {
p.Message("&T/Warp [name] &H- Move to that warp");
p.Message("&T/Warp list &H- List all the warps");
p.Message("&T/Warp create [name] &H- Create a warp at your position");
p.Message("&T/Warp delete [name] &H- Deletes a warp");
p.Message("&T/Warp move [name] &H- Moves a warp to your position");
}
}
}

View File

@ -17,15 +17,22 @@
*/
using System;
namespace MCGalaxy.Commands.World {
public sealed class CmdWaypoint : CmdWarp {
namespace MCGalaxy.Modules.Warps
{
sealed class CmdWaypoint : WarpCommand
{
public override string name { get { return "Waypoint"; } }
public override string shortcut { get { return "wp"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Builder; } }
public override CommandPerm[] ExtraPerms { get { return null; } }
public override void Use(Player p, string message, CommandData data) {
UseCore(p, message, data, p.Waypoints, "Waypoint");
public override void Use(Player p, string message, CommandData data) {
if (!p.Extras.Contains("MCG_WAYPOINTS")) {
p.Extras["MCG_WAYPOINTS"] = LoadList(Paths.WAYPOINTS_DIR + p.name + ".save");
}
// TODO: Better thread safety
WarpList waypoints = (WarpList)p.Extras["MCG_WAYPOINTS"];
UseCore(p, message, data, waypoints, "Waypoint");
}
public override void Help(Player p) {

View File

@ -16,10 +16,11 @@ using System;
using System.Collections.Generic;
using System.IO;
namespace MCGalaxy {
namespace MCGalaxy.Modules.Warps
{
/// <summary> A named pair of position and orientation, located on a particular map. </summary>
public class Warp {
public class Warp
{
/// <summary> Position of this warp. </summary>
public Position Pos;
/// <summary> Orientation of this warp. </summary>
@ -30,13 +31,15 @@ namespace MCGalaxy {
public string Level;
}
public sealed class WarpList {
public static WarpList Global = new WarpList();
public sealed class WarpList
{
public static WarpList Global;
public List<Warp> Items = new List<Warp>();
public string Filename;
public Warp Find(string name) {
foreach (Warp wp in Items) {
foreach (Warp wp in Items)
{
if (wp.Name.CaselessEq(name)) return wp;
}
return null;
@ -80,6 +83,16 @@ namespace MCGalaxy {
}
}
/// <summary> Find partial matches of 'name' against this list of warps. </summary>
public Warp FindMatch(Player p, string name) {
string group = (this == Global) ? "warps" : "waypoints";
int matches;
return Matcher.Find(p, name, out matches, Items,
null, wp => wp.Name, group);
}
public void Load() {
if (!File.Exists(Filename)) return;

View File

@ -1,4 +1,4 @@
/*
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
@ -16,22 +16,16 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Threading;
using MCGalaxy.Commands;
using MCGalaxy.Maths;
namespace MCGalaxy.Commands.World {
public class CmdWarp : Command2 {
public override string name { get { return "Warp"; } }
namespace MCGalaxy.Modules.Warps
{
abstract class WarpCommand : Command2
{
public override string type { get { return CommandTypes.World; } }
public override bool museumUsable { get { return false; } }
public override bool SuperUseable { get { return false; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "can manage warps") }; }
}
public override void Use(Player p, string message, CommandData data) {
UseCore(p, message, data, WarpList.Global, "Warp");
}
static void PrintWarp(Player p, Warp warp) {
Vec3S32 pos = warp.Pos.BlockCoords;
@ -52,7 +46,7 @@ namespace MCGalaxy.Commands.World {
group + " list", group + "s", modifier);
return;
} else if (args.Length == 1) {
Warp warp = Matcher.FindWarps(p, warps, cmd);
Warp warp = warps.FindMatch(p, cmd);
if (warp != null) warps.Goto(warp, p);
return;
}
@ -66,32 +60,32 @@ namespace MCGalaxy.Commands.World {
p.Message("{0} {1} created.", group, name);
} else if (IsDeleteAction(cmd)) {
if (checkExtraPerms && !CheckExtraPerm(p, data, 1)) return;
Warp warp = Matcher.FindWarps(p, warps, name);
Warp warp = warps.FindMatch(p, name);
if (warp == null) return;
warps.Remove(warp, p);
p.Message("{0} {1} deleted.", group, warp.Name);
} else if (IsEditAction(cmd)) {
if (checkExtraPerms && !CheckExtraPerm(p, data, 1)) return;
Warp warp = Matcher.FindWarps(p, warps, name);
Warp warp = warps.FindMatch(p, name);
if (warp == null) return;
warps.Update(warp, p);
p.Message("{0} {1} moved.", group, warp.Name);
} else if (cmd.CaselessEq("goto")) {
Warp warp = Matcher.FindWarps(p, warps, name);
Warp warp = warps.FindMatch(p, name);
if (warp != null) warps.Goto(warp, p);
} else {
Help(p);
}
}
public override void Help(Player p) {
p.Message("&T/Warp [name] &H- Move to that warp");
p.Message("&T/Warp list &H- List all the warps");
p.Message("&T/Warp create [name] &H- Create a warp at your position");
p.Message("&T/Warp delete [name] &H- Deletes a warp");
p.Message("&T/Warp move [name] &H- Moves a warp to your position");
protected static WarpList LoadList(string path) {
WarpList list = new WarpList();
list.Filename = path;
list.Load();
return list;
}
}
}

View File

@ -0,0 +1,48 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Events.ServerEvents;
namespace MCGalaxy.Modules.Warps
{
public sealed class WarpsPlugin : Plugin
{
public override string name { get { return "Warps"; } }
Command cmdWarps = new CmdWarp();
Command cmdWaypoints = new CmdWaypoint();
public override void Load(bool startup) {
Server.EnsureDirectoryExists(Paths.WAYPOINTS_DIR);
OnConfigUpdatedEvent.Register(OnConfigUpdated, Priority.Low);
Command.Register(cmdWarps);
Command.Register(cmdWaypoints);
}
public override void Unload(bool shutdown) {
OnConfigUpdatedEvent.Unregister(OnConfigUpdated);
Command.Unregister(cmdWarps, cmdWaypoints);
}
static void OnConfigUpdated() {
if (WarpList.Global == null) return;
WarpList.Global.Load();
}
}
}

View File

@ -221,7 +221,6 @@ namespace MCGalaxy {
internal DateTime cmdUnblocked;
List<DateTime> partialLog;
public WarpList Waypoints;
public DateTime LastPatrol;
public LevelPermission Rank { get { return group.Permission; } }

View File

@ -70,7 +70,6 @@ namespace MCGalaxy {
SetIP(Socket.IP);
CriticalTasks = new VolatileArray<SchedulerTask>();
Waypoints = new WarpList();
spamChecker = new SpamChecker(this);
partialLog = new List<DateTime>(20);

View File

@ -18,15 +18,6 @@
using System;
using System.Collections.Generic;
using MCGalaxy.Core;
using MCGalaxy.Modules.Games.Countdown;
using MCGalaxy.Modules.Games.CTF;
using MCGalaxy.Modules.Games.LS;
using MCGalaxy.Modules.Games.TW;
using MCGalaxy.Modules.Games.ZS;
using MCGalaxy.Modules.Moderation.Notes;
using MCGalaxy.Modules.Relay.Discord;
using MCGalaxy.Modules.Relay.IRC;
using MCGalaxy.Modules.Security;
using MCGalaxy.Scripting;
namespace MCGalaxy
@ -134,20 +125,21 @@ namespace MCGalaxy
public static void LoadAll() {
LoadCorePlugin(new CorePlugin());
LoadCorePlugin(new NotesPlugin());
LoadCorePlugin(new DiscordPlugin());
LoadCorePlugin(new IRCPlugin());
LoadCorePlugin(new IPThrottler());
LoadCorePlugin(new MCGalaxy.Modules.Moderation.Notes.NotesPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Relay.Discord.DiscordPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Relay.IRC.IRCPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Security.IPThrottler());
#if !MCG_STANDALONE
LoadCorePlugin(new MCGalaxy.Modules.Compiling.CompilerPlugin());
#endif
LoadCorePlugin(new MCGalaxy.Modules.Warps.WarpsPlugin());
LoadCorePlugin(new CountdownPlugin());
LoadCorePlugin(new CTFPlugin());
LoadCorePlugin(new LSPlugin());
LoadCorePlugin(new TWPlugin());
LoadCorePlugin(new ZSPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Games.Countdown.CountdownPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Games.CTF.CTFPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Games.LS.LSPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Games.TW.TWPlugin());
LoadCorePlugin(new MCGalaxy.Modules.Games.ZS.ZSPlugin());
IScripting.AutoloadPlugins();
}

View File

@ -48,7 +48,7 @@ namespace MCGalaxy
public const string CPEDisabledFile = "properties/cpe.properties";
public const string ImportsDir = "extra/import/";
public const string WaypointsDir = "extra/Waypoints/";
public const string WAYPOINTS_DIR = "extra/Waypoints/";
/// <summary> Relative path of the file containing a map's bots. </summary>
public static string BotsPath(string map) { return "extra/bots/" + map + ".json"; }

View File

@ -138,7 +138,6 @@ namespace MCGalaxy
PlayerDB.EnsureDirectoriesExist();
EnsureDirectoryExists("extra");
EnsureDirectoryExists(Paths.WaypointsDir);
EnsureDirectoryExists("extra/bots");
EnsureDirectoryExists(Paths.ImportsDir);
EnsureDirectoryExists("blockdefs");
@ -172,8 +171,6 @@ namespace MCGalaxy
AwardsList.Load();
PlayerAwards.Load();
Economy.Load();
WarpList.Global.Filename = "extra/warps.save";
WarpList.Global.Load();
CommandExtraPerms.Load();
ProfanityFilter.Init();
Team.LoadList();

View File

@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using MCGalaxy.Modules.Awards;
namespace MCGalaxy
{
@ -26,14 +25,6 @@ namespace MCGalaxy
/// <remarks> returns number of matches found, and the matching item if only 1 match is found. </remarks>
public static class Matcher
{
/// <summary> Finds partial matches of 'name' against the list of all awards. </summary>
public static string FindAwards(Player p, string name) {
int matches;
Award award = Find(p, name, out matches, AwardsList.Awards,
null, a => a.Name, "awards");
return award == null ? null : award.Name;
}
/// <summary> Finds partial matches of 'color' against the list of colors. </summary>
public static string FindColor(Player p, string color) {
int matches;
@ -72,14 +63,6 @@ namespace MCGalaxy
null, g => Colors.Strip(g.Name), g => g.ColoredName, "ranks");
}
/// <summary> Find partial matches of 'name' against a list of warps. </summary>
public static Warp FindWarps(Player p, WarpList warps, string name) {
string group = (warps == WarpList.Global) ? "warps" : "waypoints";
int matches;
return Find(p, name, out matches, warps.Items,
null, wp => wp.Name, group);
}
/// <summary> Find partial matches of 'name' against the list of zones in a map. </summary>
public static Zone FindZones(Player p, Level lvl, string name) {
int matches;