Skip to content

Commit

Permalink
Merge pull request #38 from mkdasher/dev
Browse files Browse the repository at this point in the history
merge for 1.0.9
  • Loading branch information
Madghostek authored Aug 15, 2021
2 parents 763c9a7 + 9fe0502 commit b281e77
Show file tree
Hide file tree
Showing 39 changed files with 1,203 additions and 410 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### ![#f03c15](https://via.placeholder.com/15/f03c15/000000?text=+) **Experimental - Expect instability**
# Mupen64 Lua

[![Release](https://img.shields.io/github/v/release/mkdasher/mupen64-rr-lua-?label=Release)](https://github.com/mkdasher/mupen64-rr-lua-/releases)
Expand All @@ -8,7 +9,7 @@
[comment]: <> (Second image has to be inline so another approach is used)
[**__\>\> Latest Release \<\<__**](https://github.com/mkdasher/mupen64-rr-lua-/releases/latest/)<br>

This repository contains source code for continued development of Mupen64 - Nintendo 64 emulator with TAS support, and TASinput plugin.
This repository contains the latest source code for continued development of Mupen64 - Nintendo 64 emulator with TAS support, and TASInput plugin.

This version includes <a href="https://imgur.com/a/SA2CgEB" target="_blank">new Features such as: AVISplit, Reset recording, WiiVC and Backwards Compatibility</a> options all in one.

Expand Down
144 changes: 133 additions & 11 deletions lua/LuaConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#include "../main/savestates.h"
#include "../main/win/Config.h"
#include <vcr.h>

#include <gdiplus.h>

#ifdef LUA_CONSOLE

//nice msvc pragma smh
#pragma comment(lib, "lua54.lib")
#pragma comment (lib,"Gdiplus.lib")


extern unsigned long op;
Expand All @@ -49,6 +50,8 @@ bool traceLogMode;
bool enablePCBreak;
bool maximumSpeedMode;
bool anyLuaRunning = false;
bool gdiPlusInitialized = false;
ULONG_PTR gdiPlusToken;

#define DEBUG_GETLASTERROR 0//if(GetLastError()){ShowInfo("Line:%d GetLastError:%d",__LINE__,GetLastError());SetLastError(0);}

Expand Down Expand Up @@ -150,7 +153,6 @@ struct EmulationLock{
}
};

void runLUA(HWND wnd, char path[]); // bad solution
void ConsoleWrite(HWND, const char*);
void SetWindowLua(HWND wnd, Lua *lua);
void SetButtonState(HWND wnd, bool state);
Expand Down Expand Up @@ -205,8 +207,12 @@ class Lua {
hMutex = CreateMutex(0, 0, 0);
newLuaState();
runFile(path);
if(isrunning())
if (isrunning())
{
SetButtonState(ownWnd, true);
AddToRecentScripts(path);
strcpy(Config.LuaScriptPath, path);
}
ShowInfo("Lua run");
}
void stop() {
Expand Down Expand Up @@ -335,7 +341,6 @@ class Lua {
void runFile(char *path) {
//int GetErrorMessage(lua_State *L);
int result;
SetButtonState(ownWnd, true);
//lua_pushcfunction(L, GetErrorMessage);
result = luaL_dofile(L, path);
if(result) {
Expand Down Expand Up @@ -636,11 +641,14 @@ INT_PTR CALLBACK DialogProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return TRUE;
}
case WM_CLOSE:

if (Config.LuaWarnOnClose
&& (MessageBox(0, "Are you sure you want to close this dialog and terminate this lua script instance?", "Confirm closing", MB_TASKMODAL | MB_TOPMOST | MB_YESNO | MB_ICONQUESTION) == IDNO))
return TRUE;

DestroyWindow(wnd);
return TRUE;
case WM_DESTROY:{
GetWindowText(GetDlgItem(wnd, IDC_TEXTBOX_LUASCRIPTPATH),
Config.LuaScriptPath, MAX_PATH);
LuaMessage::Msg *msg = new LuaMessage::Msg();
msg->type = LuaMessage::DestroyLua;
msg->destroyLua.wnd = wnd;
Expand All @@ -657,8 +665,10 @@ INT_PTR CALLBACK DialogProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return FALSE;
}
std::string OpenLuaFileDialog() {
EmulationLock lock;


int storePaused = emu_paused;
pauseEmu(1);

OPENFILENAME ofn;
char filename[MAX_PATH] = "";

Expand All @@ -676,8 +686,13 @@ std::string OpenLuaFileDialog() {
ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
ofn.lpstrInitialDir = NULL;

if(!GetOpenFileName(&ofn))
if (!GetOpenFileName(&ofn)) {
if (!storePaused) resumeEmu(1);
return "";
}

if (!storePaused) resumeEmu(1);

return ofn.lpstrFile;
}
void SetButtonState(HWND wnd, bool state) {
Expand Down Expand Up @@ -710,6 +725,7 @@ BOOL WmCommand(HWND wnd, WORD id, WORD code, HWND control){
//strcpy(Config.LuaScriptPath, msg->runPath.path);
anyLuaRunning = true;
luaMessage.post(msg);
shouldSave = true;
return TRUE;
}
case IDC_BUTTON_LUASTOP: {
Expand Down Expand Up @@ -755,9 +771,13 @@ void CreateLuaWindow(void(*callback)()) {
if(!luaDC) {
InitializeLuaDC(mainHWND);
}

int LuaWndId = Config.LuaSimpleDialog ? IDD_LUAWINDOW_SIMPLIFIED : IDD_LUAWINDOW;

HWND wnd = CreateDialogParam(app_hInstance,
MAKEINTRESOURCE(IDD_LUAWINDOW), mainHWND, DialogProc,
MAKEINTRESOURCE(LuaWndId), mainHWND, DialogProc,
(LPARAM)callback);

ShowWindow(wnd, SW_SHOW); //タブストップ利かないのと同じ原因だと思う
}
void ConsoleWrite(HWND wnd, const char *str) {
Expand Down Expand Up @@ -2067,6 +2087,102 @@ int DrawRect(lua_State *L) {
luaL_checknumber(L, 3), luaL_checknumber(L, 4));
return 0;
}

VOID checkGDIPlusInitialized() {
// will be inlined by compiler
if (!gdiPlusInitialized) {
printf("lua initialize gdiplus\n");
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&gdiPlusToken, &gdiplusStartupInput, NULL);
gdiPlusInitialized = true;
}
}

int FillPolygonAlpha(lua_State* L) {

checkGDIPlusInitialized();
Lua* lua = GetLuaClass(L);

Gdiplus::PointF pt1;
Gdiplus::PointF pt2;
Gdiplus::PointF pt3;
byte a, r, g, b;

pt1.X = luaL_checknumber(L, 1);
pt1.Y = luaL_checknumber(L, 2);

pt2.X = luaL_checknumber(L, 3);
pt2.Y = luaL_checknumber(L, 4);

pt3.X = luaL_checknumber(L, 5);
pt3.Y = luaL_checknumber(L, 6);

a = luaL_checknumber(L, 7);
r = luaL_checknumber(L, 8);
g = luaL_checknumber(L, 9);
b = luaL_checknumber(L, 10);

Gdiplus::Graphics gfx(luaDC);
Gdiplus::SolidBrush brush(Gdiplus::Color(a, r, g, b));

Gdiplus::PointF pts[3] = {pt1,pt2,pt3};
gfx.FillPolygon(&brush, pts, 1);


return 0;
}


int FillEllipseAlpha(lua_State* L) {
checkGDIPlusInitialized();
Lua* lua = GetLuaClass(L);

int left, top, right, bottom;
byte a, r, g, b;

bottom = luaL_checknumber(L, 1);
left = luaL_checknumber(L, 2);
right = luaL_checknumber(L, 3);
top = luaL_checknumber(L, 4);

a = luaL_checknumber(L, 5);
r = luaL_checknumber(L, 6);
g = luaL_checknumber(L, 7);
b = luaL_checknumber(L, 8);

Gdiplus::Graphics gfx(luaDC);
Gdiplus::SolidBrush brush(Gdiplus::Color(a, r, g, b));

gfx.FillEllipse(&brush, left, top, right, bottom);

return 0;
}
int FillRectAlpha(lua_State* L)
{
checkGDIPlusInitialized();

Lua* lua = GetLuaClass(L);

int left, top, right, bottom;
byte a, r, g, b;

bottom = luaL_checknumber(L, 1);
left = luaL_checknumber(L, 2);
right = luaL_checknumber(L, 3);
top = luaL_checknumber(L, 4);

a = luaL_checknumber(L, 5);
r = luaL_checknumber(L, 6);
g = luaL_checknumber(L, 7);
b = luaL_checknumber(L, 8);

Gdiplus::Graphics gfx(luaDC);
Gdiplus::SolidBrush brush(Gdiplus::Color(a, r, g, b));

gfx.FillRectangle(&brush, left, top, right, bottom);

return 0;
}
int FillRect(lua_State* L) {
/*
(Info)
Expand Down Expand Up @@ -2994,7 +3110,13 @@ const luaL_Reg wguiFuncs[] = {
{"text", TextOut},
{"drawtext", DrawText},
{"rect", DrawRect},
{"fillrect", FillRect}, // Experimental
{"fillrect", FillRect},
/*<GDIPlus>*/
// GDIPlus functions marked with "a" suffix
{"fillrecta", FillRectAlpha},
{"fillellipsea", FillEllipseAlpha},
{"fillpolygona", FillPolygonAlpha},
/*</GDIPlus*/
{"ellipse", DrawEllipse},
{"polygon", DrawPolygon},
{"line", DrawLine},
Expand Down
2 changes: 1 addition & 1 deletion lua/LuaConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern bool traceLogMode;
extern bool enablePCBreak;
extern bool maximumSpeedMode;
extern bool anyLuaRunning;

extern unsigned long gdiPlusToken;

#endif

Expand Down
104 changes: 104 additions & 0 deletions lua/Recent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//Based on recent roms but better
#include "Recent.h"
#include "LuaConsole.h"

#ifdef _WIN32
#include <windows.h>
#include "win/main_win.h"
#include "../winproject/resource.h" //for menu id
#endif

BOOL freezeRecentScript;

//takes config and puts the entries to menu
void BuildRecentScriptsMenu(HWND hwnd) {
int i;
bool empty = false;
MENUITEMINFO menuinfo = { 0 };
HMENU hMenu = GetMenu(hwnd);
HMENU hSubMenu = GetSubMenu(hMenu, 5);
hSubMenu = GetSubMenu(hSubMenu, 2);
//DeleteMenu(hSubMenu, ID_LUA_RECENT, MF_BYCOMMAND); //remove the "no recent scripts" entry, add later if in fact no recent

menuinfo.cbSize = sizeof(MENUITEMINFO);
menuinfo.fMask = MIIM_TYPE | MIIM_ID;
menuinfo.fType = MFT_STRING;
menuinfo.fState = MFS_ENABLED;
for (i = 0; i < LUA_MAX_RECENT; i++) {
if (strcmp(Config.RecentScripts[i], "") == 0)
{
if (i == 0)
{
menuinfo.dwTypeData = "No recent scripts";
empty = true;
}
else break;
}
else
menuinfo.dwTypeData = Config.RecentScripts[i];


menuinfo.cch = strlen(menuinfo.dwTypeData);
menuinfo.wID = ID_LUA_RECENT + i;
InsertMenuItem(hSubMenu, i+3, TRUE, &menuinfo);
if (empty) EnableMenuItem(hSubMenu, ID_LUA_RECENT, MF_DISABLED);
}
}

void ClearRecent(BOOL clear_array) {
int i;
HMENU hMenu;

hMenu = GetMenu(mainHWND);
for (i = 0; i < LUA_MAX_RECENT; i++) {
DeleteMenu(hMenu, ID_LUA_RECENT + i, MF_BYCOMMAND);
}
if (clear_array) {
memset(Config.RecentScripts, 0, LUA_MAX_RECENT * sizeof(Config.RecentScripts[0]));
}

}

void RefreshRecent()
{
//nuke the menu
ClearRecent(FALSE);
//rebuild
BuildRecentScriptsMenu(mainHWND);


}


//Adds path to recent paths, if already in list then just moves things around
//newer scripts are earlier in array
void AddToRecentScripts(char* path)
{
if (Config.RecentScriptsFreeze) return; // fuck off?

int i = 0;
//Either finds index of path in recent list, or stops at last one
//notice how it doesn't matter if last==path or not, we do same swapping later
for (; i<LUA_MAX_RECENT-1; ++i)
{
//if matches or empty (list is not full), break
if (Config.RecentScripts[i][0]==0 || !strcmp(Config.RecentScripts[i], path)) break;
}
//now swap all elements backwards starting from `i`
for (int j = i; j>0; --j)
{
strcpy(Config.RecentScripts[j], Config.RecentScripts[j-1]);
}
//now write to top
strcpy(Config.RecentScripts[0], path);
//rebuild menu
RefreshRecent();
}

void RunRecentScript(WORD menuItem)
{
char path[MAX_PATH];
int index = menuItem - ID_LUA_RECENT;
sprintf(path, Config.RecentScripts[index]);
LuaOpenAndRun(path);
}
12 changes: 12 additions & 0 deletions lua/Recent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#ifdef _WIN32
#include <windows.h> //linux trolled
// lol
#endif
#define LUA_MAX_RECENT 5

//functions
void AddToRecentScripts(char* path);
void BuildRecentScriptsMenu(HWND);
void RunRecentScript(WORD);
void ClearRecent(BOOL);
12 changes: 12 additions & 0 deletions main/debughook.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//MSVC ONLY
#include "debughook.h"
#undef fwrite
size_t fwrite2(const void* ptr, size_t size, size_t count, FILE* stream){
printf("Writing %d bytes: " ,size*count);
for (int i = 0; i < size * count; i++)
{
printf("%hhx", ((const char*)ptr)[i]);
}
printf("\n");
return fwrite(ptr, size, count, stream);
}
Loading

0 comments on commit b281e77

Please sign in to comment.