Пример #1
0
        public static (List <string>, Dictionary <string, Entity>, List <string>) Names()
        {
            var em           = World.DefaultGameObjectInjectionWorld.EntityManager;
            var countryTags  = new Dictionary <string, Entity>();
            var countries    = new List <string>();
            var countryPaths = new List <string>();

            // Creating default countries.
            var target = em.CreateEntity(typeof(Country), typeof(OceanCountry));

            em.SetComponentData(target, new Country {
                Color = new Color32(0, 191, 255, 255)
            });
            countryTags.Add("OCEAN", target);
            em.SetName(target, "Country: Ocean"); // DEBUG

            target = em.CreateEntity(typeof(Country), typeof(UncolonizedCountry));
            em.SetComponentData(target, new Country {
                Color = new Color32(255, 228, 181, 255)
            });
            countryTags.Add("UNCOLONIZED", target);
            em.SetName(target, "Country: Uncolonized"); // DEBUG

            // Setting default good prices using prices
            var defaultPrice = new NativeArray <Prices>(LoadChain.GoodNum, Allocator.Temp);

            for (var index = 0; index < defaultPrice.Length; index++)
            {
                defaultPrice[index] = new Prices(1);
            }

            // Setting holder for total goods transacted
            var goodsTraded = new NativeArray <Inventory>(LoadChain.GoodNum, Allocator.Temp);

            foreach (var rawCommonLine in File.ReadLines(Path.Combine(Application.streamingAssetsPath,
                                                                      "Common", "countries.txt")))
            {
                if (CommentDetector(rawCommonLine, out var line))
                {
                    continue;
                }

                var tag = Regex.Match(line, @"^.+?(?=\=)").Value.Trim();
                if (string.IsNullOrEmpty(tag))
                {
                    throw new Exception("No tag found. " + line);
                }

                if (tag.Equals("dynamic_tags"))
                {
                    if (YesNoConverter(Regex.Match(line, @"(?<=\=).+$").Value))
                    {
                        continue;
                    }

                    break;
                }

                var targetFile = Regex.Match(line, "(?<=\").+(?=\")").Value.Trim();
                if (string.IsNullOrEmpty(targetFile))
                {
                    throw new Exception("No country file found. " + line);
                }

                //countryPaths.Add(Path.Combine(Application.streamingAssetsPath, "Common", targetFile));
                //countries.Add(Path.GetFileNameWithoutExtension(countryPaths.Last()));

                var fileTree = new List <(string, object)>();
                ParseFile.Main(Path.Combine(Application.streamingAssetsPath,
                                            "common", targetFile), fileTree);

                var currentCountry = new Country();

                foreach (var(key, value) in fileTree)
                {
                    if (!key.Equals("color"))
                    {
                        continue;
                    }

                    var color = ParseColor32((string)value);
                    currentCountry.Color = color;
                    break;
                }

                target = em.CreateEntity(typeof(Country), typeof(Prices), typeof(Inventory), typeof(StateWrapper));
                em.SetComponentData(target, currentCountry);
                em.GetBuffer <Prices>(target).AddRange(defaultPrice);
                em.GetBuffer <Inventory>(target).AddRange(goodsTraded);
                countryTags.Add(tag, target);
                em.SetName(target, "Country: " + Path.GetFileNameWithoutExtension(targetFile)); // DEBUG
            }

            defaultPrice.Dispose();
            goodsTraded.Dispose();
            return(countries, countryTags, countryPaths);

            bool CommentDetector(string line, out string sliced)
            {
                // Comment Detector. Will also lowercase everything. Throwing away comments.
                sliced = line.ToLowerInvariant().Split(new[] { "#" }, StringSplitOptions.None)[0].Trim();
                return(sliced.Length == 0);
            }

            bool YesNoConverter(string word)
            {
                switch (word.Trim())
                {
                case "yes":
                    return(true);

                case "no":
                    return(false);

                default:
                    throw new Exception("Unknown yes/no. " + word);
                }
            }

            Color32 ParseColor32(string colorString)
            {
                var subColor = Regex.Match(colorString, @"\d(.*)\d").Value
                               .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                if (subColor.Length != 3)
                {
                    throw new Exception("Color invalid. { R G B }: " + colorString);
                }

                if (FloatDetector())
                {
                    byte.TryParse(subColor[0], out var r);
                    byte.TryParse(subColor[1], out var g);
                    byte.TryParse(subColor[2], out var b);
                    return(new Color32(r, g, b, 255));
                }
                else
                {
                    float.TryParse(subColor[0], out var r);
                    float.TryParse(subColor[1], out var g);
                    float.TryParse(subColor[2], out var b);
                    return(new Color(r, g, b, 1));
                }

                bool FloatDetector()
                {
                    var points = colorString.Count(x => x.Equals('.'));

                    switch (points)
                    {
                    case 3:
                    case 2:
                    case 1:
                        return(false);

                    case 0:
                        return(true);

                    default:
                        throw new Exception("Color invalid. { R G B }: " + colorString);
                    }
                }
            }
        }
