Merge pull request 'master' (#12) from Alienjack/AxibugEmuOnline:master into master

Reviewed-on: #12
This commit is contained in:
sin365 2024-07-19 12:59:29 +08:00
commit 1a3294d3a3
27 changed files with 446 additions and 282 deletions

View File

@ -13,7 +13,7 @@ namespace MyNes.Core
public override void Load(string fileName, bool loadDumps)
{
var fileStream = MyNesMain.FileManager.OpenRomFile(fileName);
var fileStream = MyNesMain.Supporter.OpenRomFile(fileName);
if (fileStream.Length < 16)
{
fileStream.Close();

View File

@ -9,7 +9,7 @@ namespace MyNes.Core
{
public static EmuSettings EmuSettings { get; private set; }
public static RendererSettings RendererSettings { get; private set; }
public static IFileManager FileManager { get; private set; }
public static IExternalSupporter Supporter { get; private set; }
public static string WorkingFolder { get; private set; }
internal static List<Board> Boards { get; private set; }
@ -20,10 +20,10 @@ namespace MyNes.Core
public static WaveRecorder WaveRecorder { get; private set; }
public static void Initialize(IFileManager fileManager, IVideoProvider videoProvider, IAudioProvider audioProvider)
public static void Initialize(IExternalSupporter fileManager, IVideoProvider videoProvider, IAudioProvider audioProvider)
{
Tracer.WriteLine("Initializing My Nes Core ....");
FileManager = fileManager;
Supporter = fileManager;
WorkingFolder = fileManager.GetWorkingFolderPath();
Tracer.WriteLine("Loading emu settings ...");
EmuSettings = new EmuSettings(Path.Combine(WorkingFolder, "emusettings.ini"));
@ -211,11 +211,5 @@ namespace MyNes.Core
}
}
}
public interface IFileManager
{
string GetWorkingFolderPath();
public Stream OpenDatabaseFile();
public Stream OpenPaletteFile();
public Stream OpenRomFile(string path);
}
}

View File

@ -28,7 +28,7 @@ namespace MyNes.Core
Ready = false;
_databaseRoms.Clear();
var stream = MyNesMain.FileManager.OpenDatabaseFile();
var stream = MyNesMain.Supporter.OpenDatabaseFile();
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.DtdProcessing = DtdProcessing.Ignore;
xmlReaderSettings.IgnoreWhitespace = true;

View File

@ -133,7 +133,7 @@ namespace MyNes.Core
private static bool nos_ignore_reload;
private static byte[][] sq_duty_cycle_sequences = new byte[4][]
private static readonly byte[][] sq_duty_cycle_sequences = new byte[4][]
{
new byte[8] { 0, 0, 0, 0, 0, 0, 0, 1 },
new byte[8] { 0, 0, 0, 0, 0, 0, 1, 1 },
@ -141,7 +141,7 @@ namespace MyNes.Core
new byte[8] { 1, 1, 1, 1, 1, 1, 0, 0 }
};
private static byte[] sq_duration_table = new byte[32]
private static readonly byte[] sq_duration_table = new byte[32]
{
10, 254, 20, 2, 40, 4, 80, 6, 160, 8,
60, 10, 14, 12, 26, 14, 12, 16, 24, 18,
@ -195,7 +195,7 @@ namespace MyNes.Core
private static bool sq1_ignore_reload;
private static byte[] trl_step_seq = new byte[32]
private static readonly byte[] trl_step_seq = new byte[32]
{
15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
5, 4, 3, 2, 1, 0, 0, 1, 2, 3,
@ -247,8 +247,6 @@ namespace MyNes.Core
private static bool apu_irq_flag;
private static bool apu_irq_do_it;
internal static bool apu_irq_delta_occur;
private static bool apu_seq_mode;
@ -313,8 +311,6 @@ namespace MyNes.Core
private static SoundHighPassFilter audio_high_pass_filter_440;
private static SoundDCBlockerFilter audio_dc_blocker_filter;
private static bool audio_sq1_outputable;
private static bool audio_sq2_outputable;
@ -453,11 +449,9 @@ namespace MyNes.Core
private static IJoypadConnecter joypad4;
private static IShortcutsHandler shortucts;
public static bool IsFourPlayers;
private static byte[] reverseLookup = new byte[256]
private static readonly byte[] reverseLookup = new byte[256]
{
0, 128, 64, 192, 32, 160, 96, 224, 16, 144,
80, 208, 48, 176, 112, 240, 8, 136, 72, 200,
@ -663,24 +657,10 @@ namespace MyNes.Core
private static Thread mainThread;
private static double fps_time_last;
private static double fps_time_start;
private static double fps_time_token;
private static double fps_time_dead;
private static double fps_time_period;
private static double fps_time_frame_time;
private static double emu_time_target_fps = 60.0;
private static bool emu_frame_clocking_mode;
private static bool emu_frame_done;
private static bool render_initialized;
private static RenderVideoFrame render_video;
@ -1661,7 +1641,6 @@ namespace MyNes.Core
audio_low_pass_filter_14K = new SoundLowPassFilter(0.00815686);
audio_high_pass_filter_90 = new SoundHighPassFilter(0.999835);
audio_high_pass_filter_440 = new SoundHighPassFilter(0.996039);
audio_dc_blocker_filter = new SoundDCBlockerFilter(0.995);
apu_update_playback_func = APUUpdatePlaybackWithFilters;
}
@ -3887,15 +3866,6 @@ namespace MyNes.Core
{
joypad4 = new BlankJoypad();
}
if (shortucts == null)
{
shortucts = new BlankShortuctsHandler();
}
}
public static void SetupShortcutsHandler(IShortcutsHandler hh)
{
shortucts = hh;
}
public static void SetupControllers(IJoypadConnecter joy1, IJoypadConnecter joy2, IJoypadConnecter joy3, IJoypadConnecter joy4)
@ -5162,7 +5132,6 @@ namespace MyNes.Core
hardReset();
Tracer.WriteLine("EMU is ready.");
success = true;
emu_frame_clocking_mode = !useThread;
ON = true;
PAUSED = false;
if (useThread)
@ -5274,7 +5243,7 @@ namespace MyNes.Core
private static Stopwatch sw = new Stopwatch();
private static double fixTime;
public static int currentFrame;
public static ulong currentFrame;
private static void EmuClock()
{
while (ON)
@ -5294,6 +5263,7 @@ namespace MyNes.Core
fixTime = waitTime - GetTime();
};
currentFrame++;
continue;
}
@ -5303,7 +5273,6 @@ namespace MyNes.Core
render_audio_toggle_pause(paused: true);
}
Thread.Sleep(100);
shortucts.Update();
switch (emu_request_mode)
{
case RequestMode.HardReset:
@ -5370,7 +5339,6 @@ namespace MyNes.Core
}
isPaused = false;
ppu_frame_finished = false;
emu_frame_done = true;
joypad1.Update();
joypad2.Update();
if (IsFourPlayers)
@ -5378,7 +5346,6 @@ namespace MyNes.Core
joypad3.Update();
joypad4.Update();
}
shortucts.Update();
if (SoundEnabled)
{
render_audio_get_is_playing(out render_audio_is_playing);
@ -5391,8 +5358,6 @@ namespace MyNes.Core
audio_samples_added = 0;
audio_timer = 0.0;
}
fps_time_token = GetTime() - fps_time_start;
fps_time_start = GetTime();
}
private static double GetTime()
@ -5400,11 +5365,6 @@ namespace MyNes.Core
return (double)Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
}
public static void GetSpeedValues(out double frame_time, out double immediate_frame_time)
{
frame_time = fps_time_token;
immediate_frame_time = fps_time_frame_time;
}
public static void SetFramePeriod(ref double period)
{
@ -5490,7 +5450,7 @@ namespace MyNes.Core
{
Tracer.WriteLine("Palette set to load from file.");
var paletteFileStream = MyNesMain.FileManager.OpenPaletteFile();
var paletteFileStream = MyNesMain.Supporter.OpenPaletteFile();
if (paletteFileStream != null)
{
PaletteFileWrapper.LoadFile(paletteFileStream, out var palette);

View File

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

View File

@ -0,0 +1,7 @@
namespace MyNes.Core
{
public enum EnumJoyIndex : byte
{
P1, P2, P3, P4
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f25db9f5a7339c34f94e6e978be38c82
guid: d0185abce4b430d478095a8d5621aef1
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,7 @@
namespace MyNes.Core
{
public enum EnumKeyKind
{
Up, Down, Left, Right, Select, Start, B, A, TurboB, TurboA
}
}

View File

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

View File

@ -0,0 +1,13 @@
using System.IO;
namespace MyNes.Core
{
public interface IExternalSupporter
{
string GetWorkingFolderPath();
public Stream OpenDatabaseFile();
public Stream OpenPaletteFile();
public Stream OpenRomFile(string path);
public bool IsKeyPressing(EnumJoyIndex index,EnumKeyKind key);
}
}

View File

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

View File

@ -1,5 +1,48 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &319390252125274553
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3543948837876491845}
- component: {fileID: 2496653285840897638}
m_Layer: 0
m_Name: Input
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3543948837876491845
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 319390252125274553}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2496653285840897638
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 319390252125274553}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d79c33962dea7dc48b2c5fcd45afe1ad, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8662582774585465456
GameObject:
m_ObjectHideFlags: 0
@ -17,7 +60,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &8662582774585465455
RectTransform:
m_ObjectHideFlags: 0
@ -25,11 +68,11 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8662582774585465456}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: -1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775350046794}
m_Father: {fileID: 8662582775439058149}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@ -80,7 +123,7 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8662582774971523581}
- component: {fileID: 3209572454846341542}
- component: {fileID: 8662582774971523580}
- component: {fileID: 8662582774971523579}
m_Layer: 5
@ -90,8 +133,8 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8662582774971523581
RectTransform:
--- !u!4 &3209572454846341542
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
@ -99,16 +142,11 @@ RectTransform:
m_GameObject: {fileID: 8662582774971523582}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: -1, z: 1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775439058149}
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!82 &8662582774971523580
AudioSource:
m_ObjectHideFlags: 0
@ -227,18 +265,18 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8662582775350046794}
- component: {fileID: 7667066390141474019}
- component: {fileID: 8662582775350046791}
- component: {fileID: 8662582775350046790}
m_Layer: 5
m_Name: video
m_Name: Video
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8662582775350046794
RectTransform:
--- !u!4 &7667066390141474019
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
@ -248,16 +286,10 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 8662582774585465455}
- {fileID: 8662582775359084755}
m_Father: {fileID: 8662582775439058149}
- {fileID: 8662582775439058149}
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &8662582775350046791
CanvasRenderer:
m_ObjectHideFlags: 0
@ -306,11 +338,11 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8662582775359084756}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775350046794}
m_Father: {fileID: 8662582775439058149}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0}
@ -390,9 +422,9 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 8662582775350046794}
- {fileID: 8662582774971523581}
m_Father: {fileID: 8662582775964487076}
- {fileID: 8662582774585465455}
- {fileID: 8662582775359084755}
m_Father: {fileID: 7667066390141474019}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@ -489,7 +521,9 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 8662582775439058149}
- {fileID: 7667066390141474019}
- {fileID: 3209572454846341542}
- {fileID: 3543948837876491845}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -507,3 +541,4 @@ MonoBehaviour:
m_EditorClassIdentifier:
VideoCom: {fileID: 8662582775350046790}
AudioCom: {fileID: 8662582774971523579}
InputManager: {fileID: 2496653285840897638}

