cleanup some lua files

This commit is contained in:
adelikat 2019-11-29 16:35:21 -06:00
parent 2a12cac9e5
commit 7aa170283e
19 changed files with 109 additions and 162 deletions

View File

@ -3,6 +3,7 @@ using System.ComponentModel;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
[Description("A library for performing standard bitwise operations.")]

View File

@ -11,6 +11,8 @@ using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
using NLua;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
[Description("A library for interacting with the currently loaded emulator core")]
@ -267,37 +269,34 @@ namespace BizHawk.Client.Common
[LuaMethod("setrenderplanes", "Toggles the drawing of sprites and background planes. Set to false or nil to disable a pane, anything else will draw them")]
public void SetRenderPlanes(params bool[] luaParam)
{
if (Emulator is NES)
if (Emulator is NES nes)
{
// in the future, we could do something more arbitrary here.
// but this isn't any worse than the old system
var nes = Emulator as NES;
var s = nes.GetSettings();
s.DispSprites = luaParam[0];
s.DispBackground = luaParam[1];
nes.PutSettings(s);
}
else if (Emulator is QuickNES)
else if (Emulator is QuickNES quicknes)
{
var quicknes = Emulator as QuickNES;
var s = quicknes.GetSettings();
// this core doesn't support disabling BG
bool showsp = GetSetting(0, luaParam);
if (showsp && s.NumSprites == 0)
bool showSp = GetSetting(0, luaParam);
if (showSp && s.NumSprites == 0)
{
s.NumSprites = 8;
}
else if (!showsp && s.NumSprites > 0)
else if (!showSp && s.NumSprites > 0)
{
s.NumSprites = 0;
}
quicknes.PutSettings(s);
}
else if (Emulator is PCEngine)
else if (Emulator is PCEngine pce)
{
var pce = Emulator as PCEngine;
var s = pce.GetSettings();
s.ShowOBJ1 = GetSetting(0, luaParam);
s.ShowBG1 = GetSetting(1, luaParam);
@ -309,17 +308,15 @@ namespace BizHawk.Client.Common
pce.PutSettings(s);
}
else if (Emulator is SMS)
else if (Emulator is SMS sms)
{
var sms = Emulator as SMS;
var s = sms.GetSettings();
s.DispOBJ = GetSetting(0, luaParam);
s.DispBG = GetSetting(1, luaParam);
sms.PutSettings(s);
}
else if (Emulator is WonderSwan)
else if (Emulator is WonderSwan ws)
{
var ws = Emulator as WonderSwan;
var s = ws.GetSettings();
s.EnableSprites = GetSetting(0, luaParam);
s.EnableFG = GetSetting(1, luaParam);
@ -330,12 +327,7 @@ namespace BizHawk.Client.Common
private static bool GetSetting(int index, bool[] settings)
{
if (index < settings.Length)
{
return settings[index];
}
return true;
return index >= settings.Length || settings[index];
}
[LuaMethodExample("emu.yield( );")]
@ -349,24 +341,18 @@ namespace BizHawk.Client.Common
[LuaMethod("getdisplaytype", "returns the display type (PAL vs NTSC) that the emulator is currently running in")]
public string GetDisplayType()
{
if (RegionableCore != null)
{
return RegionableCore.Region.ToString();
}
return "";
return RegionableCore != null
? RegionableCore.Region.ToString()
: "";
}
[LuaMethodExample("local stemuget = emu.getboardname();")]
[LuaMethod("getboardname", "returns (if available) the board name of the loaded ROM")]
public string GetBoardName()
{
if (BoardInfo != null)
{
return BoardInfo.BoardName;
}
return "";
return BoardInfo != null
? BoardInfo.BoardName
: "";
}
[LuaMethod("getluacore", "returns the name of the Lua core currently in use")]

View File

@ -6,6 +6,8 @@ using NLua;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
[Description("A library for registering lua functions to emulator events.\n All events support multiple registered methods.\nAll registered event methods can be named and return a Guid when registered")]

View File

@ -3,6 +3,8 @@ using NLua;
using BizHawk.Emulation.Common;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
public sealed class GameInfoLuaLibrary : LuaLibraryBase
@ -22,24 +24,14 @@ namespace BizHawk.Client.Common
[LuaMethod("getromname", "returns the name of the currently loaded rom, if a rom is loaded")]
public string GetRomName()
{
if (Global.Game != null)
{
return Global.Game.Name ?? "";
}
return "";
return Global.Game?.Name ?? "";
}
[LuaMethodExample("local stgamget = gameinfo.getromhash( );")]
[LuaMethod("getromhash", "returns the hash of the currently loaded rom, if a rom is loaded")]
public string GetRomHash()
{
if (Global.Game != null)
{
return Global.Game.Hash ?? "";
}
return "";
return Global.Game?.Hash ?? "";
}
[LuaMethodExample("if ( gameinfo.indatabase( ) ) then\r\n\tconsole.log( \"returns whether or not the currently loaded rom is in the game database\" );\r\nend;")]
@ -58,24 +50,14 @@ namespace BizHawk.Client.Common
[LuaMethod("getstatus", "returns the game database status of the currently loaded rom. Statuses are for example: GoodDump, BadDump, Hack, Unknown, NotInDatabase")]
public string GetStatus()
{
if (Global.Game != null)
{
return Global.Game.Status.ToString();
}
return "";
return Global.Game?.Status.ToString();
}
[LuaMethodExample("if ( gameinfo.isstatusbad( ) ) then\r\n\tconsole.log( \"returns the currently loaded rom's game database status is considered 'bad'\" );\r\nend;")]
[LuaMethod("isstatusbad", "returns the currently loaded rom's game database status is considered 'bad'")]
public bool IsStatusBad()
{
if (Global.Game != null)
{
return Global.Game.IsRomStatusBad();
}
return true;
return Global.Game?.IsRomStatusBad() ?? true;
}
[LuaMethodExample("local stgamget = gameinfo.getboardtype( );")]

View File

@ -6,6 +6,8 @@ using NLua;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
[Description("Functions specific to GenesisHawk (functions may not run when an Genesis game is not loaded)")]
@ -24,12 +26,9 @@ namespace BizHawk.Client.Common
private GPGX.GPGXSettings GetSettings()
{
if (Genesis != null)
{
return Genesis.GetSettings();
}
return new GPGX.GPGXSettings();
return Genesis != null
? Genesis.GetSettings()
: new GPGX.GPGXSettings();
}
private void PutSettings(GPGX.GPGXSettings settings)

View File

@ -1,6 +1,7 @@
using System;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
public sealed class JoypadLuaLibrary : LuaLibraryBase

View File

@ -5,6 +5,7 @@ using NLua;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
[Description("Main memory library reads and writes from the Main memory domain (the default memory domain set by any given core)")]

View File

@ -6,6 +6,7 @@ using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Common.BufferExtensions;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
[Description("These functions behavior identically to the mainmemory functions but the user can set the memory domain to read and write from. The default domain is main memory. Use getcurrentmemorydomain(), and usememorydomain() to control which domain is used. Each core has its own set of valid memory domains. Use getmemorydomainlist() to get a list of memory domains for the current core loaded.")]

View File

@ -6,6 +6,8 @@ using NLua;
using BizHawk.Emulation.Common;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
public sealed class MemorySavestateEmuLuaLibrary : LuaLibraryBase

View File

@ -2,6 +2,7 @@
using System.IO;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
public sealed class MovieLuaLibrary : LuaLibraryBase
@ -176,9 +177,9 @@ namespace BizHawk.Client.Common
{
// Lua numbers are always double, integer precision holds up
// to 53 bits, so throw an error if it's bigger than that.
const double PrecisionLimit = 9007199254740992d;
const double precisionLimit = 9007199254740992d;
if (count > PrecisionLimit)
if (count > precisionLimit)
{
throw new Exception("Rerecord count exceeds Lua integer precision.");
}
@ -208,8 +209,8 @@ namespace BizHawk.Client.Common
{
var movie = Global.MovieSession.Movie;
var system = movie.HeaderEntries[HeaderKeys.PLATFORM];
var pal = movie.HeaderEntries.ContainsKey(HeaderKeys.PAL) &&
movie.HeaderEntries[HeaderKeys.PAL] == "1";
var pal = movie.HeaderEntries.ContainsKey(HeaderKeys.PAL)
&& movie.HeaderEntries[HeaderKeys.PAL] == "1";
return new PlatformFrameRates()[system, pal];
}

View File

@ -8,6 +8,8 @@ using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
[Description("Functions related specifically to Nes Cores")]
@ -31,7 +33,7 @@ namespace BizHawk.Client.Common
private bool NESAvailable => Neshawk != null || Quicknes != null;
private bool HasMemoryDOmains => MemoryDomains != null;
private bool HasMemoryDomains => MemoryDomains != null;
public NesLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback) { }
@ -42,7 +44,7 @@ namespace BizHawk.Client.Common
[LuaMethod("addgamegenie", "Adds the specified game genie code. If an NES game is not currently loaded or the code is not a valid game genie code, this will have no effect")]
public void AddGameGenie(string code)
{
if (NESAvailable && HasMemoryDOmains)
if (NESAvailable && HasMemoryDomains)
{
var decoder = new NESGameGenieDecoder(code);
var watch = Watch.GenerateWatch(

View File

@ -4,6 +4,8 @@ using BizHawk.Emulation.Common;
using NLua;
using BizHawk.Emulation.Cores.Nintendo.SNES;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace BizHawk.Client.Common
{
[Description("Functions specific to SNESHawk (functions may not run when an SNES game is not loaded)")]
@ -22,12 +24,9 @@ namespace BizHawk.Client.Common
private LibsnesCore.SnesSettings GetSettings()
{
if (Snes != null)
{
return Snes.GetSettings();
}
return new LibsnesCore.SnesSettings();
return Snes != null
? Snes.GetSettings()
: new LibsnesCore.SnesSettings();
}
private void PutSettings(LibsnesCore.SnesSettings settings)

View File

@ -4,21 +4,21 @@ using System.ComponentModel;
using System.Data.SQLite;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
[Description("A library for performing SQLite operations.")]
public sealed class SQLLuaLibrary : LuaLibraryBase
public sealed class SqlLuaLibrary : LuaLibraryBase
{
public SQLLuaLibrary(Lua lua)
public SqlLuaLibrary(Lua lua)
: base(lua) { }
public SQLLuaLibrary(Lua lua, Action<string> logOutputCallback)
public SqlLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback) { }
public override string Name => "SQL";
SQLiteConnection m_dbConnection;
string connectionString;
SQLiteConnection _mDBConnection;
[LuaMethodExample("local stSQLcre = SQL.createdatabase( \"eg_db\" );")]
[LuaMethod("createdatabase", "Creates a SQLite Database. Name should end with .db")]
@ -29,11 +29,10 @@ namespace BizHawk.Client.Common
SQLiteConnection.CreateFile(name);
return "Database Created Successfully";
}
catch (SQLiteException sqlEX)
catch (SQLiteException sqlEx)
{
return sqlEX.Message;
return sqlEx.Message;
}
}
@ -43,21 +42,23 @@ namespace BizHawk.Client.Common
{
try
{
SQLiteConnectionStringBuilder connBuilder = new SQLiteConnectionStringBuilder();
connBuilder.DataSource = name;
connBuilder.Version = 3; //SQLite version
connBuilder.JournalMode = SQLiteJournalModeEnum.Wal; //Allows for reads and writes to happen at the same time
connBuilder.DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted; //This only helps make the database lock left. May be pointless now
connBuilder.SyncMode = SynchronizationModes.Off; //This shortens the delay for do synchronous calls.
m_dbConnection = new SQLiteConnection(connBuilder.ToString());
connectionString = connBuilder.ToString();
m_dbConnection.Open();
m_dbConnection.Close();
var connBuilder = new SQLiteConnectionStringBuilder
{
DataSource = name
, Version = 3 // SQLite version
, JournalMode = SQLiteJournalModeEnum.Wal // Allows for reads and writes to happen at the same time
, DefaultIsolationLevel = System.Data.IsolationLevel.ReadCommitted // This only helps make the database lock left. May be pointless now
, SyncMode = SynchronizationModes.Off // This shortens the delay for do synchronous calls.
};
_mDBConnection = new SQLiteConnection(connBuilder.ToString());
_mDBConnection.Open();
_mDBConnection.Close();
return "Database Opened Successfully";
}
catch (SQLiteException sqlEX)
catch (SQLiteException sqlEx)
{
return sqlEX.Message;
return sqlEx.Message;
}
}
@ -72,11 +73,11 @@ namespace BizHawk.Client.Common
}
try
{
m_dbConnection.Open();
_mDBConnection.Open();
string sql = query;
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteCommand command = new SQLiteCommand(sql, _mDBConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();
_mDBConnection.Close();
return "Command ran successfully";
@ -87,7 +88,7 @@ namespace BizHawk.Client.Common
}
catch (SQLiteException sqlEx)
{
m_dbConnection.Close();
_mDBConnection.Close();
return sqlEx.Message;
}
}
@ -104,9 +105,9 @@ namespace BizHawk.Client.Common
try
{
var table = Lua.NewTable();
m_dbConnection.Open();
_mDBConnection.Open();
string sql = $"PRAGMA read_uncommitted =1;{query}";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
var command = new SQLiteCommand(sql, _mDBConnection);
SQLiteDataReader reader = command.ExecuteReader();
bool rows = reader.HasRows;
long rowCount = 0;
@ -124,7 +125,7 @@ namespace BizHawk.Client.Common
rowCount += 1;
}
reader.Close();
m_dbConnection.Close();
_mDBConnection.Close();
if (rows == false)
{
return "No rows found";
@ -137,12 +138,11 @@ namespace BizHawk.Client.Common
{
return "Database not opened.";
}
catch (SQLiteException sqlEX)
catch (SQLiteException sqlEx)
{
m_dbConnection.Close();
return sqlEX.Message;
_mDBConnection.Close();
return sqlEx.Message;
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using NLua;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
{
[Description("A library exposing standard .NET string methods")]
@ -68,96 +69,66 @@ namespace BizHawk.Client.Common
[LuaMethod("replace", "Returns a string that replaces all occurances of str2 in str1 with the value of replace")]
public static string Replace(string str, string str2, string replace)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Replace(str2, replace);
return string.IsNullOrEmpty(str)
? null
: str.Replace(str2, replace);
}
[LuaMethodExample("local stbiztou = bizstring.toupper( \"Some string\" );")]
[LuaMethod("toupper", "Returns an uppercase version of the given string")]
public static string ToUpper(string str)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.ToUpper();
return string.IsNullOrEmpty(str)
? null
: str.ToUpper();
}
[LuaMethodExample("local stbiztol = bizstring.tolower( \"Some string\" );")]
[LuaMethod("tolower", "Returns an lowercase version of the given string")]
public static string ToLower(string str)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.ToLower();
return string.IsNullOrEmpty(str)
? null
: str.ToLower();
}
[LuaMethodExample("local stbizsub = bizstring.substring( \"Some string\", 6, 3 );")]
[LuaMethod("substring", "Returns a string that represents a substring of str starting at position for the specified length")]
public static string SubString(string str, int position, int length)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Substring(position, length);
return string.IsNullOrEmpty(str)
? null
: str.Substring(position, length);
}
[LuaMethodExample("local stbizrem = bizstring.remove( \"Some string\", 4, 5 );")]
[LuaMethod("remove", "Returns a string that represents str with the given position and count removed")]
public static string Remove(string str, int position, int count)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Remove(position, count);
return string.IsNullOrEmpty(str)
? null
: str.Remove(position, count);
}
[LuaMethodExample("if ( bizstring.contains( \"Some string\", \"Some\") ) then\r\n\tconsole.log( \"Returns whether or not str contains str2\" );\r\nend;")]
[LuaMethod("contains", "Returns whether or not str contains str2")]
public static bool Contains(string str, string str2)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
return str.Contains(str2);
return !string.IsNullOrEmpty(str) && str.Contains(str2);
}
[LuaMethodExample("if ( bizstring.startswith( \"Some string\", \"Some\") ) then\r\n\tconsole.log( \"Returns whether str starts with str2\" );\r\nend;")]
[LuaMethod("startswith", "Returns whether str starts with str2")]
public static bool StartsWith(string str, string str2)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
return str.StartsWith(str2);
return !string.IsNullOrEmpty(str) && str.StartsWith(str2);
}
[LuaMethodExample("if ( bizstring.endswith( \"Some string\", \"string\") ) then\r\n\tconsole.log( \"Returns whether str ends wth str2\" );\r\nend;")]
[LuaMethod("endswith", "Returns whether str ends wth str2")]
public static bool EndsWith(string str, string str2)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
return str.EndsWith(str2);
return !string.IsNullOrEmpty(str) && str.EndsWith(str2);
}
[LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")]

View File

@ -1,10 +1,9 @@
using System;
using System.ComponentModel;
using NLua;
using BizHawk.Client.Common;
// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.EmuHawk
{
[Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")]

View File

@ -23,7 +23,6 @@ namespace BizHawk.Client.Common
Example = example;
}
public string Name { get; }
public string Example { get; }
}

View File

@ -186,13 +186,13 @@ __Types and notation__
public string Example => _luaExampleAttribute?.Example;
private string _paramterList = null;
private string _parameterList;
public string ParameterList
{
get
{
if (_paramterList == null)
if (_parameterList == null)
{
var parameters = _method.GetParameters();
@ -218,10 +218,10 @@ __Types and notation__
}
list.Append(')');
_paramterList = list.ToString();
_parameterList = list.ToString();
}
return _paramterList;
return _parameterList;
}
}

View File

@ -30,7 +30,7 @@ namespace BizHawk.Client.Common
{
string target = $"{_currentDirectory}\\";
// first we'll bypass it with a general hack: dont do any setting if the value's already there (even at the OS level, setting the directory can be slow)
// first we'll bypass it with a general hack: don't do any setting if the value's already there (even at the OS level, setting the directory can be slow)
// yeah I know, not the smoothest move to compare strings here, in case path normalization is happening at some point
// but you got any better ideas?
if (currDirSpeedHack == null)
@ -117,8 +117,7 @@ namespace BizHawk.Client.Common
lock (SandboxForThread)
{
LuaSandbox sandbox;
if (SandboxForThread.TryGetValue(thread, out sandbox))
if (SandboxForThread.TryGetValue(thread, out var sandbox))
{
return sandbox;
}

View File

@ -193,6 +193,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=botting/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=bsnes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bundler/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Byteswap/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=chromeless/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Coalescer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Coleco/@EntryIndexedValue">True</s:Boolean>
@ -254,6 +255,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=luaf/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=luases/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mainform/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=mainmemory/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=mame/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mednafen/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Multidisk/@EntryIndexedValue">True</s:Boolean>