Separate FindZone into FindZone and FindZoneOwners to optimise performance and reduce memory allocations.

FindZone would always create a string list of owners (for every block affected), instead of just every two seconds. This resulted in a huge amount of memory allocations when drawing in a world you were not zone whitelisted in.
This commit is contained in:
UnknownShadow200 2016-08-31 12:26:08 +10:00
parent 9873c32550
commit 267787b799
4 changed files with 30 additions and 35 deletions

View File

@ -103,25 +103,9 @@ namespace MCGalaxy.Commands {
} }
bool CheckZone(Player p, Vec3S32[] marks, object state, byte type, byte extType) { bool CheckZone(Player p, Vec3S32[] marks, object state, byte type, byte extType) {
Level lvl = p.level;
Vec3S32 P = marks[0]; Vec3S32 P = marks[0];
string owners = ""; string zoneMsg = p.level.FindZoneOwners((ushort)P.X, (ushort)P.Y, (ushort)P.Z);
Player.Message(p, zoneMsg);
for (int i = 0; i < lvl.ZoneList.Count; i++) {
Level.Zone zn = lvl.ZoneList[i];
if (P.X < zn.smallX || P.X > zn.bigX || P.Y < zn.smallY || P.Y > zn.bigY || P.Z < zn.smallZ || P.Z > zn.bigZ)
continue;
if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp"))
owners += ", " + zn.Owner.Substring(3);
else if (zn.Owner != "")
owners += ", " + zn.Owner;
}
if (owners.Length == 0)
Player.Message(p, "No zones affect this block.");
else
Player.Message(p, "This zone belongs to &b{0}.", owners.Remove(0, 2));
return true; return true;
} }

View File

@ -49,7 +49,7 @@ namespace MCGalaxy.Commands.World {
Player.Message(p, "Cleared &cALL %Sblock changes for &d" + lvl.name); Player.Message(p, "Cleared &cALL %Sblock changes for &d" + lvl.name);
} else if (args[0] == "disable") { } else if (args[0] == "disable") {
lvl.UseBlockDB = false; lvl.UseBlockDB = false;
Player.Message(p, "&cDisabled %Srecording further block changesfor &d" + lvl.name); Player.Message(p, "&cDisabled %Srecording further block changes for &d" + lvl.name);
Level.SaveSettings(lvl); Level.SaveSettings(lvl);
} else if (args[0] == "enable") { } else if (args[0] == "enable") {
lvl.UseBlockDB = true; lvl.UseBlockDB = true;

View File

@ -175,23 +175,18 @@ namespace MCGalaxy {
bool CheckZonePerms(Player p, ushort x, ushort y, ushort z, ref bool inZone) { bool CheckZonePerms(Player p, ushort x, ushort y, ushort z, ref bool inZone) {
if (p.Rank < LevelPermission.Admin) { if (p.Rank < LevelPermission.Admin) {
string owners = ""; bool zoneAllow = FindZones(p, x, y, z, ref inZone);
bool zoneAllow = FindZones(p, x, y, z, ref inZone, ref owners);
if (zoneAllow) return true; if (zoneAllow) return true;
if (p.ZoneSpam > DateTime.UtcNow) return false; if (p.ZoneSpam > DateTime.UtcNow) return false;
if (owners != "") Player.Message(p, FindZoneOwners(x, y, z));
Player.Message(p, "This zone belongs to &b" + owners.Remove(0, 2) + ".");
else
Player.Message(p, "This zone belongs to no one.");
p.ZoneSpam = DateTime.UtcNow.AddSeconds(2); p.ZoneSpam = DateTime.UtcNow.AddSeconds(2);
return false; return false;
} }
return true; return true;
} }
bool FindZones(Player p, ushort x, ushort y, ushort z, bool FindZones(Player p, ushort x, ushort y, ushort z, ref bool inZone) {
ref bool inZone, ref string Owners) {
if (ZoneList.Count == 0) return true; if (ZoneList.Count == 0) return true;
bool zoneAllow = true; bool zoneAllow = true;
@ -204,16 +199,32 @@ namespace MCGalaxy {
if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp")) { if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp")) {
string grpName = zn.Owner.Substring(3); string grpName = zn.Owner.Substring(3);
if (Group.Find(grpName).Permission <= p.Rank) return true; if (Group.Find(grpName).Permission <= p.Rank) return true;
Owners += ", " + grpName;
} else { } else {
if (zn.Owner.CaselessEq(p.name)) return true; if (zn.Owner.CaselessEq(p.name)) return true;
Owners += ", " + zn.Owner;
} }
zoneAllow = false; zoneAllow = false;
} }
return zoneAllow; return zoneAllow;
} }
internal string FindZoneOwners(ushort x, ushort y, ushort z) {
string owners = "";
for (int i = 0; i < ZoneList.Count; i++) {
Zone zn = ZoneList[i];
if (x < zn.smallX || x > zn.bigX || y < zn.smallY || y > zn.bigY || z < zn.smallZ || z > zn.bigZ)
continue;
if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp")) {
owners += ", " + zn.Owner.Substring(3);
} else {
owners += ", " + zn.Owner;
}
}
if (owners == "") return "No zones affect this block";
return "This zone belongs to &b" + owners.Remove(0, 2) + ".";
}
bool CheckRank(Player p) { bool CheckRank(Player p) {
if (p.ZoneSpam <= DateTime.UtcNow) { if (p.ZoneSpam <= DateTime.UtcNow) {
BuildAccess.CheckDetailed(p, false); BuildAccess.CheckDetailed(p, false);