1- using BepInEx ;
2- using BepInEx . Logging ;
3- using Mono . Cecil ;
1+ using Mono . Cecil ;
42using NetcodePatcher . CodeGen ;
53using System ;
64using System . Collections . Generic ;
@@ -16,56 +14,114 @@ namespace NetcodePatcher
1614{
1715 public static class Patcher
1816 {
19- // when ran from command line
20-
17+
2118 public static void Main ( string [ ] args )
2219 {
23- // if not enough args
20+ // check if enough args, otherwise print usage
2421 if ( args . Length < 2 )
2522 {
26- // print usage
27- Console . WriteLine ( "Usage: NetcodePatcher.dll <pluginPath> <managedPath>" ) ;
23+ Console . WriteLine ( "Usage: NetcodePatcher.exe <pluginPath> <managedPath>" ) ;
2824 return ;
2925 }
3026
27+ // get paths from args
28+ string pluginPath = args [ 0 ] ;
29+ string managedPath = args [ 1 ] ;
3130
32- var pluginPath = args [ 0 ] ;
33- var managedPath = args [ 1 ] ;
34- // initialize
35- Patch ( pluginPath , managedPath ) ;
31+ // patch
32+ NetcodePatcher . Patcher . Patch ( pluginPath , managedPath ) ;
3633 }
37-
38- public static IEnumerable < string > TargetDLLs
34+ public class Logging
3935 {
40- get
36+ private readonly object lockObject = new object ( ) ;
37+ public string filePath ;
38+
39+ public Logging ( string fileName )
4140 {
42- return Patcher . CollectTargetDLLs ( ) ;
41+ // set filepath to assembly location + filename
42+ filePath = Path . Combine ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) , fileName ) ;
43+
44+ // Use lock to ensure only one instance is modifying the file at a time
45+ lock ( lockObject )
46+ {
47+ // if file exists, empty file, else create file
48+ if ( File . Exists ( filePath ) )
49+ {
50+ File . WriteAllText ( filePath , "" ) ;
51+ }
52+ else
53+ {
54+ File . Create ( filePath ) . Close ( ) ; // Close the FileStream to release the file lock
55+ }
56+ }
4357 }
44- }
4558
46- public static void Initialize ( )
47- {
48- Patch ( ) ;
59+ public void LogMessage ( string message )
60+ {
61+ // Use lock to ensure only one instance is modifying the file at a time
62+ lock ( lockObject )
63+ {
64+ // write message to file
65+ File . AppendAllText ( filePath , $ "{ message } \r \n ") ;
66+ }
67+
68+ // print message to console
69+ Console . WriteLine ( message ) ;
70+ }
71+
72+ public void LogWarning ( string message )
73+ {
74+ // Use lock to ensure only one instance is modifying the file at a time
75+ lock ( lockObject )
76+ {
77+ // write message to file
78+ File . AppendAllText ( filePath , $ "[Warning]: { message } \r \n ") ;
79+ }
80+
81+ // print message to console
82+ Console . WriteLine ( $ "[Warning]: { message } ") ;
83+ }
84+
85+ public void LogError ( string message )
86+ {
87+ // Use lock to ensure only one instance is modifying the file at a time
88+ lock ( lockObject )
89+ {
90+ // write message to file
91+ File . AppendAllText ( filePath , $ "[Error]: { message } \r \n ") ;
92+ }
93+
94+ // print message to console
95+ Console . WriteLine ( $ "[Error]: { message } ") ;
96+ }
97+
98+ public void LogInfo ( string message )
99+ {
100+ // Use lock to ensure only one instance is modifying the file at a time
101+ lock ( lockObject )
102+ {
103+ // write message to file
104+ File . AppendAllText ( filePath , $ "[Info]: { message } \r \n ") ;
105+ }
106+
107+ // print message to console
108+ Console . WriteLine ( $ "[Info]: { message } ") ;
109+ }
49110 }
50111
51- public static void Patch ( string pluginPath = null , string managedPath = null )
112+ public static void Patch ( string pluginPath , string managedPath )
52113 {
53114 Patcher . Logger . LogMessage ( "Initializing NetcodePatcher" ) ;
54115 HashSet < string > hashSet = new HashSet < string > ( ) ;
55- List < string > references = managedPath == null ? new List < string > ( ) {
56- Paths . ManagedPath + "\\ Unity.Netcode.Runtime.dll" ,
57- Paths . ManagedPath + "\\ UnityEngine.CoreModule.dll" ,
58- Paths . ManagedPath + "\\ Unity.Netcode.Components.dll" ,
59- Paths . ManagedPath + "\\ Unity.Networking.Transport.dll" ,
60- } : new List < string > ( )
116+ List < string > references = new List < string > ( )
61117 {
62118 managedPath + "\\ Unity.Netcode.Runtime.dll" ,
63119 managedPath + "\\ UnityEngine.CoreModule.dll" ,
64120 managedPath + "\\ Unity.Netcode.Components.dll" ,
65121 managedPath + "\\ Unity.Networking.Transport.dll" ,
66122 } ;
67123
68- foreach ( string text3 in Directory . GetFiles ( pluginPath != null ? pluginPath : Paths . PluginPath , "*.dll" , SearchOption . AllDirectories ) )
124+ foreach ( string text3 in Directory . GetFiles ( pluginPath , "*.dll" , SearchOption . AllDirectories ) )
69125 {
70126 string fileName = Path . GetFileName ( text3 ) ;
71127 if ( ! fileName . ToLower ( ) . Contains ( "mmhook" ) )
@@ -77,7 +133,7 @@ public static void Patch(string pluginPath = null, string managedPath = null)
77133 if ( typeDefinition . BaseType != null )
78134 {
79135 ; // check if subclass of NetworkBehaviour
80- if ( typeDefinition . IsSubclassOf ( typeof ( NetworkBehaviour ) . FullName ) || typeDefinition . HasInterface ( typeof ( INetworkMessage ) . FullName ) || typeDefinition . HasInterface ( typeof ( INetworkSerializable ) . FullName ) )
136+ if ( typeDefinition . IsSubclassOf ( typeof ( NetworkBehaviour ) . FullName ) )
81137 {
82138
83139 hashSet . Add ( text3 ) ;
@@ -89,6 +145,7 @@ public static void Patch(string pluginPath = null, string managedPath = null)
89145 }
90146 foreach ( string text4 in hashSet )
91147 {
148+ var success = true ;
92149 try
93150 {
94151 Patcher . Logger . LogMessage ( "Patching : " + Path . GetFileName ( text4 ) ) ;
@@ -98,31 +155,31 @@ public static void Patch(string pluginPath = null, string managedPath = null)
98155 // replace || with new line
99156 warning = warning . Replace ( "|| " , "\r \n " ) . Replace ( "||" , " " ) ;
100157 Patcher . Logger . LogWarning ( $ "Warning when patching ({ Path . GetFileName ( text4 ) } ): { warning } ") ;
158+ success = false ;
101159 } ,
102160 ( error ) =>
103161 {
104162 error = error . Replace ( "|| " , "\r \n " ) . Replace ( "||" , " " ) ;
105163 Patcher . Logger . LogError ( $ "Error when patching ({ Path . GetFileName ( text4 ) } ): { error } ") ;
164+ success = false ;
106165 } ) ;
107166
108167 }
109168 catch ( Exception exception )
110169 {
111170 // error
112171 Patcher . Logger . LogWarning ( $ "Did not patch ({ Path . GetFileName ( text4 ) } ): { exception . Message } (Already patched?)") ;
172+ success = false ;
113173 }
114- }
115- }
116174
117- public static void Patch ( AssemblyDefinition _ )
118- {
119- }
175+ if ( success )
176+ {
177+ Patcher . Logger . LogMessage ( $ "Patched ({ Path . GetFileName ( text4 ) } ) successfully") ;
178+ }
179+ }
120180
121- private static IEnumerable < string > CollectTargetDLLs ( )
122- {
123- return new List < string > ( ) ;
124181 }
125182
126- private static readonly ManualLogSource Logger = BepInEx . Logging . Logger . CreateLogSource ( "NetcodePatcher" ) ;
183+ public static Logging Logger = new Logging ( "NetcodePatcher.log " ) ;
127184 }
128185}
0 commit comments