WinMain.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FILE: WinMain.cpp //////////////////////////////////////////////////////////
  19. //-----------------------------------------------------------------------------
  20. //
  21. // Westwood Studios Pacific.
  22. //
  23. // Confidential Information
  24. // Copyright (C) 2001 - All Rights Reserved
  25. //
  26. //-----------------------------------------------------------------------------
  27. //
  28. // Project: MapCacheBuilder
  29. //
  30. // File name: WinMain.cpp
  31. //
  32. // Created: Matthew D. Campbell, October 2002
  33. //
  34. // Desc: Application entry point for the standalone MapCache Builder
  35. //
  36. //-----------------------------------------------------------------------------
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  39. #include <windows.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. // USER INCLUDES //////////////////////////////////////////////////////////////
  43. #include "Lib/BaseType.h"
  44. #include "Common/Debug.h"
  45. #include "Common/GameMemory.h"
  46. #include "Common/GlobalData.h"
  47. #include "Common/NameKeyGenerator.h"
  48. #include "Resource.h"
  49. #include "Common/ThingFactory.h"
  50. #include "Common/FileSystem.h"
  51. #include "Win32Device/Common/Win32LocalFileSystem.h"
  52. #include "Win32Device/Common/Win32BIGFileSystem.h"
  53. #include "Common/SubSystemInterface.h"
  54. #include "GameClient/MapUtil.h"
  55. #include "W3DDevice/Common/W3DModuleFactory.h"
  56. #include "Common/FileSystem.h"
  57. #include "Common/ArchiveFileSystem.h"
  58. #include "Common/LocalFileSystem.h"
  59. #include "Common/Debug.h"
  60. #include "Common/StackDump.h"
  61. #include "Common/GameMemory.h"
  62. #include "Common/Science.h"
  63. #include "Common/ThingFactory.h"
  64. #include "Common/INI.h"
  65. #include "Common/GameAudio.h"
  66. #include "Common/SpecialPower.h"
  67. #include "Common/TerrainTypes.h"
  68. #include "Common/DamageFX.h"
  69. #include "Common/Upgrade.h"
  70. #include "Common/ModuleFactory.h"
  71. #include "Common/PlayerTemplate.h"
  72. #include "Common/MultiplayerSettings.h"
  73. #include "GameLogic/Armor.h"
  74. #include "GameLogic/CaveSystem.h"
  75. #include "GameLogic/CrateSystem.h"
  76. #include "GameLogic/ObjectCreationList.h"
  77. #include "GameLogic/Weapon.h"
  78. #include "GameLogic/RankInfo.h"
  79. #include "GameLogic/SidesList.h"
  80. #include "GameLogic/ScriptEngine.h"
  81. #include "GameLogic/ScriptActions.h"
  82. #include "GameClient/Anim2D.h"
  83. #include "GameClient/GameText.h"
  84. #include "GameClient/ParticleSys.h"
  85. #include "GameClient/Water.h"
  86. #include "GameClient/TerrainRoads.h"
  87. #include "GameClient/FXList.h"
  88. #include "GameClient/VideoPlayer.h"
  89. #include "GameLogic/Locomotor.h"
  90. #include "W3DDevice/Common/W3DModuleFactory.h"
  91. #include "W3DDevice/GameClient/W3DParticleSys.h"
  92. #include "MilesAudioDevice/MilesAudioManager.h"
  93. #include <io.h>
  94. #include "win32device/GameClient/Win32Mouse.h"
  95. #include "Win32Device/Common/Win32LocalFileSystem.h"
  96. #include "Win32Device/Common/Win32BIGFileSystem.h"
  97. // DEFINES ////////////////////////////////////////////////////////////////////
  98. // PRIVATE TYPES //////////////////////////////////////////////////////////////
  99. #ifdef _INTERNAL
  100. // for occasional debugging...
  101. //#pragma optimize("", off)
  102. //#pragma message("************************************** WARNING, optimization disabled for debugging purposes")
  103. #endif
  104. // PRIVATE DATA ///////////////////////////////////////////////////////////////
  105. static SubsystemInterfaceList _TheSubsystemList;
  106. template<class SUBSYSTEM>
  107. void initSubsystem(SUBSYSTEM*& sysref, SUBSYSTEM* sys, const char* path1 = NULL, const char* path2 = NULL, const char* dirpath = NULL)
  108. {
  109. sysref = sys;
  110. _TheSubsystemList.initSubsystem(sys, path1, path2, dirpath, NULL);
  111. }
  112. ///////////////////////////////////////////////////////////////////////////////
  113. // PUBLIC DATA ////////////////////////////////////////////////////////////////
  114. ///////////////////////////////////////////////////////////////////////////////
  115. HINSTANCE ApplicationHInstance = NULL; ///< our application instance
  116. /// just to satisfy the game libraries we link to
  117. HWND ApplicationHWnd = NULL;
  118. char *gAppPrefix = "MC_";
  119. // Where are the default string files?
  120. const Char *g_strFile = "data\\Generals.str";
  121. const Char *g_csfFile = "data\\%s\\Generals.csf";
  122. // PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
  123. ///////////////////////////////////////////////////////////////////////////////
  124. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // strtrim ====================================================================
  127. /** Trim leading and trailing whitespace from a character string (in place). */
  128. //=============================================================================
  129. static char* strtrim(char* buffer)
  130. {
  131. if (buffer != NULL) {
  132. // Strip leading white space from the string.
  133. char * source = buffer;
  134. while ((*source != 0) && ((unsigned char)*source <= 32))
  135. {
  136. source++;
  137. }
  138. if (source != buffer)
  139. {
  140. strcpy(buffer, source);
  141. }
  142. // Clip trailing white space from the string.
  143. for (int index = strlen(buffer)-1; index >= 0; index--)
  144. {
  145. if ((*source != 0) && ((unsigned char)buffer[index] <= 32))
  146. {
  147. buffer[index] = '\0';
  148. }
  149. else
  150. {
  151. break;
  152. }
  153. }
  154. }
  155. return buffer;
  156. }
  157. static char *nextParam(char *newSource, char *seps)
  158. {
  159. static char *source = NULL;
  160. if (newSource)
  161. {
  162. source = newSource;
  163. }
  164. if (!source)
  165. {
  166. return NULL;
  167. }
  168. // find first separator
  169. char *first = source;//strpbrk(source, seps);
  170. if (first)
  171. {
  172. // go past initial spaces
  173. char *firstNonSpace = first;
  174. while (*firstNonSpace == ' ')
  175. ++firstNonSpace;
  176. first = firstNonSpace;
  177. // go past separator
  178. char *firstSep = strpbrk(first, seps);
  179. char firstChar[2] = {0,0};
  180. if (firstSep == first)
  181. {
  182. firstChar[0] = *first;
  183. while (*first == firstChar[0]) first++;
  184. }
  185. // find end
  186. char *end;
  187. if (firstChar[0])
  188. end = strpbrk(first, firstChar);
  189. else
  190. end = strpbrk(first, seps);
  191. // trim string & save next start pos
  192. if (end)
  193. {
  194. source = end+1;
  195. *end = 0;
  196. if (!*source)
  197. source = NULL;
  198. }
  199. else
  200. {
  201. source = NULL;
  202. }
  203. if (first && !*first)
  204. first = NULL;
  205. }
  206. return first;
  207. }
  208. ///////////////////////////////////////////////////////////////////////////////
  209. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  210. ///////////////////////////////////////////////////////////////////////////////
  211. // WinMain ====================================================================
  212. /** Application entry point */
  213. //=============================================================================
  214. Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  215. LPSTR lpCmdLine, Int nCmdShow )
  216. {
  217. // start the log
  218. DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
  219. initMemoryManager();
  220. try
  221. {
  222. // save application instance
  223. ApplicationHInstance = hInstance;
  224. // Set the current directory to the app directory.
  225. char buf[_MAX_PATH];
  226. GetModuleFileName(NULL, buf, sizeof(buf));
  227. char *pEnd = buf + strlen(buf);
  228. while (pEnd != buf) {
  229. if (*pEnd == '\\') {
  230. *pEnd = 0;
  231. break;
  232. }
  233. pEnd--;
  234. }
  235. ::SetCurrentDirectory(buf);
  236. /*
  237. ** Convert WinMain arguments to simple main argc and argv
  238. */
  239. std::list<std::string> argvSet;
  240. char *token;
  241. token = nextParam(lpCmdLine, "\" ");
  242. while (token != NULL) {
  243. char * str = strtrim(token);
  244. argvSet.push_back(str);
  245. DEBUG_LOG(("Adding '%s'\n", str));
  246. token = nextParam(NULL, "\" ");
  247. }
  248. // not part of the subsystem list, because it should normally never be reset!
  249. TheNameKeyGenerator = new NameKeyGenerator;
  250. TheNameKeyGenerator->init();
  251. TheFileSystem = new FileSystem;
  252. initSubsystem(TheLocalFileSystem, (LocalFileSystem*)new Win32LocalFileSystem);
  253. initSubsystem(TheArchiveFileSystem, (ArchiveFileSystem*)new Win32BIGFileSystem);
  254. INI ini;
  255. initSubsystem(TheWritableGlobalData, new GlobalData(), "Data\\INI\\Default\\GameData.ini", "Data\\INI\\GameData.ini");
  256. initSubsystem(TheGameText, CreateGameTextInterface());
  257. initSubsystem(TheScienceStore, new ScienceStore(), "Data\\INI\\Default\\Science.ini", "Data\\INI\\Science.ini");
  258. initSubsystem(TheMultiplayerSettings, new MultiplayerSettings(), "Data\\INI\\Default\\Multiplayer.ini", "Data\\INI\\Multiplayer.ini");
  259. initSubsystem(TheTerrainTypes, new TerrainTypeCollection(), "Data\\INI\\Default\\Terrain.ini", "Data\\INI\\Terrain.ini");
  260. initSubsystem(TheTerrainRoads, new TerrainRoadCollection(), "Data\\INI\\Default\\Roads.ini", "Data\\INI\\Roads.ini");
  261. initSubsystem(TheScriptEngine, (ScriptEngine*)(new ScriptEngine()));
  262. initSubsystem(TheAudio, (AudioManager*)new MilesAudioManager());
  263. initSubsystem(TheVideoPlayer, (VideoPlayerInterface*)(new VideoPlayer()));
  264. initSubsystem(TheModuleFactory, (ModuleFactory*)(new W3DModuleFactory()));
  265. initSubsystem(TheSidesList, new SidesList());
  266. initSubsystem(TheCaveSystem, new CaveSystem());
  267. initSubsystem(TheRankInfoStore, new RankInfoStore(), NULL, "Data\\INI\\Rank.ini");
  268. initSubsystem(ThePlayerTemplateStore, new PlayerTemplateStore(), "Data\\INI\\Default\\PlayerTemplate.ini", "Data\\INI\\PlayerTemplate.ini");
  269. initSubsystem(TheSpecialPowerStore, new SpecialPowerStore(), "Data\\INI\\Default\\SpecialPower.ini", "Data\\INI\\SpecialPower.ini" );
  270. initSubsystem(TheParticleSystemManager, (ParticleSystemManager*)(new W3DParticleSystemManager()));
  271. initSubsystem(TheFXListStore, new FXListStore(), "Data\\INI\\Default\\FXList.ini", "Data\\INI\\FXList.ini");
  272. initSubsystem(TheWeaponStore, new WeaponStore(), NULL, "Data\\INI\\Weapon.ini");
  273. initSubsystem(TheObjectCreationListStore, new ObjectCreationListStore(), "Data\\INI\\Default\\ObjectCreationList.ini", "Data\\INI\\ObjectCreationList.ini");
  274. initSubsystem(TheLocomotorStore, new LocomotorStore(), NULL, "Data\\INI\\Locomotor.ini");
  275. initSubsystem(TheDamageFXStore, new DamageFXStore(), NULL, "Data\\INI\\DamageFX.ini");
  276. initSubsystem(TheArmorStore, new ArmorStore(), NULL, "Data\\INI\\Armor.ini");
  277. initSubsystem(TheThingFactory, new ThingFactory(), "Data\\INI\\Default\\Object.ini", NULL, "Data\\INI\\Object");
  278. initSubsystem(TheCrateSystem, new CrateSystem(), "Data\\INI\\Default\\Crate.ini", "Data\\INI\\Crate.ini");
  279. initSubsystem(TheUpgradeCenter, new UpgradeCenter, "Data\\INI\\Default\\Upgrade.ini", "Data\\INI\\Upgrade.ini");
  280. initSubsystem(TheAnim2DCollection, new Anim2DCollection ); //Init's itself.
  281. _TheSubsystemList.postProcessLoadAll();
  282. TheWritableGlobalData->m_buildMapCache = TRUE;
  283. TheMapCache = new MapCache;
  284. // add in allowed maps
  285. for (std::list<std::string>::const_iterator cit = argvSet.begin(); cit != argvSet.end(); ++cit)
  286. {
  287. DEBUG_LOG(("Adding shipping map: '%s'\n", cit->c_str()));
  288. TheMapCache->addShippingMap((*cit).c_str());
  289. }
  290. TheMapCache->updateCache();
  291. delete TheMapCache;
  292. TheMapCache = NULL;
  293. // load the dialog box
  294. //DialogBox( hInstance, (LPCTSTR)IMAGE_PACKER_DIALOG,
  295. // NULL, (DLGPROC)ImagePackerProc );
  296. // delete TheGlobalData
  297. //delete TheGlobalData;
  298. //TheGlobalData = NULL;
  299. _TheSubsystemList.shutdownAll();
  300. delete TheFileSystem;
  301. TheFileSystem = NULL;
  302. delete TheNameKeyGenerator;
  303. TheNameKeyGenerator = NULL;
  304. }
  305. catch (...)
  306. {
  307. DEBUG_CRASH(("Munkee munkee!"));
  308. }
  309. // close the log
  310. shutdownMemoryManager();
  311. DEBUG_SHUTDOWN();
  312. // all done
  313. return 0;
  314. } // end WinMain