z_mirror/hare-fmm
git clone https://git.raiguard.me/z_mirror/hare-fmm
74b341b
trunk
1// The Factorio mods directory contains a "mod-list.json" file that contains the 2// enabled states of each mod in the directory. 3 4use encoding::json; 5use strings; 6 7type modlist = []modlist_entry; 8type modlist_entry = struct { 9 name: str, 10 enabled: bool, 11 version: (version | void), 12}; 13 14fn modlist_free(modlist: modlist) void = { 15 for (let entry &.. modlist) { 16 free(entry.name); 17 }; 18}; 19 20// Marshals a [[modlist]] into a [[json::value]] for writing. The return value 21// must be freed with [[encoding::json::finish]]. 22fn modlist_marshal(modlist: modlist) json::value = { 23 let mods: []json::value = []; 24 for (let mod &.. modlist) { 25 let mod_obj = json::newobject(); 26 json::set(&mod_obj, "name", strings::dup(mod.name)!)!; 27 json::set(&mod_obj, "enabled", mod.enabled)!; 28 if (mod.version is version) { 29 json::set(&mod_obj, "version", version_str(mod.version as version, false))!; 30 }; 31 append(mods, mod_obj)!; 32 }; 33 let json = json::newobject(); 34 json::set(&json, "mods", mods)!; 35 return json; 36}; 37 38// Unmarshals a [[modlist]] from a [[json::value]]. The return value must be 39// freed with [[modlist_free]]. This function assumes that the input is valid. 40fn modlist_unmarshal(root: json::value) modlist = { 41 const mods = json::get(&(root as json::object), "mods") as *json::value; 42 const mods = *mods as []json::value; 43 44 let modlist: []modlist_entry = []; 45 for (const mod .. mods) { 46 const mod = mod as json::object; 47 const name = json::get(&mod, "name") as *json::value; 48 const enabled = json::get(&mod, "enabled") as *json::value; 49 const version: (version | void) = 50 match (json::get(&mod, "version")) { 51 case let version: *json::value => 52 yield version_parse(*version as str)!; 53 case => 54 yield void; 55 }; 56 append(modlist, modlist_entry { 57 name = strings::dup(*name as str)!, 58 enabled = *enabled as bool, 59 version = version, 60 })!; 61 }; 62 return modlist; 63};