// Value returned is UTF-8 encoded std::string getenv(const std::string& name) { std::wstring nameWide(name.begin(), name.end()); // get the variable DWORD nSize = 256; std::vector<wchar_t> buffer(nSize); DWORD result = ::GetEnvironmentVariableW(nameWide.c_str(), &(buffer[0]), nSize); if (result == 0) // not found { return std::string(); } if (result > nSize) // not enough space in buffer { nSize = result; buffer.resize(nSize); result = ::GetEnvironmentVariableW(nameWide.c_str(), &(buffer[0]), nSize); if (result == 0 || result > nSize) return std::string(); // VERY unexpected failure case } // return it return string_utils::wideToUtf8(&(buffer[0])); }
void ComponentLoader::Initialize() { // run local initialization functions InitFunctionBase::RunAll(); // set up the root component m_rootComponent = FxGameComponent::Create(); AddComponent(m_rootComponent); // parse and load additional components FILE* componentCache = _pfopen(MakeRelativeCitPath(L"components.json").c_str(), _P("rb")); if (!componentCache) { FatalError("Could not find component cache storage file (components.json)."); } // read component cache file fseek(componentCache, 0, SEEK_END); int length = ftell(componentCache); fseek(componentCache, 0, SEEK_SET); char* cacheBuf = new char[length + 1]; fread(cacheBuf, 1, length, componentCache); cacheBuf[length] = '\0'; fclose(componentCache); // parse the list rapidjson::Document doc; doc.Parse(cacheBuf); delete[] cacheBuf; if (doc.HasParseError()) { FatalError("Error parsing components.json: %d", doc.GetParseError()); } // look through the list for components to load std::vector<std::string> components; for (auto it = doc.Begin(); it != doc.End(); it++) { const char* name = it->GetString(); components.push_back(name); // replace colons with dashes char* nameStr = strdup(name); char* p = nameStr; while (*p) { if (*p == ':') { *p = '-'; } p++; } fwPlatformString nameWide(nameStr); free(nameStr); AddComponent(new DllGameComponent(va(PLATFORM_LIBRARY_STRING, nameWide.c_str()))); } // load the components, but don't instance them std::vector<fwRefContainer<ComponentData>> componentDatas; for (auto& component : components) { auto comp = LoadComponent(component.c_str()); if (!comp.GetRef()) { FatalError("Could not load component %s.", component.c_str()); } componentDatas.push_back(comp); } // sort the list by dependency std::queue<fwRefContainer<ComponentData>> sortedList = SortDependencyList(componentDatas); // clear the loaded list (it'll be added afterwards in order) m_loadedComponents.clear(); while (!sortedList.empty()) { auto comp = sortedList.front(); sortedList.pop(); m_loadedComponents.push_back(comp); // create a component instance if need be if (comp->ShouldAutoInstance()) { trace("Initializing instance of %s.\n", comp->GetName().c_str()); comp->CreateInstance(std::string()); } } }