Move classes into Program class.
This commit is contained in:
parent
497f5887bd
commit
eac331b4ee
|
@ -4,40 +4,43 @@ using System.Collections.Generic;
|
|||
|
||||
namespace IngameScript
|
||||
{
|
||||
public class ConfigParser
|
||||
partial class Program
|
||||
{
|
||||
private Dictionary<string, string> _config;
|
||||
private IMyTerminalBlock _input;
|
||||
|
||||
public ConfigParser(IMyTerminalBlock input)
|
||||
public class ConfigParser
|
||||
{
|
||||
_input = input;
|
||||
_config = new Dictionary<string, string>();
|
||||
Parse();
|
||||
}
|
||||
private Dictionary<string, string> _config;
|
||||
private IMyTerminalBlock _input;
|
||||
|
||||
// Get a config value, or a default value.
|
||||
// that also does type inference, but the type of `defaultValue` must contain a `Parse()` method.
|
||||
public T GetValue<T>(string key, T defaultValue)
|
||||
{
|
||||
if (!_config.ContainsKey(key))
|
||||
public ConfigParser(IMyTerminalBlock input)
|
||||
{
|
||||
return defaultValue;
|
||||
|
||||
_input = input;
|
||||
_config = new Dictionary<string, string>();
|
||||
Parse();
|
||||
}
|
||||
return (T)Convert.ChangeType(_config[key], typeof(T));
|
||||
}
|
||||
|
||||
// Only call this method manually if you are monitoring CustomData for changes.
|
||||
public void Parse()
|
||||
{
|
||||
_config.Clear();
|
||||
string[] lines = _input.CustomData.Split('\n');
|
||||
foreach (string line in lines)
|
||||
// Get a config value, or a default value.
|
||||
// that also does type inference, but the type of `defaultValue` must contain a `Parse()` method.
|
||||
public T GetValue<T>(string key, T defaultValue)
|
||||
{
|
||||
string[] tokens = line.Split('=');
|
||||
if (tokens.Length != 2) continue;
|
||||
_config[tokens[0]] = tokens[1];
|
||||
if (!_config.ContainsKey(key))
|
||||
{
|
||||
return defaultValue;
|
||||
|
||||
}
|
||||
return (T)Convert.ChangeType(_config[key], typeof(T));
|
||||
}
|
||||
|
||||
// Only call this method manually if you are monitoring CustomData for changes.
|
||||
public void Parse()
|
||||
{
|
||||
_config.Clear();
|
||||
string[] lines = _input.CustomData.Split('\n');
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string[] tokens = line.Split('=');
|
||||
if (tokens.Length != 2) continue;
|
||||
_config[tokens[0]] = tokens[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,70 +8,82 @@ using System.Text;
|
|||
|
||||
namespace IngameScript
|
||||
{
|
||||
public class Console
|
||||
partial class Program
|
||||
{
|
||||
private MyGridProgram _program;
|
||||
private int _maxLines;
|
||||
private List<string> _buffer;
|
||||
|
||||
private const int DefaultMaxLines = 10;
|
||||
|
||||
public Console(MyGridProgram program)
|
||||
public interface IConsole
|
||||
{
|
||||
_program = program;
|
||||
_buffer = new List<string>();
|
||||
|
||||
// Check the PB's custom data for a maxlines directive.
|
||||
ConfigParser config = new ConfigParser(_program.Me);
|
||||
_maxLines = config.GetValue("ConsoleMaxLines", DefaultMaxLines);
|
||||
void Print(string text);
|
||||
void PrintLower(string text);
|
||||
}
|
||||
|
||||
public PrefixedConsole CreatePrefixedConsole(string prefix)
|
||||
public class Console : IConsole
|
||||
{
|
||||
return new PrefixedConsole(this, prefix);
|
||||
private MyGridProgram _program;
|
||||
private int _maxLines;
|
||||
private List<string> _buffer;
|
||||
|
||||
private const int DefaultMaxLines = 10;
|
||||
|
||||
public Console(MyGridProgram program)
|
||||
{
|
||||
_program = program;
|
||||
_buffer = new List<string>();
|
||||
|
||||
// Check the PB's custom data for a maxlines directive.
|
||||
ConfigParser config = new ConfigParser(_program.Me);
|
||||
_maxLines = config.GetValue("ConsoleMaxLines", DefaultMaxLines);
|
||||
}
|
||||
|
||||
public PrefixedConsole CreatePrefixedConsole(string prefix)
|
||||
{
|
||||
return new PrefixedConsole(this, prefix);
|
||||
}
|
||||
|
||||
public void Print(string text)
|
||||
{
|
||||
_program.Echo(text);
|
||||
_program.Me.GetSurface(0).WriteText(writeToBuffer(text));
|
||||
}
|
||||
|
||||
// Text written with this method goes to the lower screen / keyboard,
|
||||
// with no buffering.
|
||||
public void PrintLower(string text)
|
||||
{
|
||||
_program.Me.GetSurface(1).WriteText(text);
|
||||
}
|
||||
|
||||
// Adds the text to the buffer, trims the top if necessary, and builds a printable
|
||||
// string.
|
||||
private string writeToBuffer(string text)
|
||||
{
|
||||
_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();
|
||||
}
|
||||
}
|
||||
|
||||
public void Print(string text)
|
||||
// This class is necessary because we need to keep a *single* buffer
|
||||
// to write to. So we can ask the primary Console for a prefixer.
|
||||
public class PrefixedConsole : IConsole
|
||||
{
|
||||
_program.Echo(text);
|
||||
_program.Me.GetSurface(0).WriteText(writeToBuffer(text));
|
||||
}
|
||||
private IConsole _console;
|
||||
private string _prefix;
|
||||
|
||||
// Text written with this method goes to the lower screen / keyboard,
|
||||
// with no buffering.
|
||||
public void PrintLower(string text)
|
||||
{
|
||||
_program.Me.GetSurface(1).WriteText(text);
|
||||
}
|
||||
public PrefixedConsole(IConsole console, string prefix)
|
||||
{
|
||||
_console = console;
|
||||
_prefix = prefix + ": ";
|
||||
}
|
||||
|
||||
// Adds the text to the buffer, trims the top if necessary, and builds a printable
|
||||
// string.
|
||||
private string writeToBuffer(string text)
|
||||
{
|
||||
_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();
|
||||
}
|
||||
}
|
||||
public void Print(string text)
|
||||
{
|
||||
_console.Print(_prefix + text);
|
||||
}
|
||||
|
||||
// This class is necessary because we need to keep a *single* buffer
|
||||
// to write to. So we can ask the primary Console for a prefixer.
|
||||
public class PrefixedConsole
|
||||
{
|
||||
private Console _console;
|
||||
private string _prefix;
|
||||
|
||||
public PrefixedConsole(Console console, string prefix)
|
||||
{
|
||||
_console = console;
|
||||
_prefix = prefix + ": ";
|
||||
}
|
||||
|
||||
public void Print(string text)
|
||||
{
|
||||
_console.Print(_prefix + text);
|
||||
// sub-consoles can't print to the ephemeral display
|
||||
public void PrintLower(string text) { }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user