MCPcopy Create free account
hub / github.com/MCJack123/craftos2 / getComputerConfig

Function getComputerConfig

src/configuration.cpp:37–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35}
36
37struct computer_configuration getComputerConfig(int id) {
38 struct computer_configuration cfg = {"", true, false, false, 0, 0};
39 std::ifstream in(getBasePath() / "config" / (std::to_string(id) + ".json"));
40 if (!in.is_open()) return cfg;
41 if (in.peek() == std::ifstream::traits_type::eof()) { in.close(); return cfg; } // treat an empty file as if it didn't exist in the first place
42 Value root;
43 Poco::JSON::Object::Ptr p;
44 try { p = root.parse(in); } catch (Poco::JSON::JSONException &e) {
45 cfg.loadFailure = true;
46 showMessage("An error occurred while parsing the per-computer configuration file for computer " + std::to_string(id) + ": " + e.message() + ". The current session's config will be reset to default, and any changes made will not be saved.");
47 in.close();
48 return cfg;
49 } catch (Poco::Exception &e) {
50 cfg.loadFailure = true;
51 showMessage("An error occurred while parsing the per-computer configuration file for computer " + std::to_string(id) + ": " + e.message() + ". The current session's config will be reset to default, and any changes made will not be saved.");
52 in.close();
53 return cfg;
54 }catch (std::exception &e) {
55 cfg.loadFailure = true;
56 showMessage("An error occurred while parsing the per-computer configuration file for computer " + std::to_string(id) + ": " + e.what() + ". The current session's config will be reset to default, and any changes made will not be saved.");
57 in.close();
58 return cfg;
59 } catch (...) {
60 cfg.loadFailure = true;
61 showMessage("An error occurred while parsing the per-computer configuration file for computer " + std::to_string(id) + ": unknown. The current session's config will be reset to default, and any changes made will not be saved.");
62 in.close();
63 return cfg;
64 }
65 in.close();
66 if (root.isMember("isColor")) cfg.isColor = root["isColor"].asBool();
67 if (root.isMember("label")) {
68 if (root.isMember("base64")) cfg.label = b64decode(root["label"].asString());
69 else cfg.label = std::string(root["label"].asString());
70 }
71#if !defined(__IPHONEOS__)
72 if (root.isMember("startFullscreen")) cfg.startFullscreen = root["startFullscreen"].asBool();
73#endif
74 if (root.isMember("computerWidth")) cfg.computerWidth = root["computerWidth"].asInt();
75 if (root.isMember("computerHeight")) cfg.computerHeight = root["computerHeight"].asInt();
76 return cfg;
77}
78
79void setComputerConfig(int id, const computer_configuration& cfg) {
80 if (cfg.loadFailure) return;

Callers 1

ComputerMethod · 0.85

Calls 10

showMessageFunction · 0.85
b64decodeFunction · 0.85
parseMethod · 0.80
whatMethod · 0.80
isMemberMethod · 0.80
asBoolMethod · 0.80
asStringMethod · 0.80
asIntMethod · 0.80
closeMethod · 0.65
getBasePathFunction · 0.50

Tested by

no test coverage detected