Cleanup Http/Drawer2D a little bit more

This commit is contained in:
UnknownShadow200 2020-11-08 00:21:53 +11:00
parent 6b63ccbd38
commit ce5611c91c
4 changed files with 61 additions and 63 deletions

View File

@ -59,36 +59,14 @@ void Drawer2D_SetDefaultFont(const cc_string* fontName) {
static int Drawer2D_AdjHeight(int point) { return Math_CeilDiv(point * 3, 2); } static int Drawer2D_AdjHeight(int point) { return Math_CeilDiv(point * 3, 2); }
void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags) { void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags) {
cc_string* font; if (!Drawer2D_BitmappedText) { Font_MakeDefault(desc, size, flags); return; }
cc_result res;
int i;
if (Drawer2D_BitmappedText) {
/* TODO: Scale X and Y independently */ /* TODO: Scale X and Y independently */
size = Display_ScaleY(size); size = Display_ScaleY(size);
desc->handle = NULL; desc->handle = NULL;
desc->size = size; desc->size = size;
desc->flags = flags; desc->flags = flags;
desc->height = Drawer2D_AdjHeight(size); desc->height = Drawer2D_AdjHeight(size);
return;
}
for (i = 0; i < Array_Elems(font_candidates); i++) {
font = &font_candidates[i];
if (!font->length) continue;
res = Font_Make(desc, &font_candidates[i], size, flags);
if (res == ERR_INVALID_ARGUMENT) {
/* Fon't doesn't exist in list, skip over it */
} else if (res) {
Font_Free(desc);
Logger_SysWarn2(res, "creating font", font);
} else {
if (i) String_Copy(&font_candidates[0], font);
return;
}
}
Logger_Abort2(res, "Failed to init default font");
} }
static struct Bitmap fontBitmap; static struct Bitmap fontBitmap;
@ -706,6 +684,8 @@ cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size,
desc->height = 0; desc->height = 0;
return 0; return 0;
} }
void Font_MakeDefault(struct FontDesc* desc, int size, int flags) { Font_Make(desc, NULL, size, flags); }
void Font_Free(struct FontDesc* desc) { void Font_Free(struct FontDesc* desc) {
desc->size = 0; desc->size = 0;
} }
@ -1034,6 +1014,29 @@ cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size,
return 0; return 0;
} }
void Font_MakeDefault(struct FontDesc* desc, int size, int flags) {
cc_string* font;
cc_result res;
int i;
for (i = 0; i < Array_Elems(font_candidates); i++) {
font = &font_candidates[i];
if (!font->length) continue;
res = Font_Make(desc, &font_candidates[i], size, flags);
if (res == ERR_INVALID_ARGUMENT) {
/* Fon't doesn't exist in list, skip over it */
} else if (res) {
Font_Free(desc);
Logger_SysWarn2(res, "creating font", font);
} else {
if (i) String_Copy(&font_candidates[0], font);
return;
}
}
Logger_Abort2(res, "Failed to init default font");
}
void Font_Free(struct FontDesc* desc) { void Font_Free(struct FontDesc* desc) {
struct SysFont* font; struct SysFont* font;
desc->size = 0; desc->size = 0;

View File

@ -20,7 +20,7 @@ void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, struct FontDesc* font, cc
/* Sets default system font name and raises ChatEvents.FontChanged */ /* Sets default system font name and raises ChatEvents.FontChanged */
void Drawer2D_SetDefaultFont(const cc_string* fontName); void Drawer2D_SetDefaultFont(const cc_string* fontName);
/* Initialises the given font. When Drawer2D_BitmappedText is false, creates native font handle using Font_Make. */ /* Initialises the given font. When Drawer2D_BitmappedText is false, creates a system font using Font_MakeDefault. */
CC_API void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags); CC_API void Drawer2D_MakeFont(struct FontDesc* desc, int size, int flags);
/* Whether text should be drawn and measured using the currently set font bitmap. */ /* Whether text should be drawn and measured using the currently set font bitmap. */
@ -110,6 +110,9 @@ void Font_SetPadding(struct FontDesc* desc, int amount);
cc_string Font_Lookup(const cc_string* fontName, int flags); cc_string Font_Lookup(const cc_string* fontName, int flags);
/* Allocates a new system font from the given arguments. */ /* Allocates a new system font from the given arguments. */
cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size, int flags); cc_result Font_Make(struct FontDesc* desc, const cc_string* fontName, int size, int flags);
/* Allocates a new system font from the given arguments using default system font. */
/* NOTE: Unlike Font_Make, this may fallback onto other system fonts (e.g. Arial, Roboto, etc) */
void Font_MakeDefault(struct FontDesc* desc, int size, int flags);
/* Frees an allocated font. */ /* Frees an allocated font. */
CC_API void Font_Free(struct FontDesc* desc); CC_API void Font_Free(struct FontDesc* desc);
/* Attempts to decode one or fonts from the given file. */ /* Attempts to decode one or fonts from the given file. */

View File

