From 7eac4e75869607848e06ea93e9a97adc7f39b405 Mon Sep 17 00:00:00 2001 From: "ALIENJACK\\alien" Date: Mon, 23 Sep 2024 18:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1EmuCore=E5=92=8C=E9=94=AE?= =?UTF-8?q?=E4=BD=8D=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Script/ControlSchemes.meta | 8 ++++ .../Script/ControlSchemes/ControlScheme.cs | 44 +++++++++++++++++++ .../ControlSchemes/ControlScheme.cs.meta | 11 +++++ .../Script/ControlSchemes/NesGamingScheme.cs | 18 ++++++++ .../ControlSchemes/NesGamingScheme.cs.meta | 11 +++++ .../Script/ControlSchemes/NormalScheme.cs | 18 ++++++++ .../ControlSchemes/NormalScheme.cs.meta | 11 +++++ .../Assets/Script/IEmuCore.cs | 17 +++++++ .../Assets/Script/IEmuCore.cs.meta | 11 +++++ .../Assets/Script/Manager/AppRoom.cs | 2 +- .../Assets/Script/NesEmulator/NesEmulator.cs | 37 ++++++++++++++-- .../UI/CommandDispatcher/CommandDispatcher.cs | 19 +------- .../Assets/Script/UI/InGameUI/InGameUI.cs | 30 +++---------- .../Script/UI/InGameUI/InGameUI_LoadState.cs | 10 +---- .../Script/UI/InGameUI/InGameUI_SaveState.cs | 10 ++--- .../Script/UI/InGameUI/StepPerformer.cs | 24 +++------- .../Assets/Script/UI/LaunchUI.cs | 5 +++ 17 files changed, 206 insertions(+), 80 deletions(-) create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs.meta diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes.meta b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes.meta new file mode 100644 index 0000000..e0fa40f --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 04e926e140ae5bc4fa46bd64067261cf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs new file mode 100644 index 0000000..50d5e43 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace AxibugEmuOnline.Client +{ + public abstract class ControlScheme + { + private static ControlScheme m_current; + public static ControlScheme Current + { + get => m_current; + set + { + m_current = value; + + m_current.SetUIKeys(CommandDispatcher.Instance.GetKeyMapper()); + } + } + + public string Name { get; private set; } + + public virtual void SetUIKeys(Dictionary uiKeyMapper) + { + uiKeyMapper.Clear(); + + uiKeyMapper[KeyCode.A] = EnumCommand.SelectItemLeft; + uiKeyMapper[KeyCode.D] = EnumCommand.SelectItemRight; + uiKeyMapper[KeyCode.W] = EnumCommand.SelectItemUp; + uiKeyMapper[KeyCode.S] = EnumCommand.SelectItemDown; + uiKeyMapper[KeyCode.K] = EnumCommand.Enter; + uiKeyMapper[KeyCode.L] = EnumCommand.Back; + uiKeyMapper[KeyCode.I] = EnumCommand.OptionMenu; + + uiKeyMapper[KeyCode.LeftArrow] = EnumCommand.SelectItemLeft; + uiKeyMapper[KeyCode.RightArrow] = EnumCommand.SelectItemRight; + uiKeyMapper[KeyCode.UpArrow] = EnumCommand.SelectItemUp; + uiKeyMapper[KeyCode.DownArrow] = EnumCommand.SelectItemDown; + uiKeyMapper[KeyCode.Return] = EnumCommand.Enter; + uiKeyMapper[KeyCode.Escape] = EnumCommand.Back; + uiKeyMapper[KeyCode.RightShift] = EnumCommand.OptionMenu; + uiKeyMapper[KeyCode.LeftShift] = EnumCommand.OptionMenu; + } + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs.meta b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs.meta new file mode 100644 index 0000000..8739d08 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/ControlScheme.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c29cb72b155d20a48a3a47a7a05160bd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs new file mode 100644 index 0000000..6336b35 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace AxibugEmuOnline.Client +{ + public class NesGamingScheme : ControlScheme + { + public override void SetUIKeys(Dictionary uiKeyMapper) + { + base.SetUIKeys(uiKeyMapper); + } + } + + public static partial class ControlSchemeSetts + { + public static NesGamingScheme NES { get; private set; } = new NesGamingScheme(); + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs.meta b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs.meta new file mode 100644 index 0000000..b87af5c --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NesGamingScheme.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a90914c97f6349a4e96302cc0ceeeed0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs new file mode 100644 index 0000000..b1d36b8 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AxibugEmuOnline.Client +{ + + public class NormalScheme : ControlScheme + { + } + + public static partial class ControlSchemeSetts + { + public static NormalScheme Normal { get; private set; } = new NormalScheme(); + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs.meta b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs.meta new file mode 100644 index 0000000..41f00f7 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/ControlSchemes/NormalScheme.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d43d5934b9afba14782405dc1b6eb455 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs b/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs new file mode 100644 index 0000000..c60b316 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace AxibugEmuOnline.Client +{ + public interface IEmuCore + { + object GetState(); + byte[] GetStateBytes(); + void LoadState(object state); + void LoadStateFromBytes(byte[] data); + void Pause(); + void Resume(); + void SetupScheme(); + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs.meta b/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs.meta new file mode 100644 index 0000000..2463578 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/IEmuCore.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb293dc53af6e384aaa9f35864fb6605 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/Manager/AppRoom.cs b/AxibugEmuOnline.Client/Assets/Script/Manager/AppRoom.cs index 22e12c4..e779df2 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Manager/AppRoom.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Manager/AppRoom.cs @@ -298,7 +298,7 @@ namespace AxibugEmuOnline.Client.Manager if (!InRoom) return; _Protobuf_Room_Leave.RoomID = mineRoomMiniInfo.RoomID; - App.log.Info($"创建房间"); + App.log.Info($"LeavnRoom"); App.network.SendToServer((int)CommandID.CmdRoomLeave, ProtoBufHelper.Serizlize(_Protobuf_Room_Leave)); } diff --git a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesEmulator.cs b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesEmulator.cs index 5d458ab..4061b1c 100644 --- a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesEmulator.cs +++ b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesEmulator.cs @@ -1,5 +1,6 @@ using AxibugEmuOnline.Client.ClientCore; using System; +using System.Diagnostics; using System.IO; using System.Xml.Linq; using UnityEngine; @@ -8,7 +9,7 @@ using VirtualNes.Core.Debug; namespace AxibugEmuOnline.Client { - public class NesEmulator : MonoBehaviour + public class NesEmulator : MonoBehaviour, IEmuCore { public NES NesCore { get; private set; } @@ -37,7 +38,7 @@ namespace AxibugEmuOnline.Client catch (Exception ex) { NesCore = null; - Debug.LogError(ex); + App.log.Error(ex.ToString()); } } @@ -79,7 +80,9 @@ namespace AxibugEmuOnline.Client m_bPause = false; } -#if UNITY_EDITOR + + + [Conditional("UNITY_EDITOR")] [ContextMenu("ImportNesDB")] public void ImportNesDB() { @@ -103,6 +106,32 @@ namespace AxibugEmuOnline.Client UnityEditor.EditorUtility.SetDirty(db); UnityEditor.AssetDatabase.SaveAssets(); } -#endif + + public void SetupScheme() + { + ControlScheme.Current = ControlSchemeSetts.NES; + } + + public void LoadState(object state) + { + NesCore.LoadState((State)state); + } + + public object GetState() + { + return NesCore.GetState(); + } + + public byte[] GetStateBytes() + { + return NesCore.GetState().ToBytes(); + } + + public void LoadStateFromBytes(byte[] data) + { + State st = new State(); + st.FromByte(data); + NesCore.LoadState(st); + } } } diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/CommandDispatcher/CommandDispatcher.cs b/AxibugEmuOnline.Client/Assets/Script/UI/CommandDispatcher/CommandDispatcher.cs index 487a421..000df4e 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/CommandDispatcher/CommandDispatcher.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/CommandDispatcher/CommandDispatcher.cs @@ -14,26 +14,11 @@ namespace AxibugEmuOnline.Client Dictionary m_keyMapper = new Dictionary(); + public Dictionary GetKeyMapper() => m_keyMapper; + private void Awake() { Instance = this; - - m_keyMapper.Add(KeyCode.A, EnumCommand.SelectItemLeft); - m_keyMapper.Add(KeyCode.D, EnumCommand.SelectItemRight); - m_keyMapper.Add(KeyCode.W, EnumCommand.SelectItemUp); - m_keyMapper.Add(KeyCode.S, EnumCommand.SelectItemDown); - m_keyMapper.Add(KeyCode.K, EnumCommand.Enter); - m_keyMapper.Add(KeyCode.L, EnumCommand.Back); - m_keyMapper.Add(KeyCode.I, EnumCommand.OptionMenu); - - m_keyMapper.Add(KeyCode.LeftArrow, EnumCommand.SelectItemLeft); - m_keyMapper.Add(KeyCode.RightArrow, EnumCommand.SelectItemRight); - m_keyMapper.Add(KeyCode.UpArrow, EnumCommand.SelectItemUp); - m_keyMapper.Add(KeyCode.DownArrow, EnumCommand.SelectItemDown); - m_keyMapper.Add(KeyCode.Return, EnumCommand.Enter); - m_keyMapper.Add(KeyCode.Escape, EnumCommand.Back); - m_keyMapper.Add(KeyCode.RightShift, EnumCommand.OptionMenu); - m_keyMapper.Add(KeyCode.LeftShift, EnumCommand.OptionMenu); } private void OnDestroy() diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI.cs b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI.cs index 5cd0b30..2e402b1 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI.cs @@ -1,9 +1,6 @@ using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.Event; -using AxibugEmuOnline.Client.Manager; -using System; using System.Collections.Generic; -using VirtualNes.Core; namespace AxibugEmuOnline.Client { @@ -18,7 +15,7 @@ namespace AxibugEmuOnline.Client public bool IsOnline => App.roomMgr.RoomState > AxibugProtobuf.RoomGameState.OnlyHost; private RomFile m_rom; - private object m_core; + public IEmuCore Core { get; private set; } private object m_state; private List menus = new List(); @@ -43,11 +40,6 @@ namespace AxibugEmuOnline.Client Instance = null; } - /// - /// ȡģĶ - /// - /// ģĶ - public T GetCore() => (T)m_core; /// ٿ public void SaveQuickState(object state) { @@ -58,27 +50,17 @@ namespace AxibugEmuOnline.Client /// /// /// - public bool GetQuickState(out T state) + public object GetQuickState() { - state = default(T); - - if (m_state is T) - { - state = (T)m_state; - return true; - } - else - { - return false; - } + return m_state; } - public void Show(RomFile currentRom, object core) + public void Show(RomFile currentRom, IEmuCore core) { CommandDispatcher.Instance.RegistController(this); m_rom = currentRom; - m_core = core; + Core = core; m_stepPerformer.Reset(); if (App.user.IsLoggedIn) @@ -113,6 +95,8 @@ namespace AxibugEmuOnline.Client Eventer.Instance.UnregisterEvent(EEvent.OnRoomWaitStepChange, OnServerStepUpdate); App.roomMgr.SendLeavnRoom(); App.emu.StopGame(); + + ControlScheme.Current = ControlSchemeSetts.Normal; } } } diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_LoadState.cs b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_LoadState.cs index e36a63b..9f33a2a 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_LoadState.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_LoadState.cs @@ -17,15 +17,7 @@ namespace AxibugEmuOnline.Client public override void OnExcute() { Stopwatch sw = Stopwatch.StartNew(); - switch (m_gameUI.RomFile.Platform) - { - case EnumPlatform.NES: - if (m_gameUI.GetQuickState(out var quickState)) - { - m_gameUI.GetCore().NesCore.LoadState(quickState); - } - break; - } + m_gameUI.Core.LoadState(m_gameUI.GetQuickState()); sw.Stop(); App.log.Info($"{m_gameUI.RomFile.Platform}====>ռغʱ:{sw.Elapsed.TotalMilliseconds}ms"); } diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_SaveState.cs b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_SaveState.cs index 871873c..8c00ff4 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_SaveState.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/InGameUI_SaveState.cs @@ -1,5 +1,6 @@ using AxibugEmuOnline.Client.ClientCore; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; @@ -19,13 +20,8 @@ namespace AxibugEmuOnline.Client public override void OnExcute() { Stopwatch sw = Stopwatch.StartNew(); - switch (m_gameUI.RomFile.Platform) - { - case EnumPlatform.NES: - var state = m_gameUI.GetCore().NesCore.GetState(); - m_gameUI.SaveQuickState(state); - break; - } + object state = m_gameUI.Core.GetState(); + m_gameUI.SaveQuickState(state); sw.Stop(); App.log.Info($"{m_gameUI.RomFile.Platform}====>ȡպʱ:{sw.Elapsed.TotalMilliseconds}ms"); } diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/StepPerformer.cs b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/StepPerformer.cs index 90a595f..9564601 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/StepPerformer.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/InGameUI/StepPerformer.cs @@ -26,22 +26,14 @@ namespace AxibugEmuOnline.Client PauseCore(); if (App.roomMgr.IsHost) { - if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) - { - var stateRaw = m_inGameUI.GetCore().NesCore.GetState().ToBytes(); - App.roomMgr.SendHostRaw(stateRaw); - } + var stateRaw = m_inGameUI.Core.GetStateBytes(); + App.roomMgr.SendHostRaw(stateRaw); } break; //加载存档并发送Ready通知 case 1: PauseCore(); - var state = new State(); - state.FromByte(App.roomMgr.RawData); - if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) - { - m_inGameUI.GetCore().NesCore.LoadState(state); - } + m_inGameUI.Core.LoadStateFromBytes(App.roomMgr.RawData); App.roomMgr.SendRoomPlayerReady(); break; case 2: @@ -53,18 +45,12 @@ namespace AxibugEmuOnline.Client private void PauseCore() { - if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) - { - m_inGameUI.GetCore().Pause(); - } + m_inGameUI.Core.Pause(); } private void ResumeCore() { - if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) - { - m_inGameUI.GetCore().Resume(); - } + m_inGameUI.Core.Resume(); } internal void Reset() diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/LaunchUI.cs b/AxibugEmuOnline.Client/Assets/Script/UI/LaunchUI.cs index 2e49518..eaf597d 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/LaunchUI.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/LaunchUI.cs @@ -33,6 +33,11 @@ namespace AxibugEmuOnline.Client MainMenu.ListenControlAction = true; } + private void Start() + { + ControlScheme.Current = ControlSchemeSetts.Normal; + } + public void HideMainMenu() { BG.gameObject.SetActiveEx(false);