1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <wxdialogs/MainDialog.h>
22 #include <wxdialogs/SettingsDialog.h>
23 #include <scorched/ScorchedParams.h>
24 #include <common/Defines.h>
25 #include <common/OptionsGame.h>
26 #include <engine/ModDirs.h>
27 #include <wx/wx.h>
28 #include <wx/utils.h>
29 #include <wx/dir.h>
30 #include "ServerS.cpp"
31 
32 extern char scorched3dAppName[128];
33 
34 class ServerSFrame: public wxDialog
35 {
36 public:
37 	ServerSFrame(OptionsGame &options);
38 
39 	virtual bool TransferDataToWindow();
40 	virtual bool TransferDataFromWindow();
41 
42 	void onSettingsButton(wxCommandEvent &event);
43 	void onPublishAutoButton(wxCommandEvent &event);
44 
45 private:
46 	DECLARE_EVENT_TABLE()
47 
48 	OptionsGame &options_;
49 };
50 
BEGIN_EVENT_TABLE(ServerSFrame,wxDialog)51 BEGIN_EVENT_TABLE(ServerSFrame, wxDialog)
52     EVT_BUTTON(IDC_BUTTON_SETTINGS,  ServerSFrame::onSettingsButton)
53 	EVT_BUTTON(IDC_PUBLISHAUTO,  ServerSFrame::onPublishAutoButton)
54 END_EVENT_TABLE()
55 
56 ServerSFrame::ServerSFrame(OptionsGame &options) :
57 	wxDialog(getMainDialog(), -1, wxString(scorched3dAppName, wxConvUTF8),
58 			 wxDefaultPosition, wxDefaultSize),
59 	 options_(options)
60 {
61 #ifdef _WIN32
62 	// Set the frame's icon
63 	wxIcon icon(convertString(S3D::getDataFile("data/images/tank2.ico")), wxBITMAP_TYPE_ICO);
64 	SetIcon(icon);
65 #endif
66 
67 	// Create the positioning sizer
68 	wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
69 
70 	// Create all the display controlls
71 	createControls(this, topsizer);
72 	IDOK_CTRL->SetDefault();
73 
74 	// use the sizer for layout
75 	SetSizer(topsizer);
76 	topsizer->SetSizeHints(this); // set size hints to honour minimum size
77 
78 	CentreOnScreen();
79 }
80 
onPublishAutoButton(wxCommandEvent & event)81 void ServerSFrame::onPublishAutoButton(wxCommandEvent &event)
82 {
83 	IDC_PUBLISHIP_CTRL->SetValue(wxT("AutoDetect"));
84 }
85 
onSettingsButton(wxCommandEvent & event)86 void ServerSFrame::onSettingsButton(wxCommandEvent &event)
87 {
88 	TransferDataFromWindow();
89 	// Don't save until the whole options have been choosen
90 	showSettingsDialog(true, options_);
91 }
92 
TransferDataToWindow()93 bool ServerSFrame::TransferDataToWindow()
94 {
95 	IDC_SERVER_PORT_CTRL->SetValue(
96 		convertString(S3D::formatStringBuffer("%i", options_.getPortNo())));
97 	IDC_SERVER_PORT_CTRL->SetToolTip(
98 		convertString(options_.getPortNoEntry().getDescription()));
99 	IDC_SERVERMANAGEMENT_PORT_CTRL->SetValue(
100 		convertString(S3D::formatStringBuffer("%i", options_.getManagementPortNo())));
101 	IDC_SERVERMANAGEMENT_PORT_CTRL->SetToolTip(
102 		convertString(options_.getManagementPortNoEntry().getDescription()));
103 	IDC_SERVER_NAME_CTRL->SetValue(
104 		convertString(options_.getServerName()));
105 	IDC_SERVER_NAME_CTRL->SetToolTip(
106 		convertString(options_.getServerNameEntry().getDescription()));
107 	IDC_PUBLISH_CTRL->SetValue(options_.getPublishServer());
108 	IDC_PUBLISH_CTRL->SetToolTip(
109 		convertString(options_.getPublishServerEntry().getDescription()));
110 	IDC_USEUPNP_CTRL->SetValue(options_.getUseUPnP());
111 	IDC_USEUPNP_CTRL->SetToolTip(
112 		convertString(options_.getUseUPnPEntry().getDescription()));
113 	IDC_PUBLISHIP_CTRL->SetValue(
114 		convertString(options_.getPublishAddress()));
115 	IDC_PUBLISHIP_CTRL->SetToolTip(
116 		convertString(options_.getPublishAddressEntry().getDescription()));
117 	IDC_ALLOWSAME_CTRL->SetValue(options_.getAllowSameIP());
118 	IDC_ALLOWSAME_CTRL->SetToolTip(
119 		convertString(options_.getAllowSameIPEntry().getDescription()));
120 	IDC_ALLOWSAMEID_CTRL->SetValue(options_.getAllowSameUniqueId());
121 	IDC_ALLOWSAMEID_CTRL->SetToolTip(
122 		convertString(options_.getAllowSameUniqueIdEntry().getDescription()));
123 	IDC_LOGTOFILE_CTRL->SetValue(0 == strcmp("file", options_.getServerFileLogger()));
124 	IDC_LOGTOFILE_CTRL->SetToolTip(
125 		convertString(options_.getServerFileLoggerEntry().getDescription()));
126 
127 	ModDirs modDirs;
128 	if (!modDirs.loadModDirs()) S3D::dialogExit("ModFiles", "Failed to load mod files");
129 	std::list<ModInfo>::iterator itor;
130 	for (itor = modDirs.getDirs().begin();
131 		itor != modDirs.getDirs().end();
132 		++itor)
133 	{
134 		ModInfo &info = *itor;
135 		IDC_SERVER_MOD_CTRL->Append(convertString(info.getName()));
136 	}
137 	if (IDC_SERVER_MOD_CTRL->FindString(convertString(options_.getMod())) != -1)
138 		IDC_SERVER_MOD_CTRL->SetValue(convertString(options_.getMod()));
139 	else
140 		IDC_SERVER_MOD_CTRL->SetValue(wxT("none"));
141 	IDC_SERVER_MOD_CTRL->SetToolTip(
142 		convertString("The Scorched3D mod to use for this game."));
143 
144 	return true;
145 }
146 
TransferDataFromWindow()147 bool ServerSFrame::TransferDataFromWindow()
148 {
149 	options_.getPortNoEntry().setValue(atoi(IDC_SERVER_PORT_CTRL->GetValue().mb_str(wxConvUTF8)));
150 	options_.getManagementPortNoEntry().setValue(atoi(IDC_SERVERMANAGEMENT_PORT_CTRL->GetValue().mb_str(wxConvUTF8)));
151 	options_.getServerNameEntry().setValue(std::string(IDC_SERVER_NAME_CTRL->GetValue().mb_str(wxConvUTF8)));
152 	options_.getPublishServerEntry().setValue(IDC_PUBLISH_CTRL->GetValue());
153 	options_.getPublishAddressEntry().setValue(std::string(IDC_PUBLISHIP_CTRL->GetValue().mb_str(wxConvUTF8)));
154 	options_.getUseUPnPEntry().setValue(IDC_USEUPNP_CTRL->GetValue());
155 	options_.getAllowSameIPEntry().setValue(IDC_ALLOWSAME_CTRL->GetValue());
156 	options_.getAllowSameUniqueIdEntry().setValue(IDC_ALLOWSAMEID_CTRL->GetValue());
157 	options_.getModEntry().setValue(std::string(IDC_SERVER_MOD_CTRL->GetValue().mb_str(wxConvUTF8)));
158 	options_.getServerFileLoggerEntry().setValue((IDC_LOGTOFILE_CTRL->GetValue()?"file":"none"));
159 	return true;
160 }
161 
showServerSDialog()162 bool showServerSDialog()
163 {
164 	OptionsGame tmpOptions;
165 	std::string serverFileSrc = S3D::getDataFile("data/server.xml");
166 	std::string serverFileDest = S3D::getSettingsFile("server.xml");
167 	if (S3D::fileExists(serverFileDest.c_str()))
168 	{
169 		tmpOptions.readOptionsFromFile((char *) serverFileDest.c_str());
170 	}
171 	else
172 	{
173 		tmpOptions.readOptionsFromFile((char *) serverFileSrc.c_str());
174 	}
175 
176 	ServerSFrame frame(tmpOptions);
177 	if (frame.ShowModal() == wxID_OK)
178 	{
179 		tmpOptions.writeOptionsToFile((char *) serverFileDest.c_str(),
180 			ScorchedParams::instance()->getWriteFullOptions());
181 		std::string fileName = S3D::formatStringBuffer("-startserver \"%s\"", serverFileDest.c_str());
182 		runScorched3D(fileName.c_str(), true);
183 		return true;
184 	}
185 	return false;
186 }
187 
188