Ompimize modlist

This commit is contained in:
momo5502 2016-04-04 17:18:57 +02:00
parent 883f71b7bb
commit b912eb618b
7 changed files with 61 additions and 58 deletions

2
deps/libtomcrypt vendored

@ -1 +1 @@
Subproject commit 912eff4949f46c0b426d2180429a6fa4c1144f1d
Subproject commit bb56ef08eb80854e78011da1f99a35f7cb2411fd

2
deps/protobuf vendored

@ -1 +1 @@
Subproject commit 5e933847cc9e7826f1a9ee8b3dc1df4960b1ea5d
Subproject commit 452e2b2c5c607ab5d63cd813793f1aa960f19d1c

View File

@ -67,6 +67,29 @@ namespace Components
return fileList;
}
std::vector<std::string> FileSystem::GetSysFileList(std::string path, std::string extension, bool folders)
{
std::vector<std::string> fileList;
int numFiles = 0;
char** files = Game::Sys_ListFiles(path.data(), extension.data(), NULL, &numFiles, folders);
if (files)
{
for (int i = 0; i < numFiles; ++i)
{
if (files[i])
{
fileList.push_back(files[i]);
}
}
Game::Sys_FreeFileList(files);
}
return fileList;
}
void FileSystem::DeleteFile(std::string folder, std::string file)
{
char path[MAX_PATH] = { 0 };

View File

@ -41,6 +41,7 @@ namespace Components
const char* GetName() { return "FileSystem"; };
static std::vector<std::string> GetFileList(std::string path, std::string extension);
static std::vector<std::string> GetSysFileList(std::string path, std::string extension, bool folders = false);
static void DeleteFile(std::string folder, std::string file);
private:

View File

@ -2,39 +2,34 @@
namespace Components
{
static Dvar::Var cl_modVidRestart;
std::vector<std::string> ModList::Mods;
unsigned int ModList::CurrentMod;
ModList::modInfo_t ModList::modInfo;
bool ModList::hasMod(const char* modName)
bool ModList::HasMod(std::string modName)
{
int count;
auto fs_homepath = Dvar::Var("fs_basepath").Get<const char*>();
char** mods = Game::Sys_ListFiles((char*)Utils::VA("%s\\%s", fs_homepath, "mods"), NULL, NULL, &count, 1);
auto list = FileSystem::GetSysFileList(Dvar::Var("fs_basepath").Get<std::string>() + "\\mods", "", true);
for (int i = 0; i < count; i++)
for (auto mod : list)
{
if (!_stricmp(modName, mods[i]))
if (mod == modName)
{
Game::Sys_FreeFileList(mods);
return true;
}
}
Game::Sys_FreeFileList(mods);
return false;
}
unsigned int ModList::GetItemCount()
{
return ModList::modInfo.max;
return ModList::Mods.size();
}
const char* ModList::GetItemText(unsigned int index, int column)
{
if (ModList::modInfo.current >= 0 && ModList::modInfo.current < ModList::modInfo.max)
if (index < ModList::Mods.size())
{
return ModList::modInfo.mods[index];
return ModList::Mods[index].data();
}
return "...";
@ -42,50 +37,43 @@ namespace Components
void ModList::Select(unsigned int index)
{
ModList::modInfo.current = index;
ModList::CurrentMod = index;
}
void ModList::UIScript_LoadMods()
{
if (ModList::modInfo.mods != NULL && *ModList::modInfo.mods != NULL)
{
Game::Sys_FreeFileList(ModList::modInfo.mods);
}
auto fs_homepath = Dvar::Var("fs_basepath").Get<const char*>();
auto searchFolder = (char*)Utils::VA("%s\\%s", fs_homepath, "mods");
Game::Com_Printf(0, "Searching for mods in %s...\n", searchFolder);
ModList::modInfo.mods = Game::Sys_ListFiles(searchFolder, NULL, NULL, &ModList::modInfo.max, 1);
Game::Com_Printf(0, "Found %i mods!\n", ModList::modInfo.max);
auto folder = Dvar::Var("fs_basepath").Get<std::string>() + "\\mods";
Game::Com_Printf(0, "Searching for mods in %s...\n", folder.data());
ModList::Mods = FileSystem::GetSysFileList(folder, "", true);
Game::Com_Printf(0, "Found %i mods!\n", ModList::Mods.size());
}
void ModList::UIScript_RunMod()
{
if (ModList::modInfo.mods != NULL
&& *ModList::modInfo.mods != NULL
&& ModList::modInfo.current >= 0
&& ModList::modInfo.current < ModList::modInfo.max)
if (ModList::CurrentMod < ModList::Mods.size())
{
Dvar::Var("fs_game").Set(Utils::VA("mods/%s", ModList::modInfo.mods[ModList::modInfo.current]));
//Game::Cmd_ExecuteSingleCommand(0, 0, Utils::VA("fs_game \"mods/%s\"", modInfo.mods[modInfo.current]));
if (cl_modVidRestart.Get<bool>())
auto fsGame = Dvar::Var("fs_game");
fsGame.Set(Utils::VA("mods/%s", ModList::Mods[ModList::CurrentMod].data()));
fsGame.Get<Game::dvar_t*>()->pad2[0] = 1;
if (Dvar::Var("cl_modVidRestart").Get<bool>())
{
Game::Cmd_ExecuteSingleCommand(0, 0, "vid_restart");
Command::Execute("vid_restart", false);
}
else
{
Game::Cmd_ExecuteSingleCommand(0, 0, "closemenu mods_menu");
Command::Execute("closemenu mods_menu", false);
}
}
}
void ModList::UIScript_ClearMods()
{
Dvar::Var("fs_game").Set("");
//Game::Cmd_ExecuteSingleCommand(0, 0, "fs_game \"\"");
if (cl_modVidRestart.Get<bool>())
auto fsGame = Dvar::Var("fs_game");
fsGame.Set("");
fsGame.Get<Game::dvar_t*>()->pad2[0] = 1;
if (Dvar::Var("cl_modVidRestart").Get<bool>())
{
Game::Cmd_ExecuteSingleCommand(0, 0, "vid_restart");
}
@ -97,12 +85,8 @@ namespace Components
ModList::ModList()
{
Dvar::OnInit([]()
{
cl_modVidRestart = Dvar::Register("cl_modVidRestart", true, Game::dvar_flag::DVAR_FLAG_SAVED, "Perform a vid_restart when loading a mod.");
});
ModList::modInfo.max = ModList::modInfo.current = 0;
ModList::CurrentMod = 0;
Dvar::Register("cl_modVidRestart", true, Game::dvar_flag::DVAR_FLAG_SAVED, "Perform a vid_restart when loading a mod.");
UIScript::Add("LoadMods", ModList::UIScript_LoadMods);
UIScript::Add("RunMod", ModList::UIScript_RunMod);
@ -113,5 +97,6 @@ namespace Components
ModList::~ModList()
{
ModList::Mods.clear();
}
}

View File

@ -5,19 +5,13 @@ namespace Components
public:
ModList();
~ModList();
const char* GetName() { return "Mods"; };
const char* GetName() { return "ModList"; };
private:
struct modInfo_t
{
char** mods;
int max;
int current;
};
static std::vector<std::string> Mods;
static unsigned int CurrentMod;
static bool hasMod(const char* modName);
static modInfo_t modInfo;
static bool HasMod(std::string modName);
static unsigned int GetItemCount();
static const char* GetItemText(unsigned int index, int column);

View File

@ -317,7 +317,7 @@ namespace Game
typedef bool(__cdecl * Sys_IsMainThread_t)();
extern Sys_IsMainThread_t Sys_IsMainThread;
typedef char** (__cdecl * Sys_ListFiles_t)(char* path, char* extension, int noclue, int* amount, bool listFolders);
typedef char** (__cdecl * Sys_ListFiles_t)(const char *directory, const char *extension, const char *filter, int *numfiles, int wantsubs);
extern Sys_ListFiles_t Sys_ListFiles;
typedef bool(__cdecl * Sys_SendPacket_t)(netsrc_t sock, size_t len, const char *format, netadr_t adr);