From aa984889d9f1d58d53cd2582c31c1ba9d6e946f8 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Thu, 16 Jan 2025 11:14:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MAME.Unity/Assets/Script/UMAME/UMAME.cs | 19 +++++++- .../Script/UMAME/UniInterface/KeyCodeCore.cs | 43 ++++++++++++++----- .../Script/UMAME/UniInterface/UniKeyboard.cs | 5 +-- .../Script/UMAME/UniInterface/UniTimeSpan.cs | 41 ++++++++++++++++++ .../UMAME/UniInterface/UniTimeSpan.cs.meta | 11 +++++ .../UMAME/UniInterface/UniVideoPlayer.cs | 1 + 6 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs create mode 100644 MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs.meta diff --git a/MAME.Unity/Assets/Script/UMAME/UMAME.cs b/MAME.Unity/Assets/Script/UMAME/UMAME.cs index dea0252..ff707d0 100644 --- a/MAME.Unity/Assets/Script/UMAME/UMAME.cs +++ b/MAME.Unity/Assets/Script/UMAME/UMAME.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Threading; using UnityEngine; using UnityEngine.UI; @@ -30,10 +31,12 @@ public class UMAME : MonoBehaviour public Dictionary ALLGame; public List HadGameList = new List(); string mChangeRomName = string.Empty; + public UniTimeSpan mTimeSpan; public bool bQuickTestRom = false; public string mQuickTestRom = string.Empty; public ReplayWriter mReplayWriter; public ReplayReader mReplayReader; + public long currEmuFrame => emu.currEmuFrame; Dropdown optionDropdown; @@ -70,8 +73,9 @@ public class UMAME : MonoBehaviour mUniKeyboard = this.gameObject.AddComponent(); mUniResources = new UniResources(); mChangeRomName = UniMAMESetting.instance.LastGameRom; + mTimeSpan = new UniTimeSpan(); - emu.Init(RomPath, mUniLog, mUniResources, mUniVideoPlayer, mUniSoundPlayer, mUniKeyboard.mKeyCodeCore, mUniMouse); + emu.Init(RomPath, mUniLog, mUniResources, mUniVideoPlayer, mUniSoundPlayer, mUniKeyboard.mKeyCodeCore, mUniMouse, mTimeSpan); ALLGame = emu.GetGameList(); Debug.Log($"ALLGame:{ALLGame.Count}"); @@ -106,6 +110,8 @@ public class UMAME : MonoBehaviour void LoadGame(bool bReplay = false) { + Application.targetFrameRate = 60; + mReplayWriter = new ReplayWriter(mChangeRomName, "fuck", ReplayData.ReplayFormat.FM32IP64, Encoding.UTF8); mChangeRomName = HadGameList[optionDropdown.value].Name; UniMAMESetting.instance.LastGameRom = mChangeRomName; @@ -137,10 +143,19 @@ public class UMAME : MonoBehaviour Debug.Log($"ROM加载失败"); } } - private void Update() + + void Update() { mFPS.text = ($"fpsv {mUniVideoPlayer.videoFPS.ToString("F2")} fpsa {mUniSoundPlayer.audioFPS.ToString("F2")}"); + if (!bInGame) + return; + + //采集本帧Input + mUniKeyboard.UpdateInputKey(); + //放行下一帧 + emu.UnlockNextFreme(); + if (Input.GetKeyDown(KeyCode.F1)) { SaveReplay(); diff --git a/MAME.Unity/Assets/Script/UMAME/UniInterface/KeyCodeCore.cs b/MAME.Unity/Assets/Script/UMAME/UniInterface/KeyCodeCore.cs index de1ce28..a25c037 100644 --- a/MAME.Unity/Assets/Script/UMAME/UniInterface/KeyCodeCore.cs +++ b/MAME.Unity/Assets/Script/UMAME/UniInterface/KeyCodeCore.cs @@ -16,12 +16,30 @@ public class KeyCodeCore : IKeyboard bool bReplayMode; List ReplayCheckKey = new List(); + ulong last_CurryInpuAllData_test = 0; + public MotionKey[] GetPressedKeys() { if (!bReplayMode) { - //UMAME.instance.mReplayWriter.NextFrame(CurryInpuAllData); + //UMAME.instance.mReplayWriter.NextFramebyFrameIdx((int)UMAME.instance.mUniVideoPlayer.mFrame, CurryInpuAllData); UMAME.instance.mReplayWriter.NextFramebyFrameIdx((int)UMAME.instance.mUniVideoPlayer.mFrame, CurryInpuAllData); + +#if UNITY_EDITOR + if (last_CurryInpuAllData_test != CurryInpuAllData) + { + last_CurryInpuAllData_test = CurryInpuAllData; + string TempStr = ""; + foreach (var item in mCurrKey) + { + TempStr += $"{item.ToString()}|"; + } + if (!string.IsNullOrEmpty(TempStr)) + Debug.Log($"{UMAME.instance.mUniVideoPlayer.mFrame} | Input-> {TempStr}"); + else + Debug.Log($"{UMAME.instance.mUniVideoPlayer.mFrame} | Input-> 0"); + } +#endif return mCurrKey; } else @@ -39,17 +57,21 @@ public class KeyCodeCore : IKeyboard temp.Add(ReplayCheckKey[i]); } mCurrKey = temp.ToArray(); - } + #if UNITY_EDITOR - string TempStr = ""; - foreach (var item in mCurrKey) - { - TempStr += $"{item.ToString()}|"; - } - if (!string.IsNullOrEmpty(TempStr)) - Debug.Log($"Input-銆媨CurryInpuAllData} => {TempStr}"); + string TempStr = ""; + foreach (var item in mCurrKey) + { + TempStr += $"{item.ToString()}|"; + } + if (!string.IsNullOrEmpty(TempStr)) + Debug.Log($"{UMAME.instance.mUniVideoPlayer.mFrame} | Input-> {TempStr}"); + else + Debug.Log($"{UMAME.instance.mUniVideoPlayer.mFrame} | Input-> 0"); #endif + } + return mCurrKey; } } @@ -66,9 +88,8 @@ public class KeyCodeCore : IKeyboard { ReplayCheckKey.Add(mkey); } - dictKeyCfgs.Clear(); - dictKeyCfgs.Add(KeyCode.P, MotionKey.EMU_PAUSED); + //dictKeyCfgs.Add(KeyCode.P, MotionKey.EMU_PAUSED); dictKeyCfgs.Add(KeyCode.Alpha1, MotionKey.P1_GAMESTART); dictKeyCfgs.Add(KeyCode.Alpha5, MotionKey.P1_INSERT_COIN); dictKeyCfgs.Add(KeyCode.W, MotionKey.P1_UP); diff --git a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniKeyboard.cs b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniKeyboard.cs index 91f05fe..2a1bb85 100644 --- a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniKeyboard.cs +++ b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniKeyboard.cs @@ -66,11 +66,8 @@ public class UniKeyboard : MonoBehaviour mKeyCodeCore.Init(this,false); } - void OnEnable() - { - } - void Update() + public void UpdateInputKey() { mKeyCodeCore.UpdateLogic(); } diff --git a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs new file mode 100644 index 0000000..f62edbd --- /dev/null +++ b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs @@ -0,0 +1,41 @@ +using MAME.Core; + +public class UniTimeSpan : ITimeSpan +{ + public ulong tick; + double tickDetailus = 16666.666667; + object tickLock = new object(); + + public void SetTick(ulong nexttick) + { + //lock (tickLock) + { + tick = nexttick; + } + } + + //这个函数无意义 + public uint GetTickCount() + { + //lock (tickLock) + { + //return (uint)(tick * tickDetail); + return 0; + } + } + + public bool QueryPerformanceCounter(ref long lpPerformanceCount) + { + lock (tickLock) + { + lpPerformanceCount = (long)tick; + return true; + } + } + + public bool QueryPerformanceFrequency(ref long PerformanceFrequency) + { + PerformanceFrequency = (long)(1000000 / tickDetailus); + return true; + } +} diff --git a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs.meta b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs.meta new file mode 100644 index 0000000..a61d747 --- /dev/null +++ b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniTimeSpan.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a35bf49414a7b194eb8d359004336a8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniVideoPlayer.cs b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniVideoPlayer.cs index cd4ec06..0ecfc03 100644 --- a/MAME.Unity/Assets/Script/UMAME/UniInterface/UniVideoPlayer.cs +++ b/MAME.Unity/Assets/Script/UMAME/UniInterface/UniVideoPlayer.cs @@ -72,6 +72,7 @@ public class UniVideoPlayer : MonoBehaviour, IVideoPlayer public void SubmitVideo(int[] data, long frame_number) { mFrame = (ulong)frame_number; + UMAME.instance.mTimeSpan.SetTick(mFrame); var current = UMAME.sw.Elapsed; var delta = current - lastElapsed; lastElapsed = current;