mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-17 19:45:23 -04:00
minor code cleanup
This commit is contained in:
parent
e647c72811
commit
bd314098de
60
src/Chat.c
60
src/Chat.c
@ -55,27 +55,27 @@ void Chat_SetLogName(const String* name) { }
|
|||||||
static void Chat_OpenLog(struct DateTime* now) { }
|
static void Chat_OpenLog(struct DateTime* now) { }
|
||||||
static void Chat_AppendLog(const String* text) { }
|
static void Chat_AppendLog(const String* text) { }
|
||||||
#else
|
#else
|
||||||
static char Chat_LogNameBuffer[STRING_SIZE];
|
static char logNameBuffer[STRING_SIZE];
|
||||||
static String Chat_LogName = String_FromArray(Chat_LogNameBuffer);
|
static String logName = String_FromArray(logNameBuffer);
|
||||||
static char Chat_LogPathBuffer[FILENAME_SIZE];
|
static char logPathBuffer[FILENAME_SIZE];
|
||||||
static String Chat_LogPath = String_FromArray(Chat_LogPathBuffer);
|
static String logPath = String_FromArray(logPathBuffer);
|
||||||
|
|
||||||
static struct Stream Chat_LogStream;
|
static struct Stream logStream;
|
||||||
static struct DateTime ChatLog_LastLogDate;
|
static struct DateTime lastLogDate;
|
||||||
|
|
||||||
static void Chat_ResetLog(void) {
|
static void Chat_ResetLog(void) {
|
||||||
Chat_LogName.length = 0;
|
logName.length = 0;
|
||||||
ChatLog_LastLogDate.Day = 0;
|
lastLogDate.Day = 0;
|
||||||
ChatLog_LastLogDate.Month = 0;
|
lastLogDate.Month = 0;
|
||||||
ChatLog_LastLogDate.Year = 0;
|
lastLogDate.Year = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Chat_CloseLog(void) {
|
static void Chat_CloseLog(void) {
|
||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
if (!Chat_LogStream.Meta.File) return;
|
if (!logStream.Meta.File) return;
|
||||||
|
|
||||||
res = Chat_LogStream.Close(&Chat_LogStream);
|
res = logStream.Close(&logStream);
|
||||||
if (res) { Logger_Warn2(res, "closing", &Chat_LogPath); }
|
if (res) { Logger_Warn2(res, "closing", &logPath); }
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool Chat_AllowedLogChar(char c) {
|
static bool Chat_AllowedLogChar(char c) {
|
||||||
@ -87,13 +87,13 @@ static bool Chat_AllowedLogChar(char c) {
|
|||||||
void Chat_SetLogName(const String* name) {
|
void Chat_SetLogName(const String* name) {
|
||||||
char c;
|
char c;
|
||||||
int i;
|
int i;
|
||||||
if (Chat_LogName.length) return;
|
if (logName.length) return;
|
||||||
|
|
||||||
for (i = 0; i < name->length; i++) {
|
for (i = 0; i < name->length; i++) {
|
||||||
c = name->buffer[i];
|
c = name->buffer[i];
|
||||||
|
|
||||||
if (Chat_AllowedLogChar(c)) {
|
if (Chat_AllowedLogChar(c)) {
|
||||||
String_Append(&Chat_LogName, c);
|
String_Append(&logName, c);
|
||||||
} else if (c == '&') {
|
} else if (c == '&') {
|
||||||
i++; /* skip over following colour code */
|
i++; /* skip over following colour code */
|
||||||
}
|
}
|
||||||
@ -109,34 +109,32 @@ static void Chat_OpenLog(struct DateTime* now) {
|
|||||||
FileHandle file;
|
FileHandle file;
|
||||||
int i;
|
int i;
|
||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
|
|
||||||
String* path = &Chat_LogPath;
|
|
||||||
if (!Utils_EnsureDirectory("logs")) { Chat_DisableLogging(); return; }
|
if (!Utils_EnsureDirectory("logs")) { Chat_DisableLogging(); return; }
|
||||||
|
|
||||||
/* Ensure multiple instances do not end up overwriting each other's log entries. */
|
/* Ensure multiple instances do not end up overwriting each other's log entries. */
|
||||||
for (i = 0; i < 20; i++) {
|
for (i = 0; i < 20; i++) {
|
||||||
path->length = 0;
|
logPath.length = 0;
|
||||||
String_Format3(path, "logs/%p4-%p2-%p2 ", &now->Year, &now->Month, &now->Day);
|
String_Format3(&logPath, "logs/%p4-%p2-%p2 ", &now->Year, &now->Month, &now->Day);
|
||||||
|
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
String_Format2(path, "%s _%i.log", &Chat_LogName, &i);
|
String_Format2(&logPath, "%s _%i.log", &logName, &i);
|
||||||
} else {
|
} else {
|
||||||
String_Format1(path, "%s.log", &Chat_LogName);
|
String_Format1(&logPath, "%s.log", &logName);
|
||||||
}
|
}
|
||||||
|
|
||||||
res = File_Append(&file, path);
|
res = File_Append(&file, &logPath);
|
||||||
if (res && res != ReturnCode_FileShareViolation) {
|
if (res && res != ReturnCode_FileShareViolation) {
|
||||||
Chat_DisableLogging();
|
Chat_DisableLogging();
|
||||||
Logger_Warn2(res, "appending to", path);
|
Logger_Warn2(res, "appending to", &logPath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res == ReturnCode_FileShareViolation) continue;
|
if (res == ReturnCode_FileShareViolation) continue;
|
||||||
Stream_FromFile(&Chat_LogStream, file);
|
Stream_FromFile(&logStream, file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chat_LogStream.Meta.File = NULL;
|
logStream.Meta.File = NULL;
|
||||||
Chat_DisableLogging();
|
Chat_DisableLogging();
|
||||||
Chat_Add1("&cFailed to open a chat log file after %i tries, giving up", &i);
|
Chat_Add1("&cFailed to open a chat log file after %i tries, giving up", &i);
|
||||||
}
|
}
|
||||||
@ -146,26 +144,26 @@ static void Chat_AppendLog(const String* text) {
|
|||||||
struct DateTime now;
|
struct DateTime now;
|
||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
|
|
||||||
if (!Chat_LogName.length || !Chat_Logging) return;
|
if (!logName.length || !Chat_Logging) return;
|
||||||
DateTime_CurrentLocal(&now);
|
DateTime_CurrentLocal(&now);
|
||||||
|
|
||||||
if (now.Day != ChatLog_LastLogDate.Day || now.Month != ChatLog_LastLogDate.Month || now.Year != ChatLog_LastLogDate.Year) {
|
if (now.Day != lastLogDate.Day || now.Month != lastLogDate.Month || now.Year != lastLogDate.Year) {
|
||||||
Chat_CloseLog();
|
Chat_CloseLog();
|
||||||
Chat_OpenLog(&now);
|
Chat_OpenLog(&now);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatLog_LastLogDate = now;
|
lastLogDate = now;
|
||||||
if (!Chat_LogStream.Meta.File) return;
|
if (!logStream.Meta.File) return;
|
||||||
|
|
||||||
/* [HH:mm:ss] text */
|
/* [HH:mm:ss] text */
|
||||||
String_InitArray(str, strBuffer);
|
String_InitArray(str, strBuffer);
|
||||||
String_Format3(&str, "[%p2:%p2:%p2] ", &now.Hour, &now.Minute, &now.Second);
|
String_Format3(&str, "[%p2:%p2:%p2] ", &now.Hour, &now.Minute, &now.Second);
|
||||||
String_AppendColorless(&str, text);
|
String_AppendColorless(&str, text);
|
||||||
|
|
||||||
res = Stream_WriteLine(&Chat_LogStream, &str);
|
res = Stream_WriteLine(&logStream, &str);
|
||||||
if (!res) return;
|
if (!res) return;
|
||||||
Chat_DisableLogging();
|
Chat_DisableLogging();
|
||||||
Logger_Warn2(res, "writing to", &Chat_LogPath);
|
Logger_Warn2(res, "writing to", &logPath);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -359,6 +359,7 @@ static void ServerInfo_Parse(struct JsonContext* ctx, const String* val) {
|
|||||||
} else if (String_CaselessEqualsConst(&ctx->CurKey, "featured")) {
|
} else if (String_CaselessEqualsConst(&ctx->CurKey, "featured")) {
|
||||||
Convert_ParseBool(val, &info->Featured);
|
Convert_ParseBool(val, &info->Featured);
|
||||||
} else if (String_CaselessEqualsConst(&ctx->CurKey, "country_abbr")) {
|
} else if (String_CaselessEqualsConst(&ctx->CurKey, "country_abbr")) {
|
||||||
|
/* Two letter country codes, see ISO 3166-1 alpha-2 */
|
||||||
String_Copy(&info->Country, val);
|
String_Copy(&info->Country, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -566,7 +567,7 @@ static int flagsCount, flagsCapacity;
|
|||||||
struct Flag {
|
struct Flag {
|
||||||
Bitmap Bmp;
|
Bitmap Bmp;
|
||||||
String Name;
|
String Name;
|
||||||
char _nameBuffer[8];
|
char _nameBuffer[2];
|
||||||
};
|
};
|
||||||
static struct Flag* flags;
|
static struct Flag* flags;
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ struct ServerInfo {
|
|||||||
int _order; /* (internal) order in servers table after filtering */
|
int _order; /* (internal) order in servers table after filtering */
|
||||||
char _hashBuffer[32], _nameBuffer[STRING_SIZE];
|
char _hashBuffer[32], _nameBuffer[STRING_SIZE];
|
||||||
char _ipBuffer[16], _mppassBuffer[STRING_SIZE];
|
char _ipBuffer[16], _mppassBuffer[STRING_SIZE];
|
||||||
char _countryBuffer[8], _softBuffer[STRING_SIZE];
|
char _countryBuffer[2], _softBuffer[STRING_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LWebTask;
|
struct LWebTask;
|
||||||
|
@ -138,9 +138,9 @@ void Options_Load(void) {
|
|||||||
String entry, key, value;
|
String entry, key, value;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!Options.Filename) {
|
if (!Options.Path) {
|
||||||
EntryList_Init(&Options, NULL, "options-default.txt", '=');
|
EntryList_Init(&Options, "options-default.txt", '=');
|
||||||
EntryList_Init(&Options, NULL, "options.txt", '=');
|
EntryList_Init(&Options, "options.txt", '=');
|
||||||
} else {
|
} else {
|
||||||
/* Reset all the unchanged options */
|
/* Reset all the unchanged options */
|
||||||
for (i = Options.Entries.Count - 1; i >= 0; i--) {
|
for (i = Options.Entries.Count - 1; i >= 0; i--) {
|
||||||
|
@ -1296,7 +1296,7 @@ static void Font_Init(void) {
|
|||||||
Window_ShowDialog("One time load", "Initialising font cache, this can take several seconds.");
|
Window_ShowDialog("One time load", "Initialising font cache, this can take several seconds.");
|
||||||
}
|
}
|
||||||
|
|
||||||
EntryList_Init(&font_list, NULL, FONT_CACHE_FILE, '=');
|
EntryList_Init(&font_list, FONT_CACHE_FILE, '=');
|
||||||
for (i = 0; i < Array_Elems(dirs); i++) {
|
for (i = 0; i < Array_Elems(dirs); i++) {
|
||||||
Directory_Enum(&dirs[i], NULL, Font_DirCallback);
|
Directory_Enum(&dirs[i], NULL, Font_DirCallback);
|
||||||
}
|
}
|
||||||
|
@ -518,10 +518,10 @@ void Atlas_Free(void) {
|
|||||||
static struct EntryList acceptedList, deniedList, etagCache, lastModifiedCache;
|
static struct EntryList acceptedList, deniedList, etagCache, lastModifiedCache;
|
||||||
|
|
||||||
void TextureCache_Init(void) {
|
void TextureCache_Init(void) {
|
||||||
EntryList_Init(&acceptedList, "texturecache", "acceptedurls.txt", ' ');
|
EntryList_Init(&acceptedList, "texturecache/acceptedurls.txt", ' ');
|
||||||
EntryList_Init(&deniedList, "texturecache", "deniedurls.txt", ' ');
|
EntryList_Init(&deniedList, "texturecache/deniedurls.txt", ' ');
|
||||||
EntryList_Init(&etagCache, "texturecache", "etags.txt", ' ');
|
EntryList_Init(&etagCache, "texturecache/etags.txt", ' ');
|
||||||
EntryList_Init(&lastModifiedCache, "texturecache", "lastmodified.txt", ' ');
|
EntryList_Init(&lastModifiedCache, "texturecache/lastmodified.txt", ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TextureCache_HasAccepted(const String* url) { return EntryList_Find(&acceptedList, url) >= 0; }
|
bool TextureCache_HasAccepted(const String* url) { return EntryList_Find(&acceptedList, url) >= 0; }
|
||||||
@ -607,7 +607,6 @@ void TextureCache_Set(const String* url, const uint8_t* data, uint32_t length) {
|
|||||||
|
|
||||||
String_InitArray(path, pathBuffer);
|
String_InitArray(path, pathBuffer);
|
||||||
TextureCache_MakePath(&path, url);
|
TextureCache_MakePath(&path, url);
|
||||||
if (!Utils_EnsureDirectory("texturecache")) return;
|
|
||||||
|
|
||||||
res = Stream_WriteAllTo(&path, data, length);
|
res = Stream_WriteAllTo(&path, data, length);
|
||||||
if (res) { Logger_Warn2(res, "caching", url); }
|
if (res) { Logger_Warn2(res, "caching", url); }
|
||||||
|
20
src/Utils.c
20
src/Utils.c
@ -302,11 +302,7 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
|
|||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
|
|
||||||
String_InitArray(path, pathBuffer);
|
String_InitArray(path, pathBuffer);
|
||||||
if (list->Folder) {
|
String_AppendConst(&path, list->Path);
|
||||||
String_Format2(&path, "%c/%c", list->Folder, list->Filename);
|
|
||||||
} else {
|
|
||||||
String_AppendConst(&path, list->Filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
res = Stream_OpenFile(&stream, &path);
|
res = Stream_OpenFile(&stream, &path);
|
||||||
if (res == ReturnCode_FileNotFound) return;
|
if (res == ReturnCode_FileNotFound) return;
|
||||||
@ -331,7 +327,7 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
|
|||||||
/* If don't prevent this here, client aborts in StringsBuffer_Add */
|
/* If don't prevent this here, client aborts in StringsBuffer_Add */
|
||||||
if (entry.length > STRINGSBUFFER_LEN_MASK) {
|
if (entry.length > STRINGSBUFFER_LEN_MASK) {
|
||||||
entry.length = 0;
|
entry.length = 0;
|
||||||
String_Format1(&entry, "Skipping extremely long line in %c, file may have been corrupted", list->Filename);
|
String_Format1(&entry, "Skipping extremely long line in %c, file may have been corrupted", list->Path);
|
||||||
Logger_WarnFunc(&entry); continue;
|
Logger_WarnFunc(&entry); continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,12 +346,7 @@ void EntryList_Save(struct EntryList* list) {
|
|||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
|
|
||||||
String_InitArray(path, pathBuffer);
|
String_InitArray(path, pathBuffer);
|
||||||
if (list->Folder) {
|
String_AppendConst(&path, list->Path);
|
||||||
String_Format2(&path, "%c/%c", list->Folder, list->Filename);
|
|
||||||
if (!Utils_EnsureDirectory(list->Folder)) return;
|
|
||||||
} else {
|
|
||||||
String_AppendConst(&path, list->Filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
res = Stream_CreateFile(&stream, &path);
|
res = Stream_CreateFile(&stream, &path);
|
||||||
if (res) { Logger_Warn2(res, "creating", &path); return; }
|
if (res) { Logger_Warn2(res, "creating", &path); return; }
|
||||||
@ -416,9 +407,8 @@ int EntryList_Find(struct EntryList* list, const String* key) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntryList_Init(struct EntryList* list, const char* folder, const char* file, char separator) {
|
void EntryList_Init(struct EntryList* list, const char* path, char separator) {
|
||||||
list->Folder = folder;
|
list->Path = path;
|
||||||
list->Filename = file;
|
|
||||||
list->Separator = separator;
|
list->Separator = separator;
|
||||||
EntryList_Load(list, NULL);
|
EntryList_Load(list, NULL);
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,7 @@ int Convert_ToBase64(const uint8_t* src, int len, char* dst);
|
|||||||
int Convert_FromBase64(const char* src, int len, uint8_t* dst);
|
int Convert_FromBase64(const char* src, int len, uint8_t* dst);
|
||||||
|
|
||||||
struct EntryList {
|
struct EntryList {
|
||||||
const char* Folder;
|
const char* Path;
|
||||||
const char* Filename;
|
|
||||||
char Separator;
|
char Separator;
|
||||||
StringsBuffer Entries;
|
StringsBuffer Entries;
|
||||||
};
|
};
|
||||||
@ -78,5 +77,5 @@ CC_NOINLINE STRING_REF String EntryList_UNSAFE_Get(struct EntryList* list, const
|
|||||||
/* Finds the index of the entry whose key caselessly equals the given key. */
|
/* Finds the index of the entry whose key caselessly equals the given key. */
|
||||||
CC_NOINLINE int EntryList_Find(struct EntryList* list, const String* key);
|
CC_NOINLINE int EntryList_Find(struct EntryList* list, const String* key);
|
||||||
/* Initialises the EntryList and loads the entries from disc. */
|
/* Initialises the EntryList and loads the entries from disc. */
|
||||||
void EntryList_Init(struct EntryList* list, const char* folder, const char* file, char separator);
|
void EntryList_Init(struct EntryList* list, const char* path, char separator);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2687,7 +2687,7 @@ static void SpecialInputWidget_IntersectsBody(struct SpecialInputWidget* w, int
|
|||||||
|
|
||||||
/* TODO: Not be so hacky */
|
/* TODO: Not be so hacky */
|
||||||
if (w->SelectedIndex == 0) str.length = 2;
|
if (w->SelectedIndex == 0) str.length = 2;
|
||||||
InputWidget_AppendString(w->AppendObj, &str);
|
InputWidget_AppendString(w->Target, &str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SpecialInputTab_Init(struct SpecialInputTab* tab, STRING_REF String* title, int itemsPerRow, int charsPerItem, STRING_REF String* contents) {
|
static void SpecialInputTab_Init(struct SpecialInputTab* tab, STRING_REF String* title, int itemsPerRow, int charsPerItem, STRING_REF String* contents) {
|
||||||
@ -2882,10 +2882,10 @@ static struct WidgetVTABLE SpecialInputWidget_VTABLE = {
|
|||||||
SpecialInputWidget_MouseDown, Widget_Mouse, Widget_MouseMove, Widget_MouseScroll,
|
SpecialInputWidget_MouseDown, Widget_Mouse, Widget_MouseMove, Widget_MouseScroll,
|
||||||
Widget_CalcPosition,
|
Widget_CalcPosition,
|
||||||
};
|
};
|
||||||
void SpecialInputWidget_Create(struct SpecialInputWidget* w, const FontDesc* font, struct InputWidget* appendObj) {
|
void SpecialInputWidget_Create(struct SpecialInputWidget* w, const FontDesc* font, struct InputWidget* target) {
|
||||||
Widget_Reset(w);
|
Widget_Reset(w);
|
||||||
w->VTABLE = &SpecialInputWidget_VTABLE;
|
w->VTABLE = &SpecialInputWidget_VTABLE;
|
||||||
w->VerAnchor = ANCHOR_MAX;
|
w->VerAnchor = ANCHOR_MAX;
|
||||||
w->Font = *font;
|
w->Font = *font;
|
||||||
w->AppendObj = appendObj;
|
w->Target = target;
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,7 @@ struct SpecialInputWidget {
|
|||||||
Size2D ElementSize;
|
Size2D ElementSize;
|
||||||
int SelectedIndex;
|
int SelectedIndex;
|
||||||
bool PendingRedraw;
|
bool PendingRedraw;
|
||||||
struct InputWidget* AppendObj;
|
struct InputWidget* Target;
|
||||||
struct Texture Tex;
|
struct Texture Tex;
|
||||||
FontDesc Font;
|
FontDesc Font;
|
||||||
struct SpecialInputTab Tabs[5];
|
struct SpecialInputTab Tabs[5];
|
||||||
@ -213,7 +213,7 @@ struct SpecialInputWidget {
|
|||||||
char __ColBuffer[DRAWER2D_MAX_COLS * 4];
|
char __ColBuffer[DRAWER2D_MAX_COLS * 4];
|
||||||
};
|
};
|
||||||
|
|
||||||
CC_NOINLINE void SpecialInputWidget_Create(struct SpecialInputWidget* w, const FontDesc* font, struct InputWidget* appendObj);
|
CC_NOINLINE void SpecialInputWidget_Create(struct SpecialInputWidget* w, const FontDesc* font, struct InputWidget* target);
|
||||||
CC_NOINLINE void SpecialInputWidget_UpdateCols(struct SpecialInputWidget* w);
|
CC_NOINLINE void SpecialInputWidget_UpdateCols(struct SpecialInputWidget* w);
|
||||||
CC_NOINLINE void SpecialInputWidget_SetActive(struct SpecialInputWidget* w, bool active);
|
CC_NOINLINE void SpecialInputWidget_SetActive(struct SpecialInputWidget* w, bool active);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user