Esempio n. 1
0
 public virtual void VerifyAssets(SheetConvertingContext context)
 {
     using (context.Logger.BeginScope(Index))
     {
         SheetUtility.VerifyAssets(context, this);
     }
 }
Esempio n. 2
0
 public virtual void PostLoad(SheetConvertingContext context)
 {
     using (context.Logger.BeginScope(Index))
     {
         SheetUtility.MapReferences(context, this);
     }
 }
Esempio n. 3
0
        public virtual void PostLoad()
        {
            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
            };

            foreach (var prop in GetSheetProperties())
            {
                var sheet = prop.GetValue(this) as ISheet;
                if (sheet == null)
                {
                    continue;
                }

                sheet.Name = prop.Name;
            }

            foreach (var prop in GetSheetProperties())
            {
                var sheet = prop.GetValue(this) as ISheet;
                if (sheet == null)
                {
                    continue;
                }

                sheet.PostLoad(context);
            }
        }
 protected virtual void PostLoad(SheetConvertingContext context)
 {
     foreach (var sheet in AllSheets)
     {
         sheet.PostLoad(context);
     }
 }
Esempio n. 5
0
        public async Task <bool> Import(SheetConvertingContext context)
        {
            var sheetProps = context.Container.GetSheetProperties();

            foreach (var prop in sheetProps)
            {
                var path = Path.Combine(_loadPath, $"{prop.Name}.{Extension}");

                if (!_fileSystem.Exists(path))
                {
                    context.Logger.LogError("Failed to find sheet: {SheetName}", prop.Name);
                    continue;
                }

                string data;

                using (var stream = _fileSystem.OpenRead(path))
                    using (var reader = new StreamReader(stream))
                        data = await reader.ReadToEndAsync();

                var sheet = Deserialize(data, prop.PropertyType, context.Logger) as ISheet;
                prop.SetValue(context.Container, sheet);
            }

            return(true);
        }
Esempio n. 6
0
        public static void ConvertFromRaw(SheetConvertingContext context, object obj, Dictionary <string, string> row)
        {
            var type         = obj.GetType();
            var bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty;

            var parentTag = context.Tag;

            foreach (var item in row)
            {
                var prop = type.GetProperty(item.Key, bindingFlags);
                if (prop == null)
                {
                    continue;
                }

                context.SetTag(parentTag, item.Key);

                try
                {
                    object value = ConvertValue(context, prop.PropertyType, item.Value);
                    prop.SetValue(obj, value);
                }
                catch (Exception ex)
                {
                    context.Logger.LogError($"[{context.Tag}] Failed to convert value \"{item.Value}\" of type {prop.PropertyType}\n{ex}");
                }
            }
        }