View File

@ -1,183 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class DefaultAudioOutput : MonoBehaviour
{
public float Gain = 0.05f;
private int _samplesAvailable;
private PipeStream _pipeStream;
private byte[] _buffer;
void Awake()
{
// Get Unity Buffer size
int bufferLength = 0, numBuffers = 0;
AudioSettings.GetDSPBufferSize(out bufferLength, out numBuffers);
_samplesAvailable = bufferLength;
// Prepare our buffer
_pipeStream = new PipeStream();
_pipeStream.MaxBufferLength = _samplesAvailable * 2 * 2;
_buffer = new byte[_samplesAvailable * 2];
}
List<byte> waveBytes = new List<byte>();
void OnAudioFilterRead(float[] data, int channels)
{
// This method is not called if you don't own Unity PRO.
if (_buffer.Length != data.Length)
{
Debug.Log("Does DSPBufferSize or speakerMode changed? Audio disabled.");
return;
}
int r = _pipeStream.Read(_buffer, 0, data.Length);
for (int i = 0; i < r; ++i)
{
var normalize = (sbyte)(_buffer[i]) / 127f;
data[i] = Gain * normalize;
waveBytes.Add(_buffer[i]);
}
}
private void OnDestroy()
{
File.WriteAllBytes("e:/wav.wav", waveBytes.ToArray());
}
public int GetOutputSampleRate()
{
return AudioSettings.outputSampleRate;
}
public int GetSamplesAvailable()
{
return _samplesAvailable;
}
public void Play(byte[] data)
{
_pipeStream.Write(data, 0, data.Length);
}
}
public class PipeStream : Stream
{
private readonly Queue<byte> _buffer = new Queue<byte>();
private long _maxBufferLength = 8192;
public long MaxBufferLength
{
get { return _maxBufferLength; }
set { _maxBufferLength = value; }
}
public new void Dispose()
{
_buffer.Clear();
}
public override void Flush()
{
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (offset != 0)
throw new NotImplementedException("Offsets with value of non-zero are not supported");
if (buffer == null)
throw new ArgumentException("Buffer is null");
if (offset + count > buffer.Length)
throw new ArgumentException("The sum of offset and count is greater than the buffer length. ");
if (offset < 0 || count < 0)
throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
if (count == 0)
return 0;
int readLength = 0;
lock (_buffer)
{
// fill the read buffer
for (; readLength < count && Length > 0; readLength++)
{
buffer[readLength] = _buffer.Dequeue();
}
}
return readLength;
}
private bool ReadAvailable(int count)
{
return (Length >= count);
}
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentException("Buffer is null");
if (offset + count > buffer.Length)
throw new ArgumentException("The sum of offset and count is greater than the buffer length. ");
if (offset < 0 || count < 0)
throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
if (count == 0)
return;
lock (_buffer)
{
while (Length >= _maxBufferLength)
return;
// queue up the buffer data
foreach (byte b in buffer)
{
_buffer.Enqueue(b);
}
}
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return _buffer.Count; }
}
public override long Position
{
get { return 0; }
set { throw new NotImplementedException(); }
}
}

