/// <summary> /// Downloads an online mod and installs it. If the mod has any settings or controls, /// a settings file is created (if it doesn't exist already) with the defaults populated. /// </summary> /// <param name="mod">The mod to download.</param> public void DownloadMod(Mod mod) { var newPath = $"{Constants.SFMFDirectory}/{mod.Name}-{mod.Version}.dll"; mod.SettingsPath = $"{Constants.ModSettingsDirectory}/{mod.Name}.csv"; // Don't everwrite any custom settings that may already exist for this mod, load them instead. if (!File.Exists(mod.SettingsPath)) { SaveModSettings(mod); } else { mod.Settings = GetModSettings(mod.SettingsPath); } using (var client = new WebClient()) client.DownloadFileAsync(new Uri(mod.Download), newPath); mod.Path = newPath; Manifest.Add(mod); SaveManifest(); InstallMod(mod); }
private void AddFile(string type, string path, byte[] data) { files.Add(path, new Dictionary <string, object> { { "IsDirectory", false }, { "Data", data } }); Manifest.Add(new ManifestFile(this) { FullPath = path, MediaType = type }); }
/// <summary> /// Loads a mod from the user's local machine and installs it. /// </summary> /// <param name="path">The absolute path of the mod to load.</param> public void LoadLocalMod(string path) { var name = Path.GetFileNameWithoutExtension(path); var settingsPath = $"{Constants.ModSettingsDirectory}/{name}.csv"; var mod = new Mod { Name = name, Version = "local", Path = path, SettingsPath = settingsPath, Settings = GetModSettings(settingsPath), DisableScoreReporting = true, Local = true }; Manifest.Add(mod); SaveManifest(); InstallMod(mod); }
public async Task InitializeAsync() { if (await this.VerifyMimetypeAsync() == false) { throw new Exception("Invalid mimetype."); } ContentLocation = await this.GetContentLocationAsync(); Metadata = await this.GetMetadataAsync(); foreach (var item in (await this.GetManifestAsync()).ToList()) { Manifest.Add(item.Id, item); } Spine = await this.GetSpineAsync(); TableOfContents = await this.GetTableOfContentsAsync(); Cover = await this.GetCoverAsync(); }
public void AppendItem(EpubItem item) { Manifest.Add(item); AbsolutePathIndex.Add(item.AbsolutePath, item); IdIndex.Add(item.Id, item); }
//Creates an optimal list of goods to buy to exploit an arbitrage, given //the details of the journey that change the profitability of a product. //The considerations include //Whether the products will "fit" onto our "vehicle" (both of these are //abstract ideas. For example, we can consider our budget a vehicle with //limited financial space), //What variable costs may arise from transporting a product (beyond the //purchase cost), and //If we have a target metric (like a target total mass, or budget), we can //use the journey's fixed costs to make a better prediction of a product's //profitability by spreading the fixed costs over the cargo. //"fill_space" and "constraint_spaces" represent that first point. Note //that they are considered to be orthogonal. F.e. putting a widget on board //simultaniously takes up literal volume in the hold, while also taking up //an abstract "volume" of the budget available to buy products, as well as //possibly reducing the remaining mass that can be packed without changing //the performance characteristics of the craft. public Manifest MakeShoppingList( Manifest products, Func <Item, float, float> GetTransportCosts = null, Space fill_space = null, float fixed_costs = 0, IEnumerable <Space> constraint_spaces = null, Func <Item, bool> IsProductLegal = null) { //Input sanitizing if (GetTransportCosts == null) { GetTransportCosts = (product, quantity) => 0; } if (constraint_spaces == null) { constraint_spaces = Enumerable.Empty <Space>(); } if (IsProductLegal == null) { IsProductLegal = product => true; } products = new Manifest(products.Samples .Where(product => IsProductLegal(product)) .Select(product => (product, products.GetQuantity(product)))); //Functions Manifest shopping_list = new Manifest(); Action <Item, float> AddToShoppingList = (product, quantity) => { shopping_list.Add(product, quantity); fill_space.Pack(product, quantity); foreach (Space space in constraint_spaces) { space.Pack(product, quantity); } }; //The "marginal quantity" of an item is the maximum additional amount //you can acquire without experiencing significant nonlinearity - //that is, the first unit acquired has a different density than the //last unit. //F.e. when purchasing a product, the ratio of mass to cost changes as //you buy more - you will have to pay more, and thus its //"financial density" goes down. This prevents accurately estimating a //product's profitability. Func <Item, float> CalculateMarginalQuantity = product => { float marginal_quantity = products[product] - shopping_list[product]; if (fill_space != null) { marginal_quantity = Mathf.Min(marginal_quantity, fill_space.GetMarginalQuantity(product)); } foreach (Space constraint_space in constraint_spaces) { marginal_quantity = Mathf.Min(marginal_quantity, constraint_space.GetMarginalQuantity(product)); } return(marginal_quantity); }; Dictionary <Item, float> marginal_quantities = null; Action BakeMarginalQuantities = () => { marginal_quantities = products.Samples.ToDictionary(product => product, CalculateMarginalQuantity); }; BakeMarginalQuantities(); Func <Item, float> GetMarginalQuantity = product => { return(marginal_quantities[product]); }; //Estimates how many units of product will fit into the remaining //volume available in the Space prodided, using the product's next //marginal quantity's density as a guide. Func <Item, Space, float> GetProjectedCapacity = (product, space) => { float marginal_quantity = GetMarginalQuantity(product); return(marginal_quantity / space.GetCapacityFraction(product, marginal_quantity)); }; //Determines if a product may prevent complete filling of fill_space //by seeing whether the quantity necessary for fill_space is greater //than the remaining volume available in the constraint_spaces. //This function does not directly compare capacity of fill space to //constraint space because we are only interested whether at the //_current_rate_ the product is projected to outrun the volume //available. This prevents unecessary culling when dealing with //supra-linear spaces. //f.e. the first widget bought may cost $1, which does not endanger //going over budget. But the last widget may cost $1T, which does. //Note however, that this could be suboptimal in cases of //sub-linear spaces. Func <Item, bool> IsDense = product => { float fill_quantity = GetProjectedCapacity(product, fill_space); foreach (Space constraint_space in constraint_spaces) { if (GetProjectedCapacity(product, constraint_space) < fill_quantity) { return(false); } } return(true); }; Func <Item, float, float> GetVariableCosts = (product, quantity) => { return(GetPurchaseCost(product, quantity) + GetTransportCosts(product, quantity)); }; Func <Item, bool> IsProductProfitable = product => { float marginal_quantity = GetMarginalQuantity(product); return(GetSaleValue(product, marginal_quantity) > GetVariableCosts(product, marginal_quantity)); }; Func <Item, float> GetProductROI = (product) => { float quantity = GetMarginalQuantity(product); float existing_quantity = shopping_list[product]; float variable_costs = GetVariableCosts(product, existing_quantity + quantity) - GetVariableCosts(product, existing_quantity); float total_costs = variable_costs; if (fill_space != null) { total_costs += fixed_costs * fill_space.GetCapacityFraction(product, quantity); } float sale_value = GetSaleValue(product, existing_quantity + quantity) - GetSaleValue(product, existing_quantity); return((sale_value - total_costs) / total_costs); }; Action <Manifest> MarginalizePackage = package => { Dictionary <Item, float> fractions = package.Samples.ToDictionary( product => product, product => GetMarginalQuantity(product) / package[product]); Item most_marginal_product = package.Samples .MinElement(product => fractions[product]); }; Func <Manifest, float> GetPackageROI = package => { float variable_costs = 0; float sale_value = 0; foreach (Item product in package.Samples) { float quantity = package[product]; float existing_quantity = shopping_list[product]; variable_costs += GetVariableCosts(product, quantity + existing_quantity) - GetVariableCosts(product, existing_quantity); sale_value += GetSaleValue(product, quantity + existing_quantity) - GetSaleValue(product, existing_quantity); } return((sale_value - variable_costs) / variable_costs); }; Func <List <Item> > GetLegalProducts = () => { return(products.Samples .Where(IsProductLegal) .Where(product => GetMarginalQuantity(product) > 0.000001f) .Where(IsProductProfitable) .ToList()); }; List <Item> legal_products = GetLegalProducts(); //While there are still products available, pick the best product //(measured by roi) and add a small amount of it to the shopping list. //(products are filtered if they won't fit or aren't profitable) //Shopping list is created such that the fill_space (if provided) has //as little remaining room inside it as possible. Furthermore, none of //the Spaces are allowed to be overfilled. If any space has no //remaining room for more products, the shopping list is finished. //Consequentally, in order to avoid returning an underfilled shopping //list, if the best product would overfill a constraint space if you //had enough of it to fill up the "fill_space" (such a product is //considered to not be "dense"), then this tries to find a combination //of products (a "package") that _is_ dense while maximizing quantity //of the best product and beating the best dense product. while (legal_products.Count > 0) { legal_products.Sort(product => - GetProductROI(product)); List <Item> dense_products = null; if (fill_space != null) { dense_products = legal_products .Where(IsDense) .ToList() .GetRangeOrGetClose(0, 10); } //If best_product is dense, just add it to the shopping_list. If //not, try to create a package that includes the best_product while //being dense (by including a dense product to compensate). If you //fail, make next best product the new best_product and try again. //Package Construction: //Compute ratio of GetMarginalQuantity to GetCapacityFraction and //use it to guess how much of each component product is required to //fill up the fill_space and the constraint_space. From here, its a //simple linear mixing of the two components such that the //fill_space and the constraint space are both full. This is //repeated for all constraint spaces and the smallest package is //selected. Finally, marginalize the package. //This algorithm assumes linearity to perform the mixing, however, //because we use GetMarginalQuantity, linearity is ensured over //the quantities we are using. And because the package is //marginalized at the end, these assumptions hold. //This algorithm assumes that components are in direct competition //for space, which may not be the case (f.e. a liquid and a solid). //This may cause suboptimality. Item best_product = legal_products.First(); Manifest package = null; while (fill_space != null && dense_products.Count != 0 && !dense_products.Contains(best_product) && package == null) { legal_products.Remove(best_product); foreach (Item dense_product in dense_products) { IEnumerable <Space> constraining_spaces = constraint_spaces .Where(constraint_space => GetProjectedCapacity(best_product, fill_space) > GetProjectedCapacity(best_product, constraint_space)); Manifest candidate_package = constraining_spaces .Select(constraint_space => { float af = GetProjectedCapacity(best_product, fill_space); float ac = GetProjectedCapacity(best_product, constraint_space); float bf = GetProjectedCapacity(dense_product, fill_space); float bc = GetProjectedCapacity(dense_product, constraint_space); float a_hybrid = (1 - bf / bc) / (1 / ac - bf / bc / af); float b_hybrid = bf * (1 - a_hybrid / af); return(new Manifest( new Dictionary <Item, float> { { best_product, a_hybrid }, { dense_product, b_hybrid } })); }).MinElement(package_ => package_[best_product]); MarginalizePackage(candidate_package); if (GetPackageROI(candidate_package) > GetProductROI(dense_products.First())) { package = candidate_package; break; } } best_product = legal_products.First(); } if (package == null) { AddToShoppingList(best_product, GetMarginalQuantity(best_product)); } else { foreach (Item product in package.Samples) { AddToShoppingList(product, package[product]); } } BakeMarginalQuantities(); legal_products = GetLegalProducts(); } return(shopping_list); }
public async Task MaterializeAsync(ILogStore store, byte[] secretKey = default, long?startingFrom = default, CancellationToken cancellationToken = default) { if (startingFrom == default) { startingFrom = Interlocked.Read(ref _index) + 1; } foreach (var entry in store.StreamEntries((ulong)startingFrom, secretKey)) { Interlocked.Exchange(ref _index, (long)entry.Index.GetValueOrDefault()); foreach (var @object in entry.Objects) { switch (@object.Data) { case Namespace ns: { var key = ns; Namespaces.Add(key); _namespace = key; if (!Revisions.ContainsKey(_namespace.Value)) { Revisions.Add(_namespace.Value, new Dictionary <string, ulong>(StringComparer.OrdinalIgnoreCase)); } if (!Manifest.ContainsKey(_namespace.Value)) { Manifest.Add(_namespace.Value, new Dictionary <ulong, List <Schema> >()); } if (!Roles.ContainsKey(_namespace.Value)) { Roles.Add(_namespace.Value, new List <string>()); } break; } case Schema schema: { var key = schema.Name; if (!Revisions[_namespace.Value].TryGetValue(key, out var revision)) { Revisions[_namespace.Value].Add(key, revision = 1); } if (!Manifest[_namespace.Value].TryGetValue(revision, out var manifest)) { Manifest[_namespace.Value].Add(revision, manifest = new List <Schema>()); } if (!Schemas.TryGetValue(key, out var schemaMap)) { Schemas.Add(key, schemaMap = new Dictionary <ulong, List <Schema> >()); } if (!schemaMap.TryGetValue(revision, out var list)) { schemaMap.Add(revision, list = new List <Schema>()); } manifest.Add(schema); list.Add(schema); await _events.OnSchemaAddedAsync(store, schema, cancellationToken); break; } case RevokeRole revokeRole: { if (!revokeRole.Verify()) { throw new InvalidOperationException($"invalid {revokeRole.Type}"); } if (RoleGrants.TryGetValue(_namespace.Value, out var lookup) && lookup.Count == 1 && lookup[Constants.DefaultOwnerRole].Count == 1) { throw new CannotRemoveSingleOwnerException("cannot revoke admin rights of only owner"); } break; } default: throw new NotImplementedException(@object.Data.GetType().Name); } } } }
public static void UpdateDownloadableLanguagesInfo(string ocrLanguagesManifestFileLocalPath, CompletionDelegate completion) { DownloadableLanguagesError = null; List <Task> downloadTasks = new List <Task>(); Task task = Task.Run(async() => { string[] directories = { LanguagesDirectory }; void ParseManifestJsonText(string jsonContent) { _originalDeserializedManifest = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonContent); if (_originalDeserializedManifest != null) { Manifest = new Dictionary <string, Dictionary <string, Dictionary <string, object> > >(_originalDeserializedManifest.Count); } foreach (KeyValuePair <string, object> entry in _originalDeserializedManifest) { if (entry.Value.GetType() != typeof(string)) { // Not the "Version" tag Dictionary <string, Dictionary <string, object> > val = (JToken.FromObject(entry.Value)).ToObject(typeof(Dictionary <string, Dictionary <string, object> >)) as Dictionary <string, Dictionary <string, object> >; Manifest.Add(entry.Key, val); } } DownloadableLanguagesError = Manifest != null ? null : new Exception("Error parsing OCR languages manifest file"); if (Manifest == null) { Manifest = GenerateTemporaryManifestWithLanguageFilesInDirectories(directories); } UpdateLanguagesWithCurrentManifest(); completion?.Invoke(null, DownloadableLanguagesError); } if (!string.IsNullOrWhiteSpace(ocrLanguagesManifestFileLocalPath)) { // Read from the embedded resource using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ocrLanguagesManifestFileLocalPath)) using (StreamReader reader = new StreamReader(stream)) { string jsonContent = reader.ReadToEnd(); ParseManifestJsonText(jsonContent); } } else if (AdditionalLanguagesURL != null) { string url = Path.Combine(AdditionalLanguagesURL, "Manifest.json"); string fileDownloadLocation = Path.Combine(_downloadLanguagesDirectory, Path.GetFileName(url)); HttpClientDownloader httpClientDownloader = new HttpClientDownloader(); httpClientDownloader.DownloadCompleted += (object sender, DownloadCompletedEventArgs e) => { if (!string.IsNullOrWhiteSpace(fileDownloadLocation) && File.Exists(fileDownloadLocation)) { string jsonContent = File.ReadAllText(fileDownloadLocation); ParseManifestJsonText(jsonContent); } else { Manifest = null; DownloadableLanguagesError = e.Error; } }; try { await httpClientDownloader.DownloadFileAsync(url, fileDownloadLocation, null); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Manifest = GenerateTemporaryManifestWithLanguageFilesInDirectories(directories); } }); if (!string.IsNullOrWhiteSpace(ocrLanguagesManifestFileLocalPath)) { downloadTasks.Add(task); Task.WaitAll(downloadTasks.ToArray(), -1); } }
static void Main(string[] args) { if (args != null && args.Length == 4 && args[0] == "extract") { ExtractPackage(args[1], args[3], args[2]); } else if (args != null && args.Length == 3 && args[0] == "generate") { string path = args[1], output = args[2]; DirectoryInfo outputDirectory = GetOutputLocation(output); Manifest manifest = new Manifest(); DirectoryInfo sourceDirectory = new DirectoryInfo(path); DirectoryInfo[] subDirectories = sourceDirectory.GetDirectories(); foreach (DirectoryInfo subDirectory in subDirectories) { manifest.Add(GeneratePackage(subDirectory, outputDirectory)); } //manifest.Id = GenerateManifestId(manifest); XmlSerializer x = new XmlSerializer(typeof(Manifest)); using (XmlWriter manifestWriter = XmlWriter.Create(output + "/manifest.xml")) { x.Serialize(manifestWriter, manifest); } } else if (args != null && args.Length == 7 && args[0] == "teamcity") { string szTeamCityServer = args[1], szBuildId = args[2], szBranchName = args[3], szUserName = args[4], szPassword = args[5], fileName = args[6]; WebRequest getBuildInfo = WebRequest.Create(szTeamCityServer + "/app/rest/builds/buildType:(id:" + szBuildId + "),branch:" + szBranchName + ",status:SUCCESS/artifacts"); getBuildInfo.Credentials = new System.Net.NetworkCredential(szUserName, szPassword); WebResponse webResponse = getBuildInfo.GetResponse(); string szResponse = string.Empty; if (webResponse != null) { Stream objStream = webResponse.GetResponseStream(); StreamReader objReader = new StreamReader(objStream); while (!objReader.EndOfStream) { szResponse += objReader.ReadLine(); } } XmlDocument buildInfoDoc = new XmlDocument(); buildInfoDoc.LoadXml(szResponse); XmlNodeList files = buildInfoDoc.SelectNodes("/files/file"); if (files != null) { foreach (XmlNode file in files) { if (file.Attributes != null) { foreach (XmlAttribute attribute in file.Attributes) { if (attribute.Name == "href") { string szHref = attribute.Value; if (szHref.EndsWith(".zip")) { int startId = szHref.IndexOf("id:"); if (startId >= 0) { int endId = szHref.IndexOf("/", startId); string szId = szHref.Substring(startId + 3, endId - startId - 3); int startFile = szHref.LastIndexOf("/"); if (startFile >= 0) { string szArtifactName = szHref.Substring(startFile + 1); string szArtifact = szTeamCityServer + "/repository/download/" + szBuildId + "/" + szId + ":id/" + szArtifactName; WebClient downloadClient = new WebClient(); downloadClient.Credentials = new System.Net.NetworkCredential(szUserName, szPassword); downloadClient.DownloadFile(szArtifact, szArtifactName); if (File.Exists(szArtifactName)) { Directory.CreateDirectory("Temp"); ZipFile.ExtractToDirectory(szArtifactName, @"Temp\Client"); DirectoryInfo outputDirectory = GetOutputLocation(Directory.GetCurrentDirectory()); Manifest manifest = new Manifest(); DirectoryInfo sourceDirectory = new DirectoryInfo("Temp"); DirectoryInfo[] subDirectories = sourceDirectory.GetDirectories(); foreach (DirectoryInfo subDirectory in subDirectories) { manifest.Add(GeneratePackage(subDirectory, outputDirectory)); } //manifest.Id = GenerateManifestId(manifest); XmlSerializer x = new XmlSerializer(typeof(Manifest)); using (XmlWriter manifestWriter = XmlWriter.Create(outputDirectory + "/manifest.xml")) { x.Serialize(manifestWriter, manifest); } Directory.Delete("Temp", true); File.Delete(szArtifactName); } } } } } } } } } } else if (args != null && args.Length == 2) { if (args[0] == "Client") { string szCurrentDirectory = Directory.GetCurrentDirectory(); if (Directory.Exists(Path.Combine(szCurrentDirectory, "bin")) && Directory.Exists(Path.Combine(szCurrentDirectory, "appdata")) && Directory.Exists(Path.Combine(szCurrentDirectory, "data")) && Directory.Exists(Path.Combine(szCurrentDirectory, "upgrade"))) { Directory.CreateDirectory(Path.Combine(szCurrentDirectory, "Temp")); string szDestination = Path.Combine(szCurrentDirectory, @"Temp\Client"); Directory.CreateDirectory(szDestination); DirectoryCopy(Path.Combine(szCurrentDirectory, "bin"), Path.Combine(szDestination, "bin"), true); DirectoryCopy(Path.Combine(szCurrentDirectory, "appdata"), Path.Combine(szDestination, "appdata"), true); DirectoryCopy(Path.Combine(szCurrentDirectory, "data"), Path.Combine(szDestination, "data"), true); DirectoryCopy(Path.Combine(szCurrentDirectory, "upgrade"), Path.Combine(szDestination, "upgrade"), true); File.Copy(Path.Combine(szCurrentDirectory, "install.config"), Path.Combine(szDestination, "install.config")); DirectoryInfo outputDirectory = GetOutputLocation(szCurrentDirectory); Manifest manifest = new Manifest(); DirectoryInfo sourceDirectory = new DirectoryInfo(Path.Combine(szCurrentDirectory, "Temp")); DirectoryInfo[] subDirectories = sourceDirectory.GetDirectories(); foreach (DirectoryInfo subDirectory in subDirectories) { manifest.Add(GeneratePackage(subDirectory, outputDirectory)); } //manifest.Id = GenerateManifestId(manifest); XmlSerializer x = new XmlSerializer(typeof(Manifest)); using (XmlWriter manifestWriter = XmlWriter.Create(outputDirectory + "/manifest.xml")) { x.Serialize(manifestWriter, manifest); } WebClient client = new WebClient(); client.UploadFile(args[1], outputDirectory + "/manifest.xml"); client.UploadFile(args[1], outputDirectory + "/Client.zip"); Directory.Delete(Path.Combine(szCurrentDirectory, "Temp"), true); File.Delete(outputDirectory + "/manifest.xml"); File.Delete(outputDirectory + "/Client.zip"); } } else if (args[0] == "CaptureEngine") { string szCurrentDirectory = Directory.GetCurrentDirectory(); if (Directory.Exists(Path.Combine(szCurrentDirectory, "x86")) && Directory.Exists(Path.Combine(szCurrentDirectory, "x64")) && File.Exists(Path.Combine(szCurrentDirectory, "Ancile.CaptureEngine.dll"))) { Directory.CreateDirectory(Path.Combine(szCurrentDirectory, "Temp")); string szDestination = Path.Combine(szCurrentDirectory, @"Temp\CaptureEngine"); Directory.CreateDirectory(szDestination); DirectoryCopy(Path.Combine(szCurrentDirectory, "x86"), Path.Combine(szDestination, "x86"), true); DirectoryCopy(Path.Combine(szCurrentDirectory, "x64"), Path.Combine(szDestination, "x64"), true); File.Copy(Path.Combine(szCurrentDirectory, "Ancile.CaptureEngine.dll"), Path.Combine(szDestination, "Ancile.CaptureEngine.dll")); DirectoryInfo outputDirectory = GetOutputLocation(szCurrentDirectory); Manifest manifest = new Manifest(); DirectoryInfo sourceDirectory = new DirectoryInfo(Path.Combine(szCurrentDirectory, "Temp")); DirectoryInfo[] subDirectories = sourceDirectory.GetDirectories(); foreach (DirectoryInfo subDirectory in subDirectories) { manifest.Add(GeneratePackage(subDirectory, outputDirectory)); } //manifest.Id = GenerateManifestId(manifest); XmlSerializer x = new XmlSerializer(typeof(Manifest)); using (XmlWriter manifestWriter = XmlWriter.Create(outputDirectory + "/manifest.xml")) { x.Serialize(manifestWriter, manifest); } WebClient client = new WebClient(); client.UploadFile(args[1], outputDirectory + "/manifest.xml"); client.UploadFile(args[1], outputDirectory + "/CaptureEngine.zip"); Directory.Delete(Path.Combine(szCurrentDirectory, "Temp"), true); File.Delete(outputDirectory + "/manifest.xml"); File.Delete(outputDirectory + "/CaptureEngine.zip"); } } } }