Add console system to print debug output to the PB screen.
This commit is contained in:
77
Mixins/Console/Console.cs
Normal file
77
Mixins/Console/Console.cs
Normal file
@ -0,0 +1,77 @@
|
||||
// A utility class representing output to the console.
|
||||
// Provides a Print() function that will both call MyGridProgram.Echo()
|
||||
// and also output to the Programmable Block's LCD.
|
||||
|
||||
using Sandbox.ModAPI.Ingame;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace IngameScript
|
||||
{
|
||||
public class Console
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
11
Mixins/Console/Console.projitems
Normal file
11
Mixins/Console/Console.projitems
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||
<HasSharedItems>true</HasSharedItems>
|
||||
<SharedGUID>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</SharedGUID>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)/**/*.cs" Visible=" '$(ShowCommonFiles)' == 'true' " />
|
||||
</ItemGroup>
|
||||
</Project>
|
19
Mixins/Console/Console.shproj
Normal file
19
Mixins/Console/Console.shproj
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</ProjectGuid>
|
||||
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props"/>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props"/>
|
||||
<PropertyGroup/>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="Console.projitems" Label="Shared"/>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/>
|
||||
</Project>
|
Reference in New Issue
Block a user