2015-03-16 04:16:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace ScriptTool
|
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public class Compiler : ICompiler
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-25 17:35:12 +00:00
|
|
|
|
private static int[] virtualWidths;
|
|
|
|
|
private static int[] renderWidths;
|
2018-04-04 05:28:38 +01:00
|
|
|
|
private static int[] virtualWidthsSaturn;
|
|
|
|
|
private static int[] renderWidthsSaturn;
|
2015-03-25 17:35:12 +00:00
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public IEnumerable<IControlCode> ControlCodes { get; set; }
|
2015-03-16 04:16:12 +00:00
|
|
|
|
public Dictionary<string, int> AddressMap { get; set; }
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public Func<byte[], int, bool> ControlCodePredicate { get; set; }
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
2015-03-25 17:35:12 +00:00
|
|
|
|
static Compiler()
|
|
|
|
|
{
|
2019-01-07 00:40:02 +00:00
|
|
|
|
byte[] widths = Asset.ReadAllBytes("m2-widths-main.bin");
|
|
|
|
|
byte[] saturnWidths = Asset.ReadAllBytes("m2-widths-saturn.bin");
|
2015-03-25 17:35:12 +00:00
|
|
|
|
virtualWidths = new int[widths.Length / 2];
|
|
|
|
|
renderWidths = new int[widths.Length / 2];
|
2018-04-04 05:28:38 +01:00
|
|
|
|
virtualWidthsSaturn = new int[saturnWidths.Length / 2];
|
|
|
|
|
renderWidthsSaturn = new int[saturnWidths.Length / 2];
|
2015-03-25 17:35:12 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < widths.Length; i += 2)
|
|
|
|
|
{
|
|
|
|
|
virtualWidths[i / 2] = widths[i];
|
|
|
|
|
renderWidths[i / 2] = widths[i + 1];
|
2018-04-04 05:28:38 +01:00
|
|
|
|
virtualWidthsSaturn[i / 2] = saturnWidths[i];
|
|
|
|
|
renderWidthsSaturn[i / 2] = saturnWidths[i + 1];
|
2015-03-25 17:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public Compiler(IEnumerable<IControlCode> controlCodes,
|
|
|
|
|
Func<byte[], int, bool> controlCodePredicate)
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
ControlCodes = controlCodes;
|
|
|
|
|
ControlCodePredicate = controlCodePredicate;
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
AddressMap = new Dictionary<string, int>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsHexByte(string str)
|
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
try
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
Convert.ToByte(str, 16);
|
|
|
|
|
return true;
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
2015-03-22 23:55:34 +00:00
|
|
|
|
catch
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
return false;
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
2015-03-22 23:55:34 +00:00
|
|
|
|
}
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
private byte GetByte(char c, IDictionary<byte, string> charLookup)
|
|
|
|
|
{
|
|
|
|
|
return charLookup.First(kv => kv.Value[0] == c).Key; // lazy
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void ScanString(string str, ref int referenceAddress, IDictionary<byte, string> charLookup, bool scanCodesOnly)
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
ISet<IControlCode> codes;
|
|
|
|
|
IList<string> references;
|
|
|
|
|
ScanString(str, ref referenceAddress, charLookup, scanCodesOnly, out references, out codes);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void ScanString(string str, IDictionary<byte, string> charLookup, bool scanCodesOnly,
|
|
|
|
|
out IList<string> references)
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
int temp = 0;
|
|
|
|
|
ISet<IControlCode> codes;
|
|
|
|
|
ScanString(str, ref temp, charLookup, scanCodesOnly, out references, out codes);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void ScanString(string str, IDictionary<byte, string> charLookup, bool scanCodesOnly,
|
|
|
|
|
out ISet<IControlCode> codes)
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-20 21:40:43 +00:00
|
|
|
|
int temp = 0;
|
2015-03-22 23:55:34 +00:00
|
|
|
|
IList<string> references;
|
|
|
|
|
ScanString(str, ref temp, charLookup, scanCodesOnly, out references, out codes);
|
2015-03-20 21:40:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void ScanString(string str, IDictionary<byte, string> charLookup, bool scanCodesOnly,
|
|
|
|
|
out IList<string> references, out ISet<IControlCode> controlCodes)
|
2015-03-20 21:40:43 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
int temp = 0;
|
|
|
|
|
ScanString(str, ref temp, charLookup, scanCodesOnly, out references, out controlCodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ScanString(string str, ref int referenceAddress, IDictionary<byte, string> charLookup, bool scanCodesOnly,
|
|
|
|
|
out IList<string> references, out ISet<IControlCode> controlCodes)
|
|
|
|
|
{
|
|
|
|
|
references = new List<string>();
|
|
|
|
|
controlCodes = new HashSet<IControlCode>();
|
|
|
|
|
|
2015-03-16 04:16:12 +00:00
|
|
|
|
for (int i = 0; i < str.Length; )
|
|
|
|
|
{
|
|
|
|
|
if (str[i] == '[')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf(']', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Opening bracket has no matching closing bracket: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
string[] codeStrings = str.Substring(i + 1, str.IndexOf(']', i + 1) - i - 1)
|
|
|
|
|
.Split(' ');
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
IControlCode code = ControlCodes.FirstOrDefault(c => c.IsMatch(codeStrings));
|
|
|
|
|
if (!controlCodes.Contains(code))
|
|
|
|
|
{
|
|
|
|
|
controlCodes.Add(code);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 04:16:12 +00:00
|
|
|
|
foreach (var codeString in codeStrings)
|
|
|
|
|
{
|
|
|
|
|
if (codeString[0] == '_')
|
|
|
|
|
{
|
|
|
|
|
if (codeString[codeString.Length - 1] != '_')
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Reference has no closing underscore: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
if (codeString.Length <= 2)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Reference is empty: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (!scanCodesOnly)
|
|
|
|
|
referenceAddress += 4;
|
|
|
|
|
|
|
|
|
|
references.Add(codeString.Substring(1, codeString.Length - 2));
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
else if (IsHexByte(codeString))
|
|
|
|
|
{
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (!scanCodesOnly)
|
|
|
|
|
referenceAddress++;
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(String.Format(
|
|
|
|
|
"Encountered invalid code string at position {0}: {1}", i, codeString));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf(']', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (str[i] == ']')
|
|
|
|
|
{
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Closing bracket has no matching opening bracket: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
else if (str[i] == '^')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf('^', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Label has no matching closing caret: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
string label = str.Substring(i + 1, str.IndexOf('^', i + 1) - i - 1);
|
|
|
|
|
|
|
|
|
|
if (AddressMap.ContainsKey(label))
|
2017-02-24 20:38:46 +00:00
|
|
|
|
throw new Exception("Label " + label + " already defined: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
2015-03-25 17:35:12 +00:00
|
|
|
|
if (!scanCodesOnly)
|
2015-03-20 21:40:43 +00:00
|
|
|
|
AddressMap.Add(label, referenceAddress);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
i = str.IndexOf('^', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!(str[i] == '\r') && !(str[i] == '\n'))
|
|
|
|
|
{
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (!scanCodesOnly)
|
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
GetByte(str[i], charLookup); // just check if it's valid
|
2015-03-20 21:40:43 +00:00
|
|
|
|
referenceAddress++;
|
|
|
|
|
}
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void CompileString(string str, IList<byte> buffer, ref int referenceAddress, IDictionary<byte, string> charLookup)
|
2015-03-16 04:16:12 +00:00
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
CompileString(str, buffer, ref referenceAddress, charLookup, -1);
|
2015-03-18 16:31:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 23:55:34 +00:00
|
|
|
|
public void CompileString(string str, IList<byte> buffer, ref int referenceAddress, IDictionary<byte, string> charLookup, int padLength)
|
2015-03-18 16:31:42 +00:00
|
|
|
|
{
|
|
|
|
|
int previousBufferSize = buffer.Count;
|
|
|
|
|
|
2015-03-16 04:16:12 +00:00
|
|
|
|
for (int i = 0; i < str.Length; )
|
|
|
|
|
{
|
|
|
|
|
if (str[i] == '[')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf(']', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Opening bracket has no matching closing bracket: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
string[] codeStrings = str.Substring(i + 1, str.IndexOf(']', i + 1) - i - 1)
|
|
|
|
|
.Split(' ');
|
|
|
|
|
|
|
|
|
|
// Match the code
|
2015-03-22 23:55:34 +00:00
|
|
|
|
IControlCode code = ControlCodes.FirstOrDefault(c => c.IsMatch(codeStrings));
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
if (code == null)
|
|
|
|
|
{
|
|
|
|
|
// Direct copy
|
|
|
|
|
for (int j = 0; j < codeStrings.Length; j++)
|
|
|
|
|
{
|
|
|
|
|
if (!IsHexByte(codeStrings[j]))
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Code string for unrecognized control code block must be a byte literal: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
byte value = byte.Parse(codeStrings[j], System.Globalization.NumberStyles.HexNumber);
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (buffer != null)
|
|
|
|
|
buffer.Add(value);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
referenceAddress++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Validate
|
|
|
|
|
if (!code.IsValid(codeStrings))
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Invalid control code: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
// Parse
|
2015-03-22 23:55:34 +00:00
|
|
|
|
code.Compile(codeStrings, buffer, ref referenceAddress, AddressMap);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf(']', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (str[i] == ']')
|
|
|
|
|
{
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Closing bracket has no matching opening bracket: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
else if (str[i] == '^')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf('^', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Label has no matching closing caret: position " + i);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
|
|
|
|
i = str.IndexOf('^', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!(str[i] == '\r') && !(str[i] == '\n'))
|
|
|
|
|
{
|
2015-03-22 23:55:34 +00:00
|
|
|
|
byte value = GetByte(str[i], charLookup);
|
2015-03-16 04:16:12 +00:00
|
|
|
|
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (buffer != null)
|
|
|
|
|
buffer.Add(value);
|
2015-03-22 23:55:34 +00:00
|
|
|
|
|
2015-03-16 04:16:12 +00:00
|
|
|
|
referenceAddress++;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-18 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
// Pad the remaining bytes
|
|
|
|
|
if (padLength != -1)
|
|
|
|
|
{
|
|
|
|
|
int bytesWritten = buffer.Count - previousBufferSize;
|
2015-03-22 23:55:34 +00:00
|
|
|
|
|
2015-03-18 16:31:42 +00:00
|
|
|
|
if (bytesWritten > padLength)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Exceeded pad length: wrote " + bytesWritten +
|
|
|
|
|
" bytes, but the pad length is " + padLength + " bytes");
|
2015-03-18 16:31:42 +00:00
|
|
|
|
|
|
|
|
|
for (int i = bytesWritten; i < padLength; i++)
|
|
|
|
|
{
|
2015-03-20 21:40:43 +00:00
|
|
|
|
if (buffer != null)
|
|
|
|
|
buffer.Add(0);
|
2015-03-22 23:55:34 +00:00
|
|
|
|
|
2015-03-18 16:31:42 +00:00
|
|
|
|
referenceAddress++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
2015-03-21 20:47:30 +00:00
|
|
|
|
|
|
|
|
|
public string StripText(string str)
|
|
|
|
|
{
|
2015-03-30 17:49:51 +01:00
|
|
|
|
if (str == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-21 20:47:30 +00:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < str.Length; )
|
|
|
|
|
{
|
|
|
|
|
if (str[i] == '[')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf(']', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Opening bracket has no matching closing bracket: position " + i);
|
2015-03-21 20:47:30 +00:00
|
|
|
|
|
|
|
|
|
sb.Append(str.Substring(i, str.IndexOf(']', i + 1) - i + 1));
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf(']', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (str[i] == ']')
|
|
|
|
|
{
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Closing bracket has no matching opening bracket: position " + i);
|
2015-03-21 20:47:30 +00:00
|
|
|
|
}
|
|
|
|
|
else if (str[i] == '^')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf('^', i + 1) == -1)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
throw new Exception("Label has no matching closing caret: position " + i);
|
2015-03-21 20:47:30 +00:00
|
|
|
|
|
|
|
|
|
sb.Append(str.Substring(i, str.IndexOf('^', i + 1) - i + 1));
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf('^', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2015-03-25 17:35:12 +00:00
|
|
|
|
|
|
|
|
|
public IList<string> FormatPreviewM12(string str, out IList<int> widths, IDictionary<byte, string> charLookup)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
widths = new List<int>();
|
|
|
|
|
int currentWidth = 0;
|
2018-04-04 05:28:38 +01:00
|
|
|
|
bool useSaturnWidths = false;
|
2015-03-25 17:35:12 +00:00
|
|
|
|
|
|
|
|
|
var strings = new List<string>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < str.Length; )
|
|
|
|
|
{
|
|
|
|
|
if (str[i] == '[')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf(']', i + 1) == -1)
|
|
|
|
|
throw new Exception("Opening bracket has no matching closing bracket: position " + i);
|
|
|
|
|
|
|
|
|
|
string[] codeStrings = str.Substring(i + 1, str.IndexOf(']', i + 1) - i - 1)
|
|
|
|
|
.Split(' ');
|
|
|
|
|
|
|
|
|
|
M12ControlCode code = (M12ControlCode)ControlCodes.FirstOrDefault(c => c.IsMatch(codeStrings));
|
|
|
|
|
|
|
|
|
|
foreach (var codeString in codeStrings)
|
|
|
|
|
{
|
|
|
|
|
if (codeString[0] == '_')
|
|
|
|
|
{
|
|
|
|
|
if (codeString[codeString.Length - 1] != '_')
|
|
|
|
|
throw new Exception("Reference has no closing underscore: position " + i);
|
|
|
|
|
|
|
|
|
|
if (codeString.Length <= 2)
|
|
|
|
|
throw new Exception("Reference is empty: position " + i);
|
|
|
|
|
}
|
|
|
|
|
else if (!IsHexByte(codeString))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(String.Format(
|
|
|
|
|
"Encountered invalid code string at position {0}: {1}", i, codeString));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf(']', i + 1) + 1;
|
|
|
|
|
|
2015-03-25 19:07:04 +00:00
|
|
|
|
if (code == null)
|
2015-03-25 17:35:12 +00:00
|
|
|
|
{
|
2015-03-25 19:07:04 +00:00
|
|
|
|
// Not matched to anything -- check if it's a valid character sequence
|
|
|
|
|
foreach (var codeString in codeStrings)
|
|
|
|
|
{
|
|
|
|
|
if (!IsHexByte(codeString))
|
|
|
|
|
{
|
|
|
|
|
sb.Append("[INVALID]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte b = Convert.ToByte(codeString, 16);
|
|
|
|
|
if (!charLookup.ContainsKey(b))
|
|
|
|
|
{
|
|
|
|
|
sb.Append("[INVALID]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.Append(charLookup[b]);
|
|
|
|
|
currentWidth += virtualWidths[b - 0x50];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (code.Identifier)
|
|
|
|
|
{
|
|
|
|
|
case 0xC:
|
|
|
|
|
case 0xD:
|
|
|
|
|
case 0xE:
|
|
|
|
|
case 0xF:
|
|
|
|
|
case 0x10:
|
|
|
|
|
case 0x11:
|
|
|
|
|
case 0x12:
|
|
|
|
|
case 0x15:
|
|
|
|
|
case 0x2D:
|
|
|
|
|
case 0x9F:
|
|
|
|
|
case 0xAD:
|
2018-04-04 05:28:38 +01:00
|
|
|
|
// Name code
|
2015-03-25 19:07:04 +00:00
|
|
|
|
sb.Append("[NAME]");
|
|
|
|
|
currentWidth += 60;
|
|
|
|
|
break;
|
|
|
|
|
|
2018-04-04 05:28:38 +01:00
|
|
|
|
case 0x1A:
|
2018-04-04 05:53:57 +01:00
|
|
|
|
// Name (60/96) or item (80/??) or number (36/72).
|
2018-04-04 05:28:38 +01:00
|
|
|
|
sb.Append("[NAME]");
|
|
|
|
|
currentWidth += 80;
|
|
|
|
|
break;
|
|
|
|
|
|
2015-06-16 20:15:36 +01:00
|
|
|
|
case 0x0:
|
2015-03-25 19:07:04 +00:00
|
|
|
|
case 0x1:
|
|
|
|
|
case 0x2:
|
2015-06-16 20:15:36 +01:00
|
|
|
|
case 0x3:
|
2015-03-25 19:07:04 +00:00
|
|
|
|
// Line break
|
|
|
|
|
strings.Add(sb.ToString());
|
|
|
|
|
sb.Clear();
|
|
|
|
|
widths.Add(currentWidth);
|
|
|
|
|
currentWidth = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x20:
|
2018-04-04 05:28:38 +01:00
|
|
|
|
sb.Append("[SMAAAASH!!]");
|
2015-03-25 19:07:04 +00:00
|
|
|
|
currentWidth += 72;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x21:
|
2018-04-04 05:28:38 +01:00
|
|
|
|
sb.Append("[YOU WON!]");
|
2015-03-25 19:07:04 +00:00
|
|
|
|
currentWidth += 72;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x23:
|
|
|
|
|
case 0x63:
|
|
|
|
|
case 0xB7:
|
|
|
|
|
sb.Append("[MONEY]");
|
|
|
|
|
currentWidth += 36;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x24:
|
|
|
|
|
case 0x25:
|
|
|
|
|
case 0x26:
|
|
|
|
|
case 0x27:
|
|
|
|
|
case 0x28:
|
|
|
|
|
case 0x29:
|
|
|
|
|
case 0x2A:
|
|
|
|
|
case 0x2B:
|
|
|
|
|
sb.Append("[STAT]");
|
|
|
|
|
currentWidth += 18;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x1F:
|
|
|
|
|
sb.Append("_");
|
|
|
|
|
currentWidth += 10;
|
|
|
|
|
break;
|
2018-04-04 05:28:38 +01:00
|
|
|
|
|
|
|
|
|
case 0x7B:
|
|
|
|
|
useSaturnWidths = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0x7C:
|
|
|
|
|
useSaturnWidths = false;
|
|
|
|
|
break;
|
2015-03-25 19:07:04 +00:00
|
|
|
|
}
|
2015-03-25 17:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (str[i] == ']')
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Closing bracket has no matching opening bracket: position " + i);
|
|
|
|
|
}
|
|
|
|
|
else if (str[i] == '^')
|
|
|
|
|
{
|
|
|
|
|
if (str.IndexOf('^', i + 1) == -1)
|
|
|
|
|
throw new Exception("Label has no matching closing caret: position " + i);
|
|
|
|
|
|
|
|
|
|
string label = str.Substring(i + 1, str.IndexOf('^', i + 1) - i - 1);
|
|
|
|
|
|
|
|
|
|
i = str.IndexOf('^', i + 1) + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!(str[i] == '\r') && !(str[i] == '\n'))
|
|
|
|
|
{
|
|
|
|
|
sb.Append(str[i]);
|
2018-04-04 05:28:38 +01:00
|
|
|
|
int v = GetByte(str[i], charLookup) - 0x50;
|
|
|
|
|
currentWidth += useSaturnWidths ? virtualWidthsSaturn[v] : virtualWidths[v];
|
2015-03-25 17:35:12 +00:00
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
strings.Add(sb.ToString());
|
|
|
|
|
widths.Add(currentWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
|
}
|
2015-03-25 21:40:01 +00:00
|
|
|
|
|
|
|
|
|
public IControlCode GetLastControlCode(string str)
|
|
|
|
|
{
|
|
|
|
|
if (str.Length < 2)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!(str[str.Length - 1] == ']'))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
int lastOpenBracket = str.LastIndexOf('[');
|
|
|
|
|
|
|
|
|
|
if (lastOpenBracket < str.LastIndexOf(']', str.Length - 2))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var codeStrings = str.Substring(lastOpenBracket + 1, str.Length - lastOpenBracket - 2)
|
|
|
|
|
.Split(' ');
|
|
|
|
|
|
|
|
|
|
return ControlCodes.FirstOrDefault(c => c.IsMatch(codeStrings));
|
|
|
|
|
}
|
2015-03-16 04:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|