Skip to content

Commit

Permalink
Merge pull request GenieClient#14 from GenieClient/Refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
mj-colonel-panic authored Jan 13, 2022
2 parents 758d76f + 6759cb7 commit f85c603
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 173 deletions.
39 changes: 14 additions & 25 deletions Core/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
// Imports Jint
using GenieClient.Genie.Collections;

namespace GenieClient.Genie
{
Expand All @@ -24,6 +25,10 @@ public class Command

public delegate void EventDisconnectEventHandler();

public event EventExitEventHandler EventExit;

public delegate void EventExitEventHandler();

public event EventEchoTextEventHandler EventEchoText;

public delegate void EventEchoTextEventHandler(string sText, string sWindow);
Expand Down Expand Up @@ -233,7 +238,7 @@ public string ParseCommand(string sText, bool bSendToGame = false, bool bUserInp
if (sRow.Trim().StartsWith(Conversions.ToString(oGlobals.Config.cCommandChar)))
{
// Get result from function then send result to game
var oArgs = new ArrayList();
var oArgs = new Genie.Collections.ThreadedArrayList();
oArgs = Utility.ParseArgs(sRow);
if (oArgs.Count > 0)
{
Expand Down Expand Up @@ -304,9 +309,6 @@ public string ParseCommand(string sText, bool bSendToGame = false, bool bUserInp
sResult = "";
break;
}
// Case "jint"
// Dim jint As New JintEngine()
// EchoText(jint.Run("var blah = 1;return blah + 10;").ToString())
case "link":
{
string sWindow = string.Empty;
Expand Down Expand Up @@ -400,7 +402,11 @@ public string ParseCommand(string sText, bool bSendToGame = false, bool bUserInp
EventDisconnect?.Invoke();
break;
}

case "exit":
{
EventExit?.Invoke();
break;
}
case "clear":
{
ClearWindow(oGlobals.ParseGlobalVars(ParseAllArgs(oArgs, 1)));
Expand Down Expand Up @@ -2510,7 +2516,7 @@ private string GetArgumentString(string strRow)
private string ParseAlias(string sText)
{
string sResult = "";
var oArgs = new ArrayList();
var oArgs = new Genie.Collections.ThreadedArrayList();
oArgs = Utility.ParseArgs(sText);
string sKey = GetKeywordString(sText);
if (oGlobals.AliasList.ContainsKey(sKey) == true)
Expand Down Expand Up @@ -2601,24 +2607,7 @@ private void SendTextToGame(string sText, [Optional, DefaultParameterValue(false
// EchoText("Send: " & sText & vbNewLine)
SendText(sText, bUserInput, sOrigin);
}

// Private Function ParseAllArgs(ByoList As ArrayList, Optional ByVal iStartIndex As Integer = 1) As String
// Dim sResult As String = String.Empty

// For i As Integer = iStartIndex To oList.Count - 1
// If Not IsNothing(oList.Item(i)) Then
// sResult &= " " & ParseCommand(oList.Item(i).ToString)
// End If
// Next

// If sResult.Length > 0 Then
// sResult = sResult.Substring(1) ' Remove first space
// End If

// Return sResult
// End Function

private string ParseAllArgs(ArrayList oList, int iStartIndex = 1, bool bParseQuickSend = true)
private string ParseAllArgs(ThreadedArrayList oList, int iStartIndex = 1, bool bParseQuickSend = true)
{
string sResult = string.Empty;
string sCommand = string.Empty;
Expand Down
26 changes: 17 additions & 9 deletions Core/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public void Connect(string sHostname, int iPort)
}
}

public void Disconnect()
public void Disconnect(bool ExitOnDisconnect = false)
{
Disconnect(m_SocketClient);
Disconnect(m_SocketClient, ExitOnDisconnect);
}

public void Send(string sText)
Expand Down Expand Up @@ -189,18 +189,18 @@ private void ConnectCallback(IAsyncResult ar)
}
}

private void Disconnect(Socket s)
private void Disconnect(Socket ConnectedSocket, bool ExitOnDisconnect = false)
{
if (Information.IsNothing(s))
if (Information.IsNothing(ConnectedSocket))
{
return;
}

if (s.Connected == true)
if (ConnectedSocket.Connected == true)
{
// PrintText("Disconnecting from: " & s.RemoteEndPoint.ToString())

s.BeginDisconnect(false, new AsyncCallback(DisconnectCallback), s);
ConnectedSocket.BeginDisconnect(false, new AsyncCallback(DisconnectCallback), new object[] { ConnectedSocket, ExitOnDisconnect });
}

m_SocketClient = null;
Expand All @@ -211,13 +211,21 @@ private void DisconnectCallback(IAsyncResult ar)
try
{
// Retrieve the socket from the state object
Socket s = (Socket)ar.AsyncState;

Socket s = (Socket)(ar.AsyncState as object[])[0];
bool ExitOnDisconnect = (bool)(ar.AsyncState as object[])[1];
// Complete the connection
s.EndDisconnect(ar);
ParseData(System.Environment.NewLine); // Show lines not yet sent out
PrintText(Utility.GetTimeStamp() + " Connection closed.");
EventDisconnected?.Invoke();
if (ExitOnDisconnect)
{
System.Windows.Forms.Application.Exit();
}
else
{
EventDisconnected?.Invoke();
}

}
catch (SocketException ex)
{
Expand Down
29 changes: 14 additions & 15 deletions Core/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Xml;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using GenieClient.Genie.Collections;

namespace GenieClient.Genie
{
Expand Down Expand Up @@ -99,33 +100,27 @@ private Connection m_oSocket
{
_m_oSocket.EventConnected -= GameSocket_EventConnected;
_m_oSocket.EventDisconnected -= GameSocket_EventDisconnected;

// Private Sub GameSocket_EventDataSent() Handles oSocket.EventDataSent
// If m_oConnectState = ConnectStates.ConnectingGameServer Then
// m_oConnectState = ConnectStates.ConnectedGameHandshake
// oSocket.Send(oGlobals.Config.sConnectString & vbLf)
// End If
// End Sub
_m_oSocket.EventConnectionLost -= GameSocket_EventConnectionLost;

_m_oSocket.EventParseRow -= GameSocket_EventParseRow;
_m_oSocket.EventParsePartialRow -= GameSocket_EventParsePartialRow;
_m_oSocket.EventDataRecieveEnd -= GameSocket_EventDataRecieveEnd;

_m_oSocket.EventPrintText -= GameSocket_EventPrintText;
_m_oSocket.EventPrintError -= GameSocket_EventPrintError;
_m_oSocket.EventConnectionLost -= GameSocket_EventConnectionLost;
}

_m_oSocket = value;
if (_m_oSocket != null)
{
_m_oSocket.EventConnected += GameSocket_EventConnected;
_m_oSocket.EventDisconnected += GameSocket_EventDisconnected;
_m_oSocket.EventConnectionLost += GameSocket_EventConnectionLost;
_m_oSocket.EventParseRow += GameSocket_EventParseRow;
_m_oSocket.EventParsePartialRow += GameSocket_EventParsePartialRow;
_m_oSocket.EventDataRecieveEnd += GameSocket_EventDataRecieveEnd;
_m_oSocket.EventPrintText += GameSocket_EventPrintText;
_m_oSocket.EventPrintError += GameSocket_EventPrintError;
_m_oSocket.EventConnectionLost += GameSocket_EventConnectionLost;
}
}
}
Expand Down Expand Up @@ -387,11 +382,11 @@ public void Connect(string sGenieKey, string sAccountName, string sPassword, str
DoConnect(argsHostName, argiPort);
}

public void Disconnect()
public void Disconnect(bool ExitOnDisconnect = false)
{
if (m_oSocket.IsConnected)
{
m_oSocket.Disconnect();
m_oSocket.Disconnect(ExitOnDisconnect);
}
}

Expand Down Expand Up @@ -912,9 +907,9 @@ private void ParseRow(string sText)
}
}

private ArrayList _CharacterList = new ArrayList();
private ThreadedArrayList _CharacterList = new ThreadedArrayList();

public ArrayList CharacterList
public ThreadedArrayList CharacterList
{
get
{
Expand All @@ -933,7 +928,7 @@ private void ParseKeyRow(string sText)
}
else
{
var oData = new ArrayList();
var oData = new ThreadedArrayList();
foreach (string strLine in sText.Split(Conversions.ToChar(Constants.vbTab)))
oData.Add(strLine);
if (oData.Count > 0)
Expand Down Expand Up @@ -2819,7 +2814,7 @@ private void PrintTextToWindow(string text, Color color, Color bgcolor, WindowTa

m_bLastRowWasBlank = true;
}
else if (Regex.IsMatch(text, @"^.*\> ?$"))
else if (Regex.IsMatch(text, @"^.*\" + m_oGlobals.Config.sPrompt + "?$"))
{
if (m_bLastRowWasBlank)
{
Expand Down Expand Up @@ -3005,6 +3000,10 @@ private void GameSocket_EventDisconnected()
}
}

private void GameSocket_EventExit()
{
Disconnect(true);
}
private void GameSocket_EventParseRow(StringBuilder row)
{
var rowVar = row.ToString();
Expand Down
2 changes: 1 addition & 1 deletion Forms/Components/ComponentTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ComponentTextBox()

public delegate void PageDownEventHandler();

private ArrayList HistoryArray = new ArrayList();
private Genie.Collections.ThreadedArrayList HistoryArray = new Genie.Collections.ThreadedArrayList();
private int HistoryPos = -1;
private int HistorySize = 20;
private int HistoryMinLenght = 3;
Expand Down
32 changes: 19 additions & 13 deletions Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using GenieClient.Genie.Collections;

namespace GenieClient
{
// Imports Microsoft.Win32

public partial class FormMain
{
public FormMain()
Expand All @@ -35,7 +34,7 @@ public FormMain()
// Add any initialization after the InitializeComponent() call.
LocalDirectory.CheckUserDirectory();
bool bCustomConfigFile = false;
var al = new ArrayList();
var al = new ThreadedArrayList();
al = Utility.ParseArgs(Interaction.Command());
foreach (string cmd in al)
{
Expand Down Expand Up @@ -298,6 +297,7 @@ public Genie.Command m_oCommand
_m_oCommand.EventReconnect -= ReconnectToGame;
_m_oCommand.EventConnect -= ConnectToGame;
_m_oCommand.EventDisconnect -= DisconnectFromGame;
_m_oCommand.EventExit -= DisconnectAndExit;
_m_oCommand.EventClassChange -= Command_EventClassChange;
_m_oCommand.EventPresetChanged -= ClassCommand_PresetChanged;
_m_oCommand.EventShowScriptExplorer -= Command_ShowScriptExplorer;
Expand Down Expand Up @@ -345,6 +345,7 @@ public Genie.Command m_oCommand
_m_oCommand.EventReconnect += ReconnectToGame;
_m_oCommand.EventConnect += ConnectToGame;
_m_oCommand.EventDisconnect += DisconnectFromGame;
_m_oCommand.EventExit += DisconnectAndExit;
_m_oCommand.EventClassChange += Command_EventClassChange;
_m_oCommand.EventPresetChanged += ClassCommand_PresetChanged;
_m_oCommand.EventShowScriptExplorer += Command_ShowScriptExplorer;
Expand Down Expand Up @@ -427,7 +428,7 @@ private FormSkin m_oOutputMain
private FormSkin m_oOutputDeath;
private FormSkin m_oOutputRoom;
private FormSkin m_oOutputLog;
private ArrayList m_oFormList = new ArrayList();
private ThreadedArrayList m_oFormList = new ThreadedArrayList();
private string m_sConfigFile = string.Empty;
// private string m_sUpdateVersion = string.Empty;
// private bool m_bIsUpdateMajor = false;
Expand All @@ -437,7 +438,7 @@ private FormSkin m_oOutputMain
// Private WithEvents m_oWorker As New System.ComponentModel.BackgroundWorker
// Private m_bRunWorker As Boolean = True

public ArrayList FormList
public ThreadedArrayList FormList
{
get
{
Expand Down Expand Up @@ -1493,7 +1494,7 @@ private void TextBoxInput_KeyDown(object sender, KeyEventArgs e)
{
try
{
var oMatchList = new Genie.Collections.ArrayList();
var oMatchList = new Genie.Collections.ThreadedArrayList();
int I = 0;
int iCurrentId = 0;
foreach (DictionaryEntry de in m_oGlobals.AliasList)
Expand Down Expand Up @@ -1538,7 +1539,7 @@ private void TextBoxInput_KeyDown(object sender, KeyEventArgs e)
try
{
TextBoxInput.Tag = TextBoxInput.Text; // Save match pattern
var oMatchList = new Genie.Collections.ArrayList();
var oMatchList = new Genie.Collections.ThreadedArrayList();
foreach (DictionaryEntry de in m_oGlobals.AliasList)
{
if (de.Key.ToString().StartsWith(TextBoxInput.Text))
Expand Down Expand Up @@ -2828,7 +2829,7 @@ private void AddScriptToToolStrip(Script oScript)
}
}

private Script LoadScript(string sScriptName, ArrayList oArgList)
private Script LoadScript(string sScriptName, ThreadedArrayList oArgList)
{
if (m_oGlobals.Config.bAbortDupeScript == true)
{
Expand Down Expand Up @@ -3806,7 +3807,7 @@ private void ParseTriggers(string sText, bool bBufferWait = true)
m_oRegMatch = oTrigger.oRegexTrigger.Match(sText);
if (m_oRegMatch.Success == true)
{
var RegExpArg = new ArrayList();
var RegExpArg = new ThreadedArrayList();
if (m_oRegMatch.Groups.Count > 0)
{
int J;
Expand Down Expand Up @@ -3960,7 +3961,7 @@ private void Command_EventListScripts(string sFilter)
}
}

private void TriggerAction(string sAction, ArrayList oArgs)
private void TriggerAction(string sAction, ThreadedArrayList oArgs)
{
if (m_bTriggersEnabled == true)
{
Expand Down Expand Up @@ -4027,7 +4028,7 @@ private void ClassCommand_RunScript(string sText)
{
try
{
var al = new ArrayList();
var al = new ThreadedArrayList();
al = Utility.ParseArgs(sText, true);
string ScriptName = Conversions.ToString(al[0].ToString().ToLower().Trim().Substring(1));
if (ScriptName.EndsWith(".cmd") == false)
Expand Down Expand Up @@ -4258,7 +4259,7 @@ public void TriggerVariableChanged(string sVariableName) // When variables chang

if (s.Length > 0 & (s ?? "") != "0")
{
TriggerAction(oTrigger.sAction, new ArrayList());
TriggerAction(oTrigger.sAction, new ThreadedArrayList());
}
}
}
Expand Down Expand Up @@ -5407,6 +5408,11 @@ private void DisconnectFromGame()
m_oGame.Disconnect();
}

private void DisconnectAndExit()
{
m_oGame.Disconnect(true);
}

private void ConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
{
My.MyProject.Forms.FormConfig.MdiParent = this;
Expand Down Expand Up @@ -6362,7 +6368,7 @@ private void Config_ConfigChanged(Genie.Config.ConfigFieldUpdated oField)

private void Command_EventClassChange()
{
var al = new ArrayList();
var al = new ThreadedArrayList();
if (m_oGlobals.ClassList.AcquireReaderLock())
{
try
Expand Down
Loading

0 comments on commit f85c603

Please sign in to comment.