// Write the field here
        public override void Serialize(CppStreamWriter writer, IField field, bool asHeader)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }
            // If we could not resolve the type name, don't serialize the field (this should cause a critical failure in the type)
            if (ResolvedTypeNames[field] == null)
            {
                throw new UnresolvedTypeException(field.DeclaringType, field.Type);
            }

            var fieldString = "";

            foreach (var spec in field.Specifiers)
            {
                fieldString += $"{spec} ";
            }
            writer.WriteComment(fieldString + field.Type + " " + field.Name);

            writer.WriteComment($"Offset: 0x{field.Offset:X}");
            if (!field.Specifiers.IsStatic() && !field.Specifiers.IsConst())
            {
                writer.WriteFieldDeclaration(ResolvedTypeNames[field] !, SafeFieldNames[field]);
            }
            writer.Flush();
            Serialized(field);
        }
        // Outputs a DEFINE_IL2CPP_ARG_TYPE call for all root or non-generic types defined by this file
        private void DefineIl2CppArgTypes(CppStreamWriter writer, CppTypeContext context)
        {
            var type = context.LocalType;

            // DEFINE_IL2CPP_ARG_TYPE
            var(ns, il2cppName) = type.This.GetIl2CppName();
            // For Name and Namespace here, we DO want all the `, /, etc
            if (!type.This.IsGeneric)
            {
                IncludeIl2CppTypeCheckIfNotAlready(writer);
                string fullName = context.GetCppName(context.LocalType.This, true, true, CppTypeContext.NeedAs.Definition, CppTypeContext.ForceAsType.Literal)
                                  ?? throw new UnresolvedTypeException(context.LocalType.This, context.LocalType.This);
                if (context.LocalType.Info.Refness == Refness.ReferenceType)
                {
                    fullName += "*";
                }
                writer.WriteLine($"DEFINE_IL2CPP_ARG_TYPE({fullName}, \"{ns}\", \"{il2cppName}\");");
            }
            else if (type.This.DeclaringType is null || !type.This.DeclaringType.IsGeneric)
            {
                IncludeIl2CppTypeCheckIfNotAlready(writer);
                string templateName = context.GetCppName(context.LocalType.This, true, false, CppTypeContext.NeedAs.Declaration, CppTypeContext.ForceAsType.Literal)
                                      ?? throw new UnresolvedTypeException(context.LocalType.This, context.LocalType.This);
                var structStr = context.LocalType.Info.Refness == Refness.ReferenceType ? "CLASS" : "STRUCT";
                writer.WriteLine($"DEFINE_IL2CPP_ARG_TYPE_GENERIC_{structStr}({templateName}, \"{ns}\", \"{il2cppName}\");");
            }

            foreach (var nested in context.NestedContexts.Where(n => n.InPlace))
            {
                DefineIl2CppArgTypes(writer, nested);
            }
        }
 internal void Write(string filename)
 {
     _stream = new StreamWriter(new MemoryStream());
     _stream.WriteLine(ApplicationMk);
     _stream.Flush();
     CppStreamWriter.WriteIfDifferent(filename, _stream.BaseStream);
 }
        internal void Serialize(CppTypeContext context)
        {
            var data           = context.LocalType;
            var headerLocation = Path.Combine(_config.OutputDirectory, _config.OutputHeaderDirectory, context.HeaderFileName);

            Directory.CreateDirectory(Path.GetDirectoryName(headerLocation));
            using var ms        = new MemoryStream();
            using var rawWriter = new StreamWriter(ms);
            using var writer    = new CppStreamWriter(rawWriter, "  ");
            // Write header
            writer.WriteComment($"Autogenerated from {nameof(CppHeaderCreator)}");
            writer.WriteComment("Created by Sc2ad");
            writer.WriteComment("=========================================================================");
            writer.WriteLine("#pragma once");
            // TODO: determine when/if we need this
            // For sizes that are valid, we ALSO want to write with pack of 1
            // Invalid sizes are ignored.

            // Write SerializerContext and actual type
            try
            {
                _serializer.Serialize(writer, context, true);
            }
            catch (UnresolvedTypeException e)
            {
                if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.DisplayInFile)
                {
                    writer.WriteComment("Unresolved type exception!");
                    writer.WriteLine("/*");
                    writer.WriteLine(e);
                    writer.WriteLine("*/");
                }
                else if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.SkipIssue)
                {
                    return;
                }
                else if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.Elevate)
                {
                    throw new InvalidOperationException($"Cannot elevate {e} to a parent type- there is no parent type!");
                }
            }
            // End the namespace
            writer.CloseDefinition();
            hasIl2CppTypeCheckInclude = context.NeedIl2CppUtilsFunctionsInHeader;

            if (data.This.Namespace == "System" && data.This.Name == "ValueType")
            {
                IncludeIl2CppTypeCheckIfNotAlready(writer);
                writer.WriteLine("template<class T>");
                writer.WriteLine("struct is_value_type<T, typename std::enable_if_t<std::is_base_of_v<System::ValueType, T>>> : std::true_type{};");
            }

            DefineIl2CppArgTypes(writer, context);
            writer.WriteLine("#include \"extern/beatsaber-hook/shared/utils/il2cpp-utils-methods.hpp\"");
            _serializer.WritePostSerializeMethods(writer, context, true);
            writer.Flush();

            writer.WriteIfDifferent(headerLocation, context);
        }
 private void IncludeIl2CppTypeCheckIfNotAlready(CppStreamWriter writer)
 {
     if (hasIl2CppTypeCheckInclude)
     {
         return;
     }
     writer.WriteInclude("extern/beatsaber-hook/shared/utils/il2cpp-type-check.hpp");
     hasIl2CppTypeCheckInclude = true;
 }
