diff --git a/rabi_splitter_WPF/MainContext.cs b/rabi_splitter_WPF/MainContext.cs index cc91b16..388592b 100644 --- a/rabi_splitter_WPF/MainContext.cs +++ b/rabi_splitter_WPF/MainContext.cs @@ -120,6 +120,11 @@ namespace rabi_splitter_WPF } } + public void Log(string message) + { + this.DebugLog += message + "\n"; + } + public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] @@ -130,145 +135,196 @@ namespace rabi_splitter_WPF } class MainContext : INotifyPropertyChanged - { - private bool _musicStart; - private bool _musicEnd; - private bool _computer; - private bool _miruDe; - private bool _sideCh; - private bool _aliusI; - private bool _tm2; - private bool _irisu1; - private bool _dontSplitOnReload; - private bool _debugArea; + public string oldtitle; + public int veridx; + private int _serverPort; private string _gameVer; private string _gameMusic; private bool _igt; - public bool _autoReset; - public bool _autoStart; - public bool Noah1Reload + + private string _text1; + private string _text2; + private string _text3; + private string _text4; + private string _text5; + private string _text6; + private string _text7; + private string _text8; + private string _text9; + private string _text10; + private string _text11; + private string _text12; + private string _text13; + private string _text14; + private string _text15; + + public string Text1 + { + get { return _text1; } + set + { + if (value == _text1) return; + _gameVer = value; + OnPropertyChanged(nameof(Text1)); + } + } + + public string Text2 + { + get { return _text2; } + set + { + if (value == _text2) return; + _gameVer = value; + OnPropertyChanged(nameof(Text2)); + } + } + + public string Text3 { - get { return _noah1Reload; } + get { return _text3; } set { - if (value == _noah1Reload) return; - _noah1Reload = value; - OnPropertyChanged(nameof(Noah1Reload)); + if (value == _text3) return; + _gameVer = value; + OnPropertyChanged(nameof(Text3)); } } - public bool MusicStart + public string Text4 { - get { return _musicStart; } + get { return _text4; } set { - if (value == _musicStart) return; - _musicStart = value; - OnPropertyChanged(nameof(MusicStart)); + if (value == _text4) return; + _gameVer = value; + OnPropertyChanged(nameof(Text4)); } } - public bool MusicEnd + public string Text5 { - get { return _musicEnd; } + get { return _text5; } set { - if (value == _musicEnd) return; - _musicEnd = value; - OnPropertyChanged(nameof(MusicEnd)); + if (value == _text5) return; + _gameVer = value; + OnPropertyChanged(nameof(Text5)); } } - public bool Computer + public string Text6 { - get { return _computer; } + get { return _text6; } set { - if (value == _computer) return; - _computer = value; - OnPropertyChanged(nameof(Computer)); + if (value == _text6) return; + _gameVer = value; + OnPropertyChanged(nameof(Text6)); } } - public bool MiruDe + public string Text7 { - get { return _miruDe; } + get { return _text7; } set { - if (value == _miruDe) return; - _miruDe = value; - OnPropertyChanged(nameof(MiruDe)); + if (value == _text7) return; + _gameVer = value; + OnPropertyChanged(nameof(Text7)); + } + } + + public string Text8 + { + get { return _text8; } + set + { + if (value == _text8) return; + _gameVer = value; + OnPropertyChanged(nameof(Text8)); } } - public bool SideCh + public string Text9 { - get { return _sideCh; } + get { return _text9; } set { - if (value == _sideCh) return; - _sideCh = value; - OnPropertyChanged(nameof(SideCh)); + if (value == _text9) return; + _gameVer = value; + OnPropertyChanged(nameof(Text9)); } } - public bool AliusI + public string Text10 { - get { return _aliusI; } + get { return _text10; } set { - if (value == _aliusI) return; - _aliusI = value; - OnPropertyChanged(nameof(AliusI)); + if (value == _text10) return; + _gameVer = value; + OnPropertyChanged(nameof(Text10)); } } - public bool Tm2 + public string Text11 { - get { return _tm2; } + get { return _text11; } set { - if (value == _tm2) return; - _tm2 = value; - OnPropertyChanged(nameof(Tm2)); + if (value == _text11) return; + _gameVer = value; + OnPropertyChanged(nameof(Text11)); } } - public bool Irisu1 + public string Text12 { - get { return _irisu1; } + get { return _text12; } set { - if (value == _irisu1) return; - _irisu1 = value; - OnPropertyChanged(nameof(Irisu1)); + if (value == _text12) return; + _gameVer = value; + OnPropertyChanged(nameof(Text12)); } } - public bool DontSplitOnReload + public string Text13 { - get { return _dontSplitOnReload; } + get { return _text13; } set { - if (value == _dontSplitOnReload) return; - _dontSplitOnReload = value; - OnPropertyChanged(nameof(DontSplitOnReload)); + if (value == _text13) return; + _gameVer = value; + OnPropertyChanged(nameof(Text13)); } } - public bool DebugArea + public string Text14 { - get { return _debugArea; } + get { return _text14; } set { - if (value == _debugArea) return; - _debugArea = value; - OnPropertyChanged(nameof(DebugArea)); + if (value == _text14) return; + _gameVer = value; + OnPropertyChanged(nameof(Text14)); } } + public string Text15 + { + get { return _text15; } + set + { + if (value == _text15) return; + _gameVer = value; + OnPropertyChanged(nameof(Text15)); + } + } + public int ServerPort { get { return _serverPort; } @@ -312,63 +368,11 @@ namespace rabi_splitter_WPF OnPropertyChanged(nameof(Igt)); } } - - public bool AutoStart - { - get { return _autoStart; } - set - { - if (value == _autoStart) return; - _autoStart = value; - OnPropertyChanged(nameof(AutoStart)); - } - } - - public bool AutoReset - { - get { return _autoReset; } - set - { - if (value == _autoReset) return; - _autoReset = value; - OnPropertyChanged(nameof(AutoReset)); - - } - } - - public int previousBlackness = -1; - public string oldtitle; - public int veridx; - public int lastmoney; - public int lastmapid; - public int lastmusicid; - public int lastplaytime = 0; - public bool bossbattle; - public List lastbosslist; - public int lastnoah3hp; - public int lastTM; - public DateTime LastTMAddTime; - private bool _noah1Reload; - + public MainContext() { - this.MusicEnd = true; - this.MusicStart = false; - this.Computer = true; - this.MiruDe = true; - this.SideCh = true; - this.AliusI = true; - this.Tm2 = true; - this.Irisu1 = true; - this.DontSplitOnReload = false; - this.DebugArea = false; this.ServerPort = 16834; this.Igt = true; - this.Noah1Reload = false; - this.AutoStart = false; - this.AutoReset = true; - - } public event PropertyChangedEventHandler PropertyChanged; @@ -378,9 +382,6 @@ namespace rabi_splitter_WPF { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - - - } } diff --git a/rabi_splitter_WPF/MainWindow.xaml b/rabi_splitter_WPF/MainWindow.xaml index 17991a3..92fc22a 100644 --- a/rabi_splitter_WPF/MainWindow.xaml +++ b/rabi_splitter_WPF/MainWindow.xaml @@ -16,7 +16,7 @@ - + @@ -26,23 +26,43 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/rabi_splitter_WPF/MainWindow.xaml.cs b/rabi_splitter_WPF/MainWindow.xaml.cs index 09fc12a..93a125e 100644 --- a/rabi_splitter_WPF/MainWindow.xaml.cs +++ b/rabi_splitter_WPF/MainWindow.xaml.cs @@ -25,18 +25,18 @@ namespace rabi_splitter_WPF public partial class MainWindow : Window { private MainContext mainContext; + private RabiRibiDisplay rabiRibiDisplay; private DebugContext debugContext; private static TcpClient tcpclient; private static NetworkStream networkStream; private readonly Regex titleReg = new Regex(@"ver.*?(\d+\.?\d+.*)$"); private readonly Thread memoryThread; + private void ReadMemory() { - var processlist = Process.GetProcessesByName("rabiribi"); if (processlist.Length > 0) { - Process process = processlist[0]; if (process.MainWindowTitle != mainContext.oldtitle) { @@ -44,393 +44,37 @@ namespace rabi_splitter_WPF string rabiver; if (result.Success) { - - rabiver = result.Groups[1].Value; + rabiver = result.Groups[1].Value; mainContext.veridx = Array.IndexOf(StaticData.VerNames, rabiver); if (mainContext.veridx < 0) { - mainContext.GameVer = rabiver + " Running (not support)"; - + mainContext.GameVer = rabiver + " Running (not supported)"; return; } - - - } else { mainContext.veridx = -1; mainContext.GameVer = "Running (Unknown version)"; - return; } mainContext.GameVer = rabiver + " Running"; mainContext.oldtitle = process.MainWindowTitle; - } - - + if (mainContext.veridx < 0) return; - - #region read igt - - int igt = MemoryHelper.GetMemoryValue(process, StaticData.IGTAddr[mainContext.veridx]); - if (igt > 0 && mainContext.Igt) - { - sendigt((float)igt / 60); - } - - #endregion - - #region Detect Reload - - bool reloaded = false; - { - int playtime = MemoryHelper.GetMemoryValue(process, StaticData.PlaytimeAddr[mainContext.veridx]); - reloaded = playtime != 0 && playtime < mainContext.lastplaytime; - if (reloaded) DebugLog("Reload Game!"); - mainContext.lastplaytime = playtime; - } - - #endregion - - #region Detect Start Game - - { - int blackness = MemoryHelper.GetMemoryValue(process, StaticData.BlacknessAddr[mainContext.veridx]); - if (mainContext.previousBlackness == 0 && blackness >= 100000) - { - // Sudden increase by 100000 - // Have to be careful, though. I don't know whether anything else causes blackness to increase by 100000 - if (mainContext.AutoStart) sendstarttimer(); - DebugLog("Start Game!"); - } - mainContext.previousBlackness = blackness; - } - - #endregion - - #region CheckMoney - - if (mainContext.Computer) - { - var newmoney = MemoryHelper.GetMemoryValue(process, StaticData.MoneyAddress[mainContext.veridx]); - if (newmoney - mainContext.lastmoney == 17500) - { - sendsplit(); - DebugLog("get 17500 en, split"); - } - mainContext.lastmoney = newmoney; - } - - #endregion - - int mapid = MemoryHelper.GetMemoryValue(process, StaticData.MapAddress[mainContext.veridx]); - if (mainContext.lastmapid != mapid) - { - DebugLog("newmap: " + mapid + ":" + StaticData.MapNames[mapid]); - mainContext.lastmapid = mapid; - } - - - #region checkTM - - - - #endregion - - #region Music - - int musicaddr = StaticData.MusicAddr[mainContext.veridx]; - int musicid = MemoryHelper.GetMemoryValue(process, musicaddr); - if (musicid > 0 && musicid < StaticData.MusicNames.Length) - { - if (mainContext.lastmusicid != musicid) - { - DebugLog("new music:" + musicid + ":" + StaticData.MusicNames[musicid]); - mainContext.GameMusic = StaticData.MusicNames[musicid]; - - if ((musicid == 45 || musicid == 46 || musicid == 53) && mainContext.AutoReset) - { - DebugLog("Title music, reset"); - //reset - sendreset(); - mainContext.AliusI = true; - mainContext.Noah1Reload = false; - mainContext.bossbattle = false; - } - - else - { - var bossmusicflag = StaticData.BossMusics.Contains(musicid); - if (bossmusicflag) - { - if (mainContext.bossbattle) - { - if (mainContext.Noah1Reload && (mainContext.lastmusicid == 52 || musicid == 52)) - { - DebugLog("noah 1 reload? ignore"); - } - else - { - if (mainContext.MusicStart || mainContext.MusicEnd) - { - sendsplit(); - DebugLog("new boss music, split"); - - } - if (musicid == 37) - { - mainContext.Noah1Reload = true; - DebugLog("noah1 music start, ignore MR forever"); - } - } - - mainContext.lastmusicid = musicid; - return; - } - } - if (!mainContext.bossbattle) - { - - if (musicid == 54 && mainContext.AliusI) - { - mainContext.bossbattle = false; - mainContext.AliusI = false; - DebugLog("Alius music, ignore once"); - - } - else if (musicid == 42 && mapid == 1 && mainContext.Irisu1) - { - mainContext.bossbattle = false; - DebugLog("Irisu P1, ignore"); - - } - else - { - if (bossmusicflag) - { - if (mapid == 5 && musicid == 44 && mainContext.SideCh) - { - mainContext.bossbattle = false; - DebugLog("sidechapter, ignore"); - - } - else - { - mainContext.bossbattle = true; - mainContext.lastbosslist = new List(); - mainContext.lastnoah3hp = -1; - if (musicid == 37) - { - mainContext.Noah1Reload = true; - DebugLog("noah1 music start, ignore MR forever"); - } - if (mainContext.MusicStart) - { - sendsplit(); - DebugLog("music start, split"); - - } - } - } - } - } - else - { - if (!bossmusicflag) //boss music end! - { - mainContext.bossbattle = false; - if (mainContext.MusicEnd) - { - if (!mainContext.DontSplitOnReload || !reloaded) sendsplit(); - DebugLog(reloaded ? "music end, don't split (reload)" : "music end, split"); - - } - } - } - } - mainContext.lastmusicid = musicid; - - } - } - else - { - mainContext.GameMusic = "N/A"; - } - - #endregion Music - - #region SpecialBOSS - - if (mainContext.bossbattle) - { - if (mainContext.MiruDe || false)//todo noah3 option - { - int Noah3HP = -1; - - if (mapid >= 0 && mapid < StaticData.MapNames.Length) - { - int ptr = MemoryHelper.GetMemoryValue(process, StaticData.EnenyPtrAddr[mainContext.veridx]); - List bosses = new List(); - for (var i = 0; i < 50; i++) - { - ptr = ptr + StaticData.EnenyEntitySize[mainContext.veridx]; - - var emyid = MemoryHelper.GetMemoryValue(process, - ptr + StaticData.EnenyEnitiyIDOffset[mainContext.veridx], false); - if (StaticData.BossNames.ContainsKey(emyid)) - { - bosses.Add(emyid); - if (emyid == 1053) - { - Noah3HP = MemoryHelper.GetMemoryValue(process, - ptr + StaticData.EnenyEnitiyHPOffset[mainContext.veridx], false); - } - - } - - } - if (mainContext.MiruDe && mapid==8) - { - foreach (var boss in mainContext.lastbosslist) - { - - if (boss == 1043) - { - if (!bosses.Contains(boss)) //despawn - { - sendsplit(); - DebugLog("miru despawn, split"); - mainContext.bossbattle = false; - - } - } - } - } - -// if (cbBoss3.Checked) -// { -// if (bosses.Contains(1053) && Noah3HP < lastnoah3hp && Noah3HP == 1) -// { -// sendsplit(); -// DebugLog("noah3 hp 1, split"); -// bossbattle = false; -// } -// } - if (mainContext.Tm2 && musicid == 8) - { - bool f = true; - foreach (var boss in mainContext.lastbosslist) - { - - if (boss == 1024) - { - if (!bosses.Contains(boss)) //despawn - { - sendsplit(); - DebugLog("nixie despawn, split"); - mainContext.bossbattle = false; - f = false; - break; - } - } - } - - int newTM = MemoryHelper.GetMemoryValue(process, StaticData.TownMemberAddr[mainContext.veridx]); - if (newTM - mainContext.lastTM == 1 && f) //for after 1.71 , 1.71 isn't TM+2 at once when skip Nixie, it's TM+1 twice - - { - if (DateTime.Now - mainContext.LastTMAddTime < TimeSpan.FromSeconds(1)) - { - var d = DateTime.Now - mainContext.LastTMAddTime; - mainContext.bossbattle = false; - sendsplit(); - DebugLog("TM+2 in " + d.TotalMilliseconds + " ms, split"); - } - mainContext.LastTMAddTime = DateTime.Now; - } - else if (newTM - mainContext.lastTM == 2 && f)//for 1.65-1.70 - { - mainContext.bossbattle = false; - sendsplit(); - DebugLog("TM+2, split"); - } - mainContext.lastTM = newTM; - } - mainContext.lastbosslist = bosses; - mainContext.lastnoah3hp = Noah3HP; - - - } - - - } - } - - - #endregion SpecialBOSS - - if (mainContext.DebugArea) - { - int ptr = MemoryHelper.GetMemoryValue(process, StaticData.EnenyPtrAddr[mainContext.veridx]); - // List bosses = new List(); - // List HPS = new List(); -// this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => debugContext.BossList.Clear())); -// ptr += StaticData.EnenyEntitySize[mainContext.veridx] * 3; - for (var i = 0; i < 50; i++) - { - ptr += StaticData.EnenyEntitySize[mainContext.veridx]; - debugContext.BossList[i].BossID = MemoryHelper.GetMemoryValue(process, - ptr + StaticData.EnenyEnitiyIDOffset[mainContext.veridx], false); - debugContext.BossList[i].BossHP = MemoryHelper.GetMemoryValue(process, - ptr + StaticData.EnenyEnitiyHPOffset[mainContext.veridx], false); - - - - } - - } - debugContext.BossEvent = mainContext.bossbattle; + rabiRibiDisplay.ReadMemory(process); } else { - mainContext.oldtitle = ""; - mainContext.GameVer = "Not Found"; mainContext.GameMusic = "N/A"; - } } - - private void DebugLog(string log) - { - this.debugContext.DebugLog += log + "\n"; - } - - private void sendsplit() - { - SendMessage("split\r\n"); - } - - private void sendreset() - { - SendMessage("reset\r\n"); - } - - private void sendstarttimer() - { - SendMessage("starttimer\r\n"); - } - - private void sendigt(float time) - { - SendMessage($"setgametime {time}\r\n"); - } - - private void SendMessage(string message) + + public void SendMessage(string message) { if (tcpclient != null && tcpclient.Connected) { @@ -446,6 +90,7 @@ namespace rabi_splitter_WPF } } } + void disconnect() { tcpclient = null; @@ -453,8 +98,11 @@ namespace rabi_splitter_WPF { BtnConnect.IsEnabled = true; })); + } - + private void DebugLog(string log) + { + this.debugContext.Log(log); } public MainWindow() @@ -466,6 +114,7 @@ namespace rabi_splitter_WPF DebugPanel.DataContext = debugContext; this.Grid.ItemsSource = debugContext.BossList; BossEventDebug.DataContext = debugContext; + rabiRibiDisplay = new RabiRibiDisplay(mainContext, debugContext, this); memoryThread = new Thread(() => { while (true) diff --git a/rabi_splitter_WPF/MemoryHelper.cs b/rabi_splitter_WPF/MemoryHelper.cs index 22719cc..d548d8d 100644 --- a/rabi_splitter_WPF/MemoryHelper.cs +++ b/rabi_splitter_WPF/MemoryHelper.cs @@ -9,6 +9,9 @@ namespace rabi_splitter_WPF [DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); + [DllImport("kernel32.dll")] + public static extern bool CloseHandle(IntPtr hObject); + [DllImport("kernel32.dll")] public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); @@ -57,6 +60,7 @@ namespace rabi_splitter_WPF datasize, ref bytesRead); } + CloseHandle(processHandle); switch (Type.GetTypeCode(typeof(T))) { diff --git a/rabi_splitter_WPF/RabiRibiDisplay.cs b/rabi_splitter_WPF/RabiRibiDisplay.cs new file mode 100644 index 0000000..756df6b --- /dev/null +++ b/rabi_splitter_WPF/RabiRibiDisplay.cs @@ -0,0 +1,150 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace rabi_splitter_WPF +{ + class RabiRibiDisplay + { + private MainContext mainContext; + private DebugContext debugContext; + private MainWindow mainWindow; + + public int previousBlackness = -1; + public int lastmoney; + public int lastmapid; + public int lastmusicid; + public int lastplaytime = 0; + public bool bossbattle; + public List lastbosslist; + public int lastnoah3hp; + public int lastTM; + public DateTime LastTMAddTime; + private bool _noah1Reload; + + + public RabiRibiDisplay(MainContext mainContext, DebugContext debugContext, MainWindow mainWindow) + { + this.mainContext = mainContext; + this.debugContext = debugContext; + this.mainWindow = mainWindow; + } + + public void ReadMemory(Process process) + { + #region read igt + + int igt = MemoryHelper.GetMemoryValue(process, StaticData.IGTAddr[mainContext.veridx]); + if (igt > 0 && mainContext.Igt) + { + sendigt((float)igt / 60); + } + + #endregion + + #region Detect Reload + + bool reloaded = false; + { + int playtime = MemoryHelper.GetMemoryValue(process, StaticData.PlaytimeAddr[mainContext.veridx]); + reloaded = playtime != 0 && playtime < lastplaytime; + if (reloaded) DebugLog("Reload Game!"); + lastplaytime = playtime; + } + + #endregion + + #region Detect Start Game + + { + int blackness = MemoryHelper.GetMemoryValue(process, StaticData.BlacknessAddr[mainContext.veridx]); + if (previousBlackness == 0 && blackness >= 100000) + { + // Sudden increase by 100000 + // Have to be careful, though. I don't know whether anything else causes blackness to increase by 100000 + DebugLog("Start Game!"); + } + previousBlackness = blackness; + } + + #endregion + + #region CheckMoney + + { + var newmoney = MemoryHelper.GetMemoryValue(process, StaticData.MoneyAddress[mainContext.veridx]); + if (newmoney - lastmoney == 17500) + { + sendsplit(); + DebugLog("get 17500 en, split"); + } + lastmoney = newmoney; + } + + #endregion + + int mapid = MemoryHelper.GetMemoryValue(process, StaticData.MapAddress[mainContext.veridx]); + if (lastmapid != mapid) + { + DebugLog("newmap: " + mapid + ":" + StaticData.MapNames[mapid]); + lastmapid = mapid; + } + + + #region checkTM + + + + #endregion + + UpdateDebugArea(process); + } + + private void UpdateDebugArea(Process process) + { + int ptr = MemoryHelper.GetMemoryValue(process, StaticData.EnenyPtrAddr[mainContext.veridx]); + // List bosses = new List(); + // List HPS = new List(); + // this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => debugContext.BossList.Clear())); + // ptr += StaticData.EnenyEntitySize[mainContext.veridx] * 3; + for (var i = 0; i < 50; i++) + { + ptr += StaticData.EnenyEntitySize[mainContext.veridx]; + debugContext.BossList[i].BossID = MemoryHelper.GetMemoryValue(process, + ptr + StaticData.EnenyEnitiyIDOffset[mainContext.veridx], false); + debugContext.BossList[i].BossHP = MemoryHelper.GetMemoryValue(process, + ptr + StaticData.EnenyEnitiyHPOffset[mainContext.veridx], false); + } + + debugContext.BossEvent = bossbattle; + } + + private void DebugLog(string log) + { + this.debugContext.Log(log); + } + + private void sendsplit() + { + mainWindow.SendMessage("split\r\n"); + } + + private void sendreset() + { + mainWindow.SendMessage("reset\r\n"); + } + + private void sendstarttimer() + { + mainWindow.SendMessage("starttimer\r\n"); + } + + private void sendigt(float time) + { + mainWindow.SendMessage($"setgametime {time}\r\n"); + } + + } +} diff --git a/rabi_splitter_WPF/rabi_display.csproj b/rabi_splitter_WPF/rabi_display.csproj index 849729f..db6f0f8 100644 --- a/rabi_splitter_WPF/rabi_display.csproj +++ b/rabi_splitter_WPF/rabi_display.csproj @@ -55,6 +55,7 @@ MSBuild:Compile Designer + MSBuild:Compile