예제 #1
0
        private static void Definitions(string cacheFolder, string path,
                                        out DefinitionsLoad definitionOutput)
        {
            var filePath = Path.Combine(Application.streamingAssetsPath, path);

            // Second run, checks if ID texture generated used definitions.
            var usedPath = Path.Combine(Application.streamingAssetsPath, cacheFolder, "usedDefinitions.json");

            definitionOutput = new DefinitionsLoad();
            definitionOutput.Generate(filePath, usedPath);

            File.WriteAllText(Path.Combine(Application.streamingAssetsPath, cacheFolder,
                                           "Definition.json"), JsonConvert.SerializeObject(definitionOutput));
        }
예제 #2
0
        protected override void OnStartRunning()
        {
            Debug.Log("CACHE READ START!");

            _blobAssetReferences = new List <IDisposable>();

            var definitions = new DefinitionsLoad();

            definitions.ReadCache(CacheFolder);

            var areas = new AreasLoad();

            areas.ReadCache(CacheFolder);

            var regions = new RegionsLoad();

            regions.ReadCache(CacheFolder);

            var supers = new SuperRegionsLoad();

            supers.ReadCache(CacheFolder);

            var countries = new CountryColorsLoad();

            countries.ReadCache(CacheFolder);

            var provinces = new ProvinceHistoryLoad();

            provinces.ReadCache(CacheFolder);

            GenerateCountryEntities(in countries, out var countryList);
            GenerateProvinceEntities(in provinces, countryList, out var provList);

            CreateProvinceAreaLookups(in areas, provList, out var provinceToArea);
            CreateAreaRegionLookups(in regions, out var areaToRegion);
            CreateRegionSuperLookups(in supers, out var regionToSuper);

            CreateUpwardsSingleton(provinceToArea, areaToRegion, regionToSuper);

            CreateColorBlob(definitions.Colors, areas.AreaColors, regions.RegionColors, supers.Colors);

            Debug.Log("STARTUP ENTITIES CREATED!");
        }
예제 #3
0
        public static Texture2D Process(string provinceMap, string cacheFolder)
        {
            var rawProvinceMap = new Texture2D(1, 1, TextureFormat.RGB24, false, true)
            {
                filterMode = FilterMode.Point
            };

            var rawBytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, provinceMap));

            rawProvinceMap.LoadImage(rawBytes);

            if (rawProvinceMap.height < 3)
            {
                throw new Exception("No province map was inserted into the input property on inspector.");
            }

            var definitions = new DefinitionsLoad();

            definitions.ReadCache(cacheFolder);

            if (definitions.Names.Count >= math.pow(2, 23))
            {
                throw new Exception("Province number overflow. " +
                                    "Why the f**k do you have more than 8,388,608 provinces?");
            }

            // Native hash map expands like list. This wont run in player so it doesn't matter about optimization.
            using var provinceLookup = new NativeHashMap <Color, int>(definitions.Colors.Count, Allocator.TempJob);

            for (var i = 0; i < definitions.Colors.Count; i++)
            {
                provinceLookup.TryAdd(definitions.Colors[i], i);
            }

            var outputTexture = new Texture2D(rawProvinceMap.width, rawProvinceMap.height,
                                              TextureFormat.RGBA32, true, true)
            {
                filterMode = FilterMode.Point
            };

            using var rawData  = new NativeArray <Color32>(rawProvinceMap.GetPixels32(), Allocator.TempJob);
            using var provData = new NativeArray <ushort>(rawData.Length, Allocator.TempJob);
            var output = outputTexture.GetRawTextureData <Color32>();

            using var observedIDs = new NativeHashSet <Color>(provinceLookup.Count(), Allocator.TempJob);

            new EncodeID
            {
                ProvinceLookup = provinceLookup,
                RawData        = rawData,
                Output         = output,
                IndexOutput    = provData,
                ObservedIDs    = observedIDs.AsParallelWriter()
            }.Schedule(rawData.Length, 32).Complete();

            using var observedArray = observedIDs.ToNativeArray(Allocator.TempJob);

            File.WriteAllText(Path.Combine(Application.streamingAssetsPath, cacheFolder, "usedDefinitions.json"),
                              JsonConvert.SerializeObject(observedArray.ToArray().Select(color => (Color32)color)));

            var provArray = provData.ToArray();
            var byteArray = new byte[provArray.Length * sizeof(ushort)];

            Buffer.BlockCopy(provArray, 0, byteArray, 0, byteArray.Length);

            File.WriteAllBytes(Path.Combine(Application.streamingAssetsPath, cacheFolder, "ProvinceIDs.bytes"),
                               byteArray);

            outputTexture.Apply();

            return(outputTexture);
        }