bool JSON_ParseFile(char *filename, const unordered_map<const char*, jsonParseFunc>& parsers, void* output) { if(!filename || !filename[0]) { R_Message(PRIORITY_WARNING, "JSON_ParseFile: bad filename sent\n"); return false; } File* file = trap->OpenFile(filename, "rb"); if(!file) { R_Message(PRIORITY_WARNING, "JSON_ParseFile: could not open file %s\n", filename); return false; } size_t numChars = trap->GetFileSize(file); char* s = (char*)trap->Zone_Alloc(numChars * sizeof(char), "files"); trap->ReadPlaintext(file, numChars, s); trap->CloseFile(file); char error[1024] = {0}; cJSON* root = cJSON_ParsePooled(s, error, sizeof(error)); if(error[0] || !root) { R_Message(PRIORITY_ERROR, "ERROR: %s: %s\n", filename, error); trap->Zone_FastFree(s, "files"); return false; } for(auto it = cJSON_GetFirstItem(root); it; it = cJSON_GetNextItem(it)) { JSON_ParseFieldSet(root, parsers, output); } trap->Zone_FastFree(s, "files"); return true; }
void MemoryManager::PrintMemUsage() { R_Message(PRIORITY_MESSAGE, "\n%-10s %20s %20s %20s %20s %20s %20s\n", "Tag", "Cur Usage (b)", "Cur Usage (KB)", "Cur Usage (MB)", "Peak Usage (b)", "Peak Usage (KB)", "Peak Usage (MB)"); R_Message(PRIORITY_MESSAGE, "%-10s %20s %20s %20s %20s %20s %20s\n", "-----", "-------------", "--------------", "--------------", "--------------", "---------------", "---------------"); for(auto it = zone.begin(); it != zone.end(); ++it) { R_Message(PRIORITY_MESSAGE, "%-10s %20i %20.2f %20.2f %20i %20.2f %20.2f\n", it->first.c_str(), it->second.zoneInUse, (float)((double)it->second.zoneInUse/1024.0f), (float)((double)it->second.zoneInUse/1048576.0f), it->second.peakUsage, (float)((double)it->second.peakUsage/1024.0f), (float)((double)it->second.peakUsage/1048576.0f)); } }
void Sys_Error(const char* error, ...) { va_list argptr; char text[4096]; va_start (argptr, error); vsnprintf(text, sizeof(text), error, argptr); va_end (argptr); R_Message(PRIORITY_ERRFATAL, text); R_Message(PRIORITY_MESSAGE, "\n"); RenderCode::Exit(true); setGameQuitting(false); throw false; }
void Sys_FreeLibrary(ptModule module) { if(!module) { R_Message(PRIORITY_ERROR, "attempted to free library which doesn't exist!\n"); return; } FreeLibrary((HMODULE)module); }
void NPC::interact(Entity* interacter) { if(ptInteractingWith != nullptr) { // Don't interact with us if we're busy.. R_Message(PRIORITY_MESSAGE, "%s is busy\n", getname().c_str()); return; } ptInteractingWith = (Actor*)interacter; ptServer->GetClient()->NPCStartInteraction(this, *getcurrentoptions()); }
ptModule Sys_LoadLibrary(string name) { name.append(".dll"); string filepath = File::GetFileSearchPath(name); ptModule hLib = (ptModule)LoadLibrary(filepath.c_str()); if(!hLib) { R_Message(PRIORITY_ERROR, "module load failure: %i\n", GetLastError()); } return hLib; }
void Sys_SendToClipboard(string text) { if(text.length() <= 0) { OpenClipboard(0); EmptyClipboard(); CloseClipboard(); return; } HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, text.length()); if(hMem == 0) { R_Message(PRIORITY_ERROR, "Sys_SendToClipboard: couldn't GlobalAlloc (out of memory?)\n"); return; } memcpy(GlobalLock(hMem), text.c_str(), text.length()); GlobalUnlock(hMem); OpenClipboard(0); EmptyClipboard(); SetClipboardData(CF_TEXT, hMem); CloseClipboard(); }
// free some zone memory (quicker but still SLOW and not recommended) void MemoryManager::FastFree(void* memory, const string& tag) { try { auto memblock = zone[tag].zone.find(memory); if(memblock == zone[tag].zone.end()) { R_Message(PRIORITY_WARNING, "WARNING: could not dealloc memory block at 0x%X, memory not allocated!\n", (unsigned int)memory); return; } auto mpair = memblock->second; zone[tag].zoneInUse -= mpair.memInUse; zone[tag].zone.erase(memory); if(!mpair.isClassObject) free(memory); else delete memory; } catch( out_of_range e ) { Sys_Error("Zone::FastFree(): corrupt zone memory!"); return; } }
GameModule::GameModule(string module) { ptModuleHandle = Sys_LoadLibrary(module); if(ptModuleHandle == nullptr) { R_Message(PRIORITY_ERRFATAL, "FATAL: GameModule failed to load\n"); } }
void Shutdown() { R_Message(PRIORITY_MESSAGE, "Shutting down zone memory..."); delete mem; // yea f**k up dem peasant's RAM }
MemoryManager::MemoryManager() { R_Message(PRIORITY_NOTE, "Initializing zone memory\n"); }
void A1Q1_Test::ChangeToState(const int toState) { if(toState == A1Q1_STARTED) { ptServer->GetClient()->NewQuest("Test Quest", "a1q1_test", A1Q1_Test::DescriptionPointer); R_Message(PRIORITY_MESSAGE, "a1q1 state changed to A1Q1_STARTED\n"); } }