1+ using System ;
2+ using System . Diagnostics ;
3+ using System . IO ;
4+ using UnityEditor ;
5+ using UnityEngine ;
6+
7+ namespace Editor
8+ {
9+ [ InitializeOnLoad ]
10+ public static class PycResetter
11+ {
12+ private const string GitCommand = "git.exe" ;
13+ private const string MenuItem = "Tools/Restore python files" ;
14+
15+ private static bool _checked = true ;
16+
17+ static PycResetter ( )
18+ {
19+ Menu . SetChecked ( MenuItem , _checked ) ;
20+ }
21+
22+ [ MenuItem ( MenuItem ) ]
23+ private static void RegisterMenu ( )
24+ {
25+ _checked = ! _checked ;
26+ Menu . SetChecked ( MenuItem , _checked ) ;
27+ }
28+
29+ [ InitializeOnEnterPlayMode ]
30+ private static void RegisterOnPlayModeStateChanged ( )
31+ {
32+ if ( ! _checked )
33+ {
34+ return ;
35+ }
36+
37+ if ( CanExecuteCommand ( GitCommand ) ) { }
38+ {
39+ var cwd = new DirectoryInfo ( $ "{ Application . dataPath } ") . Parent . FullName ;
40+ GitRestore ( cwd ) ;
41+ GitClean ( cwd ) ;
42+
43+ void OnPlayModeStateChanged ( PlayModeStateChange change )
44+ {
45+ if ( change == PlayModeStateChange . ExitingPlayMode )
46+ {
47+ GitRestore ( cwd ) ;
48+ GitClean ( cwd ) ;
49+ EditorApplication . playModeStateChanged -= OnPlayModeStateChanged ;
50+ }
51+ }
52+
53+ EditorApplication . playModeStateChanged += OnPlayModeStateChanged ;
54+ }
55+ }
56+
57+ private static void GitRestore ( string cwd )
58+ {
59+ var process = new Process
60+ {
61+ StartInfo = new ProcessStartInfo
62+ {
63+ FileName = GitCommand ,
64+ ArgumentList = { "restore" , "Assets/StreamingAssets/python-3.11.3-embed-amd64/*" } ,
65+ UseShellExecute = false ,
66+ RedirectStandardOutput = false ,
67+ RedirectStandardInput = false ,
68+ RedirectStandardError = false ,
69+ CreateNoWindow = true ,
70+ WorkingDirectory = cwd
71+ }
72+ } ;
73+ process . Start ( ) ;
74+ process . Dispose ( ) ;
75+ }
76+
77+ private static void GitClean ( string cwd )
78+ {
79+ var targetFolders = new [ ]
80+ {
81+ "Assets/StreamingAssets/python-3.11.3-embed-amd64/*" ,
82+ "Assets/StreamingAssets/myproject/*" ,
83+ "Assets/StreamingAssets/test_package/*"
84+ } ;
85+ foreach ( string targetFolder in targetFolders )
86+ {
87+ var process = new Process
88+ {
89+ StartInfo = new ProcessStartInfo
90+ {
91+ FileName = GitCommand ,
92+ ArgumentList = { "clean" , "-dfx" , targetFolder } ,
93+ UseShellExecute = false ,
94+ RedirectStandardOutput = false ,
95+ RedirectStandardInput = false ,
96+ RedirectStandardError = false ,
97+ CreateNoWindow = true ,
98+ WorkingDirectory = cwd
99+ }
100+ } ;
101+ process . Start ( ) ;
102+ process . Dispose ( ) ;
103+ }
104+ }
105+
106+ private static bool CanExecuteCommand ( string command )
107+ {
108+ if ( ! command . EndsWith ( ".exe" ) && ! command . EndsWith ( ".com" ) )
109+ {
110+ command += ".exe" ;
111+ }
112+
113+ var path = Environment . GetEnvironmentVariable ( "PATH" ) . Split ( ";" ) ;
114+ foreach ( string p in path )
115+ {
116+ var fullPath = Path . Combine ( p , command ) ;
117+ if ( File . Exists ( fullPath ) )
118+ {
119+ return true ;
120+ }
121+ }
122+ return false ;
123+ }
124+ }
125+ }
0 commit comments