Redo StringBuffer to not need 'ref index' everywhere

This commit is contained in:
UnknownShadow200 2018-01-18 18:29:10 +11:00
parent b081b53f2a
commit 8692f7693a
10 changed files with 139 additions and 148 deletions

View File

@ -142,14 +142,13 @@ namespace ClassicalSharp.Gui.Screens {
void SetFetchStatus(int progress) {
lastDownload.Clear();
int index = 0;
if (progress == -2) {
lastDownload.Append(ref index, "&eRetrieving texture pack..");
lastDownload.Append("&eRetrieving texture pack..");
} else if (progress == -1) {
lastDownload.Append(ref index, "&eDownloading texture pack");
lastDownload.Append("&eDownloading texture pack");
} else if (progress >= 0 && progress <= 100) {
lastDownload.Append(ref index, "&eDownloading texture pack (&7")
.AppendNum(ref index, progress).Append(ref index, "&e%)");
lastDownload.Append("&eDownloading texture pack (&7")
.AppendNum(progress).Append("&e%)");
}
status.SetText(1, lastDownload.ToString());
}
@ -330,7 +329,7 @@ namespace ClassicalSharp.Gui.Screens {
game.Keyboard.KeyRepeat = true;
input.Text.Clear();
input.Text.Append(0, initialText);
input.Text.Set(initialText);
input.Recreate();
}

View File

@ -49,23 +49,21 @@ namespace ClassicalSharp.Gui.Screens {
accumulator += delta;
if (accumulator < 1) return;
int index = 0;
int fps = (int)(frames / accumulator);
statusBuffer.Clear().AppendNum(fps).Append(" fps, ");
statusBuffer.Clear()
.AppendNum(ref index, fps).Append(ref index, " fps, ");
if (game.ClassicMode) {
statusBuffer.AppendNum(ref index, game.ChunkUpdates).Append(ref index, " chunk updates");
statusBuffer.AppendNum(game.ChunkUpdates).Append(" chunk updates");
} else {
if (game.ChunkUpdates > 0) {
statusBuffer.AppendNum(ref index, game.ChunkUpdates).Append(ref index, " chunks/s, ");
statusBuffer.AppendNum(game.ChunkUpdates).Append(" chunks/s, ");
}
int indices = (game.Vertices >> 2) * 6;
statusBuffer.AppendNum(ref index, indices).Append(ref index, " vertices");
statusBuffer.AppendNum(indices).Append(" vertices");
int ping = PingList.AveragePingMilliseconds();
if (ping != 0) {
statusBuffer.Append(ref index, ", ping ").AppendNum(ref index, ping).Append(ref index, " ms");
statusBuffer.Append(", ping ").AppendNum(ping).Append(" ms");
}
}
@ -154,17 +152,14 @@ namespace ClassicalSharp.Gui.Screens {
hacks.Flying != fly || game.Fov != lastFov) {
speeding = hacks.Speeding; halfSpeeding = hacks.HalfSpeeding; noclip = hacks.Noclip; fly = hacks.Flying;
lastFov = game.Fov;
int index = 0;
statusBuffer.Clear();
if (game.Fov != game.DefaultFov) statusBuffer.Append(ref index, "Zoom fov ")
.AppendNum(ref index, lastFov).Append(ref index, " ");
if (fly) statusBuffer.Append(ref index, "Fly ON ");
if (game.Fov != game.DefaultFov) statusBuffer.Append("Zoom fov ").AppendNum(lastFov).Append(" ");
if (fly) statusBuffer.Append("Fly ON ");
bool speed = (speeding || halfSpeeding) &&
(hacks.CanSpeed || hacks.BaseHorSpeed > 1);
if (speed) statusBuffer.Append(ref index, "Speed ON ");
if (noclip) statusBuffer.Append(ref index, "Noclip ON ");
bool speed = (speeding || halfSpeeding) && (hacks.CanSpeed || hacks.BaseHorSpeed > 1);
if (speed) statusBuffer.Append("Speed ON ");
if (noclip) statusBuffer.Append("Noclip ON ");
hackStates.SetText(statusBuffer.ToString());
}
}

View File

@ -88,17 +88,19 @@ namespace ClassicalSharp.Gui.Widgets {
return;
}
if (typingLogPos == game.Chat.InputLog.Count)
if (typingLogPos == game.Chat.InputLog.Count) {
originalText = Text.ToString();
if (game.Chat.InputLog.Count > 0) {
typingLogPos--;
if (typingLogPos < 0) typingLogPos = 0;
Text.Clear();
Text.Append(0, game.Chat.InputLog[typingLogPos]);
caret = -1;
Recreate();
}
if (game.Chat.InputLog.Count == 0) return;
typingLogPos--;
Text.Clear();
if (typingLogPos < 0) typingLogPos = 0;
Text.Set(game.Chat.InputLog[typingLogPos]);
caret = -1;
Recreate();
}
void DownKey(bool controlDown) {
@ -109,19 +111,19 @@ namespace ClassicalSharp.Gui.Widgets {
return;
}
if (game.Chat.InputLog.Count > 0) {
typingLogPos++;
Text.Clear();
if (typingLogPos >= game.Chat.InputLog.Count) {
typingLogPos = game.Chat.InputLog.Count;
if (originalText != null)
Text.Append(0, originalText);
} else {
Text.Append(0, game.Chat.InputLog[typingLogPos]);
}
caret = -1;
Recreate();
if (game.Chat.InputLog.Count == 0) return;
typingLogPos++;
Text.Clear();
if (typingLogPos >= game.Chat.InputLog.Count) {
typingLogPos = game.Chat.InputLog.Count;
if (originalText != null) Text.Set(originalText);
} else {
Text.Set(game.Chat.InputLog[typingLogPos]);
}
caret = -1;
Recreate();
}
void TabKey() {
@ -154,16 +156,15 @@ namespace ClassicalSharp.Gui.Widgets {
Append(matches[0]);
} else if (matches.Count > 1) {
StringBuffer sb = new StringBuffer(Utils.StringLength);
int index = 0;
sb.Append(ref index, "&e");
sb.AppendNum(ref index, matches.Count);
sb.Append(ref index, " matching names: ");
sb.Append("&e");
sb.AppendNum(matches.Count);
sb.Append(" matching names: ");
for (int i = 0; i < matches.Count; i++) {
string match = matches[i];
if ((sb.Length + match.Length + 1) > sb.Capacity) break;
sb.Append(ref index, match);
sb.Append(ref index, ' ');
sb.Append(match);
sb.Append(' ');
}
game.Chat.Add(sb.ToString(), MessageType.ClientStatus3);
}

View File

@ -152,14 +152,12 @@ namespace ClassicalSharp.Gui.Widgets {
}
StringBuffer buffer = new StringBuffer(count * 4);
int index = 0;
for (int i = 0; i < IDrawer2D.Cols.Length; i++) {
if (i >= 'A' && i <= 'F') continue;
if (IDrawer2D.Cols[i].A == 0) continue;
char code = Utils.CP437ToUnicode((byte)i);
buffer.Append(ref index, '&').Append(ref index, code)
.Append(ref index, '%').Append(ref index, code);
buffer.Append('&').Append(code).Append('%').Append(code);
}
return buffer.ToString();
}

View File

@ -178,20 +178,19 @@ namespace ClassicalSharp.Gui.Widgets {
}
void UpdateBlockInfoString(BlockID block) {
int index = 0;
buffer.Clear();
if (game.PureClassic) { buffer.Append(ref index, "Select block"); return; }
if (game.PureClassic) { buffer.Append("Select block"); return; }
buffer.Append(ref index, BlockInfo.Name[block]);
buffer.Append(BlockInfo.Name[block]);
if (game.ClassicMode) return;
buffer.Append(ref index, " (ID ");
buffer.AppendNum(ref index, block);
buffer.Append(ref index, "&f, place ");
buffer.Append(ref index, BlockInfo.CanPlace[block] ? "&aYes" : "&cNo");
buffer.Append(ref index, "&f, delete ");
buffer.Append(ref index, BlockInfo.CanDelete[block] ? "&aYes" : "&cNo");
buffer.Append(ref index, "&f)");
buffer.Append(" (ID ");
buffer.AppendNum(block);
buffer.Append("&f, place ");
buffer.Append(BlockInfo.CanPlace[block] ? "&aYes" : "&cNo");
buffer.Append("&f, delete ");
buffer.Append(BlockInfo.CanDelete[block] ? "&aYes" : "&cNo");
buffer.Append("&f)");
}
int lastCreatedIndex = -1000;

View File

@ -224,17 +224,16 @@ namespace ClassicalSharp {
}
static void SplitUppercase(StringBuffer buffer, int start, int end) {
int index = 0;
for (int i = start; i < end; i++) {
char c = Block.RawNames[i];
bool upper = Char.IsUpper(c) && i > start;
bool nextLower = i < end - 1 && !Char.IsUpper(Block.RawNames[i + 1]);
if (upper && nextLower) {
buffer.Append(ref index, ' ');
buffer.Append(ref index, Char.ToLower(c));
buffer.Append(' ');
buffer.Append(Char.ToLower(c));
} else {
buffer.Append(ref index, c);
buffer.Append(c);
}
}
}

View File

@ -96,7 +96,6 @@ namespace ClassicalSharp.Commands {
public void PrintDefinedCommands(Game game) {
StringBuffer sb = new StringBuffer(Utils.StringLength);
int index = 0;
for (int i = 0; i < RegisteredCommands.Count; i++) {
Command cmd = RegisteredCommands[i];
@ -105,10 +104,9 @@ namespace ClassicalSharp.Commands {
if ((sb.Length + name.Length + 2) > sb.Capacity) {
game.Chat.Add(sb.ToString());
sb.Clear();
index = 0;
}
sb.Append(ref index, name);
sb.Append(ref index, ", ");
sb.Append(name);
sb.Append(", ");
}
if (sb.Length > 0)

View File

@ -83,10 +83,9 @@ namespace ClassicalSharp {
name = Utils.StripColours(name);
StringBuffer buffer = new StringBuffer(name.Length);
int len = 0;
for (int i = 0; i < name.Length; i++) {
if (Allowed(name[i])) buffer.Append(ref len, name[i]);
if (Allowed(name[i])) buffer.Append(name[i]);
}
logName = buffer.ToString();
}
@ -116,13 +115,13 @@ namespace ClassicalSharp {
if (32 + text.Length > logBuffer.Capacity)
logBuffer = new StringBuffer(32 + text.Length);
int index = 0;
logBuffer.Clear() // [HH:mm:ss] text
.Append(ref index, '[').AppendPaddedNum(ref index, 2, now.Hour)
.Append(ref index, ':').AppendPaddedNum(ref index, 2, now.Minute)
.Append(ref index, ':').AppendPaddedNum(ref index, 2, now.Second)
.Append(ref index, "] ").AppendColourless(ref index, text)
.Append(ref index, Environment.NewLine);
.Append('[').AppendPaddedNum(2, now.Hour)
.Append(':').AppendPaddedNum(2, now.Minute)
.Append(':').AppendPaddedNum(2, now.Second)
.Append("] ").AppendColourless(text)
.Append(Environment.NewLine);
writer.Write(logBuffer.value, 0, logBuffer.Length);
}

View File

@ -6,58 +6,49 @@ namespace ClassicalSharp {
public class StringBuffer {
public char[] value;
public int Capacity;
public int Capacity, Length;
public StringBuffer(int capacity) {
this.Capacity = capacity;
value = new char[capacity];
}
public StringBuffer Append(int index, char c) {
value[index] = c;
public StringBuffer Append(char c) {
value[Length++] = c;
return this;
}
public StringBuffer Append(int index, string s) {
return Append(ref index, s);
}
public StringBuffer Append(ref int index, string s) {
public StringBuffer Append(string s) {
for (int i = 0; i < s.Length; i++)
value[index++] = s[i];
value[Length++] = s[i];
return this;
}
public StringBuffer AppendColourless(ref int index, string s) {
public StringBuffer AppendColourless(string s) {
for (int i = 0; i < s.Length; i++) {
char token = s[i];
if (token == '&')
i++;// Skip over the following colour code.
else
value[index++] = token;
value[Length++] = token;
}
return this;
}
public StringBuffer Append(ref int index, char c) {
value[index++] = c;
return this;
}
static char[] numBuffer = new char[20];
public StringBuffer AppendNum(ref int index, int num) {
public StringBuffer AppendNum(int num) {
int numLen = MakeNum(num);
for (int i = numLen - 1; i >= 0; i--)
value[index++] = numBuffer[i];
value[Length++] = numBuffer[i];
return this;
}
public StringBuffer AppendPaddedNum(ref int index, int minDigits, int num) {
public StringBuffer AppendPaddedNum(int minDigits, int num) {
for (int i = 0; i < minDigits; i++)
numBuffer[i] = '0';
int numLen = Math.Max(minDigits, MakeNum(num));
for (int i = numLen - 1; i >= 0; i--)
value[index++] = numBuffer[i];
value[Length++] = numBuffer[i];
return this;
}
@ -71,54 +62,10 @@ namespace ClassicalSharp {
}
public StringBuffer Clear() {
for (int i = 0; i < Capacity; i++)
value[i] = '\0';
Length = 0;
return this;
}
public void DeleteAt(int index) {
for (int i = index; i < Capacity - 1; i++)
value[i] = value[i + 1];
value[Capacity - 1] = '\0';
}
public void InsertAt(int index, char c) {
for (int i = Capacity - 1; i > index; i--)
value[i] = value[i - 1];
value[index] = c;
}
public bool Empty {
get {
for (int i = 0; i < Capacity; i++) {
if (value[i] != '\0') return false;
}
return true;
}
}
public int Length {
get {
int len = Capacity;
for (int i = Capacity - 1; i >= 0; i--) {
if (value[i] != '\0') break;
len--;
}
return len;
}
}
public int TextLength {
get {
int len = Capacity;
for (int i = Capacity - 1; i >= 0; i--) {
if (value[i] != '\0' && value[i] != ' ') break;
len--;
}
return len;
}
}
public override string ToString() {
return new String(value, 0, Length);
}

View File

@ -3,13 +3,69 @@ using System;
namespace ClassicalSharp {
public unsafe sealed class WrappableStringBuffer : StringBuffer {
public unsafe sealed class WrappableStringBuffer {
public int Capacity;
public char[] value;
char[] wrap;
public WrappableStringBuffer(int capacity) : base(capacity) {
public WrappableStringBuffer(int capacity) {
this.Capacity = capacity;
value = new char[capacity];
wrap = new char[capacity];
}
public void DeleteAt(int index) {
for (int i = index; i < Capacity - 1; i++)
value[i] = value[i + 1];
value[Capacity - 1] = '\0';
}
public void InsertAt(int index, char c) {
for (int i = Capacity - 1; i > index; i--)
value[i] = value[i - 1];
value[index] = c;
}
public void Set(string s) {
for (int i = 0; i < s.Length; i++) value[i] = s[i];
}
public void Clear() {
for (int i = 0; i < Capacity; i++) value[i] = '\0';
}
public bool Empty {
get {
for (int i = 0; i < Capacity; i++) {
if (value[i] != '\0') return false;
}
return true;
}
}
public int Length {
get {
int len = Capacity;
for (int i = Capacity - 1; i >= 0; i--) {
if (value[i] != '\0') break;
len--;
}
return len;
}
}
public int TextLength {
get {
int len = Capacity;
for (int i = Capacity - 1; i >= 0; i--) {
if (value[i] != '\0' && value[i] != ' ') break;
len--;
}
return len;
}
}
public void WordWrap(IDrawer2D drawer, string[] lines, int maxLines, int maxPerLine) {
int len = Length;
int* lineLens = stackalloc int[lines.Length];