1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "SelectionWidget.h"
#ifndef HEADLESS
#include <functional>
#include "System/FileSystem/ArchiveScanner.h"
#include "System/FileSystem/DataDirsAccess.h"
#include "System/FileSystem/FileSystem.h"
#include "System/FileSystem/VFSHandler.h"
#include "System/Exceptions.h"
#include "System/Config/ConfigHandler.h"
#include "System/AIScriptHandler.h"
#include "ExternalAI/LuaAIImplHandler.h"
#include "ExternalAI/Interface/SSkirmishAILibrary.h"
#include "System/Info.h"
#include "alphanum.hpp"
const std::string SelectionWidget::NoDemoSelect = "No demo selected";
const std::string SelectionWidget::NoModSelect = "No game selected";
const std::string SelectionWidget::NoMapSelect = "No map selected";
const std::string SelectionWidget::NoScriptSelect = "No script selected";
const std::string SelectionWidget::SandboxAI = "Player Only: Testing Sandbox";
CONFIG(std::string, LastSelectedMod).defaultValue(SelectionWidget::NoModSelect).description("Stores the previously played game.");
CONFIG(std::string, LastSelectedMap).defaultValue(SelectionWidget::NoMapSelect).description("Stores the previously played map.");
CONFIG(std::string, LastSelectedScript).defaultValue(SelectionWidget::NoScriptSelect).description("Stores the previously played AI.");
// returns absolute filename for given archive name, empty if not found
static const std::string GetAbsFileName(const std::string& name) {
if (name.empty())
return name;
const std::string& filename = archiveScanner->ArchiveFromName(name);
if (filename == name)
return "";
return (archiveScanner->GetArchivePath(filename) + filename);
}
SelectionWidget::SelectionWidget(agui::GuiElement* parent) : agui::GuiElement(parent)
{
SetPos(0.5f, 0.2f);
SetSize(0.4f, 0.2f);
curSelect = nullptr;
agui::VerticalLayout* vl = new agui::VerticalLayout(this);
vl->SetBorder(true);
agui::HorizontalLayout* modL = new agui::HorizontalLayout(vl);
mod = new agui::Button("Select", modL);
mod->Clicked.connect(std::bind(&SelectionWidget::ShowModList, this));
mod->SetSize(0.1f, 0.00f, true);
userDemo = NoDemoSelect;
userMod = configHandler->GetString("LastSelectedMod");
userMap = configHandler->GetString("LastSelectedMap");
userScript = configHandler->GetString("LastSelectedScript");
if (GetAbsFileName(userMod).empty())
userMod = NoModSelect;
if (GetAbsFileName(userMap).empty())
userMap = NoMapSelect;
agui::HorizontalLayout* mapL = new agui::HorizontalLayout(vl);
map = new agui::Button("Select", mapL);
map->Clicked.connect(std::bind(&SelectionWidget::ShowMapList, this));
map->SetSize(0.1f, 0.00f, true);
agui::HorizontalLayout* scriptL = new agui::HorizontalLayout(vl);
script = new agui::Button("Select", scriptL);
script->Clicked.connect(std::bind(&SelectionWidget::ShowScriptList, this));
script->SetSize(0.1f, 0.00f, true);
modT = new agui::TextElement(userMod, modL);
mapT = new agui::TextElement(userMap, mapL);
scriptT = new agui::TextElement(userScript, scriptL);
UpdateAvailableScripts();
}
SelectionWidget::~SelectionWidget()
{
CleanWindow();
}
void SelectionWidget::ShowDemoList(const std::function<void(const std::string&)>& demoSelectCB)
{
if (curSelect != nullptr)
return;
curSelect = new ListSelectWnd("Select demo");
curSelect->Selected.connect(std::bind(&SelectionWidget::SelectDemo, this, std::placeholders::_1));
curSelect->WantClose.connect(std::bind(&SelectionWidget::CleanWindow, this));
const std::string cwd = std::move(FileSystem::EnsurePathSepAtEnd(FileSystemAbstraction::GetCwd()));
const std::string dir = std::move(FileSystem::EnsurePathSepAtEnd("demos"));
// FIXME: names overflow the box
for (const std::string& demo: dataDirsAccess.FindFiles(cwd + dir, "*.sdfz", 0)) {
curSelect->list->AddItem(demo.substr(demo.find(dir) + 6), "");
}
demoSelectedCB = demoSelectCB;
}
void SelectionWidget::ShowModList()
{
if (curSelect != nullptr)
return;
curSelect = new ListSelectWnd("Select game");
curSelect->Selected.connect(std::bind(&SelectionWidget::SelectMod, this, std::placeholders::_1));
curSelect->WantClose.connect(std::bind(&SelectionWidget::CleanWindow, this));
std::vector<CArchiveScanner::ArchiveData> found = std::move(archiveScanner->GetPrimaryMods());
std::sort(found.begin(), found.end(), [](const CArchiveScanner::ArchiveData& a, const CArchiveScanner::ArchiveData& b) {
return (doj::alphanum_less<std::string>()(a.GetNameVersioned(), b.GetNameVersioned()));
});
for (const CArchiveScanner::ArchiveData& ad: found) {
curSelect->list->AddItem(ad.GetNameVersioned(), ad.GetDescription());
}
curSelect->list->SetCurrentItem(userMod);
}
void SelectionWidget::ShowMapList()
{
if (curSelect != nullptr)
return;
curSelect = new ListSelectWnd("Select map");
curSelect->Selected.connect(std::bind(&SelectionWidget::SelectMap, this, std::placeholders::_1));
curSelect->WantClose.connect(std::bind(&SelectionWidget::CleanWindow, this));
std::vector<std::string> arFound = std::move(archiveScanner->GetMaps());
std::sort(arFound.begin(), arFound.end(), doj::alphanum_less<std::string>());
for (const std::string& arName: arFound) {
curSelect->list->AddItem(arName, arName);
}
curSelect->list->SetCurrentItem(userMap);
}
void SelectionWidget::AddAIScriptsFromArchive()
{
if (userMod == SelectionWidget::NoModSelect || userMap == SelectionWidget::NoMapSelect )
return;
vfsHandler->GrabLock();
vfsHandler->SetName("SelWidgetVFS");
// menu is only shown on startup, or after reload which already unmaps
vfsHandler->UnMapArchives(false);
// archives will be kept around for PreGame and stashed on reload
vfsHandler->AddArchiveWithDeps(userMod, false);
vfsHandler->AddArchiveWithDeps(userMap, false);
for (const auto& infoItem: luaAIImplHandler.LoadInfoItems()) {
for (const auto& infoProp: infoItem) {
if (infoProp.key == SKIRMISH_AI_PROPERTY_SHORT_NAME)
availableScripts.push_back(infoProp.GetValueAsString());
}
}
vfsHandler->ReMapArchives(false);
vfsHandler->SetName("SpringVFS");
vfsHandler->FreeLock();
}
void SelectionWidget::UpdateAvailableScripts()
{
//FIXME: lua ai's should be handled in AIScriptHandler.cpp, too respecting the selected game and map
// maybe also merge it with StartScriptGen.cpp
availableScripts.clear();
availableScripts.reserve(16);
// load selected archives to get lua ais
AddAIScriptsFromArchive();
// add sandbox script to list
availableScripts.push_back(SandboxAI);
// add native ai's to the list, too (but second, lua ai's are prefered)
for (const auto& pair: CAIScriptHandler::Instance().GetScriptMap()) {
availableScripts.push_back(pair.first);
}
if (std::find(availableScripts.begin(), availableScripts.end(), userScript) != availableScripts.end())
return;
SelectScript(SelectionWidget::NoScriptSelect);
}
void SelectionWidget::ShowScriptList()
{
if (curSelect != nullptr)
return;
curSelect = new ListSelectWnd("Select script");
curSelect->Selected.connect(std::bind(&SelectionWidget::SelectScript, this, std::placeholders::_1));
curSelect->WantClose.connect(std::bind(&SelectionWidget::CleanWindow, this));
for (const std::string& scriptName: availableScripts) {
curSelect->list->AddItem(scriptName, "");
}
curSelect->list->SetCurrentItem(userScript);
}
void SelectionWidget::SelectDemo(const std::string& demo)
{
CleanWindow();
demoSelectedCB("demos/" + demo);
}
void SelectionWidget::SelectMod(const std::string& mod)
{
if (mod == userMod) {
CleanWindow();
return;
}
configHandler->SetString("LastSelectedMod", userMod = mod);
modT->SetText(userMod);
//SelectScript(SelectionWidget::NoScriptSelect); //reset AI as LuaAI maybe doesn't exist in this game
UpdateAvailableScripts();
CleanWindow();
}
void SelectionWidget::SelectScript(const std::string& script)
{
configHandler->SetString("LastSelectedScript", userScript = script);
scriptT->SetText(userScript);
CleanWindow();
}
void SelectionWidget::SelectMap(const std::string& map)
{
configHandler->SetString("LastSelectedMap", userMap = map);
mapT->SetText(userMap);
UpdateAvailableScripts();
CleanWindow();
}
void SelectionWidget::CleanWindow()
{
if (curSelect == nullptr)
return;
agui::gui->RmElement(curSelect);
curSelect = nullptr;
}
#endif
|