root/cafu/trunk/CaWE/AppCaWE.cpp

Revision 497, 11.0 KB (checked in by Carsten, 2 months ago)

CaWE: One more change regarding opening files from the command line, amending r493 and r496.

Line 
1/*
2=================================================================================
3This file is part of Cafu, the open-source game engine and graphics engine
4for multiplayer, cross-platform, real-time 3D action.
5Copyright (C) 2002-2012 Carsten Fuchs Software.
6
7Cafu is free software: you can redistribute it and/or modify it under the terms
8of the GNU General Public License as published by the Free Software Foundation,
9either version 3 of the License, or (at your option) any later version.
10
11Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17
18For support and more information about Cafu, visit us at <http://www.cafu.de>.
19=================================================================================
20*/
21
22#include "wx/wx.h"
23#include "wx/confbase.h"
24#include "wx/dir.h"
25#include "wx/fileconf.h"
26#include "wx/filename.h"
27#include "wx/image.h"
28#include "wx/snglinst.h"
29#include "wx/splash.h"
30#include "wx/stdpaths.h"
31
32#include "AppCaWE.hpp"
33#include "ArtProvider.hpp"
34#include "ChildFrameViewWin2D.hpp"
35#include "ChildFrameViewWin3D.hpp"
36#include "CursorMan.hpp"
37#include "GameConfig.hpp"
38#include "ParentFrame.hpp"
39#include "MapDocument.hpp"
40#include "MapElement.hpp"
41#include "Options.hpp"
42#include "EditorMaterialManager.hpp"
43#include "ToolbarMaterials.hpp"
44#include "Tool.hpp"
45#include "ToolManager.hpp"
46
47#include "ClipSys/CollisionModelMan_impl.hpp"
48#include "ConsoleCommands/Console.hpp"
49#include "ConsoleCommands/ConsoleStdout.hpp"
50#include "ConsoleCommands/ConsoleInterpreter.hpp"
51#include "ConsoleCommands/ConsoleInterpreterImpl.hpp"
52#include "ConsoleCommands/ConVar.hpp"
53#include "ConsoleCommands/ConFunc.hpp"
54#include "FileSys/FileManImpl.hpp"
55#include "GuiSys/Window.hpp"
56#include "MaterialSystem/MaterialManagerImpl.hpp"
57#include "TypeSys.hpp"
58
59#include <fstream>
60
61
62static cf::ConsoleStdoutT ConsoleStdout;
63cf::ConsoleI* Console=&ConsoleStdout;
64
65//static cf::ConsoleFileT ConsoleFile("info.log");
66//cf::ConsoleI* Console=&ConsoleFile;
67
68static cf::FileSys::FileManImplT FileManImpl;
69cf::FileSys::FileManI* cf::FileSys::FileMan=&FileManImpl;
70
71static cf::ClipSys::CollModelManImplT CCM;
72cf::ClipSys::CollModelManI* cf::ClipSys::CollModelMan=&CCM;
73
74ConsoleInterpreterI* ConsoleInterpreter=NULL;
75MaterialManagerI*    MaterialManager   =NULL;
76
77
78OptionsT Options;
79
80
81IMPLEMENT_APP(AppCaWE)
82
83
84AppCaWE::AppCaWE()
85    : wxApp(),
86      m_Locale(NULL),
87      m_CmdLineParser(),
88      m_FileConfig(NULL),
89      m_ParentFrame(NULL)
90{
91    static ConsoleInterpreterImplT ConInterpreterImpl;
92    ConsoleInterpreter=&ConInterpreterImpl;
93
94    // All global convars and confuncs have registered themselves in linked lists.
95    // Register them with the console interpreter now.
96    ConFuncT::RegisterStaticList();
97    ConVarT ::RegisterStaticList();
98
99    SetAppName("CaWE");
100    SetVendorName("Carsten Fuchs Software");
101    wxStandardPaths::Get().UseAppInfo(wxStandardPaths::AppInfo_VendorName | wxStandardPaths::AppInfo_AppName);
102}
103
104
105bool AppCaWE::OnInit()
106{
107    cf::GuiSys::GetWindowTIM().Init();  // The one-time init of the Window type info manager.
108    GetMapElemTIM().Init();             // The one-time init of the map elements type info manager.
109    GetToolTIM().Init();                // The one-time init of the tools type info manager.
110
111    // Parse the command line.
112    // Note that this replaces (and in fact conflicts with) wxApp::OnInit().
113    m_CmdLineParser.SetCmdLine(argc, argv);
114    OnInitCmdLine(m_CmdLineParser);
115    m_CmdLineParser.AddParam("filename", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
116
117    if (m_CmdLineParser.Parse() != 0)
118    {
119        OnExit();
120        return false;
121    }
122
123    OnCmdLineParsed(m_CmdLineParser);   // Just for setting wxLog to verbose when "--verbose" is given.
124
125    #ifndef NDEBUG
126    {
127        std::ofstream OutFile("Doxygen/scripting/GuiWindows.tmpl");
128
129        if (OutFile.is_open())
130            cf::GuiSys::GetWindowTIM().CreateLuaDoxygenHeader(OutFile);
131    }
132    #endif
133
134    const std::string AppDir="./CaWE";
135
136    // This is for registering the CaWE.cmat file farther below.
137    cf::FileSys::FileMan->MountFileSystem(cf::FileSys::FS_TYPE_LOCAL_PATH, AppDir+"/res/", AppDir+"/res/");
138    cf::FileSys::FileMan->MountFileSystem(cf::FileSys::FS_TYPE_LOCAL_PATH, "./", "");
139
140
141    for (unsigned int DirNr=0; DirNr<2; DirNr++)
142    {
143        wxString TestDirs[]={ "CaWE", "Games" };
144
145        if (!wxDirExists(TestDirs[DirNr]))
146        {
147            wxMessageDialog Msg(NULL, "Could not find directory \"" + TestDirs[DirNr] + "\" in " + wxGetCwd(),
148                "Subdirectory not found", wxOK | wxCANCEL | wxCANCEL_DEFAULT | wxICON_ERROR);
149
150            Msg.SetExtendedMessage("Did you run the program from the right working directory?\n\nTo get help, please post this error at the Cafu forums or mailing-list.");
151            Msg.SetOKCancelLabels("Open www.cafu.de", "Close");
152
153            while (Msg.ShowModal()==wxID_OK)
154                wxLaunchDefaultBrowser("http://www.cafu.de");
155
156            OnExit();
157            return false;
158        }
159    }
160
161
162    const wxString UserDataDir=wxStandardPaths::Get().GetUserDataDir();
163
164    if (!wxFileName::Mkdir(UserDataDir, 0777, wxPATH_MKDIR_FULL))
165        wxMessageBox(wxString("Config file storage path \n")+UserDataDir+"\n doesn't exist, and I was not able to create it, either.", "Warning!");
166
167
168    static wxSingleInstanceChecker SIC(GetAppName()+"-"+wxGetUserId(), UserDataDir);
169
170    if (SIC.IsAnotherRunning())
171    {
172        wxMessageBox("An instance of CaWE is already running!");
173        OnExit();
174        return false;
175    }
176
177
178    // Undo the wx locale initialization, as we want to be sure to use the same (default) locale "C" always and everywhere.
179    // Using other locales introduces a lot of subtle errors. E.g. reading floating point numbers from anywhere
180    // (like map files!) fails because e.g. "1.4" is no proper floating point string in the German locale (but "1,4" is).
181    // setlocale(LC_ALL, "C");      // This alone is not enough, see http://trac.wxwidgets.org/ticket/12970 for details.
182    for (int LangNr=wxLANGUAGE_ENGLISH; LangNr<=wxLANGUAGE_ENGLISH_ZIMBABWE; LangNr++)
183    {
184        if (wxLocale::IsAvailable(LangNr))
185        {
186            m_Locale=new wxLocale(LangNr, wxLOCALE_DONT_LOAD_DEFAULT);
187
188            wxLogDebug("Program locale set to %s (%s, %s).", m_Locale->GetName(), m_Locale->GetCanonicalName(), m_Locale->GetLocale());
189            break;
190        }
191    }
192
193    if (!m_Locale)
194    {
195        // If the above for some reason didn't work, set at least the CRT to the "C" locale.
196        setlocale(LC_ALL, "C");
197        wxLogDebug("Program locale set to \"C\".");
198    }
199
200
201    // Activate a log console as the wx debug target.
202    // wxLog::SetActiveTarget(new wxLogWindow(NULL, "wx Debug Console", true, false));
203
204    srand(time(NULL));          // Re-seed the random number generator.
205    wxInitAllImageHandlers();   // Needed e.g. for cursor initialization under GTK.
206
207    // Initialize the global cursor manager instance.
208    wxASSERT(CursorMan==NULL);
209    CursorMan=new CursorManT;
210
211    // Initialize the global art provider.
212    if (wxArtProvider::HasNativeProvider())
213        wxArtProvider::PushBack(new ArtProviderT("default"));
214    else
215        wxArtProvider::Push(new ArtProviderT("default"));
216
217
218    // Set the globally used configuration storage object for easy access via wxConfigBase::Get().
219    m_FileConfig=new wxFileConfig(GetAppName(), GetVendorName(), UserDataDir+"/CaWE.config");
220    wxConfigBase::Set(m_FileConfig);
221
222    // Setup the global Material Manager pointer.
223    static MaterialManagerImplT MatManImpl;
224
225    MaterialManager=&MatManImpl;
226
227    // Register the material script with the CaWE materials definitions.
228    if (MaterialManager->RegisterMaterialScript(AppDir+"/res/CaWE.cmat", AppDir+"/res/").Size()==0)
229        wxMessageBox("CaWE.cmat not found in \""+AppDir+"\".", "WARNING");
230
231    // Initialize the global options and game configs from the CaWE configuration files.
232    // This also registers additional material scripts with the MaterialManager that must be known before the ParentFrameT ctor is called,
233    // because under wxMSW, the ParentFrameT ctor calls the ParentFrameT::OnShow() method, where the global Gui Manager initialization
234    // expects the "Gui/Default" material to be known.
235    Options.Init();
236
237
238    // Create the MDI parent frame.
239    m_ParentFrame=new ParentFrameT(m_CmdLineParser);
240
241    SetTopWindow(m_ParentFrame);
242
243
244    // Check for autosave files.
245    wxDir AutoSaveDir(UserDataDir);
246
247    if (AutoSaveDir.IsOpened())
248    {
249        ArrayT<wxString> AutoSavedFileNames;
250        wxString         AutoSavedFileName;
251
252        for (bool more=AutoSaveDir.GetFirst(&AutoSavedFileName, "autosave*.cmap", wxDIR_FILES); more; more=AutoSaveDir.GetNext(&AutoSavedFileName))
253            AutoSavedFileNames.PushBack(AutoSavedFileName);
254
255        if (AutoSavedFileNames.Size()>0)
256        {
257            const unsigned long ID=wxGetLocalTime();
258            wxString RecoveredList;
259
260            for (unsigned long FileNr=0; FileNr<AutoSavedFileNames.Size(); FileNr++)
261            {
262                const wxString NewName =wxString::Format("recovered_%lu_%lu.cmap", FileNr, ID);
263                const bool     RenameOK=wxRenameFile(UserDataDir+"/"+AutoSavedFileNames[FileNr], UserDataDir+"/"+NewName);
264
265                RecoveredList+=RenameOK ? NewName : AutoSavedFileNames[FileNr] + "(WARNING: renaming to "+NewName+" failed! Back it up immediately yourself!)";
266                RecoveredList+="\n";
267            }
268
269            wxMessageBox(wxString("A previously crashed or otherwise prematurely aborted instance of CaWE left auto-saved files.\n")+
270                         "They are kept in "+UserDataDir+" under the name(s)\n\n"+RecoveredList+"\n"+
271                         "You may open them as usual, and save, move, delete or rename them at your convenience.",
272                         "Crash recovery: auto-save files found and recovered!", wxOK | wxICON_EXCLAMATION);
273        }
274    }
275    else
276    {
277        wxMessageBox(wxString("Could not open auto-save directory ")+UserDataDir, "WARNING", wxOK | wxICON_ERROR);
278    }
279
280    return true;
281}
282
283
284int AppCaWE::OnExit()
285{
286    // TODO: delete Options; Options=NULL;
287
288    wxConfigBase::Set(NULL);
289
290    delete m_FileConfig;
291    m_FileConfig=NULL;
292
293    ConsoleInterpreter=NULL;     // This is very important, to make sure that no ConFuncT or ConVarT dtor accesses the ConsoleInterpreter that might already have been destroyed then.
294
295    // Shutdown the global cursor manager instance.
296    delete CursorMan;
297    CursorMan=NULL;
298
299    delete m_Locale;
300    m_Locale=NULL;
301
302    return wxApp::OnExit();
303}
Note: See TracBrowser for help on using the browser.