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

ImGui related fixes #342

Merged
merged 2 commits into from
Sep 15, 2024
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
32 changes: 9 additions & 23 deletions spt/cvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,36 +239,22 @@ const char* WrangleLegacyCommandName(const char* name, bool useTempStorage, bool

size_t allocLen = newName ? strlen(newName) + 2 // ex: +y_spt_duckspam
: strlen(name) + 5; // ex: tas_pause, +myprefix_something
AssertMsg(allocLen < SPT_MAX_CVAR_NAME_LEN, "spt: cvar name too long!");

// (re)allocate new string
char* allocStr;
if (useTempStorage)
{
if (allocLen > tmpLen)
{
delete[] tmpStr;
tmpStr = new char[allocLen];
tmpLen = allocLen;
}
allocStr = tmpStr;
}
else
{
allocStr = new char[allocLen];
}
static char tmpStorage[SPT_MAX_CVAR_NAME_LEN];
char* retStr = useTempStorage ? tmpStorage : new char[allocLen];
retStr[0] = name[0];

if (newName)
{
allocStr[0] = name[0];
strcpy(allocStr + 1, newName);
strcpy(retStr + 1, newName);
}
else
{
allocStr[0] = name[0];
strcpy(allocStr + plusMinus, "spt_");
strcat(allocStr, name + plusMinus);
strcpy(retStr + plusMinus, "spt_");
strcat(retStr, name + plusMinus);
}
return allocStr;
return retStr;
}

// Check if spt_ prefix is at the start. If not, create a new command and hide the old one with
Expand Down Expand Up @@ -306,7 +292,7 @@ static void HandleBackwardsCompatibility(FeatureCommand& featCmd, const char* cm
}

// After the push the featCmd reference can be invalid!!!
FeatureCommand newCmd = { featCmd.owner, newCommand, true, allocatedName, false };
FeatureCommand newCmd = {featCmd.owner, newCommand, true, allocatedName, false};
cmd_to_feature.push_back(newCmd);
}

Expand Down
7 changes: 5 additions & 2 deletions spt/cvars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "convar.hpp"

// max length including null terminator
#define SPT_MAX_CVAR_NAME_LEN 64

void Cvar_RegisterSPTCvars();
void Cvar_UnregisterSPTCvars();
void Cvar_InitConCommandBase(ConCommandBase& concommand, void* owner);
Expand All @@ -27,8 +30,8 @@ extern ConVar* _sv_bounce;
extern ConVar* _sv_cheats;

/*
* Converts legacy SPT command names to new ones. Uses either a temporary string that is only
* valid until the next call, or allocates a new string that must be deallocated manually.
* Converts legacy SPT command names to new ones. Uses either a temporary string that is only valid
* until the next call, or allocates a new string w/ new[] that must be deallocated manually.
*
* Examples:
* spt_hud_position -> spt_hud_position (allocated=false)
Expand Down
2 changes: 1 addition & 1 deletion spt/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/*
* Prefer using DECL_STATIC_HOOK_XXX over DECL_HOOK_XXX.
* The difference is that with the latter you can do:
* The difference is that with the former you can do:
*
* IMPL_HOOK_XXX(SptFeature, void, GameFn, int arg1) {
* ORIG_GameFn(arg1);
Expand Down
13 changes: 1 addition & 12 deletions spt/features/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,6 @@ std::string Updater::Request(const char* url)
return response;
}

static int GetPluginBuildNumber()
{
// Use the way Source Engine calculates the build number to calculate SPT "build number"
int pluginBuildNumber = 0;
char sptVersion[12];
snprintf(sptVersion, sizeof sptVersion, SPT_VERSION);
pluginBuildNumber = utils::DateToBuildNumber(sptVersion);
return pluginBuildNumber;
}

bool Updater::FetchReleaseInfo()
{
std::string data = Request("https://api.github.com/repos/YaLTeR/SourcePauseTool/releases/latest");
Expand Down Expand Up @@ -422,7 +412,6 @@ void Updater::UpdatePlugin()
return;
}


fs::copy(tmpPath, sptPath, ec);
if (ec)
{
Expand Down Expand Up @@ -475,7 +464,7 @@ int Updater::CheckUpdate()
}

int latestBuildNum = release.build;
int currentBuildNum = GetPluginBuildNumber();
int currentBuildNum = utils::DateToBuildNumber(SPT_VERSION);

DevMsg("Latest: %d\n", latestBuildNum);
DevMsg("Current: %d\n", currentBuildNum);
Expand Down
8 changes: 4 additions & 4 deletions spt/features/visualizations/imgui/spt_imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ void SptImGui::TextInputAutocomplete(const char* inputTextLabel,
* results. If we change this to a more generic (non ConCommand) based function,
* feed the textbox input directly to the autocomplete func instead of prefixing it.
*/
static ImGuiTextBuffer tmpBuf;
tmpBuf.Buf.clear();
tmpBuf.appendf("%s %s", ud.cmdName, data->Buf);
pd.nAutocomplete = ud.autocompleteFn(tmpBuf.Buf.Data, pd.autocomplete);
static char tmpBuf[SPT_MAX_CVAR_NAME_LEN + COMMAND_COMPLETION_ITEM_LENGTH + 2];
snprintf(tmpBuf, sizeof tmpBuf, "%s %s", ud.cmdName, data->Buf);
tmpBuf[sizeof(tmpBuf) - 1] = '\0';
pd.nAutocomplete = ud.autocompleteFn(tmpBuf, pd.autocomplete);
size_t nTrim = strlen(ud.cmdName) + 1;
for (int i = 0; i < pd.nAutocomplete; i++)
memmove(pd.autocomplete[i], pd.autocomplete[i] + nTrim, data->BufSize - nTrim);
Expand Down