zoon / C-Sharp-Latest-for-Unity

Unity editor utility that automatically sets C# version to latest available
BSD 2-Clause "Simplified" License
13 stars 1 forks source link

Many System.Xml.XmlException: There is no Unicode byte order mark. Cannot switch to Unicode exceptions in Unity 2018.3.b{x} #1

Open tkaczz opened 6 years ago

tkaczz commented 6 years ago

Console window is spammed with these same messages System.Xml.XmlException: There is no Unicode byte order mark. Cannot switch to Unicode. at System.Xml.XmlTextReaderImpl.Throw (System.Exception e) [0x00027] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo (System.String res) [0x00017] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.CheckEncoding (System.String newEncodingName) [0x000ad] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration (System.Boolean isTextDecl) [0x0065c] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.Read () [0x000c6] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.Linq.XDocument.Load (System.Xml.XmlReader reader, System.Xml.Linq.LoadOptions options) [0x00016] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at System.Xml.Linq.XDocument.Load (System.String uri, System.Xml.Linq.LoadOptions options) [0x0000f] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at CSharpLatest.CsProjectPostprocessor.UpdateProjectFile (System.String csproj, System.String csver) [0x00000] in <d168b503304540e1af1dd3fbda8143ac>:0 UnityEngine.Debug:LogError(Object) CSharpLatest.CsProjectPostprocessor:UpdateProjectFile(String, String) CSharpLatest.CsProjectPostprocessor:OnGeneratedCSProjectFiles() CSharpLatest.Bootstrap:Main() UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes()

And this System.Xml.XmlException: There is no Unicode byte order mark. Cannot switch to Unicode. at System.Xml.XmlTextReaderImpl.Throw (System.Exception e) [0x00027] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo (System.String res) [0x00017] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.CheckEncoding (System.String newEncodingName) [0x000ad] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration (System.Boolean isTextDecl) [0x0065c] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.XmlTextReaderImpl.Read () [0x000c6] in <7fd195060d8c41448694ab221d3b56ca>:0 at System.Xml.Linq.XDocument.Load (System.Xml.XmlReader reader, System.Xml.Linq.LoadOptions options) [0x00016] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at System.Xml.Linq.XDocument.Load (System.String uri, System.Xml.Linq.LoadOptions options) [0x0000f] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <428ab70c13974bd0bcd1eeafc00ea3e2>:0 at CSharpLatest.CsProjectPostprocessor.UpdateProjectFile (System.String csproj, System.String csver) [0x00000] in <d168b503304540e1af1dd3fbda8143ac>:0 UnityEngine.Debug:LogError(Object) CSharpLatest.CsProjectPostprocessor:UpdateProjectFile(String, String) CSharpLatest.CsProjectPostprocessor:OnGeneratedCSProjectFiles() UnityEditor.SyncVS:SyncVisualStudioProjectIfItAlreadyExists()

As a result LangVersion is still stucked on 7.

How to reproduce:

  1. Add incremental compiler, at the moment I had "com.unity.incrementalcompiler": "0.0.42-preview.24",
  2. Import package with newest version of your app
  3. Console window is spammed with prevoiusly described messages
  4. Assembly(...).csproj is still at LangVersion 7, because bug still exist

Maybe that can help https://stackoverflow.com/questions/29915467/there-is-no-unicode-byte-order-mark-cannot-switch-to-unicode

tkaczz commented 6 years ago

After few hours of baby steps I produced code that works (almost?) correctly

private static void UpdateProjectFile(string csproj, string csver)
{
    StreamReader streamRead = null;
    StreamWriter streamWrite = null;

    try
    {
        streamRead = new StreamReader(csproj);
        XDocument xdoc = XDocument.Load(streamRead);
        streamRead.Close();

        if(ChangeOrSetProperty(xdoc.Root, xdoc.Root?.Name.NamespaceName, "LangVersion", csver)) {
            streamWrite = new StreamWriter(csproj);
            xdoc.Save(streamWrite);
            streamWrite.Close();
        }

    }
    catch (Exception e)
    {
        Debug.LogError(e);
    }
    finally 
    {
        streamRead?.Close();
        streamWrite?.Close();
    }
}

There is one bug. In original csproj file, encoding in header is set to utf-16 but file is written in utf-8 BOM format. Function that I posted sets the header to utf-8, and writes file with utf-8 format. At least a good start.