private bool cmd_scan(CommandContext ctx, Command cmd, IOutputDevice outputDevice) { // Grab the manifest var manifest = ModuleRegistry.GetModule <ConfigSystem>().GetConfig <Manifest>(ctx, "Manifest"); if (manifest == null) { outputDevice.WriteStaticLine("$redFailed to load the manifest"); ctx.ErrorFlag = true; return(true); } // Iterate each game foreach (var game in manifest.Games) { if (!ScanForGame(ctx, game)) { continue; } ctx.LocatedGame = game; break; } if (ctx.LocatedGame == null) { outputDevice.WriteStaticLine("$redNo recognised games found in current directory"); ctx.ErrorFlag = true; return(true); } outputDevice.WriteStaticLine($"$whiteIdentified game $green{ctx.LocatedGame.Name}"); return(true); }
/// <summary> /// Gets the configuration for the specified name /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="name"></param> /// <returns></returns> public T GetConfig <T>(CommandContext context, string name) where T : class { var path = Path.Combine(context.WorkingDirectory, $"uMod.{name}.json"); if (!ModuleRegistry.GetModule <FileSystem>().SecurityCheck(path)) { return(null); } return(!File.Exists(path) ? null : JsonConvert.DeserializeObject <T>(File.ReadAllText(path))); }
private bool cmd_fetch(CommandContext ctx, Command cmd, IOutputDevice outputDevice) { // Identify URL var url = cmd.GetNamedArg("url"); var nextBasicArg = 0; if (string.IsNullOrEmpty(url)) { if (cmd.SimpleArgs.Length <= nextBasicArg) { outputDevice.WriteStaticLine("$redURL of resource must be specified."); ctx.ErrorFlag = true; return(true); } url = cmd.SimpleArgs[nextBasicArg++]; } if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) { outputDevice.WriteStaticLine("$redURL is not well formed."); ctx.ErrorFlag = true; return(true); } // Identify output file var outPath = cmd.GetNamedArg("out"); if (string.IsNullOrEmpty(outPath)) { outPath = cmd.SimpleArgs.Length <= nextBasicArg?Path.GetFileName(url) : cmd.SimpleArgs[nextBasicArg++]; } outPath = Path.GetFullPath(Path.Combine(ctx.WorkingDirectory, outPath)); // Security check the output path if (!ModuleRegistry.GetModule <FileSystem>().SecurityCheck(outPath)) { outputDevice.WriteStaticLine("$redDestination path blocked due to security reasons."); ctx.ErrorFlag = true; return(true); } ILabel sizeLabel = null; try { // Let UI know we're starting to do something var label = outputDevice.WriteLabel($"Requesting {url}..."); // Create request, acquire response var req = WebRequest.Create(url); var response = req.GetResponse(); // Let UI know so far so good label.Text = $"Downloading {url}..."; sizeLabel = outputDevice.WriteLabel(""); // Did the server give us a content size? If so, add a progress bar var progBar = response.ContentLength > 0 ? outputDevice.WriteProgressBar() : null; var totalSize = response.ContentLength > 0 ? FormatContentLength(response.ContentLength) : "unknown"; // Write to file using (var outStream = File.OpenWrite(outPath)) { // Get the response stream and allocate a buffer var stream = response.GetResponseStream(); var buffer = new byte[1024]; // Iterate for as long as we can long totalRead = 0; while (stream != null && stream.CanRead) { // Read into the buffer, write to file, check eos (end of stream) var read = stream.Read(buffer, 0, 1024); outStream.Write(buffer, 0, read); if (read == 0) { break; } totalRead += read; // Update UI if (progBar != null) { progBar.Progress = (totalRead / (float)response.ContentLength); } sizeLabel.Text = $"{FormatContentLength(totalRead)} / {totalSize}"; } } } catch (Exception ex) { outputDevice.WriteStaticLine("Unknown error when fetching resource:"); outputDevice.WriteStaticLine(ex.ToString()); if (sizeLabel != null) { sizeLabel.Text = "$redCancelled"; } ctx.ErrorFlag = true; return(true); } ctx.ErrorFlag = false; return(true); }