Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default arguments #427

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions src/Avara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,49 @@ void SetHiDPI() {

#include <SDL2/SDL.h>
#include <nanogui/nanogui.h>
#include <string.h>
#include <sstream>
#include <string>

using namespace nanogui;

void NullLogger(void *userdata, int category, SDL_LogPriority priority, const char *message) {}

// combine 'defaultArgs' and command-line arguments
std::vector<std::string> combinedArgs(std::string defaultArgs, int argc, char* argv[]) {
std::vector<std::string> args;
// first parse/insert the defaultArgs
std::stringstream ss(defaultArgs);
std::string arg, text;
while (std::getline(ss, text, ' ')) {
// look for quoted text
if (text[0] == '\'') {
arg = text.substr(1);
// wait for the entire quoted string
continue;
} else if (arg.size() > 0) {
// append to existing until we find the final quote
arg += ' ';
size_t lastChar = text.size() - 1;
if (text[lastChar] == '\'') {
arg += text.substr(0, lastChar);
} else {
arg += text;
continue;
}
} else {
arg = text;
}
args.push_back(arg);
arg = "";
}

// now add actual command-line arguments (inserted AFTER defaultArgs so they override)
for (int i = 1; i < argc; i++) {
args.push_back(std::string(argv[i]));
}
return args;
}

int main(int argc, char *argv[]) {
// Allow Windows to run in HiDPI mode.
SetHiDPI();
Expand All @@ -58,58 +95,56 @@ int main(int argc, char *argv[]) {
OpenAvaraTCP();

// The Avara application itself.
CAvaraApp *app = new CAvaraAppImpl();
CAvaraAppImpl *app = new CAvaraAppImpl();

// process command-line arguments
std::string connectAddress;
std::vector<std::string> textCommands;
bool host = false;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
std::vector<std::string> args = combinedArgs(app->Get<std::string>(kDefaultArgs), argc, argv);
for (int i = 0; i < args.size(); i++) {
std::string &arg = args[i];
if (arg == "-p" || arg == "--port") {
int port = atoi(argv[++i]); // pre-inc to next arg
int port = atoi(args[++i].c_str()); // pre-inc to next arg
app->Set(kDefaultClientUDPPort, port);
} else if (arg == "-n" || arg == "--name") {
app->Set(kPlayerNameTag, std::string(argv[++i]));
app->Set(kPlayerNameTag, args[++i]);
} else if (arg == "-c" || arg == "--connect") {
connectAddress = std::string(argv[++i]);
connectAddress = args[++i];
app->Set(kLastAddress, connectAddress);
} else if (arg == "-s" || arg == "--serve" ||
arg == "-S" || arg == "--Serve") {
host = true;
app->Set(kTrackerRegister, arg[1] == 'S' || arg[2] == 'S');
} else if (arg == "-f" || arg == "--frametime") {
uint16_t frameTime = atol(argv[++i]); // pre-inc to next arg
uint16_t frameTime = atol(args[++i].c_str()); // pre-inc to next arg
app->GetGame()->SetFrameTime(frameTime);
} else if (arg == "-i" || arg == "--keys-from-stdin") {
app->GetGame()->SetKeysFromStdin();
} else if (arg == "-if" || arg == "--keys-from-file") {
// redirect a playback file to stdin
freopen(argv[++i], "r", stdin);
freopen(args[++i].c_str(), "r", stdin);
app->GetGame()->SetKeysFromStdin();
} else if (arg == "-o" || arg == "--keys-to-stdout") {
app->GetGame()->SetKeysToStdout();
} else if (arg == "-/" || arg == "--command") {
std::string textCommand = argv[++i];
std::string textCommand = args[++i];
if (textCommand[0] != '/') {
textCommand.insert(0, "/");
}
textCommands.push_back(textCommand);
} else {
SDL_Log("Unknown command-line argument '%s'\n", argv[i]);
SDL_Log("Unknown command-line argument '%s'\n", args[i].c_str());
exit(1);
}
}

auto p = CPlayerManagerImpl::LocalPlayer();
auto *tui = ((CAvaraAppImpl *)app)->GetTui();
auto defaultCmd = "/rand avara aa emo ex #fav -#bad -#koth";
if (textCommands.size() > 0) {
auto p = CPlayerManagerImpl::LocalPlayer();
auto *tui = ((CAvaraAppImpl *)app)->GetTui();
for (auto cmd: textCommands) {
tui->ExecuteMatchingCommand(cmd, p);
}
} else {
tui->ExecuteMatchingCommand(defaultCmd, p);
}

if(host == true) {
Expand Down
2 changes: 1 addition & 1 deletion src/game/CNetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#if ROUTE_THRU_SERVER
#define kAvaraNetVersion 666
#else
#define kAvaraNetVersion 12
#define kAvaraNetVersion 13
#endif

#define kMessageBufferMaxAge 90
Expand Down
4 changes: 3 additions & 1 deletion src/gui/Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ using json = nlohmann::json;
// other
#define kGoodGamePhrases "ggs"
#define kShowElo "showElo"
#define kDefaultArgs "defaultArgs"

// Key names are from https://wiki.libsdl.org/SDL_Scancode
static json defaultPrefs = {
Expand Down Expand Up @@ -197,7 +198,8 @@ static json defaultPrefs = {
{kThrottle, 0},
{kGoodGamePhrases, {}},
{kShowElo, false},
{kUseLegacyRenderer, false}
{kUseLegacyRenderer, false},
{kDefaultArgs, "-/ '/rand avara aa emo ex #fav -#bad'"}
};


Expand Down
2 changes: 1 addition & 1 deletion src/net/CUDPComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ ClockTick CUDPComm::GetClock() {
** more urgent data. If not, any data marked urgent will be resent even if there
** no other data to send within twice that period.
*/
void CUDPComm::IUDPComm(short clientCount, short bufferCount, short version, ClockTick urgentTimePeriod) {
void CUDPComm::IUDPComm(short clientCount, short bufferCount, uint16_t version, ClockTick urgentTimePeriod) {
// create queues big enough to hold UDPPacketInfo packets
InitializePacketQueues(bufferCount, sizeof(UDPPacketInfo));

Expand Down
4 changes: 2 additions & 2 deletions src/net/CUDPComm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum { udpCramInfo }; // Selectors for kpPacketProtocolControl packer p1 params.
class CUDPComm : public CCommManager {
public:
int32_t seed;
short softwareVersion;
uint16_t softwareVersion;
short maxClients;
short clientLimit;
Str255 password;
Expand Down Expand Up @@ -96,7 +96,7 @@ class CUDPComm : public CCommManager {
Boolean specialWakeup;
Str255 inviteString;

virtual void IUDPComm(short clientCount, short bufferCount, short version, ClockTick urgentTimePeriod);
virtual void IUDPComm(short clientCount, short bufferCount, uint16_t version, ClockTick urgentTimePeriod);

virtual void Disconnect();
virtual void WritePrefs();
Expand Down