View File

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

View File

@ -0,0 +1,43 @@
using MyNes.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public class InputManager : MonoBehaviour
{
private KeyMapper m_p1Mapper = new LocalKeyMapper();
private KeyMapper m_p2Mapper = new NetKeyMapper();
private KeyMapper m_p3Mapper = new NetKeyMapper();
private KeyMapper m_p4Mapper = new NetKeyMapper();
private void Awake()
{
m_p1Mapper.Init();
m_p2Mapper.Init();
m_p3Mapper.Init();
m_p4Mapper.Init();
}
private void Update()
{
m_p1Mapper.Update();
m_p2Mapper.Update();
m_p3Mapper.Update();
m_p4Mapper.Update();
}
public bool IsKeyPress(EnumJoyIndex joyIndex, EnumKeyKind keyKind)
{
switch (joyIndex)
{
case EnumJoyIndex.P1: return m_p1Mapper.IsPressing(keyKind);
case EnumJoyIndex.P2: return m_p2Mapper.IsPressing(keyKind);
case EnumJoyIndex.P3: return m_p3Mapper.IsPressing(keyKind);
case EnumJoyIndex.P4: return m_p4Mapper.IsPressing(keyKind);
default: return default;
}
}
}
}

View File

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

View File

@ -0,0 +1,15 @@
using MyNes.Core;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public abstract class KeyMapper
{
public abstract void Init();
public abstract void Update();
public abstract bool IsPressing(EnumKeyKind keyKind);
}
}

