Major refactor and cleanup as we look towards reuse.

This commit is contained in:
2025-02-11 11:03:48 -05:00
parent 5db025bb47
commit af97dbf6e8
6 changed files with 138 additions and 128 deletions

View File

@ -9,35 +9,48 @@ using VRage.Game.ModAPI.Ingame.Utilities;
namespace IngameScript
{
// A Program that supports consoles by initializing a MyIni instance
// and providing a public console for member objects to refer back to.
// (It is probably an anti-pattern that this lives outside of the Program class,
// but we can't find a cleaner way to implement this.)
public interface IConsoleProgram
{
MyIni Ini { get; }
Program.IConsole Console { get; }
}
partial class Program
{
public interface IConsole
{
void Print(string text);
void PrintLower(string text);
void UpdateTickCount();
}
public class Console : IConsole
// You should only instantiate one MainConsole, typically in your Program code.
// Either use a reference to that instance directly where needed or use it to create PrefixedConsoles.
public class MainConsole : IConsole
{
private MyGridProgram _program;
private Program _program;
private int _maxLines;
private List<string> _buffer;
private StringBuilder _builder;
private string _programName;
private int _tickCount = 0;
private const int DefaultMaxLines = 10;
public Console(MyGridProgram program, MyIni ini)
public MainConsole(Program program, string programName)
{
_program = program;
_programName = programName;
_buffer = new List<string>();
_builder = new StringBuilder();
// Check the PB's custom data for a maxlines directive.
ini.TryParse(program.Me.CustomData);
_maxLines = ini.Get("console", "maxLines").ToInt32(DefaultMaxLines);
}
public PrefixedConsole CreatePrefixedConsole(string prefix)
{
return new PrefixedConsole(this, prefix);
_program.Ini.TryParse(program.Me.CustomData);
_maxLines = _program.Ini.Get("console", "maxLines").ToInt32(DefaultMaxLines);
}
public void Print(string text)
@ -46,8 +59,16 @@ namespace IngameScript
_program.Me.GetSurface(0).WriteText(writeToBuffer(text));
}
// Text written with this method goes to the lower screen / keyboard,
// with no buffering.
// Write the "standard" text to the lower console
public void UpdateTickCount()
{
PrintLower($"Airlock Controller\nTotal Ticks: {++_tickCount}");
}
// Most programs probably want to use UpdateTickCount to get program name and
// tick information.
// If you want something else on the lower screen, write it here. This is unbuffered,
// so text will be replaced instead of appended.
public void PrintLower(string text)
{
_program.Me.GetSurface(1).WriteText(text);
@ -57,11 +78,11 @@ namespace IngameScript
// string.
private string writeToBuffer(string text)
{
_builder.Clear();
_buffer.Add(text);
if (_buffer.Count > _maxLines) _buffer.RemoveAt(0);
StringBuilder result = new StringBuilder("", 800);
foreach (string line in _buffer) result.AppendLine(line);
return result.ToString();
foreach (string line in _buffer) _builder.AppendLine(line);
return _builder.ToString();
}
}
@ -85,6 +106,7 @@ namespace IngameScript
// sub-consoles can't print to the ephemeral display
public void PrintLower(string text) { }
public void UpdateTickCount() { }
}
}
}