unitysetup.powershell icon indicating copy to clipboard operation
unitysetup.powershell copied to clipboard

Loading modules.json with the wrong encoding

Open yaegashi opened this issue 3 years ago • 1 comments

After installing Unity Editor 2019.4.15f1 with Unity Hub 2.4.2 on Japanese Windows 10 Pro 2004, Get-UnitySetupInstance complains with the following error:

PS C:\Program Files\Unity\Hub\Editor\2019.4.15f1> Get-UnitySetupInstance
警告: ':' または '}' ではなく無効なオブジェクトが渡されました。 (7767): [{"id":"visualstudio","name":"Microsoft Visual Studio Community 2019","description":"Downloads and installs.... (snipped)

It orignates from this line:

https://github.com/microsoft/unitysetup.powershell/blob/602fb720ea42220dae7a85b5b63cbc5538ff06a4/UnitySetup/UnitySetup.psm1#L1236

Get-Content -Raw loads modules.json with the wrong encoding which causes subsequent ConvertFrom-Json to fail:

PS C:\Program Files\Unity\Hub\Editor\2019.4.15f1> Get-Content .\modules.json -Raw | ConvertFrom-Json
ConvertFrom-Json : ':' または '}' ではなく無効なオブジェクトが渡されました。 (7767): [{"id":"visualstudio","name":"Microsoft Visual Studio Community 2019","description":"Downloads and installs.... (snipped)

modules.json is actually in UTF-8 without BOM. Get-Content -Encoding UTF8 would workaround this case, but I'm not confident whether it's a correct fix:

PS C:\Program Files\Unity\Hub\Editor\2019.4.15f1> Get-Content .\modules.json -Encoding UTF8 | ConvertFrom-Json


id            : visualstudio
name          : Microsoft Visual Studio Community 2019
description   : Downloads and installs Microsoft Visual Studio Community 2019. By installing this you accept the license terms for Visual Studio.
downloadUrl   : https://go.microsoft.com/fwlink/?linkid=2086755
category      : Dev tools
installedSize : 1331200000
downloadSize  : 1363148800
visible       : True
selected      : False
cmd           : --productId "Microsoft.VisualStudio.Product.Community" --add "Microsoft.VisualStudio.Workload.ManagedGame" --add "Microsoft.VisualStudio.Workload.NativeDesktop" --add "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" --add "Microsoft.VisualStudio.Comp
                onent.Windows10SDK.16299.Desktop" --campaign "Unity3d_Unity" --passive --norestart --wait
eulaUrl1      : https://go.microsoft.com/fwlink/?linkid=2092534
eulaLabel1    : Visual Studio 2019 Community License Terms
eulaMessage   : Please review and accept the license terms before downloading and installing Microsoft Visual Studio.

...(snipped)...

id            : language-ja
name          : 日本語
description   : 日本語
downloadUrl   : https://new-translate.unity3d.jp/v1/live/54/2019.4/ja
category      : Language packs (Preview)
visible       : True
selected      : False
destination   : {UNITY_PATH}/Editor/Data/Localization
installedSize : 1739231
downloadSize  : 1739231

...(snipped)

It would be better to have a fall back path on modules.json parsing failures by adding a try-catch block.

yaegashi avatar Nov 29 '20 05:11 yaegashi

The try/catch would be a great/small PR =). In newer versions of PowerShell the default value is UTF8NoBOM so that may be a viable work around for some.

If you make a PR with the change to being explicit about UTF8 I'd be more than willing to review and try it out on my side. =)

jwittner avatar Nov 30 '20 18:11 jwittner