1- #pragma warning disable 0649
2- using HTC . UnityPlugin . UPMRegistryTool . SimpleJSON ;
1+ #if UNITY_2018_1_OR_NEWER
2+ using HTC . UnityPlugin . UPMRegistryTool . Editor . Utils ;
33using System ;
4- using System . Collections . Generic ;
5- using System . IO ;
64using System . Text . RegularExpressions ;
75using UnityEngine ;
86
9- namespace HTC . UnityPlugin . UPMRegistryTool
7+ namespace HTC . UnityPlugin . UPMRegistryTool . Editor . Configs
108{
11- [ CreateAssetMenu ( menuName = "RegistryToolSettings" , fileName = "RegistryToolSettings" ) ]
129 public class RegistryToolSettings : ScriptableObject
1310 {
14- [ Serializable ]
15- public struct ScopedRegistry
16- {
17- public string name ;
18- public string url ;
19- public List < string > scopes ;
20-
21- public bool Equals ( ScopedRegistry otherInfo )
22- {
23- if ( name != otherInfo . name || url != otherInfo . url ) { return false ; }
24-
25- if ( scopes == null || otherInfo . scopes == null )
26- {
27- if ( scopes == null && otherInfo . scopes == null ) { return true ; }
28- return false ;
29- }
30-
31- if ( scopes . Count != otherInfo . scopes . Count ) { return false ; }
32-
33- for ( int i = 0 , imax = scopes . Count ; i < imax ; i ++ )
34- {
35- if ( scopes [ i ] != otherInfo . scopes [ i ] )
36- {
37- return false ;
38- }
39- }
40-
41- return true ;
42- }
43- }
44-
45- [ SerializeField ]
46- public string ProjectManifestPath ;
47- [ SerializeField ]
48- public string LicenseResourcePath ;
49- [ SerializeField ]
50- public ScopedRegistry ViveRegistry ;
51-
52- [ NonSerialized ]
53- public string ViveRegistryHost ;
54- [ NonSerialized ]
55- public int ViveRegistryPort ;
56-
5711 private const string RESOURCES_PATH = "RegistryToolSettings" ;
58- private static RegistryToolSettings instance ;
59-
60- public static RegistryToolSettings Instance
61- {
62- get
63- {
64- if ( instance == null )
65- {
66- instance = Resources . Load < RegistryToolSettings > ( RESOURCES_PATH ) ;
67-
68- if ( instance != null )
69- {
70- var match = Regex . Match ( instance . ViveRegistry . url , @"^https?:\/\/(.+?)(?::(\d+))?\/?$" ) ;
71- instance . ViveRegistryHost = match . Success && match . Groups . Count > 1 ? match . Groups [ 1 ] . Value : default ( string ) ;
72-
73- if ( ! int . TryParse ( match . Groups [ 2 ] . Value , out instance . ViveRegistryPort ) ) { instance . ViveRegistryPort = 80 ; }
74- }
75- else
76- {
77- Debug . LogErrorFormat ( "RegistrySettings.asset not found. ({0})" , RESOURCES_PATH ) ;
78- }
79- }
80-
81- return instance ;
82- }
83- }
84-
85- public static bool IsRegistryExists ( ScopedRegistry reg )
86- {
87- JSONNode manifestNode ;
88- if ( ! LoadManifestNode ( out manifestNode ) ) { return false ; }
89-
90- return IsRegistryExists ( manifestNode , reg ) ;
91- }
92-
93- public static void AddRegistry ( ScopedRegistry reg )
94- {
95- JSONNode manifestNode ;
96- if ( ! LoadManifestNode ( out manifestNode ) ) { return ; }
97-
98- int nameMatchIndex ;
99- if ( ! IsRegistryExists ( manifestNode , reg , out nameMatchIndex ) )
100- {
101- if ( nameMatchIndex < 0 )
102- {
103- AddRegistry ( EnsureRegistriesNode ( manifestNode ) , reg ) ;
104- }
105- else
106- {
107- AddRegistryAt ( EnsureRegistriesNode ( manifestNode ) , reg , nameMatchIndex ) ;
108- }
109- }
110-
111- SaveManifestNode ( manifestNode ) ;
112- }
113-
114- public static void RemoveRegistry ( ScopedRegistry reg )
115- {
116- JSONNode manifestNode ;
117- if ( ! LoadManifestNode ( out manifestNode ) ) { return ; }
118-
119- int nameMatchIndex ;
120- IsRegistryExists ( manifestNode , reg , out nameMatchIndex ) ;
121- if ( nameMatchIndex > 0 )
122- {
123- RemoveRegistryAt ( manifestNode , nameMatchIndex ) ;
124- }
125-
126- SaveManifestNode ( manifestNode ) ;
127- }
128-
129- private static bool IsRegistryExists ( JSONNode manifestNode , ScopedRegistry reg )
130- {
131- int i ;
132- return IsRegistryExists ( manifestNode , reg , out i ) ;
133- }
134-
135- private static bool IsRegistryExists ( JSONNode manifestNode , ScopedRegistry reg , out int index )
136- {
137- index = - 1 ;
138- var regsNode = manifestNode [ "scopedRegistries" ] ;
139-
140- if ( ! regsNode . IsArray ) { return false ; }
141- if ( regsNode . Count == 0 ) { return false ; }
14212
143- for ( int i = 0 , imax = regsNode . Count ; i < imax ; ++ i )
144- {
145- var regNode = regsNode [ i ] ;
146- var nameNode = regNode [ "name" ] ;
147- var urlNode = regNode [ "url" ] ;
148- var scopesNode = regNode [ "scopes" ] ;
149-
150- if ( ! nameNode . IsString || nameNode . Value != reg . name ) { continue ; }
151- index = i ;
152-
153- if ( ! urlNode . IsString || urlNode . Value != reg . url ) { break ; }
154- if ( ! scopesNode . IsArray ) { break ; }
155- if ( scopesNode . Count != ( reg . scopes == null ? 0 : reg . scopes . Count ) ) { break ; }
156-
157- var allMatch = true ;
158- for ( int j = scopesNode . Count - 1 ; j >= 0 ; -- j )
159- {
160- var scopeNode = scopesNode [ j ] ;
161- if ( ! scopeNode . IsString ) { allMatch = false ; break ; }
162- if ( scopeNode . Value != reg . scopes [ j ] ) { allMatch = false ; break ; }
163- }
164- return allMatch ;
165- }
13+ private static RegistryToolSettings PrivateInstance ;
16614
167- return false ;
168- }
169-
170- private static JSONArray EnsureRegistriesNode ( JSONNode manifestNode )
171- {
172- var regsNode = manifestNode [ "scopedRegistries" ] ;
173- if ( ! regsNode . IsArray )
174- {
175- manifestNode . Add ( "scopedRegistries" , regsNode = new JSONArray ( ) ) ;
176- }
177- return regsNode . AsArray ;
178- }
15+ public string ProjectManifestPath ;
16+ public RegistryInfo Registry ;
17917
180- private static void AddRegistry ( JSONArray regsNode , ScopedRegistry reg )
181- {
182- if ( regsNode . Count == 0 ) { regsNode . Add ( new JSONObject ( ) ) ; }
183- AddRegistryAt ( regsNode , reg , regsNode . Count - 1 ) ;
184- }
18+ [ NonSerialized ] public string RegistryHost ;
19+ [ NonSerialized ] public int RegistryPort ;
18520
186- private static void AddRegistryAt ( JSONArray regsNode , ScopedRegistry reg , int index )
21+ public static RegistryToolSettings Instance ( )
18722 {
188- var regNode = regsNode [ index ] ;
189- var nameNode = regNode [ "name" ] ;
190- var urlNode = regNode [ "url" ] ;
191- var scopesNode = regNode [ "scopes" ] ;
192-
193- if ( nameNode . IsString ) { nameNode . Value = reg . name ; }
194- else { regNode . Add ( "name" , new JSONString ( reg . name ) ) ; }
195-
196- if ( urlNode . IsString ) { urlNode . Value = reg . url ; }
197- else { regNode . Add ( "url" , new JSONString ( reg . url ) ) ; }
198-
199- if ( ! scopesNode . IsArray ) { regNode . Add ( "scopes" , scopesNode = new JSONArray ( ) ) ; }
200-
201- int i = 0 ;
202- foreach ( var scope in reg . scopes )
23+ if ( PrivateInstance == null )
20324 {
204- var scopeNode = scopesNode [ i ] ;
205- if ( scopeNode . IsString ) { scopeNode . Value = scope ; }
206- else { scopesNode [ i ] = new JSONString ( scope ) ; }
207- ++ i ;
25+ PrivateInstance = Resources . Load < RegistryToolSettings > ( RESOURCES_PATH ) ;
26+ PrivateInstance . Init ( ) ;
20827 }
20928
210- while ( scopesNode . Count > i ) { scopesNode . Remove ( scopesNode . Count - 1 ) ; }
29+ return PrivateInstance ;
21130 }
21231
213- private static void RemoveRegistryAt ( JSONNode manifestNode , int index )
32+ private void Init ( )
21433 {
215- manifestNode [ "scopedRegistries" ] . Remove ( index ) ;
216- }
217-
218- private static bool LoadManifestNode ( out JSONNode node )
219- {
220- node = default ( JSONNode ) ;
34+ Match match = Regex . Match ( Registry . Url , @"^https?:\/\/(.+?)(?::(\d+))?\/?$" ) ;
35+ RegistryHost = match . Groups [ 1 ] . Value ;
22136
222- if ( Instance == null ) { return false ; }
223-
224- var json = File . ReadAllText ( instance . ProjectManifestPath ) ;
225- node = JSON . Parse ( json ) ;
226- if ( ! node . IsObject )
37+ int port = 0 ;
38+ RegistryPort = 80 ;
39+ if ( int . TryParse ( match . Groups [ 2 ] . Value , out port ) )
22740 {
228- Debug . LogError ( "Parse pkg manifest from " + instance . ProjectManifestPath + " faild, json=\n " + json , instance ) ;
229- return false ;
41+ RegistryPort = port ;
23042 }
231-
232- return true ;
233- }
234-
235- private static void SaveManifestNode ( JSONNode node )
236- {
237- if ( Instance == null ) { return ; }
238-
239- File . WriteAllText ( instance . ProjectManifestPath , node . ToString ( 2 ) ) ;
24043 }
24144 }
242- }
45+ }
46+ #endif
0 commit comments