Example #1
0
        private string ProcessUserDataStructure()
        {
            if (CheckExistingMethod(n => n == _type.Name) is { } existing)
            {
                return(existing);
            }

            var method = _builder.AddMethod($"Write{_type.Name}", Accessibility.Private)
                         .AddParameter("Stream", "stream")
                         .AddParameter(_type, "data")
                         .MakeStaticMethod();

            var steps = new List <string>();

            foreach (var property in _type.GetMembers().Where(m => m is IPropertySymbol).Cast <IPropertySymbol>())
            {
                var type      = property.Type as INamedTypeSymbol;
                var processor = new TypeProcessor(_builder, type, $"data.{property.Name}");
                steps.Add(processor.Process());
            }

            method.WithBody(w =>
            {
                foreach (string step in steps)
                {
                    w.AppendLine(step);
                }
            });

            return($"{method.Name}(stream, {_access});");
        }
Example #2
0
        private string ProcessList(INamedTypeSymbol type)
        {
            if (CheckExistingMethod(n => n == $"IList<{_type.Name}>") is { } existing)
            {
                return(existing);
            }

            var method = _builder.AddMethod($"Write{type.Name}List", Accessibility.Private)
                         .AddParameter("Stream", "stream")
                         .AddParameter($"IList<{type.Name}>", "list")
                         .MakeStaticMethod();

            var    processor = new TypeProcessor(_builder, type, "element");
            string step      = processor.Process();

            method.WithBody(w =>
            {
                w.AppendLine("stream.WriteU2((ushort) list.Count);");

                using (w.Block("foreach (var element in list)"))
                {
                    w.AppendLine(step);
                }
            });

            return($"{method.Name}(stream, {_access});");
        }
Example #3
0
        private static string ProcessClass(INamedTypeSymbol attribute)
        {
            string attributeName = attribute.Name;
            string builderName   = attributeName + "Builder";

            var builder = CreateBuilder(builderName, attributeName);
            var steps   = new List <string>();

            foreach (var property in attribute.GetMembers().OfType <IPropertySymbol>())
            {
                if (property.Type is not INamedTypeSymbol type)
                {
                    steps.Add($"throw new NotSupportedException(\"Unsupported type {property.Type.Name}\");");
                    continue;
                }

                var processor = new TypeProcessor(builder.Class, type, $"attribute.{property.Name}");
                steps.Add(processor.Process());
            }

            builder.WithBody(w =>
            {
                foreach (string step in steps)
                {
                    w.AppendLine(step);
                }
            });

            return(builder.Class.Build());
        }
Example #4
0
        public string Process()
        {
            if (_type.EnumUnderlyingType is { } underlyingType)
            {
                var processor = new TypeProcessor(_builder, underlyingType, $"({underlyingType.Name}) {_access}");
                return(processor.Process());
            }
            if (_type.SpecialType != SpecialType.None)
            {
                return(ProcessSpecialType(_type));
            }
            if (_type.IsGenericType && _type.Name == "IList")
            {
                return(ProcessList(_type.TypeArguments[0] as INamedTypeSymbol));
            }

            return(ProcessUserDataStructure());
        }