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) {
Level lvl = p.level;
Vec3S32 P = marks[0];
string owners = "";
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));
string zoneMsg = p.level.FindZoneOwners((ushort)P.X, (ushort)P.Y, (ushort)P.Z);
Player.Message(p, zoneMsg);
return true;
}

View File

@ -49,7 +49,7 @@ namespace MCGalaxy.Commands.World {
Player.Message(p, "Cleared &cALL %Sblock changes for &d" + lvl.name);
} else if (args[0] == "disable") {
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);
} else if (args[0] == "enable") {
lvl.UseBlockDB = true;

View File

@ -175,23 +175,18 @@ namespace MCGalaxy {
bool CheckZonePerms(Player p, ushort x, ushort y, ushort z, ref bool inZone) {
if (p.Rank < LevelPermission.Admin) {
string owners = "";
bool zoneAllow = FindZones(p, x, y, z, ref inZone, ref owners);
bool zoneAllow = FindZones(p, x, y, z, ref inZone);
if (zoneAllow) return true;
if (p.ZoneSpam > DateTime.UtcNow) return false;
if (owners != "")
Player.Message(p, "This zone belongs to &b" + owners.Remove(0, 2) + ".");
else
Player.Message(p, "This zone belongs to no one.");
Player.Message(p, FindZoneOwners(x, y, z));
p.ZoneSpam = DateTime.UtcNow.AddSeconds(2);
return false;
}
return true;
}
bool FindZones(Player p, ushort x, ushort y, ushort z,
ref bool inZone, ref string Owners) {
bool FindZones(Player p, ushort x, ushort y, ushort z, ref bool inZone) {
if (ZoneList.Count == 0) return true;
bool zoneAllow = true;
@ -204,16 +199,32 @@ namespace MCGalaxy {
if (zn.Owner.Length >= 3 && zn.Owner.StartsWith("grp")) {
string grpName = zn.Owner.Substring(3);
if (Group.Find(grpName).Permission <= p.Rank) return true;
Owners += ", " + grpName;
} else {
if (zn.Owner.CaselessEq(p.name)) return true;
Owners += ", " + zn.Owner;
}
zoneAllow = false;
}
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) {
if (p.ZoneSpam <= DateTime.UtcNow) {
BuildAccess.CheckDetailed(p, false);