Skip to content

Commit

Permalink
feat(ma/ndmf): inconsistency script recompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
poi-vrc committed Apr 23, 2024
1 parent 4241a8c commit 03ce989
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Editor/Detail/NDMF/ScriptRecompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2024 chocopoi
*
* This file is part of DressingFramework.
*
* DressingFramework is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* DressingFramework is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DressingFramework. If not, see <https://www.gnu.org/licenses/>.
*/
#if UNITY_EDITOR
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

namespace Chocopoi.DressingFramework.Detail.NDMF
{
[InitializeOnLoad]
public static class ScriptRecompiler
{
private const string NDMFPackageName = "nadena.dev.ndmf";
private const string MAPackageName = "nadena.dev.modular-avatar";
private const string TempFileName = "Temp/com.chocopoi.vrc.dressingframework.ScriptRecompiler.lastRecompileTime.txt";

private static ListRequest s_listReq = null;

Check warning on line 31 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L31

Added line #L31 was not covered by tests

static ScriptRecompiler()
{
s_listReq = Client.List();
EditorApplication.update += OnEditorUpdate;
}

Check warning on line 37 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L34-L37

Added lines #L34 - L37 were not covered by tests

private static bool CheckNDMFInconsistency(PackageCollection pkgs)
{

Check warning on line 40 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L40

Added line #L40 was not covered by tests
#if DK_NDMF
return !pkgs.Any(p => p.name == NDMFPackageName);

Check warning on line 42 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L42

Added line #L42 was not covered by tests
#else
return pkgs.Any(p => p.name == NDMFPackageName);

Check warning on line 44 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L44

Added line #L44 was not covered by tests
#endif
}

Check warning on line 46 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L46

Added line #L46 was not covered by tests

private static bool CheckMAInconsistency(PackageCollection pkgs)
{

Check warning on line 49 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L49

Added line #L49 was not covered by tests
#if DK_MA
return !pkgs.Any(p => p.name == MAPackageName);

Check warning on line 51 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L51

Added line #L51 was not covered by tests
#else
return pkgs.Any(p => p.name == MAPackageName);

Check warning on line 53 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L53

Added line #L53 was not covered by tests
#endif
}

Check warning on line 55 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L55

Added line #L55 was not covered by tests

private static void WriteLastRecompileTime()
{
var writer = new StreamWriter(TempFileName);
writer.WriteLine(DateTime.Now.ToString());
writer.Close();
}

Check warning on line 62 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L58-L62

Added lines #L58 - L62 were not covered by tests

private static bool IsLastRecompileMinuteAgo()
{
if (!File.Exists(TempFileName))
{
return false;

Check warning on line 68 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L65-L68

Added lines #L65 - L68 were not covered by tests
}

var reader = new StreamReader(TempFileName);
var dateTimeLine = reader.ReadLine();
reader.Close();

Check warning on line 73 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L71-L73

Added lines #L71 - L73 were not covered by tests
try
{
var dateTime = DateTime.Parse(dateTimeLine);
if ((DateTime.Now - dateTime).TotalMinutes > 1)
{
return true;

Check warning on line 79 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L75-L79

Added lines #L75 - L79 were not covered by tests
}
}
catch
{
Debug.Log("Unable to parse data time from last recompile time file.");
File.Delete(TempFileName);
return false;

Check warning on line 86 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L81-L86

Added lines #L81 - L86 were not covered by tests
}
return false;
}

Check warning on line 89 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L88-L89

Added lines #L88 - L89 were not covered by tests

private static void OnEditorUpdate()
{
if (!s_listReq.IsCompleted)
{
return;

Check warning on line 95 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L92-L95

Added lines #L92 - L95 were not covered by tests
}

if (CheckNDMFInconsistency(s_listReq.Result) || CheckMAInconsistency(s_listReq.Result))
{
if (IsLastRecompileMinuteAgo())
{
Debug.LogError("[DressingFramework] Version defines and package presence inconsistency detected, but last recompile was just a minute ago. Probably there is a compilation error? Recompilation is not requested to prevent endless loop.");
}

Check warning on line 103 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L98-L103

Added lines #L98 - L103 were not covered by tests
else
{
Debug.Log("[DressingFramework] Version defines and package presence inconsistency detected, requesting recompilation");
WriteLastRecompileTime();
CompilationPipeline.RequestScriptCompilation();
}
}

Check warning on line 110 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L105-L110

Added lines #L105 - L110 were not covered by tests

EditorApplication.update -= OnEditorUpdate;
}

Check warning on line 113 in Editor/Detail/NDMF/ScriptRecompiler.cs

View check run for this annotation

Codecov / codecov/patch

Editor/Detail/NDMF/ScriptRecompiler.cs#L112-L113

Added lines #L112 - L113 were not covered by tests
}
}
#endif
11 changes: 11 additions & 0 deletions Editor/Detail/NDMF/ScriptRecompiler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03ce989

Please sign in to comment.