Пример #2
0
        public static void Main(IReadOnlyDictionary <int, Entity> provEntityLookup, IReadOnlyDictionary <string, Entity> tagLookup,
                                BlobAssetReference <MarketMatrix>[] marketIdentities, int[] maxEmploy, BlobAssetReference <ProvToState> provToState)
        {
            var em = World.DefaultGameObjectInjectionWorld.EntityManager;

            var provinces = Directory.GetFiles(Path.Combine(Application.streamingAssetsPath, "history", "provinces"),
                                               "*.txt", SearchOption.AllDirectories);

            using var cores = new NativeList <Cores>(Allocator.TempJob);
            foreach (var province in provinces)
            {
                cores.Clear();

                var fileTree = new List <(string, object)>();
                ParseFile.Main(province, fileTree);

                var idString = Regex.Match(Path.GetFileNameWithoutExtension(province)
                                           ?? throw new Exception("Invalid province file path, null path."), @"\d+").Value;
                if (!int.TryParse(idString, out var provId))
                {
                    throw new Exception("Invalid province file name. Must include province ID. " + province);
                }

                var provEntity = provEntityLookup[provId];

                // Removing ocean tag
                if (em.HasComponent <OceanProvince>(provEntity))
                {
                    em.RemoveComponent <OceanProvince>(provEntity);
                    em.AddComponents(provEntity, new ComponentTypes(typeof(Population), typeof(Inventory),
                                                                    typeof(ProvinceRgo), typeof(FactoryWrapper), typeof(Cores)));
                }

                var     target = em.GetComponentData <Province>(provEntity);
                ref var state  = ref provToState.Value.Lookup[target.Index];

                foreach (var(key, value) in fileTree)
                {
                    switch (key)
                    {
                    case "owner":
                        target.Owner = tagLookup[(string)value];
                        if (!em.HasComponent <Inhabited>(state))
                        {
                            em.AddComponent(state, typeof(Inhabited));
                        }
                        continue;

                    case "controller":
                        target.Controller = tagLookup[(string)value];
                        continue;

                    case "add_core":
                        cores.Add(tagLookup[(string)value]);
                        continue;

                    case "trade_goods":
                        var rand = Random.Range(0f, 10f);
                        // 1 (50%), 2 (30%), or 3(20%).
                        var tradeGood = (int)math.ceil(math.pow(rand, 3) /
                                                       600f - math.pow(rand, 2) / 200f + 11 * rand / 60);

                        em.SetComponentData(provEntity, new ProvinceRgo(tradeGood, 0));
                        continue;

                    case "life_rating":
                        target.LifeRating = int.Parse((string)value);
                        continue;
                    }
                }

                if (target.Owner == tagLookup["OCEAN"])
                {
                    target.Owner = tagLookup["UNCOLONIZED"];
                    em.AddComponent <UncolonizedProvince>(provEntity);
                }

                em.SetComponentData(provEntity, target);
                em.GetBuffer <Cores>(provEntity).AddRange(cores);
            }