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
|
/*
Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>
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 2 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 <http://www.gnu.org/licenses/>.
*/
#include "IAILibraryManager.h"
#include "AILibraryManager.h"
#include "AIInterfaceKey.h"
#include "SkirmishAIKey.h"
#include <iostream>
#include <limits.h>
#include <string.h>
IAILibraryManager* IAILibraryManager::myAILibraryManager = NULL;
IAILibraryManager* IAILibraryManager::GetInstance() {
if (myAILibraryManager == NULL) {
myAILibraryManager = new CAILibraryManager();
}
return myAILibraryManager;
}
std::string fillUpTo(const std::string& str, unsigned int numChars) {
std::string filler = "";
filler.append(numChars - str.size(), ' ');
return filler;
}
void IAILibraryManager::OutputAIInterfacesInfo() {
const IAILibraryManager* myLibManager = IAILibraryManager::GetInstance();
const T_interfaceSpecs& keys =
myLibManager->GetInterfaceKeys();
std::cout << "#" << std::endl;
std::cout << "# Available Spring Skirmish AIs" << std::endl;
std::cout << "# -----------------------------" << std::endl;
std::cout << "# [Name] [Version]" << std::endl;
T_interfaceSpecs::const_iterator key;
for (key=keys.begin(); key != keys.end(); key++) {
std::cout << " ";
std::cout << key->GetShortName() << fillUpTo(key->GetShortName(), 20);
std::cout << key->GetVersion() << fillUpTo(key->GetVersion(), 20)
<< std::endl;
}
std::cout << "#" << std::endl;
}
SkirmishAIKey IAILibraryManager::ResolveSkirmishAIKey(
const SkirmishAIKey& skirmishAIKey) const {
std::vector<SkirmishAIKey> fittingKeys
= FittingSkirmishAIKeys(skirmishAIKey);
if (fittingKeys.size() > 0) {
// look for the one with the highest version number,
// in case there are multiple fitting ones.
size_t bestIndex = 0;
const std::string* bestVersion = &(fittingKeys[0].GetVersion());
for (size_t k = 1; k < fittingKeys.size(); ++k) {
if (IAILibraryManager::VersionCompare(fittingKeys[k].GetVersion(), *bestVersion) > 0) {
bestIndex = k;
bestVersion = &(fittingKeys[k].GetVersion());
}
}
bestVersion = NULL;
return fittingKeys[bestIndex];
} else {
return SkirmishAIKey();
}
}
void IAILibraryManager::OutputSkirmishAIInfo() {
const IAILibraryManager* myLibManager = IAILibraryManager::GetInstance();
const T_skirmishAIKeys& keys = myLibManager->GetSkirmishAIKeys();
std::cout << "#" << std::endl;
std::cout << "# Available Spring Skirmish AIs" << std::endl;
std::cout << "# -----------------------------" << std::endl;
std::cout << "# [Name] [Version] "
"[Interface-name] [Interface-version]" << std::endl;
T_skirmishAIKeys::const_iterator key;
for (key=keys.begin(); key != keys.end(); key++) {
std::cout << " ";
std::cout << key->GetShortName() << fillUpTo(key->GetShortName(), 20);
std::cout << key->GetVersion() << fillUpTo(key->GetVersion(), 20);
std::cout << key->GetInterface().GetShortName()
<< fillUpTo(key->GetInterface().GetShortName(), 20);
std::cout << key->GetInterface().GetVersion() << std::endl;
}
const T_dupSkirm& duplicateSkirmishAIInfos =
myLibManager->GetDuplicateSkirmishAIInfos();
for (T_dupSkirm::const_iterator info = duplicateSkirmishAIInfos.begin();
info != duplicateSkirmishAIInfos.end(); ++info) {
std::cout << "# WARNING: Duplicate Skirmish AI Info found:"
<< std::endl;
std::cout << "# \tfor Skirmish AI: " << info->first.GetShortName()
<< " " << info->first.GetVersion() << std::endl;
std::cout << "# \tin files:" << std::endl;
std::set<std::string>::const_iterator dir;
for (dir = info->second.begin(); dir != info->second.end(); ++dir) {
std::cout << "# \t" << dir->c_str() << std::endl;
}
}
std::cout << "#" << std::endl;
}
static std::vector<std::string> split(const std::string& str, const char sep) {
std::vector<std::string> tokens;
std::string delimitters = ".";
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimitters, 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of(delimitters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(sep, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimitters, lastPos);
}
return tokens;
}
int IAILibraryManager::VersionCompare(
const std::string& version1,
const std::string& version2) {
std::vector<std::string> parts1 = split(version1, '.');
std::vector<std::string> parts2 = split(version2, '.');
unsigned int maxParts = parts1.size() > parts2.size() ? parts1.size() : parts2.size();
int diff = 0;
for (unsigned int i=0; i < maxParts; ++i) {
const std::string& v1p = i < parts1.size() ? parts1.at(i) : "0";
const std::string& v2p = i < parts2.size() ? parts2.at(i) : "0";
diff += (1<<((maxParts-i)*2)) * v1p.compare(v2p);
}
// computed the sing of diff -> 1, 0 or -1
int sign = (diff != 0) | -(int)((unsigned int)((int)diff) >> (sizeof(int) * CHAR_BIT - 1));
return sign;
}
|