Esempio n. 7
0
        public static void VerifyAssets(SheetConvertingContext context, object obj)
        {
            var assetProps = obj.GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => p.GetCustomAttribute(typeof(SheetAssetAttribute)) != null);

            foreach (var prop in assetProps)
            {
                using (context.Logger.BeginScope(prop.Name))
                {
                    foreach (var verifier in context.Verifiers)
                    {
                        if (!verifier.TargetType.IsAssignableFrom(prop.PropertyType))
                        {
                            continue;
                        }

                        foreach (var att in prop.GetCustomAttributes(verifier.TargetAttribute))
                        {
                            var err = verifier.Verify(att, prop.GetValue(obj));
                            if (err != null)
                            {
                                context.Logger.LogError("Verification: {Error}", err);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 8
0
 public virtual void VerifyAssets(SheetConvertingContext context)
 {
     using (context.Logger.BeginScope(Name))
     {
         foreach (var row in Items)
         {
             row.VerifyAssets(context);
         }
     }
 }
Esempio n. 9
0
 public virtual void PostLoad(SheetConvertingContext context)
 {
     using (context.Logger.BeginScope(Name))
     {
         foreach (var row in Items)
         {
             row.PostLoad(context);
         }
     }
 }
Esempio n. 10
0
        public async Task <bool> Store(ISheetExporter exporter)
        {
            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
            };

            var success = await exporter.Export(context);

            return(success);
        }
Esempio n. 11
0
        public override void VerifyAssets(SheetConvertingContext context)
        {
            base.VerifyAssets(context);

            using (context.Logger.BeginScope(Id))
            {
                for (int idx = 0; idx < Arr.Count; ++idx)
                {
                    Arr[idx].VerifyAssets(context);
                }
            }
        }
Esempio n. 12
0
        public override void VerifyAssets(SheetConvertingContext context)
        {
            base.VerifyAssets(context);

            var parentTag = context.Tag;

            for (int idx = 0; idx < Arr.Count; ++idx)
            {
                context.SetTag(parentTag, Id);
                Arr[idx].VerifyAssets(context);
            }
        }
Esempio n. 13
0
        public override void PostLoad(SheetConvertingContext context)
        {
            base.PostLoad(context);

            using (context.Logger.BeginScope(Id))
            {
                for (int idx = 0; idx < Arr.Count; ++idx)
                {
                    Arr[idx].Index = idx;
                    Arr[idx].PostLoad(context);
                }
            }
        }
        public virtual void Verify(params SheetVerifier[] verifiers)
        {
            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
                Verifiers = verifiers
            };

            foreach (var sheet in AllSheets)
            {
                sheet.VerifyAssets(context);
            }
        }
Esempio n. 15
0
        public override void PostLoad(SheetConvertingContext context)
        {
            base.PostLoad(context);

            var parentTag = context.Tag;

            for (int idx = 0; idx < Arr.Count; ++idx)
            {
                context.SetTag(parentTag, Id);

                Arr[idx].Index = idx;
                Arr[idx].PostLoad(context);
            }
        }
Esempio n. 16
0
            void ISheetReference.Map(SheetConvertingContext context)
            {
                var sheet = context.Container.AllSheets
                            .FirstOrDefault(x => x.GetType().IsSubclassOf(typeof(Sheet <TKey, TValue>)));

                if (sheet != null)
                {
                    Ref = (sheet as Sheet <TKey, TValue>)[Id];
                }

                if (Id != null && Ref == null)
                {
                    context.Logger.LogError($"[{context.Tag}] Failed to find reference \"{Id}\" on {sheet.Name}");
                }
            }
Esempio n. 17
0
        public static void MapReferences(SheetConvertingContext context, object obj)
        {
            var refProps = obj.GetType()
                           .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                           .Where(p => typeof(ISheetReference).IsAssignableFrom(p.PropertyType));

            foreach (var prop in refProps)
            {
                using (context.Logger.BeginScope(prop.Name))
                {
                    var refer = prop.GetValue(obj) as ISheetReference;
                    refer.Map(context);
                    prop.SetValue(obj, refer);
                }
            }
        }
Esempio n. 18
0
            void ISheetReference.Map(SheetConvertingContext context)
            {
                var sheet = context.Container.GetSheetProperties()
                            .Where(p => p.PropertyType.IsSubclassOf(typeof(Sheet <TKey, TValue>)))
                            .Select(p => p.GetValue(context.Container) as Sheet <TKey, TValue>)
                            .FirstOrDefault();

                if (sheet != null)
                {
                    Ref = sheet[Id];
                }

                if (Id != null && Ref == null)
                {
                    context.Logger.LogError("Failed to find reference \"{ReferenceId}\" on {SheetName}", Id, sheet.Name);
                }
            }
Esempio n. 19
0
        public override void ConvertFromRaw(SheetConvertingContext context, RawSheetRow row)
        {
            base.ConvertFromRaw(context, row);

            Arr = new List <TElem>();

            var parentTag = context.Tag;

            foreach (var item in row)
            {
                context.SetTag(parentTag, Id, Arr.Count);

                var elem = new TElem();
                SheetUtility.ConvertFromRaw(context, elem, item);
                Arr.Add(elem);
            }
        }
        public async Task Load(string loadPath, string ext = "json", Func <string, string> processor = null)
        {
            var sheetProps = GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => p.PropertyType.IsSubclassOf(typeof(Sheet)));

            var deserializeMethod = typeof(SheetUtility).GetMethod(
                nameof(SheetUtility.Deserialize),
                BindingFlags.Public | BindingFlags.Static);

            AllSheets.Clear();

            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
            };

            foreach (var prop in sheetProps)
            {
                string data;

                var path = Path.Combine(loadPath, $"{prop.Name}.{ext}");
                using (var file = File.OpenText(path))
                    data = await file.ReadToEndAsync();

                if (processor != null)
                {
                    data = processor(data);
                }

                var genericMethod = deserializeMethod.MakeGenericMethod(prop.PropertyType);
                var sheet         = genericMethod.Invoke(null, new object[] { data, _logger }) as Sheet;
                prop.SetValue(this, sheet);

                if (sheet != null)
                {
                    sheet.Name = prop.Name;
                    AllSheets.Add(sheet);
                }
            }

            PostLoad(context);

            IsLoaded = true;
        }
        public async Task Bake(ISheetImporter importer, TimeZoneInfo timeZoneInfo)
        {
            AllSheets.Clear();

            var context = new SheetConvertingContext
            {
                Container    = this,
                Logger       = _logger,
                TimeZoneInfo = timeZoneInfo
            };

            var sheetProps = GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => p.PropertyType.IsSubclassOf(typeof(Sheet)));

            var success = await importer.Load();

            if (!success)
            {
                _logger.LogError($"Failed to import");
                return;
            }

            foreach (var prop in sheetProps)
            {
                var data = importer.GetData(prop.Name);
                if (data == null)
                {
                    _logger.LogError($"Failed to find sheet: {prop.Name}");
                    continue;
                }

                var rawSheet = new RawSheet(data);
                var sheet    = Activator.CreateInstance(prop.PropertyType) as Sheet;
                sheet.ConvertFromRaw(rawSheet, context);
                prop.SetValue(this, sheet);

                sheet.Name = prop.Name;
                AllSheets.Add(sheet);
            }

            PostLoad(context);

            IsLoaded = true;
        }
