public void AssignDefaults(IReadOnlyList <string> construction) { Color = LoadMethods.ParseColor32(construction[0]); Uncivilized = LoadMethods.YesNoConverter(construction[1]); CanReduceMilitancy = LoadMethods.YesNoConverter(construction[2]); if (!DateTime.TryParse(construction[3], out var dateResult)) { throw new Exception("Unknown date. " + construction[3]); } int.TryParse("9" + dateResult.ToString("yyyyMMdd"), out Date); }
public static (Entity, Entity, List <string>, List <string>) Main() { var goods = new NativeList <EntityWrapper>(Allocator.Temp); var goodsCategory = new NativeList <EntityWrapper>(Allocator.Temp); var goodNames = new List <string>(); var goodCategoryNames = new List <string>(); var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); var em = World.Active.EntityManager; var currentCategory = new Entity(); FileUnpacker.ParseFile(Path.Combine(Application.streamingAssetsPath, "Common", "goods.txt"), fileTree, values, GoodsMagicOverride); foreach (var category in fileTree) { foreach (var goodKvp in (List <KeyValuePair <int, object> >)category.Value) { var currentEntity = goods[goodKvp.Key - (int)MagicUnifiedNumbers.Goods]; var currentGood = em.GetComponentData <GoodsEntity>(currentEntity); foreach (var goodProperty in (List <KeyValuePair <int, object> >)goodKvp.Value) { var targetStr = values[(int)goodProperty.Value]; switch ((LoadVariables)goodProperty.Key) { case LoadVariables.Cost: float.TryParse(targetStr, out var cost); currentGood.Cost = cost; break; case LoadVariables.Color: currentGood.Color = LoadMethods.ParseColor32(targetStr); break; case LoadVariables.AvailableFromStart: currentGood.Availability = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.OverseasPenalty: currentGood.OverseasPenalty = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.Money: currentGood.Money = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.Tradeable: currentGood.Tradable = LoadMethods.YesNoConverter(targetStr); break; } } em.SetComponentData(currentEntity, currentGood); } } var goodCollectorEntity = FileUnpacker.GetCollector <GoodsCollection>(goods); goods.Dispose(); var categoryCollectorEntity = FileUnpacker.GetCollector <GoodsCategoryCollection>(goodsCategory); goodsCategory.Dispose(); return(goodCollectorEntity, categoryCollectorEntity, goodNames, goodCategoryNames); int GoodsMagicOverride(int parent, string target) { switch (parent) { case -1: var targetCategory = em.CreateEntity(typeof(GoodsCategoryEntity)); em.SetComponentData(targetCategory, new GoodsCategoryEntity { Index = goodCategoryNames.Count }); goodsCategory.Add(targetCategory); currentCategory = targetCategory; goodCategoryNames.Add(target); return((int)MagicUnifiedNumbers.Placeholder); case (int)MagicUnifiedNumbers.Placeholder: var targetGood = em.CreateEntity(typeof(GoodsEntity)); em.SetComponentData(targetGood, new GoodsEntity { Index = goodNames.Count, Category = currentCategory }); goods.Add(targetGood); goodNames.Add(target); return((int)MagicUnifiedNumbers.Goods + goodNames.Count - 1); default: return((int)MagicUnifiedNumbers.ContinueMagicNumbers); } } }
public ProvinceHistoryLoad(string path, IReadOnlyList <string> provinceNames, IReadOnlyList <int> idIndex, IReadOnlyList <string> countryTags) { ProvinceCacheInfos = new ProvinceCacheInfo[provinceNames.Count]; // var provinceLookup = new Dictionary<string, int>(provinceNames.Count); // for (var index = 0; index < provinceNames.Count; index++) // provinceLookup[provinceNames[index]] = index; var idLookup = new Dictionary <int, int>(idIndex.Count); for (var index = 0; index < idIndex.Count; index++) { idLookup[idIndex[index]] = index; } var tagLookup = new Dictionary <string, int>(countryTags.Count); for (var index = 0; index < countryTags.Count; index++) { tagLookup[countryTags[index]] = index; } var nativeTag = tagLookup["nat"]; var oceanTag = tagLookup["ocean"]; foreach (var filePath in Directory.EnumerateFiles(path, "*.txt")) { var fileMatch = Regex.Match(Path.GetFileNameWithoutExtension(filePath), @"(?<index>.*?)\-(?<name>.*)"); var name = fileMatch.Groups["name"].Value.Trim().ToLowerInvariant(); var prevIndex = fileMatch.Groups["index"].Value.Trim(); if (!int.TryParse(prevIndex, out var index)) { Debug.LogError($"Unknown index in file name: {filePath}"); continue; } if (!idLookup.TryGetValue(index, out index)) { Debug.LogError($"Unknown index lookup: {filePath}."); continue; } var defineName = provinceNames[index]; //if (!defineName.Equals(name, StringComparison.Ordinal)) // Debug.Log($"Definition name: {defineName} and file name: {name}. Mismatched."); var target = new ProvinceCacheInfo { Cores = new List <int>(), FileName = name, Name = defineName, Index = index }; var isCity = false; foreach (var rawLine in File.ReadLines(filePath, Encoding.GetEncoding(1252))) { if (LoadMethods.CommentDetector(rawLine, out var sliced, false)) { continue; } var variable = Regex.Match(sliced, @"^(?<key>.*?)\=(?<value>.*)"); // Very lazy check for nested declarations. Probably shouldn't rely on this. var key = variable.Groups["key"].Value; if (!variable.Success || key.IndexOfAny(new[] { '\t', ' ' }) == 0) { //Debug.Log($"Failed parsing: {sliced}"); continue; } key = key.Trim(); if (key.IndexOf('1') == 0) { break; } var value = variable.Groups["value"].Value.Trim(); switch (key) { case "add_core": if (!tagLookup.TryGetValue(value, out var core)) { Debug.Log($"Unknown country tag in cores: {value}."); } target.Cores.Add(core); break; case "owner": if (!tagLookup.TryGetValue(value, out target.Owner)) { Debug.Log($"Unknown country tag in owner: {value}."); } break; case "controller": if (!tagLookup.TryGetValue(value, out target.Controller)) { Debug.Log($"Unknown country tag in controller: {value}."); } break; case "religion": target.Religion = value; break; case "culture": target.Culture = value; break; case "base_tax": target.Tax = int.Parse(value); break; case "base_production": target.Production = int.Parse(value); break; case "base_manpower": target.Manpower = int.Parse(value); break; case "capital": target.Capital = value.Replace("\"", ""); break; case "is_city": isCity = LoadMethods.YesNoConverter(value); break; case "trade_goods": target.Goods = value; break; } } if (!isCity) { if (target.Tax == 0 && string.IsNullOrWhiteSpace(target.Capital) && string.IsNullOrWhiteSpace(target.Goods)) { // Ocean, splash splash. target.Owner = oceanTag; target.Controller = oceanTag; } else { // Native land, un colonized target.Owner = nativeTag; target.Controller = nativeTag; } } ProvinceCacheInfos[index] = target; } for (var i = 0; i < ProvinceCacheInfos.Length; i++) { if (ProvinceCacheInfos[i].FileName != null) { continue; } var name = provinceNames[i]; Debug.Log("Orphaned id index: " + idIndex[i] + " of name: " + name + " of index: " + i); ProvinceCacheInfos[i] = new ProvinceCacheInfo { FileName = name, Name = name, Index = i }; } }
public static (Entity, string[]) Main(Entity technologies, Entity inventions, Entity cultures, Entity ideologies, Entity subPolicies, Entity governments, Entity nationalValues) { var allCountries = new NativeArray <EntityWrapper>(LookupDictionaries.CountryTags.Count, Allocator.Temp); var em = World.Active.EntityManager; var countryArchetype = em.CreateArchetype(typeof(CountryPopulationComponent), typeof(CountryPoliticsComponent), typeof(CountryTechnologyComponent)); var rulingParties = new string[LookupDictionaries.CountryTags.Count]; var countryPolicies = new NativeArray <CountryPolicies>(LookupDictionaries.PolicyGroups.Count, Allocator.Temp); using (var nationalValueList = em.GetBuffer <EntityWrapper>(nationalValues).ToNativeArray(Allocator.Temp)) using (var governmentList = em.GetBuffer <EntityWrapper>(governments).ToNativeArray(Allocator.Temp)) using (var subPolicyList = em.GetBuffer <EntityWrapper>(subPolicies).ToNativeArray(Allocator.Temp)) using (var ideologyList = em.GetBuffer <EntityWrapper>(ideologies).ToNativeArray(Allocator.Temp)) using (var inventionList = em.GetBuffer <EntityWrapper>(inventions).ToNativeArray(Allocator.Temp)) using (var cultureList = em.GetBuffer <EntityWrapper>(cultures).ToNativeArray(Allocator.Temp)) using (var technologyList = em.GetBuffer <EntityWrapper>(technologies).ToNativeArray(Allocator.Temp)) using (var countryUpperHouse = new NativeList <CountryUpperHouse>(Allocator.Temp)) using (var countryInventions = new NativeList <CountryInventions>(Allocator.Temp)) using (var countryTechnologies = new NativeList <CountryTechnologies>(Allocator.Temp)) using (var countryCultures = new NativeList <CountryCultures>(Allocator.Temp)) { foreach (var countryFile in Directory.EnumerateFiles( Path.Combine(Application.streamingAssetsPath, "History", "countries"), "*.txt")) { var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); FileUnpacker.ParseFile(countryFile, fileTree, values, CountryHistoryMagicOverride); var countryTag = Regex.Match(Path.GetFileNameWithoutExtension(countryFile) ?? "", @"^.+?(?=\-)") .Value .Trim().ToLower(); if (!LookupDictionaries.CountryTags.TryGetValue(countryTag, out var countryIndex)) { continue; } var countryEntity = em.CreateEntity(countryArchetype); var currentCountry = new CountryEntity { Index = countryIndex }; var currentPopulation = new CountryPopulationComponent(); var currentPolitics = new CountryPoliticsComponent(); var currentTechnology = new CountryTechnologyComponent(); // Resetting polices for (var index = 0; index < countryPolicies.Length; index++) { countryPolicies[index] = Entity.Null; } foreach (var target in fileTree) { var targetStr = target.Key < (int)LoadVariables.BreakCore ? values[(int)target.Value] : string.Empty; switch ((LoadVariables)target.Key) { case LoadVariables.Capital: currentPolitics.Capital = int.Parse(targetStr); break; case LoadVariables.RulingParty: rulingParties[countryIndex] = targetStr; break; case LoadVariables.UpperHouse: foreach (var ideology in (List <KeyValuePair <int, object> >)target.Value) { if (!float.TryParse(values[(int)ideology.Value], out var ideologyPercentage)) { throw new Exception("Country ideology parsing failed! " + values[(int)ideology.Value]); } if (ideologyPercentage > 0.01) { countryUpperHouse.Add( new CountryUpperHouse( ideologyList[ideology.Key - (int)MagicUnifiedNumbers.Ideology], ideologyPercentage)); } } break; case LoadVariables.Government: if (!LookupDictionaries.Governments.TryGetValue(targetStr, out var governmentIndex)) { throw new Exception("Unknown government. " + targetStr); } currentPolitics.Government = governmentList[governmentIndex]; break; case LoadVariables.Prestige: currentPolitics.Prestige = int.Parse(targetStr); break; case LoadVariables.Civilized: currentTechnology.Civilized = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.NonStateCultureLiteracy: currentTechnology.NonStateCultureLiteracy = int.Parse(targetStr); break; case LoadVariables.Literacy: currentTechnology.Literacy = int.Parse(targetStr); break; case LoadVariables.Plurality: currentTechnology.Plurality = int.Parse(targetStr); break; case LoadVariables.Consciousness: currentPopulation.Consciousness = int.Parse(targetStr); break; case LoadVariables.NonStateConsciousness: currentPopulation.NonStateConsciousness = int.Parse(targetStr); break; case LoadVariables.PrimaryCulture: if (!LookupDictionaries.Cultures.TryGetValue(targetStr, out var primaryCultureIndex)) { throw new Exception("Unknown primary culture. " + targetStr); } currentPopulation.PrimaryCulture = cultureList[primaryCultureIndex]; break; case LoadVariables.NationalValue: if (!LookupDictionaries.NationalValues.TryGetValue(targetStr, out var natValIndex)) { throw new Exception("Unknown national value. " + targetStr); } currentPopulation.NationalValue = nationalValueList[natValIndex]; break; case LoadVariables.Culture: if (!LookupDictionaries.Cultures.TryGetValue(targetStr, out var cultureIndex)) { throw new Exception("Unknown culture. " + targetStr); } countryCultures.Add(cultureList[cultureIndex]); break; case LoadVariables.ForeignInvestment: case LoadVariables.Oob: case LoadVariables.Decision: // Skipping break; default: switch ((MagicUnifiedNumbers)(target.Key / 10000 * 10000)) { case MagicUnifiedNumbers.Placeholder: break; case MagicUnifiedNumbers.Invention: countryInventions.Add( new CountryInventions( inventionList[target.Key - (int)MagicUnifiedNumbers.Invention], LoadMethods.YesNoConverter(values[(int)target.Value]))); break; case MagicUnifiedNumbers.Technology: countryTechnologies.Add(technologyList[target.Key - (int)MagicUnifiedNumbers.Technology]); break; case MagicUnifiedNumbers.PolicyGroup: if (!LookupDictionaries.SubPolicies.TryGetValue(values[(int)target.Value], out var subPolicyIndex)) { throw new Exception("Unknown policy group. " + values[(int)target.Value]); } countryPolicies[countryIndex * LookupDictionaries.PolicyGroups.Count + (target.Key - (int)MagicUnifiedNumbers.PolicyGroup)] = subPolicyList[subPolicyIndex]; break; } Debug.LogWarning("Uncaught load variable on country history load " + (LoadVariables)target.Key); break; } } em.SetComponentData(countryEntity, currentCountry); em.SetComponentData(countryEntity, currentPopulation); em.SetComponentData(countryEntity, currentPolitics); em.SetComponentData(countryEntity, currentTechnology); em.AddBuffer <CountryUpperHouse>(countryEntity).AddRange(countryUpperHouse); countryUpperHouse.Clear(); em.AddBuffer <CountryTechnologies>(countryEntity).AddRange(countryTechnologies); countryTechnologies.Clear(); em.AddBuffer <CountryInventions>(countryEntity).AddRange(countryInventions); countryInventions.Clear(); em.AddBuffer <CountryCultures>(countryEntity).AddRange(countryCultures); countryCultures.Clear(); em.AddBuffer <CountryPolicies>(countryEntity).AddRange(countryPolicies); allCountries[countryIndex] = countryEntity; } } // Not in using statement as the values are mutable. countryPolicies.Dispose(); var countryCollector = FileUnpacker.GetCollector <CountryCollection>(allCountries); allCountries.Dispose(); return(countryCollector, rulingParties); int CountryHistoryMagicOverride(int parent, string target) { // Skipping other start dates return(Regex.IsMatch(target, @"\d+") ? (int)MagicUnifiedNumbers.Placeholder : (int)MagicUnifiedNumbers.ContinueMagicNumbers); } }
public static (Entity, List <string>) Main() { var popFiles = Directory.GetFiles(Path.Combine(Application.streamingAssetsPath, "PopTypes"), "*.txt"); // Output arrays var popTypes = new NativeList <EntityWrapper>(Allocator.Temp); var popNames = new List <string>(); popNames.AddRange(popFiles.Select(Path.GetFileNameWithoutExtension)); var em = World.Active.EntityManager; using (var needsList = new NativeList <DataGood>(Allocator.Temp)) using (var popRebels = new NativeList <DataValue>(Allocator.Temp)) { for (var index = 0; index < popTypes.Length; index++) { // Generating file tree var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); FileUnpacker.ParseFile(popFiles[index], fileTree, values, (i, s) => (int)MagicUnifiedNumbers.ContinueMagicNumbers); var currentEntity = em.CreateEntity(typeof(PopTypeEntity)); var currentPopType = new PopTypeEntity { Index = index }; foreach (var topLevel in fileTree) { switch ((LoadVariables)topLevel.Key) { case LoadVariables.Sprite: // Skipping sprite break; case LoadVariables.Color: currentPopType.Color = LoadMethods.ParseColor32(values[(int)topLevel.Value]); break; case LoadVariables.Strata: if (!Enum.TryParse(values[(int)topLevel.Value], true, out PopTypeEntity.Standing strata)) { throw new Exception("Strata unknown: " + values[(int)topLevel.Value]); } currentPopType.Strata = strata; break; case LoadVariables.StateCapitalOnly: currentPopType.StateCapitalOnly = LoadMethods.YesNoConverter(values[(int)topLevel.Value]); break; case LoadVariables.Rebel: RebelParser(topLevel); em.AddBuffer <DataValue>(currentEntity).AddRange(popRebels); popRebels.Clear(); break; case LoadVariables.CountryMigrationTarget: case LoadVariables.MigrationTarget: case LoadVariables.PromoteTo: case LoadVariables.Ideologies: var collapsedList = new List <float2>(); RestParser(topLevel, collapsedList); break; case LoadVariables.LifeNeeds: case LoadVariables.EverydayNeeds: case LoadVariables.LuxuryNeeds: NeedsParser(topLevel); break; } } em.AddBuffer <DataGood>(currentEntity).AddRange(needsList); em.SetComponentData(currentEntity, currentPopType); popTypes.Add(currentEntity); needsList.Clear(); // DEBUG //break; void RestParser(KeyValuePair <int, object> currentBranch, List <float2> targetList) { } void NeedsParser(KeyValuePair <int, object> currentBranch) { // There should be no nested values within needs. var startingNeeds = needsList.Length; foreach (var goodsKvp in (List <KeyValuePair <int, object> >)currentBranch.Value) { if (!float.TryParse(values[(int)goodsKvp.Value], out var goodsNeeded)) { throw new Exception("Unknown goods needed: " + values[(int)goodsKvp.Value]); } needsList.Add(new DataGood(goodsKvp.Key - (int)MagicUnifiedNumbers.Goods, goodsNeeded)); } switch ((LoadVariables)currentBranch.Key) { case LoadVariables.LifeNeeds: currentPopType.lifeRange = new int2(startingNeeds, needsList.Length); break; case LoadVariables.EverydayNeeds: currentPopType.everydayRange = new int2(startingNeeds, needsList.Length); break; case LoadVariables.LuxuryNeeds: currentPopType.luxuryRange = new int2(startingNeeds, needsList.Length); break; } } void RebelParser(KeyValuePair <int, object> currentBranch) { foreach (var armyKvp in (List <KeyValuePair <int, object> >)currentBranch.Value) { if (!float.TryParse(values[(int)armyKvp.Value], out var armyRatio)) { throw new Exception("Unknown army ratio: " + values[(int)armyKvp.Value]); } if (armyRatio < 0.001) { continue; } popRebels.Add(new DataValue(armyKvp.Key - (int)MagicUnifiedNumbers.Unit, armyRatio)); } } } } var popTypeCollectorEntity = FileUnpacker.GetCollector <PopTypeCollection>(popTypes); popTypes.Dispose(); return(popTypeCollectorEntity, popNames); }
public static void Main() { // TODO: Needs policies and ideologies. // Output events list. var events = new List <GameEventInfo>(); // Collapse tree into arrays var eventRanges = new List <int3>(); // x: type. z: start index, inclusive. w: end index, exclusive // If type == random_list, x is replaced with chance. // Value arrays var eventActions = new List <float2>(); // x: type. z: threshold var stringValues = new List <string>(); // TODO: Possibly convert all disposes of parent location to using? var parentLocation = new NativeMultiHashMap <int, int>(10, Allocator.TempJob); foreach (var eventFile in Directory.EnumerateFiles(Path.Combine(Application.streamingAssetsPath, "Events"), "*.txt")) { var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); FileUnpacker.ParseFile(eventFile, fileTree, values, EventMagicOverride); GameEventInfo currentEvent; foreach (var parsedEvent in fileTree) { currentEvent = new GameEventInfo { Fired = false, Index = eventRanges.Count }; parentLocation.Add(parsedEvent.Key, eventRanges.Count); eventRanges.Add(new int3(parsedEvent.Key, -1, -1)); //FileUnpacker.ProcessQueue(parsedEvent, eventActions, eventRanges, //parentLocation, values, EventSwitchOverride); events.Add(currentEvent); } bool EventSwitchOverride(string targetStr, KeyValuePair <int, object> kvpObject) { switch ((LoadVariables)kvpObject.Key) { case LoadVariables.FireOnlyOnce: currentEvent.FireOnlyOnce = LoadMethods.YesNoConverter(targetStr); return(true); case LoadVariables.ChangeRegionName: case LoadVariables.ChangeProvinceName: case LoadVariables.Title: case LoadVariables.Desc: case LoadVariables.Picture: case LoadVariables.Name: eventActions.Add(new float2(kvpObject.Key, stringValues.Count)); stringValues.Add(targetStr.Replace("\"", "")); return(true); case LoadVariables.HasLeader: // String // Skipping return(true); default: return(false); } } // Assigns magic numbers. int EventMagicOverride(int parent, string str) { if ((parent == (int)LoadVariables.AddCasusBelli || parent == (int)LoadVariables.CasusBelli) && str.Equals("type")) { return((int)LoadVariables.TypeCasusBelli); } if (parent != (int)LoadVariables.RandomList) { return((int)MagicUnifiedNumbers.ContinueMagicNumbers); } if (!int.TryParse(str, out var probability)) { throw new Exception("Random list probability unknown! " + str); } return((int)MagicUnifiedNumbers.Probabilities + probability); } } parentLocation.Dispose(); }
public static (Entity, List <string>) Main() { var governments = new NativeList <EntityWrapper>(Allocator.Temp); var governmentNames = new List <string>(); var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); var em = World.Active.EntityManager; FileUnpacker.ParseFile(Path.Combine(Application.streamingAssetsPath, "Common", "governments.txt"), fileTree, values, GovernmentMagicOverride); using (var governIdeologies = new NativeList <DataInt>(Allocator.Temp)) { for (var index = 0; index < fileTree.Count; index++) { var currentEntity = governments[index]; var currentGovernment = new GovernmentEntity { Index = fileTree[index].Key - (int)MagicUnifiedNumbers.Placeholder }; foreach (var govProp in (List <KeyValuePair <int, object> >)fileTree[index].Value) { var targetStr = values[(int)govProp.Value]; switch (govProp.Key / 10000) { case 0: switch ((LoadVariables)govProp.Key) { case LoadVariables.AppointRulingParty: currentGovernment.AppointRulingParty = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.Election: currentGovernment.Election = LoadMethods.YesNoConverter(targetStr); break; case LoadVariables.Duration: // Election cycle if (!int.TryParse(targetStr, out var duration)) { throw new Exception("Unknown government duration. " + targetStr); } currentGovernment.Duration = duration; break; case LoadVariables.FlagType: if (!Enum.TryParse(targetStr, true, out GovernmentEntity.Flag flagType)) { throw new Exception("Unknown government flag type. " + targetStr); } currentGovernment.FlagType = flagType; break; default: throw new Exception("Invalid government file structure. " + (LoadVariables)govProp.Key); } break; case (int)MagicUnifiedNumbers.Ideology / 10000: if (LoadMethods.YesNoConverter(targetStr)) { governIdeologies.Add(govProp.Key - (int)MagicUnifiedNumbers.Ideology); } break; default: throw new Exception("Invalid magic number detected in governments."); } } em.AddBuffer <DataInt>(currentEntity).AddRange(governIdeologies); governIdeologies.Clear(); em.SetComponentData(currentEntity, currentGovernment); } } var governmentCollectorEntity = FileUnpacker.GetCollector <GovernmentCollection>(governments); governments.Dispose(); return(governmentCollectorEntity, governmentNames); int GovernmentMagicOverride(int parent, string raw) { if (parent != -1) { return((int)MagicUnifiedNumbers.ContinueMagicNumbers); } governments.Add(em.CreateEntity(typeof(GovernmentEntity))); governmentNames.Add(raw); // Government is used as a color override. return((int)MagicUnifiedNumbers.Placeholder + governmentNames.Count - 1); } }
public static (Entity, List <string>) Main() { // Output Arrays var unitNames = new List <string>(); var units = new NativeList <EntityWrapper>(Allocator.Temp); var em = World.Active.EntityManager; using (var generalList = new NativeList <DataValue>(Allocator.Temp)) using (var goodsList = new NativeList <DataGood>(Allocator.Temp)) { foreach (var unitPaths in Directory.EnumerateFiles(Path.Combine(Application.streamingAssetsPath, "Units"))) { // Resetting fileTree and values var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); FileUnpacker.ParseFile(unitPaths, fileTree, values, UnitsMagicOverride); foreach (var unit in fileTree) { var currentEntity = units[unit.Key - (int)MagicUnifiedNumbers.Unit]; var currentUnit = new UnitEntity { Index = unit.Key - (int)MagicUnifiedNumbers.Unit }; foreach (var quality in (List <KeyValuePair <int, object> >)unit.Value) { // TODO: Possible conversion to UnifiedVariables? switch ((LoadVariables)quality.Key) { case LoadVariables.UnitType: case LoadVariables.Type: if (!Enum.TryParse(values[(int)quality.Value].Replace("_", ""), true, out UnitTypes unitType)) { throw new Exception("Unknown unit type: " + values[(int)quality.Value]); } generalList.Add(new DataValue(quality.Key, (int)unitType)); break; case LoadVariables.Capital: generalList.Add(new DataValue(quality.Key, LoadMethods.YesNoConverter(values[(int)quality.Value]) ? 1f : 0f)); break; case LoadVariables.Priority: case LoadVariables.MaxStrength: case LoadVariables.DefaultOrganisation: case LoadVariables.WeightedValue: case LoadVariables.MaximumSpeed: case LoadVariables.ColonialPoints: if (!float.TryParse(values[(int)quality.Value], out var coreFloat)) { throw new Exception("Unknown core float: " + values[(int)quality.Value]); } generalList.Add(new DataValue(quality.Key, coreFloat)); break; case LoadVariables.LimitPerPort: case LoadVariables.MinPortLevel: case LoadVariables.BuildTime: if (!float.TryParse(values[(int)quality.Value], out var buildFloat)) { throw new Exception("Unknown build float: " + values[(int)quality.Value]); } generalList.Add(new DataValue(quality.Key, buildFloat)); break; case LoadVariables.CanBuildOverseas: generalList.Add(new DataValue(quality.Key, LoadMethods.YesNoConverter(values[(int)quality.Value]) ? 1f : 0f)); break; case LoadVariables.PrimaryCulture: generalList.Add(new DataValue(quality.Key, LoadMethods.YesNoConverter(values[(int)quality.Value]) ? 1f : 0f)); generalList.Add(new DataValue(quality.Key, LoadMethods.YesNoConverter(values[(int)quality.Value]) ? 1f : 0f)); break; case LoadVariables.SupplyConsumptionScore: case LoadVariables.SupplyConsumption: if (!float.TryParse(values[(int)quality.Value], out var supplyFloat)) { throw new Exception("Unknown supply float: " + values[(int)quality.Value]); } generalList.Add(new DataValue(quality.Key, supplyFloat)); break; case LoadVariables.Reconnaissance: case LoadVariables.Attack: case LoadVariables.Defence: case LoadVariables.Discipline: case LoadVariables.Support: case LoadVariables.Maneuver: case LoadVariables.Siege: case LoadVariables.Hull: case LoadVariables.GunPower: case LoadVariables.FireRange: case LoadVariables.Evasion: case LoadVariables.TorpedoAttack: if (!float.TryParse(values[(int)quality.Value], out var abilityFloat)) { throw new Exception("Unknown ability float: " + values[(int)quality.Value]); } generalList.Add(new DataValue(quality.Key, abilityFloat)); break; case LoadVariables.BuildCost: if (!(quality.Value is List <KeyValuePair <int, object> > buildCostActual)) { throw new Exception("Unknown build cost."); } foreach (var goodKvp in buildCostActual) { if (!float.TryParse(values[(int)goodKvp.Value], out var buildCostFloat)) { throw new Exception( "Unknown buildCost float: " + values[(int)goodKvp.Value]); } goodsList.Add(new DataGood(goodKvp.Key, buildCostFloat)); } currentUnit.BuildCost = goodsList.Length; break; case LoadVariables.SupplyCost: if (!(quality.Value is List <KeyValuePair <int, object> > supplyCostActual)) { throw new Exception("Unknown supply cost."); } foreach (var goodKvp in supplyCostActual) { if (!float.TryParse(values[(int)goodKvp.Value], out var supplyCostFloat)) { throw new Exception( "Unknown supplyCost float: " + values[(int)goodKvp.Value]); } goodsList.Add(new DataGood(goodKvp.Key, supplyCostFloat)); } currentUnit.SupplyCost = goodsList.Length; break; } } em.AddBuffer <DataValue>(currentEntity).AddRange(generalList); generalList.Clear(); em.AddBuffer <DataGood>(currentEntity).AddRange(goodsList); goodsList.Clear(); em.SetComponentData(currentEntity, currentUnit); } int UnitsMagicOverride(int parent, string str) { if (parent != -1) { return((int)MagicUnifiedNumbers.ContinueMagicNumbers); } units.Add(em.CreateEntity(typeof(UnitEntity))); unitNames.Add(str); return((int)MagicUnifiedNumbers.Unit + unitNames.Count - 1); } } } var unitCollectorEntity = FileUnpacker.GetCollector <UnitCollection>(units); units.Dispose(); return(unitCollectorEntity, unitNames); }
public static (Entity, List <string>) Main() { var fileTree = new List <KeyValuePair <int, object> >(); var values = new List <string>(); var buildings = new NativeList <EntityWrapper>(Allocator.Temp); var buildingNames = new List <string>(); var em = World.Active.EntityManager; FileUnpacker.ParseFile(Path.Combine(Application.streamingAssetsPath, "Common", "buildings.txt"), fileTree, values, BuildingMagicOverride); foreach (var buildingKvp in fileTree) { var currentEntity = buildings[buildingKvp.Key - (int)MagicUnifiedNumbers.Building].Entity; // ReSharper disable once ConvertToUsingDeclaration using (var buildingActions = new NativeList <DataValue>(Allocator.Temp)) { foreach (var attribute in (List <KeyValuePair <int, object> >)buildingKvp.Value) { var targetStr = attribute.Key < (int)LoadVariables.BreakCore ? values[(int)attribute.Value] : ""; switch ((LoadVariables)attribute.Key) { case LoadVariables.MaxLevel: case LoadVariables.Time: case LoadVariables.FortLevel: case LoadVariables.ColonialRange: case LoadVariables.NavalCapacity: case LoadVariables.LocalShipBuild: case LoadVariables.Cost: case LoadVariables.Infrastructure: case LoadVariables.MovementCost: case LoadVariables.ColonialMultiplier: case LoadVariables.ColonialBase: if (!float.TryParse(targetStr, out var floatValue)) { throw new Exception("Unknown float: " + targetStr); } buildingActions.Add(new DataValue(attribute.Key, floatValue)); break; case LoadVariables.DefaultEnabled: case LoadVariables.Province: case LoadVariables.PopBuildFactory: case LoadVariables.OnePerState: buildingActions.Add( new DataValue(attribute.Key, LoadMethods.YesNoConverter(targetStr) ? 1 : 0)); break; case LoadVariables.GoodsCost: using (var buildingGoods = new NativeList <DataGood>(Allocator.Temp)) { foreach (var goodKvp in (List <KeyValuePair <int, object> >)attribute.Value) { if (goodKvp.Key / 10000 != (int)MagicUnifiedNumbers.Goods / 10000) { throw new Exception("Unknown good located in Goods Cost " + buildingNames[ buildingKvp.Key - (int)MagicUnifiedNumbers.Building]); } if (!float.TryParse(values[(int)goodKvp.Value], out var goodValue)) { throw new Exception("Unknown goods float: " + values[(int)goodKvp.Value]); } buildingGoods.Add( new DataGood(goodKvp.Key - (int)MagicUnifiedNumbers.Goods, goodValue)); } em.AddBuffer <DataGood>(currentEntity).AddRange(buildingGoods); } break; } } em.AddBuffer <DataValue>(currentEntity).AddRange(buildingActions); } } var buildingCollectorEntity = FileUnpacker.GetCollector <BuildingCollection>(buildings); buildings.Dispose(); return(buildingCollectorEntity, buildingNames); int BuildingMagicOverride(int parent, string str) { if (parent != -1) { return((int)MagicUnifiedNumbers.ContinueMagicNumbers); } var targetBuilding = em.CreateEntity(typeof(BuildingEntity)); em.SetComponentData(targetBuilding, new BuildingEntity { Index = buildingNames.Count }); buildings.Add(targetBuilding); buildingNames.Add(str); return((int)MagicUnifiedNumbers.Building + buildingNames.Count - 1); } }