View File

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

View File

@ -0,0 +1,72 @@
using MyNes.Core;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public class LocalKeyMapper : KeyMapper
{
private Dictionary<KeyCode, EnumKeyKind> m_mapper = new Dictionary<KeyCode, EnumKeyKind>();
private Dictionary<EnumKeyKind, KeyCode> m_mapperOpp = new Dictionary<EnumKeyKind, KeyCode>();
private Dictionary<EnumKeyKind, int> m_keyIndexTable = new Dictionary<EnumKeyKind, int>();
private EnumKeyKind[] m_focusKeys;
private bool[] m_keyStates;
public override void Init()
{
SetKeyMapper(KeyCode.W, EnumKeyKind.Up);
SetKeyMapper(KeyCode.S, EnumKeyKind.Down);
SetKeyMapper(KeyCode.A, EnumKeyKind.Left);
SetKeyMapper(KeyCode.D, EnumKeyKind.Right);
SetKeyMapper(KeyCode.V, EnumKeyKind.Select);
SetKeyMapper(KeyCode.B, EnumKeyKind.Start);
SetKeyMapper(KeyCode.J, EnumKeyKind.B);
SetKeyMapper(KeyCode.K, EnumKeyKind.A);
SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB);
SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA);
SetComplete();
}
void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey)
{
if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射
{
m_mapperOpp.Remove(joyKey);
m_mapper.Remove(keyCode);
}
m_mapper[inputKeycode] = joyKey;
m_mapperOpp[joyKey] = inputKeycode;
}
void SetComplete()
{
m_focusKeys = m_mapperOpp.Keys.ToArray();
m_keyStates = new bool[m_focusKeys.Length];
m_keyIndexTable.Clear();
for (int i = 0; i < m_focusKeys.Length; i++)
{
m_keyIndexTable[m_focusKeys[i]] = i;
}
}
public override void Update()
{
if (m_focusKeys == null) return;
for (int i = 0; i < m_focusKeys.Length; i++)
{
var keyCode = m_mapperOpp[m_focusKeys[i]];
m_keyStates[i] = UnityEngine.Input.GetKey(keyCode);
}
}
public override bool IsPressing(EnumKeyKind keyKind)
{
if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false
return m_keyStates[index];
}
}
}

