抽象EmuCore和键位集

This commit is contained in:
ALIENJACK\alien 2024-09-23 18:15:34 +08:00
parent e1fb708d6e
commit 7eac4e7586
17 changed files with 206 additions and 80 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 04e926e140ae5bc4fa46bd64067261cf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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<KeyCode, EnumCommand> 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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c29cb72b155d20a48a3a47a7a05160bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,18 @@
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class NesGamingScheme : ControlScheme
{
public override void SetUIKeys(Dictionary<KeyCode, EnumCommand> uiKeyMapper)
{
base.SetUIKeys(uiKeyMapper);
}
}
public static partial class ControlSchemeSetts
{
public static NesGamingScheme NES { get; private set; } = new NesGamingScheme();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a90914c97f6349a4e96302cc0ceeeed0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d43d5934b9afba14782405dc1b6eb455
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb293dc53af6e384aaa9f35864fb6605
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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));
}

View File

@ -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);
}
}
}

View File

@ -14,26 +14,11 @@ namespace AxibugEmuOnline.Client
Dictionary<KeyCode, EnumCommand> m_keyMapper = new Dictionary<KeyCode, EnumCommand>();
public Dictionary<KeyCode, EnumCommand> 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()

View File

@ -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<OptionMenu> menus = new List<OptionMenu>();
@ -43,11 +40,6 @@ namespace AxibugEmuOnline.Client
Instance = null;
}
/// <summary>
/// 获取模拟器核心对象
/// </summary>
/// <typeparam name="T">模拟器核心对象类型</typeparam>
public T GetCore<T>() => (T)m_core;
/// <summary> ±£´æ¿ìËÙ¿ìÕÕ </summary>
public void SaveQuickState(object state)
{
@ -58,27 +50,17 @@ namespace AxibugEmuOnline.Client
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public bool GetQuickState<T>(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<int>(EEvent.OnRoomWaitStepChange, OnServerStepUpdate);
App.roomMgr.SendLeavnRoom();
App.emu.StopGame();
ControlScheme.Current = ControlSchemeSetts.Normal;
}
}
}

View File

@ -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<State>(out var quickState))
{
m_gameUI.GetCore<NesEmulator>().NesCore.LoadState(quickState);
}
break;
}
m_gameUI.Core.LoadState(m_gameUI.GetQuickState());
sw.Stop();
App.log.Info($"{m_gameUI.RomFile.Platform}====>快照加载耗时:{sw.Elapsed.TotalMilliseconds}ms");
}

View File

@ -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<NesEmulator>().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");
}

View File

@ -26,22 +26,14 @@ namespace AxibugEmuOnline.Client
PauseCore();
if (App.roomMgr.IsHost)
{
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES)
{
var stateRaw = m_inGameUI.GetCore<NesEmulator>().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<NesEmulator>().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<NesEmulator>().Pause();
}
m_inGameUI.Core.Pause();
}
private void ResumeCore()
{
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES)
{
m_inGameUI.GetCore<NesEmulator>().Resume();
}
m_inGameUI.Core.Resume();
}
internal void Reset()

View File

@ -33,6 +33,11 @@ namespace AxibugEmuOnline.Client
MainMenu.ListenControlAction = true;
}
private void Start()
{
ControlScheme.Current = ControlSchemeSetts.Normal;
}
public void HideMainMenu()
{
BG.gameObject.SetActiveEx(false);