Welcome to mirror list, hosted at ThFree Co, Russian Federation.

online_utility.cpp « Online « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9473502826d1238653b305610f56621a9c9bc47d (plain)
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
//-----------------------------------------------------------------------------
//           Name: online_utility.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include <Online/online_utility.h>
#include <Main/engine.h>
#include <Internal/config.h>

#if ENABLE_STEAMWORKS
#include <isteamfriends.h>
#endif

using std::stringstream;

// Name is invalid if (less than 3 letters) or (contains any non alpha numerical characters)
bool OnlineUtility::IsValidPlayerName(const std::string& name) {
    if (name.length() <= 2)
        return false;

    for (char c : name) {
        bool valid_char = (48 <= c && c <= 57) || (65 <= c && c <= 90) || (97 <= c && c <= 122) || c == 32;
        if (!valid_char) {
            return false;
        }
    }

    return true;
}

std::string OnlineUtility::GetDefaultPlayerName() {
#if ENABLE_STEAMWORKS
    if(SteamFriends() != nullptr) {
        const char * name_ptr = SteamFriends()->GetPersonaName();
        if(name_ptr != nullptr) {
            std::string default_name(name_ptr);
            if(IsValidPlayerName(default_name)) {
                return default_name;
            }
        }
    }
#endif

    // Steam name isn't valid, use fallback
    return "Unknown Rabbit";
}

std::string OnlineUtility::GetPlayerName() {
    return config.GetRef("playername").str();
}

std::string OnlineUtility::GetActiveModsString() {
    bool active_mods = false;
    stringstream modlist;
    vector<ModInstance*> mods = ModLoading::Instance().GetAllMods();
    for(auto & mod : mods) {
        if(mod->IsActive() && !mod->IsCore()) {
            if(active_mods) {
                modlist << ", ";
            }
            active_mods = true;

            if(mod->SupportsOnline()) {
                modlist << mod->id;
            } else {
                modlist << "[" << mod->id << "]";
            }
        }
    }

    return modlist.str();
}

bool OnlineUtility::HasActiveIncompatibleMods() {
    for (auto& it : ModLoading::Instance().GetAllMods()) {
        if (it->IsActive() && !it->IsCore() && !it->SupportsOnline()) {
            return true;
        }
    }
    return false;
}

std::string OnlineUtility::GetActiveIncompatibleModsString() {
    bool active_mods = false;
    stringstream modlist;
    vector<ModInstance*> mods = ModLoading::Instance().GetAllMods();
    for(auto & mod : mods) {
        if(mod->IsActive() && !mod->IsCore() && !mod->SupportsOnline()) {
            if(active_mods) {
                modlist << ", ";
            }
            active_mods = true;
            modlist << "\"" << mod->id << "\"";
        }
    }

    return modlist.str();
}

void OnlineUtility::HandleCommand(PlayerID playerID, const std::string& message)
{
    auto command = OnlineUtility::CommandFromString(message);
    //TODO send command to angelscript
}

bool OnlineUtility::IsCommand(const std::string& message)
{
    return message.size() > 1 && message.front() == '/';
}

std::vector<std::string> OnlineUtility::CommandFromString(const std::string& message)
{
    std::string command_str = message.substr(1);
    std::vector<std::string> command;

    size_t start = command_str.find_first_not_of(' ');
    size_t end = command_str.find_first_of(' ', start);
    while (start != std::string::npos) {
        command.push_back(command_str.substr(start, end != std::string::npos ? end - start : std::string::npos));
        start = command_str.find_first_not_of(' ', end);
        end = command_str.find_first_of(' ', start);
    }
    return command;
}