| 1 | /* |
|---|
| 2 | ================================================================================= |
|---|
| 3 | This file is part of Cafu, the open-source game engine and graphics engine |
|---|
| 4 | for multiplayer, cross-platform, real-time 3D action. |
|---|
| 5 | Copyright (C) 2002-2012 Carsten Fuchs Software. |
|---|
| 6 | |
|---|
| 7 | Cafu is free software: you can redistribute it and/or modify it under the terms |
|---|
| 8 | of the GNU General Public License as published by the Free Software Foundation, |
|---|
| 9 | either version 3 of the License, or (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|---|
| 12 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 13 | PURPOSE. See the GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with Cafu. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | For support and more information about Cafu, visit us at <http://www.cafu.de>. |
|---|
| 19 | ================================================================================= |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include "ConDefs.hpp" |
|---|
| 23 | #include "ConsoleCommands/Console.hpp" |
|---|
| 24 | #include "ConsoleCommands/ConVar.hpp" |
|---|
| 25 | #include "ConsoleCommands/ConFunc.hpp" |
|---|
| 26 | #include "MaterialSystem/MaterialManager.hpp" |
|---|
| 27 | #include "MaterialSystem/Renderer.hpp" |
|---|
| 28 | #include "SoundSystem/Sound.hpp" |
|---|
| 29 | #include "SoundSystem/SoundSys.hpp" |
|---|
| 30 | #include "SoundSystem/SoundShaderManager.hpp" |
|---|
| 31 | #include "TextParser/TextParser.hpp" |
|---|
| 32 | |
|---|
| 33 | #include "wx/wx.h" |
|---|
| 34 | #include "wx/display.h" |
|---|
| 35 | |
|---|
| 36 | extern "C" |
|---|
| 37 | { |
|---|
| 38 | #include <lua.h> |
|---|
| 39 | #include <lualib.h> |
|---|
| 40 | #include <lauxlib.h> |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | #include <fstream> |
|---|
| 44 | #include <map> |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | static const int CLIENT_RUNMODE=1; |
|---|
| 48 | static const int SERVER_RUNMODE=2; |
|---|
| 49 | static const int DefaultRunMode=CLIENT_RUNMODE | SERVER_RUNMODE; |
|---|
| 50 | |
|---|
| 51 | /*static*/ ConVarT Options_RunMode ("dlg_RunMode", DefaultRunMode, ConVarT::FLAG_MAIN_EXE, "1 is client-only, 2 is server-only, 3 is both.", 1, 3); |
|---|
| 52 | /*static*/ ConVarT Options_DeathMatchPlayerName ("dlg_dmPlayerName", "Player", ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "Player name for the DeathMatch game."); |
|---|
| 53 | /*static*/ ConVarT Options_DeathMatchModelName ("dlg_dmModelName", "James", ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "Name of the model to use for the DeathMatch game."); |
|---|
| 54 | /*static*/ ConVarT Options_ClientFullScreen ("dlg_clFullScreen", true, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "When true, the video mode is changed on startup and fullscreen display is used. Otherwise, Cafu runs in an application window on the desktop."); |
|---|
| 55 | /*static*/ ConVarT Options_ClientWindowSizeX ("dlg_clWindowSizeX", 1024, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "The size of the client window in X direction."); |
|---|
| 56 | /*static*/ ConVarT Options_ClientWindowSizeY ("dlg_clWindowSizeY", 768, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "The size of the client window in Y direction."); |
|---|
| 57 | /*static*/ ConVarT Options_ClientDesiredRenderer ("dlg_clDesiredRenderer", "", ConVarT::FLAG_MAIN_EXE, "If set, overrides the auto-selection of the renderer and determines which renderer is to be used instead."); |
|---|
| 58 | /*static*/ ConVarT Options_ClientDesiredSoundSystem("dlg_clDesiredSoundSystem", "", ConVarT::FLAG_MAIN_EXE, "If set, overrides the auto-selection of the sound system and determines which sound system is to be used instead."); |
|---|
| 59 | /*static*/ ConVarT Options_ClientPortNr ("dlg_clPortNr", 33000, ConVarT::FLAG_MAIN_EXE, "The client port number.", 0, 0xFFFF); |
|---|
| 60 | /*static*/ ConVarT Options_ClientRemoteName ("dlg_clRemoteName", "192.168.1.1", ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "Name or IP of the server the client connects to."); |
|---|
| 61 | /*static*/ ConVarT Options_ClientRemotePortNr ("dlg_clRemotePort", 30000, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "Port number of the remote server.", 0, 0xFFFF); |
|---|
| 62 | /*static*/ ConVarT Options_ClientDisplayBPP ("dlg_clDisplayBPP", 32, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "The display depth in bits-per-pixel. Normally use 32, or 0 for system default.", 0, 64); |
|---|
| 63 | /*static*/ ConVarT Options_ClientDisplayRefresh ("dlg_clDisplayRefresh", 0, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "The display refresh rate. Use 0 for system default, check with your monitor specs for any other value."); |
|---|
| 64 | /*static*/ ConVarT Options_ClientTextureDetail ("dlg_clTextureDetail", 0, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_PERSISTENT, "0 high detail, 1 medium detail, 2 low detail", 0, 2); |
|---|
| 65 | /*static*/ ConVarT Options_ServerGameName ("dlg_svGameName", "DeathMatch", ConVarT::FLAG_MAIN_EXE, "Name of the game (MOD) to load."); |
|---|
| 66 | /*static*/ ConVarT Options_ServerWorldName ("dlg_svWorldName", "TechDemo", ConVarT::FLAG_MAIN_EXE, "Name of the world to load."); |
|---|
| 67 | /*static*/ ConVarT Options_ServerPortNr ("dlg_svPortNr", 30000, ConVarT::FLAG_MAIN_EXE, "Server port number.", 0, 0xFFFF); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | // Notes about the GlobalTime convar: |
|---|
| 71 | // 1) It's declared for use with "extern ConVarT GlobalTime;" elsewhere, so we don't use "static" here. |
|---|
| 72 | // 2) It's intentionally not defined very precisely (except for that it is in seconds and ever increases), |
|---|
| 73 | // because I especially don't want to define where "0" is (at program start, at start of first frame, at new map start, ...?). |
|---|
| 74 | ConVarT GlobalTime("time", 0.0, ConVarT::FLAG_MAIN_EXE | ConVarT::FLAG_READ_ONLY, "The ever proceeding time, in seconds."); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | static int ConFunc_CleanupPersistentConfig_Callback(lua_State* LuaState) |
|---|
| 78 | { |
|---|
| 79 | const char* CfgFileName=luaL_checkstring(LuaState, 1); |
|---|
| 80 | TextParserT TP(CfgFileName, "="); |
|---|
| 81 | std::map<std::string, std::string> Pairs; |
|---|
| 82 | |
|---|
| 83 | if (TP.IsAtEOF()) |
|---|
| 84 | { |
|---|
| 85 | // This function must not cause a Lua error, or otherwise the calling config.lua script is aborted early! |
|---|
| 86 | Console->Warning(cf::va("Unable to open file %s.\n", CfgFileName)); |
|---|
| 87 | return 0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | try |
|---|
| 91 | { |
|---|
| 92 | while (!TP.IsAtEOF()) |
|---|
| 93 | { |
|---|
| 94 | std::string Key =TP.GetNextToken(); TP.AssertAndSkipToken("="); |
|---|
| 95 | std::string Value=TP.GetNextToken(); |
|---|
| 96 | |
|---|
| 97 | if (TP.WasLastTokenQuoted()) Value="\""+Value+"\""; |
|---|
| 98 | |
|---|
| 99 | Pairs[Key]=Value; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | catch (const TextParserT::ParseError&) |
|---|
| 103 | { |
|---|
| 104 | // This function must not cause a Lua error, or otherwise the calling config.lua script is aborted early! |
|---|
| 105 | Console->Warning(cf::va("Problem parsing the config file near byte %lu (%.3f%%).", TP.GetReadPosByte(), TP.GetReadPosPercent()*100.0)); |
|---|
| 106 | return 0; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | // Ok, all the right keys and values are now in Pairs, now rewrite the config file. |
|---|
| 111 | std::ofstream CfgFile(CfgFileName, std::ios::out); |
|---|
| 112 | |
|---|
| 113 | if (!CfgFile.bad()) |
|---|
| 114 | { |
|---|
| 115 | for (std::map<std::string, std::string>::const_iterator It=Pairs.begin(); It!=Pairs.end(); ++It) |
|---|
| 116 | { |
|---|
| 117 | CfgFile << It->first << " = " << It->second << "\n"; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | CfgFile << "\n"; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | static ConFuncT ConFunc_CleanupPersistentConfig("CleanupPersistentConfig", ConFunc_CleanupPersistentConfig_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | static int ConFunc_VideoInfo_Callback(lua_State* LuaState) |
|---|
| 130 | { |
|---|
| 131 | // Console->Print(GetVideoModes()); |
|---|
| 132 | Console->Print(cf::va("Renderer Info: %s\n", MatSys::Renderer ? MatSys::Renderer->GetDescription() : "[No renderer active.]")); |
|---|
| 133 | return 0; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | static ConFuncT ConFunc_VideoInfo("VideoInfo", ConFunc_VideoInfo_Callback, ConFuncT::FLAG_MAIN_EXE, "Prints some information about the OpenGL window and renderer."); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | static int ConFunc_forceRM_Callback(lua_State* LuaState) |
|---|
| 140 | { |
|---|
| 141 | static MatSys::RenderMaterialT* ForceRM=NULL; |
|---|
| 142 | |
|---|
| 143 | // First check if the MatSys::Renderer is available, we need it below in all usage-cases of this function. |
|---|
| 144 | if (MatSys::Renderer==NULL) |
|---|
| 145 | { |
|---|
| 146 | ForceRM=NULL; // The ForceRM was either never assigned, or automatically freed when the MatSys::Renderer last went down. |
|---|
| 147 | return luaL_error(LuaState, "MatSys::Renderer is not available."); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | // If a ForceRM was set in a previous call to this function, free it. |
|---|
| 151 | if (ForceRM!=NULL) |
|---|
| 152 | { |
|---|
| 153 | if (MatSys::Renderer->GetCurrentMaterial()==ForceRM) |
|---|
| 154 | { |
|---|
| 155 | MatSys::Renderer->FreeMaterial(ForceRM); |
|---|
| 156 | } |
|---|
| 157 | else |
|---|
| 158 | { |
|---|
| 159 | // Getting here is highly unusual, because either there is code somewhere else that improperly broke our material lock, |
|---|
| 160 | // or the MatSys::Renderer was shutdown and re-instantiated since the previous call to this function. |
|---|
| 161 | // In the latter case, we must not try to free the ForceRM, so the above if-test is a nice protection. |
|---|
| 162 | Console->Warning("The previously forced material has been unforced elsewhere!\n"); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | ForceRM=NULL; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // If no parameters were given, remove any lock to restore normal operation. |
|---|
| 169 | if (lua_gettop(LuaState)==0) |
|---|
| 170 | { |
|---|
| 171 | MatSys::Renderer->LockCurrentMaterial(false); |
|---|
| 172 | return 0; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | // Establish the lock for the given material name. |
|---|
| 176 | if (MaterialManager==NULL) return luaL_error(LuaState, "The MaterialManager is not available."); |
|---|
| 177 | |
|---|
| 178 | const char* MatName=luaL_checkstring(LuaState, 1); |
|---|
| 179 | MaterialT* Mat =MaterialManager->GetMaterial(MatName); |
|---|
| 180 | |
|---|
| 181 | if (Mat==NULL) return luaL_error(LuaState, "Unknown material \"%s\".", MatName); |
|---|
| 182 | |
|---|
| 183 | ForceRM=MatSys::Renderer->RegisterMaterial(Mat); |
|---|
| 184 | |
|---|
| 185 | if (ForceRM==NULL) return luaL_error(LuaState, "Could not register material \"%s\".", MatName); |
|---|
| 186 | |
|---|
| 187 | MatSys::Renderer->SetCurrentMaterial(ForceRM); |
|---|
| 188 | MatSys::Renderer->LockCurrentMaterial(true); |
|---|
| 189 | |
|---|
| 190 | return 0; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | static ConFuncT ConFunc_forceRM("forceRM", ConFunc_forceRM_Callback, ConFuncT::FLAG_MAIN_EXE, |
|---|
| 194 | "Enforces the use of the given material for all rendering. Restores normal rendering when no parameter is given."); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | static SoundI* ConMusic=NULL; // For playing background music from the console. |
|---|
| 198 | |
|---|
| 199 | static int ConFunc_MusicLoad_Callback(lua_State* LuaState) |
|---|
| 200 | { |
|---|
| 201 | if (SoundSystem==NULL) |
|---|
| 202 | { |
|---|
| 203 | return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | if (ConMusic!=NULL) |
|---|
| 207 | { |
|---|
| 208 | SoundSystem->DeleteSound(ConMusic); |
|---|
| 209 | ConMusic=NULL; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | ConMusic=SoundSystem->CreateSound2D(SoundShaderManager->GetSoundShader(luaL_checkstring(LuaState, 1))); |
|---|
| 213 | return 0; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | static ConFuncT ConFunc_MusicLoad("MusicLoad", ConFunc_MusicLoad_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | static int ConFunc_MusicPlay_Callback(lua_State* LuaState) |
|---|
| 220 | { |
|---|
| 221 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 222 | if (ConMusic ==NULL) return luaL_error(LuaState, "No sound shader loaded."); |
|---|
| 223 | |
|---|
| 224 | ConMusic->Play(); |
|---|
| 225 | return 0; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | static ConFuncT ConFunc_MusicPlay("MusicPlay", ConFunc_MusicPlay_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | static int ConFunc_MusicIsPlaying_Callback(lua_State* LuaState) |
|---|
| 232 | { |
|---|
| 233 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 234 | |
|---|
| 235 | lua_pushboolean(LuaState, ConMusic!=NULL && ConMusic->IsPlaying()); |
|---|
| 236 | return 1; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | static ConFuncT ConFunc_MusicIsPlaying("MusicIsPlaying", ConFunc_MusicIsPlaying_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | static int ConFunc_MusicStop_Callback(lua_State* LuaState) |
|---|
| 243 | { |
|---|
| 244 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 245 | if (ConMusic ==NULL) return luaL_error(LuaState, "No sound shader loaded."); |
|---|
| 246 | |
|---|
| 247 | ConMusic->Stop(); |
|---|
| 248 | return 0; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | static ConFuncT ConFunc_MusicStop("MusicStop", ConFunc_MusicStop_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | static int ConFunc_MusicSetVol_Callback(lua_State* LuaState) |
|---|
| 255 | { |
|---|
| 256 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 257 | if (ConMusic ==NULL) return luaL_error(LuaState, "No sound shader loaded."); |
|---|
| 258 | |
|---|
| 259 | ConMusic->SetInnerVolume(float(lua_tonumber(LuaState, 1))); |
|---|
| 260 | return 0; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | static ConFuncT ConFunc_MusicSetVol("MusicSetVolume", ConFunc_MusicSetVol_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | static int ConFunc_GetMasterVolume_Callback(lua_State* LuaState) |
|---|
| 267 | { |
|---|
| 268 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 269 | |
|---|
| 270 | lua_pushnumber(LuaState, SoundSystem->GetMasterVolume()); |
|---|
| 271 | return 1; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | static ConFuncT ConFunc_GetMasterVolume("GetMasterVolume", ConFunc_GetMasterVolume_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | static int ConFunc_SetMasterVolume_Callback(lua_State* LuaState) |
|---|
| 278 | { |
|---|
| 279 | if (SoundSystem==NULL) return luaL_error(LuaState, "SoundSystem is not available."); |
|---|
| 280 | |
|---|
| 281 | SoundSystem->SetMasterVolume(float(lua_tonumber(LuaState, 1))); |
|---|
| 282 | return 0; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | static ConFuncT ConFunc_SetMasterVolume("SetMasterVolume", ConFunc_SetMasterVolume_Callback, ConFuncT::FLAG_MAIN_EXE, ""); |
|---|