uses stringbuilder instead of alloc+concat for speed now

This commit is contained in:
David Anderson 2006-03-16 19:29:46 +00:00
parent 82fe1e10d9
commit 89e13334ae
2 changed files with 105 additions and 79 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Collections; using System.Collections;
using System.Text;
namespace BinLogReader namespace BinLogReader
{ {
@ -47,45 +48,52 @@ public static bool HasFlag(BinLogFlags a, BinLogFlags b)
return ( (a & b) == b ); return ( (a & b) == b );
} }
public static string PluginText(Plugin pl, BinLogFlags flags) public static void PluginText(StringBuilder sb, Plugin pl, BinLogFlags flags)
{ {
string plugintext = "";
if (HasFlag(flags, BinLogFlags.Show_PlugId) if (HasFlag(flags, BinLogFlags.Show_PlugId)
&& HasFlag(flags, BinLogFlags.Show_PlugFile)) && HasFlag(flags, BinLogFlags.Show_PlugFile))
{ {
plugintext = "\"" + pl.File + "\"" + " (" + pl.Index + ")"; sb.Append("\"");
sb.Append(pl.File);
sb.Append("\"");
sb.Append(" (");
sb.Append(pl.Index);
sb.Append(")");
} }
else if (HasFlag(flags, BinLogFlags.Show_PlugId)) else if (HasFlag(flags, BinLogFlags.Show_PlugId))
{ {
plugintext = pl.Index.ToString(); sb.Append(pl.Index);
} }
else if (HasFlag(flags, BinLogFlags.Show_PlugFile)) else if (HasFlag(flags, BinLogFlags.Show_PlugFile))
{ {
plugintext = "\"" + pl.File + "\""; sb.Append("\"");
sb.Append(pl.File);
sb.Append("\"");
} }
return plugintext;
} }
public static string BinLogString(BinLogEntry ble, BinLogFlags flags) public static void BinLogString(StringBuilder sb, BinLogEntry ble, BinLogFlags flags)
{ {
string logtext = ""; bool realtime = false;
if (HasFlag(flags, BinLogFlags.Show_RealTime)) if (HasFlag(flags, BinLogFlags.Show_RealTime))
logtext += ble.realtime.ToString(); {
sb.Append(ble.realtime.ToString());
realtime = true;
}
if (HasFlag(flags, BinLogFlags.Show_GameTime)) if (HasFlag(flags, BinLogFlags.Show_GameTime))
{ {
if (logtext.Length > 0) if (realtime)
{ {
logtext += ", " + ble.gametime.ToString(); sb.Append(", ");
sb.Append(ble.gametime.ToString());
} }
else else
{ {
logtext += ble.gametime.ToString(); sb.Append(ble.gametime.ToString());
} }
} }
logtext += ": "; sb.Append(": ");
logtext += ble.ToLogString(flags); ble.ToLogString(sb, flags);
return logtext;
} }
public float gametime public float gametime
@ -116,7 +124,7 @@ public long realtime
} }
public abstract BinLogOp Op(); public abstract BinLogOp Op();
public abstract string ToLogString(BinLogFlags flags); public abstract void ToLogString(StringBuilder sb, BinLogFlags flags);
}; };
public class BinLogSetLine : BinLogEntry public class BinLogSetLine : BinLogEntry
@ -137,12 +145,13 @@ public BinLogSetLine(int _line, float gt, long rt, Plugin _pl)
line = _line; line = _line;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugtext = BinLogEntry.PluginText(plugin, flags); sb.Append("Plugin ");
string logtext = "Plugin hit line " + Line + "."; BinLogEntry.PluginText(sb, plugin, flags);
sb.Append(" hit line ");
return logtext; sb.Append(Line);
sb.Append(".");
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -169,13 +178,15 @@ public BinLogPublic(int pi, float gt, long rt, Plugin _pl)
pubidx = pi; pubidx = pi;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugtext = BinLogEntry.PluginText(plugin, flags); sb.Append("Plugin ");
string logtext = "Plugin " + plugtext + " had public function "; BinLogEntry.PluginText(sb, plugin, flags);
logtext += "\"" + Public + "\" (" + pubidx + ") called."; sb.Append(" had public function \"");
sb.Append(Public);
return logtext; sb.Append("\" (");
sb.Append(pubidx);
sb.Append(") called.");
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -198,13 +209,17 @@ public BinLogSetString(long addr, int _maxlen, string fmt, float gt, long rt, Pl
text = fmt; text = fmt;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugtext = BinLogEntry.PluginText(plugin, flags); sb.Append("Setting string (addr ");
string logtext = "Setting string (addr " + address + ") (maxlen " + maxlen + ") from Plugin " + plugtext + ". String:"; sb.Append(address);
logtext += "\n\t " + text; sb.Append(") (maxlen ");
sb.Append(maxlen);
return logtext; sb.Append(") from Plugin ");
BinLogEntry.PluginText(sb, plugin, flags);
sb.Append(". String:");
sb.Append("\n\t ");
sb.Append(text);
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -225,13 +240,15 @@ public BinLogGetString(long addr, string fmt, float gt, long rt, Plugin _pl)
text = fmt; text = fmt;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugtext = BinLogEntry.PluginText(plugin, flags); sb.Append("Retrieving string (addr ");
string logtext = "Retrieving string (addr " + address + ") from Plugin " + plugtext + ". String:"; sb.AppendFormat("0x{0:X}", address);
logtext += "\n\t " + text; sb.Append(") from Plugin ");
BinLogEntry.PluginText(sb, plugin, flags);
return logtext; sb.Append(". String:");
sb.Append("\n\t ");
sb.Append(text);
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -250,9 +267,10 @@ public BinLogNativeRet(long ret, float gt, long rt, Plugin _pl)
returnval = ret; returnval = ret;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
return "Native returned: " + returnval; sb.Append("Native returned: ");
sb.Append(returnval);
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -281,14 +299,17 @@ public BinLogNativeCall(int na, int nu, float gt, long rt, Plugin _pl)
numparams = nu; numparams = nu;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugtext = BinLogEntry.PluginText(plugin, flags); sb.Append("Plugin ");
string logtext = "Plugin " + plugtext + " called native "; BinLogEntry.PluginText(sb, plugin, flags);
logtext += "\"" + Native + "\" (" + nativeidx + ")"; sb.Append(" called native \"");
logtext += " with " + numparams + " parameters."; sb.Append(Native);
sb.Append("\" (");
return logtext; sb.Append(nativeidx);
sb.Append(") with ");
sb.Append(numparams);
sb.Append(" parameters.");
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -306,25 +327,26 @@ public BinLogSimple(BinLogOp op, float gt, long rt, Plugin _pl) :
my_op = op; my_op = op;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
switch (my_op) switch (my_op)
{ {
case BinLogOp.BinLog_Start: case BinLogOp.BinLog_Start:
{ {
return "Binary log started."; sb.Append("Binary log started.");
break;
} }
case BinLogOp.BinLog_End: case BinLogOp.BinLog_End:
{ {
return "Binary log ended."; sb.Append("Binary log ended.");
break;
} }
case BinLogOp.BinLog_Invalid: case BinLogOp.BinLog_Invalid:
{ {
return "Binary log corrupt past this point."; sb.Append("Binary log corrupt past this point.");
break;
} }
} }
return "";
} }
public override BinLogOp Op() public override BinLogOp Op()
@ -359,21 +381,19 @@ public BinLogNativeParams(float gt, long rt, Plugin _pl)
{ {
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string logtext = "Native parameters: ("; sb.Append("Native parameters: (");
if (plist != null) if (plist != null)
{ {
for (int i=0; i<plist.Count; i++) for (int i=0; i<plist.Count; i++)
{ {
logtext += plist[i].ToString(); sb.Append(plist[i].ToString());
if (i < plist.Count - 1) if (i < plist.Count - 1)
logtext += ", "; sb.Append(", ");
} }
} }
logtext += ")"; sb.Append(")");
return logtext;
} }
} }
@ -396,13 +416,16 @@ public BinLogFmtString(int pa, int ma, string str, float gt, long rt, Plugin _pl
text = str; text = str;
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugintext = BinLogEntry.PluginText(pl, flags); sb.Append("Plugin ");
string logtext = "Plugin " + plugintext + " formatted parameter " + parm; BinLogEntry.PluginText(sb, pl, flags);
logtext += " (maxlen " + maxlen + "), result: \n\t" + text; sb.Append(" formatted parameter ");
sb.Append(parm);
return logtext; sb.Append(" (maxlen ");
sb.Append(maxlen);
sb.Append("), result: \n\t");
sb.Append(text);
} }
} }
@ -439,13 +462,15 @@ public string version
} }
} }
public override string ToLogString(BinLogFlags flags) public override void ToLogString(StringBuilder sb, BinLogFlags flags)
{ {
string plugintext = BinLogEntry.PluginText(pl, flags); sb.Append("Plugin ");
string logtext = "Plugin " + plugintext + " registered as "; BinLogEntry.PluginText(sb, pl, flags);
logtext += "(\"" + _title + "\", \"" + _version + "\")"; sb.Append(" registered as (\"");
sb.Append(_title);
return logtext; sb.Append("\", \"");
sb.Append(_version);
sb.Append("\")");
} }
public BinLogRegister(float gt, long rt, Plugin _pl) : public BinLogRegister(float gt, long rt, Plugin _pl) :

View File

@ -4,6 +4,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Windows.Forms; using System.Windows.Forms;
using System.Data; using System.Data;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace BinLogReader namespace BinLogReader
@ -309,15 +310,15 @@ private void UpdateViews(ViewAreas v, BinLogFlags b)
{ {
ArrayList al = binlog.OpList; ArrayList al = binlog.OpList;
BinLogEntry ble; BinLogEntry ble;
string fulltext = ""; StringBuilder sb = new StringBuilder(al.Count * 10);
BinLogFlags flags = (BinLogFlags.Show_GameTime | BinLogFlags.Show_PlugFile | BinLogFlags.Show_PlugId);
for (int i=0; i<al.Count; i++) for (int i=0; i<al.Count; i++)
{ {
ble = (BinLogEntry)al[i]; ble = (BinLogEntry)al[i];
fulltext += BinLogEntry.BinLogString(ble, BinLogEntry.BinLogString(sb, ble, flags);
(BinLogFlags.Show_GameTime | BinLogFlags.Show_PlugFile | BinLogFlags.Show_PlugId)); sb.Append("\n");
fulltext += "\n";
} }
TextLog.Text = fulltext; TextLog.Text = sb.ToString();
} }
} }