2020-07-09 06:31:15 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Mods
|
|
|
|
{
|
|
|
|
class IPSwitchPatcher
|
|
|
|
{
|
|
|
|
const string BidHeader = "@nsobid-";
|
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
private enum Token
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
String,
|
|
|
|
EscapeChar,
|
|
|
|
Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly StreamReader _reader;
|
|
|
|
public string BuildId { get; }
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
public IPSwitchPatcher(StreamReader reader)
|
|
|
|
{
|
|
|
|
string header = reader.ReadLine();
|
|
|
|
if (header == null || !header.StartsWith(BidHeader))
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Error?.Print(LogClass.ModLoader, "IPSwitch: Malformed PCHTXT file. Skipping...");
|
2020-11-13 02:35:49 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reader = reader;
|
|
|
|
BuildId = header.Substring(BidHeader.Length).TrimEnd().TrimEnd('0');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uncomments line and unescapes C style strings within
|
|
|
|
private static string PreprocessLine(string line)
|
|
|
|
{
|
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
Token state = Token.Normal;
|
|
|
|
|
|
|
|
for (int i = 0; i < line.Length; ++i)
|
|
|
|
{
|
|
|
|
char c = line[i];
|
|
|
|
char la = i + 1 != line.Length ? line[i + 1] : '\0';
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case Token.Normal:
|
|
|
|
state = c == '"' ? Token.String :
|
|
|
|
c == '/' && la == '/' ? Token.Comment :
|
|
|
|
c == '/' && la != '/' ? Token.Comment : // Ignore error and stop parsing
|
|
|
|
Token.Normal;
|
|
|
|
break;
|
|
|
|
case Token.String:
|
|
|
|
state = c switch
|
|
|
|
{
|
2021-09-15 01:24:49 +02:00
|
|
|
'"' => Token.Normal,
|
2020-07-09 06:31:15 +02:00
|
|
|
'\\' => Token.EscapeChar,
|
2021-09-15 01:24:49 +02:00
|
|
|
_ => Token.String
|
2020-07-09 06:31:15 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case Token.EscapeChar:
|
|
|
|
state = Token.String;
|
|
|
|
c = c switch
|
|
|
|
{
|
2021-09-15 01:24:49 +02:00
|
|
|
'a' => '\a',
|
|
|
|
'b' => '\b',
|
|
|
|
'f' => '\f',
|
|
|
|
'n' => '\n',
|
|
|
|
'r' => '\r',
|
|
|
|
't' => '\t',
|
|
|
|
'v' => '\v',
|
2020-07-09 06:31:15 +02:00
|
|
|
'\\' => '\\',
|
2021-09-15 01:24:49 +02:00
|
|
|
_ => '?'
|
2020-07-09 06:31:15 +02:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == Token.Comment) break;
|
|
|
|
|
|
|
|
if (state < Token.EscapeChar)
|
|
|
|
{
|
|
|
|
str.Append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.ToString().Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ParseHexByte(byte c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
{
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
{
|
|
|
|
return c - 'A' + 10;
|
|
|
|
}
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
{
|
|
|
|
return c - 'a' + 10;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Big Endian
|
|
|
|
static byte[] Hex2ByteArrayBE(string hexstr)
|
|
|
|
{
|
|
|
|
if ((hexstr.Length & 1) == 1) return null;
|
|
|
|
|
|
|
|
byte[] bytes = new byte[hexstr.Length >> 1];
|
|
|
|
|
|
|
|
for (int i = 0; i < hexstr.Length; i += 2)
|
|
|
|
{
|
|
|
|
int high = ParseHexByte((byte)hexstr[i]);
|
2021-09-15 01:24:49 +02:00
|
|
|
int low = ParseHexByte((byte)hexstr[i + 1]);
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
bytes[i >> 1] = (byte)((high << 4) | low);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto base discovery
|
|
|
|
private static bool ParseInt(string str, out int value)
|
|
|
|
{
|
|
|
|
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
|
|
|
{
|
2021-09-15 01:24:49 +02:00
|
|
|
return int.TryParse(str.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value);
|
2020-07-09 06:31:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 01:24:49 +02:00
|
|
|
return int.TryParse(str, System.Globalization.NumberStyles.Integer, null, out value);
|
2020-07-09 06:31:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private MemPatch Parse()
|
|
|
|
{
|
|
|
|
if (_reader == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemPatch patches = new MemPatch();
|
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
bool enabled = false;
|
2020-07-09 06:31:15 +02:00
|
|
|
bool printValues = false;
|
|
|
|
int offset_shift = 0;
|
|
|
|
|
|
|
|
string line;
|
|
|
|
int lineNum = 0;
|
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
static void Print(string s) => Logger.Info?.Print(LogClass.ModLoader, $"IPSwitch: {s}");
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
void ParseWarn() => Logger.Warning?.Print(LogClass.ModLoader, $"IPSwitch: Parse error at line {lineNum} for bid={BuildId}");
|
2020-07-09 06:31:15 +02:00
|
|
|
|
|
|
|
while ((line = _reader.ReadLine()) != null)
|
|
|
|
{
|
2020-11-13 02:35:49 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
line = PreprocessLine(line);
|
|
|
|
lineNum += 1;
|
|
|
|
|
|
|
|
if (line.Length == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (line.StartsWith('#'))
|
|
|
|
{
|
|
|
|
Print(line);
|
|
|
|
}
|
|
|
|
else if (line.StartsWith("@stop"))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (line.StartsWith("@enabled"))
|
|
|
|
{
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
else if (line.StartsWith("@disabled"))
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
else if (line.StartsWith("@flag"))
|
|
|
|
{
|
|
|
|
var tokens = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
if (tokens.Length < 2)
|
|
|
|
{
|
|
|
|
ParseWarn();
|
2020-11-13 02:35:49 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens[1] == "offset_shift")
|
|
|
|
{
|
|
|
|
if (tokens.Length != 3 || !ParseInt(tokens[2], out offset_shift))
|
|
|
|
{
|
|
|
|
ParseWarn();
|
2020-11-13 02:35:49 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tokens[1] == "print_values")
|
|
|
|
{
|
|
|
|
printValues = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (line.StartsWith('@'))
|
|
|
|
{
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokens = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
if (tokens.Length < 2)
|
|
|
|
{
|
|
|
|
ParseWarn();
|
2020-11-13 02:35:49 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
if (!int.TryParse(tokens[0], System.Globalization.NumberStyles.HexNumber, null, out int offset))
|
2020-07-09 06:31:15 +02:00
|
|
|
{
|
|
|
|
ParseWarn();
|
2020-11-13 02:35:49 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += offset_shift;
|
|
|
|
|
|
|
|
if (printValues)
|
|
|
|
{
|
|
|
|
Print($"print_values 0x{offset:x} <= {tokens[1]}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens[1][0] == '"')
|
|
|
|
{
|
|
|
|
var patch = Encoding.ASCII.GetBytes(tokens[1].Trim('"'));
|
|
|
|
patches.Add((uint)offset, patch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var patch = Hex2ByteArrayBE(tokens[1]);
|
|
|
|
patches.Add((uint)offset, patch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return patches;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddPatches(MemPatch patches)
|
|
|
|
{
|
|
|
|
patches.AddFrom(Parse());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|