Files
SAPRecorderApp/SAPGui.cs
T
2024-01-05 15:10:10 +08:00

128 lines
3.8 KiB
C#

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using SAPFEWSELib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using System.Diagnostics;
namespace SAPRecorderApp;
public class SapGui
{
public GuiApplication Application { get; set; }
public GuiConnection Connection { get; set; }
public GuiSession Session { get; set; }
public TextBox textBox { get; set; }
public SapGui()
{
SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
if (SapGuilRot != null)
{
object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
Application = (GuiApplication)engine;
Connection = (GuiConnection)Application.Connections.ElementAt(0);
Session = (GuiSession)Connection.Sessions.ElementAt(0);
}
}
public void login(string desc, string username, string password)
{
Application.OpenConnection(desc);
Connection = (GuiConnection)Application.Connections.ElementAt(0);
Session = (GuiSession)Connection.Sessions.ElementAt(0);
GuiFrameWindow frame = (GuiFrameWindow)Session.FindById("wnd[0]");
frame.Maximize();
GuiTextField usernameTextField = (GuiTextField)Session.FindById("wnd[0]/usr/txtRSYST-BNAME");
usernameTextField.Text = username;
GuiTextField passwordTextField = (GuiTextField)Session.FindById("wnd[0]/usr/pwdRSYST-BCODE");
passwordTextField.Text = password;
frame.SendVKey(0);
}
public void BeginRecord()
{
Session.Change += Session_Change;
}
private void SetTextBox(string text)
{
textBox.Text = text;
}
private void Session_Change(GuiSession Session, GuiComponent Component, object CommandArray)
{
object[] objs = CommandArray as object[];
objs = objs[0] as object[];
var Action = "SetProperty";
switch (objs[0].ToString().ToLower())
{
case "m":
Action = "InvokeMethod";
break;
case "sp":
Action = "SetProperty";
break;
}
var ActionName = objs[1].ToString();
string id = Component.Id;
var pathToRoot = new List<GuiComponent>();
GuiComponent element = Component;
while (element != null)
{
// Program.log(element.Id);
if (element is GuiSession)
{
id = id.Substring(element.Id.Length + 1);
}
pathToRoot.Add(element);
element = element.Parent as GuiComponent;
}
var prefix = $"sap.session.findByid({id})";
if (Action == "InvokeMethod")
{
prefix += $".{ActionName}(";
var parameters = new List<string>();
if (objs.Length >= 3)
{
var array = objs[2] as object[];
if (array != null)
{
foreach (var item in array)
{
Console.WriteLine(item.ToString());
parameters.Add(item.ToString());
}
prefix += string.Join(",", parameters);
}
}
prefix += ")";
}
else if (Action == "SetProperty")
{
prefix += $".{ActionName} = \"{objs[2].ToString()}\"";
}
Console.WriteLine(prefix);
Globals.recordTextQueue.Add(prefix);
//SetTextBox(prefix + "\n");
}
}