2020-08-03 03:30:58 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2021-01-11 19:27:55 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets
|
|
|
|
|
{
|
|
|
|
|
internal class SoftwareKeyboardApplet : IApplet
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private const string DefaultText = "Ryujinx";
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private readonly Switch _device;
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private const int StandardBufferSize = 0x7D8;
|
|
|
|
|
private const int InteractiveBufferSize = 0x7D4;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
private SoftwareKeyboardState _state = SoftwareKeyboardState.Uninitialized;
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private bool _isBackground = false;
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
private AppletSession _normalSession;
|
|
|
|
|
private AppletSession _interactiveSession;
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// Configuration for foreground mode
|
|
|
|
|
private SoftwareKeyboardConfig _keyboardFgConfig;
|
|
|
|
|
private SoftwareKeyboardCalc _keyboardCalc;
|
|
|
|
|
private SoftwareKeyboardDictSet _keyboardDict;
|
|
|
|
|
|
|
|
|
|
// Configuration for background mode
|
|
|
|
|
private SoftwareKeyboardInitialize _keyboardBgInitialize;
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private byte[] _transferMemory;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
private string _textValue = null;
|
|
|
|
|
private bool _okPressed = false;
|
2019-12-03 05:00:56 +01:00
|
|
|
|
private Encoding _encoding = Encoding.Unicode;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
public event EventHandler AppletStateChanged;
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
public SoftwareKeyboardApplet(Horizon system)
|
|
|
|
|
{
|
|
|
|
|
_device = system.Device;
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
public ResultCode Start(AppletSession normalSession,
|
|
|
|
|
AppletSession interactiveSession)
|
|
|
|
|
{
|
|
|
|
|
_normalSession = normalSession;
|
|
|
|
|
_interactiveSession = interactiveSession;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.DataAvailable += OnInteractiveData;
|
|
|
|
|
|
|
|
|
|
var launchParams = _normalSession.Pop();
|
|
|
|
|
var keyboardConfig = _normalSession.Pop();
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// TODO: A better way would be handling the background creation properly
|
|
|
|
|
// in LibraryAppleCreator / Acessor instead of guessing by size.
|
|
|
|
|
if (keyboardConfig.Length == Marshal.SizeOf<SoftwareKeyboardInitialize>())
|
2020-08-03 03:30:58 +02:00
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
_isBackground = true;
|
|
|
|
|
|
|
|
|
|
_keyboardBgInitialize = ReadStruct<SoftwareKeyboardInitialize>(keyboardConfig);
|
|
|
|
|
_state = SoftwareKeyboardState.Uninitialized;
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
_isBackground = false;
|
2020-08-03 03:30:58 +02:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (keyboardConfig.Length < Marshal.SizeOf<SoftwareKeyboardConfig>())
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"SoftwareKeyboardConfig size mismatch. Expected {Marshal.SizeOf<SoftwareKeyboardConfig>():x}. Got {keyboardConfig.Length:x}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_keyboardFgConfig = ReadStruct<SoftwareKeyboardConfig>(keyboardConfig);
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (!_normalSession.TryPop(out _transferMemory))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, "SwKbd Transfer Memory is null");
|
|
|
|
|
}
|
2019-12-03 05:00:56 +01:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_keyboardFgConfig.UseUtf8)
|
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
_state = SoftwareKeyboardState.Ready;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
ExecuteForegroundKeyboard();
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultCode GetResult()
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private void ExecuteForegroundKeyboard()
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2020-08-03 03:30:58 +02:00
|
|
|
|
string initialText = null;
|
|
|
|
|
|
|
|
|
|
// Initial Text is always encoded as a UTF-16 string in the work buffer (passed as transfer memory)
|
|
|
|
|
// InitialStringOffset points to the memory offset and InitialStringLength is the number of UTF-16 characters
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_transferMemory != null && _keyboardFgConfig.InitialStringLength > 0)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
initialText = Encoding.Unicode.GetString(_transferMemory, _keyboardFgConfig.InitialStringOffset, 2 * _keyboardFgConfig.InitialStringLength);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the max string length is 0, we set it to a large default
|
|
|
|
|
// length.
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_keyboardFgConfig.StringLengthMax == 0)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
_keyboardFgConfig.StringLengthMax = 100;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 03:30:58 +02:00
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
HeaderText = _keyboardFgConfig.HeaderText,
|
|
|
|
|
SubtitleText = _keyboardFgConfig.SubtitleText,
|
|
|
|
|
GuideText = _keyboardFgConfig.GuideText,
|
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardFgConfig.SubmitText) ? _keyboardFgConfig.SubmitText : "OK"),
|
|
|
|
|
StringLengthMin = _keyboardFgConfig.StringLengthMin,
|
|
|
|
|
StringLengthMax = _keyboardFgConfig.StringLengthMax,
|
2020-08-03 03:30:58 +02:00
|
|
|
|
InitialText = initialText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Call the configured GUI handler to get user's input
|
|
|
|
|
if (_device.UiHandler == null)
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
|
2020-08-03 03:30:58 +02:00
|
|
|
|
_okPressed = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_okPressed = _device.UiHandler.DisplayInputDialog(args, out _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_textValue ??= initialText ?? DefaultText;
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// If the game requests a string with a minimum length less
|
|
|
|
|
// than our default text, repeat our default text until we meet
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// the minimum length requirement.
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// This should always be done before the text truncation step.
|
2021-01-11 19:27:55 +01:00
|
|
|
|
while (_textValue.Length < _keyboardFgConfig.StringLengthMin)
|
2019-12-03 05:00:56 +01:00
|
|
|
|
{
|
|
|
|
|
_textValue = String.Join(" ", _textValue, _textValue);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
// If our default text is longer than the allowed length,
|
|
|
|
|
// we truncate it.
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_textValue.Length > _keyboardFgConfig.StringLengthMax)
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
_textValue = _textValue.Substring(0, (int)_keyboardFgConfig.StringLengthMax);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 05:00:56 +01:00
|
|
|
|
// Does the application want to validate the text itself?
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_keyboardFgConfig.CheckText)
|
2019-12-03 05:00:56 +01:00
|
|
|
|
{
|
|
|
|
|
// The application needs to validate the response, so we
|
|
|
|
|
// submit it to the interactive output buffer, and poll it
|
|
|
|
|
// for validation. Once validated, the application will submit
|
|
|
|
|
// back a validation status, which is handled in OnInteractiveDataPushIn.
|
|
|
|
|
_state = SoftwareKeyboardState.ValidationPending;
|
|
|
|
|
|
|
|
|
|
_interactiveSession.Push(BuildResponse(_textValue, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-11-18 12:16:26 +01:00
|
|
|
|
{
|
|
|
|
|
// If the application doesn't need to validate the response,
|
|
|
|
|
// we push the data to the non-interactive output buffer
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// and poll it for completion.
|
2019-11-18 12:16:26 +01:00
|
|
|
|
_state = SoftwareKeyboardState.Complete;
|
|
|
|
|
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInteractiveData(object sender, EventArgs e)
|
|
|
|
|
{
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// Obtain the validation status response.
|
2019-11-18 12:16:26 +01:00
|
|
|
|
var data = _interactiveSession.Pop();
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
if (_isBackground)
|
|
|
|
|
{
|
|
|
|
|
OnBackgroundInteractiveData(data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnForegroundInteractiveData(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnForegroundInteractiveData(byte[] data)
|
|
|
|
|
{
|
2019-11-18 12:16:26 +01:00
|
|
|
|
if (_state == SoftwareKeyboardState.ValidationPending)
|
|
|
|
|
{
|
|
|
|
|
// TODO(jduncantor):
|
|
|
|
|
// If application rejects our "attempt", submit another attempt,
|
|
|
|
|
// and put the applet back in PendingValidation state.
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
// For now we assume success, so we push the final result
|
2019-11-18 12:16:26 +01:00
|
|
|
|
// to the standard output buffer and carry on our merry way.
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
|
|
|
|
|
_state = SoftwareKeyboardState.Complete;
|
|
|
|
|
}
|
|
|
|
|
else if(_state == SoftwareKeyboardState.Complete)
|
|
|
|
|
{
|
|
|
|
|
// If we have already completed, we push the result text
|
|
|
|
|
// back on the output buffer and poll the application.
|
|
|
|
|
_normalSession.Push(BuildResponse(_textValue, false));
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// We shouldn't be able to get here through standard swkbd execution.
|
|
|
|
|
throw new InvalidOperationException("Software Keyboard is in an invalid state.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 19:27:55 +01:00
|
|
|
|
private void OnBackgroundInteractiveData(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
// WARNING: Only invoke applet state changes after an explicit finalization
|
|
|
|
|
// request from the game, this is because the inline keyboard is expected to
|
|
|
|
|
// keep running in the background sending data by itself.
|
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(data))
|
|
|
|
|
using (BinaryReader reader = new BinaryReader(stream))
|
|
|
|
|
{
|
|
|
|
|
var request = (InlineKeyboardRequest)reader.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
long remaining;
|
|
|
|
|
|
|
|
|
|
// Always show the keyboard if the state is 'Ready'.
|
|
|
|
|
bool showKeyboard = _state == SoftwareKeyboardState.Ready;
|
|
|
|
|
|
|
|
|
|
switch (request)
|
|
|
|
|
{
|
|
|
|
|
case InlineKeyboardRequest.Unknown0: // Unknown request sent by some games after calc
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.UseChangedStringV2:
|
|
|
|
|
// Not used because we only send the entire string after confirmation.
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.UseMovedCursorV2:
|
|
|
|
|
// Not used because we only send the entire string after confirmation.
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.SetCustomizeDic:
|
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining != Marshal.SizeOf<SoftwareKeyboardDictSet>())
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard DictSet of {remaining} bytes!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keyboardDictData = reader.ReadBytes((int)remaining);
|
|
|
|
|
_keyboardDict = ReadStruct<SoftwareKeyboardDictSet>(keyboardDictData);
|
|
|
|
|
}
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Calc:
|
|
|
|
|
// Put the keyboard in a Ready state, this will force showing
|
|
|
|
|
_state = SoftwareKeyboardState.Ready;
|
|
|
|
|
remaining = stream.Length - stream.Position;
|
|
|
|
|
if (remaining != Marshal.SizeOf<SoftwareKeyboardCalc>())
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"Received invalid Software Keyboard Calc of {remaining} bytes!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var keyboardCalcData = reader.ReadBytes((int)remaining);
|
|
|
|
|
_keyboardCalc = ReadStruct<SoftwareKeyboardCalc>(keyboardCalcData);
|
|
|
|
|
|
|
|
|
|
if (_keyboardCalc.Utf8Mode == 0x1)
|
|
|
|
|
{
|
|
|
|
|
_encoding = Encoding.UTF8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Force showing the keyboard regardless of the state, an unwanted
|
|
|
|
|
// input dialog may show, but it is better than a soft lock.
|
|
|
|
|
if (_keyboardCalc.Appear.ShouldBeHidden == 0)
|
|
|
|
|
{
|
|
|
|
|
showKeyboard = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Send an initialization finished signal.
|
|
|
|
|
_interactiveSession.Push(InlineResponses.FinishedInitialize());
|
|
|
|
|
// Start a task with the GUI handler to get user's input.
|
|
|
|
|
new Task(() =>
|
|
|
|
|
{
|
|
|
|
|
bool submit = true;
|
|
|
|
|
string inputText = (!string.IsNullOrWhiteSpace(_keyboardCalc.InputText) ? _keyboardCalc.InputText : DefaultText);
|
|
|
|
|
|
|
|
|
|
// Call the configured GUI handler to get user's input.
|
|
|
|
|
if (!showKeyboard)
|
|
|
|
|
{
|
|
|
|
|
// Submit the default text to avoid soft locking if the keyboard was ignored by
|
|
|
|
|
// accident. It's better to change the name than being locked out of the game.
|
|
|
|
|
submit = true;
|
|
|
|
|
inputText = DefaultText;
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.Application, "Received a dummy Calc, keyboard will not be shown");
|
|
|
|
|
}
|
|
|
|
|
else if (_device.UiHandler == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning?.Print(LogClass.Application, "GUI Handler is not set. Falling back to default");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var args = new SoftwareKeyboardUiArgs
|
|
|
|
|
{
|
|
|
|
|
HeaderText = "", // The inline keyboard lacks these texts
|
|
|
|
|
SubtitleText = "",
|
|
|
|
|
GuideText = "",
|
|
|
|
|
SubmitText = (!string.IsNullOrWhiteSpace(_keyboardCalc.Appear.OkText) ? _keyboardCalc.Appear.OkText : "OK"),
|
|
|
|
|
StringLengthMin = 0,
|
|
|
|
|
StringLengthMax = 100,
|
|
|
|
|
InitialText = inputText
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submit = _device.UiHandler.DisplayInputDialog(args, out inputText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (submit)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard OK...");
|
|
|
|
|
|
|
|
|
|
if (_encoding == Encoding.UTF8)
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedEnterUtf8(inputText));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedEnter(inputText));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Sending keyboard Cancel...");
|
|
|
|
|
_interactiveSession.Push(InlineResponses.DecidedCancel());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Why is this necessary? Does the software expect a constant stream of responses?
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
|
|
|
|
|
Logger.Debug?.Print(LogClass.ServiceAm, "Resetting state of the keyboard...");
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
}).Start();
|
|
|
|
|
break;
|
|
|
|
|
case InlineKeyboardRequest.Finalize:
|
|
|
|
|
// The game wants to close the keyboard applet and will wait for a state change.
|
|
|
|
|
_state = SoftwareKeyboardState.Uninitialized;
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// We shouldn't be able to get here through standard swkbd execution.
|
|
|
|
|
Logger.Error?.Print(LogClass.ServiceAm, $"Invalid Software Keyboard request {request} during state {_state}!");
|
|
|
|
|
_interactiveSession.Push(InlineResponses.Default());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 12:16:26 +01:00
|
|
|
|
private byte[] BuildResponse(string text, bool interactive)
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01:00
|
|
|
|
int bufferSize = interactive ? InteractiveBufferSize : StandardBufferSize;
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(new byte[bufferSize]))
|
|
|
|
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
|
|
|
|
{
|
2019-12-03 05:00:56 +01:00
|
|
|
|
byte[] output = _encoding.GetBytes(text);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
|
|
|
|
|
if (!interactive)
|
|
|
|
|
{
|
|
|
|
|
// Result Code
|
2020-08-03 03:30:58 +02:00
|
|
|
|
writer.Write(_okPressed ? 0U : 1U);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-26 06:14:21 +01:00
|
|
|
|
// In interactive mode, we write the length of the text as a long, rather than
|
|
|
|
|
// a result code. This field is inclusive of the 64-bit size.
|
|
|
|
|
writer.Write((long)output.Length + 8);
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.Write(output);
|
|
|
|
|
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-03 08:53:06 +02:00
|
|
|
|
|
|
|
|
|
private static T ReadStruct<T>(byte[] data)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
|
|
|
|
|
|
try
|
2021-01-11 19:27:55 +01:00
|
|
|
|
{
|
2020-04-03 08:53:06 +02:00
|
|
|
|
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|