minor code cleanup

This commit is contained in:
UnknownShadow200 2019-05-30 23:17:05 +10:00
parent e647c72811
commit bd314098de
10 changed files with 52 additions and 65 deletions

View File

@ -55,27 +55,27 @@ void Chat_SetLogName(const String* name) { }
static void Chat_OpenLog(struct DateTime* now) { }
static void Chat_AppendLog(const String* text) { }
#else
static char Chat_LogNameBuffer[STRING_SIZE];
static String Chat_LogName = String_FromArray(Chat_LogNameBuffer);
static char Chat_LogPathBuffer[FILENAME_SIZE];
static String Chat_LogPath = String_FromArray(Chat_LogPathBuffer);
static char logNameBuffer[STRING_SIZE];
static String logName = String_FromArray(logNameBuffer);
static char logPathBuffer[FILENAME_SIZE];
static String logPath = String_FromArray(logPathBuffer);
static struct Stream Chat_LogStream;
static struct DateTime ChatLog_LastLogDate;
static struct Stream logStream;
static struct DateTime lastLogDate;
static void Chat_ResetLog(void) {
Chat_LogName.length = 0;
ChatLog_LastLogDate.Day = 0;
ChatLog_LastLogDate.Month = 0;
ChatLog_LastLogDate.Year = 0;
logName.length = 0;
lastLogDate.Day = 0;
lastLogDate.Month = 0;
lastLogDate.Year = 0;
}
static void Chat_CloseLog(void) {
ReturnCode res;
if (!Chat_LogStream.Meta.File) return;
if (!logStream.Meta.File) return;
res = Chat_LogStream.Close(&Chat_LogStream);
if (res) { Logger_Warn2(res, "closing", &Chat_LogPath); }
res = logStream.Close(&logStream);
if (res) { Logger_Warn2(res, "closing", &logPath); }
}
static bool Chat_AllowedLogChar(char c) {
@ -87,13 +87,13 @@ static bool Chat_AllowedLogChar(char c) {
void Chat_SetLogName(const String* name) {
char c;
int i;
if (Chat_LogName.length) return;
if (logName.length) return;
for (i = 0; i < name->length; i++) {
c = name->buffer[i];
if (Chat_AllowedLogChar(c)) {
String_Append(&Chat_LogName, c);
String_Append(&logName, c);
} else if (c == '&') {
i++; /* skip over following colour code */
}
@ -109,34 +109,32 @@ static void Chat_OpenLog(struct DateTime* now) {
FileHandle file;
int i;
ReturnCode res;
String* path = &Chat_LogPath;
if (!Utils_EnsureDirectory("logs")) { Chat_DisableLogging(); return; }
/* Ensure multiple instances do not end up overwriting each other's log entries. */
for (i = 0; i < 20; i++) {
path->length = 0;
String_Format3(path, "logs/%p4-%p2-%p2 ", &now->Year, &now->Month, &now->Day);
logPath.length = 0;
String_Format3(&logPath, "logs/%p4-%p2-%p2 ", &now->Year, &now->Month, &now->Day);
if (i > 0) {
String_Format2(path, "%s _%i.log", &Chat_LogName, &i);
String_Format2(&logPath, "%s _%i.log", &logName, &i);
} 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) {
Chat_DisableLogging();
Logger_Warn2(res, "appending to", path);
Logger_Warn2(res, "appending to", &logPath);
return;
}
if (res == ReturnCode_FileShareViolation) continue;
Stream_FromFile(&Chat_LogStream, file);
Stream_FromFile(&logStream, file);
return;
}
Chat_LogStream.Meta.File = NULL;
logStream.Meta.File = NULL;
Chat_DisableLogging();
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;
ReturnCode res;
if (!Chat_LogName.length || !Chat_Logging) return;
if (!logName.length || !Chat_Logging) return;
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_OpenLog(&now);
}
ChatLog_LastLogDate = now;
if (!Chat_LogStream.Meta.File) return;
lastLogDate = now;
if (!logStream.Meta.File) return;
/* [HH:mm:ss] text */
String_InitArray(str, strBuffer);
String_Format3(&str, "[%p2:%p2:%p2] ", &now.Hour, &now.Minute, &now.Second);
String_AppendColorless(&str, text);
res = Stream_WriteLine(&Chat_LogStream, &str);
res = Stream_WriteLine(&logStream, &str);
if (!res) return;
Chat_DisableLogging();
Logger_Warn2(res, "writing to", &Chat_LogPath);
Logger_Warn2(res, "writing to", &logPath);
}
#endif

View File

@ -359,6 +359,7 @@ static void ServerInfo_Parse(struct JsonContext* ctx, const String* val) {
} else if (String_CaselessEqualsConst(&ctx->CurKey, "featured")) {
Convert_ParseBool(val, &info->Featured);
} else if (String_CaselessEqualsConst(&ctx->CurKey, "country_abbr")) {
/* Two letter country codes, see ISO 3166-1 alpha-2 */
String_Copy(&info->Country, val);
}
}
@ -566,7 +567,7 @@ static int flagsCount, flagsCapacity;
struct Flag {
Bitmap Bmp;
String Name;
char _nameBuffer[8];
char _nameBuffer[2];
};
static struct Flag* flags;

View File

@ -37,7 +37,7 @@ struct ServerInfo {
int _order; /* (internal) order in servers table after filtering */
char _hashBuffer[32], _nameBuffer[STRING_SIZE];
char _ipBuffer[16], _mppassBuffer[STRING_SIZE];
char _countryBuffer[8], _softBuffer[STRING_SIZE];
char _countryBuffer[2], _softBuffer[STRING_SIZE];
};
struct LWebTask;

View File

@ -138,9 +138,9 @@ void Options_Load(void) {
String entry, key, value;
int i;
if (!Options.Filename) {
EntryList_Init(&Options, NULL, "options-default.txt", '=');
EntryList_Init(&Options, NULL, "options.txt", '=');
if (!Options.Path) {
EntryList_Init(&Options, "options-default.txt", '=');
EntryList_Init(&Options, "options.txt", '=');
} else {
/* Reset all the unchanged options */
for (i = Options.Entries.Count - 1; i >= 0; i--) {

View File

@ -1296,7 +1296,7 @@ static void Font_Init(void) {
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++) {
Directory_Enum(&dirs[i], NULL, Font_DirCallback);
}

View File

@ -518,10 +518,10 @@ void Atlas_Free(void) {
static struct EntryList acceptedList, deniedList, etagCache, lastModifiedCache;
void TextureCache_Init(void) {
EntryList_Init(&acceptedList, "texturecache", "acceptedurls.txt", ' ');
EntryList_Init(&deniedList, "texturecache", "deniedurls.txt", ' ');
EntryList_Init(&etagCache, "texturecache", "etags.txt", ' ');
EntryList_Init(&lastModifiedCache, "texturecache", "lastmodified.txt", ' ');
EntryList_Init(&acceptedList, "texturecache/acceptedurls.txt", ' ');
EntryList_Init(&deniedList, "texturecache/deniedurls.txt", ' ');
EntryList_Init(&etagCache, "texturecache/etags.txt", ' ');
EntryList_Init(&lastModifiedCache, "texturecache/lastmodified.txt", ' ');
}
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);
TextureCache_MakePath(&path, url);
if (!Utils_EnsureDirectory("texturecache")) return;
res = Stream_WriteAllTo(&path, data, length);
if (res) { Logger_Warn2(res, "caching", url); }

View File

@ -302,11 +302,7 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
ReturnCode res;
String_InitArray(path, pathBuffer);
if (list->Folder) {
String_Format2(&path, "%c/%c", list->Folder, list->Filename);
} else {
String_AppendConst(&path, list->Filename);
}
String_AppendConst(&path, list->Path);
res = Stream_OpenFile(&stream, &path);
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 (entry.length > STRINGSBUFFER_LEN_MASK) {
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;
}
@ -350,12 +346,7 @@ void EntryList_Save(struct EntryList* list) {
ReturnCode res;
String_InitArray(path, pathBuffer);
if (list->Folder) {
String_Format2(&path, "%c/%c", list->Folder, list->Filename);
if (!Utils_EnsureDirectory(list->Folder)) return;
} else {
String_AppendConst(&path, list->Filename);
}
String_AppendConst(&path, list->Path);
res = Stream_CreateFile(&stream, &path);
if (res) { Logger_Warn2(res, "creating", &path); return; }
@ -416,9 +407,8 @@ int EntryList_Find(struct EntryList* list, const String* key) {
return -1;
}
void EntryList_Init(struct EntryList* list, const char* folder, const char* file, char separator) {
list->Folder = folder;
list->Filename = file;
void EntryList_Init(struct EntryList* list, const char* path, char separator) {
list->Path = path;
list->Separator = separator;
EntryList_Load(list, NULL);
}

View File

@ -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);
struct EntryList {
const char* Folder;
const char* Filename;
const char* Path;
char Separator;
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. */
CC_NOINLINE int EntryList_Find(struct EntryList* list, const String* key);
/* 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

View File

@ -2687,7 +2687,7 @@ static void SpecialInputWidget_IntersectsBody(struct SpecialInputWidget* w, int
/* TODO: Not be so hacky */
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) {
@ -2882,10 +2882,10 @@ static struct WidgetVTABLE SpecialInputWidget_VTABLE = {
SpecialInputWidget_MouseDown, Widget_Mouse, Widget_MouseMove, Widget_MouseScroll,
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);
w->VTABLE = &SpecialInputWidget_VTABLE;
w->VerAnchor = ANCHOR_MAX;
w->Font = *font;
w->AppendObj = appendObj;
w->Target = target;
}

View File

@ -205,7 +205,7 @@ struct SpecialInputWidget {
Size2D ElementSize;
int SelectedIndex;
bool PendingRedraw;
struct InputWidget* AppendObj;
struct InputWidget* Target;
struct Texture Tex;
FontDesc Font;
struct SpecialInputTab Tabs[5];
@ -213,7 +213,7 @@ struct SpecialInputWidget {
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_SetActive(struct SpecialInputWidget* w, bool active);
#endif