@ -7,7 +7,8 @@
#include "Game.h" #include "Game.h"
#include "Utils.h" #include "Utils.h"
void HttpRequest_Free(struct HttpRequest* request) { /* Frees data from a HTTP request. */
static void HttpRequest_Free(struct HttpRequest* request) {
Mem_Free(request->data); Mem_Free(request->data);
request->data = NULL; request->data = NULL;
request->size = 0; request->size = 0;
@ -219,7 +220,7 @@ static void Http_CleanCacheTask(struct ScheduledTask* task) {
item = &processedReqs.entries[i]; item = &processedReqs.entries[i];
if (item->timeDownloaded + (10 * 1000) >= now) continue; if (item->timeDownloaded + (10 * 1000) >= now) continue;
HttpRequest_Free(item); Mem_Free(item->data);
RequestList_RemoveAt(&processedReqs, i); RequestList_RemoveAt(&processedReqs, i);
} }
} }
@ -586,11 +587,7 @@ static cc_result Http_BackendDo(struct HttpRequest* req, cc_string* url) {
char urlStr[NATIVE_STR_LEN]; char urlStr[NATIVE_STR_LEN];
void* post_data = req->data; void* post_data = req->data;
CURLcode res; CURLcode res;
if (!curlSupported) return ERR_NOT_SUPPORTED;
if (!curlSupported) {
HttpRequest_Free(req);
return ERR_NOT_SUPPORTED;
}
req->meta = NULL; req->meta = NULL;
Http_SetRequestHeaders(req); Http_SetRequestHeaders(req);
@ -665,6 +662,25 @@ struct HttpCacheEntry {
#define HTTP_CACHE_ENTRIES 10 #define HTTP_CACHE_ENTRIES 10
static struct HttpCacheEntry http_cache[HTTP_CACHE_ENTRIES]; static struct HttpCacheEntry http_cache[HTTP_CACHE_ENTRIES];
/* Converts characters to UTF8, then calls Http_URlEncode on them. */
static void HttpCache_UrlEncodeUrl(cc_string* dst, const cc_string* src) {
cc_uint8 data[4];
int i, len;
char c;
for (i = 0; i < src->length; i++) {
c = src->buffer[i];
len = Convert_CP437ToUtf8(c, data);
/* URL path/query must not be URL encoded (it normally would be) */
if (c == '/' || c == '?' || c == '=') {
String_Append(dst, c);
} else {
Http_UrlEncode(dst, data, len);
}
}
}
/* Splits up the components of a URL */ /* Splits up the components of a URL */
static void HttpCache_MakeEntry(const cc_string* url, struct HttpCacheEntry* entry, cc_string* resource) { static void HttpCache_MakeEntry(const cc_string* url, struct HttpCacheEntry* entry, cc_string* resource) {
cc_string scheme, path, addr, name, port, _resource; cc_string scheme, path, addr, name, port, _resource;
@ -678,7 +694,7 @@ static void HttpCache_MakeEntry(const cc_string* url, struct HttpCacheEntry* ent
String_UNSAFE_Separate(&path, '/', &addr, &_resource); String_UNSAFE_Separate(&path, '/', &addr, &_resource);
String_UNSAFE_Separate(&addr, ':', &name, &port); String_UNSAFE_Separate(&addr, ':', &name, &port);
/* Address may have unicode characters - need to percent encode them */ /* Address may have unicode characters - need to percent encode them */
Http_UrlEncodeUrl(resource, &_resource); HttpCache_UrlEncodeUrl(resource, &_resource);
String_InitArray_NT(entry->Address, entry->_addressBuffer); String_InitArray_NT(entry->Address, entry->_addressBuffer);
String_Copy(&entry->Address, &name); String_Copy(&entry->Address, &name);
@ -1135,24 +1151,6 @@ void Http_UrlEncodeUtf8(cc_string* dst, const cc_string* src) {
} }
} }
void Http_UrlEncodeUrl(cc_string* dst, const cc_string* src) {
cc_uint8 data[4];
int i, len;
char c;
for (i = 0; i < src->length; i++) {
c = src->buffer[i];
len = Convert_CP437ToUtf8(c, data);
/* URL path/query must not be URL encoded (it normally would be) */
if (c == '/' || c == '?' || c == '=') {
String_Append(dst, c);
} else {
Http_UrlEncode(dst, data, len);
}
}
}
/*########################################################################################################################* /*########################################################################################################################*
*-----------------------------------------------------Http component------------------------------------------------------* *-----------------------------------------------------Http component------------------------------------------------------*

View File

@ -40,9 +40,6 @@ struct HttpRequest {
struct StringsBuffer* cookies; /* Cookie list sent in requests. May be modified by the response. */ struct StringsBuffer* cookies; /* Cookie list sent in requests. May be modified by the response. */
}; };
/* Frees data from a HTTP request. */
void HttpRequest_Free(struct HttpRequest* request);
/* Aschronously performs a http GET request to download a skin. */ /* Aschronously performs a http GET request to download a skin. */
/* If url is a skin, downloads from there. (if not, downloads from SKIN_SERVER/[skinName].png) */ /* If url is a skin, downloads from there. (if not, downloads from SKIN_SERVER/[skinName].png) */
int Http_AsyncGetSkin(const cc_string* skinName); int Http_AsyncGetSkin(const cc_string* skinName);
@ -62,11 +59,8 @@ void Http_TryCancel(int reqID);
/* Encodes data using % or URL encoding. */ /* Encodes data using % or URL encoding. */
void Http_UrlEncode(cc_string* dst, const cc_uint8* data, int len); void Http_UrlEncode(cc_string* dst, const cc_uint8* data, int len);
/* Converts characters to UTF8, then calls Http_URlEncode on them. */ /* Converts characters to UTF8, then calls Http_UrlEncode on them. */
void Http_UrlEncodeUtf8(cc_string* dst, const cc_string* src); void Http_UrlEncodeUtf8(cc_string* dst, const cc_string* src);
/* Converts characters to UTF8, then calls Http_URlEncode on them. */
/* NOTE: '/' is NOT url encoded, whereas Http_UrlEncodeUtf8 url encodes them */
void Http_UrlEncodeUrl(cc_string* dst, const cc_string* src);
/* Outputs more detailed information about errors with http requests. */ /* Outputs more detailed information about errors with http requests. */
cc_bool Http_DescribeError(cc_result res, cc_string* dst); cc_bool Http_DescribeError(cc_result res, cc_string* dst);