Exemplo n.º 1
0
        private void ensureModuleInfoCache()
        {
            if (_moduleInfoCache == null)
            {
                lock (this)
                    if (_moduleInfoCache == null)
                    {
                        const int cols = 20; // number of icons per row
                        const int w    = 32; // width of an icon in pixels
                        const int h    = 32; // height of an icon in pixels

                        var iconFiles = new DirectoryInfo(_config.ModIconDir).EnumerateFiles("*.png", SearchOption.TopDirectoryOnly).OrderBy(file => file.Name != "blank.png").ToArray();
                        var rows      = (iconFiles.Length + cols - 1) / cols;
                        var coords    = new Dictionary <string, (int x, int y)>();

                        using (var bmp = new Bitmap(w * cols, h * rows))
                        {
                            using (var g = Graphics.FromImage(bmp))
                            {
                                for (int i = 0; i < iconFiles.Length; i++)
                                {
                                    using (var icon = new Bitmap(iconFiles[i].FullName))
                                        g.DrawImage(icon, w * (i % cols), h * (i / cols));
                                    coords.Add(Path.GetFileNameWithoutExtension(iconFiles[i].Name), (i % cols, i / cols));
                                }
                            }
                            using (var mem = new MemoryStream())
                            {
                                bmp.Save(mem, ImageFormat.Png);
                                _moduleInfoCache = new ModuleInfoCache {
                                    IconSpritePng = mem.ToArray()
                                };
                                _moduleInfoCache.IconSpriteMd5 = MD5.Create().ComputeHash(_moduleInfoCache.IconSpritePng).ToHex();

                                var modules = new DirectoryInfo(_config.ModJsonDir)
                                              .EnumerateFiles("*.json", SearchOption.TopDirectoryOnly)
                                              .ParallelSelect(4, file =>
                                {
                                    try
                                    {
                                        var origFile = File.ReadAllText(file.FullName);
                                        var modJson  = JsonDict.Parse(origFile);
                                        var mod      = ClassifyJson.Deserialize <KtaneModuleInfo>(modJson);

#if DEBUG
                                        var newJson    = (JsonDict)ClassifyJson.Serialize(mod);
                                        var newJsonStr = newJson.ToStringIndented();
                                        if (newJsonStr != origFile)
                                        {
                                            File.WriteAllText(file.FullName, newJsonStr);
                                        }
                                        modJson = newJson;
#endif

                                        return((modJson, mod, file.LastWriteTimeUtc).Nullable());
                                    }
Exemplo n.º 2
0
        private ModuleInfoCache getModuleInfoCache()
        {
            ModuleInfoCache mic = null;

            do
            {
                if (_moduleInfoCache == null)
                {
                    lock (this)
                        if (_moduleInfoCache == null)
                        {
                            const int cols = 20; // number of icons per row
                            const int w    = 32; // width of an icon in pixels
                            const int h    = 32; // height of an icon in pixels

                            var iconFiles = new DirectoryInfo(Path.Combine(_config.BaseDir, "Icons")).EnumerateFiles("*.png", SearchOption.TopDirectoryOnly).OrderBy(file => file.Name != "blank.png").ToArray();
                            var rows      = (iconFiles.Length + cols - 1) / cols;
                            var coords    = new Dictionary <string, (int x, int y)>();

                            using var bmp = new Bitmap(w * cols, h * rows);
                            using (var g = Graphics.FromImage(bmp))
                            {
                                for (int i = 0; i < iconFiles.Length; i++)
                                {
                                    using (var icon = new Bitmap(iconFiles[i].FullName))
                                        g.DrawImage(icon, w * (i % cols), h * (i / cols));
                                    coords.Add(Path.GetFileNameWithoutExtension(iconFiles[i].Name), (i % cols, i / cols));
                                }
                            }
                            using var mem = new MemoryStream();
                            bmp.Save(mem, ImageFormat.Png);

                            // This needs to be a separate variable (don’t use _moduleInfoCache itself) because that field needs to stay null until it is fully initialized
                            var moduleInfoCache = new ModuleInfoCache {
                                IconSpritePng = mem.ToArray()
                            };
                            moduleInfoCache.IconSpriteMd5 = MD5.Create().ComputeHash(moduleInfoCache.IconSpritePng).ToHex();

                            // Load TP data from the spreadsheet
                            JsonList entries;
                            try
                            {
                                entries = new HClient().Get("https://spreadsheets.google.com/feeds/list/1G6hZW0RibjW7n72AkXZgDTHZ-LKj0usRkbAwxSPhcqA/1/public/values?alt=json").DataJson["feed"]["entry"].GetList();
                            }
                            catch (Exception e)
                            {
                                Log.Exception(e);
                                entries = new JsonList();
                            }

                            var moduleLoadExceptions = new JsonList();
                            var modules = new DirectoryInfo(Path.Combine(_config.BaseDir, "JSON"))
                                          .EnumerateFiles("*.json", SearchOption.TopDirectoryOnly)
                                          .ParallelSelect(Environment.ProcessorCount, file =>
                            {
                                try
                                {
                                    var origFile = File.ReadAllText(file.FullName);
                                    var modJson  = JsonDict.Parse(origFile);
                                    var mod      = ClassifyJson.Deserialize <KtaneModuleInfo>(modJson);

#if DEBUG
                                    var newJson    = (JsonDict)ClassifyJson.Serialize(mod);
                                    var newJsonStr = newJson.ToStringIndented();
                                    if (newJsonStr != origFile)
                                    {
                                        File.WriteAllText(file.FullName, newJsonStr);
                                    }
                                    modJson = newJson;
Exemplo n.º 3
0
        private void ensureModuleInfoCache()
        {
            if (_moduleInfoCache == null)
            {
                lock (this)
                    if (_moduleInfoCache == null)
                    {
                        const int cols = 20; // number of icons per row
                        const int w    = 32; // width of an icon in pixels
                        const int h    = 32; // height of an icon in pixels

                        var iconFiles = new DirectoryInfo(_config.ModIconDir).EnumerateFiles("*.png", SearchOption.TopDirectoryOnly).OrderBy(file => file.Name != "blank.png").ToArray();
                        var rows      = (iconFiles.Length + cols - 1) / cols;
                        var coords    = new Dictionary <string, (int x, int y)>();

                        using var bmp = new Bitmap(w * cols, h * rows);
                        using (var g = Graphics.FromImage(bmp))
                        {
                            for (int i = 0; i < iconFiles.Length; i++)
                            {
                                using (var icon = new Bitmap(iconFiles[i].FullName))
                                    g.DrawImage(icon, w * (i % cols), h * (i / cols));
                                coords.Add(Path.GetFileNameWithoutExtension(iconFiles[i].Name), (i % cols, i / cols));
                            }
                        }
                        using var mem = new MemoryStream();
                        bmp.Save(mem, ImageFormat.Png);
                        _moduleInfoCache = new ModuleInfoCache {
                            IconSpritePng = mem.ToArray()
                        };
                        _moduleInfoCache.IconSpriteMd5 = MD5.Create().ComputeHash(_moduleInfoCache.IconSpritePng).ToHex();

                        // Load TP data from the spreadsheet
                        JsonList entries;
                        try
                        {
                            entries = new HClient().Get("https://spreadsheets.google.com/feeds/list/1WEzVOKxOO5CDGoqAHjJKrC-c-ZGgsTPRLXBCs8RrAwU/1/public/values?alt=json").DataJson["feed"]["entry"].GetList();
                        }
                        catch (Exception e)
                        {
                            _logger.Exception(e);
                            entries = new JsonList();
                        }

                        var modules = new DirectoryInfo(_config.ModJsonDir)
                                      .EnumerateFiles("*.json", SearchOption.TopDirectoryOnly)
                                      .ParallelSelect(4, file =>
                        {
                            try
                            {
                                var origFile = File.ReadAllText(file.FullName);
                                var modJson  = JsonDict.Parse(origFile);
                                var mod      = ClassifyJson.Deserialize <KtaneModuleInfo>(modJson);

#if DEBUG
                                var newJson    = (JsonDict)ClassifyJson.Serialize(mod);
                                var newJsonStr = newJson.ToStringIndented();
                                if (newJsonStr != origFile)
                                {
                                    File.WriteAllText(file.FullName, newJsonStr);
                                }
                                modJson = newJson;