Experimental stuff for now. Don't think too much about it. Branched off Rabi-Ribi Autosplitter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rabiribi-display/rabi_splitter_WPF/MainWindow.xaml.cs

186 lines
5.9 KiB

8 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace rabi_splitter_WPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private MainContext mainContext;
8 years ago
private RabiRibiDisplay rabiRibiDisplay;
8 years ago
private DebugContext debugContext;
private VariableExportContext variableExportContext;
8 years ago
private static TcpClient tcpclient;
private static NetworkStream networkStream;
private readonly Regex titleReg = new Regex(@"ver.*?(\d+\.?\d+.*)$");
private readonly Thread memoryThread;
8 years ago
8 years ago
private void ReadMemory()
{
var processlist = Process.GetProcessesByName("rabiribi");
if (processlist.Length > 0)
{
Process process = processlist[0];
if (process.MainWindowTitle != mainContext.oldtitle)
{
var result = titleReg.Match(process.MainWindowTitle);
string rabiver;
if (result.Success)
{
8 years ago
rabiver = result.Groups[1].Value;
8 years ago
mainContext.veridx = Array.IndexOf(StaticData.VerNames, rabiver);
if (mainContext.veridx < 0)
{
8 years ago
mainContext.GameVer = rabiver + " Running (not supported)";
8 years ago
return;
}
}
else
{
mainContext.veridx = -1;
mainContext.GameVer = "Running (Unknown version)";
return;
}
mainContext.GameVer = rabiver + " Running";
mainContext.oldtitle = process.MainWindowTitle;
}
8 years ago
8 years ago
if (mainContext.veridx < 0) return;
8 years ago
rabiRibiDisplay.ReadMemory(process);
8 years ago
}
else
{
mainContext.oldtitle = "";
mainContext.GameVer = "Not Found";
mainContext.GameMusic = "N/A";
}
}
8 years ago
public void SendMessage(string message)
8 years ago
{
if (tcpclient != null && tcpclient.Connected)
{
try
{
var b = Encoding.UTF8.GetBytes(message);
8 years ago
networkStream.Write(b, 0, b.Length);
}
catch (Exception)
{
disconnect();
}
}
}
8 years ago
8 years ago
void disconnect()
{
tcpclient = null;
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
BtnConnect.IsEnabled = true;
}));
8 years ago
}
8 years ago
8 years ago
private void DebugLog(string log)
{
this.debugContext.Log(log);
8 years ago
}
public MainWindow()
{
InitializeComponent();
mainContext=new MainContext();
debugContext=new DebugContext();
variableExportContext = new VariableExportContext();
8 years ago
this.DataContext = mainContext;
DebugPanel.DataContext = debugContext;
8 years ago
this.Grid.ItemsSource = debugContext.BossList;
EntityDataPanel.DataContext = debugContext;
this.EntityStats.ItemsSource = debugContext.EntityStatsListView;
this.VariableExportTab.DataContext = variableExportContext;
this.VariableExportTab.Initialise(debugContext, variableExportContext);
8 years ago
BossEventDebug.DataContext = debugContext;
8 years ago
rabiRibiDisplay = new RabiRibiDisplay(mainContext, debugContext, this);
8 years ago
memoryThread = new Thread(() =>
{
while (true)
{
8 years ago
try
{
ReadMemory();
}
catch (Exception e)
{
DebugLog(e.ToString());
}
8 years ago
Thread.Sleep(1000 / 60);
}
});
memoryThread.IsBackground = true;
memoryThread.Start();
}
private void BtnConnect_Click(object sender, RoutedEventArgs e)
{
if (tcpclient != null && tcpclient.Connected) return;
try
{
tcpclient = new TcpClient("127.0.0.1", Convert.ToInt32(mainContext.ServerPort));
networkStream = tcpclient.GetStream();
this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
BtnConnect.IsEnabled = false;
}));
}
catch (Exception)
{
tcpclient = null;
networkStream = null;
MessageBox.Show(this, "Connect Failed");
}
}
private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
{
Process.Start("https://github.com/copyliu/rabiribi_splitter");
}
8 years ago
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var s = sender as TextBox;
if (s != null)
{
s.ScrollToEnd();
}
}
8 years ago
}
}