View File

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

View File

@ -0,0 +1,62 @@
using MyNes.Core;
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
namespace AxibugEmuOnline.Client
{
public class NesJoyController : IJoypadConnecter
{
private EnumJoyIndex m_joyIndex;
private bool turbo;
public NesJoyController(EnumJoyIndex joyIndex)
{
m_joyIndex = joyIndex;
}
public override void Update()
{
turbo = !turbo;
DATA = 0;
var state = MyNesMain.Supporter;
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.A))
{
DATA |= 1;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.B))
{
DATA |= 2;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboA) && turbo)
{
DATA |= 1;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.TurboB) && turbo)
{
DATA |= 2;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Select))
{
DATA |= 4;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Start))
{
DATA |= 8;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Up))
{
DATA |= 16;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Down))
{
DATA |= 32;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Left))
{
DATA |= 64;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Right))
{
DATA |= 128;
}
}
}
}

View File

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

View File

@ -0,0 +1,24 @@
using AxibugEmuOnline.Client.Input;
using MyNes.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class NetKeyMapper : KeyMapper
{
public override void Init()
{
}
public override void Update()
{
}
public override bool IsPressing(EnumKeyKind keyKind)
{
return false;
}
}
}

View File

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

View File

@ -32,7 +32,6 @@ namespace AxibugEmuOnline.Client
public void Initialize()
{
m_rawBufferWarper = new Texture2D(256, 240);
//m_drawCanvas.texture = RenderTexture.GetTemporary(256, 240, 0, UnityEngine.Experimental.Rendering.GraphicsFormat.B8G8R8A8_SRGB);
}
public void GetColor(uint value, ref Color res)

View File

@ -1,17 +1,31 @@
using MyNes;
using AxibugEmuOnline.Client.Input;
using MyNes.Core;
using System.IO;
using UnityEngine;
namespace AxibugEmuOnline.Client.Manager
{
public class AppEmu : IFileManager
public class AppEmu : IExternalSupporter
{
public void Init(IVideoProvider videoCom, IAudioProvider audioCom)
{
MyNesMain.Initialize(this, videoCom, audioCom);
private InputManager m_inputMgr;
NesEmu.LoadGame("kirby.nes", out var successed, true);
public void Init(IVideoProvider videoCom, IAudioProvider audioCom, InputManager inputManager)
{
m_inputMgr = inputManager;
MyNesMain.Initialize(this, videoCom, audioCom);
NesEmu.SetupControllers(
new NesJoyController(EnumJoyIndex.P1),
new NesJoyController(EnumJoyIndex.P2),
new NesJoyController(EnumJoyIndex.P3),
new NesJoyController(EnumJoyIndex.P4));
}
public bool LoadGame(string romName)
{
NesEmu.LoadGame(romName, out var successed, true);
return successed;
}
public void Dispose()
@ -44,5 +58,10 @@ namespace AxibugEmuOnline.Client.Manager
MemoryStream ms = new MemoryStream(ta.bytes);
return ms;
}
public bool IsKeyPressing(EnumJoyIndex index, EnumKeyKind key)
{
return m_inputMgr.IsKeyPress(index, key);
}
}
}

View File

@ -1,3 +1,4 @@
using AxibugEmuOnline.Client.Input;
using AxibugEmuOnline.Client.Manager;
using MyNes.Core;
using System.IO;
@ -10,12 +11,15 @@ namespace AxibugEmuOnline.Client
{
public UguiVideoProvider VideoCom;
public AudioProvider AudioCom;
public InputManager InputManager;
private AppEmu m_appEnum = new AppEmu();
private void Start()
{
m_appEnum.Init(VideoCom, AudioCom);
m_appEnum.Init(VideoCom, AudioCom, InputManager);
m_appEnum.LoadGame("kirby.nes");
}
private void OnDestroy()