master #39

Merged
sin365 merged 7 commits from Alienjack/AxibugEmuOnline:master into master 2024-09-24 18:02:02 +08:00
17 changed files with 206 additions and 80 deletions
Showing only changes of commit 7eac4e7586 - Show all commits

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) if (!InRoom)
return; return;
_Protobuf_Room_Leave.RoomID = mineRoomMiniInfo.RoomID; _Protobuf_Room_Leave.RoomID = mineRoomMiniInfo.RoomID;
App.log.Info($"创建房间"); App.log.Info($"LeavnRoom");
App.network.SendToServer((int)CommandID.CmdRoomLeave, ProtoBufHelper.Serizlize(_Protobuf_Room_Leave)); App.network.SendToServer((int)CommandID.CmdRoomLeave, ProtoBufHelper.Serizlize(_Protobuf_Room_Leave));
} }

View File

@ -1,5 +1,6 @@
using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.ClientCore;
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Xml.Linq; using System.Xml.Linq;
using UnityEngine; using UnityEngine;
@ -8,7 +9,7 @@ using VirtualNes.Core.Debug;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
public class NesEmulator : MonoBehaviour public class NesEmulator : MonoBehaviour, IEmuCore
{ {
public NES NesCore { get; private set; } public NES NesCore { get; private set; }
@ -37,7 +38,7 @@ namespace AxibugEmuOnline.Client
catch (Exception ex) catch (Exception ex)
{ {
NesCore = null; NesCore = null;
Debug.LogError(ex); App.log.Error(ex.ToString());
} }
} }
@ -79,7 +80,9 @@ namespace AxibugEmuOnline.Client
m_bPause = false; m_bPause = false;
} }
#if UNITY_EDITOR
[Conditional("UNITY_EDITOR")]
[ContextMenu("ImportNesDB")] [ContextMenu("ImportNesDB")]
public void ImportNesDB() public void ImportNesDB()
{ {
@ -103,6 +106,32 @@ namespace AxibugEmuOnline.Client
UnityEditor.EditorUtility.SetDirty(db); UnityEditor.EditorUtility.SetDirty(db);
UnityEditor.AssetDatabase.SaveAssets(); 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>(); Dictionary<KeyCode, EnumCommand> m_keyMapper = new Dictionary<KeyCode, EnumCommand>();
public Dictionary<KeyCode, EnumCommand> GetKeyMapper() => m_keyMapper;
private void Awake() private void Awake()
{ {
Instance = this; 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() private void OnDestroy()

View File

@ -1,9 +1,6 @@
using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.Event; using AxibugEmuOnline.Client.Event;
using AxibugEmuOnline.Client.Manager;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using VirtualNes.Core;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
@ -18,7 +15,7 @@ namespace AxibugEmuOnline.Client
public bool IsOnline => App.roomMgr.RoomState > AxibugProtobuf.RoomGameState.OnlyHost; public bool IsOnline => App.roomMgr.RoomState > AxibugProtobuf.RoomGameState.OnlyHost;
private RomFile m_rom; private RomFile m_rom;
private object m_core; public IEmuCore Core { get; private set; }
private object m_state; private object m_state;
private List<OptionMenu> menus = new List<OptionMenu>(); private List<OptionMenu> menus = new List<OptionMenu>();
@ -43,11 +40,6 @@ namespace AxibugEmuOnline.Client
Instance = null; Instance = null;
} }
/// <summary>
/// 获取模拟器核心对象
/// </summary>
/// <typeparam name="T">模拟器核心对象类型</typeparam>
public T GetCore<T>() => (T)m_core;
/// <summary> ±£´æ¿ìËÙ¿ìÕÕ </summary> /// <summary> ±£´æ¿ìËÙ¿ìÕÕ </summary>
public void SaveQuickState(object state) public void SaveQuickState(object state)
{ {
@ -58,27 +50,17 @@ namespace AxibugEmuOnline.Client
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <returns></returns> /// <returns></returns>
public bool GetQuickState<T>(out T state) public object GetQuickState()
{ {
state = default(T); return m_state;
if (m_state is T)
{
state = (T)m_state;
return true;
}
else
{
return false;
}
} }
public void Show(RomFile currentRom, object core) public void Show(RomFile currentRom, IEmuCore core)
{ {
CommandDispatcher.Instance.RegistController(this); CommandDispatcher.Instance.RegistController(this);
m_rom = currentRom; m_rom = currentRom;
m_core = core; Core = core;
m_stepPerformer.Reset(); m_stepPerformer.Reset();
if (App.user.IsLoggedIn) if (App.user.IsLoggedIn)
@ -113,6 +95,8 @@ namespace AxibugEmuOnline.Client
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomWaitStepChange, OnServerStepUpdate); Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomWaitStepChange, OnServerStepUpdate);
App.roomMgr.SendLeavnRoom(); App.roomMgr.SendLeavnRoom();
App.emu.StopGame(); App.emu.StopGame();
ControlScheme.Current = ControlSchemeSetts.Normal;
} }
} }
} }