Beispiel #6
0
 public void Close()
 {
     if (_filePath != null && _stream != null)
     {
         _stream.Flush();
         CppStreamWriter.WriteIfDifferent(_filePath, _stream.BaseStream);
     }
     else
     {
         Console.Error.WriteLine("Closing AndroidMkSerializer without writing anything!");
     }
     _stream?.Dispose();
 }
Beispiel #7
0
        internal void Serialize(CppTypeContext context)
        {
            var             sourceLocation = _config.OneSourceFile ? Path.Combine(_config.OutputDirectory, _config.OutputSourceDirectory, context.CppFileName) : overallSourceLocation;
            CppStreamWriter?writer         = overallStreamWriter;

            if (writer is null)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(sourceLocation));
                using var ms        = new MemoryStream();
                using var rawWriter = new StreamWriter(ms);
                writer = new CppStreamWriter(rawWriter, "  ");
            }
            // Write header
            writer.WriteComment($"Autogenerated from {nameof(CppSourceCreator)}");
            writer.WriteComment($"Created by Sc2ad");
            writer.WriteComment("=========================================================================");
            try
            {
                // Write SerializerContext and actual type
                _serializer.Serialize(writer, context, false);
            }
            catch (UnresolvedTypeException e)
            {
                if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.DisplayInFile)
                {
                    writer.WriteLine("// Unresolved type exception!");
                    writer.WriteLine("/*");
                    writer.WriteLine(e);
                    writer.WriteLine("*/");
                }
                else if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.SkipIssue)
                {
                    return;
                }
                else if (_config.UnresolvedTypeExceptionHandling?.TypeHandling == UnresolvedTypeExceptionHandling.Elevate)
                {
                    throw new InvalidOperationException($"Cannot elevate {e} to a parent type- there is no parent type!");
                }
            }
            writer.Flush();

            if (overallStreamWriter is null)
            {
                writer.WriteIfDifferent(sourceLocation, context);
                writer.Close();
                writer.Dispose();
            }
        }
Beispiel #8
0
 public abstract void Serialize(CppStreamWriter writer, T obj, bool asHeader);