2024-08-30 17:33:22 +08:00
|
|
|
|
using AxiReplay;
|
|
|
|
|
using MAME.Core;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
using System;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
using System.Text;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class UMAME : MonoBehaviour
|
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static UMAME instance { get; private set; }
|
|
|
|
|
MAMEEmu emu;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
UniLog mUniLog;
|
|
|
|
|
UniMouse mUniMouse;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
[HideInInspector]
|
|
|
|
|
public UniVideoPlayer mUniVideoPlayer;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
UniSoundPlayer mUniSoundPlayer;
|
|
|
|
|
UniKeyboard mUniKeyboard;
|
|
|
|
|
UniResources mUniResources;
|
|
|
|
|
public Text mFPS;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
public Button btnStop;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
public Button btnStart;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public Button btnRePlay;
|
|
|
|
|
public Button btnRePayySave;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
public Button btnRomDir;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public Button btnSaveState;
|
|
|
|
|
public Button btnLoadState;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
public Dictionary<string, RomInfo> ALLGame;
|
|
|
|
|
public List<RomInfo> HadGameList = new List<RomInfo>();
|
2024-08-08 16:24:18 +08:00
|
|
|
|
string mChangeRomName = string.Empty;
|
|
|
|
|
public bool bQuickTestRom = false;
|
|
|
|
|
public string mQuickTestRom = string.Empty;
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public ReplayWriter mReplayWriter;
|
|
|
|
|
public ReplayReader mReplayReader;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
|
|
|
|
Dropdown optionDropdown;
|
|
|
|
|
|
|
|
|
|
public static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
|
2024-08-08 16:24:18 +08:00
|
|
|
|
public static bool bInGame { get; private set; }
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR_WIN
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static string EmuDataPath => "G:/MAME.Core";
|
2024-07-30 17:38:24 +08:00
|
|
|
|
#elif UNITY_ANDROID
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static string EmuDataPath => Application.persistentDataPath;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
#elif UNITY_PSP2
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static string EmuDataPath => Application.dataPath;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
#else
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static string EmuDataPath => Application.persistentDataPath;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2024-08-30 17:33:22 +08:00
|
|
|
|
public static string RomPath => EmuDataPath + "/roms/";
|
|
|
|
|
public static string SavePath => EmuDataPath + "/sav/";
|
|
|
|
|
|
2024-07-30 17:38:24 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
// ǿ<>ƺ<EFBFBD><C6BA><EFBFBD>
|
|
|
|
|
Screen.orientation = ScreenOrientation.LandscapeLeft;
|
|
|
|
|
|
|
|
|
|
instance = this;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
mFPS = GameObject.Find("FPS").GetComponent<Text>();
|
|
|
|
|
optionDropdown = GameObject.Find("optionDropdown").GetComponent<Dropdown>();
|
2024-08-30 17:33:22 +08:00
|
|
|
|
emu = new MAMEEmu();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
mUniLog = new UniLog();
|
|
|
|
|
mUniMouse = this.gameObject.AddComponent<UniMouse>();
|
|
|
|
|
mUniVideoPlayer = this.gameObject.AddComponent<UniVideoPlayer>();
|
|
|
|
|
mUniSoundPlayer = GameObject.Find("Audio").transform.GetComponent<UniSoundPlayer>();
|
|
|
|
|
mUniKeyboard = this.gameObject.AddComponent<UniKeyboard>();
|
|
|
|
|
mUniResources = new UniResources();
|
2024-08-08 16:24:18 +08:00
|
|
|
|
mChangeRomName = UniMAMESetting.instance.LastGameRom;
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
2024-08-30 17:33:22 +08:00
|
|
|
|
emu.Init(RomPath, mUniLog, mUniResources, mUniVideoPlayer, mUniSoundPlayer, mUniKeyboard.mKeyCodeCore, mUniMouse);
|
2024-08-08 16:24:18 +08:00
|
|
|
|
ALLGame = emu.GetGameList();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
|
|
|
|
|
Debug.Log($"ALLGame:{ALLGame.Count}");
|
2024-08-08 16:24:18 +08:00
|
|
|
|
|
|
|
|
|
#if !UNITY_EDITOR
|
|
|
|
|
bQuickTestRom = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (bQuickTestRom)
|
|
|
|
|
mChangeRomName = mQuickTestRom;
|
|
|
|
|
|
2024-08-07 18:23:55 +08:00
|
|
|
|
GetHadRomList();
|
2024-08-08 16:24:18 +08:00
|
|
|
|
|
|
|
|
|
if (bQuickTestRom)
|
|
|
|
|
LoadGame();
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
2024-08-07 18:23:55 +08:00
|
|
|
|
btnStop.onClick.AddListener(StopGame);
|
2024-08-30 17:33:22 +08:00
|
|
|
|
btnStart.onClick.AddListener(() => { LoadGame(false); });
|
|
|
|
|
btnRePlay.onClick.AddListener(() => { LoadGame(true); });
|
|
|
|
|
btnRePayySave.onClick.AddListener(() => SaveReplay());
|
|
|
|
|
btnSaveState.onClick.AddListener(SaveState);
|
|
|
|
|
btnLoadState.onClick.AddListener(LoadState);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:33:22 +08:00
|
|
|
|
void OnDisable()
|
2024-07-30 17:38:24 +08:00
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
StopGame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LoadGame(bool bReplay = false)
|
|
|
|
|
{
|
|
|
|
|
mReplayWriter = new ReplayWriter(mChangeRomName, "fuck", ReplayData.ReplayFormat.FM32IP64, Encoding.UTF8);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
mChangeRomName = HadGameList[optionDropdown.value].Name;
|
2024-08-08 16:24:18 +08:00
|
|
|
|
UniMAMESetting.instance.LastGameRom = mChangeRomName;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
StopGame();
|
2024-08-08 16:24:18 +08:00
|
|
|
|
//<2F><>ȡROM
|
|
|
|
|
emu.LoadRom(mChangeRomName);
|
|
|
|
|
//<2F><>ȡ<EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
if (emu.bRom)
|
|
|
|
|
{
|
2024-08-30 17:33:22 +08:00
|
|
|
|
if (bReplay)
|
|
|
|
|
{
|
|
|
|
|
string Path = SavePath + Machine.sName + ".rp";
|
|
|
|
|
mReplayReader = new ReplayReader(Path);
|
|
|
|
|
mUniKeyboard.mKeyCodeCore.SetRePlay(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 16:24:18 +08:00
|
|
|
|
//<2F><>ȡROM֮<4D><D6AE><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>߳<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
emu.GetGameScreenSize(out int _width, out int _height, out IntPtr _framePtr);
|
|
|
|
|
Debug.Log($"_width->{_width}, _height->{_height}, _framePtr->{_framePtr}");
|
|
|
|
|
mUniVideoPlayer.Initialize(_width, _height, _framePtr);
|
|
|
|
|
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ƶ
|
|
|
|
|
mUniSoundPlayer.Initialize();
|
|
|
|
|
//<2F><>ʼ<EFBFBD><CABC>Ϸ
|
|
|
|
|
emu.StartGame();
|
|
|
|
|
bInGame = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-07-30 17:38:24 +08:00
|
|
|
|
{
|
2024-08-08 16:24:18 +08:00
|
|
|
|
Debug.Log($"ROM<4F><4D><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
mFPS.text = ($"fpsv {mUniVideoPlayer.videoFPS.ToString("F2")} fpsa {mUniSoundPlayer.audioFPS.ToString("F2")}");
|
2024-08-30 17:33:22 +08:00
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.F1))
|
|
|
|
|
{
|
|
|
|
|
SaveReplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.F2))
|
|
|
|
|
{
|
|
|
|
|
string Path = SavePath + Machine.sName + ".rp";
|
|
|
|
|
string dbgPath = SavePath + Machine.sName + ".rpread";
|
|
|
|
|
mReplayReader = new ReplayReader(Path, true, dbgPath);
|
|
|
|
|
mUniKeyboard.mKeyCodeCore.Init(mUniKeyboard, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SaveReplay()
|
|
|
|
|
{
|
|
|
|
|
string Path = SavePath + Machine.sName + ".rp";
|
|
|
|
|
string dbgPath = SavePath + Machine.sName + ".rpwrite";
|
|
|
|
|
mReplayWriter.SaveData(Path, true, dbgPath);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 18:23:55 +08:00
|
|
|
|
void StopGame()
|
|
|
|
|
{
|
2024-08-08 16:24:18 +08:00
|
|
|
|
if (bInGame)
|
2024-08-07 18:23:55 +08:00
|
|
|
|
{
|
2024-08-08 16:24:18 +08:00
|
|
|
|
emu.StopGame();
|
|
|
|
|
mUniVideoPlayer.StopVideo();
|
|
|
|
|
mUniSoundPlayer.StopPlay();
|
|
|
|
|
bInGame = false;
|
2024-08-07 18:23:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 17:38:24 +08:00
|
|
|
|
void GetHadRomList()
|
|
|
|
|
{
|
|
|
|
|
HadGameList.Clear();
|
|
|
|
|
optionDropdown.options.Clear();
|
|
|
|
|
|
2024-08-30 17:33:22 +08:00
|
|
|
|
Debug.Log($"GetHadRomList:{RomPath}");
|
|
|
|
|
string[] directoryEntries = Directory.GetDirectories(RomPath);
|
2024-07-30 17:38:24 +08:00
|
|
|
|
for (int i = 0; i < directoryEntries.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string path = directoryEntries[i];
|
|
|
|
|
string dirName = Path.GetFileName(path);
|
|
|
|
|
if (ALLGame.ContainsKey(dirName))
|
2024-08-30 17:33:22 +08:00
|
|
|
|
{
|
2024-07-30 17:38:24 +08:00
|
|
|
|
HadGameList.Add(ALLGame[dirName]);
|
|
|
|
|
optionDropdown.options.Add(new Dropdown.OptionData(dirName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Debug.Log($"HadGameList:{HadGameList.Count}");
|
|
|
|
|
|
|
|
|
|
RomInfo tempCurrRom = HadGameList.Where(w => w.Name == mChangeRomName).FirstOrDefault();
|
|
|
|
|
if (tempCurrRom != null)
|
|
|
|
|
{
|
|
|
|
|
optionDropdown.value = HadGameList.IndexOf(tempCurrRom);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
optionDropdown.value = 0;
|
|
|
|
|
}
|
|
|
|
|
optionDropdown.RefreshShownValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenFolderRomPath()
|
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Process.Start("explorer.exe", "/select," + RomPath);
|
|
|
|
|
}
|
2024-08-30 17:33:22 +08:00
|
|
|
|
|
|
|
|
|
void SaveState()
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(SavePath))
|
|
|
|
|
Directory.CreateDirectory(SavePath);
|
|
|
|
|
|
|
|
|
|
FileStream fs = new FileStream(SavePath + Machine.sName + ".sta", FileMode.Create);
|
|
|
|
|
BinaryWriter bw = new BinaryWriter(fs);
|
|
|
|
|
emu.SaveState(bw);
|
|
|
|
|
bw.Close();
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
|
|
|
|
byte[] screenData = UMAME.instance.mUniVideoPlayer.GetScreenImg();
|
|
|
|
|
|
|
|
|
|
FileStream fsImg = new FileStream(SavePath + Machine.sName + ".jpg", FileMode.Create);
|
|
|
|
|
fsImg.Write(screenData, 0, screenData.Length);
|
|
|
|
|
fsImg.Close();
|
|
|
|
|
}
|
|
|
|
|
void LoadState()
|
|
|
|
|
{
|
|
|
|
|
string Path = SavePath + Machine.sName + ".sta";
|
|
|
|
|
if (!File.Exists(Path))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{Path}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
BinaryReader br = new BinaryReader(fs);
|
|
|
|
|
emu.LoadState(br);
|
|
|
|
|
br.Close();
|
|
|
|
|
fs.Close();
|
|
|
|
|
}
|
2024-07-30 17:38:24 +08:00
|
|
|
|
}
|