| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- /*
- XCC Utilities and Library
- Copyright (C) 2000 Olaf van der Spek <[email protected]>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "xcc_dirs.h"
- #include <boost/algorithm/string.hpp>
- #include <cassert>
- #include <windows.h>
- #include "reg_key.h"
- #include "string_conversion.h"
- #include "xcc_registry.h"
- bool g_enable_log = false;
- static string dune2_dir;
- static string td_primary_dir;
- static string td_secondary_dir;
- static string cd_dir;
- static string data_dir;
- static string ra_dir;
- static string dune2000_dir;
- static string ts_dir;
- static string ra2_dir;
- static string nox_dir;
- static string rg_dir;
- static string gr_dir;
- static string gr_zh_dir;
- static string ebfd_dir;
- static string bfme_dir;
- static string tw_dir;
- bool xcc_dirs::enable_log()
- {
- return g_enable_log;
- }
- string xcc_dirs::get_dir(t_game game)
- {
- switch (game)
- {
- case game_td:
- return td_primary_dir;
- case game_ra:
- return ra_dir;
- case game_ts:
- case game_ts_fs:
- return ts_dir;
- case game_dune2:
- return dune2_dir;
- case game_dune2000:
- return dune2000_dir;
- case game_ra2:
- case game_ra2_yr:
- return ra2_dir;
- case game_nox:
- return nox_dir;
- case game_rg:
- return rg_dir;
- case game_gr:
- return gr_dir;
- case game_gr_zh:
- return gr_zh_dir;
- case game_ebfd:
- return ebfd_dir;
- case game_bfme:
- return bfme_dir;
- case game_tw:
- return tw_dir;
- }
- assert(false);
- return "";
- }
- string xcc_dirs::get_exe(t_game game)
- {
- switch (game)
- {
- case game_td:
- return td_primary_dir + "c&c95.exe";
- case game_ra:
- return ra_dir + "ra95.exe";
- case game_ts:
- return ts_dir + "sun.exe";
- case game_dune2:
- return dune2_dir + "dune2.exe";
- case game_dune2000:
- return dune2000_dir + "dune2000.exe";
- case game_ra2:
- return ra2_dir + "ra2.exe";
- case game_nox:
- return nox_dir + "nox.exe";
- case game_ra2_yr:
- return ra2_dir + "ra2md.exe";
- case game_gr:
- return gr_dir + "generals.exe";
- case game_bfme:
- return bfme_dir + "lotrbfme.exe";
- }
- assert(false);
- return "";
- }
- string xcc_dirs::get_audio_mix(t_game game)
- {
- switch (game)
- {
- case game_ra2:
- return "audio.mix";
- case game_ra2_yr:
- return "audiomd.mix";
- }
- assert(false);
- return "";
- }
- string xcc_dirs::get_csf_fname(t_game game)
- {
- switch (game)
- {
- case game_ra2:
- return "ra2.csf";
- case game_ra2_yr:
- return "ra2md.csf";
- case game_gr:
- case game_gr_zh:
- return "data/english/generals.csf";
- }
- assert(false);
- return "";
- }
- static string get_suffix(t_game game)
- {
- return game == game_ra2_yr ? "md" : "";
- }
- string xcc_dirs::get_ecache_mix(t_game game, bool dir, int i)
- {
- return get_ecache_mix(game, dir, nwzl(2, i));
- }
- string xcc_dirs::get_ecache_mix(t_game game, bool dir, const string& s)
- {
- string r = "ecache" + get_suffix(game) + s + ".mix";
- if (dir)
- r = get_dir(game) + r;
- return r;
- }
- string xcc_dirs::get_expand_mix(t_game game, int i)
- {
- return get_expand_mix(game, nwzl(2, i));
- }
- string xcc_dirs::get_expand_mix(t_game game, const string& s)
- {
- return get_dir(game) + "expand" + get_suffix(game) + s + ".mix";
- }
- string xcc_dirs::get_language_mix(t_game game)
- {
- switch (game)
- {
- case game_ra2:
- return ra2_dir + "language.mix";
- case game_ra2_yr:
- return ra2_dir + "langmd.mix";
- case game_gr:
- return gr_dir + "english.big";
- case game_gr_zh:
- return gr_zh_dir + "englishzh.big";
- }
- assert(false);
- return "";
- }
- string xcc_dirs::get_local_mix(t_game game)
- {
- switch (game)
- {
- case game_ts:
- case game_ra2:
- return "local.mix";
- case game_ra2_yr:
- return "localmd.mix";
- }
- assert(false);
- return "";
- }
- string xcc_dirs::get_main_mix(t_game game)
- {
- switch (game)
- {
- case game_ra:
- return ra_dir + "redalert.mix";
- case game_ts:
- return ts_dir + "tibsun.mix";
- case game_ra2:
- return ra2_dir + "ra2.mix";
- case game_ra2_yr:
- return ra2_dir + "ra2md.mix";
- }
- assert(false);
- return "";
- }
- static void set_path(string s, string& path)
- {
- boost::to_lower(s);
- if (!s.empty() && s.back() != '\\')
- s += '\\';
- path = s;
- }
- void xcc_dirs::set_dir(t_game game, const string &s)
- {
- switch (game)
- {
- case game_td:
- set_path(s, td_primary_dir);
- break;
- case game_ra:
- set_path(s, ra_dir);
- break;
- case game_ts:
- set_path(s, ts_dir);
- break;
- case game_dune2:
- set_path(s, dune2_dir);
- break;
- case game_dune2000:
- set_path(s, dune2000_dir);
- break;
- case game_ra2:
- set_path(s, ra2_dir);
- break;
- case game_nox:
- set_path(s, nox_dir);
- break;
- case game_rg:
- set_path(s, rg_dir);
- break;
- case game_gr:
- set_path(s, gr_dir);
- break;
- case game_gr_zh:
- set_path(s, gr_zh_dir);
- break;
- case game_ebfd:
- set_path(s, ebfd_dir);
- break;
- case game_bfme:
- set_path(s, bfme_dir);
- break;
- case game_tw:
- set_path(s, tw_dir);
- break;
- default:
- assert(false);
- }
- }
- void xcc_dirs::set_td_secondary_dir(const string& s)
- {
- set_path(s, td_secondary_dir);
- }
- void xcc_dirs::set_cd_dir(const string& s)
- {
- set_path(s, cd_dir);
- }
- void xcc_dirs::set_data_dir(const string& s)
- {
- set_path(s, data_dir);
- }
- void xcc_dirs::reset_cd_dir()
- {
- int drive_map = GetLogicalDrives();
- char drive_root[] = "a:\\";
- for (int i = 0; i < 26; i++)
- {
- if (drive_map >> i & 1 && GetDriveTypeA(drive_root) == DRIVE_CDROM)
- {
- set_cd_dir(drive_root);
- break;
- }
- drive_root[0]++;
- }
- }
- void xcc_dirs::reset_data_dir()
- {
- set_data_dir(GetModuleFileName().get_path());
- }
- static void read_dir(const string& key, const string& value, t_game game)
- {
- Creg_key h;
- string s;
- if (xcc_dirs::get_dir(game).empty()
- && ERROR_SUCCESS == h.open(HKEY_LOCAL_MACHINE, key, KEY_QUERY_VALUE)
- && ERROR_SUCCESS == h.query_value(value, s))
- xcc_dirs::set_dir(game, Cfname(s).get_path());
- }
- void xcc_dirs::load_from_registry()
- {
- Creg_key kh_base;
- if (!Cxcc_registry::get_base_key(kh_base))
- {
- string s;
- if (ERROR_SUCCESS == kh_base.query_value("dune2_dir", s))
- set_dir(game_dune2, s);
- if (ERROR_SUCCESS == kh_base.query_value("dir1", s))
- set_dir(game_td, s);
- if (ERROR_SUCCESS == kh_base.query_value("dir2", s))
- set_td_secondary_dir(s);
- if (ERROR_SUCCESS == kh_base.query_value("ra_dir", s))
- set_dir(game_ra, s);
- if (ERROR_SUCCESS == kh_base.query_value("cd_dir", s))
- set_cd_dir(s);
- if (ERROR_SUCCESS == kh_base.query_value("data_dir", s))
- set_data_dir(s);
- if (ERROR_SUCCESS == kh_base.query_value("enable_log", s))
- g_enable_log = true;
- }
- if (cd_dir.empty())
- reset_cd_dir();
- if (data_dir.empty())
- reset_data_dir();
- read_dir("Software\\Westwood\\Dune 2", "InstallPath", game_dune2);
- read_dir("Software\\Westwood\\Command & Conquer Windows 95 Edition", "InstallPath", game_td);
- read_dir("Software\\Westwood\\Red Alert Windows 95 Edition", "InstallPath", game_ra);
- read_dir("Software\\Westwood\\Dune 2000", "InstallPath", game_dune2000);
- read_dir("Software\\Westwood\\Tiberian Sun", "InstallPath", game_ts);
- read_dir("Software\\Westwood\\Red Alert 2", "InstallPath", game_ra2);
- read_dir("Software\\Westwood\\Nox", "InstallPath", game_nox);
- read_dir("Software\\Westwood\\Renegade", "InstallPath", game_rg);
- read_dir("Software\\Westwood\\Emperor", "InstallPath", game_ebfd);
- /*
- read_dir("Software\\Electronic Arts\\EA Games\\Generals", "InstallPath", game_gr);
- read_dir("Software\\Electronic Arts\\EA Games\\Command and Conquer Generals Zero Hour", "InstallPath", game_gr_zh);
- read_dir("Software\\Electronic Arts\\EA Games\\The Battle for Middle-earth", "InstallPath", game_bfme);
- read_dir("Software\\Electronic Arts\\Electronic Arts\\Command and Conquer 3", "InstallPath", game_tw);
- */
- }
- void xcc_dirs::save_to_registry()
- {
- Creg_key kh_base;
- if (Cxcc_registry::get_base_key(kh_base))
- return;
- kh_base.set_value("dune2_dir", dune2_dir);
- kh_base.set_value("dir1", td_primary_dir);
- kh_base.set_value("dir2", td_secondary_dir);
- kh_base.set_value("ra_dir", ra_dir);
- };
- string xcc_dirs::find_file(Cfname s)
- {
- if (!s.get_path().empty() || s.exists())
- return s;
- s.set_path(td_primary_dir);
- if (s.exists())
- return s;
- s.set_path(td_secondary_dir);
- if (s.exists())
- return s;
- s.set_path(ra_dir);
- if (s.exists())
- return s;
- s.set_path(ts_dir);
- if (s.exists())
- return s;
- s.set_path(ra2_dir);
- if (s.exists())
- return s;
- s.set_path(cd_dir);
- return s;
- }
- bool xcc_dirs::is_available(t_game game)
- {
- return Cfname(get_exe(game)).exists();
- }
- const string& xcc_dirs::get_td_secondary_dir()
- {
- return td_secondary_dir;
- }
- const string& xcc_dirs::get_cd_dir()
- {
- return cd_dir;
- }
- const string& xcc_dirs::get_data_dir()
- {
- return data_dir;
- }
|