View File

@ -17,15 +17,7 @@ namespace AxibugEmuOnline.Client
public override void OnExcute() public override void OnExcute()
{ {
Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew();
switch (m_gameUI.RomFile.Platform) m_gameUI.Core.LoadState(m_gameUI.GetQuickState());
{
case EnumPlatform.NES:
if (m_gameUI.GetQuickState<State>(out var quickState))
{
m_gameUI.GetCore<NesEmulator>().NesCore.LoadState(quickState);
}
break;
}
sw.Stop(); sw.Stop();
App.log.Info($"{m_gameUI.RomFile.Platform}====>快照加载耗时:{sw.Elapsed.TotalMilliseconds}ms"); App.log.Info($"{m_gameUI.RomFile.Platform}====>快照加载耗时:{sw.Elapsed.TotalMilliseconds}ms");
} }

View File

@ -1,5 +1,6 @@
using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.ClientCore;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
@ -19,13 +20,8 @@ namespace AxibugEmuOnline.Client
public override void OnExcute() public override void OnExcute()
{ {
Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew();
switch (m_gameUI.RomFile.Platform) object state = m_gameUI.Core.GetState();
{ m_gameUI.SaveQuickState(state);
case EnumPlatform.NES:
var state = m_gameUI.GetCore<NesEmulator>().NesCore.GetState();
m_gameUI.SaveQuickState(state);
break;
}
sw.Stop(); sw.Stop();
App.log.Info($"{m_gameUI.RomFile.Platform}====>获取快照耗时:{sw.Elapsed.TotalMilliseconds}ms"); App.log.Info($"{m_gameUI.RomFile.Platform}====>获取快照耗时:{sw.Elapsed.TotalMilliseconds}ms");
} }

View File

@ -26,22 +26,14 @@ namespace AxibugEmuOnline.Client
PauseCore(); PauseCore();
if (App.roomMgr.IsHost) if (App.roomMgr.IsHost)
{ {
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) var stateRaw = m_inGameUI.Core.GetStateBytes();
{ App.roomMgr.SendHostRaw(stateRaw);
var stateRaw = m_inGameUI.GetCore<NesEmulator>().NesCore.GetState().ToBytes();
App.roomMgr.SendHostRaw(stateRaw);
}
} }
break; break;
//加载存档并发送Ready通知 //加载存档并发送Ready通知
case 1: case 1:
PauseCore(); PauseCore();
var state = new State(); m_inGameUI.Core.LoadStateFromBytes(App.roomMgr.RawData);
state.FromByte(App.roomMgr.RawData);
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES)
{
m_inGameUI.GetCore<NesEmulator>().NesCore.LoadState(state);
}
App.roomMgr.SendRoomPlayerReady(); App.roomMgr.SendRoomPlayerReady();
break; break;
case 2: case 2:
@ -53,18 +45,12 @@ namespace AxibugEmuOnline.Client
private void PauseCore() private void PauseCore()
{ {
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) m_inGameUI.Core.Pause();
{
m_inGameUI.GetCore<NesEmulator>().Pause();
}
} }
private void ResumeCore() private void ResumeCore()
{ {
if (m_inGameUI.RomFile.Platform == EnumPlatform.NES) m_inGameUI.Core.Resume();
{
m_inGameUI.GetCore<NesEmulator>().Resume();
}
} }
internal void Reset() internal void Reset()

View File

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