summaryrefslogtreecommitdiffstats
path: root/src/CBot/TestCBot/CBotConsoleDlg.cpp
blob: 5f29e86d8cd3a30f55065b940eac478bb75670e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// CBotConsoleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestCBot.h"
#include "CBotConsoleDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBotConsoleDlg dialog


CBotConsoleDlg::CBotConsoleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBotConsoleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBotConsoleDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pProg = NULL;
	m_threadinfo.m_bRun	  = FALSE;
	m_code  = 0;
}


void CBotConsoleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBotConsoleDlg)
	DDX_Control(pDX, IDOK, m_cOK);
	DDX_Control(pDX, IDC_EDIT2, m_Edit2);
	DDX_Control(pDX, IDC_EDIT1, m_Edit1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBotConsoleDlg, CDialog)
	//{{AFX_MSG_MAP(CBotConsoleDlg)
	ON_MESSAGE(WM_ENDPROG, EndProg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBotConsoleDlg message handlers

UINT ThreadProc(ThreadInfo *info)
{
	CTime	t0  = CTime::GetCurrentTime();
	int		Cpt = 0;

	info->m_pProg->Start("LaCommande");
	while ( !info->m_bStop && !info->m_pProg->Run() )
	{
#if	0
		const char* FunctionName;
		const char* FN;
		int start, end;

		info->m_pProg->GetRunPos(FunctionName, start, end);

		if ( FunctionName != NULL )
		{
			info->m_pEditx->SetSel(start, end);

			char	buffer[200];
			sprintf( buffer, "step %s, %d, %d",FunctionName, start, end);
			AfxMessageBox( buffer );

			int level = 0;
			do
			{
				CBotVar* t = info->m_pProg->GivStackVars(FN, level--);
				if ( FN != FunctionName ) break;
				if ( t != NULL )
				{
					CString	s ;
					while ( t != NULL )
					{
						if (s.IsEmpty()) s+= "Stack -> ";
						else			 s+= " , ";
						s += t->GivValString();
						t = t->GivNext();
					}
					AfxMessageBox(s);
				}
			} while (TRUE);
		}
#endif
		Cpt++;
		if ( Cpt%50 == 0 ) info->m_pEdit1->ReplaceSel(".");
	}

	if ( info->m_bStop )
	{
		info->m_pEdit1->ReplaceSel("\r\nInterrompu\r\n");
	}
	else if (info->m_pProg->GivError() == 0)
	{
		CTime	t = CTime::GetCurrentTime();
		CTimeSpan ts = t - t0;

		char	buffer[200];
		sprintf( buffer, "\r\nEx�cution termin�e en %d secondes.\r\nInterrompue %d fois.\r\n",
				 ts.GetTotalSeconds(), Cpt);

		info->m_pEdit1->ReplaceSel(buffer);
	}

	info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ;
	return 0 ;
}

LONG CBotConsoleDlg::EndProg(UINT wparam, LONG lparam)
{
	m_threadinfo.m_bRun	  = FALSE;

	if (m_pProg->GetError(m_code, m_start, m_end))
	{
		CBotString	TextError;
		TextError = CBotProgram::GivErrorText(m_code);
		AfxMessageBox(TextError);
		CDialog::OnCancel();
		return 1;
	}
	delete m_pProg;
	m_pProg = NULL;

	m_Edit2.EnableWindow(TRUE);
	m_cOK.EnableWindow(TRUE);

	m_Edit2.SetWindowText("");
	m_Edit2.SetFocus();
	return 0 ;		
}

void CBotConsoleDlg::OnOK() 
{
	CTestCBotApp* pApp = (CTestCBotApp*)AfxGetApp();
	pApp->m_pConsole = &m_Edit1;
	m_code  = 0;

	CString	Commande;
	m_Edit2.GetWindowText(Commande);

	CString s = "void LaCommande() { " + Commande + " ;}";
	m_pProg = new CBotProgram();
	CBotStringArray liste;
	m_pProg->Compile(s, liste);

	int	err, start, end;
	if ( m_pProg->GetError(err, start, end) )
	{
		CBotString	TextError;
		TextError = CBotProgram::GivErrorText(err);
		AfxMessageBox(TextError);
		m_Edit2.SetSel(start-20, end-20);
		return;
	}

	m_Edit1.ReplaceSel("\r\n" + Commande + " ->\r\n");

	m_Edit2.SetWindowText("");
	m_Edit1.SetFocus();
	m_Edit2.EnableWindow(FALSE);
	m_cOK.EnableWindow(FALSE);

	// lance un processus paral�le pour l'ex�cution
	m_threadinfo.m_pWndMessage = this ;

	m_threadinfo.m_pEdit1 = &m_Edit1;
	m_threadinfo.m_pEditx = m_pEditx;
	m_threadinfo.m_pProg  = m_pProg;
	m_threadinfo.m_bStop  = FALSE;
	m_threadinfo.m_bRun	  = TRUE;

	AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ;
}

void CBotConsoleDlg::OnCancel() 
{
	if (!m_threadinfo.m_bRun) CDialog::OnCancel();
	m_threadinfo.m_bStop = TRUE ;
}


BOOL CBotConsoleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_Edit1.ReplaceSel("Les fonctions suivantes sont disponibles:\r\n");
	for ( int i = 0; i < m_pListe->GivSize(); i++ )
	{
		CBotString x = (*m_pListe)[i] + "\r\n";
		m_Edit1.ReplaceSel(x);
	}
	m_Edit1.ReplaceSel("Entrez une commande ci-dessous.\r\n\r\n");

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}