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); }
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(); } }