Change String_IndexOfString to just String_IndexOfConst, because that's all we ever use it for anyways

This commit is contained in:
UnknownShadow200 2020-04-04 14:50:14 +11:00
parent d3726bdef6
commit 209a4046fb
13 changed files with 43 additions and 58 deletions

View file

@ -221,11 +221,11 @@ a { text-decoration: none; }
#### static/jquery.js
Download some version of jQuery for this. Version 2.1.1 is known to work.
## Results
## Notes
* If you don't want the game to resize to fit different resolutions, remove the `resizeGameCanvas` code.
* tmpl_lite.html and
* tmpl_lite.html and mobile_mode is used to deliver a minified page for mobile/tablet devices
## Results

View file

@ -84,4 +84,4 @@ You are required to have this HTML code somewhere in the page:
### Complete example
The links below show implementing a simple website that hosts the web client
* [Flask (python webserver)](hosting-full-example.md)
* [Flask (python webserver)](hosting-flask.md)

View file

@ -172,14 +172,13 @@ cc_bool HacksComp_CanJumpHigher(struct HacksComp* hacks) {
return hacks->Enabled && hacks->CanSpeed;
}
static String HacksComp_UNSAFE_FlagValue(const char* flagRaw, struct HacksComp* hacks) {
static String HacksComp_UNSAFE_FlagValue(const char* flag, struct HacksComp* hacks) {
String* joined = &hacks->HacksFlags;
String flag = String_FromReadonly(flagRaw);
int beg, end;
beg = String_IndexOfString(joined, &flag);
beg = String_IndexOfConst(joined, flag);
if (beg < 0) return String_Empty;
beg += flag.length;
beg += String_CalcLen(flag, UInt16_MaxValue);
end = String_IndexOfAt(joined, beg, ' ');
if (end < 0) end = joined->length;
@ -205,26 +204,20 @@ static int HacksComp_ParseFlagInt(const char* flagRaw, struct HacksComp* hacks)
return value;
}
static void HacksComp_ParseFlag(struct HacksComp* hacks, const char* incFlag, const char* excFlag, cc_bool* target) {
String include = String_FromReadonly(incFlag);
String exclude = String_FromReadonly(excFlag);
static void HacksComp_ParseFlag(struct HacksComp* hacks, const char* include, const char* exclude, cc_bool* target) {
String* joined = &hacks->HacksFlags;
if (String_ContainsString(joined, &include)) {
if (String_ContainsConst(joined, include)) {
*target = true;
} else if (String_ContainsString(joined, &exclude)) {
} else if (String_ContainsConst(joined, exclude)) {
*target = false;
}
}
static void HacksComp_ParseAllFlag(struct HacksComp* hacks, const char* incFlag, const char* excFlag) {
String include = String_FromReadonly(incFlag);
String exclude = String_FromReadonly(excFlag);
static void HacksComp_ParseAllFlag(struct HacksComp* hacks, const char* include, const char* exclude) {
String* joined = &hacks->HacksFlags;
if (String_ContainsString(joined, &include)) {
if (String_ContainsConst(joined, include)) {
HacksComp_SetAll(hacks, true);
} else if (String_ContainsString(joined, &exclude)) {
} else if (String_ContainsConst(joined, exclude)) {
HacksComp_SetAll(hacks, false);
}
}
@ -243,9 +236,8 @@ void HacksComp_SetUserType(struct HacksComp* hacks, cc_uint8 value, cc_bool setB
}
void HacksComp_RecheckFlags(struct HacksComp* hacks) {
static const String noHaxFlag = String_FromConst("-hax");
/* Can use hacks by default (also case with WoM), no need to check +hax */
cc_bool hax = !String_ContainsString(&hacks->HacksFlags, &noHaxFlag);
cc_bool hax = !String_ContainsConst(&hacks->HacksFlags, "-hax");
HacksComp_SetAll(hacks, hax);
hacks->CanBePushed = true;

View file

@ -1903,7 +1903,6 @@ static void Gfx_RestoreState(void) {
}
cc_bool Gfx_WarnIfNecessary(void) {
static const String intel = String_FromConst("Intel");
String renderer = String_FromReadonly((const char*)glGetString(GL_RENDERER));
#ifdef CC_BUILD_GL11
@ -1911,7 +1910,7 @@ cc_bool Gfx_WarnIfNecessary(void) {
Chat_AddRaw("&cAs such you may experience poor performance.");
Chat_AddRaw("&cIt is likely you need to install video card drivers.");
#endif
if (!String_ContainsString(&renderer, &intel)) return false;
if (!String_ContainsConst(&renderer, "Intel")) return false;
Chat_AddRaw("&cIntel graphics cards are known to have issues with the OpenGL build.");
Chat_AddRaw("&cVSync may not work, and you may see disappearing clouds and map edges.");

View file

@ -476,11 +476,9 @@ static struct HttpCacheEntry http_cache[HTTP_CACHE_ENTRIES];
/* Splits up the components of a URL */
static void HttpCache_MakeEntry(const String* url, struct HttpCacheEntry* entry, String* resource) {
static const String schemeEnd = String_FromConst("://");
String scheme, path, addr, name, port;
/* URL is of form [scheme]://[server name]:[server port]/[resource] */
int idx = String_IndexOfString(url, &schemeEnd);
int idx = String_IndexOfConst(url, "://");
scheme = idx == -1 ? String_Empty : String_UNSAFE_Substring(url, 0, idx);
path = idx == -1 ? *url : String_UNSAFE_SubstringAt(url, idx + 3);

View file

@ -1586,10 +1586,6 @@ void FontListScreen_Show(void) {
*#########################################################################################################################*/
/* TODO: Hotkey added event for CPE */
static void HotkeyListScreen_EntryClick(void* screen, void* widget) {
static const String ctrl = String_FromConst("Ctrl");
static const String shift = String_FromConst("Shift");
static const String alt = String_FromConst("Alt");
struct ListScreen* s = (struct ListScreen*)screen;
struct HotkeyData h, original = { 0 };
String text, key, value;
@ -1603,9 +1599,9 @@ static void HotkeyListScreen_EntryClick(void* screen, void* widget) {
}
String_UNSAFE_Separate(&text, '+', &key, &value);
if (String_ContainsString(&value, &ctrl)) flags |= HOTKEY_MOD_CTRL;
if (String_ContainsString(&value, &shift)) flags |= HOTKEY_MOD_SHIFT;
if (String_ContainsString(&value, &alt)) flags |= HOTKEY_MOD_ALT;
if (String_ContainsConst(&value, "Ctrl")) flags |= HOTKEY_MOD_CTRL;
if (String_ContainsConst(&value, "Shift")) flags |= HOTKEY_MOD_SHIFT;
if (String_ContainsConst(&value, "Alt")) flags |= HOTKEY_MOD_ALT;
trigger = Utils_ParseEnum(&key, KEY_NONE, Input_Names, INPUT_COUNT);
for (i = 0; i < HotkeysText.count; i++) {

View file

@ -145,7 +145,7 @@ static cc_bool RayTrace(struct RayTracer* t, const Vec3* origin, const Vec3* dir
IVec3 pOrigin;
cc_bool insideMap;
float reachSq;
Vec3 v, minBB, maxBB;
Vec3 v;
float dxMin, dxMax, dx;
float dyMin, dyMax, dy;

View file

@ -211,17 +211,16 @@ static void WoM_UpdateIdentifier(void) {
}
static void WoM_CheckMotd(void) {
static const String cfg = String_FromConst("cfg=");
String url; char urlBuffer[STRING_SIZE];
String motd, host;
int index;
motd = Server.MOTD;
if (!motd.length) return;
index = String_IndexOfString(&motd, &cfg);
index = String_IndexOfConst(&motd, "cfg=");
if (Game_PureClassic || index == -1) return;
host = String_UNSAFE_SubstringAt(&motd, index + cfg.length);
host = String_UNSAFE_SubstringAt(&motd, index + 4);
String_InitArray(url, urlBuffer);
String_Format1(&url, "http://%s", &host);
/* TODO: Replace $U with username */
@ -424,7 +423,8 @@ static void MapState_Read(struct MapState* m) {
}
static void Classic_StartLoading(void) {
World_NewMap();
World_Reset();
Event_RaiseVoid(&WorldEvents.NewMap);
Stream_ReadonlyMemory(&map_part, NULL, 0);
LoadingScreen_Show(&Server.Name, &Server.MOTD);
@ -515,8 +515,9 @@ static void Classic_LevelFinalise(cc_uint8* data) {
map_begunLoading = false;
WoM_CheckSendWomID();
if (map.allocFailed) return;
#ifdef EXTENDED_BLOCKS
if (map2.allocFailed) FreeMapStates();
if (map2.allocFailed) { FreeMapStates(); return; }
#endif
width = Stream_GetU16_BE(data + 0);
@ -527,13 +528,17 @@ static void Classic_LevelFinalise(cc_uint8* data) {
Chat_AddRaw("&cFailed to load map, try joining a different map");
Chat_AddRaw(" &cBlocks array size does not match volume of map");
FreeMapStates();
return;
}
World_SetNewMap(map.blocks, width, height, length);
#ifdef EXTENDED_BLOCKS
/* defer allocation of second map array if possible */
if (cpe_extBlocks && map2.blocks) World_SetMapUpper(map2.blocks);
if (cpe_extBlocks && map2.blocks) {
World_SetMapUpper(map2.blocks);
}
#endif
World_SetNewMap(map.blocks, width, height, length);
Event_RaiseVoid(&WorldEvents.MapLoaded);
}
static void Classic_SetBlock(cc_uint8* data) {
@ -1429,7 +1434,7 @@ static void CPE_Tick(void) {
*------------------------------------------------------Custom blocks------------------------------------------------------*
*#########################################################################################################################*/
static void BlockDefs_OnBlockUpdated(BlockID block, cc_bool didBlockLight) {
if (!World.Loaded) return;
if (!World.Blocks) return;
/* Need to refresh lighting when a block's light blocking state changes */
if (Blocks.BlocksLight[block] != didBlockLight) Lighting_Refresh();
}

View file

@ -1360,7 +1360,6 @@ static void GeneratingScreen_Init(void* screen) {
static void GeneratingScreen_EndGeneration(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
struct LocationUpdate update;
float x, z;
Gui_Remove((struct Screen*)&LoadingScreen);

View file

@ -343,15 +343,15 @@ void String_UNSAFE_TrimEnd(String* str) {
}
}
int String_IndexOfString(const String* str, const String* sub) {
int String_IndexOfConst(const String* str, const char* sub) {
int i, j;
for (i = 0; i < str->length; i++) {
for (j = 0; j < sub->length && (i + j) < str->length; j++) {
for (j = 0; sub[j] && (i + j) < str->length; j++) {
if (str->buffer[i + j] != sub->buffer[j]) break;
if (str->buffer[i + j] != sub[j]) break;
}
if (j == sub->length) return i;
if (sub[j] == '\0') return i;
}
return -1;
}

View file

@ -137,9 +137,9 @@ CC_API void String_UNSAFE_TrimEnd(String* str);
/* Returns first index of the given substring in the given string, -1 if not found. */
/* e.g. index of "ab" within "cbabd" is 2 */
CC_API int String_IndexOfString(const String* str, const String* sub);
CC_API int String_IndexOfConst(const String* str, const char* sub);
/* Returns non-zero if given substring is inside the given string. */
#define String_ContainsString(str, sub) (String_IndexOfString(str, sub) >= 0)
#define String_ContainsConst(str, sub) (String_IndexOfConst(str, sub) >= 0)
/* Returns non-zero if given substring is case-insensitively inside the given string. */
CC_API int String_CaselessContains(const String* str, const String* sub);
/* Returns non-zero if given substring is case-insensitively equal to the beginning of the given string. */
@ -150,7 +150,7 @@ CC_API int String_CaselessEnds(const String* str, const String* sub);
/* -X if a.length < b.length, X if a.length > b.length */
/* -X if a.buffer[i] < b.buffer[i], X if a.buffer[i] > b.buffer[i] */
/* else returns 0. NOTE: The return value is not just in -1,0,1! */
CC_API int String_Compare(const String* a, const String* b);
CC_API int String_Compare(const String* a, const String* b);
/* See String_Format4 */
CC_API void String_Format1(String* str, const char* format, const void* a1);

View file

@ -706,7 +706,6 @@ void TexturePack_ExtractZip_File(const String* filename) {
static cc_bool texturePackDefault = true;
void TexturePack_ExtractCurrent(cc_bool forceReload) {
static const String zipExt = String_FromConst(".zip");
String url = World_TextureUrl, file;
struct Stream stream;
cc_bool zip;
@ -720,7 +719,7 @@ void TexturePack_ExtractCurrent(cc_bool forceReload) {
TexturePack_ExtractZip_File(&file);
texturePackDefault = true;
} else {
zip = String_ContainsString(&url, &zipExt);
zip = String_ContainsConst(&url, ".zip");
res = zip ? TexturePack_ExtractZip(&stream) : TexturePack_ExtractPng(&stream);
if (res) Logger_Warn2(res, zip ? "extracting" : "decoding", &url);

View file

@ -20,11 +20,8 @@ int Utils_ParseEnum(const String* text, int defValue, const char* const* names,
}
cc_bool Utils_IsUrlPrefix(const String* value) {
static const String http = String_FromConst("http://");
static const String https = String_FromConst("https://");
return String_IndexOfString(value, &http) == 0
|| String_IndexOfString(value, &https) == 0;
return String_IndexOfConst(value, "http://") == 0
|| String_IndexOfConst(value, "https://") == 0;
}
cc_bool Utils_EnsureDirectory(const char* dirName) {