Esempio n. 22
0
        public async Task <bool> Export(SheetConvertingContext context)
        {
            var sheetProps = context.Container.GetSheetProperties();

            foreach (var prop in sheetProps)
            {
                var sheet = prop.GetValue(context.Container);
                var data  = Serialize(sheet, prop.PropertyType, context.Logger);

                var path = Path.Combine(_loadPath, $"{prop.Name}.{Extension}");

                using (var stream = _fileSystem.OpenWrite(path))
                    using (var writer = new StreamWriter(stream))
                        await writer.WriteAsync(data);
            }

            return(true);
        }
Esempio n. 23
0
        public async Task <bool> Bake(ISheetImporter importer)
        {
            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
            };

            var success = await importer.Import(context);

            if (!success)
            {
                return(false);
            }

            PostLoad();

            return(true);
        }
Esempio n. 24
0
        public virtual void Verify(params SheetVerifier[] verifiers)
        {
            var context = new SheetConvertingContext
            {
                Container = this,
                Logger    = _logger,
                Verifiers = verifiers
            };

            foreach (var prop in GetSheetProperties())
            {
                var sheet = prop.GetValue(this) as ISheet;
                if (sheet == null)
                {
                    continue;
                }

                sheet.VerifyAssets(context);
            }
        }
Esempio n. 25
0
        public static object ConvertValue(SheetConvertingContext context, Type type, string value)
        {
            if (type.IsEnum)
            {
                return(Enum.Parse(type, value));
            }

            if (typeof(ISheetReference).IsAssignableFrom(type))
            {
                var targetType = type.GetInterfaces()
                                 .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ISheetReference <>))
                                 .Select(x => x.GetGenericArguments()[0])
                                 .First();

                return(Activator.CreateInstance(type, ConvertValue(context, targetType, value)));
            }

            if (typeof(DateTime).IsAssignableFrom(type))
            {
                var local = DateTime.Parse(value);
                return(TimeZoneInfo.ConvertTimeToUtc(local, context.TimeZoneInfo));
            }

            if (typeof(TimeSpan).IsAssignableFrom(type))
            {
                return(TimeSpan.Parse(value));
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                if (string.IsNullOrEmpty(value))
                {
                    return(null);
                }

                var underlyingType = Nullable.GetUnderlyingType(type);
                return(ConvertValue(context, underlyingType, value));
            }

            return(Convert.ChangeType(value, type));
        }
Esempio n. 26
0
 public abstract void VerifyAssets(SheetConvertingContext context);
Esempio n. 27
0
 public abstract void PostLoad(SheetConvertingContext context);
Esempio n. 28
0
 internal abstract void ConvertFromRaw(RawSheet gsheet, SheetConvertingContext context);
Esempio n. 29
0
 public virtual void VerifyAssets(SheetConvertingContext context)
 {
     context.SetTag(context.Tag, Index);
     SheetUtility.VerifyAssets(context, this);
 }
Esempio n. 30
0
 public virtual void PostLoad(SheetConvertingContext context)
 {
     context.SetTag(context.Tag, Index);
     SheetUtility.MapReferences(context, this);
 }